1 11 12 package org.eclipse.jdt.internal.ui.preferences; 13 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 19 import org.eclipse.core.runtime.Platform; 20 import org.eclipse.core.runtime.preferences.InstanceScope; 21 22 import org.eclipse.swt.SWT; 23 import org.eclipse.swt.events.SelectionEvent; 24 import org.eclipse.swt.events.SelectionListener; 25 import org.eclipse.swt.graphics.FontMetrics; 26 import org.eclipse.swt.graphics.GC; 27 import org.eclipse.swt.graphics.Image; 28 import org.eclipse.swt.graphics.RGB; 29 import org.eclipse.swt.layout.GridData; 30 import org.eclipse.swt.layout.GridLayout; 31 import org.eclipse.swt.widgets.Button; 32 import org.eclipse.swt.widgets.Composite; 33 import org.eclipse.swt.widgets.Control; 34 import org.eclipse.swt.widgets.Label; 35 36 import org.eclipse.jface.dialogs.Dialog; 37 import org.eclipse.jface.preference.ColorSelector; 38 import org.eclipse.jface.preference.PreferenceConverter; 39 import org.eclipse.jface.resource.JFaceResources; 40 import org.eclipse.jface.viewers.ArrayContentProvider; 41 import org.eclipse.jface.viewers.ComboViewer; 42 import org.eclipse.jface.viewers.ISelectionChangedListener; 43 import org.eclipse.jface.viewers.IStructuredContentProvider; 44 import org.eclipse.jface.viewers.IStructuredSelection; 45 import org.eclipse.jface.viewers.LabelProvider; 46 import org.eclipse.jface.viewers.SelectionChangedEvent; 47 import org.eclipse.jface.viewers.StructuredSelection; 48 import org.eclipse.jface.viewers.StructuredViewer; 49 import org.eclipse.jface.viewers.TableViewer; 50 import org.eclipse.jface.viewers.Viewer; 51 52 import org.eclipse.ui.texteditor.AnnotationPreference; 53 import org.eclipse.ui.texteditor.MarkerAnnotationPreferences; 54 55 import org.eclipse.ui.editors.text.EditorsUI; 56 57 import org.eclipse.jdt.internal.ui.JavaPlugin; 58 59 import org.osgi.service.prefs.BackingStoreException; 60 61 68 class LinkedModeConfigurationBlock extends AbstractConfigurationBlock { 69 70 private static final String EXIT= "org.eclipse.jdt.ui.link.exit"; private static final String TARGET= "org.eclipse.jdt.ui.link.target"; private static final String MASTER= "org.eclipse.jdt.ui.link.master"; private static final String SLAVE= "org.eclipse.jdt.ui.link.slave"; 75 private static final class ListItem { 76 final String label; 77 final Image image; 78 final String colorKey; 79 final String highlightKey; 80 final String overviewRulerKey; 81 final String textStyleKey; 82 final String textKey; 83 final String verticalRulerKey; 84 final List validStyles; 85 86 ListItem(String label, Image image, String colorKey, String textKey, String overviewRulerKey, String highlightKey, String verticalRulerKey, String textStyleKey, List validStyles) { 87 this.label= label; 88 this.image= image; 89 this.colorKey= colorKey; 90 this.highlightKey= highlightKey; 91 this.overviewRulerKey= overviewRulerKey; 92 this.textKey= textKey; 93 this.textStyleKey= textStyleKey; 94 this.verticalRulerKey= verticalRulerKey; 95 this.validStyles= validStyles; 96 } 97 } 98 99 private static final class ItemContentProvider implements IStructuredContentProvider { 100 101 public Object [] getElements(Object inputElement) { 102 return (ListItem[]) inputElement; 103 } 104 105 public void dispose() { 106 } 107 108 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 109 } 110 } 111 112 private final class ItemLabelProvider extends LabelProvider { 113 114 public String getText(Object element) { 115 return ((ListItem) element).label; 116 } 117 } 118 119 private static class ArrayLabelProvider extends LabelProvider { 120 public String getText(Object element) { 121 return ((String []) element)[0].toString(); 122 } 123 } 124 125 final static String [] HIGHLIGHT= new String [] {PreferencesMessages.LinkedModeConfigurationBlock_HIGHLIGHT, "unused"}; final static String [] UNDERLINE= new String [] {PreferencesMessages.LinkedModeConfigurationBlock_UNDERLINE, AnnotationPreference.STYLE_UNDERLINE}; 127 final static String [] BOX= new String [] {PreferencesMessages.LinkedModeConfigurationBlock_BOX, AnnotationPreference.STYLE_BOX}; 128 final static String [] IBEAM= new String [] {PreferencesMessages.LinkedModeConfigurationBlock_IBEAM, AnnotationPreference.STYLE_IBEAM}; 129 final static String [] SQUIGGLES= new String [] {PreferencesMessages.LinkedModeConfigurationBlock_SQUIGGLES, AnnotationPreference.STYLE_SQUIGGLES}; 130 131 private ColorSelector fAnnotationForegroundColorEditor; 132 133 private Button fShowInTextCheckBox; 134 135 private StructuredViewer fAnnotationTypeViewer; 136 private final ListItem[] fListModel; 137 138 private ComboViewer fDecorationViewer; 139 private FontMetrics fFontMetrics; 140 141 public LinkedModeConfigurationBlock(OverlayPreferenceStore store) { 142 super(new OverlayPreferenceStore(EditorsUI.getPreferenceStore(), new OverlayPreferenceStore.OverlayKey[0])); 143 final MarkerAnnotationPreferences prefs= new MarkerAnnotationPreferences(); 144 getPreferenceStore().addKeys(createOverlayStoreKeys(prefs)); 145 fListModel= createAnnotationTypeListModel(prefs); 146 } 147 148 private OverlayPreferenceStore.OverlayKey[] createOverlayStoreKeys(MarkerAnnotationPreferences preferences) { 149 ArrayList overlayKeys= new ArrayList (); 150 151 Iterator e= preferences.getAnnotationPreferences().iterator(); 152 while (e.hasNext()) { 153 AnnotationPreference info= (AnnotationPreference) e.next(); 154 155 if (isLinkedModeAnnotation(info)) { 156 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, info.getColorPreferenceKey())); 157 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, info.getTextPreferenceKey())); 158 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, info.getTextStylePreferenceKey())); 159 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, info.getHighlightPreferenceKey())); 160 } 161 } 162 163 OverlayPreferenceStore.OverlayKey[] keys= new OverlayPreferenceStore.OverlayKey[overlayKeys.size()]; 164 overlayKeys.toArray(keys); 165 return keys; 166 } 167 168 private boolean isLinkedModeAnnotation(AnnotationPreference info) { 169 final Object type= info.getAnnotationType(); 170 return type.equals(MASTER) 171 || (type.equals(SLAVE)) 172 || (type.equals(TARGET)) 173 || (type.equals(EXIT)); 174 } 175 176 private ListItem[] createAnnotationTypeListModel(MarkerAnnotationPreferences preferences) { 177 ArrayList listModelItems= new ArrayList (); 178 Iterator e= preferences.getAnnotationPreferences().iterator(); 179 180 while (e.hasNext()) { 181 AnnotationPreference info= (AnnotationPreference) e.next(); 182 if (isLinkedModeAnnotation(info)) { 183 String label= info.getPreferenceLabel(); 184 List styles= getStyles(info.getAnnotationType()); 185 listModelItems.add(new ListItem(label, null, info.getColorPreferenceKey(), info.getTextPreferenceKey(), info.getOverviewRulerPreferenceKey(), info.getHighlightPreferenceKey(), info.getVerticalRulerPreferenceKey(), info.getTextStylePreferenceKey(), styles)); 186 } 187 } 188 189 ListItem[] items= new ListItem[listModelItems.size()]; 190 listModelItems.toArray(items); 191 return items; 192 } 193 194 195 private List getStyles(Object type) { 196 if (type.equals(MASTER)) 197 return Arrays.asList(new String [][] {BOX, HIGHLIGHT, UNDERLINE, SQUIGGLES}); 198 if (type.equals(SLAVE)) 199 return Arrays.asList(new String [][] {BOX, HIGHLIGHT, UNDERLINE, SQUIGGLES}); 200 if (type.equals(TARGET)) 201 return Arrays.asList(new String [][] {BOX, HIGHLIGHT, UNDERLINE, SQUIGGLES}); 202 if (type.equals(EXIT)) 203 return Arrays.asList(new String [][] {IBEAM}); 204 return new ArrayList (); 205 } 206 207 213 public Control createControl(Composite parent) { 214 OverlayPreferenceStore store= getPreferenceStore(); 215 store.load(); 216 store.start(); 217 218 initializeDialogUnits(parent); 219 220 Composite composite= new Composite(parent, SWT.NONE); 221 GridLayout layout= new GridLayout(); 222 composite.setLayout(layout); 223 224 Label label= new Label(composite, SWT.LEFT); 225 label.setText(PreferencesMessages.LinkedModeConfigurationBlock_annotationPresentationOptions); 226 GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL); 227 label.setLayoutData(gd); 228 229 Composite editorComposite= new Composite(composite, SWT.NONE); 230 layout= new GridLayout(); 231 layout.numColumns= 2; 232 layout.marginHeight= 0; 233 layout.marginWidth= 0; 234 editorComposite.setLayout(layout); 235 gd= new GridData(SWT.FILL, SWT.FILL, true, true); 236 editorComposite.setLayoutData(gd); 237 238 fAnnotationTypeViewer= new TableViewer(editorComposite, SWT.SINGLE | SWT.V_SCROLL | SWT.BORDER); 239 fAnnotationTypeViewer.setLabelProvider(new ItemLabelProvider()); 240 fAnnotationTypeViewer.setContentProvider(new ItemContentProvider()); 241 gd= new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false); 242 gd.heightHint= convertHeightInCharsToPixels(5); 243 fAnnotationTypeViewer.getControl().setLayoutData(gd); 244 245 Composite optionsComposite= new Composite(editorComposite, SWT.NONE); 246 layout= new GridLayout(); 247 layout.marginHeight= 0; 248 layout.marginWidth= 0; 249 layout.numColumns= 2; 250 optionsComposite.setLayout(layout); 251 optionsComposite.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false)); 252 253 255 fShowInTextCheckBox= new Button(optionsComposite, SWT.CHECK); 256 fShowInTextCheckBox.setText(PreferencesMessages.LinkedModeConfigurationBlock_labels_showIn); 257 gd= new GridData(GridData.FILL_HORIZONTAL); 258 gd.horizontalAlignment= GridData.BEGINNING; 259 fShowInTextCheckBox.setLayoutData(gd); 260 261 fDecorationViewer= new ComboViewer(optionsComposite, SWT.READ_ONLY); 262 fDecorationViewer.setContentProvider(new ArrayContentProvider()); 263 fDecorationViewer.setLabelProvider(new ArrayLabelProvider()); 264 gd= new GridData(GridData.FILL_HORIZONTAL); 265 gd.horizontalAlignment= GridData.BEGINNING; 266 fDecorationViewer.getControl().setLayoutData(gd); 267 fDecorationViewer.setInput(new Object [] {HIGHLIGHT, SQUIGGLES, BOX, UNDERLINE, IBEAM}); 268 269 label= new Label(optionsComposite, SWT.LEFT); 270 label.setText(PreferencesMessages.LinkedModeConfigurationBlock_color); 271 gd= new GridData(); 272 gd.horizontalAlignment= GridData.BEGINNING; 273 label.setLayoutData(gd); 274 275 fAnnotationForegroundColorEditor= new ColorSelector(optionsComposite); 276 Button foregroundColorButton= fAnnotationForegroundColorEditor.getButton(); 277 gd= new GridData(GridData.FILL_HORIZONTAL); 278 gd.horizontalAlignment= GridData.BEGINNING; 279 foregroundColorButton.setLayoutData(gd); 280 281 createDependency(fShowInTextCheckBox, new Control[] {label, foregroundColorButton}); 282 283 fAnnotationTypeViewer.addSelectionChangedListener(new ISelectionChangedListener() { 284 public void selectionChanged(SelectionChangedEvent event) { 285 handleAnnotationListSelection(); 286 } 287 }); 288 289 fShowInTextCheckBox.addSelectionListener(new SelectionListener() { 290 public void widgetDefaultSelected(SelectionEvent e) { 291 } 293 294 public void widgetSelected(SelectionEvent e) { 295 ListItem item= getSelectedItem(); 296 final boolean value= fShowInTextCheckBox.getSelection(); 297 if (value) { 298 String [] decoration= (String []) ((IStructuredSelection) fDecorationViewer.getSelection()).getFirstElement(); 300 if (HIGHLIGHT.equals(decoration)) 301 getPreferenceStore().setValue(item.highlightKey, true); 302 else 303 getPreferenceStore().setValue(item.textKey, true); 304 } else { 305 getPreferenceStore().setValue(item.textKey, false); 307 getPreferenceStore().setValue(item.highlightKey, false); 308 } 309 getPreferenceStore().setValue(item.textKey, value); 310 updateDecorationViewer(item, false); 311 } 312 }); 313 314 foregroundColorButton.addSelectionListener(new SelectionListener() { 315 public void widgetDefaultSelected(SelectionEvent e) { 316 } 318 319 public void widgetSelected(SelectionEvent e) { 320 ListItem item= getSelectedItem(); 321 PreferenceConverter.setValue(getPreferenceStore(), item.colorKey, fAnnotationForegroundColorEditor.getColorValue()); 322 } 323 }); 324 325 fDecorationViewer.addSelectionChangedListener(new ISelectionChangedListener() { 326 327 330 public void selectionChanged(SelectionChangedEvent event) { 331 String [] decoration= (String []) ((IStructuredSelection) fDecorationViewer.getSelection()).getFirstElement(); 332 ListItem item= getSelectedItem(); 333 334 if (fShowInTextCheckBox.getSelection()) { 335 if (HIGHLIGHT.equals(decoration)) { 336 getPreferenceStore().setValue(item.highlightKey, true); 337 getPreferenceStore().setValue(item.textKey, false); 338 getPreferenceStore().setValue(item.textStyleKey, AnnotationPreference.STYLE_NONE); 339 } else { 340 getPreferenceStore().setValue(item.highlightKey, false); 341 getPreferenceStore().setValue(item.textKey, true); 342 getPreferenceStore().setValue(item.textStyleKey, decoration[1]); 343 } 344 } 345 } 346 }); 347 348 return composite; 349 350 } 351 352 367 protected int convertWidthInCharsToPixels(int chars) { 368 if (fFontMetrics == null) 370 return 0; 371 return Dialog.convertWidthInCharsToPixels(fFontMetrics, chars); 372 } 373 374 389 protected int convertHeightInCharsToPixels(int chars) { 390 if (fFontMetrics == null) 392 return 0; 393 return Dialog.convertHeightInCharsToPixels(fFontMetrics, chars); 394 } 395 406 protected void initializeDialogUnits(Control testControl) { 407 GC gc = new GC(testControl); 409 gc.setFont(JFaceResources.getDialogFont()); 410 fFontMetrics = gc.getFontMetrics(); 411 gc.dispose(); 412 } 413 414 private void handleAnnotationListSelection() { 415 ListItem item= getSelectedItem(); 416 417 RGB rgb= PreferenceConverter.getColor(getPreferenceStore(), item.colorKey); 418 fAnnotationForegroundColorEditor.setColorValue(rgb); 419 420 boolean highlight= item.highlightKey == null ? false : getPreferenceStore().getBoolean(item.highlightKey); 421 boolean showInText = item.textKey == null ? false : getPreferenceStore().getBoolean(item.textKey); 422 fShowInTextCheckBox.setSelection(showInText || highlight); 423 424 updateDecorationViewer(item, true); 425 } 426 427 430 public void initialize() { 431 super.initialize(); 432 433 fAnnotationTypeViewer.setInput(fListModel); 434 fAnnotationTypeViewer.getControl().getDisplay().asyncExec(new Runnable () { 435 public void run() { 436 if (fAnnotationTypeViewer != null && !fAnnotationTypeViewer.getControl().isDisposed()) { 437 fAnnotationTypeViewer.setSelection(new StructuredSelection(fListModel[0])); 438 LinkedModeConfigurationBlock.super.initialize(); 439 } 440 } 441 }); 442 } 443 444 private ListItem getSelectedItem() { 445 return (ListItem) ((IStructuredSelection) fAnnotationTypeViewer.getSelection()).getFirstElement(); 446 } 447 448 private void updateDecorationViewer(ListItem item, boolean changed) { 449 final boolean enabled= fShowInTextCheckBox.getSelection() && !(item.highlightKey == null && item.textStyleKey == null); 452 fDecorationViewer.getControl().setEnabled(enabled); 453 454 if (changed) { 455 String [] selection= null; 456 ArrayList list= new ArrayList (); 457 458 list.addAll(item.validStyles); 459 460 if (getPreferenceStore().getBoolean(item.highlightKey)) 461 selection= HIGHLIGHT; 462 463 if (selection == null) { 465 String val= getPreferenceStore().getString(item.textStyleKey); 466 for (Iterator iter= list.iterator(); iter.hasNext();) { 467 String [] elem= (String []) iter.next(); 468 if (elem[1].equals(val)) { 469 selection= elem; 470 break; 471 } 472 } 473 } 474 475 fDecorationViewer.setInput(list.toArray(new Object [list.size()])); 476 if (selection == null) 477 selection= (String []) list.get(0); 478 fDecorationViewer.setSelection(new StructuredSelection((Object ) selection), true); 479 } 480 } 481 482 483 public void performOk() { 484 super.performOk(); 485 486 getPreferenceStore().propagate(); 487 488 try { 489 Platform.getPreferencesService().getRootNode().node(InstanceScope.SCOPE).node(EditorsUI.PLUGIN_ID).flush(); 490 } catch (BackingStoreException e) { 491 JavaPlugin.log(e); 492 } 493 } 494 495 496 public void performDefaults() { 497 getPreferenceStore().loadDefaults(); 498 499 503 handleAnnotationListSelection(); 504 super.performDefaults(); 505 } 506 507 public void dispose() { 508 OverlayPreferenceStore store= getPreferenceStore(); 509 if (store != null) { 510 store.stop(); 511 } 512 513 super.dispose(); 514 } 515 516 } 517 | Popular Tags |