Example usage for javax.swing TransferHandler getCutAction

List of usage examples for javax.swing TransferHandler getCutAction

Introduction

In this page you can find the example usage for javax.swing TransferHandler getCutAction.

Prototype

public static Action getCutAction() 

Source Link

Document

Returns an Action that performs cut operations to the clipboard.

Usage

From source file:ListCutPaste.java

/**
 * Create an Edit menu to support cut/copy/paste.
 *//*from w  w w  . j ava 2 s. c  o m*/
public JMenuBar createMenuBar() {
    JMenuItem menuItem = null;
    JMenuBar menuBar = new JMenuBar();
    JMenu mainMenu = new JMenu("Edit");
    mainMenu.setMnemonic(KeyEvent.VK_E);
    TransferActionListener actionListener = new TransferActionListener();

    menuItem = new JMenuItem("Cut");
    menuItem.setActionCommand((String) TransferHandler.getCutAction().getValue(Action.NAME));
    menuItem.addActionListener(actionListener);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
    menuItem.setMnemonic(KeyEvent.VK_T);
    mainMenu.add(menuItem);

    menuItem = new JMenuItem("Copy");
    menuItem.setActionCommand((String) TransferHandler.getCopyAction().getValue(Action.NAME));
    menuItem.addActionListener(actionListener);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
    menuItem.setMnemonic(KeyEvent.VK_C);
    mainMenu.add(menuItem);

    menuItem = new JMenuItem("Paste");
    menuItem.setActionCommand((String) TransferHandler.getPasteAction().getValue(Action.NAME));
    menuItem.addActionListener(actionListener);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK));
    menuItem.setMnemonic(KeyEvent.VK_P);
    mainMenu.add(menuItem);

    menuBar.add(mainMenu);
    return menuBar;
}

From source file:DragPictureDemo2.java

public JMenuBar createMenuBar() {
    JMenuItem menuItem = null;//  w  w w  .  j  av a  2  s  .  co  m
    JMenuBar menuBar = new JMenuBar();
    JMenu mainMenu = new JMenu("Edit");
    mainMenu.setMnemonic(KeyEvent.VK_E);
    TransferActionListener actionListener = new TransferActionListener();

    menuItem = new JMenuItem("Cut");
    menuItem.setActionCommand((String) TransferHandler.getCutAction().getValue(Action.NAME));
    menuItem.addActionListener(actionListener);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
    menuItem.setMnemonic(KeyEvent.VK_T);
    mainMenu.add(menuItem);
    menuItem = new JMenuItem("Copy");
    menuItem.setActionCommand((String) TransferHandler.getCopyAction().getValue(Action.NAME));
    menuItem.addActionListener(actionListener);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
    menuItem.setMnemonic(KeyEvent.VK_C);
    mainMenu.add(menuItem);
    menuItem = new JMenuItem("Paste");
    menuItem.setActionCommand((String) TransferHandler.getPasteAction().getValue(Action.NAME));
    menuItem.addActionListener(actionListener);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK));
    menuItem.setMnemonic(KeyEvent.VK_P);
    mainMenu.add(menuItem);

    menuBar.add(mainMenu);
    return menuBar;
}

From source file:ListCutPaste.java

/**
 * Add the cut/copy/paste actions to the action map.
 *///from   ww w .  j  a v  a  2  s.c  o  m
private void setMappings(JList list) {
    ActionMap map = list.getActionMap();
    map.put(TransferHandler.getCutAction().getValue(Action.NAME), TransferHandler.getCutAction());
    map.put(TransferHandler.getCopyAction().getValue(Action.NAME), TransferHandler.getCopyAction());
    map.put(TransferHandler.getPasteAction().getValue(Action.NAME), TransferHandler.getPasteAction());

}

From source file:DragPictureDemo2.java

