Skip to content

Commit dacf0ec

Browse files
hansonrhansonr
authored andcommitted
working on InputMap
1 parent 50f7676 commit dacf0ec

File tree

1 file changed

+346
-0
lines changed

1 file changed

+346
-0
lines changed
Lines changed: 346 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,346 @@
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 test.components;
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 java.awt.Color;
59+
import java.awt.Dimension;
60+
import java.awt.Graphics;
61+
import java.awt.GridLayout;
62+
import java.awt.event.ActionEvent;
63+
import java.awt.event.ActionListener;
64+
import java.awt.event.FocusEvent;
65+
import java.awt.event.FocusListener;
66+
import java.awt.event.KeyAdapter;
67+
import java.awt.event.KeyEvent;
68+
import java.awt.event.KeyListener;
69+
import java.awt.event.MouseEvent;
70+
import java.awt.event.MouseListener;
71+
72+
import javax.swing.AbstractAction;
73+
import javax.swing.BorderFactory;
74+
import javax.swing.FocusManager;
75+
import javax.swing.InputMap;
76+
import javax.swing.JButton;
77+
import javax.swing.JComponent;
78+
import javax.swing.JFrame;
79+
import javax.swing.JLabel;
80+
import javax.swing.JPanel;
81+
import javax.swing.JScrollPane;
82+
import javax.swing.JSplitPane;
83+
import javax.swing.JTextArea;
84+
import javax.swing.KeyStroke;
85+
import javax.swing.SwingUtilities;
86+
import javax.swing.UIManager;
87+
import javax.swing.border.BevelBorder;
88+
import javax.swing.border.Border;
89+
import javax.swing.border.EtchedBorder;
90+
91+
/**
92+
* Sourced from Oracle and adapted
93+
*
94+
* @see https
95+
* ://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html
96+
*/
97+
public class MouseEventDemo extends JPanel implements MouseListener {
98+
private class BlankArea extends JLabel {
99+
Dimension minSize = new Dimension(200, 100);
100+
101+
public BlankArea(Color color) {
102+
setBackground(color);
103+
setOpaque(true);
104+
setBorder(BorderFactory.createLineBorder(Color.black));
105+
}
106+
107+
@Override
108+
public Dimension getMinimumSize() {
109+
return minSize;
110+
}
111+
112+
@Override
113+
public Dimension getPreferredSize() {
114+
return minSize;
115+
}
116+
}
117+
118+
static int counter = 0;
119+
120+
BlankArea blankArea;
121+
122+
JTextArea textArea;
123+
124+
static final String NEWLINE = System.getProperty("line.separator");
125+
126+
/**
127+
*
128+
* @param args
129+
*/
130+
public static void main(String[] args) {
131+
// Schedule a job for the event dispatch thread:
132+
// creating and showing this application's GUI.
133+
javax.swing.SwingUtilities.invokeLater(new Runnable() {
134+
@Override
135+
public void run() {
136+
createAndShowGUI();
137+
}
138+
});
139+
}
140+
141+
/**
142+
* Create the GUI and show it. For thread safety, this method should be invoked
143+
* from the event dispatch thread.
144+
*/
145+
private static void createAndShowGUI() {
146+
// Create and set up the window.
147+
JFrame frame = new JFrame("MouseEventDemo (C to clear)");
148+
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
149+
150+
// Create and set up the content pane.
151+
JComponent newContentPane = new MouseEventDemo();
152+
newContentPane.setOpaque(true); // content panes must be opaque
153+
frame.setContentPane(newContentPane);
154+
155+
frame.addFocusListener(new FocusListener() {
156+
157+
@Override
158+
public void focusGained(FocusEvent e) {
159+
System.out.println("MED.frame focus gained from " + e.getOppositeComponent());
160+
}
161+
162+
@Override
163+
public void focusLost(FocusEvent e) {
164+
System.out.println("MED.frame focus lost to " + e.getOppositeComponent());
165+
}
166+
167+
});
168+
169+
frame.getRootPane(). addFocusListener(new FocusListener() {
170+
171+
@Override
172+
public void focusGained(FocusEvent e) {
173+
System.out.println("MEDroot focus gained from " + e.getOppositeComponent());
174+
}
175+
176+
@Override
177+
public void focusLost(FocusEvent e) {
178+
System.out.println("MEDroot focus lost to " + e.getOppositeComponent());
179+
}
180+
181+
});
182+
183+
184+
185+
// Display the window.
186+
frame.pack();
187+
frame.setVisible(true);
188+
}
189+
190+
public MouseEventDemo() {
191+
super(new GridLayout(0, 1));
192+
193+
textArea = new JTextArea();
194+
textArea.setEditable(false);
195+
JScrollPane scrollPane = new JScrollPane(textArea);
196+
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
197+
scrollPane.setPreferredSize(new Dimension(400, 75));
198+
199+
200+
blankArea = new BlankArea(Color.YELLOW);
201+
JPanel panel = new JPanel() {
202+
protected void paintBorder(Graphics g) {
203+
System.out.println("MED JPanel painting border " + getBorder());
204+
super.paintBorder(g);
205+
}
206+
public void setBorder(Border b) {
207+
super.setBorder(b);
208+
}
209+
};
210+
211+
System.out.println(UIManager.getBorder("Panel.border") + " " + panel.getBorder());
212+
// panel.setBorder(new BevelBorder(1));
213+
panel.add(blankArea);
214+
JButton btn = new JButton("clear");
215+
btn.setMnemonic('l');
216+
btn.addActionListener(new ActionListener() {
217+
218+
@Override
219+
public void actionPerformed(ActionEvent e) {
220+
textArea.setText("");
221+
log("");
222+
}
223+
224+
});
225+
226+
panel.add(btn);
227+
// JPanel scrollPane = new JPanel() {
228+
// protected void paintBorder(Graphics g) {
229+
// System.out.println("MED.scrollPanePanel painting border " + getBorder());
230+
// super.paintBorder(g);
231+
// }
232+
// };
233+
// scrollPane.add(j);
234+
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, panel, scrollPane);
235+
splitPane.setVisible(true);
236+
splitPane.setDividerLocation(0.2d);
237+
splitPane.setResizeWeight(0.5d);
238+
add(splitPane);
239+
240+
addKeyBinding();
241+
242+
addKeyListener(new KeyListener() {
243+
244+
@Override
245+
public void keyTyped(KeyEvent e) {
246+
System.out.println(e);
247+
System.out.println(FocusManager.getCurrentManager().getFocusOwner().getClass().getName());
248+
}
249+
250+
@Override
251+
public void keyPressed(KeyEvent e) {
252+
}
253+
254+
@Override
255+
public void keyReleased(KeyEvent e) {
256+
// TODO Auto-generated method stub
257+
258+
}
259+
260+
});
261+
blankArea.addMouseListener(this);
262+
addMouseListener(this);
263+
setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
264+
}
265+
266+
private void addKeyBinding() {
267+
addKeyBinding(KeyStroke.getKeyStroke('C'));
268+
addKeyBinding(KeyStroke.getKeyStroke('c'));
269+
}
270+
271+
/**
272+
* @param ks
273+
*/
274+
void addKeyBinding(final KeyStroke ks) {
275+
276+
addFocusListener(new FocusListener() {
277+
278+
@Override
279+
public void focusGained(FocusEvent e) {
280+
System.out.println("MED focus gained from " + e.getOppositeComponent());
281+
}
282+
283+
@Override
284+
public void focusLost(FocusEvent e) {
285+
System.out.println("MED focus lost to " + e.getOppositeComponent());
286+
}
287+
288+
});
289+
290+
InputMap inputMap = this.getInputMap(JComponent.WHEN_FOCUSED);
291+
inputMap.put(ks, ks);
292+
this.getActionMap().put(ks, new AbstractAction() {
293+
@Override
294+
public void actionPerformed(ActionEvent e) {
295+
textArea.setText("");
296+
log("");
297+
}
298+
});
299+
}
300+
301+
void logEvent(String eventDescription, MouseEvent e) {
302+
log("------- " + counter++ + ": " + eventDescription);
303+
log("e.isPopupTrigger: " + e.isPopupTrigger());
304+
log("SwingUtilities.isRightMouseButton: " + SwingUtilities.isRightMouseButton(e));
305+
log("SwingUtilities.isLeftMouseButton: " + SwingUtilities.isLeftMouseButton(e));
306+
log("e.isControlDown: " + e.isControlDown());
307+
log("e.isAltDown: " + e.isAltDown());
308+
log("e.isMetaDown: " + e.isMetaDown());
309+
log("e.isShiftDown: " + e.isShiftDown());
310+
log("e.getClickCount: " + e.getClickCount());
311+
}
312+
313+
/**
314+
* @param msg
315+
*/
316+
void log(String msg) {
317+
textArea.append(msg + NEWLINE);
318+
textArea.setCaretPosition(textArea.getDocument().getLength());
319+
}
320+
321+
@Override
322+
public void mousePressed(MouseEvent e) {
323+
logEvent("Mouse pressed", e);
324+
e.consume();
325+
}
326+
327+
@Override
328+
public void mouseReleased(MouseEvent e) {
329+
logEvent("Mouse released", e);
330+
e.consume();
331+
}
332+
333+
@Override
334+
public void mouseEntered(MouseEvent e) {
335+
}
336+
337+
@Override
338+
public void mouseExited(MouseEvent e) {
339+
}
340+
341+
@Override
342+
public void mouseClicked(MouseEvent e) {
343+
logEvent("Mouse clicked", e);
344+
e.consume();
345+
}
346+
}

0 commit comments

Comments
 (0)