fixed bug #1037094 (foreach)
[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 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.ui.help.WorkbenchHelp;
23
24 import net.sourceforge.phpdt.core.JavaCore;
25
26 import net.sourceforge.phpdt.internal.ui.IJavaHelpContextIds;
27 import net.sourceforge.phpdt.internal.ui.dialogs.StatusDialog;
28 import net.sourceforge.phpdt.internal.ui.dialogs.StatusInfo;
29 import net.sourceforge.phpdt.internal.ui.preferences.TodoTaskConfigurationBlock.TodoTask;
30 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.ComboDialogField;
31 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.DialogField;
32 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
33 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.LayoutUtil;
34 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.StringDialogField;
35
36 /**
37  * Dialog to enter a na new task tag
38  */
39 public class TodoTaskInputDialog extends StatusDialog {
40         
41         private class CompilerTodoTaskInputAdapter implements IDialogFieldListener {
42                 public void dialogFieldChanged(DialogField field) {
43                         doValidation();
44                 }                       
45         }
46         
47         private StringDialogField fNameDialogField;
48         private ComboDialogField fPriorityDialogField;
49         
50         private List fExistingNames;
51                 
52         public TodoTaskInputDialog(Shell parent, TodoTask task, List existingEntries) {
53                 super(parent);
54                 
55                 fExistingNames= new ArrayList(existingEntries.size());
56                 for (int i= 0; i < existingEntries.size(); i++) {
57                         TodoTask curr= (TodoTask) existingEntries.get(i);
58                         if (!curr.equals(task)) {
59                                 fExistingNames.add(curr.name);
60                         }
61                 }
62                 
63                 if (task == null) {
64                         setTitle(PreferencesMessages.getString("TodoTaskInputDialog.new.title")); //$NON-NLS-1$
65                 } else {
66                         setTitle(PreferencesMessages.getString("TodoTaskInputDialog.edit.title")); //$NON-NLS-1$
67                 }
68
69                 CompilerTodoTaskInputAdapter adapter= new CompilerTodoTaskInputAdapter();
70
71                 fNameDialogField= new StringDialogField();
72                 fNameDialogField.setLabelText(PreferencesMessages.getString("TodoTaskInputDialog.name.label")); //$NON-NLS-1$
73                 fNameDialogField.setDialogFieldListener(adapter);
74                 
75                 fNameDialogField.setText((task != null) ? task.name : ""); //$NON-NLS-1$
76                 
77                 String[] items= new String[] {
78                         PreferencesMessages.getString("TodoTaskInputDialog.priority.high"), //$NON-NLS-1$
79                         PreferencesMessages.getString("TodoTaskInputDialog.priority.normal"), //$NON-NLS-1$
80                         PreferencesMessages.getString("TodoTaskInputDialog.priority.low") //$NON-NLS-1$
81                 };
82                 
83                 fPriorityDialogField= new ComboDialogField(SWT.READ_ONLY);
84                 fPriorityDialogField.setLabelText(PreferencesMessages.getString("TodoTaskInputDialog.priority.label")); //$NON-NLS-1$
85                 fPriorityDialogField.setItems(items);
86                 if (task != null) {
87                         if (JavaCore.COMPILER_TASK_PRIORITY_HIGH.equals(task.priority)) {
88                                 fPriorityDialogField.selectItem(0);
89                         } else if (JavaCore.COMPILER_TASK_PRIORITY_NORMAL.equals(task.priority)) {
90                                 fPriorityDialogField.selectItem(1);
91                         } else {
92                                 fPriorityDialogField.selectItem(2);
93                         }
94                 } else {
95                         fPriorityDialogField.selectItem(1);
96                 }
97         }
98         
99         public TodoTask getResult() {
100                 TodoTask task= new TodoTask();
101                 task.name= fNameDialogField.getText().trim();
102                 switch (fPriorityDialogField.getSelectionIndex()) {
103                         case 0 :
104                                         task.priority= JavaCore.COMPILER_TASK_PRIORITY_HIGH;
105                                 break;
106                         case 1 :
107                                         task.priority= JavaCore.COMPILER_TASK_PRIORITY_NORMAL;
108                                 break;
109                         default :
110                                         task.priority= JavaCore.COMPILER_TASK_PRIORITY_LOW;
111                                 break;                          
112                 }
113                 return task;
114         }
115         
116         protected Control createDialogArea(Composite parent) {
117                 Composite composite= (Composite) super.createDialogArea(parent);
118                 
119                 Composite inner= new Composite(composite, SWT.NONE);
120                 GridLayout layout= new GridLayout();
121                 layout.marginHeight= 0;
122                 layout.marginWidth= 0;
123                 layout.numColumns= 2;
124                 inner.setLayout(layout);
125                 
126                 fNameDialogField.doFillIntoGrid(inner, 2);
127                 fPriorityDialogField.doFillIntoGrid(inner, 2);
128                 
129                 LayoutUtil.setHorizontalGrabbing(fNameDialogField.getTextControl(null));
130                 LayoutUtil.setWidthHint(fNameDialogField.getTextControl(null), convertWidthInCharsToPixels(45));
131                 
132                 fNameDialogField.postSetFocusOnDialogField(parent.getDisplay());
133                 
134                 applyDialogFont(composite);             
135                 return composite;
136         }
137                 
138         private void doValidation() {
139                 StatusInfo status= new StatusInfo();
140                 String newText= fNameDialogField.getText();
141                 if (newText.length() == 0) {
142                         status.setError(PreferencesMessages.getString("TodoTaskInputDialog.error.enterName")); //$NON-NLS-1$
143                 } else {
144                         if (newText.indexOf(',') != -1) {
145                                 status.setError(PreferencesMessages.getString("TodoTaskInputDialog.error.comma")); //$NON-NLS-1$
146                         } else if (fExistingNames.contains(newText)) {
147                                 status.setError(PreferencesMessages.getString("TodoTaskInputDialog.error.entryExists")); //$NON-NLS-1$
148                         } else if (Character.isWhitespace(newText.charAt(0)) ||  Character.isWhitespace(newText.charAt(newText.length() - 1))) {
149                                 status.setError(PreferencesMessages.getString("TodoTaskInputDialog.error.noSpace")); //$NON-NLS-1$
150                         }
151                 }
152                 updateStatus(status);
153         }
154
155         /*
156          * @see org.eclipse.jface.window.Window#configureShell(Shell)
157          */
158         protected void configureShell(Shell newShell) {
159                 super.configureShell(newShell);
160                 WorkbenchHelp.setHelp(newShell, IJavaHelpContextIds.TODO_TASK_INPUT_DIALOG);
161         }
162 }