KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > CommentTemplateEditDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.ui;
12
13 import org.eclipse.jface.dialogs.Dialog;
14 import org.eclipse.jface.dialogs.IDialogConstants;
15 import org.eclipse.jface.dialogs.IInputValidator;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.ModifyEvent;
18 import org.eclipse.swt.events.ModifyListener;
19 import org.eclipse.swt.layout.GridData;
20 import org.eclipse.swt.widgets.Button;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.swt.widgets.Label;
24 import org.eclipse.swt.widgets.Shell;
25 import org.eclipse.swt.widgets.Text;
26
27 /**
28  * A simple input dialog for soliciting an input string from the user.
29  * <p>
30  * This concrete dialog class can be instantiated as is, or further subclassed as
31  * required.
32  * </p>
33  */

34 public class CommentTemplateEditDialog extends Dialog {
35     /**
36      * The title of the dialog.
37      */

38     private String JavaDoc title;
39
40     /**
41      * The message to display, or <code>null</code> if none.
42      */

43     private String JavaDoc message;
44
45     /**
46      * The input value; the empty string by default.
47      */

48     private String JavaDoc value = "";//$NON-NLS-1$
49

50     /**
51      * The input validator, or <code>null</code> if none.
52      */

53     private IInputValidator validator;
54
55     /**
56      * Ok button widget.
57      */

58     private Button okButton;
59
60     /**
61      * Input text widget.
62      */

63     private Text text;
64
65     /**
66      * Error message label widget.
67      */

68     private Text errorMessageText;
69
70     /**
71      * Creates an input dialog with OK and Cancel buttons. Note that the dialog
72      * will have no visual representation (no widgets) until it is told to open.
73      * <p>
74      * Note that the <code>open</code> method blocks for input dialogs.
75      * </p>
76      *
77      * @param parentShell
78      * the parent shell, or <code>null</code> to create a top-level
79      * shell
80      * @param dialogTitle
81      * the dialog title, or <code>null</code> if none
82      * @param dialogMessage
83      * the dialog message, or <code>null</code> if none
84      * @param initialValue
85      * the initial input value, or <code>null</code> if none
86      * (equivalent to the empty string)
87      * @param validator
88      * an input validator, or <code>null</code> if none
89      */

90     public CommentTemplateEditDialog(Shell parentShell, String JavaDoc dialogTitle,
91             String JavaDoc dialogMessage, String JavaDoc initialValue, IInputValidator validator) {
92         super(parentShell);
93         this.title = dialogTitle;
94         message = dialogMessage;
95         if (initialValue == null)
96             value = "";//$NON-NLS-1$
97
else
98             value = initialValue;
99         this.validator = validator;
100     }
101
102     /*
103      * (non-Javadoc) Method declared on Dialog.
104      */

105     protected void buttonPressed(int buttonId) {
106         if (buttonId == IDialogConstants.OK_ID) {
107             value = text.getText();
108         } else {
109             value = null;
110         }
111         super.buttonPressed(buttonId);
112     }
113
114     /*
115      * (non-Javadoc)
116      *
117      * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
118      */

119     protected void configureShell(Shell shell) {
120         super.configureShell(shell);
121         if (title != null)
122             shell.setText(title);
123     }
124
125     /*
126      * (non-Javadoc)
127      *
128      * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
129      */

130     protected void createButtonsForButtonBar(Composite parent) {
131         // create OK and Cancel buttons by default
132
okButton = createButton(parent, IDialogConstants.OK_ID,
133                 IDialogConstants.OK_LABEL, true);
134         createButton(parent, IDialogConstants.CANCEL_ID,
135                 IDialogConstants.CANCEL_LABEL, false);
136         //do this here because setting the text will set enablement on the ok
137
// button
138
text.setFocus();
139         if (value != null) {
140             text.setText(value);
141             text.selectAll();
142         }
143     }
144
145     /*
146      * (non-Javadoc) Method declared on Dialog.
147      */

148     protected Control createDialogArea(Composite parent) {
149         // create composite
150
Composite composite = (Composite) super.createDialogArea(parent);
151         // create message
152
if (message != null) {
153             Label label = new Label(composite, SWT.WRAP);
154             label.setText(message);
155             GridData data = new GridData(GridData.GRAB_HORIZONTAL
156                     | GridData.GRAB_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL
157                     | GridData.VERTICAL_ALIGN_CENTER);
158             data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
159             label.setLayoutData(data);
160             label.setFont(parent.getFont());
161         }
162         text = new Text(composite, SWT.MULTI | SWT.BORDER);
163         GridData gd = new GridData(GridData.GRAB_HORIZONTAL
164                 | GridData.HORIZONTAL_ALIGN_FILL);
165         gd.heightHint = convertHeightInCharsToPixels(5);
166         text.setLayoutData(gd);
167         text.addModifyListener(new ModifyListener() {
168             public void modifyText(ModifyEvent e) {
169                 validateInput();
170             }
171         });
172         errorMessageText = new Text(composite, SWT.READ_ONLY);
173         errorMessageText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
174                 | GridData.HORIZONTAL_ALIGN_FILL));
175         errorMessageText.setBackground(errorMessageText.getDisplay()
176                 .getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
177
178         applyDialogFont(composite);
179         return composite;
180     }
181
182     /**
183      * Returns the error message label.
184      *
185      * @return the error message label
186      * @deprecated use setErrorMessage(String) instead
187      */

188     protected Label getErrorMessageLabel() {
189         return null;
190     }
191
192     /**
193      * Returns the ok button.
194      *
195      * @return the ok button
196      */

197     protected Button getOkButton() {
198         return okButton;
199     }
200
201     /**
202      * Returns the text area.
203      *
204      * @return the text area
205      */

206     protected Text getText() {
207         return text;
208     }
209
210     /**
211      * Returns the validator.
212      *
213      * @return the validator
214      */

215     protected IInputValidator getValidator() {
216         return validator;
217     }
218
219     /**
220      * Returns the string typed into this input dialog.
221      *
222      * @return the input string
223      */

224     public String JavaDoc getValue() {
225         return value;
226     }
227
228     /**
229      * Validates the input.
230      * <p>
231      * The default implementation of this framework method delegates the request
232      * to the supplied input validator object; if it finds the input invalid,
233      * the error message is displayed in the dialog's message line. This hook
234      * method is called whenever the text changes in the input field.
235      * </p>
236      */

237     protected void validateInput() {
238         String JavaDoc errorMessage = null;
239         if (validator != null) {
240             errorMessage = validator.isValid(text.getText());
241         }
242         // Bug 16256: important not to treat "" (blank error) the same as null
243
// (no error)
244
setErrorMessage(errorMessage);
245     }
246
247     /**
248      * Sets or clears the error message.
249      * If not <code>null</code>, the OK button is disabled.
250      *
251      * @param errorMessage
252      * the error message, or <code>null</code> to clear
253      * @since 3.0
254      */

255     public void setErrorMessage(String JavaDoc errorMessage) {
256         errorMessageText.setText(errorMessage == null ? "" : errorMessage); //$NON-NLS-1$
257
okButton.setEnabled(errorMessage == null);
258         errorMessageText.getParent().update();
259     }
260 }
261
Popular Tags