Search in sources :

Example 1 with MouseInputAdapter

use of javax.swing.event.MouseInputAdapter in project jna by java-native-access.

the class ShapedWindowDemo method main.

public static void main(String[] args) {
    try {
        System.setProperty("sun.java2d.noddraw", "true");
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
    }
    final JFrame frame = new JFrame("Shaped Window Demo");
    MouseInputAdapter handler = new MouseInputAdapter() {

        private Point offset;

        private void showPopup(MouseEvent e) {
            final JPopupMenu m = new JPopupMenu();
            m.add(new AbstractAction("Hide") {

                public void actionPerformed(ActionEvent e) {
                    frame.setState(JFrame.ICONIFIED);
                }

                private static final long serialVersionUID = 1L;
            });
            m.add(new AbstractAction("Close") {

                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }

                private static final long serialVersionUID = 1L;
            });
            m.pack();
            m.show(e.getComponent(), e.getX(), e.getY());
        }

        public void mousePressed(MouseEvent e) {
            offset = e.getPoint();
            if (e.isPopupTrigger()) {
                showPopup(e);
            }
        }

        public void mouseDragged(MouseEvent e) {
            if (!SwingUtilities.isLeftMouseButton(e))
                return;
            Point where = e.getPoint();
            where.translate(-offset.x, -offset.y);
            Point loc = frame.getLocationOnScreen();
            loc.translate(where.x, where.y);
            frame.setLocation(loc.x, loc.y);
        }

        public void mouseReleased(MouseEvent e) {
            if (e.isPopupTrigger()) {
                showPopup(e);
            }
        }
    };
    frame.addMouseListener(handler);
    frame.addMouseMotionListener(handler);
    ClockFace face = new ClockFace(new Dimension(150, 150));
    frame.getContentPane().setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
    frame.getContentPane().add(face);
    frame.setUndecorated(true);
    try {
        Shape mask = new Area(new Ellipse2D.Float(0, 0, 150, 150));
        WindowUtils.setWindowMask(frame, mask);
        if (WindowUtils.isWindowAlphaSupported()) {
            WindowUtils.setWindowAlpha(frame, .7f);
        }
        frame.setIconImage(face.getIconImage());
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocation(100, 100);
        frame.setVisible(true);
    } catch (UnsatisfiedLinkError e) {
        e.printStackTrace();
        String msg = e.getMessage() + "\nError loading the JNA library";
        JTextArea area = new JTextArea(msg);
        area.setOpaque(false);
        area.setFont(UIManager.getFont("Label.font"));
        area.setEditable(false);
        area.setColumns(80);
        area.setRows(8);
        area.setWrapStyleWord(true);
        area.setLineWrap(true);
        JOptionPane.showMessageDialog(frame, new JScrollPane(area), "Library Load Error: " + System.getProperty("os.name") + "/" + System.getProperty("os.arch"), JOptionPane.ERROR_MESSAGE);
        System.exit(1);
    }
}
Also used : JScrollPane(javax.swing.JScrollPane) MouseEvent(java.awt.event.MouseEvent) Shape(java.awt.Shape) JTextArea(javax.swing.JTextArea) ActionEvent(java.awt.event.ActionEvent) Point(java.awt.Point) Dimension(java.awt.Dimension) JPopupMenu(javax.swing.JPopupMenu) Ellipse2D(java.awt.geom.Ellipse2D) Area(java.awt.geom.Area) JTextArea(javax.swing.JTextArea) JFrame(javax.swing.JFrame) AbstractAction(javax.swing.AbstractAction) MouseInputAdapter(javax.swing.event.MouseInputAdapter)

Example 2 with MouseInputAdapter

use of javax.swing.event.MouseInputAdapter in project jna by java-native-access.

the class AlphaMaskDemo2 method run.

