4 * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * - Neither the name of Oracle or the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 import jalview.util.Platform;
40 import java.awt.Color;
41 import java.awt.Dimension;
42 import java.awt.GridLayout;
43 import java.awt.event.ActionEvent;
44 import java.awt.event.MouseEvent;
45 import java.awt.event.MouseListener;
47 import javax.swing.AbstractAction;
48 import javax.swing.BorderFactory;
49 import javax.swing.InputMap;
50 import javax.swing.JComponent;
51 import javax.swing.JFrame;
52 import javax.swing.JLabel;
53 import javax.swing.JPanel;
54 import javax.swing.JScrollPane;
55 import javax.swing.JSplitPane;
56 import javax.swing.JTextArea;
57 import javax.swing.KeyStroke;
58 import javax.swing.SwingUtilities;
59 import javax.swing.UIManager;
60 import javax.swing.UnsupportedLookAndFeelException;
63 * Sourced from Oracle and adapted
66 * ://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html
68 public class MouseEventDemo extends JPanel implements MouseListener
70 private class BlankArea extends JLabel
72 Dimension minSize = new Dimension(200, 100);
74 public BlankArea(Color color)
78 setBorder(BorderFactory.createLineBorder(Color.black));
82 public Dimension getMinimumSize()
88 public Dimension getPreferredSize()
94 static int counter = 0;
100 static final String NEWLINE = System.getProperty("line.separator");
102 public static void main(String[] args)
104 /* Use an appropriate Look and Feel */
107 // UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
108 // UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
109 UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
110 } catch (UnsupportedLookAndFeelException ex)
112 ex.printStackTrace();
113 } catch (IllegalAccessException ex)
115 ex.printStackTrace();
116 } catch (InstantiationException ex)
118 ex.printStackTrace();
119 } catch (ClassNotFoundException ex)
121 ex.printStackTrace();
123 /* Turn off metal's use of bold fonts */
124 UIManager.put("swing.boldMetal", Boolean.FALSE);
125 // Schedule a job for the event dispatch thread:
126 // creating and showing this application's GUI.
127 javax.swing.SwingUtilities.invokeLater(new Runnable()
138 * Create the GUI and show it. For thread safety, this method should be
139 * invoked from the event dispatch thread.
141 private static void createAndShowGUI()
143 // Create and set up the window.
144 JFrame frame = new JFrame("MouseEventDemo (C to clear)");
145 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
147 // Create and set up the content pane.
148 JComponent newContentPane = new MouseEventDemo();
149 newContentPane.setOpaque(true); // content panes must be opaque
150 frame.setContentPane(newContentPane);
152 // Display the window.
154 frame.setVisible(true);
157 public MouseEventDemo()
159 super(new GridLayout(0, 1));
161 textArea = new JTextArea();
162 textArea.setEditable(false);
163 JScrollPane scrollPane = new JScrollPane(textArea);
165 .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
166 scrollPane.setPreferredSize(new Dimension(400, 75));
168 blankArea = new BlankArea(Color.YELLOW);
169 JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
170 blankArea, scrollPane);
171 splitPane.setVisible(true);
172 splitPane.setDividerLocation(0.2d);
173 splitPane.setResizeWeight(0.5d);
178 blankArea.addMouseListener(this);
179 addMouseListener(this);
180 setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
183 private void addKeyBinding()
185 addKeyBinding(KeyStroke.getKeyStroke('C'));
186 addKeyBinding(KeyStroke.getKeyStroke('c'));
192 void addKeyBinding(final KeyStroke ks)
194 InputMap inputMap = this.getInputMap(JComponent.WHEN_FOCUSED);
195 inputMap.put(ks, ks);
196 this.getActionMap().put(ks, new AbstractAction()
199 public void actionPerformed(ActionEvent e)
201 textArea.setText("");
207 void logEvent(String eventDescription, MouseEvent e)
209 // textArea.append(eventDescription + " detected on "
210 // + e.getComponent().getClass().getName() + "." + NEWLINE);
211 log("------- " + counter++ + ": " + eventDescription);
212 log("e.isPopupTrigger: " + e.isPopupTrigger());
213 log("SwingUtilities.isRightMouseButton: "
214 + SwingUtilities.isRightMouseButton(e));
215 log("SwingUtilities.isLeftMouseButton: "
216 + SwingUtilities.isLeftMouseButton(e));
217 log("Platform.isControlDown: " + Platform.isControlDown(e));
218 log("e.isControlDown: " + e.isControlDown());
219 log("e.isAltDown: " + e.isAltDown());
220 log("e.isMetaDown: " + e.isMetaDown());
221 log("e.isShiftDown: " + e.isShiftDown());
222 log("e.getClickCount: " + e.getClickCount());
230 textArea.append(msg + NEWLINE);
231 textArea.setCaretPosition(textArea.getDocument().getLength());
235 public void mousePressed(MouseEvent e)
237 logEvent("Mouse pressed", e);
241 public void mouseReleased(MouseEvent e)
243 logEvent("Mouse released", e);
247 public void mouseEntered(MouseEvent e)
249 // eventOutput("Mouse entered", e);
253 public void mouseExited(MouseEvent e)
255 // eventOutput("Mouse exited", e);
259 public void mouseClicked(MouseEvent e)
261 logEvent("Mouse clicked", e);