6d154de5334df9498bf1ec14bd05dbb9067ee221
[jalview.git] / test / jalview / gui / MouseEventDemo.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.gui;
22
23 /*
24  * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
25  *
26  * Redistribution and use in source and binary forms, with or without
27  * modification, are permitted provided that the following conditions
28  * are met:
29  *
30  *   - Redistributions of source code must retain the above copyright
31  *     notice, this list of conditions and the following disclaimer.
32  *
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.
36  *
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.
40  *
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.
52  */
53
54 /*
55 * MouseEventDemo.java
56 */
57
58 import jalview.util.Platform;
59
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;
66
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;
79
80 /**
81  * Sourced from Oracle and adapted
82  * 
83  * @see https
84  *      ://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html
85  */
86 public class MouseEventDemo extends JPanel implements MouseListener
87 {
88   private class BlankArea extends JLabel
89   {
90     Dimension minSize = new Dimension(200, 100);
91
92     public BlankArea(Color color)
93     {
94       setBackground(color);
95       setOpaque(true);
96       setBorder(BorderFactory.createLineBorder(Color.black));
97     }
98
99     @Override
100     public Dimension getMinimumSize()
101     {
102       return minSize;
103     }
104
105     @Override
106     public Dimension getPreferredSize()
107     {
108       return minSize;
109     }
110   }
111
112   static int counter = 0;
113
114   BlankArea blankArea;
115
116   JTextArea textArea;
117
118   static final String NEWLINE = System.getProperty("line.separator");
119
120   public static void main(String[] args)
121   {
122     // Schedule a job for the event dispatch thread:
123     // creating and showing this application's GUI.
124     javax.swing.SwingUtilities.invokeLater(new Runnable()
125     {
126       @Override
127       public void run()
128       {
129         createAndShowGUI();
130       }
131     });
132   }
133
134   /**
135    * Create the GUI and show it. For thread safety, this method should be
136    * invoked from the event dispatch thread.
137    */
138   private static void createAndShowGUI()
139   {
140     // Create and set up the window.
141     JFrame frame = new JFrame("MouseEventDemo (C to clear)");
142     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
143
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);
148
149     // Display the window.
150     frame.pack();
151     frame.setVisible(true);
152   }
153
154   public MouseEventDemo()
155   {
156     super(new GridLayout(0, 1));
157
158     textArea = new JTextArea();
159     textArea.setEditable(false);
160     JScrollPane scrollPane = new JScrollPane(textArea);
161     scrollPane
162             .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
163     scrollPane.setPreferredSize(new Dimension(400, 75));
164
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);
171     add(splitPane);
172
173     addKeyBinding();
174
175     blankArea.addMouseListener(this);
176     addMouseListener(this);
177     setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
178   }
179
180   private void addKeyBinding()
181   {
182     addKeyBinding(KeyStroke.getKeyStroke('C'));
183     addKeyBinding(KeyStroke.getKeyStroke('c'));
184   }
185
186   /**
187    * @param ks
188    */
189   void addKeyBinding(final KeyStroke ks)
190   {
191     InputMap inputMap = this.getInputMap(JComponent.WHEN_FOCUSED);
192     inputMap.put(ks, ks);
193     this.getActionMap().put(ks, new AbstractAction()
194     {
195       @Override
196       public void actionPerformed(ActionEvent e)
197       {
198         textArea.setText("");
199         log("");
200       }
201     });
202   }
203
204   void logEvent(String eventDescription, MouseEvent e)
205   {
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());
218   }
219
220   /**
221    * @param msg
222    */
223   void log(String msg)
224   {
225     textArea.append(msg + NEWLINE);
226     textArea.setCaretPosition(textArea.getDocument().getLength());
227   }
228
229   @Override
230   public void mousePressed(MouseEvent e)
231   {
232     logEvent("Mouse pressed", e);
233   }
234
235   @Override
236   public void mouseReleased(MouseEvent e)
237   {
238     logEvent("Mouse released", e);
239   }
240
241   @Override
242   public void mouseEntered(MouseEvent e)
243   {
244   }
245
246   @Override
247   public void mouseExited(MouseEvent e)
248   {
249   }
250
251   @Override
252   public void mouseClicked(MouseEvent e)
253   {
254     logEvent("Mouse clicked", e);
255   }
256 }