public DTPicture(Image image) {
    super(image);
    addMouseMotionListener(this);

    //Add the cut/copy/paste key bindings to the input map.
    //Note that this step is redundant if you are installing
    //menu accelerators that cause these actions to be invoked.
    //DragPictureDemo does not use menu accelerators and, since
    //the default value of installInputMapBindings is true,
    //the bindings are installed. DragPictureDemo2 does use
    //menu accelerators and so calls setInstallInputMapBindings
    //with a value of false. Your program would do one or the
    //other, but not both.
    if (installInputMapBindings) {
        InputMap imap = this.getInputMap();
        imap.put(KeyStroke.getKeyStroke("ctrl X"), TransferHandler.getCutAction().getValue(Action.NAME));
        imap.put(KeyStroke.getKeyStroke("ctrl C"), TransferHandler.getCopyAction().getValue(Action.NAME));
        imap.put(KeyStroke.getKeyStroke("ctrl V"), TransferHandler.getPasteAction().getValue(Action.NAME));
    }//from  w ww  .  j a  va  2  s  .  c o m

    //Add the cut/copy/paste actions to the action map.
    //This step is necessary because the menu's action listener
    //looks for these actions to fire.
    ActionMap map = this.getActionMap();
    map.put(TransferHandler.getCutAction().getValue(Action.NAME), TransferHandler.getCutAction());
    map.put(TransferHandler.getCopyAction().getValue(Action.NAME), TransferHandler.getCopyAction());
    map.put(TransferHandler.getPasteAction().getValue(Action.NAME), TransferHandler.getPasteAction());
}

From source file:it.cnr.icar.eric.client.ui.swing.graph.JBGraph.java

/**
 * DOCUMENT ME!//from  w  ww .  j a v a 2  s. c  om
 *
 * @return DOCUMENT ME!
 */
