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