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 org.eclipse.core.runtime.IStatus;
 
  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;
 
  25 import org.eclipse.jface.viewers.ITableLabelProvider;
 
  26 import org.eclipse.jface.viewers.LabelProvider;
 
  27 import org.eclipse.jface.window.Window;
 
  29 import net.sourceforge.phpdt.core.IJavaProject;
 
  30 import net.sourceforge.phpdt.core.JavaCore;
 
  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;
 
  42 public class TodoTaskConfigurationBlock extends OptionsConfigurationBlock {
 
  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;
 
  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;          
 
  51         public static class TodoTask {
 
  53                 public String priority;
 
  56         private static class TodoTaskLabelProvider extends LabelProvider implements ITableLabelProvider {
 
  59                  * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
 
  61                 public Image getImage(Object element) {
 
  62                         return null; // JavaPluginImages.get(JavaPluginImages.IMG_OBJS_REFACTORING_INFO);
 
  66                  * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
 
  68                 public String getText(Object element) {
 
  69                         return getColumnText(element, 0);
 
  73                  * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, int)
 
  75                 public Image getColumnImage(Object element, int columnIndex) {
 
  79                  * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)
 
  81                 public String getColumnText(Object element, int columnIndex) {
 
  82                         TodoTask task= (TodoTask) element;
 
  83                         if (columnIndex == 0) {
 
  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$
 
  93                                 return ""; //$NON-NLS-1$
 
  99         private static final int IDX_ADD= 0;
 
 100         private static final int IDX_EDIT= 1;
 
 101         private static final int IDX_REMOVE= 2;
 
 103         private IStatus fTaskTagsStatus;
 
 104         private ListDialogField fTodoTasksList;
 
 106         public TodoTaskConfigurationBlock(IStatusChangeListener context, IJavaProject project) {
 
 107                 super(context, project, getKeys());
 
 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$
 
 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);
 
 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$
 
 126                 fTodoTasksList.setTableColumns(new ListDialogField.ColumnsDescription(columnsHeaders, true));
 
 128                 if (fTodoTasksList.getSize() > 0) {
 
 129                         fTodoTasksList.selectFirstElement();
 
 131                         fTodoTasksList.enableButton(IDX_EDIT, false);
 
 134                 fTaskTagsStatus= new StatusInfo();              
 
 137         private final static String[] getKeys() {
 
 138                 return new String[] {
 
 139                         PREF_COMPILER_TASK_TAGS, PREF_COMPILER_TASK_PRIORITIES
 
 143         public class TaskTagAdapter implements IListAdapter, IDialogFieldListener {
 
 145                 private boolean canEdit(ListDialogField field) {
 
 146                         return field.getSelectedElements().size() == 1;
 
 149                 public void customButtonPressed(ListDialogField field, int index) {
 
 150                         doTodoButtonPressed(index);
 
 153                 public void selectionChanged(ListDialogField field) {
 
 154                         field.enableButton(IDX_EDIT, canEdit(field));
 
 157                 public void doubleClicked(ListDialogField field) {
 
 158                         if (canEdit(field)) {
 
 159                                 doTodoButtonPressed(IDX_EDIT);
 
 163                 public void dialogFieldChanged(DialogField field) {
 
 164                         validateSettings(PREF_COMPILER_TASK_TAGS, null);
 
 169         protected Control createContents(Composite parent) {
 
 170                 setShell(parent.getShell());
 
 172                 Composite markersComposite= createMarkersTabContent(parent);
 
 174                 validateSettings(null, null);
 
 176                 return markersComposite;
 
 179         private Composite createMarkersTabContent(Composite folder) {
 
 181                 GridLayout layout= new GridLayout();
 
 182                 layout.marginHeight= 0;
 
 183                 layout.marginWidth= 0;
 
 184                 layout.numColumns= 2;
 
 186                 Composite markersComposite= new Composite(folder, SWT.NULL);
 
 187                 markersComposite.setLayout(layout);
 
 189                 fTodoTasksList.doFillIntoGrid(markersComposite, 3);
 
 190                 LayoutUtil.setHorizontalSpan(fTodoTasksList.getLabelControl(null), 2);
 
 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);
 
 198                 return markersComposite;
 
 201         protected void validateSettings(String changedKey, String newValue) {
 
 202                 if (changedKey != null) {
 
 203                         if (PREF_COMPILER_TASK_TAGS.equals(changedKey)) {
 
 204                                 fTaskTagsStatus= validateTaskTags();
 
 209                         fTaskTagsStatus= validateTaskTags();
 
 211                 IStatus status= fTaskTagsStatus; //StatusUtil.getMostSevere(new IStatus[] { fTaskTagsStatus });
 
 212                 fContext.statusChanged(status);
 
 215         private IStatus validateTaskTags() {
 
 216                 return new StatusInfo();
 
 220          * @see org.eclipse.jdt.internal.ui.preferences.OptionsConfigurationBlock#performOk(boolean)
 
 222         public boolean performOk(boolean enabled) {
 
 224                 return super.performOk(enabled);
 
 228         protected String[] getFullBuildDialogStrings(boolean workspaceSettings) {
 
 229                 String title= PreferencesMessages.getString("TodoTaskConfigurationBlock.needsbuild.title"); //$NON-NLS-1$
 
 231                 if (fProject == null) {
 
 232                         message= PreferencesMessages.getString("TodoTaskConfigurationBlock.needsfullbuild.message"); //$NON-NLS-1$
 
 234                         message= PreferencesMessages.getString("TodoTaskConfigurationBlock.needsprojectbuild.message"); //$NON-NLS-1$
 
 236                 return new String[] { title, message };
 
 240          * @see org.eclipse.jdt.internal.ui.preferences.OptionsConfigurationBlock#updateControls()
 
 242         protected void updateControls() {
 
 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;
 
 258                 fTodoTasksList.setElements(elements);
 
 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++) {
 
 270                         TodoTask elem= (TodoTask) list.get(i);
 
 271                         tags.append(elem.name);
 
 272                         prios.append(elem.priority);
 
 274                 fWorkingValues.put(PREF_COMPILER_TASK_TAGS, tags.toString());
 
 275                 fWorkingValues.put(PREF_COMPILER_TASK_PRIORITIES, prios.toString());
 
 278         private void doTodoButtonPressed(int index) {
 
 279                 TodoTask edited= null;
 
 280                 if (index != IDX_ADD) {
 
 281                         edited= (TodoTask) fTodoTasksList.getSelectedElements().get(0);
 
 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());
 
 289                                 fTodoTasksList.addElement(dialog.getResult());