1
package org.jdesktop.swingx.prompt;
6
import javax.swing.text.JTextComponent;
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;
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
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.
32
* @see JXFormattedTextField
36
* @author Peter Weishapl <petw@gmx.net>
39
public class PromptSupport {
41
* The prompt text property.
43
public static final String PROMPT = "promptText";
46
* The color of the prompt text poroperty.
48
public static final String FOREGROUND = "promptForeground";
51
* The prompt background property.
53
public static final String BACKGROUND = "promptBackground";
56
* The prompt background property.
58
public static final String BACKGROUND_PAINTER = "promptBackgroundPainter";
61
* The focus behavior property.
63
public static final String FOCUS_BEHAVIOR = "focusBehavior";
66
* The font style property, if different from the components font.
68
public static final String FONT_STYLE = "promptFontStyle";
72
* Determines how the {@link JTextComponent} is rendered when focused and no
76
public static enum FocusBehavior {
78
* Keep the prompt text visible.
82
* Highlight the prompt text as it would be selected.
86
* Hide the prompt text.
93
* Convenience method to set the <code>promptText</code> and
94
* <code>promptTextColor</code> on a {@link JTextComponent}.
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)}.
103
* @param promptForeground
104
* @param promptBackground
105
* @param textComponent
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);
112
if (promptForeground != null) {
113
setForeground(promptForeground, textComponent);
115
if (promptBackground != null) {
116
setBackground(promptBackground, textComponent);
121
* Get the {@link FocusBehavior} of <code>textComponent</code>.
123
* @param textComponent
124
* @return the {@link FocusBehavior} or {@link FocusBehavior#HIDE_PROMPT} if
127
public static FocusBehavior getFocusBehavior(JTextComponent textComponent) {
128
FocusBehavior fb = (FocusBehavior) textComponent.getClientProperty(FOCUS_BEHAVIOR);
130
fb = FocusBehavior.HIDE_PROMPT;
136
* Sets the {@link FocusBehavior} on <code>textComponent</code> and
137
* repaints the component to reflect the changes, if it is the focus owner.
139
* @param focusBehavior
140
* @param textComponent
142
public static void setFocusBehavior(FocusBehavior focusBehavior, JTextComponent textComponent) {
143
textComponent.putClientProperty(FOCUS_BEHAVIOR, focusBehavior);
144
if (textComponent.isFocusOwner()) {
145
textComponent.repaint();
150
* Get the prompt text of <code>textComponent</code>.
152
* @param textComponent
153
* @return the prompt text
155
public static String getPrompt(JTextComponent textComponent) {
156
return (String) textComponent.getClientProperty(PROMPT);
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
167
* Calls {@link #install(JTextComponent)} to ensure that the
168
* <code>textComponent</code>s UI is wrapped by the appropriate
169
* {@link PromptTextUI}.
173
* @param textComponent
175
public static void setPrompt(String promptText, JTextComponent textComponent) {
176
TextUIWrapper.getDefaultWrapper().install(textComponent, true);
178
// display prompt as tooltip by default
179
if (textComponent.getToolTipText() == null || textComponent.getToolTipText().equals(getPrompt(textComponent))) {
180
textComponent.setToolTipText(promptText);
183
textComponent.putClientProperty(PROMPT, promptText);
184
textComponent.repaint();
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.
191
* @param textComponent
192
* @return the color of the prompt text or
193
* {@link JTextComponent#getDisabledTextColor()} if none is set
195
public static Color getForeground(JTextComponent textComponent) {
196
if (textComponent.getClientProperty(FOREGROUND) == null) {
197
return textComponent.getDisabledTextColor();
199
return (Color) textComponent.getClientProperty(FOREGROUND);
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.
207
* @param promptTextColor
208
* @param textComponent
210
public static void setForeground(Color promptTextColor, JTextComponent textComponent) {
211
textComponent.putClientProperty(FOREGROUND, promptTextColor);
212
textComponent.repaint();
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.
220
* @param textComponent
221
* @return the the background color of the text component, when no text is
224
public static Color getBackground(JTextComponent textComponent) {
225
if (textComponent.getClientProperty(BACKGROUND) == null) {
226
return textComponent.getBackground();
228
return (Color) textComponent.getClientProperty(BACKGROUND);
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.
238
* Calls {@link #install(JTextComponent)} to ensure that the
239
* <code>textComponent</code>s UI is wrapped by the appropriate
240
* {@link PromptTextUI}.
244
* @param textComponent
246
public static void setBackground(Color background, JTextComponent textComponent) {
247
TextUIWrapper.getDefaultWrapper().install(textComponent, true);
249
textComponent.putClientProperty(BACKGROUND, background);
250
textComponent.repaint();
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.
257
* @param textComponent
258
* @return the background painter of the text component
260
public static Painter getBackgroundPainter(JTextComponent textComponent) {
261
Painter painter = (Painter) textComponent.getClientProperty(BACKGROUND_PAINTER);
263
if (painter == null) {
264
painter = Painters.EMPTY_PAINTER;
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.
277
* Calls {@link #install(JTextComponent)} to ensure that the
278
* <code>textComponent</code>s UI is wrapped by the appropriate
279
* {@link PromptTextUI}.
283
* @param textComponent
285
public static void setBackgroundPainter(Painter background, JTextComponent textComponent) {
286
TextUIWrapper.getDefaultWrapper().install(textComponent, true);
288
textComponent.putClientProperty(BACKGROUND_PAINTER, background);
289
textComponent.repaint();
294
* Set the style of the prompt font, if different from the
295
* <code>textComponent</code>s font.
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.
305
* @param textComponent
307
public static void setFontStyle(Integer fontStyle, JTextComponent textComponent) {
308
textComponent.putClientProperty(FONT_STYLE, fontStyle);
309
textComponent.revalidate();
310
textComponent.repaint();
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
318
* @param textComponent
319
* @return font style of the prompt text
321
public static Integer getFontStyle(JTextComponent textComponent) {
322
return (Integer) textComponent.getClientProperty(FONT_STYLE);