Ejemplo n.º 1
0
  /**
   * Add undo/redo support to a text component. Undo is bound to "ctrl-z", redo to "ctrl-y".
   *
   * @param textcomp The text component.
   */
  public static void addUndoSupport(JTextComponent textcomp) {
    // Based on
    // http://www.exampledepot.com/egs/javax.swing.undo/UndoText.html
    final UndoManager undo = new UndoManager();
    Document doc = textcomp.getDocument();

    // Listen for undo and redo events
    doc.addUndoableEditListener(
        new UndoableEditListener() {
          public void undoableEditHappened(UndoableEditEvent evt) {
            undo.addEdit(evt.getEdit());
          }
        });

    // Create an undo action and add it to the text component
    textcomp
        .getActionMap()
        .put(
            "Undo",
            new AbstractAction("Undo") {
              private static final long serialVersionUID = -1616574389415095169L;

              public void actionPerformed(ActionEvent evt) {
                try {
                  if (undo.canUndo()) {
                    undo.undo();
                  }
                } catch (CannotUndoException e) {
                }
              }
            });

    // Bind the undo action to ctl-Z
    textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo");

    // Create a redo action and add it to the text component
    textcomp
        .getActionMap()
        .put(
            "Redo",
            new AbstractAction("Redo") {
              private static final long serialVersionUID = 58635276936990330L;

              public void actionPerformed(ActionEvent evt) {
                try {
                  if (undo.canRedo()) {
                    undo.redo();
                  }
                } catch (CannotRedoException e) {
                }
              }
            });

    // Bind the redo action to ctl-Y
    textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo");
  }
Ejemplo n.º 2
0
 /**
  * Searches all actions of a JTextComponent for ab action of the given class and returns the first
  * one that matches that class, or null if no Action is found
  *
  * @return Action object of that class or null
  */
 public static <T extends Action> T getAction(JTextComponent target, Class<T> aClass) {
   for (Object k : target.getActionMap().allKeys()) {
     Action a = target.getActionMap().get(k);
     if (aClass.isInstance(a)) {
       @SuppressWarnings("unchecked")
       T t = (T) a;
       return t;
     }
   }
   return null;
 }
  // Create a simple JToolBar with some buttons.
  protected JToolBar createToolBar() {
    JToolBar bar = new JToolBar();

    // Add simple actions for opening & saving.
    bar.add(getOpenAction()).setText("");
    bar.add(getSaveAction()).setText("");
    bar.addSeparator();

    // Add cut/copy/paste buttons.
    bar.add(textComp.getActionMap().get(DefaultEditorKit.cutAction)).setText("");
    bar.add(textComp.getActionMap().get(DefaultEditorKit.copyAction)).setText("");
    bar.add(textComp.getActionMap().get(DefaultEditorKit.pasteAction)).setText("");
    return bar;
  }
Ejemplo n.º 4
0
  private void initUndoFeature() {
    Document doc = textcomp.getDocument();

    // Listen for undo and redo events
    doc.addUndoableEditListener(
        new UndoableEditListener() {
          public void undoableEditHappened(UndoableEditEvent ev) {
            undoMenuItem.setEnabled(true);
            addEdit(ev.getEdit());
            refreshEnabledStatus();
          }
        });

    // Create an undo action and add it to the text component
    textcomp
        .getActionMap()
        .put(
            "Undo",
            new AbstractAction("Undo") { // $NON-NLS-1$ //$NON-NLS-2$
              public static final long serialVersionUID = 0L;

              public void actionPerformed(ActionEvent ev) {
                undo();
              }
            });

    // Bind the undo action to ctl-Z
    textcomp
        .getInputMap()
        .put(KeyStroke.getKeyStroke("control Z"), "Undo"); // $NON-NLS-1$ //$NON-NLS-2$

    // Create a redo action and add it to the text component
    textcomp
        .getActionMap()
        .put(
            "Redo",
            new AbstractAction("Redo") { // $NON-NLS-1$ //$NON-NLS-2$
              public static final long serialVersionUID = 0L;

              public void actionPerformed(ActionEvent ev) {
                redo();
              }
            });

    // Bind the redo action to ctl-Y
    textcomp
        .getInputMap()
        .put(KeyStroke.getKeyStroke("control Y"), "Redo"); // $NON-NLS-1$ //$NON-NLS-2$
  }