public void run() {
    // Must find a graphics configuration with a depth of 32 bits
    GraphicsConfiguration gconfig = WindowUtils.getAlphaCompatibleGraphicsConfiguration();
    frame = new JFrame("Alpha Mask Demo");
    alphaWindow = new JWindow(frame, gconfig);
    icon = new JLabel();
    icon.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    alphaWindow.getContentPane().add(icon);
    JButton quit = new JButton("Quit");
    JLabel label = new JLabel("Drag this window by its image");
    label.setHorizontalAlignment(SwingConstants.CENTER);
    alphaWindow.getContentPane().add(label, BorderLayout.NORTH);
    alphaWindow.getContentPane().add(quit, BorderLayout.SOUTH);
    quit.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });
    MouseInputAdapter handler = new MouseInputAdapter() {

        private Point offset;

        public void mousePressed(MouseEvent e) {
            if (SwingUtilities.isLeftMouseButton(e))
                offset = e.getPoint();
        }

        public void mouseReleased(MouseEvent e) {
            offset = null;
        }

        public void mouseDragged(MouseEvent e) {
            if (offset != null) {
                Window w = (Window) e.getSource();
                Point where = e.getPoint();
                where.translate(-offset.x, -offset.y);
                Point loc = w.getLocationOnScreen();
                loc.translate(where.x, where.y);
                w.setLocation(loc.x, loc.y);
            }
        }
    };
    alphaWindow.addMouseListener(handler);
    alphaWindow.addMouseMotionListener(handler);
    JPanel p = new JPanel(new BorderLayout(8, 8));
    p.setBorder(new EmptyBorder(8, 8, 8, 8));
    p.setTransferHandler(new TransferHandler() {

        private static final long serialVersionUID = 1L;

        public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) {
            List<DataFlavor> list = Arrays.asList(transferFlavors);
            if (list.contains(URL_FLAVOR) || list.contains(URI_LIST_FLAVOR) || list.contains(DataFlavor.imageFlavor) || list.contains(DataFlavor.javaFileListFlavor)) {
                return true;
            }
            if (DataFlavor.selectBestTextFlavor(transferFlavors) != null) {
                return true;
            }
            System.err.println("No acceptable flavor found in " + Arrays.asList(transferFlavors));
            return false;
        }

        public boolean importData(JComponent comp, Transferable t) {
            try {
                if (t.isDataFlavorSupported(URL_FLAVOR)) {
                    URL url = (URL) t.getTransferData(URL_FLAVOR);
                    setImage(Toolkit.getDefaultToolkit().getImage(url));
                    return true;
                }
                if (t.isDataFlavorSupported(URI_LIST_FLAVOR)) {
                    String s = (String) t.getTransferData(URI_LIST_FLAVOR);
                    String[] uris = s.split("[\r\n]");
                    if (uris.length > 0) {
                        URL url = new URL(uris[0]);
                        setImage(Toolkit.getDefaultToolkit().getImage(url));
                        return true;
                    }
                    return false;
                }
                if (t.isDataFlavorSupported(DataFlavor.imageFlavor)) {
                    Image image = (Image) t.getTransferData(DataFlavor.imageFlavor);
                    setImage(image);
                    return true;
                }
                if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
                    List<File> files = (List<File>) t.getTransferData(DataFlavor.javaFileListFlavor);
                    File f = files.get(0);
                    URL url = new URL("file://" + f.toURI().toURL().getPath());
                    Image image = Toolkit.getDefaultToolkit().getImage(url);
                    setImage(image);
                    return true;
                }
                DataFlavor flavor = DataFlavor.selectBestTextFlavor(t.getTransferDataFlavors());
                if (flavor != null) {
                    Reader reader = flavor.getReaderForText(t);
                    char[] buf = new char[512];
                    StringBuilder b = new StringBuilder();
                    int count;
                    // encoding wrong
                    while ((count = reader.read(buf)) > 0) {
                        for (int i = 0; i < count; i++) {
                            if (buf[i] != 0)
                                b.append(buf, i, 1);
                        }
                    }
                    String html = b.toString();
                    Pattern p = Pattern.compile("<img.*src=\"([^\\\"\">]+)\"", Pattern.CANON_EQ | Pattern.UNICODE_CASE);
                    Matcher m = p.matcher(html);
                    if (m.find()) {
                        URL url = new URL(m.group(1));
                        System.out.println("Load image from " + url);
                        Image image = Toolkit.getDefaultToolkit().getImage(url);
                        setImage(image);
                        return true;
                    }
                    System.err.println("Can't parse text: " + html);
                    return false;
                }
                System.err.println("No flavor available: " + Arrays.asList(t.getTransferDataFlavors()));
            } catch (UnsupportedFlavorException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Throwable e) {
                e.printStackTrace();
            }
            return false;
        }
    });
    p.add(new JLabel("<html><center>Drop an image with an alpha channel onto this window<br>" + "You may also adjust the overall transparency with the slider</center></html>"), BorderLayout.NORTH);
    p.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    final JSlider slider = new JSlider(0, 255, 255);
    slider.addChangeListener(new ChangeListener() {

        public void stateChanged(ChangeEvent e) {
            int value = slider.getValue();
            WindowUtils.setWindowAlpha(alphaWindow, value / 255f);
        }
    });
    p.add(slider, BorderLayout.SOUTH);
    frame.getContentPane().add(p);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    centerOnScreen(frame);
    frame.setVisible(true);
    WindowUtils.setWindowTransparent(alphaWindow, true);
    alphaWindow.setLocation(frame.getX() + frame.getWidth() + 4, frame.getY());
    try {
        URL url = getClass().getResource("tardis.png");
        if (url != null) {
            setImage(Toolkit.getDefaultToolkit().getImage(url));
        }
    } catch (Exception e) {
    }
}
Also used : JPanel(javax.swing.JPanel) Matcher(java.util.regex.Matcher) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) Reader(java.io.Reader) Image(java.awt.Image) URL(java.net.URL) GraphicsConfiguration(java.awt.GraphicsConfiguration) DataFlavor(java.awt.datatransfer.DataFlavor) BorderLayout(java.awt.BorderLayout) JFrame(javax.swing.JFrame) JSlider(javax.swing.JSlider) List(java.util.List) ChangeListener(javax.swing.event.ChangeListener) EmptyBorder(javax.swing.border.EmptyBorder) Window(java.awt.Window) JWindow(javax.swing.JWindow) Pattern(java.util.regex.Pattern) MouseEvent(java.awt.event.MouseEvent) JWindow(javax.swing.JWindow) JComponent(javax.swing.JComponent) Transferable(java.awt.datatransfer.Transferable) JLabel(javax.swing.JLabel) Point(java.awt.Point) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) IOException(java.io.IOException) ActionListener(java.awt.event.ActionListener) ChangeEvent(javax.swing.event.ChangeEvent) TransferHandler(javax.swing.TransferHandler) File(java.io.File) MouseInputAdapter(javax.swing.event.MouseInputAdapter)

