comparison dwtx/cdt/managedbuilder/ui/properties/PasswordFieldEditor.d @ 110:a26bb7394581

Add two field editor from the CDT project, thanks yidabu for the port.
author Frank Benoit <benoit@tionex.de>
date Thu, 07 Aug 2008 17:37:04 +0200
parents
children
comparison
equal deleted inserted replaced
106:8ab6fb387666 110:a26bb7394581
1 /**********************************************************************
2 * Copyright (c) 2004 BitMethods Inc and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v0.5
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v05.html
7 *
8 * Contributors:
9 * BitMethods Inc - Initial API and implementation
10 * Ported to the D programming language
11 * yidabu ( D Programming Language China : http://www.d-programming-language-china.org/ )
12 ***********************************************************************/
13 module dwtx.cdt.managedbuilder.ui.properties.PasswordFieldEditor;
14
15 /// https://bugs.eclipse.org/bugs/attachment.cgi?id=33204
16 /// https://bugs.eclipse.org/bugs/show_bug.cgi?id=23495
17
18 import dwtx.jface.util.Assert;
19 import dwt.DWT;
20 import dwt.events.DisposeEvent;
21 import dwt.events.DisposeListener;
22 import dwt.events.FocusAdapter;
23 import dwt.events.FocusEvent;
24 import dwt.events.KeyAdapter;
25 import dwt.events.KeyEvent;
26 import dwt.widgets.Composite;
27 import dwt.widgets.Text;
28
29 import dwt.dwthelper.utils;
30 import dwtx.jface.preference.StringFieldEditor;
31
32 /**
33 * A field editor for a password type preference. This is essentially
34 * a StringFieldEditor, but will replace each character in the input
35 * box with an '*'.
36 * <p>
37 * This class may be used as is, or subclassed as required.
38 * </p>
39 */
40 public class PasswordFieldEditor : StringFieldEditor {
41
42 /**
43 * The validation strategy;
44 * <code>VALIDATE_ON_KEY_STROKE</code> by default.
45 */
46 private int validateStrategy;
47
48 /**
49 * Text limit of text field in characters; initially unlimited.
50 */
51 private int textLimit;
52
53 /**
54 * The text field, or <code>null</code> if none.
55 */
56 Text textField;
57
58 /**
59 * Creates a new password field editor
60 */
61 protected this() {
62 //widthInChars = UNLIMITED;
63 textLimit = UNLIMITED;
64 validateStrategy = VALIDATE_ON_KEY_STROKE;
65 }
66
67 /**
68 * Creates a password field editor.
69 * Use the method <code>setTextLimit</code> to limit the text.
70 *
71 * @param name the name of the preference this field editor works on
72 * @param labelText the label text of the field editor
73 * @param width the width of the text input field in characters,
74 * or <code>UNLIMITED</code> for no limit
75 * @param strategy either <code>VALIDATE_ON_KEY_STROKE</code> to perform
76 * on the fly checking (the default), or <code>VALIDATE_ON_FOCUS_LOST</code> to
77 * perform validation only after the text has been typed in
78 * @param parent the parent of the field editor's control
79 */
80 public this(String name, String labelText, int width,
81 int strategy, Composite parent) {
82 super(name, labelText, width, strategy, parent);
83 }
84
85 /**
86 * Creates a password field editor.
87 * Use the method <code>setTextLimit</code> to limit the text.
88 *
89 * @param name the name of the preference this field editor works on
90 * @param labelText the label text of the field editor
91 * @param width the width of the text input field in characters,
92 * or <code>UNLIMITED</code> for no limit
93 * @param parent the parent of the field editor's control
94 */
95 public this(String name, String labelText, int width,
96 Composite parent) {
97 this(name, labelText, width, VALIDATE_ON_KEY_STROKE, parent);
98 }
99
100 /**
101 * Creates a password field editor of unlimited width.
102 * Use the method <code>setTextLimit</code> to limit the text.
103 *
104 * @param name the name of the preference this field editor works on
105 * @param labelText the label text of the field editor
106 * @param parent the parent of the field editor's control
107 */
108 public this(String name, String labelText, Composite parent) {
109 this(name, labelText, UNLIMITED, parent);
110 }
111
112
113
114 /**
115 * Returns this field editor's text control.
116 * <p>
117 * The control is created if it does not yet exist
118 * </p>
119 *
120 * @param parent the parent
121 * @return the text control
122 */
123 public Text getTextControl(Composite parent) {
124 if (textField is null) {
125 textField = new Text(parent, DWT.PASSWORD | DWT.SINGLE | DWT.BORDER);
126 textField.setFont(parent.getFont());
127 switch (validateStrategy) {
128 case VALIDATE_ON_KEY_STROKE:
129 textField.addKeyListener(new class() KeyAdapter {
130
131 /* (non-Javadoc)
132 * @see dwt.events.KeyAdapter#keyReleased(dwt.events.KeyEvent)
133 */
134 public void keyReleased(KeyEvent e) {
135 valueChanged();
136 }
137 });
138
139 break;
140 case VALIDATE_ON_FOCUS_LOST:
141 textField.addKeyListener(new class() KeyAdapter {
142 public void keyPressed(KeyEvent e) {
143 clearErrorMessage();
144 }
145 });
146 textField.addFocusListener(new class() FocusAdapter {
147 public void focusGained(FocusEvent e) {
148 refreshValidState();
149 }
150
151 public void focusLost(FocusEvent e) {
152 valueChanged();
153 clearErrorMessage();
154 }
155 });
156 break;
157 default:
158 Assert.isTrue(false, "Unknown validate strategy");//$NON-NLS-1$
159 }
160 textField.addDisposeListener(new class() DisposeListener {
161 public void widgetDisposed(DisposeEvent event) {
162 textField = null;
163 }
164 });
165 if (textLimit > 0) {//Only set limits above 0 - see SWT spec
166 textField.setTextLimit(textLimit);
167 }
168 } else {
169 checkParent(textField, parent);
170 }
171 return textField;
172 }
173
174 }