Пример #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");
  }
Пример #2
0
  void InitUndoMgr() {
    final UndoManager undo = new UndoManager();
    Document doc = txt_box.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
    txt_box
        .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
    txt_box.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo");

    // Create a redo action and add it to the text component
    txt_box
        .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
    txt_box.getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo");
  }
Пример #3
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$
  }
Пример #4
0
  public CodeTextPanel(Border bb, String title, String text) {
    super(new GridLayout(1, 1));
    if (bb != null) {
      Border b = BorderFactory.createTitledBorder(bb, title);
      setBorder(b);
    }

    JScrollPane p = new JScrollPane(area);
    add(p);
    p.setBorder(BorderFactory.createEmptyBorder());
    setText(text);

    area.setFont(new Font("Courier", Font.PLAIN, 13));
    // area.setEditable(false);

    UndoManager m = new UndoManager();
    // area.setundoManager = new UndoManager();
    Document doc = area.getDocument();
    doc.addUndoableEditListener((UndoableEditEvent e) -> m.addEdit(e.getEdit()));

    InputMap im = area.getInputMap(JComponent.WHEN_FOCUSED);
    ActionMap am = area.getActionMap();

    im.put(
        KeyStroke.getKeyStroke(KeyEvent.VK_Z, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()),
        "Undo");
    im.put(
        KeyStroke.getKeyStroke(KeyEvent.VK_Y, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()),
        "Redo");

    am.put(
        "Undo",
        new AbstractAction() {
          @Override
          public void actionPerformed(ActionEvent e) {
            try {
              if (m.canUndo()) {
                m.undo();
              }
            } catch (CannotUndoException exp) {
              exp.printStackTrace();
            }
          }
        });
    am.put(
        "Redo",
        new AbstractAction() {
          @Override
          public void actionPerformed(ActionEvent e) {
            try {
              if (m.canRedo()) {
                m.redo();
              }
            } catch (CannotUndoException exp) {
              exp.printStackTrace();
            }
          }
        });

    // setWordWrap(true);
  }