Example 3 with MouseInputAdapter

use of javax.swing.event.MouseInputAdapter in project JMRI by JMRI.

the class ControlPanel method initGUI.

/**
     * Create, initialize and place GUI components.
     */
private void initGUI() {
    mainPanel = new JPanel();
    this.setContentPane(mainPanel);
    mainPanel.setLayout(new BorderLayout());
    this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    speedControlPanel = new JPanel();
    speedControlPanel.setLayout(new BoxLayout(speedControlPanel, BoxLayout.X_AXIS));
    speedControlPanel.setOpaque(false);
    mainPanel.add(speedControlPanel, BorderLayout.CENTER);
    sliderPanel = new JPanel();
    sliderPanel.setLayout(new GridBagLayout());
    sliderPanel.setOpaque(false);
    speedSlider = new JSlider(0, intSpeedSteps);
    speedSlider.setOpaque(false);
    speedSlider.setValue(0);
    speedSlider.setFocusable(false);
    // add mouse-wheel support
    speedSlider.addMouseWheelListener(new MouseWheelListener() {

        @Override
        public void mouseWheelMoved(MouseWheelEvent e) {
            if (e.getWheelRotation() > 0) {
                for (int i = 0; i < e.getScrollAmount(); i++) {
                    decelerate1();
                }
            } else {
                for (int i = 0; i < e.getScrollAmount(); i++) {
                    accelerate1();
                }
            }
        }
    });
    speedSliderContinuous = new JSlider(-intSpeedSteps, intSpeedSteps);
    speedSliderContinuous.setValue(0);
    speedSliderContinuous.setOpaque(false);
    speedSliderContinuous.setFocusable(false);
    // add mouse-wheel support
    speedSliderContinuous.addMouseWheelListener(new MouseWheelListener() {

        @Override
        public void mouseWheelMoved(MouseWheelEvent e) {
            if (e.getWheelRotation() > 0) {
                for (int i = 0; i < e.getScrollAmount(); i++) {
                    decelerate1();
                }
            } else {
                for (int i = 0; i < e.getScrollAmount(); i++) {
                    accelerate1();
                }
            }
        }
    });
    speedSpinner = new JSpinner();
    speedSpinnerModel = new SpinnerNumberModel(0, 0, intSpeedSteps, 1);
    speedSpinner.setModel(speedSpinnerModel);
    speedSpinner.setFocusable(false);
    SpeedStep128Button = new JRadioButton(Bundle.getMessage("Button128SS"));
    SpeedStep28Button = new JRadioButton(Bundle.getMessage("Button28SS"));
    SpeedStep27Button = new JRadioButton(Bundle.getMessage("Button27SS"));
    SpeedStep14Button = new JRadioButton(Bundle.getMessage("Button14SS"));
    forwardButton = new JRadioButton();
    if (jmri.jmrit.throttle.ThrottleFrameManager.instance().getThrottlesPreferences().isUsingExThrottle() && jmri.jmrit.throttle.ThrottleFrameManager.instance().getThrottlesPreferences().isUsingFunctionIcon()) {
        forwardButton.setBorderPainted(false);
        forwardButton.setContentAreaFilled(false);
        forwardButton.setText(null);
        forwardButton.setIcon(new ImageIcon(FileUtil.findURL("resources/icons/throttles/up-red.png")));
        forwardButton.setSelectedIcon(new ImageIcon(FileUtil.findURL("resources/icons/throttles/up-green.png")));
        forwardButton.setPreferredSize(new Dimension(BUTTON_SIZE, BUTTON_SIZE));
        forwardButton.setToolTipText(Bundle.getMessage("ButtonForward"));
    } else {
        forwardButton.setText(Bundle.getMessage("ButtonForward"));
    }
    reverseButton = new JRadioButton();
    if (jmri.jmrit.throttle.ThrottleFrameManager.instance().getThrottlesPreferences().isUsingExThrottle() && jmri.jmrit.throttle.ThrottleFrameManager.instance().getThrottlesPreferences().isUsingFunctionIcon()) {
        reverseButton.setBorderPainted(false);
        reverseButton.setContentAreaFilled(false);
        reverseButton.setText(null);
        reverseButton.setIcon(new ImageIcon(FileUtil.findURL("resources/icons/throttles/down-red.png")));
        reverseButton.setSelectedIcon(new ImageIcon(FileUtil.findURL("resources/icons/throttles/down-green.png")));
        reverseButton.setPreferredSize(new Dimension(BUTTON_SIZE, BUTTON_SIZE));
        reverseButton.setToolTipText(Bundle.getMessage("ButtonReverse"));
    } else {
        reverseButton.setText(Bundle.getMessage("ButtonReverse"));
    }
    propertiesPopup = new JPopupMenu();
    GridBagConstraints constraints = new GridBagConstraints();
    constraints.anchor = GridBagConstraints.CENTER;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.gridheight = 1;
    constraints.gridwidth = 1;
    constraints.ipadx = 0;
    constraints.ipady = 0;
    Insets insets = new Insets(2, 2, 2, 2);
    constraints.insets = insets;
    constraints.weightx = 1;
    constraints.weighty = 1;
    constraints.gridx = 0;
    constraints.gridy = 0;
    sliderPanel.add(speedSlider, constraints);
    //this.getContentPane().add(sliderPanel,BorderLayout.CENTER);
    speedControlPanel.add(sliderPanel);
    speedSlider.setOrientation(JSlider.VERTICAL);
    speedSlider.setMajorTickSpacing(maxSpeed / 2);
    java.util.Hashtable<Integer, JLabel> labelTable = new java.util.Hashtable<Integer, JLabel>();
    labelTable.put(Integer.valueOf(maxSpeed / 2), new JLabel("50%"));
    labelTable.put(Integer.valueOf(maxSpeed), new JLabel("100%"));
    labelTable.put(Integer.valueOf(0), new JLabel(Bundle.getMessage("LabelStop")));
    speedSlider.setLabelTable(labelTable);
    speedSlider.setPaintTicks(true);
    speedSlider.setPaintLabels(true);
    // remove old actions
    speedSlider.addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent e) {
            if (!internalAdjust) {
                boolean doIt = false;
                if (!speedSlider.getValueIsAdjusting()) {
                    doIt = true;
                    lastTrackedSliderMovementTime = System.currentTimeMillis() - trackSliderMinInterval;
                } else if (trackSlider && System.currentTimeMillis() - lastTrackedSliderMovementTime >= trackSliderMinInterval) {
                    doIt = true;
                    lastTrackedSliderMovementTime = System.currentTimeMillis();
                }
                if (doIt) {
                    float newSpeed = (speedSlider.getValue() / (intSpeedSteps * 1.0f));
                    if (log.isDebugEnabled()) {
                        log.debug("stateChanged: slider pos: " + speedSlider.getValue() + " speed: " + newSpeed);
                    }
                    if (sliderPanel.isVisible() && throttle != null) {
                        throttle.setSpeedSetting(newSpeed);
                    }
                    if (speedSpinner != null) {
                        speedSpinnerModel.setValue(Integer.valueOf(speedSlider.getValue()));
                    }
                    if (speedSliderContinuous != null) {
                        if (forwardButton.isSelected()) {
                            speedSliderContinuous.setValue(((Integer) speedSlider.getValue()).intValue());
                        } else {
                            speedSliderContinuous.setValue(-((Integer) speedSlider.getValue()).intValue());
                        }
                    }
                }
            }
        }
    });
    speedSliderContinuousPanel = new JPanel();
    speedSliderContinuousPanel.setLayout(new GridBagLayout());
    constraints = new GridBagConstraints();
    constraints.anchor = GridBagConstraints.CENTER;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.gridheight = 1;
    constraints.gridwidth = 1;
    constraints.ipadx = 0;
    constraints.ipady = 0;
    insets = new Insets(2, 2, 2, 2);
    constraints.insets = insets;
    constraints.weightx = 1;
    constraints.weighty = 1;
    constraints.gridx = 0;
    constraints.gridy = 0;
    speedSliderContinuousPanel.add(speedSliderContinuous, constraints);
    //this.getContentPane().add(sliderPanel,BorderLayout.CENTER);
    speedControlPanel.add(speedSliderContinuousPanel);
    speedSliderContinuous.setOrientation(JSlider.VERTICAL);
    speedSliderContinuous.setMajorTickSpacing(maxSpeed / 2);
    labelTable = new java.util.Hashtable<Integer, JLabel>();
    labelTable.put(Integer.valueOf(maxSpeed / 2), new JLabel("50%"));
    labelTable.put(Integer.valueOf(maxSpeed), new JLabel("100%"));
    labelTable.put(Integer.valueOf(0), new JLabel(Bundle.getMessage("LabelStop")));
    labelTable.put(Integer.valueOf(-maxSpeed / 2), new JLabel("-50%"));
    labelTable.put(Integer.valueOf(-maxSpeed), new JLabel("-100%"));
    speedSliderContinuous.setLabelTable(labelTable);
    speedSliderContinuous.setPaintTicks(true);
    speedSliderContinuous.setPaintLabels(true);
    // remove old actions
    speedSliderContinuous.addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent e) {
            if (!internalAdjust) {
                boolean doIt = false;
                if (!speedSliderContinuous.getValueIsAdjusting()) {
                    doIt = true;
                    lastTrackedSliderMovementTime = System.currentTimeMillis() - trackSliderMinInterval;
                } else if (trackSlider && System.currentTimeMillis() - lastTrackedSliderMovementTime >= trackSliderMinInterval) {
                    doIt = true;
                    lastTrackedSliderMovementTime = System.currentTimeMillis();
                }
                if (doIt) {
                    float newSpeed = (java.lang.Math.abs(speedSliderContinuous.getValue()) / (intSpeedSteps * 1.0f));
                    boolean newDir = (speedSliderContinuous.getValue() >= 0);
                    if (log.isDebugEnabled()) {
                        log.debug("stateChanged: slider pos: " + speedSliderContinuous.getValue() + " speed: " + newSpeed + " dir: " + newDir);
                    }
                    if (speedSliderContinuousPanel.isVisible() && throttle != null) {
                        throttle.setSpeedSetting(newSpeed);
                        if ((newSpeed > 0) && (newDir != forwardButton.isSelected())) {
                            throttle.setIsForward(newDir);
                        }
                    }
                    if (speedSpinner != null) {
                        speedSpinnerModel.setValue(Integer.valueOf(java.lang.Math.abs(speedSliderContinuous.getValue())));
                    }
                    if (speedSlider != null) {
                        speedSlider.setValue(Integer.valueOf(java.lang.Math.abs(speedSliderContinuous.getValue())));
                    }
                }
            }
        }
    });
    spinnerPanel = new JPanel();
    spinnerPanel.setLayout(new GridBagLayout());
    spinnerPanel.add(speedSpinner, constraints);
    speedControlPanel.add(spinnerPanel);
    // remove old actions
    speedSpinner.addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent e) {
            if (!internalAdjust) {
                //if (!speedSpinner.getValueIsAdjusting())
                //{
                float newSpeed = ((Integer) speedSpinner.getValue()).floatValue() / (intSpeedSteps * 1.0f);
                if (log.isDebugEnabled()) {
                    log.debug("stateChanged: spinner pos: " + speedSpinner.getValue() + " speed: " + newSpeed);
                }
                if (throttle != null) {
                    if (spinnerPanel.isVisible()) {
                        throttle.setSpeedSetting(newSpeed);
                    }
                    speedSlider.setValue(((Integer) speedSpinner.getValue()).intValue());
                    if (speedSliderContinuous != null) {
                        if (forwardButton.isSelected()) {
                            speedSliderContinuous.setValue(((Integer) speedSpinner.getValue()).intValue());
                        } else {
                            speedSliderContinuous.setValue(-((Integer) speedSpinner.getValue()).intValue());
                        }
                    }
                } else {
                    log.warn("no throttle object in stateChanged, ignoring change of speed to " + newSpeed);
                }
            //}
            }
        }
    });
    ButtonGroup speedStepButtons = new ButtonGroup();
    speedStepButtons.add(SpeedStep128Button);
    speedStepButtons.add(SpeedStep28Button);
    speedStepButtons.add(SpeedStep27Button);
    speedStepButtons.add(SpeedStep14Button);
    constraints.fill = GridBagConstraints.NONE;
    constraints.gridy = 1;
    spinnerPanel.add(SpeedStep128Button, constraints);
    constraints.gridy = 2;
    spinnerPanel.add(SpeedStep28Button, constraints);
    constraints.gridy = 3;
    spinnerPanel.add(SpeedStep27Button, constraints);
    constraints.gridy = 4;
    spinnerPanel.add(SpeedStep14Button, constraints);
    SpeedStep14Button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            setSpeedStepsMode(DccThrottle.SpeedStepMode14);
            throttle.setSpeedStepMode(DccThrottle.SpeedStepMode14);
        }
    });
    SpeedStep27Button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            setSpeedStepsMode(DccThrottle.SpeedStepMode27);
            throttle.setSpeedStepMode(DccThrottle.SpeedStepMode27);
        }
    });
    SpeedStep28Button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            setSpeedStepsMode(DccThrottle.SpeedStepMode28);
            throttle.setSpeedStepMode(DccThrottle.SpeedStepMode28);
        }
    });
    SpeedStep128Button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            setSpeedStepsMode(DccThrottle.SpeedStepMode128);
            throttle.setSpeedStepMode(DccThrottle.SpeedStepMode128);
        }
    });
    buttonPanel = new JPanel();
    buttonPanel.setLayout(new GridBagLayout());
    mainPanel.add(buttonPanel, BorderLayout.SOUTH);
    ButtonGroup directionButtons = new ButtonGroup();
    directionButtons.add(forwardButton);
    directionButtons.add(reverseButton);
    constraints.fill = GridBagConstraints.NONE;
    constraints.gridy = 1;
    if (jmri.jmrit.throttle.ThrottleFrameManager.instance().getThrottlesPreferences().isUsingExThrottle() && jmri.jmrit.throttle.ThrottleFrameManager.instance().getThrottlesPreferences().isUsingFunctionIcon()) {
        constraints.gridx = 3;
    }
    buttonPanel.add(forwardButton, constraints);
    if (jmri.jmrit.throttle.ThrottleFrameManager.instance().getThrottlesPreferences().isUsingExThrottle() && jmri.jmrit.throttle.ThrottleFrameManager.instance().getThrottlesPreferences().isUsingFunctionIcon()) {
        constraints.gridx = 1;
    } else {
        constraints.gridy = 2;
    }
    buttonPanel.add(reverseButton, constraints);
    if (jmri.jmrit.throttle.ThrottleFrameManager.instance().getThrottlesPreferences().isUsingExThrottle() && jmri.jmrit.throttle.ThrottleFrameManager.instance().getThrottlesPreferences().isUsingFunctionIcon()) {
        constraints.gridx = 2;
    }
    forwardButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            throttle.setIsForward(true);
            if (speedSliderContinuous != null) {
                speedSliderContinuous.setValue(java.lang.Math.abs(speedSliderContinuous.getValue()));
            }
        }
    });
    reverseButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            throttle.setIsForward(false);
            if (speedSliderContinuous != null) {
                speedSliderContinuous.setValue(-java.lang.Math.abs(speedSliderContinuous.getValue()));
            }
        }
    });
    stopButton = new JButton();
    if (jmri.jmrit.throttle.ThrottleFrameManager.instance().getThrottlesPreferences().isUsingExThrottle() && jmri.jmrit.throttle.ThrottleFrameManager.instance().getThrottlesPreferences().isUsingFunctionIcon()) {
        stopButton.setBorderPainted(false);
        stopButton.setContentAreaFilled(false);
        stopButton.setText(null);
        stopButton.setIcon(new ImageIcon(FileUtil.findURL("resources/icons/throttles/estop.png")));
        stopButton.setPressedIcon(new ImageIcon(FileUtil.findURL("resources/icons/throttles/estop24.png")));
        stopButton.setPreferredSize(new Dimension(BUTTON_SIZE, BUTTON_SIZE));
        stopButton.setToolTipText(Bundle.getMessage("ButtonEStop"));
    } else {
        stopButton.setText(Bundle.getMessage("ButtonEStop"));
    }
    constraints.gridy = 4;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    buttonPanel.add(stopButton, constraints);
    stopButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            stop();
        }
    });
    stopButton.addMouseListener(new MouseListener() {

        @Override
        public void mousePressed(MouseEvent e) {
            stop();
        }

        @Override
        public void mouseExited(MouseEvent e) {
        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseReleased(MouseEvent e) {
        }

        @Override
        public void mouseClicked(MouseEvent e) {
        }
    });
    idleButton = new JButton();
    if (jmri.jmrit.throttle.ThrottleFrameManager.instance().getThrottlesPreferences().isUsingExThrottle() && jmri.jmrit.throttle.ThrottleFrameManager.instance().getThrottlesPreferences().isUsingFunctionIcon()) {
        idleButton.setBorderPainted(false);
        idleButton.setContentAreaFilled(false);
        idleButton.setText(null);
        idleButton.setIcon(new ImageIcon(FileUtil.findURL("resources/icons/throttles/stop.png")));
        idleButton.setPressedIcon(new ImageIcon(FileUtil.findURL("resources/icons/throttles/stop24.png")));
        idleButton.setPreferredSize(new Dimension(BUTTON_SIZE, BUTTON_SIZE));
        idleButton.setToolTipText(Bundle.getMessage("ButtonIdle"));
    } else {
        idleButton.setText(Bundle.getMessage("ButtonIdle"));
    }
    if (jmri.jmrit.throttle.ThrottleFrameManager.instance().getThrottlesPreferences().isUsingExThrottle() && jmri.jmrit.throttle.ThrottleFrameManager.instance().getThrottlesPreferences().isUsingFunctionIcon()) {
        constraints.gridy = 1;
    } else {
        constraints.gridy = 3;
    }
    buttonPanel.add(idleButton, constraints);
    idleButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            speedSlider.setValue(0);
            if (speedSpinner != null) {
                speedSpinner.setValue(Integer.valueOf(0));
            }
            if (speedSliderContinuous != null) {
                speedSliderContinuous.setValue(Integer.valueOf(0));
            }
            throttle.setSpeedSetting(0);
        }
    });
    addComponentListener(new ComponentAdapter() {

        @Override
        public void componentResized(ComponentEvent e) {
            changeOrientation();
        }
    });
    JMenuItem propertiesItem = new JMenuItem(Bundle.getMessage("ControlPanelProperties"));
    propertiesItem.addActionListener(this);
    propertiesPopup.add(propertiesItem);
    // Add a mouse listener all components to trigger the popup menu.
    MouseInputAdapter popupListener = new PopupListener(propertiesPopup, this);
    MouseInputAdapterInstaller.installMouseInputAdapterOnAllComponents(popupListener, this);
    // Install the Key bindings on all Components
    KeyListenerInstaller.installKeyListenerOnAllComponents(new ControlPadKeyListener(), this);
    // set by default which speed selection method is on top
    setSpeedController(_displaySlider);
}
Also used : JPanel(javax.swing.JPanel) ImageIcon(javax.swing.ImageIcon) GridBagConstraints(java.awt.GridBagConstraints) JRadioButton(javax.swing.JRadioButton) Insets(java.awt.Insets) GridBagLayout(java.awt.GridBagLayout) ActionEvent(java.awt.event.ActionEvent) BoxLayout(javax.swing.BoxLayout) JButton(javax.swing.JButton) MouseWheelListener(java.awt.event.MouseWheelListener) SpinnerNumberModel(javax.swing.SpinnerNumberModel) MouseListener(java.awt.event.MouseListener) BorderLayout(java.awt.BorderLayout) JSlider(javax.swing.JSlider) ChangeListener(javax.swing.event.ChangeListener) JMenuItem(javax.swing.JMenuItem) ComponentAdapter(java.awt.event.ComponentAdapter) MouseEvent(java.awt.event.MouseEvent) JLabel(javax.swing.JLabel) Dimension(java.awt.Dimension) JPopupMenu(javax.swing.JPopupMenu) MouseWheelEvent(java.awt.event.MouseWheelEvent) ChangeEvent(javax.swing.event.ChangeEvent) ActionListener(java.awt.event.ActionListener) ButtonGroup(javax.swing.ButtonGroup) JSpinner(javax.swing.JSpinner) ComponentEvent(java.awt.event.ComponentEvent) MouseInputAdapter(javax.swing.event.MouseInputAdapter)

