1 11 package org.eclipse.jdt.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.core.resources.IProject; 19 20 import org.eclipse.swt.SWT; 21 import org.eclipse.swt.graphics.Font; 22 import org.eclipse.swt.graphics.Image; 23 import org.eclipse.swt.layout.GridData; 24 import org.eclipse.swt.layout.GridLayout; 25 import org.eclipse.swt.widgets.Composite; 26 import org.eclipse.swt.widgets.Control; 27 28 import org.eclipse.jface.resource.JFaceResources; 29 import org.eclipse.jface.viewers.IFontProvider; 30 import org.eclipse.jface.viewers.ITableLabelProvider; 31 import org.eclipse.jface.viewers.LabelProvider; 32 import org.eclipse.jface.viewers.Viewer; 33 import org.eclipse.jface.viewers.ViewerComparator; 34 import org.eclipse.jface.window.Window; 35 36 import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer; 37 38 import org.eclipse.jdt.core.JavaCore; 39 40 import org.eclipse.jdt.internal.corext.util.Messages; 41 42 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; 43 import org.eclipse.jdt.internal.ui.util.PixelConverter; 44 import org.eclipse.jdt.internal.ui.wizards.IStatusChangeListener; 45 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField; 46 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener; 47 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IListAdapter; 48 import org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField; 49 import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField; 50 51 53 public class TodoTaskConfigurationBlock extends OptionsConfigurationBlock { 54 55 private static final Key PREF_COMPILER_TASK_TAGS= getJDTCoreKey(JavaCore.COMPILER_TASK_TAGS); 56 private static final Key PREF_COMPILER_TASK_PRIORITIES= getJDTCoreKey(JavaCore.COMPILER_TASK_PRIORITIES); 57 58 private static final Key PREF_COMPILER_TASK_CASE_SENSITIVE= getJDTCoreKey(JavaCore.COMPILER_TASK_CASE_SENSITIVE); 59 60 private static final String PRIORITY_HIGH= JavaCore.COMPILER_TASK_PRIORITY_HIGH; 61 private static final String PRIORITY_NORMAL= JavaCore.COMPILER_TASK_PRIORITY_NORMAL; 62 private static final String PRIORITY_LOW= JavaCore.COMPILER_TASK_PRIORITY_LOW; 63 64 private static final String ENABLED= JavaCore.ENABLED; 65 private static final String DISABLED= JavaCore.DISABLED; 66 67 public static class TodoTask { 68 public String name; 69 public String priority; 70 } 71 72 private class TodoTaskLabelProvider extends LabelProvider implements ITableLabelProvider, IFontProvider { 73 74 public TodoTaskLabelProvider() { 75 } 76 77 80 public Image getImage(Object element) { 81 return null; } 83 84 87 public String getText(Object element) { 88 return getColumnText(element, 0); 89 } 90 91 94 public Image getColumnImage(Object element, int columnIndex) { 95 return null; 96 } 97 100 public String getColumnText(Object element, int columnIndex) { 101 TodoTask task= (TodoTask) element; 102 if (columnIndex == 0) { 103 String name= task.name; 104 if (isDefaultTask(task)) { 105 name=Messages.format(PreferencesMessages.TodoTaskConfigurationBlock_tasks_default, name); 106 } 107 return name; 108 } else { 109 if (PRIORITY_HIGH.equals(task.priority)) { 110 return PreferencesMessages.TodoTaskConfigurationBlock_markers_tasks_high_priority; 111 } else if (PRIORITY_NORMAL.equals(task.priority)) { 112 return PreferencesMessages.TodoTaskConfigurationBlock_markers_tasks_normal_priority; 113 } else if (PRIORITY_LOW.equals(task.priority)) { 114 return PreferencesMessages.TodoTaskConfigurationBlock_markers_tasks_low_priority; 115 } 116 return ""; } 118 } 119 120 123 public Font getFont(Object element) { 124 if (isDefaultTask((TodoTask) element)) { 125 return JFaceResources.getFontRegistry().getBold(JFaceResources.DIALOG_FONT); 126 } 127 return null; 128 } 129 } 130 131 private static class TodoTaskSorter extends ViewerComparator { 132 public int compare(Viewer viewer, Object e1, Object e2) { 133 return getComparator().compare(((TodoTask) e1).name, ((TodoTask) e2).name); 134 } 135 } 136 137 private static final int IDX_ADD= 0; 138 private static final int IDX_EDIT= 1; 139 private static final int IDX_REMOVE= 2; 140 private static final int IDX_DEFAULT= 4; 141 142 private IStatus fTaskTagsStatus; 143 private ListDialogField fTodoTasksList; 144 private SelectionButtonDialogField fCaseSensitiveCheckBox; 145 146 147 public TodoTaskConfigurationBlock(IStatusChangeListener context, IProject project, IWorkbenchPreferenceContainer container) { 148 super(context, project, getKeys(), container); 149 150 TaskTagAdapter adapter= new TaskTagAdapter(); 151 String [] buttons= new String [] { 152 PreferencesMessages.TodoTaskConfigurationBlock_markers_tasks_add_button, 153 PreferencesMessages.TodoTaskConfigurationBlock_markers_tasks_edit_button, 154 PreferencesMessages.TodoTaskConfigurationBlock_markers_tasks_remove_button, 155 null, 156 PreferencesMessages.TodoTaskConfigurationBlock_markers_tasks_setdefault_button, 157 }; 158 fTodoTasksList= new ListDialogField(adapter, buttons, new TodoTaskLabelProvider()); 159 fTodoTasksList.setDialogFieldListener(adapter); 160 fTodoTasksList.setRemoveButtonIndex(IDX_REMOVE); 161 162 String [] columnsHeaders= new String [] { 163 PreferencesMessages.TodoTaskConfigurationBlock_markers_tasks_name_column, 164 PreferencesMessages.TodoTaskConfigurationBlock_markers_tasks_priority_column, 165 }; 166 167 fTodoTasksList.setTableColumns(new ListDialogField.ColumnsDescription(columnsHeaders, true)); 168 fTodoTasksList.setViewerComparator(new TodoTaskSorter()); 169 170 171 fCaseSensitiveCheckBox= new SelectionButtonDialogField(SWT.CHECK); 172 fCaseSensitiveCheckBox.setLabelText(PreferencesMessages.TodoTaskConfigurationBlock_casesensitive_label); 173 fCaseSensitiveCheckBox.setDialogFieldListener(adapter); 174 175 unpackTodoTasks(); 176 if (fTodoTasksList.getSize() > 0) { 177 fTodoTasksList.selectFirstElement(); 178 } else { 179 fTodoTasksList.enableButton(IDX_EDIT, false); 180 fTodoTasksList.enableButton(IDX_DEFAULT, false); 181 } 182 183 fTaskTagsStatus= new StatusInfo(); 184 } 185 186 public void setEnabled(boolean isEnabled) { 187 fTodoTasksList.setEnabled(isEnabled); 188 fCaseSensitiveCheckBox.setEnabled(isEnabled); 189 } 190 191 final boolean isDefaultTask(TodoTask task) { 192 return fTodoTasksList.getIndexOfElement(task) == 0; 193 } 194 195 private void setToDefaultTask(TodoTask task) { 196 List elements= fTodoTasksList.getElements(); 197 elements.remove(task); 198 elements.add(0, task); 199 fTodoTasksList.setElements(elements); 200 fTodoTasksList.enableButton(IDX_DEFAULT, false); 201 } 202 203 private static Key[] getKeys() { 204 return new Key[] { 205 PREF_COMPILER_TASK_TAGS, PREF_COMPILER_TASK_PRIORITIES, PREF_COMPILER_TASK_CASE_SENSITIVE 206 }; 207 } 208 209 public class TaskTagAdapter implements IListAdapter, IDialogFieldListener { 210 211 private boolean canEdit(List selectedElements) { 212 return selectedElements.size() == 1; 213 } 214 215 private boolean canSetToDefault(List selectedElements) { 216 return selectedElements.size() == 1 && !isDefaultTask((TodoTask) selectedElements.get(0)); 217 } 218 219 public void customButtonPressed(ListDialogField field, int index) { 220 doTodoButtonPressed(index); 221 } 222 223 public void selectionChanged(ListDialogField field) { 224 List selectedElements= field.getSelectedElements(); 225 field.enableButton(IDX_EDIT, canEdit(selectedElements)); 226 field.enableButton(IDX_DEFAULT, canSetToDefault(selectedElements)); 227 } 228 229 public void doubleClicked(ListDialogField field) { 230 if (canEdit(field.getSelectedElements())) { 231 doTodoButtonPressed(IDX_EDIT); 232 } 233 } 234 235 public void dialogFieldChanged(DialogField field) { 236 updateModel(field); 237 } 238 239 } 240 241 protected Control createContents(Composite parent) { 242 setShell(parent.getShell()); 243 244 Composite markersComposite= createMarkersTabContent(parent); 245 246 validateSettings(null, null, null); 247 248 return markersComposite; 249 } 250 251 private Composite createMarkersTabContent(Composite folder) { 252 GridLayout layout= new GridLayout(); 253 layout.marginHeight= 0; 254 layout.marginWidth= 0; 255 layout.numColumns= 2; 256 257 PixelConverter conv= new PixelConverter(folder); 258 259 Composite markersComposite= new Composite(folder, SWT.NULL); 260 markersComposite.setLayout(layout); 261 markersComposite.setFont(folder.getFont()); 262 263 GridData data= new GridData(GridData.FILL_BOTH); 264 data.widthHint= conv.convertWidthInCharsToPixels(50); 265 Control listControl= fTodoTasksList.getListControl(markersComposite); 266 listControl.setLayoutData(data); 267 268 Control buttonsControl= fTodoTasksList.getButtonBox(markersComposite); 269 buttonsControl.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_BEGINNING)); 270 271 fCaseSensitiveCheckBox.doFillIntoGrid(markersComposite, 2); 272 273 return markersComposite; 274 } 275 276 protected void validateSettings(Key changedKey, String oldValue, String newValue) { 277 if (!areSettingsEnabled()) { 278 return; 279 } 280 281 if (changedKey != null) { 282 if (PREF_COMPILER_TASK_TAGS.equals(changedKey)) { 283 fTaskTagsStatus= validateTaskTags(); 284 } else { 285 return; 286 } 287 } else { 288 fTaskTagsStatus= validateTaskTags(); 289 } 290 IStatus status= fTaskTagsStatus; fContext.statusChanged(status); 292 } 293 294 private IStatus validateTaskTags() { 295 return new StatusInfo(); 296 } 297 298 protected final void updateModel(DialogField field) { 299 if (field == fTodoTasksList) { 300 StringBuffer tags= new StringBuffer (); 301 StringBuffer prios= new StringBuffer (); 302 List list= fTodoTasksList.getElements(); 303 for (int i= 0; i < list.size(); i++) { 304 if (i > 0) { 305 tags.append(','); 306 prios.append(','); 307 } 308 TodoTask elem= (TodoTask) list.get(i); 309 tags.append(elem.name); 310 prios.append(elem.priority); 311 } 312 setValue(PREF_COMPILER_TASK_TAGS, tags.toString()); 313 setValue(PREF_COMPILER_TASK_PRIORITIES, prios.toString()); 314 validateSettings(PREF_COMPILER_TASK_TAGS, null, null); 315 } else if (field == fCaseSensitiveCheckBox) { 316 String state= fCaseSensitiveCheckBox.isSelected() ? ENABLED : DISABLED; 317 setValue(PREF_COMPILER_TASK_CASE_SENSITIVE, state); 318 } 319 } 320 321 protected String [] getFullBuildDialogStrings(boolean workspaceSettings) { 322 String title= PreferencesMessages.TodoTaskConfigurationBlock_needsbuild_title; 323 String message; 324 if (fProject == null) { 325 message= PreferencesMessages.TodoTaskConfigurationBlock_needsfullbuild_message; 326 } else { 327 message= PreferencesMessages.TodoTaskConfigurationBlock_needsprojectbuild_message; 328 } 329 return new String [] { title, message }; 330 } 331 332 335 protected void updateControls() { 336 unpackTodoTasks(); 337 } 338 339 private void unpackTodoTasks() { 340 String currTags= getValue(PREF_COMPILER_TASK_TAGS); 341 String currPrios= getValue(PREF_COMPILER_TASK_PRIORITIES); 342 String [] tags= getTokens(currTags, ","); String [] prios= getTokens(currPrios, ","); ArrayList elements= new ArrayList (tags.length); 345 for (int i= 0; i < tags.length; i++) { 346 TodoTask task= new TodoTask(); 347 task.name= tags[i].trim(); 348 task.priority= (i < prios.length) ? prios[i] : PRIORITY_NORMAL; 349 elements.add(task); 350 } 351 fTodoTasksList.setElements(elements); 352 353 boolean isCaseSensitive= checkValue(PREF_COMPILER_TASK_CASE_SENSITIVE, ENABLED); 354 fCaseSensitiveCheckBox.setSelection(isCaseSensitive); 355 } 356 357 private void doTodoButtonPressed(int index) { 358 TodoTask edited= null; 359 if (index != IDX_ADD) { 360 edited= (TodoTask) fTodoTasksList.getSelectedElements().get(0); 361 } 362 if (index == IDX_ADD || index == IDX_EDIT) { 363 TodoTaskInputDialog dialog= new TodoTaskInputDialog(getShell(), edited, fTodoTasksList.getElements()); 364 if (dialog.open() == Window.OK) { 365 if (edited != null) { 366 fTodoTasksList.replaceElement(edited, dialog.getResult()); 367 } else { 368 fTodoTasksList.addElement(dialog.getResult()); 369 } 370 } 371 } else if (index == IDX_DEFAULT) { 372 setToDefaultTask(edited); 373 } 374 } 375 376 } 377 | Popular Tags |