Save This Page
Home » j2ssh-0.2.9-src » com.sshtools.common.ui » [javadoc | source]
    1   /*
    2    *  SSHTools - Java SSH2 API
    3    *
    4    *  Copyright (C) 2002-2003 Lee David Painter and Contributors.
    5    *
    6    *  Contributions made by:
    7    *
    8    *  Brett Smith
    9    *  Richard Pernavas
   10    *  Erwin Bolwidt
   11    *
   12    *  This program is free software; you can redistribute it and/or
   13    *  modify it under the terms of the GNU General Public License
   14    *  as published by the Free Software Foundation; either version 2
   15    *  of the License, or (at your option) any later version.
   16    *
   17    *  This program is distributed in the hope that it will be useful,
   18    *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   19    *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   20    *  GNU General Public License for more details.
   21    *
   22    *  You should have received a copy of the GNU General Public License
   23    *  along with this program; if not, write to the Free Software
   24    *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
   25    */
   26   package com.sshtools.common.ui;
   27   
   28   import java.awt;
   29   import java.awt.datatransfer;
   30   import java.awt.event;
   31   
   32   import javax.swing;
   33   import javax.swing.text;
   34   
   35   
   36   /**
   37    *
   38    *
   39    * @author $author$
   40    * @version $Revision: 1.14 $
   41    */
   42   public class XTextField extends JTextField implements ClipboardOwner {
   43       private JPopupMenu popup;
   44       private Action cutAction;
   45       private Action copyAction;
   46       private Action pasteAction;
   47       private Action deleteAction;
   48       private Action selectAllAction;
   49   
   50       /**
   51   * Creates a new XTextField object.
   52   */
   53       public XTextField() {
   54           this(null, null, 0);
   55       }
   56   
   57       /**
   58   * Creates a new XTextField object.
   59   *
   60   * @param text
   61   */
   62       public XTextField(String text) {
   63           this(null, text, 0);
   64       }
   65   
   66       /**
   67   * Creates a new XTextField object.
   68   *
   69   * @param columns
   70   */
   71       public XTextField(int columns) {
   72           this(null, null, columns);
   73       }
   74   
   75       /**
   76   * Creates a new XTextField object.
   77   *
   78   * @param text
   79   * @param columns
   80   */
   81       public XTextField(String text, int columns) {
   82           this(null, text, columns);
   83       }
   84   
   85       /**
   86   * Creates a new XTextField object.
   87   *
   88   * @param doc
   89   * @param text
   90   * @param columns
   91   */
   92       public XTextField(Document doc, String text, int columns) {
   93           super(doc, text, columns);
   94           initXtensions();
   95       }
   96   
   97       /**
   98   *
   99   *
  100   * @param clipboard
  101   * @param contents
  102   */
  103       public void lostOwnership(Clipboard clipboard, Transferable contents) {
  104       }
  105   
  106       private void showPopup(int x, int y) {
  107           //  Grab the focus, this should deselect any other selected fields.
  108           requestFocus();
  109   
  110           //  If the popup has never been show before - then build it
  111           if (popup == null) {
  112               popup = new JPopupMenu("Clipboard");
  113               popup.add(cutAction = new CutAction());
  114               popup.add(copyAction = new CopyAction());
  115               popup.add(pasteAction = new PasteAction());
  116               popup.add(deleteAction = new DeleteAction());
  117               popup.addSeparator();
  118               popup.add(selectAllAction = new SelectAllAction());
  119           }
  120   
  121           //  Enabled the actions based on the field contents
  122           cutAction.setEnabled(isEnabled() && (getSelectedText() != null));
  123           copyAction.setEnabled(isEnabled() && (getSelectedText() != null));
  124           deleteAction.setEnabled(isEnabled() && (getSelectedText() != null));
  125           pasteAction.setEnabled(isEnabled() &&
  126               Toolkit.getDefaultToolkit().getSystemClipboard().getContents(this)
  127                      .isDataFlavorSupported(DataFlavor.stringFlavor));
  128           selectAllAction.setEnabled(isEnabled());
  129   
  130           //  Make the popup visible
  131           popup.show(this, x, y);
  132       }
  133   
  134       private void initXtensions() {
  135           addMouseListener(new MouseAdapter() {
  136                   public void mouseClicked(MouseEvent evt) {
  137                       if (SwingUtilities.isRightMouseButton(evt)) {
  138                           showPopup(evt.getX(), evt.getY());
  139                       }
  140                   }
  141               });
  142           addFocusListener(new FocusListener() {
  143                   public void focusGained(FocusEvent evt) {
  144                       XTextField.this.selectAll();
  145                   }
  146   
  147                   public void focusLost(FocusEvent evt) {
  148                       //                if(popup.isVisible())
  149                       //                    popup.setVisible(false);
  150                   }
  151               });
  152       }
  153   
  154       //  Supporting actions
  155       class CopyAction extends AbstractAction {
  156           public CopyAction() {
  157               putValue(Action.NAME, "Copy");
  158               putValue(Action.SMALL_ICON,
  159                   new ResourceIcon(XTextField.class, "copy.png"));
  160               putValue(Action.SHORT_DESCRIPTION, "Copy");
  161               putValue(Action.LONG_DESCRIPTION,
  162                   "Copy the selection from the text and place it in the clipboard");
  163               putValue(Action.MNEMONIC_KEY, new Integer('c'));
  164               putValue(Action.ACCELERATOR_KEY,
  165                   KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_MASK));
  166           }
  167   
  168           public void actionPerformed(ActionEvent evt) {
  169               Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(
  170                       getText()), XTextField.this);
  171           }
  172       }
  173   
  174       class CutAction extends AbstractAction {
  175           public CutAction() {
  176               putValue(Action.NAME, "Cut");
  177               putValue(Action.SMALL_ICON,
  178                   new ResourceIcon(XTextField.class, "cut.png"));
  179               putValue(Action.SHORT_DESCRIPTION, "Cut selection");
  180               putValue(Action.LONG_DESCRIPTION,
  181                   "Cut the selection from the text and place it in the clipboard");
  182               putValue(Action.MNEMONIC_KEY, new Integer('u'));
  183               putValue(Action.ACCELERATOR_KEY,
  184                   KeyStroke.getKeyStroke(KeyEvent.VK_X, KeyEvent.CTRL_MASK));
  185           }
  186   
  187           public void actionPerformed(ActionEvent evt) {
  188               Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(
  189                       getText()), XTextField.this);
  190               setText("");
  191           }
  192       }
  193   
  194       class PasteAction extends AbstractAction {
  195           public PasteAction() {
  196               putValue(Action.NAME, "Paste");
  197               putValue(Action.SMALL_ICON,
  198                   new ResourceIcon(XTextField.class, "paste.png"));
  199               putValue(Action.SHORT_DESCRIPTION, "Paste clipboard content");
  200               putValue(Action.LONG_DESCRIPTION,
  201                   "Paste the clipboard contents to the current care position or replace the selection");
  202               putValue(Action.MNEMONIC_KEY, new Integer('p'));
  203               putValue(Action.ACCELERATOR_KEY,
  204                   KeyStroke.getKeyStroke(KeyEvent.VK_X, KeyEvent.CTRL_MASK));
  205           }
  206   
  207           public void actionPerformed(ActionEvent evt) {
  208               Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard()
  209                                       .getContents(this);
  210   
  211               if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
  212                   try {
  213                       setText(t.getTransferData(DataFlavor.stringFlavor).toString());
  214                   } catch (Exception e) {
  215                       //  Dont care
  216                   }
  217               }
  218           }
  219       }
  220   
  221       class DeleteAction extends AbstractAction {
  222           public DeleteAction() {
  223               putValue(Action.NAME, "Delete");
  224               putValue(Action.SMALL_ICON,
  225                   new ResourceIcon(XTextField.class, "delete.png"));
  226               putValue(Action.SHORT_DESCRIPTION, "Delete selection");
  227               putValue(Action.LONG_DESCRIPTION,
  228                   "Delete the selection from the text");
  229               putValue(Action.MNEMONIC_KEY, new Integer('d'));
  230               putValue(Action.ACCELERATOR_KEY,
  231                   KeyStroke.getKeyStroke(KeyEvent.VK_X, KeyEvent.CTRL_MASK));
  232           }
  233   
  234           public void actionPerformed(ActionEvent evt) {
  235               setText("");
  236           }
  237       }
  238   
  239       class SelectAllAction extends AbstractAction {
  240           SelectAllAction() {
  241               putValue(Action.SMALL_ICON, new EmptyIcon(16, 16));
  242               putValue(Action.NAME, "Select All");
  243               putValue(Action.SHORT_DESCRIPTION, "Select All");
  244               putValue(Action.LONG_DESCRIPTION, "Select all items in the context");
  245               putValue(Action.MNEMONIC_KEY, new Integer('a'));
  246               putValue(Action.ACCELERATOR_KEY,
  247                   KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.CTRL_MASK));
  248           }
  249   
  250           public void actionPerformed(ActionEvent evt) {
  251               selectAll();
  252           }
  253       }
  254   }

Save This Page
Home » j2ssh-0.2.9-src » com.sshtools.common.ui » [javadoc | source]