Remove import of org.eclipse.ui.externaltools.internal.model.ExternalToolBuilder...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / preferences / TodoTaskConfigurationBlock.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.IJavaProject;
17 import net.sourceforge.phpdt.core.JavaCore;
18 import net.sourceforge.phpdt.internal.ui.dialogs.StatusInfo;
19 import net.sourceforge.phpdt.internal.ui.wizards.IStatusChangeListener;
20 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.DialogField;
21 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
22 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.IListAdapter;
23 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.LayoutUtil;
24 import net.sourceforge.phpdt.internal.ui.wizards.dialogfields.ListDialogField;
25
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.jface.viewers.ITableLabelProvider;
28 import org.eclipse.jface.viewers.LabelProvider;
29 import org.eclipse.jface.window.Window;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.graphics.Image;
32 import org.eclipse.swt.layout.GridData;
33 import org.eclipse.swt.layout.GridLayout;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Control;
36
37 /** 
38  */
39 public class TodoTaskConfigurationBlock extends OptionsConfigurationBlock {
40
41         private static final String PREF_COMPILER_TASK_TAGS = JavaCore.COMPILER_TASK_TAGS;
42
43         private static final String PREF_COMPILER_TASK_PRIORITIES = JavaCore.COMPILER_TASK_PRIORITIES;
44
45         private static final String PRIORITY_HIGH = JavaCore.COMPILER_TASK_PRIORITY_HIGH;
46
47         private static final String PRIORITY_NORMAL = JavaCore.COMPILER_TASK_PRIORITY_NORMAL;
48
49         private static final String PRIORITY_LOW = JavaCore.COMPILER_TASK_PRIORITY_LOW;
50
51         public static class TodoTask {
52                 public String name;
53
54                 public String priority;
55         }
56
57         private static class TodoTaskLabelProvider extends LabelProvider implements
58                         ITableLabelProvider {
59
60                 /*
61                  * (non-Javadoc)
62                  * 
63                  * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
64                  */
65                 public Image getImage(Object element) {
66                         return null; // JavaPluginImages.get(JavaPluginImages.IMG_OBJS_REFACTORING_INFO);
67                 }
68
69                 /*
70                  * (non-Javadoc)
71                  * 
72                  * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
73                  */
74                 public String getText(Object element) {
75                         return getColumnText(element, 0);
76                 }
77
78                 /*
79                  * (non-Javadoc)
80                  * 
81                  * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object,
82                  *      int)
83                  */
84                 public Image getColumnImage(Object element, int columnIndex) {
85                         return null;
86                 }
87
88                 /*
89                  * (non-Javadoc)
90                  * 
91                  * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object,
92                  *      int)
93                  */
94                 public String getColumnText(Object element, int columnIndex) {
95                         TodoTask task = (TodoTask) element;
96                         if (columnIndex == 0) {
97                                 return task.name;
98                         } else {
99                                 if (PRIORITY_HIGH.equals(task.priority)) {
100                                         return PreferencesMessages
101                                                         .getString("TodoTaskConfigurationBlock.markers.tasks.high.priority"); //$NON-NLS-1$
102                                 } else if (PRIORITY_NORMAL.equals(task.priority)) {
103                                         return PreferencesMessages
104                                                         .getString("TodoTaskConfigurationBlock.markers.tasks.normal.priority"); //$NON-NLS-1$
105                                 } else if (PRIORITY_LOW.equals(task.priority)) {
106                                         return PreferencesMessages
107                                                         .getString("TodoTaskConfigurationBlock.markers.tasks.low.priority"); //$NON-NLS-1$
108                                 }
109                                 return ""; //$NON-NLS-1$
110                         }
111                 }
112
113         }
114
115         private static final int IDX_ADD = 0;
116
117         private static final int IDX_EDIT = 1;
118
119         private static final int IDX_REMOVE = 2;
120
121         private IStatus fTaskTagsStatus;
122
123         private ListDialogField fTodoTasksList;
124
125         public TodoTaskConfigurationBlock(IStatusChangeListener context,
126                         IJavaProject project) {
127                 super(context, project, getKeys());
128
129                 TaskTagAdapter adapter = new TaskTagAdapter();
130                 String[] buttons = new String[] {
131                                 /* 0 */PreferencesMessages
132                                                 .getString("TodoTaskConfigurationBlock.markers.tasks.add.button"), //$NON-NLS-1$
133                                 /* 1 */PreferencesMessages
134                                                 .getString("TodoTaskConfigurationBlock.markers.tasks.edit.button"), //$NON-NLS-1$
135                                 /* 2 */PreferencesMessages
136                                                 .getString("TodoTaskConfigurationBlock.markers.tasks.remove.button"), //$NON-NLS-1$
137
138                 };
139                 fTodoTasksList = new ListDialogField(adapter, buttons,
140                                 new TodoTaskLabelProvider());
141                 fTodoTasksList.setDialogFieldListener(adapter);
142                 fTodoTasksList.setLabelText(PreferencesMessages
143                                 .getString("TodoTaskConfigurationBlock.markers.tasks.label")); //$NON-NLS-1$
144                 fTodoTasksList.setRemoveButtonIndex(IDX_REMOVE);
145
146                 String[] columnsHeaders = new String[] {
147                                 PreferencesMessages
148                                                 .getString("TodoTaskConfigurationBlock.markers.tasks.name.column"), //$NON-NLS-1$
149                                 PreferencesMessages
150                                                 .getString("TodoTaskConfigurationBlock.markers.tasks.priority.column"), //$NON-NLS-1$
151                 };
152
153                 fTodoTasksList.setTableColumns(new ListDialogField.ColumnsDescription(
154                                 columnsHeaders, true));
155                 unpackTodoTasks();
156                 if (fTodoTasksList.getSize() > 0) {
157                         fTodoTasksList.selectFirstElement();
158                 } else {
159                         fTodoTasksList.enableButton(IDX_EDIT, false);
160                 }
161
162                 fTaskTagsStatus = new StatusInfo();
163         }
164
165         private final static String[] getKeys() {
166                 return new String[] { PREF_COMPILER_TASK_TAGS,
167                                 PREF_COMPILER_TASK_PRIORITIES };
168         }
169
170         public class TaskTagAdapter implements IListAdapter, IDialogFieldListener {
171
172                 private boolean canEdit(ListDialogField field) {
173                         return field.getSelectedElements().size() == 1;
174                 }
175
176                 public void customButtonPressed(ListDialogField field, int index) {
177                         doTodoButtonPressed(index);
178                 }
179
180                 public void selectionChanged(ListDialogField field) {
181                         field.enableButton(IDX_EDIT, canEdit(field));
182                 }
183
184                 public void doubleClicked(ListDialogField field) {
185                         if (canEdit(field)) {
186                                 doTodoButtonPressed(IDX_EDIT);
187                         }
188                 }
189
190                 public void dialogFieldChanged(DialogField field) {
191                         validateSettings(PREF_COMPILER_TASK_TAGS, null);
192                 }
193
194         }
195
196         protected Control createContents(Composite parent) {
197                 setShell(parent.getShell());
198
199                 Composite markersComposite = createMarkersTabContent(parent);
200
201                 validateSettings(null, null);
202
203                 return markersComposite;
204         }
205
206         private Composite createMarkersTabContent(Composite folder) {
207
208                 GridLayout layout = new GridLayout();
209                 layout.marginHeight = 0;
210                 layout.marginWidth = 0;
211                 layout.numColumns = 2;
212
213                 Composite markersComposite = new Composite(folder, SWT.NULL);
214                 markersComposite.setLayout(layout);
215
216                 fTodoTasksList.doFillIntoGrid(markersComposite, 3);
217                 LayoutUtil.setHorizontalSpan(fTodoTasksList.getLabelControl(null), 2);
218
219                 GridData data = (GridData) fTodoTasksList.getListControl(null)
220                                 .getLayoutData();
221                 data.grabExcessHorizontalSpace = true;
222                 data.grabExcessVerticalSpace = true;
223                 data.verticalAlignment = GridData.FILL;
224                 // data.heightHint= SWTUtil.getTableHeightHint(table, 6);
225
226                 return markersComposite;
227         }
228
229         protected void validateSettings(String changedKey, String newValue) {
230                 if (changedKey != null) {
231                         if (PREF_COMPILER_TASK_TAGS.equals(changedKey)) {
232                                 fTaskTagsStatus = validateTaskTags();
233                         } else {
234                                 return;
235                         }
236                 } else {
237                         fTaskTagsStatus = validateTaskTags();
238                 }
239                 IStatus status = fTaskTagsStatus; // StatusUtil.getMostSevere(new
240                                                                                         // IStatus[] { fTaskTagsStatus });
241                 fContext.statusChanged(status);
242         }
243
244         private IStatus validateTaskTags() {
245                 return new StatusInfo();
246         }
247
248         /*
249          * (non-Javadoc)
250          * 
251          * @see net.sourceforge.phpdt.internal.ui.preferences.OptionsConfigurationBlock#performOk(boolean)
252          */
253         public boolean performOk(boolean enabled) {
254                 packTodoTasks();
255                 return super.performOk(enabled);
256         }
257
258         protected String[] getFullBuildDialogStrings(boolean workspaceSettings) {
259                 String title = PreferencesMessages
260                                 .getString("TodoTaskConfigurationBlock.needsbuild.title"); //$NON-NLS-1$
261                 String message;
262                 if (fProject == null) {
263                         message = PreferencesMessages
264                                         .getString("TodoTaskConfigurationBlock.needsfullbuild.message"); //$NON-NLS-1$
265                 } else {
266                         message = PreferencesMessages
267                                         .getString("TodoTaskConfigurationBlock.needsprojectbuild.message"); //$NON-NLS-1$
268                 }
269                 return new String[] { title, message };
270         }
271
272         /*
273          * (non-Javadoc)
274          * 
275          * @see net.sourceforge.phpdt.internal.ui.preferences.OptionsConfigurationBlock#updateControls()
276          */
277         protected void updateControls() {
278                 unpackTodoTasks();
279         }
280
281         private void unpackTodoTasks() {
282                 String currTags = (String) fWorkingValues.get(PREF_COMPILER_TASK_TAGS);
283                 String currPrios = (String) fWorkingValues
284                                 .get(PREF_COMPILER_TASK_PRIORITIES);
285                 String[] tags = getTokens(currTags, ","); //$NON-NLS-1$
286                 String[] prios = getTokens(currPrios, ","); //$NON-NLS-1$
287                 ArrayList elements = new ArrayList(tags.length);
288                 for (int i = 0; i < tags.length; i++) {
289                         TodoTask task = new TodoTask();
290                         task.name = tags[i].trim();
291                         task.priority = (i < prios.length) ? prios[i] : PRIORITY_NORMAL;
292                         elements.add(task);
293                 }
294                 fTodoTasksList.setElements(elements);
295         }
296
297         private void packTodoTasks() {
298                 StringBuffer tags = new StringBuffer();
299                 StringBuffer prios = new StringBuffer();
300                 List list = fTodoTasksList.getElements();
301                 for (int i = 0; i < list.size(); i++) {
302                         if (i > 0) {
303                                 tags.append(',');
304                                 prios.append(',');
305                         }
306                         TodoTask elem = (TodoTask) list.get(i);
307                         tags.append(elem.name);
308                         prios.append(elem.priority);
309                 }
310                 fWorkingValues.put(PREF_COMPILER_TASK_TAGS, tags.toString());
311                 fWorkingValues.put(PREF_COMPILER_TASK_PRIORITIES, prios.toString());
312         }
313
314         private void doTodoButtonPressed(int index) {
315                 TodoTask edited = null;
316                 if (index != IDX_ADD) {
317                         edited = (TodoTask) fTodoTasksList.getSelectedElements().get(0);
318                 }
319
320                 TodoTaskInputDialog dialog = new TodoTaskInputDialog(getShell(),
321                                 edited, fTodoTasksList.getElements());
322                 if (dialog.open() == Window.OK) {
323                         if (edited != null) {
324                                 fTodoTasksList.replaceElement(edited, dialog.getResult());
325                         } else {
326                                 fTodoTasksList.addElement(dialog.getResult());
327                         }
328                 }
329         }
330
331 }