3667b52072ad0aa0ba4450853a52033b4f50ed57
[jalview.git] / test / jalview / gui / MouseEventDemo.java
1 package jalview.gui;
2
3 /*
4  * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  *   - Redistributions of source code must retain the above copyright
11  *     notice, this list of conditions and the following disclaimer.
12  *
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.
16  *
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.
20  *
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.
32  */
33
34 /*
35 * MouseEventDemo.java
36 */
37
38 import jalview.util.Platform;
39
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;
46
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
60 /**
61  * Sourced from Oracle and adapted
62  * 
63  * @see https
64  *      ://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html
65  */
66 public class MouseEventDemo extends JPanel implements MouseListener
67 {
68   private class BlankArea extends JLabel
69   {
70     Dimension minSize = new Dimension(200, 100);
71
72     public BlankArea(Color color)
73     {
74       setBackground(color);
75       setOpaque(true);
76       setBorder(BorderFactory.createLineBorder(Color.black));
77     }
78
79     @Override
80     public Dimension getMinimumSize()
81     {
82       return minSize;
83     }
84
85     @Override
86     public Dimension getPreferredSize()
87     {
88       return minSize;
89     }
90   }
91
92   static int counter = 0;
93
94   BlankArea blankArea;
95
96   JTextArea textArea;
97
98   static final String NEWLINE = System.getProperty("line.separator");
99
100   public static void main(String[] args)
101   {
102     // Schedule a job for the event dispatch thread:
103     // creating and showing this application's GUI.
104     javax.swing.SwingUtilities.invokeLater(new Runnable()
105     {
106       @Override
107       public void run()
108       {
109         createAndShowGUI();
110       }
111     });
112   }
113
114   /**
115    * Create the GUI and show it. For thread safety, this method should be
116    * invoked from the event dispatch thread.
117    */
118   private static void createAndShowGUI()
119   {
120     // Create and set up the window.
121     JFrame frame = new JFrame("MouseEventDemo (C to clear)");
122     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
123
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);
128
129     // Display the window.
130     frame.pack();
131     frame.setVisible(true);
132   }
133
134   public MouseEventDemo()
135   {
136     super(new GridLayout(0, 1));
137
138     textArea = new JTextArea();
139     textArea.setEditable(false);
140     JScrollPane scrollPane = new JScrollPane(textArea);
141     scrollPane
142             .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
143     scrollPane.setPreferredSize(new Dimension(400, 75));
144
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);
151     add(splitPane);
152
153     addKeyBinding();
154
155     blankArea.addMouseListener(this);
156     addMouseListener(this);
157     setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
158   }
159
160   private void addKeyBinding()
161   {
162     addKeyBinding(KeyStroke.getKeyStroke('C'));
163     addKeyBinding(KeyStroke.getKeyStroke('c'));
164   }
165
166   /**
167    * @param ks
168    */
169   void addKeyBinding(final KeyStroke ks)
170   {
171     InputMap inputMap = this.getInputMap(JComponent.WHEN_FOCUSED);
172     inputMap.put(ks, ks);
173     this.getActionMap().put(ks, new AbstractAction()
174     {
175       @Override
176       public void actionPerformed(ActionEvent e)
177       {
178         textArea.setText("");
179         log("");
180       }
181     });
182   }
183
184   void logEvent(String eventDescription, MouseEvent e)
185   {
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());
198   }
199
200   /**
201    * @param msg
202    */
203   void log(String msg)
204   {
205     textArea.append(msg + NEWLINE);
206     textArea.setCaretPosition(textArea.getDocument().getLength());
207   }
208
209   @Override
210   public void mousePressed(MouseEvent e)
211   {
212     logEvent("Mouse pressed", e);
213   }
214
215   @Override
216   public void mouseReleased(MouseEvent e)
217   {
218     logEvent("Mouse released", e);
219   }
220
221   @Override
222   public void mouseEntered(MouseEvent e)
223   {
224   }
225
226   @Override
227   public void mouseExited(MouseEvent e)
228   {
229   }
230
231   @Override
232   public void mouseClicked(MouseEvent e)
233   {
234     logEvent("Mouse clicked", e);
235   }
236 }