JAL-2077 less drastic exit on close
[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 import javax.swing.UIManager;
60 import javax.swing.UnsupportedLookAndFeelException;
61
62 /**
63  * Sourced from Oracle and adapted
64  * 
65  * @see https
66  *      ://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html
67  */
68 public class MouseEventDemo extends JPanel implements MouseListener
69 {
70   private class BlankArea extends JLabel
71   {
72     Dimension minSize = new Dimension(200, 100);
73
74     public BlankArea(Color color)
75     {
76       setBackground(color);
77       setOpaque(true);
78       setBorder(BorderFactory.createLineBorder(Color.black));
79     }
80
81     @Override
82     public Dimension getMinimumSize()
83     {
84       return minSize;
85     }
86
87     @Override
88     public Dimension getPreferredSize()
89     {
90       return minSize;
91     }
92   }
93
94   static int counter = 0;
95
96   BlankArea blankArea;
97
98   JTextArea textArea;
99
100   static final String NEWLINE = System.getProperty("line.separator");
101
102   public static void main(String[] args)
103   {
104     /* Use an appropriate Look and Feel */
105     try
106     {
107       // UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
108       // UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
109       UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
110     } catch (UnsupportedLookAndFeelException ex)
111     {
112       ex.printStackTrace();
113     } catch (IllegalAccessException ex)
114     {
115       ex.printStackTrace();
116     } catch (InstantiationException ex)
117     {
118       ex.printStackTrace();
119     } catch (ClassNotFoundException ex)
120     {
121       ex.printStackTrace();
122     }
123     /* Turn off metal's use of bold fonts */
124     UIManager.put("swing.boldMetal", Boolean.FALSE);
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
165             .setVerticalScrollBarPolicy(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     // textArea.append(eventDescription + " detected on "
210     // + e.getComponent().getClass().getName() + "." + NEWLINE);
211     log("------- " + counter++ + ": " + eventDescription);
212     log("e.isPopupTrigger: " + e.isPopupTrigger());
213     log("SwingUtilities.isRightMouseButton: "
214             + SwingUtilities.isRightMouseButton(e));
215     log("SwingUtilities.isLeftMouseButton: "
216             + SwingUtilities.isLeftMouseButton(e));
217     log("Platform.isControlDown: " + Platform.isControlDown(e));
218     log("e.isControlDown: " + e.isControlDown());
219     log("e.isAltDown: " + e.isAltDown());
220     log("e.isMetaDown: " + e.isMetaDown());
221     log("e.isShiftDown: " + e.isShiftDown());
222     log("e.getClickCount: " + e.getClickCount());
223   }
224
225   /**
226    * @param msg
227    */
228   void log(String msg)
229   {
230     textArea.append(msg + NEWLINE);
231     textArea.setCaretPosition(textArea.getDocument().getLength());
232   }
233
234   @Override
235   public void mousePressed(MouseEvent e)
236   {
237     logEvent("Mouse pressed", e);
238   }
239
240   @Override
241   public void mouseReleased(MouseEvent e)
242   {
243     logEvent("Mouse released", e);
244   }
245
246   @Override
247   public void mouseEntered(MouseEvent e)
248   {
249     // eventOutput("Mouse entered", e);
250   }
251
252   @Override
253   public void mouseExited(MouseEvent e)
254   {
255     // eventOutput("Mouse exited", e);
256   }
257
258   @Override
259   public void mouseClicked(MouseEvent e)
260   {
261     logEvent("Mouse clicked", e);
262   }
263 }