Debian Codegraph @

laf-widget/src/main/java/org/pushingpixels/lafwidget/text/LockBorderWidget.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.beans.PropertyChangeEvent;
33 import java.beans.PropertyChangeListener;
34
35 import javax.swing.SwingUtilities;
36 import javax.swing.border.Border;
37
38 import org.pushingpixels.lafwidget.*;
39
40 /**
41 * Adds visual indication on non-editable text components.
42 *
43 * @author Kirill Grouchnikov
44 */
45 public class LockBorderWidget extends LafWidgetAdapter {
46 /**
47 * Listens on all properties to decide whether a lock border should be shown
48 * / hidden.
49 */
50 protected PropertyChangeListener propertyChangeListener;
51
52 /**
53 * <code>true</code> if this widget is uninstalling. Fix for defect 7.
54 */
55 protected boolean isUninstalling = false;
56
57 /**
58 * Name for client property that stores the original border.
59 */
60 public static String ORIGINAL_BORDER = "lafwidget.internal.originalBorder";
61
62 /*
63 * (non-Javadoc)
64 *
65 * @see org.pushingpixels.lafwidget.LafWidgetAdapter#installListeners()
66 */
67 @Override
68 public void installListeners() {
69 this.propertyChangeListener = new PropertyChangeListener() {
70 @Override
71 public void propertyChange(PropertyChangeEvent evt) {
72 // fix for defect 5 - infinite event chain.
73 if ("border".equals(evt.getPropertyName()))
74 return;
75 if (LockBorderWidget.ORIGINAL_BORDER.equals(evt
76 .getPropertyName()))
77 return;
78 SwingUtilities.invokeLater(new Runnable() {
79 @Override
80 public void run() {
81 // fix for defect 7 - not removing lock border
82 // on LAF switch
83 if (isUninstalling)
84 return;
85 LafWidgetSupport lafSupport = LafWidgetRepository
86 .getRepository().getLafSupport();
87 boolean hasLockIcon = lafSupport.hasLockIcon(jcomp);
88 if (hasLockIcon) {
89 installLockBorder();
90 } else {
91 restoreOriginalBorder();
92 }
93 }
94
95 });
96 }
97 };
98 this.jcomp.addPropertyChangeListener(this.propertyChangeListener);
99 }
100
101 /*
102 * (non-Javadoc)
103 *
104 * @see org.pushingpixels.lafwidget.LafWidgetAdapter#uninstallListeners()
105 */
106 @Override
107 public void uninstallListeners() {
108 this.jcomp.removePropertyChangeListener(this.propertyChangeListener);
109 this.propertyChangeListener = null;
110 }
111
112 /*
113 * (non-Javadoc)
114 *
115 * @see org.pushingpixels.lafwidget.LafWidgetAdapter#uninstallUI()
116 */
117 @Override
118 public void uninstallUI() {
119 // fix for issue 7 - restoring original border on LAF switch.
120 this.isUninstalling = true;
121 Border original = (Border) this.jcomp
122 .getClientProperty(LockBorderWidget.ORIGINAL_BORDER);
123 if (original != null) {
124 this.jcomp.setBorder(original);
125 }
126 }
127
128 /*
129 * (non-Javadoc)
130 *
131 * @see org.pushingpixels.lafwidget.LafWidgetAdapter#installDefaults()
132 */
133 @Override
134 public void installDefaults() {
135 super.installDefaults();
136 LafWidgetSupport lafSupport = LafWidgetRepository.getRepository()
137 .getLafSupport();
138 boolean hasLockIcon = lafSupport.hasLockIcon(this.jcomp);
139 if (hasLockIcon) {
140 Border currBorder = this.jcomp.getBorder();
141 this.jcomp.putClientProperty(LockBorderWidget.ORIGINAL_BORDER,
142 currBorder);
143 this.jcomp.setBorder(new LockBorder(currBorder));
144 }
145 }
146
147 /*
148 * (non-Javadoc)
149 *
150 * @see org.pushingpixels.lafwidget.LafWidgetAdapter#uninstallDefaults()
151 */
152 @Override
153 public void uninstallDefaults() {
154 // fix for issue 7 - restoring original border on LAF switch.
155 this.isUninstalling = true;
156 this.jcomp.putClientProperty(LockBorderWidget.ORIGINAL_BORDER, null);
157 }
158
159 /*
160 * (non-Javadoc)
161 *
162 * @see org.pushingpixels.lafwidget.LafWidget#requiresCustomLafSupport()
163 */
164 @Override
165 public boolean requiresCustomLafSupport() {
166 return false;
167 }
168
169 /**
170 * Installs the lock border on the associated component.
171 */
172 private void installLockBorder() {
173 if (jcomp.getClientProperty(LockBorderWidget.ORIGINAL_BORDER) instanceof Border) {
174 // already installed
175 return;
176 }
177 // need to install
178 Border currBorder = jcomp.getBorder();
179 if (currBorder != null) {
180 jcomp.putClientProperty(LockBorderWidget.ORIGINAL_BORDER,
181 currBorder);
182 jcomp.setBorder(new LockBorder(currBorder));
183 }
184 }
185
186 /**
187 * Restores the original border on the associated component.
188 */
189 private void restoreOriginalBorder() {
190 if (jcomp.getClientProperty(LockBorderWidget.ORIGINAL_BORDER) instanceof Border) {
191 // revert to original
192 Border originalBorder = (Border) jcomp
193 .getClientProperty(LockBorderWidget.ORIGINAL_BORDER);
194 jcomp.setBorder(originalBorder);
195 jcomp.putClientProperty(LockBorderWidget.ORIGINAL_BORDER, null);
196 } else {
197 // already uninstalled
198 }
199 }
200 }