44 |
import java.awt.Insets; |
import java.awt.Insets; |
45 |
import java.awt.Point; |
import java.awt.Point; |
46 |
import java.awt.Rectangle; |
import java.awt.Rectangle; |
47 |
|
import java.awt.datatransfer.Clipboard; |
48 |
|
import java.awt.datatransfer.DataFlavor; |
49 |
|
import java.awt.datatransfer.StringSelection; |
50 |
|
import java.awt.datatransfer.Transferable; |
51 |
|
import java.awt.datatransfer.UnsupportedFlavorException; |
52 |
|
import java.awt.event.ActionEvent; |
53 |
import java.awt.event.InputMethodListener; |
import java.awt.event.InputMethodListener; |
54 |
import java.awt.event.KeyEvent; |
import java.awt.event.KeyEvent; |
55 |
|
import java.io.IOException; |
56 |
import java.util.Enumeration; |
import java.util.Enumeration; |
57 |
import java.util.Hashtable; |
import java.util.Hashtable; |
58 |
|
|
68 |
import javax.swing.JViewport; |
import javax.swing.JViewport; |
69 |
import javax.swing.KeyStroke; |
import javax.swing.KeyStroke; |
70 |
import javax.swing.Scrollable; |
import javax.swing.Scrollable; |
71 |
|
import javax.swing.TransferHandler; |
72 |
import javax.swing.UIManager; |
import javax.swing.UIManager; |
73 |
import javax.swing.event.CaretEvent; |
import javax.swing.event.CaretEvent; |
74 |
import javax.swing.event.CaretListener; |
import javax.swing.event.CaretListener; |
554 |
{ |
{ |
555 |
parent = p; |
parent = p; |
556 |
} |
} |
557 |
|
} |
558 |
|
|
559 |
|
class DefaultTransferHandler |
560 |
|
extends TransferHandler |
561 |
|
{ |
562 |
|
public boolean canImport(JComponent component, DataFlavor[] flavors) |
563 |
|
{ |
564 |
|
JTextComponent textComponent = (JTextComponent) component; |
565 |
|
|
566 |
|
if (! (textComponent.isEnabled() |
567 |
|
&& textComponent.isEditable() |
568 |
|
&& flavors != null)) |
569 |
|
return false; |
570 |
|
|
571 |
|
for (int i = 0; i < flavors.length; ++i) |
572 |
|
if (flavors[i].equals(DataFlavor.stringFlavor)) |
573 |
|
return true; |
574 |
|
|
575 |
|
return false; |
576 |
|
} |
577 |
|
|
578 |
|
public void exportToClipboard(JComponent component, Clipboard clipboard, |
579 |
|
int action) |
580 |
|
{ |
581 |
|
JTextComponent textComponent = (JTextComponent) component; |
582 |
|
int start = textComponent.getSelectionStart(); |
583 |
|
int end = textComponent.getSelectionEnd(); |
584 |
|
|
585 |
|
if (start == end) |
586 |
|
return; |
587 |
|
|
588 |
|
try |
589 |
|
{ |
590 |
|
// Copy text to clipboard. |
591 |
|
String data = textComponent.getDocument().getText(start, end); |
592 |
|
StringSelection selection = new StringSelection(data); |
593 |
|
clipboard.setContents(selection, null); |
594 |
|
|
595 |
|
// Delete selected text on cut action. |
596 |
|
if (action == MOVE) |
597 |
|
doc.remove(start, end - start); |
598 |
|
} |
599 |
|
catch (BadLocationException e) |
600 |
|
{ |
601 |
|
// Ignore this and do nothing. |
602 |
|
} |
603 |
|
} |
604 |
|
|
605 |
|
public int getSourceActions() |
606 |
|
{ |
607 |
|
return NONE; |
608 |
|
} |
609 |
|
|
610 |
|
public boolean importData(JComponent component, Transferable transferable) |
611 |
|
{ |
612 |
|
DataFlavor flavor = null; |
613 |
|
DataFlavor[] flavors = transferable.getTransferDataFlavors(); |
614 |
|
|
615 |
|
if (flavors == null) |
616 |
|
return false; |
617 |
|
|
618 |
|
for (int i = 0; i < flavors.length; ++i) |
619 |
|
if (flavors[i].equals(DataFlavor.stringFlavor)) |
620 |
|
flavor = flavors[i]; |
621 |
|
|
622 |
|
if (flavor == null) |
623 |
|
return false; |
624 |
|
|
625 |
|
try |
626 |
|
{ |
627 |
|
JTextComponent textComponent = (JTextComponent) component; |
628 |
|
String data = (String) transferable.getTransferData(flavor); |
629 |
|
textComponent.replaceSelection(data); |
630 |
|
return true; |
631 |
|
} |
632 |
|
catch (IOException e) |
633 |
|
{ |
634 |
|
// Ignored. |
635 |
|
} |
636 |
|
catch (UnsupportedFlavorException e) |
637 |
|
{ |
638 |
|
// Ignored. |
639 |
|
} |
640 |
|
|
641 |
|
return false; |
642 |
|
} |
643 |
} |
} |
644 |
|
|
645 |
private static final long serialVersionUID = -8796518220218978795L; |
private static final long serialVersionUID = -8796518220218978795L; |
647 |
public static final String DEFAULT_KEYMAP = "default"; |
public static final String DEFAULT_KEYMAP = "default"; |
648 |
public static final String FOCUS_ACCELERATOR_KEY = "focusAcceleratorKey"; |
public static final String FOCUS_ACCELERATOR_KEY = "focusAcceleratorKey"; |
649 |
|
|
650 |
|
private static DefaultTransferHandler defaultTransferHandler; |
651 |
private static Hashtable keymaps = new Hashtable(); |
private static Hashtable keymaps = new Hashtable(); |
652 |
private Keymap keymap; |
private Keymap keymap; |
653 |
|
private char focusAccelerator = '\0'; |
654 |
|
private NavigationFilter navigationFilter; |
655 |
|
|
656 |
/** |
/** |
657 |
* Get a Keymap from the global keymap table, by name. |
* Get a Keymap from the global keymap table, by name. |
971 |
} |
} |
972 |
catch (BadLocationException e) |
catch (BadLocationException e) |
973 |
{ |
{ |
974 |
|
// This can never happen. |
975 |
} |
} |
976 |
} |
} |
977 |
|
|
1466 |
{ |
{ |
1467 |
dragEnabled = enabled; |
dragEnabled = enabled; |
1468 |
} |
} |
1469 |
|
|
1470 |
|
public void copy() |
1471 |
|
{ |
1472 |
|
doTransferAction("copy", TransferHandler.getCopyAction()); |
1473 |
|
} |
1474 |
|
|
1475 |
|
public void cut() |
1476 |
|
{ |
1477 |
|
doTransferAction("cut", TransferHandler.getCutAction()); |
1478 |
|
} |
1479 |
|
|
1480 |
|
public void paste() |
1481 |
|
{ |
1482 |
|
doTransferAction("paste", TransferHandler.getPasteAction()); |
1483 |
|
} |
1484 |
|
|
1485 |
|
private void doTransferAction(String name, Action action) |
1486 |
|
{ |
1487 |
|
// Install default TransferHandler if none set. |
1488 |
|
if (getTransferHandler() == null) |
1489 |
|
{ |
1490 |
|
if (defaultTransferHandler == null) |
1491 |
|
defaultTransferHandler = new DefaultTransferHandler(); |
1492 |
|
|
1493 |
|
setTransferHandler(defaultTransferHandler); |
1494 |
|
} |
1495 |
|
|
1496 |
|
// Perform action. |
1497 |
|
ActionEvent event = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, |
1498 |
|
action.getValue(Action.NAME).toString()); |
1499 |
|
action.actionPerformed(event); |
1500 |
|
} |
1501 |
|
|
1502 |
|
public void setFocusAccelerator(char newKey) |
1503 |
|
{ |
1504 |
|
if (focusAccelerator == newKey) |
1505 |
|
return; |
1506 |
|
|
1507 |
|
char oldKey = focusAccelerator; |
1508 |
|
focusAccelerator = newKey; |
1509 |
|
firePropertyChange(FOCUS_ACCELERATOR_KEY, oldKey, newKey); |
1510 |
|
} |
1511 |
|
|
1512 |
|
public char getFocusAccelerator() |
1513 |
|
{ |
1514 |
|
return focusAccelerator; |
1515 |
|
} |
1516 |
|
|
1517 |
|
/** |
1518 |
|
* @since 1.4 |
1519 |
|
*/ |
1520 |
|
public NavigationFilter getNavigationFilter() |
1521 |
|
{ |
1522 |
|
return navigationFilter; |
1523 |
|
} |
1524 |
|
|
1525 |
|
/** |
1526 |
|
* @since 1.4 |
1527 |
|
*/ |
1528 |
|
public void setNavigationFilter(NavigationFilter filter) |
1529 |
|
{ |
1530 |
|
navigationFilter = filter; |
1531 |
|
} |
1532 |
} |
} |