2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
24 * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
30 * - Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
33 * - Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
37 * - Neither the name of Oracle or the names of its
38 * contributors may be used to endorse or promote products derived
39 * from this software without specific prior written permission.
41 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
42 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
43 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
45 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
46 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
47 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
48 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
49 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
50 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
51 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 import jalview.util.Platform;
60 import java.awt.Color;
61 import java.awt.Dimension;
62 import java.awt.GridLayout;
63 import java.awt.event.ActionEvent;
64 import java.awt.event.MouseEvent;
65 import java.awt.event.MouseListener;
67 import javax.swing.AbstractAction;
68 import javax.swing.BorderFactory;
69 import javax.swing.InputMap;
70 import javax.swing.JComponent;
71 import javax.swing.JFrame;
72 import javax.swing.JLabel;
73 import javax.swing.JPanel;
74 import javax.swing.JScrollPane;
75 import javax.swing.JSplitPane;
76 import javax.swing.JTextArea;
77 import javax.swing.KeyStroke;
78 import javax.swing.SwingUtilities;
81 * Sourced from Oracle and adapted
84 * ://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html
86 public class MouseEventDemo extends JPanel implements MouseListener
88 private class BlankArea extends JLabel
90 Dimension minSize = new Dimension(200, 100);
92 public BlankArea(Color color)
96 setBorder(BorderFactory.createLineBorder(Color.black));
100 public Dimension getMinimumSize()
106 public Dimension getPreferredSize()
112 static int counter = 0;
118 static final String NEWLINE = System.getProperty("line.separator");
120 public static void main(String[] args)
122 // Schedule a job for the event dispatch thread:
123 // creating and showing this application's GUI.
124 javax.swing.SwingUtilities.invokeLater(new Runnable()
135 * Create the GUI and show it. For thread safety, this method should be
136 * invoked from the event dispatch thread.
138 private static void createAndShowGUI()
140 // Create and set up the window.
141 JFrame frame = new JFrame("MouseEventDemo (C to clear)");
142 frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
144 // Create and set up the content pane.
145 JComponent newContentPane = new MouseEventDemo();
146 newContentPane.setOpaque(true); // content panes must be opaque
147 frame.setContentPane(newContentPane);
149 // Display the window.
151 frame.setVisible(true);
154 public MouseEventDemo()
156 super(new GridLayout(0, 1));
158 textArea = new JTextArea();
159 textArea.setEditable(false);
160 JScrollPane scrollPane = new JScrollPane(textArea);
162 .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
163 scrollPane.setPreferredSize(new Dimension(400, 75));
165 blankArea = new BlankArea(Color.YELLOW);
166 JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
167 blankArea, scrollPane);
168 splitPane.setVisible(true);
169 splitPane.setDividerLocation(0.2d);
170 splitPane.setResizeWeight(0.5d);
175 blankArea.addMouseListener(this);
176 addMouseListener(this);
177 setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
180 private void addKeyBinding()
182 addKeyBinding(KeyStroke.getKeyStroke('C'));
183 addKeyBinding(KeyStroke.getKeyStroke('c'));
189 void addKeyBinding(final KeyStroke ks)
191 InputMap inputMap = this.getInputMap(JComponent.WHEN_FOCUSED);
192 inputMap.put(ks, ks);
193 this.getActionMap().put(ks, new AbstractAction()
196 public void actionPerformed(ActionEvent e)
198 textArea.setText("");
204 void logEvent(String eventDescription, MouseEvent e)
206 log("------- " + counter++ + ": " + eventDescription);
207 log("e.isPopupTrigger: " + e.isPopupTrigger());
208 log("SwingUtilities.isRightMouseButton: "
209 + SwingUtilities.isRightMouseButton(e));
210 log("SwingUtilities.isLeftMouseButton: "
211 + SwingUtilities.isLeftMouseButton(e));
212 log("Platform.isControlDown: " + Platform.isControlDown(e));
213 log("e.isControlDown: " + e.isControlDown());
214 log("e.isAltDown: " + e.isAltDown());
215 log("e.isMetaDown: " + e.isMetaDown());
216 log("e.isShiftDown: " + e.isShiftDown());
217 log("e.getClickCount: " + e.getClickCount());
225 textArea.append(msg + NEWLINE);
226 textArea.setCaretPosition(textArea.getDocument().getLength());
230 public void mousePressed(MouseEvent e)
232 logEvent("Mouse pressed", e);
236 public void mouseReleased(MouseEvent e)
238 logEvent("Mouse released", e);
242 public void mouseEntered(MouseEvent e)
247 public void mouseExited(MouseEvent e)
252 public void mouseClicked(MouseEvent e)
254 logEvent("Mouse clicked", e);