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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.preferences;
13 import java.util.ArrayList;
14 import java.util.List;
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;
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;
39 public class TodoTaskConfigurationBlock extends OptionsConfigurationBlock {
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;
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;
48 public static class TodoTask {
50 public String priority;
53 private static class TodoTaskLabelProvider extends LabelProvider implements ITableLabelProvider {
56 * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
58 public Image getImage(Object element) {
59 return null; // JavaPluginImages.get(JavaPluginImages.IMG_OBJS_REFACTORING_INFO);
63 * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
65 public String getText(Object element) {
66 return getColumnText(element, 0);
70 * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, int)
72 public Image getColumnImage(Object element, int columnIndex) {
76 * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)
78 public String getColumnText(Object element, int columnIndex) {
79 TodoTask task= (TodoTask) element;
80 if (columnIndex == 0) {
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$
90 return ""; //$NON-NLS-1$
96 private static final int IDX_ADD= 0;
97 private static final int IDX_EDIT= 1;
98 private static final int IDX_REMOVE= 2;
100 private IStatus fTaskTagsStatus;
101 private ListDialogField fTodoTasksList;
103 public TodoTaskConfigurationBlock(IStatusChangeListener context, IJavaProject project) {
104 super(context, project, getKeys());
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$
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);
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$
123 fTodoTasksList.setTableColumns(new ListDialogField.ColumnsDescription(columnsHeaders, true));
125 if (fTodoTasksList.getSize() > 0) {
126 fTodoTasksList.selectFirstElement();
128 fTodoTasksList.enableButton(IDX_EDIT, false);
131 fTaskTagsStatus= new StatusInfo();
134 private final static String[] getKeys() {
135 return new String[] {
136 PREF_COMPILER_TASK_TAGS, PREF_COMPILER_TASK_PRIORITIES
140 public class TaskTagAdapter implements IListAdapter, IDialogFieldListener {
142 private boolean canEdit(ListDialogField field) {
143 return field.getSelectedElements().size() == 1;
146 public void customButtonPressed(ListDialogField field, int index) {
147 doTodoButtonPressed(index);
150 public void selectionChanged(ListDialogField field) {
151 field.enableButton(IDX_EDIT, canEdit(field));
154 public void doubleClicked(ListDialogField field) {
155 if (canEdit(field)) {
156 doTodoButtonPressed(IDX_EDIT);
160 public void dialogFieldChanged(DialogField field) {
161 validateSettings(PREF_COMPILER_TASK_TAGS, null);
166 protected Control createContents(Composite parent) {
167 setShell(parent.getShell());
169 Composite markersComposite= createMarkersTabContent(parent);
171 validateSettings(null, null);
173 return markersComposite;
176 private Composite createMarkersTabContent(Composite folder) {
178 GridLayout layout= new GridLayout();
179 layout.marginHeight= 0;
180 layout.marginWidth= 0;
181 layout.numColumns= 2;
183 Composite markersComposite= new Composite(folder, SWT.NULL);
184 markersComposite.setLayout(layout);
186 fTodoTasksList.doFillIntoGrid(markersComposite, 3);
187 LayoutUtil.setHorizontalSpan(fTodoTasksList.getLabelControl(null), 2);
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);
195 return markersComposite;
198 protected void validateSettings(String changedKey, String newValue) {
199 if (changedKey != null) {
200 if (PREF_COMPILER_TASK_TAGS.equals(changedKey)) {
201 fTaskTagsStatus= validateTaskTags();
206 fTaskTagsStatus= validateTaskTags();
208 IStatus status= fTaskTagsStatus; //StatusUtil.getMostSevere(new IStatus[] { fTaskTagsStatus });
209 fContext.statusChanged(status);
212 private IStatus validateTaskTags() {
213 return new StatusInfo();
217 * @see net.sourceforge.phpdt.internal.ui.preferences.OptionsConfigurationBlock#performOk(boolean)
219 public boolean performOk(boolean enabled) {
221 return super.performOk(enabled);
225 protected String[] getFullBuildDialogStrings(boolean workspaceSettings) {
226 String title= PreferencesMessages.getString("TodoTaskConfigurationBlock.needsbuild.title"); //$NON-NLS-1$
228 if (fProject == null) {
229 message= PreferencesMessages.getString("TodoTaskConfigurationBlock.needsfullbuild.message"); //$NON-NLS-1$
231 message= PreferencesMessages.getString("TodoTaskConfigurationBlock.needsprojectbuild.message"); //$NON-NLS-1$
233 return new String[] { title, message };
237 * @see net.sourceforge.phpdt.internal.ui.preferences.OptionsConfigurationBlock#updateControls()
239 protected void updateControls() {
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;
255 fTodoTasksList.setElements(elements);
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++) {
267 TodoTask elem= (TodoTask) list.get(i);
268 tags.append(elem.name);
269 prios.append(elem.priority);
271 fWorkingValues.put(PREF_COMPILER_TASK_TAGS, tags.toString());
272 fWorkingValues.put(PREF_COMPILER_TASK_PRIORITIES, prios.toString());
275 private void doTodoButtonPressed(int index) {
276 TodoTask edited= null;
277 if (index != IDX_ADD) {
278 edited= (TodoTask) fTodoTasksList.getSelectedElements().get(0);
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());
286 fTodoTasksList.addElement(dialog.getResult());