Added patch from #1437426: error in assign template
[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         private ComboDialogField fPriorityDialogField;
47
48         private List fExistingNames;
49
50         public TodoTaskInputDialog(Shell parent, TodoTask task, List existingEntries) {
51                 super(parent);
52
53                 fExistingNames= new ArrayList(existingEntries.size());
54                 for (int i= 0; i < existingEntries.size(); i++) {
55                         TodoTask curr= (TodoTask) existingEntries.get(i);
56                         if (!curr.equals(task)) {
57                                 fExistingNames.add(curr.name);
58                         }
59                 }
60
61                 if (task == null) {
62                         setTitle(PreferencesMessages.getString("TodoTaskInputDialog.new.title")); //$NON-NLS-1$
63                 } else {
64                         setTitle(PreferencesMessages.getString("TodoTaskInputDialog.edit.title")); //$NON-NLS-1$
65                 }
66
67                 CompilerTodoTaskInputAdapter adapter= new CompilerTodoTaskInputAdapter();
68
69                 fNameDialogField= new StringDialogField();
70                 fNameDialogField.setLabelText(PreferencesMessages.getString("TodoTaskInputDialog.name.label")); //$NON-NLS-1$
71                 fNameDialogField.setDialogFieldListener(adapter);
72
73                 fNameDialogField.setText((task != null) ? task.name : ""); //$NON-NLS-1$
74
75                 String[] items= new String[] {
76                         PreferencesMessages.getString("TodoTaskInputDialog.priority.high"), //$NON-NLS-1$
77                         PreferencesMessages.getString("TodoTaskInputDialog.priority.normal"), //$NON-NLS-1$
78                         PreferencesMessages.getString("TodoTaskInputDialog.priority.low") //$NON-NLS-1$
79                 };
80
81                 fPriorityDialogField= new ComboDialogField(SWT.READ_ONLY);
82                 fPriorityDialogField.setLabelText(PreferencesMessages.getString("TodoTaskInputDialog.priority.label")); //$NON-NLS-1$
83                 fPriorityDialogField.setItems(items);
84                 if (task != null) {
85                         if (JavaCore.COMPILER_TASK_PRIORITY_HIGH.equals(task.priority)) {
86                                 fPriorityDialogField.selectItem(0);
87                         } else if (JavaCore.COMPILER_TASK_PRIORITY_NORMAL.equals(task.priority)) {
88                                 fPriorityDialogField.selectItem(1);
89                         } else {
90                                 fPriorityDialogField.selectItem(2);
91                         }
92                 } else {
93                         fPriorityDialogField.selectItem(1);
94                 }
95         }
96
97         public TodoTask getResult() {
98                 TodoTask task= new TodoTask();
99                 task.name= fNameDialogField.getText().trim();
100                 switch (fPriorityDialogField.getSelectionIndex()) {
101                         case 0 :
102                                         task.priority= JavaCore.COMPILER_TASK_PRIORITY_HIGH;
103                                 break;
104                         case 1 :
105                                         task.priority= JavaCore.COMPILER_TASK_PRIORITY_NORMAL;
106                                 break;
107                         default :
108                                         task.priority= JavaCore.COMPILER_TASK_PRIORITY_LOW;
109                                 break;
110                 }
111                 return task;
112         }
113
114         protected Control createDialogArea(Composite parent) {
115                 Composite composite= (Composite) super.createDialogArea(parent);
116
117                 Composite inner= new Composite(composite, SWT.NONE);
118                 GridLayout layout= new GridLayout();
119                 layout.marginHeight= 0;
120                 layout.marginWidth= 0;
121                 layout.numColumns= 2;
122                 inner.setLayout(layout);
123
124                 fNameDialogField.doFillIntoGrid(inner, 2);
125                 fPriorityDialogField.doFillIntoGrid(inner, 2);
126
127                 LayoutUtil.setHorizontalGrabbing(fNameDialogField.getTextControl(null));
128                 LayoutUtil.setWidthHint(fNameDialogField.getTextControl(null), convertWidthInCharsToPixels(45));
129
130                 fNameDialogField.postSetFocusOnDialogField(parent.getDisplay());
131
132                 applyDialogFont(composite);
133                 return composite;
134         }
135
136         private void doValidation() {
137                 StatusInfo status= new StatusInfo();
138                 String newText= fNameDialogField.getText();
139                 if (newText.length() == 0) {
140                         status.setError(PreferencesMessages.getString("TodoTaskInputDialog.error.enterName")); //$NON-NLS-1$
141                 } else {
142                         if (newText.indexOf(',') != -1) {
143                                 status.setError(PreferencesMessages.getString("TodoTaskInputDialog.error.comma")); //$NON-NLS-1$
144                         } else if (fExistingNames.contains(newText)) {
145                                 status.setError(PreferencesMessages.getString("TodoTaskInputDialog.error.entryExists")); //$NON-NLS-1$
146                         } else if (Character.isWhitespace(newText.charAt(0)) ||  Character.isWhitespace(newText.charAt(newText.length() - 1))) {
147                                 status.setError(PreferencesMessages.getString("TodoTaskInputDialog.error.noSpace")); //$NON-NLS-1$
148                         }
149                 }
150                 updateStatus(status);
151         }
152
153         /*
154          * @see org.eclipse.jface.window.Window#configureShell(Shell)
155          */
156         protected void configureShell(Shell newShell) {
157                 super.configureShell(newShell);
158                 PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, IJavaHelpContextIds.TODO_TASK_INPUT_DIALOG);
159         }
160 }