Subversion Repository Public Repository

Pharmacy_09_03_18

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package com.bestray.healthcarecommonutil.util;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.awt.*;

// TODO: Auto-generated Javadoc
/**
 *  The TextPrompt class will display a prompt over top of a text component when
 *  the Document of the text field is empty. The Show property is used to
 *  determine the visibility of the prompt.
 *
 *  The Font and foreground Color of the prompt will default to those properties
 *  of the parent text component. You are free to change the properties after
 *  class construction.
 */
public class TextPrompt extends JLabel
	implements FocusListener, DocumentListener
{
	
	/**
	 * The Enum Show.
	 */
	public enum Show
	{
		
		/** The always. */
		ALWAYS,
		
		/** The focus gained. */
		FOCUS_GAINED,
		
		/** The focus lost. */
		FOCUS_LOST;
	}

	/** The component. */
	private JTextComponent component;
	
	/** The document. */
	private Document document;

	/** The show. */
	private Show show;
	
	/** The show prompt once. */
	private boolean showPromptOnce;
	
	/** The focus lost. */
	private int focusLost;

	/**
	 * Instantiates a new text prompt.
	 *
	 * @param text the text
	 * @param component the component
	 */
	public TextPrompt(String text, JTextComponent component)
	{
		this(text, component, Show.ALWAYS);
	}

	/**
	 * Instantiates a new text prompt.
	 *
	 * @param text the text
	 * @param component the component
	 * @param show the show
	 */
	public TextPrompt(String text, JTextComponent component, Show show)
	{
		this.component = component;
		setShow( show );
		document = component.getDocument();

		setText( text );
		setFont( component.getFont() );
		setForeground( component.getForeground() );
		setBorder( new EmptyBorder(component.getInsets()) );
		setHorizontalAlignment(JLabel.LEADING);

		component.addFocusListener( this );
		document.addDocumentListener( this );

		component.setLayout( new BorderLayout() );
		component.add( this );
		checkForPrompt();
	}

	/**
	 *  Convenience method to change the alpha value of the current foreground
	 *  Color to the specifice value.
	 *
	 *  @param alpha value in the range of 0 - 1.0.
	 */
	public void changeAlpha(float alpha)
	{
		changeAlpha( (int)(alpha * 255) );
	}

	/**
	 *  Convenience method to change the alpha value of the current foreground
	 *  Color to the specifice value.
	 *
	 *  @param alpha value in the range of 0 - 255.
	 */
	public void changeAlpha(int alpha)
	{
		alpha = alpha > 255 ? 255 : alpha < 0 ? 0 : alpha;

		Color foreground = getForeground();
		int red = foreground.getRed();
		int green = foreground.getGreen();
		int blue = foreground.getBlue();

		Color withAlpha = new Color(red, green, blue, alpha);
		super.setForeground( withAlpha );
	}

	/**
	 *  Convenience method to change the style of the current Font. The style
	 *  values are found in the Font class. Common values might be:
	 *  Font.BOLD, Font.ITALIC and Font.BOLD + Font.ITALIC.
	 *
	 *  @param style value representing the the new style of the Font.
	 */
	public void changeStyle(int style)
	{
		setFont( getFont().deriveFont( style ) );
	}

	/**
	 * Get the Show property.
	 *
	 * @return the Show property.
	 */
	public Show getShow()
	{
		return show;
	}

	/**
	 *  Set the prompt Show property to control when the promt is shown.
	 *  Valid values are:
	 *
	 *  Show.AWLAYS (default) - always show the prompt
	 *  Show.Focus_GAINED - show the prompt when the component gains focus
	 *      (and hide the prompt when focus is lost)
	 *  Show.Focus_LOST - show the prompt when the component loses focus
	 *      (and hide the prompt when focus is gained)
	 *
	 *  @param show a valid Show enum
	 */
	public void setShow(Show show)
	{
		this.show = show;
	}

	/**
	 * Get the showPromptOnce property.
	 *
	 * @return the showPromptOnce property.
	 */
	public boolean getShowPromptOnce()
	{
		return showPromptOnce;
	}

	/**
	 *  Show the prompt once. Once the component has gained/lost focus
	 *  once, the prompt will not be shown again.
	 *
	 *  @param showPromptOnce  when true the prompt will only be shown once,
	 *                         otherwise it will be shown repeatedly.
	 */
	public void setShowPromptOnce(boolean showPromptOnce)
	{
		this.showPromptOnce = showPromptOnce;
	}

	/**
	 *	Check whether the prompt should be visible or not. The visibility
	 *  will change on updates to the Document and on focus changes.
	 */
	private void checkForPrompt()
	{
		//  Text has been entered, remove the prompt

		if (document.getLength() > 0)
		{
			setVisible( false );
			return;
		}

		//  Prompt has already been shown once, remove it

		if (showPromptOnce && focusLost > 0)
		{
			setVisible(false);
			return;
		}

		//  Check the Show property and component focus to determine if the
		//  prompt should be displayed.

        if (component.hasFocus())
        {
        	if (show == Show.ALWAYS
        	||  show ==	Show.FOCUS_GAINED)
        		setVisible( true );
        	else
        		setVisible( false );
        }
        else
        {
        	if (show == Show.ALWAYS
        	||  show ==	Show.FOCUS_LOST)
        		setVisible( true );
        	else
        		setVisible( false );
        }
	}

//  Implement FocusListener

	/* (non-Javadoc)
 * @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent)
 */
public void focusGained(FocusEvent e)
	{
		checkForPrompt();
	}

	/* (non-Javadoc)
	 * @see java.awt.event.FocusListener#focusLost(java.awt.event.FocusEvent)
	 */
	public void focusLost(FocusEvent e)
	{
		focusLost++;
		checkForPrompt();
	}

//  Implement DocumentListener

	/* (non-Javadoc)
 * @see javax.swing.event.DocumentListener#insertUpdate(javax.swing.event.DocumentEvent)
 */
public void insertUpdate(DocumentEvent e)
	{
		checkForPrompt();
	}

	/* (non-Javadoc)
	 * @see javax.swing.event.DocumentListener#removeUpdate(javax.swing.event.DocumentEvent)
	 */
	public void removeUpdate(DocumentEvent e)
	{
		checkForPrompt();
	}

	/* (non-Javadoc)
	 * @see javax.swing.event.DocumentListener#changedUpdate(javax.swing.event.DocumentEvent)
	 */
	public void changedUpdate(DocumentEvent e) {}
}

Commits for Pharmacy_09_03_18/Dr Gyana ProjectSpace/DrGyanaCommonUtil/src/main/java/com/bestray/healthcarecommonutil/util/TextPrompt.java

Diff revisions: vs.
Revision Author Commited Message
1 girijabapi picture girijabapi Fri 27 Jul, 2018 07:30:57 +0000