Example 4 with MouseInputAdapter

use of javax.swing.event.MouseInputAdapter in project artisynth_core by artisynth.

the class ViewerFrame method addPopupManager.

public void addPopupManager() {
    myPopupManager = new ViewerPopupManager(viewer);
    viewer.addMouseInputListener(new MouseInputAdapter() {

        public void mousePressed(MouseEvent e) {
            if (e.getButton() == MouseEvent.BUTTON3) {
                displayPopup(e);
            }
        }
    });
}
Also used : MouseEvent(java.awt.event.MouseEvent) MouseInputAdapter(javax.swing.event.MouseInputAdapter)

Example 5 with MouseInputAdapter

use of javax.swing.event.MouseInputAdapter in project runelite by runelite.

the class HiscorePanel method makeSkillPanel.

private JPanel makeSkillPanel(String skillName, HiscoreSkill skill) {
    JLabel label = new JLabel();
    label.setText("--");
    // Store the skill that the label displays so we can tell them apart
    label.putClientProperty(SKILL_NAME, skillName);
    label.putClientProperty(SKILL, skill);
    String skillIcon = "skill_icons_small/" + skillName.toLowerCase() + ".png";
    log.debug("Loading skill icon from {}", skillIcon);
    try {
        BufferedImage icon;
        synchronized (ImageIO.class) {
            icon = ImageIO.read(HiscorePanel.class.getResourceAsStream(skillIcon));
        }
        label.setIcon(new ImageIcon(icon));
    } catch (IOException ex) {
        log.warn(null, ex);
    }
    // Show skill details on hover
    label.addMouseListener(new MouseInputAdapter() {

        @Override
        public void mouseEntered(MouseEvent e) {
            JLabel source = (JLabel) e.getSource();
            String skillName = (String) source.getClientProperty(SKILL_NAME);
            HiscoreSkill skill = (HiscoreSkill) label.getClientProperty(SKILL);
            changeDetail(skillName, skill);
        }
    });
    skillLabels.add(label);
    JPanel skillPanel = new JPanel();
    skillPanel.add(skillLabels.get(skillLabels.size() - 1));
    return skillPanel;
}
Also used : ImageIcon(javax.swing.ImageIcon) JPanel(javax.swing.JPanel) MouseEvent(java.awt.event.MouseEvent) JLabel(javax.swing.JLabel) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) MouseInputAdapter(javax.swing.event.MouseInputAdapter) HiscoreSkill(net.runelite.http.api.hiscore.HiscoreSkill) ImageIO(javax.imageio.ImageIO)

Aggregations

MouseInputAdapter (javax.swing.event.MouseInputAdapter)13 MouseEvent (java.awt.event.MouseEvent)12 BorderLayout (java.awt.BorderLayout)7 JPanel (javax.swing.JPanel)7 JLabel (javax.swing.JLabel)6 Point (java.awt.Point)5 JSlider (javax.swing.JSlider)5 ActionEvent (java.awt.event.ActionEvent)4 URL (java.net.URL)4 JFrame (javax.swing.JFrame)4 ChangeEvent (javax.swing.event.ChangeEvent)4 ChangeListener (javax.swing.event.ChangeListener)4 Dimension (java.awt.Dimension)3 ActionListener (java.awt.event.ActionListener)3 EmptyBorder (javax.swing.border.EmptyBorder)3 GraphicsConfiguration (java.awt.GraphicsConfiguration)2 GridBagConstraints (java.awt.GridBagConstraints)2 GridBagLayout (java.awt.GridBagLayout)2 Image (java.awt.Image)2 Insets (java.awt.Insets)2