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