1. Project Clover database Fri Dec 6 2024 13:47:14 GMT
  2. Package jalview.gui

File MouseEventDemo.java

 

Coverage histogram

../../img/srcFileCovDistChart0.png
60% of files have more coverage

Code metrics

0
52
17
2
259
151
17
0.33
3.06
8.5
1

Classes

Class
Line #
Actions
MouseEventDemo 86 47 14
0.00%
MouseEventDemo.BlankArea 88 5 3
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

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  0 toggle public BlankArea(Color color)
93    {
94  0 setBackground(color);
95  0 setOpaque(true);
96  0 setBorder(BorderFactory.createLineBorder(Color.black));
97    }
98   
 
99  0 toggle @Override
100    public Dimension getMinimumSize()
101    {
102  0 return minSize;
103    }
104   
 
105  0 toggle @Override
106    public Dimension getPreferredSize()
107    {
108  0 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  0 toggle 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  0 javax.swing.SwingUtilities.invokeLater(new Runnable()
128    {
 
129  0 toggle @Override
130    public void run()
131    {
132  0 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  0 toggle private static void createAndShowGUI()
142    {
143    // Create and set up the window.
144  0 JFrame frame = new JFrame("MouseEventDemo (C to clear)");
145  0 frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
146   
147    // Create and set up the content pane.
148  0 JComponent newContentPane = new MouseEventDemo();
149  0 newContentPane.setOpaque(true); // content panes must be opaque
150  0 frame.setContentPane(newContentPane);
151   
152    // Display the window.
153  0 frame.pack();
154  0 frame.setVisible(true);
155    }
156   
 
157  0 toggle public MouseEventDemo()
158    {
159  0 super(new GridLayout(0, 1));
160   
161  0 textArea = new JTextArea();
162  0 textArea.setEditable(false);
163  0 JScrollPane scrollPane = new JScrollPane(textArea);
164  0 scrollPane.setVerticalScrollBarPolicy(
165    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
166  0 scrollPane.setPreferredSize(new Dimension(400, 75));
167   
168  0 blankArea = new BlankArea(Color.YELLOW);
169  0 JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
170    blankArea, scrollPane);
171  0 splitPane.setVisible(true);
172  0 splitPane.setDividerLocation(0.2d);
173  0 splitPane.setResizeWeight(0.5d);
174  0 add(splitPane);
175   
176  0 addKeyBinding();
177   
178  0 blankArea.addMouseListener(this);
179  0 addMouseListener(this);
180  0 setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
181    }
182   
 
183  0 toggle private void addKeyBinding()
184    {
185  0 addKeyBinding(KeyStroke.getKeyStroke('C'));
186  0 addKeyBinding(KeyStroke.getKeyStroke('c'));
187    }
188   
189    /**
190    * @param ks
191    */
 
192  0 toggle void addKeyBinding(final KeyStroke ks)
193    {
194  0 InputMap inputMap = this.getInputMap(JComponent.WHEN_FOCUSED);
195  0 inputMap.put(ks, ks);
196  0 this.getActionMap().put(ks, new AbstractAction()
197    {
 
198  0 toggle @Override
199    public void actionPerformed(ActionEvent e)
200    {
201  0 textArea.setText("");
202  0 log("");
203    }
204    });
205    }
206   
 
207  0 toggle void logEvent(String eventDescription, MouseEvent e)
208    {
209  0 log("------- " + counter++ + ": " + eventDescription);
210  0 log("e.isPopupTrigger: " + e.isPopupTrigger());
211  0 log("SwingUtilities.isRightMouseButton: "
212    + SwingUtilities.isRightMouseButton(e));
213  0 log("SwingUtilities.isLeftMouseButton: "
214    + SwingUtilities.isLeftMouseButton(e));
215  0 log("Platform.isControlDown: " + Platform.isControlDown(e));
216  0 log("e.isControlDown: " + e.isControlDown());
217  0 log("e.isAltDown: " + e.isAltDown());
218  0 log("e.isMetaDown: " + e.isMetaDown());
219  0 log("e.isShiftDown: " + e.isShiftDown());
220  0 log("e.getClickCount: " + e.getClickCount());
221    }
222   
223    /**
224    * @param msg
225    */
 
226  0 toggle void log(String msg)
227    {
228  0 textArea.append(msg + NEWLINE);
229  0 textArea.setCaretPosition(textArea.getDocument().getLength());
230    }
231   
 
232  0 toggle @Override
233    public void mousePressed(MouseEvent e)
234    {
235  0 logEvent("Mouse pressed", e);
236    }
237   
 
238  0 toggle @Override
239    public void mouseReleased(MouseEvent e)
240    {
241  0 logEvent("Mouse released", e);
242    }
243   
 
244  0 toggle @Override
245    public void mouseEntered(MouseEvent e)
246    {
247    }
248   
 
249  0 toggle @Override
250    public void mouseExited(MouseEvent e)
251    {
252    }
253   
 
254  0 toggle @Override
255    public void mouseClicked(MouseEvent e)
256    {
257  0 logEvent("Mouse clicked", e);
258    }
259    }