~ubuntu-branches/ubuntu/natty/libswingx-java/natty

« back to all changes in this revision

Viewing changes to src/java/org/jdesktop/swingx/prompt/PromptSupport.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2010-07-26 12:11:27 UTC
  • mfrom: (4.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20100726121127-k0d3b21nhja0dn93
Tags: 1:1.6.1-1
* New upstream release.
* Switch to 3.0 (quilt) format.
* Bump Standards-Version to 3.9.1: no changes needed.
* Drop Depends on JRE: not requested anymore by new Java Policy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.jdesktop.swingx.prompt;
 
2
 
 
3
import java.awt.Color;
 
4
import java.awt.Font;
 
5
 
 
6
import javax.swing.text.JTextComponent;
 
7
 
 
8
import org.jdesktop.swingx.JXFormattedTextField;
 
9
import org.jdesktop.swingx.JXTextArea;
 
10
import org.jdesktop.swingx.JXTextField;
 
11
import org.jdesktop.swingx.painter.Painter;
 
12
import org.jdesktop.swingx.painter.Painters;
 
13
import org.jdesktop.swingx.plaf.PromptTextUI;
 
14
import org.jdesktop.swingx.plaf.TextUIWrapper;
 
15
 
 
16
/**
 
17
 * <p>
 
18
 * Sets prompt text, foreground, background and {@link FocusBehavior} properties
 
19
 * on a JTextComponent by calling
 
20
 * {@link JTextComponent#putClientProperty(Object, Object)}. These properties
 
21
 * are used by {@link PromptTextUI} instances to render the prompt of a text
 
22
 * component.
 
23
 * </p>
 
24
 * 
 
25
 * <p>
 
26
 * This class is used by {@link JXTextField}, {@link JXFormattedTextField} and
 
27
 * {@link JXTextArea} to get and set prompt properties. {@link PromptTextUI}
 
28
 * retrieves these properties using PromptSupport.
 
29
 * </p>
 
30
 * 
 
31
 * @see JXTextField
 
32
 * @see JXFormattedTextField
 
33
 * @see JXTextArea
 
34
 * @see PromptTextUI
 
35
 * 
 
36
 * @author Peter Weishapl <petw@gmx.net>
 
37
 * 
 
38
 */
 
39
public class PromptSupport {
 
40
        /**
 
41
         * The prompt text property.
 
42
         */
 
43
        public static final String PROMPT = "promptText";
 
44
 
 
45
        /**
 
46
         * The color of the prompt text poroperty.
 
47
         */
 
48
        public static final String FOREGROUND = "promptForeground";
 
49
 
 
50
        /**
 
51
         * The prompt background property.
 
52
         */
 
53
        public static final String BACKGROUND = "promptBackground";
 
54
        
 
55
        /**
 
56
         * The prompt background property.
 
57
         */
 
58
        public static final String BACKGROUND_PAINTER = "promptBackgroundPainter";
 
59
 
 
60
        /**
 
61
         * The focus behavior property.
 
62
         */
 
63
        public static final String FOCUS_BEHAVIOR = "focusBehavior";
 
64
 
 
65
        /**
 
66
         * The font style property, if different from the components font.
 
67
         */
 
68
        public static final String FONT_STYLE = "promptFontStyle";
 
69
 
 
70
        /**
 
71
         * <p>
 
72
         * Determines how the {@link JTextComponent} is rendered when focused and no
 
73
         * text is present.
 
74
         * </p>
 
75
         */
 
76
        public static enum FocusBehavior {
 
77
                /**
 
78
                 * Keep the prompt text visible.
 
79
                 */
 
80
                SHOW_PROMPT,
 
81
                /**
 
82
                 * Highlight the prompt text as it would be selected.
 
83
                 */
 
84
                HIGHLIGHT_PROMPT,
 
85
                /**
 
86
                 * Hide the prompt text.
 
87
                 */
 
88
                HIDE_PROMPT
 
89
        };
 
90
 
 
91
        /**
 
92
         * <p>
 
93
         * Convenience method to set the <code>promptText</code> and
 
94
         * <code>promptTextColor</code> on a {@link JTextComponent}.
 
95
         * </p>
 
96
         * <p>
 
97
         * If <code>stayOnUIChange</code> is true, The prompt support will stay
 
98
         * installed, even when the text components UI changes. See
 
99
         * {@link #install(JTextComponent, boolean)}.
 
100
         * </p>
 
101
         * 
 
102
         * @param promptText
 
103
         * @param promptForeground
 
104
         * @param promptBackground
 
105
         * @param textComponent
 
106
         */
 
107
        public static void init(String promptText, Color promptForeground, Color promptBackground,
 
108
                        final JTextComponent textComponent) {
 
109
                if (promptText != null && promptText.length() > 0) {
 
110
                        setPrompt(promptText, textComponent);
 
111
                }
 
112
                if (promptForeground != null) {
 
113
                        setForeground(promptForeground, textComponent);
 
114
                }
 
115
                if (promptBackground != null) {
 
116
                        setBackground(promptBackground, textComponent);
 
117
                }
 
118
        }
 
119
 
 
120
        /**
 
121
         * Get the {@link FocusBehavior} of <code>textComponent</code>.
 
122
         * 
 
123
         * @param textComponent
 
124
         * @return the {@link FocusBehavior} or {@link FocusBehavior#HIDE_PROMPT} if
 
125
         *         none is set
 
126
         */
 
127
        public static FocusBehavior getFocusBehavior(JTextComponent textComponent) {
 
128
                FocusBehavior fb = (FocusBehavior) textComponent.getClientProperty(FOCUS_BEHAVIOR);
 
129
                if (fb == null) {
 
130
                        fb = FocusBehavior.HIDE_PROMPT;
 
131
                }
 
132
                return fb;
 
133
        }
 
134
 
 
135
        /**
 
136
         * Sets the {@link FocusBehavior} on <code>textComponent</code> and
 
137
         * repaints the component to reflect the changes, if it is the focus owner.
 
138
         * 
 
139
         * @param focusBehavior
 
140
         * @param textComponent
 
141
         */
 
142
        public static void setFocusBehavior(FocusBehavior focusBehavior, JTextComponent textComponent) {
 
143
                textComponent.putClientProperty(FOCUS_BEHAVIOR, focusBehavior);
 
144
                if (textComponent.isFocusOwner()) {
 
145
                        textComponent.repaint();
 
146
                }
 
147
        }
 
148
 
 
149
        /**
 
150
         * Get the prompt text of <code>textComponent</code>.
 
151
         * 
 
152
         * @param textComponent
 
153
         * @return the prompt text
 
154
         */
 
155
        public static String getPrompt(JTextComponent textComponent) {
 
156
                return (String) textComponent.getClientProperty(PROMPT);
 
157
        }
 
158
 
 
159
        /**
 
160
         * <p>
 
161
         * Sets the prompt text on <code>textComponent</code>. Also sets the
 
162
         * tooltip text to the prompt text if <code>textComponent</code> has no
 
163
         * tooltip text or the current tooltip text is the same as the current
 
164
         * prompt text.
 
165
         * </p>
 
166
         * <p>
 
167
         * Calls {@link #install(JTextComponent)} to ensure that the
 
168
         * <code>textComponent</code>s UI is wrapped by the appropriate
 
169
         * {@link PromptTextUI}.
 
170
         * </p>
 
171
         * 
 
172
         * @param promptText
 
173
         * @param textComponent
 
174
         */
 
175
        public static void setPrompt(String promptText, JTextComponent textComponent) {
 
176
                TextUIWrapper.getDefaultWrapper().install(textComponent, true);
 
177
 
 
178
                // display prompt as tooltip by default
 
179
                if (textComponent.getToolTipText() == null || textComponent.getToolTipText().equals(getPrompt(textComponent))) {
 
180
                        textComponent.setToolTipText(promptText);
 
181
                }
 
182
 
 
183
                textComponent.putClientProperty(PROMPT, promptText);
 
184
                textComponent.repaint();
 
185
        }
 
186
 
 
187
        /**
 
188
         * Get the foreground color of the prompt text. If no color has been set,
 
189
         * the <code>textComponent</code>s disabled text color will be returned.
 
190
         * 
 
191
         * @param textComponent
 
192
         * @return the color of the prompt text or
 
193
         *         {@link JTextComponent#getDisabledTextColor()} if none is set
 
194
         */
 
195
        public static Color getForeground(JTextComponent textComponent) {
 
196
                if (textComponent.getClientProperty(FOREGROUND) == null) {
 
197
                        return textComponent.getDisabledTextColor();
 
198
                }
 
199
                return (Color) textComponent.getClientProperty(FOREGROUND);
 
200
        }
 
201
 
 
202
        /**
 
203
         * Sets the foreground color of the prompt on <code>textComponent</code>
 
204
         * and repaints the component to reflect the changes. This color will be
 
205
         * used when no text is present.
 
206
         * 
 
207
         * @param promptTextColor
 
208
         * @param textComponent
 
209
         */
 
210
        public static void setForeground(Color promptTextColor, JTextComponent textComponent) {
 
211
                textComponent.putClientProperty(FOREGROUND, promptTextColor);
 
212
                textComponent.repaint();
 
213
        }
 
214
 
 
215
        /**
 
216
         * Get the background color of the <code>textComponent</code>, when no
 
217
         * text is present. If no color has been set, the <code>textComponent</code>s
 
218
         * background color color will be returned.
 
219
         * 
 
220
         * @param textComponent
 
221
         * @return the the background color of the text component, when no text is
 
222
         *         present
 
223
         */
 
224
        public static Color getBackground(JTextComponent textComponent) {
 
225
                if (textComponent.getClientProperty(BACKGROUND) == null) {
 
226
                        return textComponent.getBackground();
 
227
                }
 
228
                return (Color) textComponent.getClientProperty(BACKGROUND);
 
229
        }
 
230
 
 
231
        /**
 
232
         * <p>
 
233
         * Sets the prompts background color on <code>textComponent</code> and
 
234
         * repaints the component to reflect the changes. This background color will
 
235
         * only be used when no text is present.
 
236
         * </p>
 
237
         * <p>
 
238
         * Calls {@link #install(JTextComponent)} to ensure that the
 
239
         * <code>textComponent</code>s UI is wrapped by the appropriate
 
240
         * {@link PromptTextUI}.
 
241
         * </p>
 
242
         * 
 
243
         * @param background
 
244
         * @param textComponent
 
245
         */
 
246
        public static void setBackground(Color background, JTextComponent textComponent) {
 
247
                TextUIWrapper.getDefaultWrapper().install(textComponent, true);
 
248
 
 
249
                textComponent.putClientProperty(BACKGROUND, background);
 
250
                textComponent.repaint();
 
251
        }
 
252
        
 
253
        /**
 
254
         * Get the background painter of the <code>textComponent</code>, when no
 
255
         * text is present. If no painter has been set, then {@code null} will be returned.
 
256
         * 
 
257
         * @param textComponent
 
258
         * @return the background painter of the text component
 
259
         */
 
260
        public static Painter getBackgroundPainter(JTextComponent textComponent) {
 
261
            Painter painter = (Painter) textComponent.getClientProperty(BACKGROUND_PAINTER);
 
262
            
 
263
            if (painter == null) {
 
264
                painter = Painters.EMPTY_PAINTER;
 
265
            }
 
266
            
 
267
            return painter;
 
268
        }
 
269
        
 
270
        /**
 
271
         * <p>
 
272
         * Sets the prompts background painter on <code>textComponent</code> and
 
273
         * repaints the component to reflect the changes. This background painter will
 
274
         * only be used when no text is present.
 
275
         * </p>
 
276
         * <p>
 
277
         * Calls {@link #install(JTextComponent)} to ensure that the
 
278
         * <code>textComponent</code>s UI is wrapped by the appropriate
 
279
         * {@link PromptTextUI}.
 
280
         * </p>
 
281
         * 
 
282
         * @param background
 
283
         * @param textComponent
 
284
         */
 
285
        public static void setBackgroundPainter(Painter background, JTextComponent textComponent) {
 
286
            TextUIWrapper.getDefaultWrapper().install(textComponent, true);
 
287
            
 
288
            textComponent.putClientProperty(BACKGROUND_PAINTER, background);
 
289
            textComponent.repaint();
 
290
        }
 
291
 
 
292
        /**
 
293
         * <p>
 
294
         * Set the style of the prompt font, if different from the
 
295
         * <code>textComponent</code>s font.
 
296
         * </p>
 
297
         * <p>
 
298
         * Allowed values are {@link Font#PLAIN}, {@link Font#ITALIC},
 
299
         * {@link Font#BOLD}, a combination of {@link Font#BOLD} and
 
300
         * {@link Font#ITALIC} or <code>null</code> if the prompt font should be
 
301
         * the same as the <code>textComponent</code>s font.
 
302
         * </p>
 
303
         * 
 
304
         * @param fontStyle
 
305
         * @param textComponent
 
306
         */
 
307
        public static void setFontStyle(Integer fontStyle, JTextComponent textComponent) {
 
308
                textComponent.putClientProperty(FONT_STYLE, fontStyle);
 
309
                textComponent.revalidate();
 
310
                textComponent.repaint();
 
311
        }
 
312
 
 
313
        /**
 
314
         * Returns the font style of the prompt text, or <code>null</code> if the
 
315
         * prompt's font style should not differ from the <code>textComponent</code>s
 
316
         * font.
 
317
         * 
 
318
         * @param textComponent
 
319
         * @return font style of the prompt text
 
320
         */
 
321
        public static Integer getFontStyle(JTextComponent textComponent) {
 
322
                return (Integer) textComponent.getClientProperty(FONT_STYLE);
 
323
        }
 
324
}