Пример #5
0
  /** config tools bar */
  private void toolsBarConfigure() {

    JToolBar toolsBar = new JToolBar();
    toolsBar.setFloatable(false);
    toolsBar.setBackground(Color.gray);
    JButton back = new JButton(new ImageIcon(iconDir + "back2.png"));
    back.setBorder(null);
    back.setEnabled(false);
    back.setContentAreaFilled(false);
    back.setToolTipText("Back");
    backJB = back;
    back.addMouseListener(
        new MouseListener() {
          @Override
          public void mouseReleased(MouseEvent arg0) {}

          @Override
          public void mousePressed(MouseEvent arg0) {
            if (((JButton) arg0.getSource()).isEnabled()) back();
            updateBackImage(arg0);
            updateforwardImage();
          }

          @Override
          public void mouseExited(MouseEvent arg0) {}

          @Override
          public void mouseEntered(MouseEvent arg0) {}

          @Override
          public void mouseClicked(MouseEvent arg0) {}
        });
    toolsBar.add(back);
    back = new JButton(new ImageIcon(iconDir + "forward2.png"));
    back.setBorder(null);
    forwardJB = back;
    back.setEnabled(false);
    back.setContentAreaFilled(false);
    back.addMouseListener(
        new MouseListener() {

          @Override
          public void mouseReleased(MouseEvent e) {}

          @Override
          public void mousePressed(MouseEvent e) {
            if (forwardJB.isEnabled()) forward();
            updateBackImage(null);
            updateforwardImage();
          }

          @Override
          public void mouseExited(MouseEvent e) {}

          @Override
          public void mouseEntered(MouseEvent e) {}

          @Override
          public void mouseClicked(MouseEvent e) {}
        });
    back.setToolTipText("Forward");
    toolsBar.add(back);
    // **********************************************
    back = new JButton(new ImageIcon(iconDir + "home.png"));
    back.setBorder(null);
    back.addMouseListener(
        new MouseListener() {

          @Override
          public void mouseReleased(MouseEvent arg0) {}

          @Override
          public void mousePressed(MouseEvent arg0) {
            Tab tabee = tabAccess.get(tab.getSelectedIndex());
            addressBar.setText("http://www.google.com");
            MyEditorPane pane = new MyEditorPane(addressBar.getText());
            pane.addHyperlinkListener(HYHandler);
            JScrollPane scPane = new JScrollPane(pane);
            tabee.addPane(pane);
            pane.setEditable(false);

            int i = tab.getSelectedIndex();
            tabcreate = false;
            change = false;
            tab.remove(i);
            tabcreate = false;
            change = false;
            tab.insertTab(tabee.getPane().getAddress(), null, scPane, null, i);
            tabcreate = false;
            change = false;
            addCloseButton(i, tabee.getPane().getAddress());
            tabcreate = false;
            change = false;
            tab.setSelectedIndex(i);
            change = false;
            tabcreate = false;
            loadPage(pane, tabee.getPane().getAddress());
            tabcreate = true;
          }

          @Override
          public void mouseExited(MouseEvent arg0) {}

          @Override
          public void mouseEntered(MouseEvent arg0) {}

          @Override
          public void mouseClicked(MouseEvent arg0) {}
        });
    back.setContentAreaFilled(false);
    back.setToolTipText("Home");
    toolsBar.add(back);
    back = new JButton(new ImageIcon(iconDir + "refresh2.png"));
    back.setBorder(null);
    back.addMouseListener(
        new MouseListener() {
          @Override
          public void mouseReleased(MouseEvent arg0) {}

          @Override
          public void mousePressed(MouseEvent arg0) {
            Tab tabee = tabAccess.get(tab.getSelectedIndex());
            MyEditorPane pane = new MyEditorPane(addressBar.getText());
            pane.addHyperlinkListener(HYHandler);
            JScrollPane scPane = new JScrollPane(pane);
            tabee.addPane(pane);
            pane.setEditable(false);

            int i = tab.getSelectedIndex();
            tabcreate = false;
            change = false;
            tab.remove(i);
            tabcreate = false;
            change = false;
            tab.insertTab(tabee.getPane().getAddress(), null, scPane, null, i);
            tabcreate = false;
            change = false;
            addCloseButton(i, tabee.getPane().getAddress());
            change = false;
            tab.setSelectedIndex(i);
            tabcreate = false;
            change = false;
            loadPage(pane, tabee.getPane().getAddress());
            tabcreate = true;
          }

          @Override
          public void mouseExited(MouseEvent arg0) {}

          @Override
          public void mouseEntered(MouseEvent arg0) {}

          @Override
          public void mouseClicked(MouseEvent arg0) {}
        });
    back.setEnabled(false);
    back.setContentAreaFilled(false);
    back.setToolTipText("Refresh");
    toolsBar.add(back);

    addressBar = new JTextField("http://www.google.com");
    addressBar.setToolTipText("Address");
    toolsBar.add(addressBar);
    Document doc = addressBar.getDocument();
    doc.addUndoableEditListener(this);
    addressBar.addKeyListener(
        new KeyListener() {
          @Override
          public void keyTyped(KeyEvent arg0) {}

          @Override
          public void keyReleased(KeyEvent arg0) {
            updateTabes(arg0);
          }

          @Override
          public void keyPressed(KeyEvent arg0) {}
        });
    // **********************************************
    back = new JButton(new ImageIcon(iconDir + "forward.png"));
    back.setBorder(null);
    back.setContentAreaFilled(false);
    back.setToolTipText("Go");
    back.addMouseListener(
        new MouseListener() {

          @Override
          public void mouseReleased(MouseEvent arg0) {}

          @Override
          public void mousePressed(MouseEvent arg0) {
            Tab tabee = tabAccess.get(tab.getSelectedIndex());
            MyEditorPane pane = new MyEditorPane(addressBar.getText());
            pane.addHyperlinkListener(HYHandler);
            JScrollPane scPane = new JScrollPane(pane);
            tabee.addPane(pane);
            pane.setEditable(false);

            int i = tab.getSelectedIndex();
            tabcreate = false;
            change = false;
            tab.remove(i);
            tabcreate = false;
            change = false;
            tab.insertTab(tabee.getPane().getAddress(), null, scPane, null, i);
            tabcreate = false;
            change = false;
            addCloseButton(i, tabee.getPane().getAddress());
            tabcreate = false;
            change = false;
            tab.setSelectedIndex(i);
            change = false;
            tabcreate = false;
            loadPage(pane, tabee.getPane().getAddress());
            tabcreate = true;
          }

          @Override
          public void mouseExited(MouseEvent arg0) {}

          @Override
          public void mouseEntered(MouseEvent arg0) {}

          @Override
          public void mouseClicked(MouseEvent arg0) {}
        });
    toolsBar.add(back);

    getContentPane().add(toolsBar, BorderLayout.NORTH);
  }