public JToolBar createToolBar() {
    JButton button = null;
    JToolBar toolbar = new JToolBar();
    toolbar.setFloatable(false);

    //TODO: SwingBoost: Localize this
    TreeNode tempTreeNode = new DefaultMutableTreeNode("loading object types...");
    objectTypeCombo = new it.cnr.icar.eric.client.ui.swing.TreeCombo(new DefaultTreeModel(tempTreeNode));
    toolbar.add(objectTypeCombo);

    // use a SwingWorker to get the real model, since it might not have been initialized yet
    final SwingWorker worker = new SwingWorker(this) {
        public Object doNonUILogic() {
            ConceptsTreeModel objectTypesTreeModel = BusinessQueryPanel.getObjectTypesTreeModel();
            return objectTypesTreeModel;
        }

        public void doUIUpdateLogic() {
            ConceptsTreeModel objectTypesTreeModel = (ConceptsTreeModel) get();
            objectTypeCombo.setModel(objectTypesTreeModel);
        }
    };
    worker.start();

    // Insert
    URL insertUrl = getClass().getClassLoader().getResource("icons/insert.gif");
    ImageIcon insertIcon = new ImageIcon(insertUrl);
    button = toolbar.add(new AbstractAction("", insertIcon) {
        /**
        * 
        */
        private static final long serialVersionUID = 1L;

        public void actionPerformed(ActionEvent e) {
            insert(new Point(10, 10));
        }
    });
    button.setText(""); //an icon-only button
    button.setToolTipText("Insert");

    // Toggle Connect Mode
    URL connectUrl = getClass().getClassLoader().getResource("icons/connecton.gif");
    ImageIcon connectIcon = new ImageIcon(connectUrl);
    button = toolbar.add(new AbstractAction("", connectIcon) {

        /**
        * 
        */
        private static final long serialVersionUID = 657528648199915209L;

        public void actionPerformed(ActionEvent e) {
            setPortsVisible(!isPortsVisible());

            URL connectUrl;

            if (isPortsVisible()) {
                connectUrl = getClass().getClassLoader().getResource("icons/connecton.gif");
            } else {
                connectUrl = getClass().getClassLoader().getResource("icons/connectoff.gif");
            }

            ImageIcon connectIcon = new ImageIcon(connectUrl);
            putValue(SMALL_ICON, connectIcon);
        }
    });
    button.setText(""); //an icon-only button
    button.setToolTipText("Toggle Connect Mode");

    // Undo
    toolbar.addSeparator();

    URL undoUrl = getClass().getClassLoader().getResource("icons/undo.gif");
    ImageIcon undoIcon = new ImageIcon(undoUrl);
    undo = new AbstractAction("", undoIcon) {
        /**
        * 
        */
        private static final long serialVersionUID = -740055667372297781L;

        public void actionPerformed(ActionEvent e) {
            undo();
        }
    };
    undo.setEnabled(false);
    button = toolbar.add(undo);
    button.setText(""); //an icon-only button
    button.setToolTipText("Undo");

    // Redo
    URL redoUrl = getClass().getClassLoader().getResource("icons/redo.gif");
    ImageIcon redoIcon = new ImageIcon(redoUrl);
    redo = new AbstractAction("", redoIcon) {
        /**
        * 
        */
        private static final long serialVersionUID = 5021485220988522968L;

        public void actionPerformed(ActionEvent e) {
            redo();
        }
    };
    redo.setEnabled(false);
    button = toolbar.add(redo);
    button.setText(""); //an icon-only button
    button.setToolTipText("Redo");

    //
    // Edit Block
    //
    toolbar.addSeparator();

    Action action;
    URL url;

    // Copy
    action = TransferHandler.getCopyAction();
    url = getClass().getClassLoader().getResource("icons/copy.gif");
    action.putValue(Action.SMALL_ICON, new ImageIcon(url));

    //Commented out until we can figure out how to assign new id to copied objects
    //button = toolbar.add(copy = new EventRedirector(action));
    button.setText(""); //an icon-only button
    button.setToolTipText("Copy");

    // Paste
    action = TransferHandler.getPasteAction();
    url = getClass().getClassLoader().getResource("icons/paste.gif");
    action.putValue(Action.SMALL_ICON, new ImageIcon(url));

    //Commented out until we can figure out how to assign new id to copied objects
    //button = toolbar.add(paste = new EventRedirector(action));
    button.setText(""); //an icon-only button
    button.setToolTipText("Paste");

    // Cut
    action = TransferHandler.getCutAction();
    url = getClass().getClassLoader().getResource("icons/cut.gif");
    action.putValue(Action.SMALL_ICON, new ImageIcon(url));

    //Commented out until we can figure out how to assign new id to copied objects
    //button = toolbar.add(cut = new EventRedirector(action));
    button.setText(""); //an icon-only button
    button.setToolTipText("Cut");

    // Remove
    URL removeUrl = getClass().getClassLoader().getResource("icons/delete.gif");
    ImageIcon removeIcon = new ImageIcon(removeUrl);
    remove = new AbstractAction("", removeIcon) {
        /**
        * 
        */
        private static final long serialVersionUID = 6889927067487680474L;

        public void actionPerformed(ActionEvent e) {
            if (!isSelectionEmpty()) {
                Object[] cells = getSelectionCells();
                cells = getDescendants(cells);
                getModel().remove(cells);

                //Remove entry from map of cells on the graph
                for (int i = 0; i < cells.length; i++) {
                    Object cell = cells[i];

                    if (cell instanceof JBGraphCell) {
                        RegistryObject ro = ((JBGraphCell) cell).getRegistryObject();
                        registryObjectToCellMap.remove(ro);
                    }
                }
            }
        }
    };
    remove.setEnabled(false);
    button = toolbar.add(remove);
    button.setText(""); //an icon-only button
    button.setToolTipText(resourceBundle.getString("menu.graphPanel.removeFromView"));

    // Zoom Std
    toolbar.addSeparator();

    URL zoomUrl = getClass().getClassLoader().getResource("icons/zoom.gif");
    ImageIcon zoomIcon = new ImageIcon(zoomUrl);
    button = toolbar.add(new AbstractAction("", zoomIcon) {
        /**
        * 
        */
        private static final long serialVersionUID = -4404610379022823602L;

        public void actionPerformed(ActionEvent e) {
            setScale(1.0);
        }
    });
    button.setText(""); //an icon-only button
    button.setToolTipText("Zoom");

    // Zoom In
    URL zoomInUrl = getClass().getClassLoader().getResource("icons/zoomin.gif");
    ImageIcon zoomInIcon = new ImageIcon(zoomInUrl);
    button = toolbar.add(new AbstractAction("", zoomInIcon) {
        /**
        * 
        */
        private static final long serialVersionUID = 6782766891458235321L;

        public void actionPerformed(ActionEvent e) {
            setScale(2 * getScale());
        }
    });
    button.setText(""); //an icon-only button
    button.setToolTipText("Zoom In");

    // Zoom Out
    URL zoomOutUrl = getClass().getClassLoader().getResource("icons/zoomout.gif");
    ImageIcon zoomOutIcon = new ImageIcon(zoomOutUrl);
    button = toolbar.add(new AbstractAction("", zoomOutIcon) {
        /**
        * 
        */
        private static final long serialVersionUID = -5480242207934335070L;

        public void actionPerformed(ActionEvent e) {
            setScale(getScale() / 2);
        }
    });
    button.setText(""); //an icon-only button
    button.setToolTipText("Zoom Out");

    // Group
    /*
    toolbar.addSeparator();
            
    URL groupUrl          =
    getClass().getClassLoader().getResource("icons/group.gif");
    ImageIcon groupIcon   = new ImageIcon(groupUrl);
    group =
    new AbstractAction("", groupIcon) {
            public void actionPerformed(ActionEvent e) {
                group(getSelectionCells());
            }
        };
    group.setEnabled(false);
    //button                = toolbar.add(group);
    button.setText(""); //an icon-only button
    button.setToolTipText("Group");
            
    // Ungroup
    URL ungroupUrl        =
    getClass().getClassLoader().getResource("icons/ungroup.gif");
    ImageIcon ungroupIcon = new ImageIcon(ungroupUrl);
    ungroup =
    new AbstractAction("", ungroupIcon) {
            public void actionPerformed(ActionEvent e) {
                ungroup(getSelectionCells());
            }
        };
    ungroup.setEnabled(false);
    //button                = toolbar.add(ungroup);
    button.setText(""); //an icon-only button
    button.setToolTipText("Ungroup");
     */
    // To Front
    toolbar.addSeparator();

    URL toFrontUrl = getClass().getClassLoader().getResource("icons/tofront.gif");
    ImageIcon toFrontIcon = new ImageIcon(toFrontUrl);
    tofront = new AbstractAction("", toFrontIcon) {
        /**
        * 
        */
        private static final long serialVersionUID = -4901428890590828561L;

        public void actionPerformed(ActionEvent e) {
            if (!isSelectionEmpty()) {
                toFront(getSelectionCells());
            }
        }
    };
    tofront.setEnabled(false);
    button = toolbar.add(tofront);
    button.setText(""); //an icon-only button
    button.setToolTipText("To Front");

    // To Back
    URL toBackUrl = getClass().getClassLoader().getResource("icons/toback.gif");
    ImageIcon toBackIcon = new ImageIcon(toBackUrl);
    toback = new AbstractAction("", toBackIcon) {
        /**
        * 
        */
        private static final long serialVersionUID = -5942025518651424307L;

        public void actionPerformed(ActionEvent e) {
            if (!isSelectionEmpty()) {
                toBack(getSelectionCells());
            }
        }
    };
    toback.setEnabled(false);
    button = toolbar.add(toback);
    button.setText(""); //an icon-only button
    button.setToolTipText("To Back");

    return toolbar;
}