Example usage for javax.swing JMenuItem setTransferHandler

List of usage examples for javax.swing JMenuItem setTransferHandler

Introduction

In this page you can find the example usage for javax.swing JMenuItem setTransferHandler.

Prototype

@BeanProperty(hidden = true, description = "Mechanism for transfer of data to and from the component")
public void setTransferHandler(TransferHandler newHandler) 

Source Link

Document

Sets the TransferHandler , which provides support for transfer of data into and out of this component via cut/copy/paste and drag and drop.

Usage

From source file:org.gtdfree.GTDFree.java

protected TrayIcon getTrayIcon() {
    if (trayIcon == null) {
        if (ApplicationHelper.isGTKLaF()) {
            trayIcon = new TrayIcon(ApplicationHelper.loadImage(ApplicationHelper.icon_name_large_tray_splash));
        } else {//from ww  w .j a v a 2 s .com
            trayIcon = new TrayIcon(ApplicationHelper.loadImage(ApplicationHelper.icon_name_small_tray_splash));
        }

        trayIcon.setImageAutoSize(true);
        trayIcon.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON1) {
                    trayIconPopup.setVisible(false);
                    if (getJFrame().isVisible()) {
                        getJFrame().setVisible(false);
                    } else {
                        pushVisible();
                    }
                } else {
                    if (trayIconPopup.isVisible()) {
                        trayIconPopup.setVisible(false);
                    } else {
                        Point p = new Point(e.getPoint());
                        /*
                         * Disabled, because we are anyway doing things like rollover,
                         * which are probably done by Frame.
                        if (getJFrame().isShowing()) {
                           SwingUtilities.convertPointFromScreen(p, getJFrame());
                           trayIconPopup.show(getJFrame(), p.x, p.y);
                        } else {
                        }*/
                        trayIconPopup.show(null, p.x, p.y);
                    }
                }
            }
        });
        trayIcon.setToolTip("GTD-Free - " + Messages.getString("GTDFree.Tray.desc")); //$NON-NLS-1$ //$NON-NLS-2$

        /*
         * Necessary only when popup is showing with null window. Hides popup.
         */
        MouseListener hideMe = new MouseAdapter() {
            @Override
            public void mouseExited(MouseEvent e) {
                if (e.getComponent() instanceof JMenuItem) {
                    JMenuItem jm = (JMenuItem) e.getComponent();
                    jm.getModel().setRollover(false);
                    jm.getModel().setArmed(false);
                    jm.repaint();
                }

                Point p = SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), trayIconPopup);
                //System.out.println(p.x+" "+p.y+" "+trayIconPopup.getWidth()+" "+trayIconPopup.getHeight());
                if (p.x < 0 || p.x >= trayIconPopup.getWidth() || p.y < 0 || p.y >= trayIconPopup.getHeight()) {
                    trayIconPopup.setVisible(false);
                }
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                if (e.getComponent() instanceof JMenuItem) {
                    JMenuItem jm = (JMenuItem) e.getComponent();
                    jm.getModel().setRollover(true);
                    jm.getModel().setArmed(true);
                    jm.repaint();
                }
            }
        };

        trayIconPopup = new JPopupMenu();
        trayIconPopup.addMouseListener(hideMe);

        JMenuItem mi = new JMenuItem(Messages.getString("GTDFree.Tray.Drop")); //$NON-NLS-1$
        mi.setIcon(ApplicationHelper.getIcon(ApplicationHelper.icon_name_small_collecting));
        mi.setToolTipText(Messages.getString("GTDFree.Tray.Drop.desc")); //$NON-NLS-1$
        mi.addMouseListener(hideMe);

        /*
         * Workaround for tray, if JFrame is showing, then mouse click is not fired
         */
        mi.addMouseListener(new MouseAdapter() {
            private boolean click = false;

            @Override
            public void mousePressed(MouseEvent e) {
                click = true;
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                if (click) {
                    click = false;
                    doMouseClicked(e);
                }
            }

            @Override
            public void mouseExited(MouseEvent e) {
                click = false;
            }

            private void doMouseClicked(MouseEvent e) {
                trayIconPopup.setVisible(false);
                Clipboard c = null;
                if (e.getButton() == MouseEvent.BUTTON1) {
                    c = Toolkit.getDefaultToolkit().getSystemClipboard();
                } else if (e.getButton() == MouseEvent.BUTTON2) {
                    c = Toolkit.getDefaultToolkit().getSystemSelection();
                } else {
                    return;
                }
                try {
                    Object o = c.getData(DataFlavor.stringFlavor);
                    if (o != null) {
                        getEngine().getGTDModel().collectAction(o.toString());
                    }
                    flashMessage(Messages.getString("GTDFree.Tray.Collect.ok"), e.getLocationOnScreen()); //$NON-NLS-1$
                } catch (Exception e1) {
                    Logger.getLogger(this.getClass()).debug("Internal error.", e1); //$NON-NLS-1$
                    flashMessage(Messages.getString("GTDFree.Tray.Collect.fail") + e1.getMessage(), //$NON-NLS-1$
                            e.getLocationOnScreen());
                }
            }
        });

        TransferHandler th = new TransferHandler() {
            private static final long serialVersionUID = 1L;

            @Override
            public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) {
                return DataFlavor.selectBestTextFlavor(transferFlavors) != null;
            }

            @Override
            public boolean importData(JComponent comp, Transferable t) {
                try {
                    DataFlavor f = DataFlavor.selectBestTextFlavor(t.getTransferDataFlavors());
                    Object o = t.getTransferData(f);
                    if (o != null) {
                        getEngine().getGTDModel().collectAction(o.toString());
                    }
                    return true;
                } catch (UnsupportedFlavorException e) {
                    Logger.getLogger(this.getClass()).debug("Internal error.", e); //$NON-NLS-1$
                } catch (IOException e) {
                    Logger.getLogger(this.getClass()).debug("Internal error.", e); //$NON-NLS-1$
                }
                return false;
            }

        };
        mi.setTransferHandler(th);

        trayIconPopup.add(mi);

        mi = new JMenuItem();
        mi.setIcon(ApplicationHelper.getIcon(ApplicationHelper.icon_name_large_delete));
        mi.setText(Messages.getString("GTDFree.Tray.Hide")); //$NON-NLS-1$
        mi.setToolTipText(Messages.getString("GTDFree.Tray.Hide.desc")); //$NON-NLS-1$
        mi.addMouseListener(hideMe);
        mi.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                trayIconPopup.setVisible(false);
                if (getJFrame().isVisible()) {
                    getJFrame().setVisible(false);
                }
            }
        });
        trayIconPopup.add(mi);

        mi = new JMenuItem();
        mi.setIcon(ApplicationHelper.getIcon(ApplicationHelper.icon_name_small_splash));
        mi.setText(Messages.getString("GTDFree.Tray.Show")); //$NON-NLS-1$
        mi.setToolTipText(Messages.getString("GTDFree.Tray.Show.desc")); //$NON-NLS-1$
        mi.addMouseListener(hideMe);
        mi.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                trayIconPopup.setVisible(false);
                pushVisible();
            }
        });
        trayIconPopup.add(mi);

        mi = new JMenuItem();
        mi.setIcon(ApplicationHelper.getIcon(ApplicationHelper.icon_name_large_exit));
        mi.setText(Messages.getString("GTDFree.Tray.Exit")); //$NON-NLS-1$
        mi.addMouseListener(hideMe);
        mi.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                trayIconPopup.setVisible(false);
                close(false);
            }
        });
        trayIconPopup.add(mi);

    }
    return trayIcon;
}