Debian Codegraph @

laf-widget/src/main/java/org/pushingpixels/lafwidget/text/SelectOnEscapeWidget.java

raw
1 /*
2 * Copyright (c) 2005-2010 Laf-Widget Kirill Grouchnikov. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * o Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 *
10 * o Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * o Neither the name of Laf-Widget Kirill Grouchnikov nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 package org.pushingpixels.lafwidget.text;
31
32 import java.awt.event.ActionEvent;
33 import java.awt.event.KeyEvent;
34 import java.beans.PropertyChangeEvent;
35 import java.beans.PropertyChangeListener;
36
37 import javax.swing.*;
38 import javax.swing.text.JTextComponent;
39
40 import org.pushingpixels.lafwidget.*;
41
42 /**
43 * Adds "select / deselect on Escape key press" behaviour on text components.
44 *
45 * @author Kirill Grouchnikov
46 * @since 2.1
47 */
48 public class SelectOnEscapeWidget extends LafWidgetAdapter<JTextComponent> {
49 protected PropertyChangeListener propertyChangeListener;
50
51 /*
52 * (non-Javadoc)
53 *
54 * @see org.pushingpixels.lafwidget.LafWidget#requiresCustomLafSupport()
55 */
56 @Override
57 public boolean requiresCustomLafSupport() {
58 return false;
59 }
60
61 private void installTracking() {
62 InputMap currMap = SwingUtilities.getUIInputMap(this.jcomp,
63 JComponent.WHEN_FOCUSED);
64
65 InputMap newMap = new InputMap();
66 if (currMap != null) {
67 KeyStroke[] kss = currMap.allKeys();
68 for (int i = 0; i < kss.length; i++) {
69 KeyStroke stroke = kss[i];
70 Object val = currMap.get(stroke);
71 newMap.put(stroke, val);
72 }
73 }
74
75 newMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
76 "flipTextSelection");
77
78 this.jcomp.getActionMap().put("flipTextSelection",
79 new AbstractAction() {
80 @Override
81 public void actionPerformed(ActionEvent e) {
82 SwingUtilities.invokeLater(new Runnable() {
83 @Override
84 public void run() {
85 int selectionLength = jcomp.getSelectionEnd()
86 - jcomp.getSelectionStart();
87 if (selectionLength == 0) {
88 jcomp.selectAll();
89 } else {
90 int lastPos = jcomp.getSelectionEnd();
91 jcomp.setSelectionStart(0);
92 jcomp.setSelectionEnd(0);
93 jcomp.setCaretPosition(lastPos);
94 }
95 }
96 });
97 }
98 });
99
100 SwingUtilities.replaceUIInputMap(this.jcomp, JComponent.WHEN_FOCUSED,
101 newMap);
102 }
103
104 private void uninstallTracking() {
105 InputMap currMap = SwingUtilities.getUIInputMap(this.jcomp,
106 JComponent.WHEN_FOCUSED);
107 if (currMap != null) {
108 InputMap newMap = new InputMap();
109 KeyStroke[] kss = currMap.allKeys();
110 for (int i = 0; i < kss.length; i++) {
111 KeyStroke stroke = kss[i];
112 Object val = currMap.get(stroke);
113 if (stroke
114 .equals(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0))
115 && "flipTextSelection".equals(val)) {
116 continue;
117 }
118 newMap.put(stroke, val);
119 }
120 SwingUtilities.replaceUIInputMap(this.jcomp,
121 JComponent.WHEN_FOCUSED, newMap);
122 }
123 this.jcomp.getActionMap().remove("flipTextSelection");
124 }
125
126 /*
127 * (non-Javadoc)
128 *
129 * @see org.pushingpixels.lafwidget.LafWidgetAdapter#installListeners()
130 */
131 @Override
132 public void installListeners() {
133 this.propertyChangeListener = new PropertyChangeListener() {
134 @Override
135 public void propertyChange(PropertyChangeEvent evt) {
136 if (LafWidget.TEXT_FLIP_SELECT_ON_ESCAPE.equals(evt
137 .getPropertyName())) {
138 boolean hasTextFlipSelection = LafWidgetUtilities
139 .hasTextFlipSelectOnEscapeProperty(jcomp);
140 if (hasTextFlipSelection) {
141 // change the input map
142 installTracking();
143 } else {
144 // remove the input map
145 uninstallTracking();
146 }
147 }
148 }
149 };
150 this.jcomp.addPropertyChangeListener(this.propertyChangeListener);
151 }
152
153 /*
154 * (non-Javadoc)
155 *
156 * @see org.pushingpixels.lafwidget.LafWidgetAdapter#uninstallListeners()
157 */
158 @Override
159 public void uninstallListeners() {
160 this.jcomp.removePropertyChangeListener(this.propertyChangeListener);
161 this.propertyChangeListener = null;
162 }
163 }