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;
43 private static final String PREF_COMPILER_TASK_PRIORITIES = JavaCore.COMPILER_TASK_PRIORITIES;
45 private static final String PRIORITY_HIGH = JavaCore.COMPILER_TASK_PRIORITY_HIGH;
47 private static final String PRIORITY_NORMAL = JavaCore.COMPILER_TASK_PRIORITY_NORMAL;
49 private static final String PRIORITY_LOW = JavaCore.COMPILER_TASK_PRIORITY_LOW;
51 public static class TodoTask {
54 public String priority;
57 private static class TodoTaskLabelProvider extends LabelProvider implements
63 * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
65 public Image getImage(Object element) {
66 return null; // JavaPluginImages.get(JavaPluginImages.IMG_OBJS_REFACTORING_INFO);
72 * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
74 public String getText(Object element) {
75 return getColumnText(element, 0);
81 * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object,
84 public Image getColumnImage(Object element, int columnIndex) {
91 * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object,
94 public String getColumnText(Object element, int columnIndex) {
95 TodoTask task = (TodoTask) element;
96 if (columnIndex == 0) {
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$
109 return ""; //$NON-NLS-1$
115 private static final int IDX_ADD = 0;
117 private static final int IDX_EDIT = 1;
119 private static final int IDX_REMOVE = 2;
121 private IStatus fTaskTagsStatus;
123 private ListDialogField fTodoTasksList;
125 public TodoTaskConfigurationBlock(IStatusChangeListener context,
126 IJavaProject project) {
127 super(context, project, getKeys());
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$
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);
146 String[] columnsHeaders = new String[] {
148 .getString("TodoTaskConfigurationBlock.markers.tasks.name.column"), //$NON-NLS-1$
150 .getString("TodoTaskConfigurationBlock.markers.tasks.priority.column"), //$NON-NLS-1$
153 fTodoTasksList.setTableColumns(new ListDialogField.ColumnsDescription(
154 columnsHeaders, true));
156 if (fTodoTasksList.getSize() > 0) {
157 fTodoTasksList.selectFirstElement();
159 fTodoTasksList.enableButton(IDX_EDIT, false);
162 fTaskTagsStatus = new StatusInfo();
165 private final static String[] getKeys() {
166 return new String[] { PREF_COMPILER_TASK_TAGS,
167 PREF_COMPILER_TASK_PRIORITIES };
170 public class TaskTagAdapter implements IListAdapter, IDialogFieldListener {
172 private boolean canEdit(ListDialogField field) {
173 return field.getSelectedElements().size() == 1;
176 public void customButtonPressed(ListDialogField field, int index) {
177 doTodoButtonPressed(index);
180 public void selectionChanged(ListDialogField field) {
181 field.enableButton(IDX_EDIT, canEdit(field));
184 public void doubleClicked(ListDialogField field) {
185 if (canEdit(field)) {
186 doTodoButtonPressed(IDX_EDIT);
190 public void dialogFieldChanged(DialogField field) {
191 validateSettings(PREF_COMPILER_TASK_TAGS, null);
196 protected Control createContents(Composite parent) {
197 setShell(parent.getShell());
199 Composite markersComposite = createMarkersTabContent(parent);
201 validateSettings(null, null);
203 return markersComposite;
206 private Composite createMarkersTabContent(Composite folder) {
208 GridLayout layout = new GridLayout();
209 layout.marginHeight = 0;
210 layout.marginWidth = 0;
211 layout.numColumns = 2;
213 Composite markersComposite = new Composite(folder, SWT.NULL);
214 markersComposite.setLayout(layout);
216 fTodoTasksList.doFillIntoGrid(markersComposite, 3);
217 LayoutUtil.setHorizontalSpan(fTodoTasksList.getLabelControl(null), 2);
219 GridData data = (GridData) fTodoTasksList.getListControl(null)
221 data.grabExcessHorizontalSpace = true;
222 data.grabExcessVerticalSpace = true;
223 data.verticalAlignment = GridData.FILL;
224 // data.heightHint= SWTUtil.getTableHeightHint(table, 6);
226 return markersComposite;
229 protected void validateSettings(String changedKey, String newValue) {
230 if (changedKey != null) {
231 if (PREF_COMPILER_TASK_TAGS.equals(changedKey)) {
232 fTaskTagsStatus = validateTaskTags();
237 fTaskTagsStatus = validateTaskTags();
239 IStatus status = fTaskTagsStatus; // StatusUtil.getMostSevere(new
240 // IStatus[] { fTaskTagsStatus });
241 fContext.statusChanged(status);
244 private IStatus validateTaskTags() {
245 return new StatusInfo();
251 * @see net.sourceforge.phpdt.internal.ui.preferences.OptionsConfigurationBlock#performOk(boolean)
253 public boolean performOk(boolean enabled) {
255 return super.performOk(enabled);
258 protected String[] getFullBuildDialogStrings(boolean workspaceSettings) {
259 String title = PreferencesMessages
260 .getString("TodoTaskConfigurationBlock.needsbuild.title"); //$NON-NLS-1$
262 if (fProject == null) {
263 message = PreferencesMessages
264 .getString("TodoTaskConfigurationBlock.needsfullbuild.message"); //$NON-NLS-1$
266 message = PreferencesMessages
267 .getString("TodoTaskConfigurationBlock.needsprojectbuild.message"); //$NON-NLS-1$
269 return new String[] { title, message };
275 * @see net.sourceforge.phpdt.internal.ui.preferences.OptionsConfigurationBlock#updateControls()
277 protected void updateControls() {
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;
294 fTodoTasksList.setElements(elements);
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++) {
306 TodoTask elem = (TodoTask) list.get(i);
307 tags.append(elem.name);
308 prios.append(elem.priority);
310 fWorkingValues.put(PREF_COMPILER_TASK_TAGS, tags.toString());
311 fWorkingValues.put(PREF_COMPILER_TASK_PRIORITIES, prios.toString());
314 private void doTodoButtonPressed(int index) {
315 TodoTask edited = null;
316 if (index != IDX_ADD) {
317 edited = (TodoTask) fTodoTasksList.getSelectedElements().get(0);
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());
326 fTodoTasksList.addElement(dialog.getResult());