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;
61 * Sourced from Oracle and adapted
64 * ://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html
66 public class MouseEventDemo extends JPanel implements MouseListener
68 private class BlankArea extends JLabel
70 Dimension minSize = new Dimension(200, 100);
72 public BlankArea(Color color)
76 setBorder(BorderFactory.createLineBorder(Color.black));
80 public Dimension getMinimumSize()
86 public Dimension getPreferredSize()
92 static int counter = 0;
98 static final String NEWLINE = System.getProperty("line.separator");
100 public static void main(String[] args)
102 // Schedule a job for the event dispatch thread:
103 // creating and showing this application's GUI.
104 javax.swing.SwingUtilities.invokeLater(new Runnable()
115 * Create the GUI and show it. For thread safety, this method should be
116 * invoked from the event dispatch thread.
118 private static void createAndShowGUI()
120 // Create and set up the window.
121 JFrame frame = new JFrame("MouseEventDemo (C to clear)");
122 frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
124 // Create and set up the content pane.
125 JComponent newContentPane = new MouseEventDemo();
126 newContentPane.setOpaque(true); // content panes must be opaque
127 frame.setContentPane(newContentPane);
129 // Display the window.
131 frame.setVisible(true);
134 public MouseEventDemo()
136 super(new GridLayout(0, 1));
138 textArea = new JTextArea();
139 textArea.setEditable(false);
140 JScrollPane scrollPane = new JScrollPane(textArea);
142 .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
143 scrollPane.setPreferredSize(new Dimension(400, 75));
145 blankArea = new BlankArea(Color.YELLOW);
146 JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
147 blankArea, scrollPane);
148 splitPane.setVisible(true);
149 splitPane.setDividerLocation(0.2d);
150 splitPane.setResizeWeight(0.5d);
155 blankArea.addMouseListener(this);
156 addMouseListener(this);
157 setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
160 private void addKeyBinding()
162 addKeyBinding(KeyStroke.getKeyStroke('C'));
163 addKeyBinding(KeyStroke.getKeyStroke('c'));
169 void addKeyBinding(final KeyStroke ks)
171 InputMap inputMap = this.getInputMap(JComponent.WHEN_FOCUSED);
172 inputMap.put(ks, ks);
173 this.getActionMap().put(ks, new AbstractAction()
176 public void actionPerformed(ActionEvent e)
178 textArea.setText("");
184 void logEvent(String eventDescription, MouseEvent e)
186 log("------- " + counter++ + ": " + eventDescription);
187 log("e.isPopupTrigger: " + e.isPopupTrigger());
188 log("SwingUtilities.isRightMouseButton: "
189 + SwingUtilities.isRightMouseButton(e));
190 log("SwingUtilities.isLeftMouseButton: "
191 + SwingUtilities.isLeftMouseButton(e));
192 log("Platform.isControlDown: " + Platform.isControlDown(e));
193 log("e.isControlDown: " + e.isControlDown());
194 log("e.isAltDown: " + e.isAltDown());
195 log("e.isMetaDown: " + e.isMetaDown());
196 log("e.isShiftDown: " + e.isShiftDown());
197 log("e.getClickCount: " + e.getClickCount());
205 textArea.append(msg + NEWLINE);
206 textArea.setCaretPosition(textArea.getDocument().getLength());
210 public void mousePressed(MouseEvent e)
212 logEvent("Mouse pressed", e);
216 public void mouseReleased(MouseEvent e)
218 logEvent("Mouse released", e);
222 public void mouseEntered(MouseEvent e)
227 public void mouseExited(MouseEvent e)
232 public void mouseClicked(MouseEvent e)
234 logEvent("Mouse clicked", e);