Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DoubleClickForwarder |
|
| 1.25;1.25 |
1 | /* | |
2 | * Copyright (c) 2009, Swiss Federal Department of Defence Civil Protection and Sport | |
3 | * (http://www.vbs.admin.ch) | |
4 | * Copyright (c) 2009, Ergon Informatik AG (http://www.ergon.ch) | |
5 | * All rights reserved. | |
6 | * | |
7 | * Licensed under the Open Permis License which accompanies this distribution, | |
8 | * and is available at http://www.openpermis.org/BSDlicenceKent.txt | |
9 | */ | |
10 | package org.openpermis.editor.policy.gui; | |
11 | ||
12 | import java.awt.event.ActionEvent; | |
13 | import java.awt.event.MouseAdapter; | |
14 | import java.awt.event.MouseEvent; | |
15 | ||
16 | import javax.swing.Action; | |
17 | import javax.swing.JComponent; | |
18 | ||
19 | ||
20 | /** | |
21 | * Mouse listener that triggers an action on a double click. | |
22 | * @since 0.1.0 | |
23 | */ | |
24 | public class DoubleClickForwarder | |
25 | extends MouseAdapter | |
26 | { | |
27 | ||
28 | //---- Static | |
29 | ||
30 | /** | |
31 | * Default command used to trigger the action. | |
32 | * @since 0.1.0 | |
33 | */ | |
34 | public static final String COMMAND = "doubleclick"; | |
35 | ||
36 | /** | |
37 | * Convenience method to add a double click action to a component. | |
38 | * @param component the component to register the action at. | |
39 | * @param action the action to register. | |
40 | * @return the action created. | |
41 | * @since 0.1.0 | |
42 | */ | |
43 | public static DoubleClickForwarder register (JComponent component, Action action) { | |
44 | 0 | final DoubleClickForwarder dca = new DoubleClickForwarder(action); |
45 | 0 | component.addMouseListener(dca); |
46 | 0 | return dca; |
47 | } | |
48 | ||
49 | //---- State | |
50 | ||
51 | /** | |
52 | * The action to trigger. | |
53 | * @since 0.1.0 | |
54 | */ | |
55 | private Action action; | |
56 | ||
57 | /** | |
58 | * The command to pass. | |
59 | * @since 0.1.0 | |
60 | */ | |
61 | private final String command; | |
62 | ||
63 | //---- Constructors | |
64 | ||
65 | /** | |
66 | * Creates a new double click listener. | |
67 | * @note Uses {@link #COMMAND} as command. | |
68 | * @param action the action to execute. | |
69 | * @since 0.1.0 | |
70 | */ | |
71 | public DoubleClickForwarder (Action action) { | |
72 | 0 | this(action, COMMAND); |
73 | 0 | } |
74 | ||
75 | /** | |
76 | * Creates a new double click listener. | |
77 | * @param action the action to execute. | |
78 | * @param command the command to issue. | |
79 | * @since 0.1.0 | |
80 | */ | |
81 | 0 | public DoubleClickForwarder (Action action, String command) { |
82 | 0 | this.action = action; |
83 | 0 | this.command = command; |
84 | 0 | } |
85 | ||
86 | //---- MouseAdapter | |
87 | ||
88 | /** | |
89 | * @since 0.1.0 | |
90 | */ | |
91 | @Override | |
92 | public void mouseClicked (MouseEvent e) { | |
93 | 0 | if (e.getClickCount() == 2) { |
94 | 0 | this.action.actionPerformed( |
95 | new ActionEvent( | |
96 | e.getSource(), | |
97 | ActionEvent.ACTION_PERFORMED, | |
98 | this.command | |
99 | ) | |
100 | ); | |
101 | } | |
102 | 0 | } |
103 | ||
104 | } |