Initial import of the Guide contributed to the project byRobert Kraske. Was originall...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / preferences / TodoTaskInputDialog.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.preferences;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import net.sourceforge.phpdt.core.JavaCore;
17 import net.sourceforge.phpdt.internal.ui.IJavaHelpContextIds;
18 import net.sourceforge.phpdt.internal.ui.dialogs.StatusDialog;
19 import net.sourceforge.phpdt.internal.ui.dialogs.StatusInfo;
20 import net.sourceforge.phpdt.internal.ui.preferences.TodoTaskConfigurationBlock.TodoTask;
21 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.ComboDialogField;
22 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.DialogField;
23 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
24 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.LayoutUtil;
25 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.StringDialogField;
26
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Control;
31 import org.eclipse.swt.widgets.Shell;
32 import org.eclipse.ui.PlatformUI;
33
34 /**
35  * Dialog to enter a na new task tag
36  */
37 public class TodoTaskInputDialog extends StatusDialog {
38
39         private class CompilerTodoTaskInputAdapter implements IDialogFieldListener {
40                 public void dialogFieldChanged(DialogField field) {
41                         doValidation();
42                 }
43         }
44
45         private StringDialogField fNameDialogField;
46
47         private ComboDialogField fPriorityDialogField;
48
49         private List fExistingNames;
50
51         public TodoTaskInputDialog(Shell parent, TodoTask task, List existingEntries) {
52                 super(parent);
53
54                 fExistingNames = new ArrayList(existingEntries.size());
55                 for (int i = 0; i < existingEntries.size(); i++) {
56                         TodoTask curr = (TodoTask) existingEntries.get(i);
57                         if (!curr.equals(task)) {
58                                 fExistingNames.add(curr.name);
59                         }
60                 }
61
62                 if (task == null) {
63                         setTitle(PreferencesMessages
64                                         .getString("TodoTaskInputDialog.new.title")); //$NON-NLS-1$
65                 } else {
66                         setTitle(PreferencesMessages
67                                         .getString("TodoTaskInputDialog.edit.title")); //$NON-NLS-1$
68                 }
69
70                 CompilerTodoTaskInputAdapter adapter = new CompilerTodoTaskInputAdapter();
71
72                 fNameDialogField = new StringDialogField();
73                 fNameDialogField.setLabelText(PreferencesMessages
74                                 .getString("TodoTaskInputDialog.name.label")); //$NON-NLS-1$
75                 fNameDialogField.setDialogFieldListener(adapter);
76
77                 fNameDialogField.setText((task != null) ? task.name : ""); //$NON-NLS-1$
78
79                 String[] items = new String[] {
80                                 PreferencesMessages
81                                                 .getString("TodoTaskInputDialog.priority.high"), //$NON-NLS-1$
82                                 PreferencesMessages
83                                                 .getString("TodoTaskInputDialog.priority.normal"), //$NON-NLS-1$
84                                 PreferencesMessages
85                                                 .getString("TodoTaskInputDialog.priority.low") //$NON-NLS-1$
86                 };
87
88                 fPriorityDialogField = new ComboDialogField(SWT.READ_ONLY);
89                 fPriorityDialogField.setLabelText(PreferencesMessages
90                                 .getString("TodoTaskInputDialog.priority.label")); //$NON-NLS-1$
91                 fPriorityDialogField.setItems(items);
92                 if (task != null) {
93                         if (JavaCore.COMPILER_TASK_PRIORITY_HIGH.equals(task.priority)) {
94                                 fPriorityDialogField.selectItem(0);
95                         } else if (JavaCore.COMPILER_TASK_PRIORITY_NORMAL
96                                         .equals(task.priority)) {
97                                 fPriorityDialogField.selectItem(1);
98                         } else {
99                                 fPriorityDialogField.selectItem(2);
100                         }
101                 } else {
102                         fPriorityDialogField.selectItem(1);
103                 }
104         }
105
106         public TodoTask getResult() {
107                 TodoTask task = new TodoTask();
108                 task.name = fNameDialogField.getText().trim();
109                 switch (fPriorityDialogField.getSelectionIndex()) {
110                 case 0:
111                         task.priority = JavaCore.COMPILER_TASK_PRIORITY_HIGH;
112                         break;
113                 case 1:
114                         task.priority = JavaCore.COMPILER_TASK_PRIORITY_NORMAL;
115                         break;
116                 default:
117                         task.priority = JavaCore.COMPILER_TASK_PRIORITY_LOW;
118                         break;
119                 }
120                 return task;
121         }
122
123         protected Control createDialogArea(Composite parent) {
124                 Composite composite = (Composite) super.createDialogArea(parent);
125
126                 Composite inner = new Composite(composite, SWT.NONE);
127                 GridLayout layout = new GridLayout();
128                 layout.marginHeight = 0;
129                 layout.marginWidth = 0;
130                 layout.numColumns = 2;
131                 inner.setLayout(layout);
132
133                 fNameDialogField.doFillIntoGrid(inner, 2);
134                 fPriorityDialogField.doFillIntoGrid(inner, 2);
135
136                 LayoutUtil.setHorizontalGrabbing(fNameDialogField.getTextControl(null));
137                 LayoutUtil.setWidthHint(fNameDialogField.getTextControl(null),
138                                 convertWidthInCharsToPixels(45));
139
140                 fNameDialogField.postSetFocusOnDialogField(parent.getDisplay());
141
142                 applyDialogFont(composite);
143                 return composite;
144         }
145
146         private void doValidation() {
147                 StatusInfo status = new StatusInfo();
148                 String newText = fNameDialogField.getText();
149                 if (newText.length() == 0) {
150                         status.setError(PreferencesMessages
151                                         .getString("TodoTaskInputDialog.error.enterName")); //$NON-NLS-1$
152                 } else {
153                         if (newText.indexOf(',') != -1) {
154                                 status.setError(PreferencesMessages
155                                                 .getString("TodoTaskInputDialog.error.comma")); //$NON-NLS-1$
156                         } else if (fExistingNames.contains(newText)) {
157                                 status.setError(PreferencesMessages
158                                                 .getString("TodoTaskInputDialog.error.entryExists")); //$NON-NLS-1$
159                         } else if (Character.isWhitespace(newText.charAt(0))
160                                         || Character.isWhitespace(newText
161                                                         .charAt(newText.length() - 1))) {
162                                 status.setError(PreferencesMessages
163                                                 .getString("TodoTaskInputDialog.error.noSpace")); //$NON-NLS-1$
164                         }
165                 }
166                 updateStatus(status);
167         }
168
169         /*
170          * @see org.eclipse.jface.window.Window#configureShell(Shell)
171          */
172         protected void configureShell(Shell newShell) {
173                 super.configureShell(newShell);
174                 PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell,
175                                 IJavaHelpContextIds.TODO_TASK_INPUT_DIALOG);
176         }
177 }