コード例 #1
0
 private void uninstallTracking() {
   InputMap currMap = SwingUtilities.getUIInputMap(this.jcomp, JComponent.WHEN_FOCUSED);
   if (currMap != null) {
     InputMap newMap = new InputMap();
     KeyStroke[] kss = currMap.allKeys();
     for (int i = 0; i < kss.length; i++) {
       KeyStroke stroke = kss[i];
       Object val = currMap.get(stroke);
       if (stroke.equals(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0))
           && "flipTextSelection".equals(val)) {
         continue;
       }
       newMap.put(stroke, val);
     }
     SwingUtilities.replaceUIInputMap(this.jcomp, JComponent.WHEN_FOCUSED, newMap);
   }
   this.jcomp.getActionMap().remove("flipTextSelection");
 }
コード例 #2
0
  private void installTracking() {
    InputMap currMap = SwingUtilities.getUIInputMap(this.jcomp, JComponent.WHEN_FOCUSED);

    InputMap newMap = new InputMap();
    if (currMap != null) {
      KeyStroke[] kss = currMap.allKeys();
      for (int i = 0; i < kss.length; i++) {
        KeyStroke stroke = kss[i];
        Object val = currMap.get(stroke);
        newMap.put(stroke, val);
      }
    }

    newMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "flipTextSelection");

    this.jcomp
        .getActionMap()
        .put(
            "flipTextSelection",
            new AbstractAction() {
              public void actionPerformed(ActionEvent e) {
                SwingUtilities.invokeLater(
                    new Runnable() {
                      public void run() {
                        int selectionLength = jcomp.getSelectionEnd() - jcomp.getSelectionStart();
                        if (selectionLength == 0) {
                          jcomp.selectAll();
                        } else {
                          int lastPos = jcomp.getSelectionEnd();
                          jcomp.setSelectionStart(0);
                          jcomp.setSelectionEnd(0);
                          jcomp.setCaretPosition(lastPos);
                        }
                      }
                    });
              }
            });

    SwingUtilities.replaceUIInputMap(this.jcomp, JComponent.WHEN_FOCUSED, newMap);
  }