KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > preferences > TodoTaskInputDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.jdt.internal.ui.preferences;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.layout.GridLayout;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.swt.widgets.Shell;
21
22 import org.eclipse.jface.dialogs.StatusDialog;
23
24 import org.eclipse.ui.PlatformUI;
25
26 import org.eclipse.jdt.core.JavaCore;
27
28 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
29 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
30 import org.eclipse.jdt.internal.ui.preferences.TodoTaskConfigurationBlock.TodoTask;
31 import org.eclipse.jdt.internal.ui.wizards.dialogfields.ComboDialogField;
32 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
33 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
34 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
35 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField;
36
37 /**
38  * Dialog to enter a na new task tag
39  */

40 public class TodoTaskInputDialog extends StatusDialog {
41     
42     private class CompilerTodoTaskInputAdapter implements IDialogFieldListener {
43         public void dialogFieldChanged(DialogField field) {
44             doValidation();
45         }
46     }
47     
48     private StringDialogField fNameDialogField;
49     private ComboDialogField fPriorityDialogField;
50     
51     private List JavaDoc fExistingNames;
52         
53     public TodoTaskInputDialog(Shell parent, TodoTask task, List JavaDoc existingEntries) {
54         super(parent);
55         
56         fExistingNames= new ArrayList JavaDoc(existingEntries.size());
57         for (int i= 0; i < existingEntries.size(); i++) {
58             TodoTask curr= (TodoTask) existingEntries.get(i);
59             if (!curr.equals(task)) {
60                 fExistingNames.add(curr.name);
61             }
62         }
63         
64         if (task == null) {
65             setTitle(PreferencesMessages.TodoTaskInputDialog_new_title);
66         } else {
67             setTitle(PreferencesMessages.TodoTaskInputDialog_edit_title);
68         }
69
70         CompilerTodoTaskInputAdapter adapter= new CompilerTodoTaskInputAdapter();
71
72         fNameDialogField= new StringDialogField();
73         fNameDialogField.setLabelText(PreferencesMessages.TodoTaskInputDialog_name_label);
74         fNameDialogField.setDialogFieldListener(adapter);
75         
76         fNameDialogField.setText((task != null) ? task.name : ""); //$NON-NLS-1$
77

78         String JavaDoc[] items= new String JavaDoc[] {
79             PreferencesMessages.TodoTaskInputDialog_priority_high,
80             PreferencesMessages.TodoTaskInputDialog_priority_normal,
81             PreferencesMessages.TodoTaskInputDialog_priority_low
82         };
83         
84         fPriorityDialogField= new ComboDialogField(SWT.READ_ONLY);
85         fPriorityDialogField.setLabelText(PreferencesMessages.TodoTaskInputDialog_priority_label);
86         fPriorityDialogField.setItems(items);
87         if (task != null) {
88             if (JavaCore.COMPILER_TASK_PRIORITY_HIGH.equals(task.priority)) {
89                 fPriorityDialogField.selectItem(0);
90             } else if (JavaCore.COMPILER_TASK_PRIORITY_NORMAL.equals(task.priority)) {
91                 fPriorityDialogField.selectItem(1);
92             } else {
93                 fPriorityDialogField.selectItem(2);
94             }
95         } else {
96             fPriorityDialogField.selectItem(1);
97         }
98     }
99     
100     public TodoTask getResult() {
101         TodoTask task= new TodoTask();
102         task.name= fNameDialogField.getText().trim();
103         switch (fPriorityDialogField.getSelectionIndex()) {
104             case 0 :
105                     task.priority= JavaCore.COMPILER_TASK_PRIORITY_HIGH;
106                 break;
107             case 1 :
108                     task.priority= JavaCore.COMPILER_TASK_PRIORITY_NORMAL;
109                 break;
110             default :
111                     task.priority= JavaCore.COMPILER_TASK_PRIORITY_LOW;
112                 break;
113         }
114         return task;
115     }
116     
117     protected Control createDialogArea(Composite parent) {
118         Composite composite= (Composite) super.createDialogArea(parent);
119         
120         Composite inner= new Composite(composite, SWT.NONE);
121         GridLayout layout= new GridLayout();
122         layout.marginHeight= 0;
123         layout.marginWidth= 0;
124         layout.numColumns= 2;
125         inner.setLayout(layout);
126         
127         fNameDialogField.doFillIntoGrid(inner, 2);
128         fPriorityDialogField.doFillIntoGrid(inner, 2);
129         
130         LayoutUtil.setHorizontalGrabbing(fNameDialogField.getTextControl(null));
131         LayoutUtil.setWidthHint(fNameDialogField.getTextControl(null), convertWidthInCharsToPixels(45));
132         
133         fNameDialogField.postSetFocusOnDialogField(parent.getDisplay());
134         
135         applyDialogFont(composite);
136         
137         PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IJavaHelpContextIds.TASK_TAG_INPUT_DIALOG);
138         
139         return composite;
140     }
141         
142     private void doValidation() {
143         StatusInfo status= new StatusInfo();
144         String JavaDoc newText= fNameDialogField.getText();
145         if (newText.length() == 0) {
146             status.setError(PreferencesMessages.TodoTaskInputDialog_error_enterName);
147         } else {
148             if (newText.indexOf(',') != -1) {
149                 status.setError(PreferencesMessages.TodoTaskInputDialog_error_comma);
150             } else if (fExistingNames.contains(newText)) {
151                 status.setError(PreferencesMessages.TodoTaskInputDialog_error_entryExists);
152             } else if (Character.isWhitespace(newText.charAt(0)) || Character.isWhitespace(newText.charAt(newText.length() - 1))) {
153                 status.setError(PreferencesMessages.TodoTaskInputDialog_error_noSpace);
154             }
155         }
156         updateStatus(status);
157     }
158
159     /*
160      * @see org.eclipse.jface.window.Window#configureShell(Shell)
161      */

162     protected void configureShell(Shell newShell) {
163         super.configureShell(newShell);
164         PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, IJavaHelpContextIds.TODO_TASK_INPUT_DIALOG);
165     }
166 }
167
Popular Tags