Source code: com/port80/eclipse/util/widgets/InputWithHistoryDialog.java
1 /*
2 * Created on Apr 25, 2003
3 */
4 package com.port80.eclipse.util.widgets;
5
6 import org.eclipse.jface.dialogs.Dialog;
7 import org.eclipse.jface.dialogs.IDialogConstants;
8 import org.eclipse.jface.dialogs.IInputValidator;
9 import org.eclipse.swt.SWT;
10 import org.eclipse.swt.events.ModifyEvent;
11 import org.eclipse.swt.events.ModifyListener;
12 import org.eclipse.swt.graphics.Point;
13 import org.eclipse.swt.layout.GridData;
14 import org.eclipse.swt.widgets.Button;
15 import org.eclipse.swt.widgets.Combo;
16 import org.eclipse.swt.widgets.Composite;
17 import org.eclipse.swt.widgets.Control;
18 import org.eclipse.swt.widgets.Label;
19 import org.eclipse.swt.widgets.Shell;
20
21 /**
22 * @author chrisl
23 */
24 public class InputWithHistoryDialog extends Dialog {
25
26 ////////////////////////////////////////////////////////////////////////
27
28 protected static final String TITLE = "Enter text";
29
30 ////////////////////////////////////////////////////////////////////////
31
32 protected String fTitle;
33 protected String fHeader;
34 protected String[] fHistory;
35 protected int fIndex;
36 //
37 protected IInputValidator fValidator;
38 protected Combo fTextCombo;
39 protected Button fOkButton;
40 protected Label fStatus;
41 //
42 protected String fValue;
43
44 ////////////////////////////////////////////////////////////////////////
45
46 public InputWithHistoryDialog(Shell parent) {
47 this(parent, TITLE, TITLE, null, 0, null);
48 }
49
50 public InputWithHistoryDialog(Shell parent, String header, String[] history, int index) {
51 this(parent, TITLE, header, history, index, null);
52 }
53
54 public InputWithHistoryDialog(
55 Shell parent,
56 String title,
57 String header,
58 String[] history,
59 int index,
60 IInputValidator validator) {
61 //
62 super(parent);
63 fTitle = title;
64 fHeader = header;
65 fHistory = history;
66 fIndex = index;
67 fValidator = validator;
68 }
69
70 ////////////////////////////////////////////////////////////////////////
71
72 protected void buttonPressed(int buttonId) {
73 if (buttonId == IDialogConstants.OK_ID) {
74 fValue = fTextCombo.getText();
75 } else {
76 fValue = null;
77 }
78 super.buttonPressed(buttonId);
79 }
80
81 protected void configureShell(Shell shell) {
82 super.configureShell(shell);
83 if (fTitle != null)
84 shell.setText(fTitle);
85 }
86
87 protected void createButtonsForButtonBar(Composite parent) {
88 // create OK and Cancel buttons by default
89 fOkButton = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
90 createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
91 //
92 //do this here because setting the text will set enablement on the ok button
93 fTextCombo.setFocus();
94 if (fHistory != null && fHistory.length > 0) {
95 fValue = (String) fHistory[fIndex];
96 fTextCombo.setItems(fHistory);
97 fTextCombo.select(fIndex);
98 fTextCombo.setText(fValue);
99 fTextCombo.setSelection(new Point(0, fValue.length()));
100 }
101 }
102 /* (non-Javadoc)
103 * Method declared on Dialog.
104 */
105 protected Control createDialogArea(Composite parent) {
106 // create composite
107 Composite composite = (Composite) super.createDialogArea(parent);
108
109 // create message
110 if (fHeader != null) {
111 Label label = new Label(composite, SWT.WRAP);
112 label.setText(fHeader);
113 GridData data =
114 new GridData(
115 GridData.GRAB_HORIZONTAL
116 | GridData.GRAB_VERTICAL
117 | GridData.HORIZONTAL_ALIGN_FILL
118 | GridData.VERTICAL_ALIGN_CENTER);
119 data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
120 ;
121 label.setLayoutData(data);
122 label.setFont(parent.getFont());
123 }
124
125 fTextCombo = new Combo(composite, SWT.SINGLE | SWT.BORDER);
126 fTextCombo.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
127 fTextCombo.addModifyListener(new ModifyListener() {
128 public void modifyText(ModifyEvent e) {
129 validateInput();
130 }
131 });
132
133 fStatus = new Label(composite, SWT.NONE);
134 fStatus.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
135 fStatus.setFont(parent.getFont());
136
137 return composite;
138 }
139
140 /**
141 * Returns the error message label.
142 *
143 * @return the error message label
144 */
145 protected Label getErrorMessageLabel() {
146 return fStatus;
147 }
148
149 /**
150 * Returns the ok button.
151 *
152 * @return the ok button
153 */
154 protected Button getOkButton() {
155 return fOkButton;
156 }
157
158 /**
159 * Returns the validator.
160 *
161 * @return the validator
162 */
163 protected IInputValidator getValidator() {
164 return fValidator;
165 }
166
167 /**
168 * Returns the string typed into this input dialog.
169 *
170 * @return the input string
171 */
172 public String getValue() {
173 return fValue;
174 }
175
176 /**
177 * Validates the input.
178 * <p>
179 * The default implementation of this framework method
180 * delegates the request to the supplied input validator object;
181 * if it finds the input invalid, the error message is displayed
182 * in the dialog's message line.
183 * This hook method is called whenever the text changes in the
184 * input field.
185 * </p>
186 */
187 protected void validateInput() {
188 String errorMessage = null;
189 if (fValidator != null) {
190 errorMessage = fValidator.isValid(fTextCombo.getText());
191 }
192 // Bug 16256: important not to treat "" (blank error) the same as null (no error)
193 fStatus.setText(errorMessage == null ? "" : errorMessage); //$NON-NLS-1$
194 fOkButton.setEnabled(errorMessage == null);
195 fStatus.getParent().update();
196 }
197
198 }