JAL-3438 spotless for 2.11.2.0
[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   /**
121    * @j2sIgnore
122    */
123   public static void main(String[] args)
124   {
125     // Schedule a job for the event dispatch thread:
126     // creating and showing this application's GUI.
127     javax.swing.SwingUtilities.invokeLater(new Runnable()
128     {
129       @Override
130       public void run()
131       {
132         createAndShowGUI();
133       }
134     });
135   }
136
137   /**
138    * Create the GUI and show it. For thread safety, this method should be
139    * invoked from the event dispatch thread.
140    */
141   private static void createAndShowGUI()
142   {
143     // Create and set up the window.
144     JFrame frame = new JFrame("MouseEventDemo (C to clear)");
145     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
146
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);
151
152     // Display the window.
153     frame.pack();
154     frame.setVisible(true);
155   }
156
157   public MouseEventDemo()
158   {
159     super(new GridLayout(0, 1));
160
161     textArea = new JTextArea();
162     textArea.setEditable(false);
163     JScrollPane scrollPane = new JScrollPane(textArea);
164     scrollPane.setVerticalScrollBarPolicy(
165             JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
166     scrollPane.setPreferredSize(new Dimension(400, 75));
167
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);
174     add(splitPane);
175
176     addKeyBinding();
177
178     blankArea.addMouseListener(this);
179     addMouseListener(this);
180     setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
181   }
182
183   private void addKeyBinding()
184   {
185     addKeyBinding(KeyStroke.getKeyStroke('C'));
186     addKeyBinding(KeyStroke.getKeyStroke('c'));
187   }
188
189   /**
190    * @param ks
191    */
192   void addKeyBinding(final KeyStroke ks)
193   {
194     InputMap inputMap = this.getInputMap(JComponent.WHEN_FOCUSED);
195     inputMap.put(ks, ks);
196     this.getActionMap().put(ks, new AbstractAction()
197     {
198       @Override
199       public void actionPerformed(ActionEvent e)
200       {
201         textArea.setText("");
202         log("");
203       }
204     });
205   }
206
207   void logEvent(String eventDescription, MouseEvent e)
208   {
209     log("------- " + counter++ + ": " + eventDescription);
210     log("e.isPopupTrigger: " + e.isPopupTrigger());
211     log("SwingUtilities.isRightMouseButton: "
212             + SwingUtilities.isRightMouseButton(e));
213     log("SwingUtilities.isLeftMouseButton: "
214             + SwingUtilities.isLeftMouseButton(e));
215     log("Platform.isControlDown: " + Platform.isControlDown(e));
216     log("e.isControlDown: " + e.isControlDown());
217     log("e.isAltDown: " + e.isAltDown());
218     log("e.isMetaDown: " + e.isMetaDown());
219     log("e.isShiftDown: " + e.isShiftDown());
220     log("e.getClickCount: " + e.getClickCount());
221   }
222
223   /**
224    * @param msg
225    */
226   void log(String msg)
227   {
228     textArea.append(msg + NEWLINE);
229     textArea.setCaretPosition(textArea.getDocument().getLength());
230   }
231
232   @Override
233   public void mousePressed(MouseEvent e)
234   {
235     logEvent("Mouse pressed", e);
236   }
237
238   @Override
239   public void mouseReleased(MouseEvent e)
240   {
241     logEvent("Mouse released", e);
242   }
243
244   @Override
245   public void mouseEntered(MouseEvent e)
246   {
247   }
248
249   @Override
250   public void mouseExited(MouseEvent e)
251   {
252   }
253
254   @Override
255   public void mouseClicked(MouseEvent e)
256   {
257     logEvent("Mouse clicked", e);
258   }
259 }