Ejemplo n.º 5
0
  /** add Undo and Redo bindings to a text component */
  public static void addUndoFunctionality(JTextComponent text) {
    // Add an undo manager
    final UndoManager undo = new UndoManager();
    Document doc = text.getDocument();

    // Listen for undo and redo events
    doc.addUndoableEditListener(
        new UndoableEditListener() {
          public void undoableEditHappened(UndoableEditEvent evt) {
            undo.addEdit(evt.getEdit());
          }
        });

    // Create an undo action and add it to the text component
    text.getActionMap()
        .put(
            "Undo",
            new AbstractAction("Undo") {
              public void actionPerformed(ActionEvent evt) {
                try {
                  if (undo.canUndo()) {
                    undo.undo();
                  }
                } catch (CannotUndoException e) {
                }
              }
            });

    // Bind the undo action to ctl-Z
    text.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo");

    // Create a redo action and add it to the text component
    text.getActionMap()
        .put(
            "Redo",
            new AbstractAction("Redo") {
              public void actionPerformed(ActionEvent evt) {
                try {
                  if (undo.canRedo()) {
                    undo.redo();
                  }
                } catch (CannotRedoException e) {
                }
              }
            });

    // Bind the redo action to ctl-Y
    text.getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo");
  }
  // Create a JMenuBar with file & edit menus.
  protected JMenuBar createMenuBar() {
    JMenuBar menubar = new JMenuBar();
    JMenu file = new JMenu("File");
    JMenu edit = new JMenu("Edit");
    menubar.add(file);
    menubar.add(edit);

    file.add(getOpenAction());
    file.add(getSaveAction());
    file.add(new ExitAction());
    edit.add(textComp.getActionMap().get(DefaultEditorKit.cutAction));
    edit.add(textComp.getActionMap().get(DefaultEditorKit.copyAction));
    edit.add(textComp.getActionMap().get(DefaultEditorKit.pasteAction));
    edit.add(textComp.getActionMap().get(DefaultEditorKit.selectAllAction));
    return menubar;
  }
  // Add icons and friendly names to actions we care about.
  protected void makeActionsPretty() {
    Action a;
    a = textComp.getActionMap().get(DefaultEditorKit.cutAction);
    a.putValue(Action.SMALL_ICON, new ImageIcon("icons/cut.gif"));
    a.putValue(Action.NAME, "Cut");

    a = textComp.getActionMap().get(DefaultEditorKit.copyAction);
    a.putValue(Action.SMALL_ICON, new ImageIcon("icons/copy.gif"));
    a.putValue(Action.NAME, "Copy");

    a = textComp.getActionMap().get(DefaultEditorKit.pasteAction);
    a.putValue(Action.SMALL_ICON, new ImageIcon("icons/paste.gif"));
    a.putValue(Action.NAME, "Paste");

    a = textComp.getActionMap().get(DefaultEditorKit.selectAllAction);
    a.putValue(Action.NAME, "Select All");
  }
Ejemplo n.º 8
0
    private void attachSystemActionPerformer(JTextComponent c) {
      if (c == null) return;

      Action action = getEditorAction(c);
      if (action == null) return;

      Action globalSystemAction = getSystemAction(c);
      if (globalSystemAction == null) return;

      if (globalSystemAction instanceof CallbackSystemAction) {
        Object key = ((CallbackSystemAction) globalSystemAction).getActionMapKey();
        c.getActionMap().put(key, action);
      }
    }
Ejemplo n.º 9
0
  /**
   * Construct an undo listener with default key mappings. The default key mappings invoke the undo
   * and redo actions on <i>textArea</i> .
   *
   * <p>A typical usage pattern would be: <code>
   *         JTextArea textArea = new JTextArea("testing");
   *         textArea.getDocument().addUndoableEditListener(new UndoListener(textArea));
   * </code>
   *
   * @param textArea the text component that is being listened to and upon which undo/redo actions
   *     will be performed
   */
  public UndoListener(JTextComponent textArea) {

    // Set the mapping for shortcut keys;
    InputMap inputMap = textArea.getInputMap();
    ActionMap actionMap = textArea.getActionMap();

    // Ctrl-z or equivalent to undo.
    inputMap.put(
        KeyStroke.getKeyStroke(KeyEvent.VK_Z, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()),
        "undo");
    actionMap.put("undo", _undoAction);
    // Ctrl-y or equivalent to redo
    inputMap.put(
        KeyStroke.getKeyStroke(KeyEvent.VK_Y, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()),
        "redo");
    actionMap.put("redo", _redoAction);
  }
Ejemplo n.º 10
0
    private void detachSystemActionPerformer(JTextComponent c) {
      if (c == null) return;

      Action action = getEditorAction(c);
      if (action == null) return;

      Action globalSystemAction = getSystemAction(c);
      if (globalSystemAction == null) return;

      if (globalSystemAction instanceof CallbackSystemAction) {
        Object key = ((CallbackSystemAction) globalSystemAction).getActionMapKey();
        ActionMap am = c.getActionMap();
        if (am != null) {
          Object ea = am.get(key);
          if (action.equals(ea)) {
            am.remove(key);
          }
        }
      }
    }