1 11 12 package org.eclipse.ui.internal.editors.text; 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.osgi.service.prefs.BackingStoreException; 20 21 import org.eclipse.core.runtime.Assert; 22 import org.eclipse.core.runtime.Platform; 23 import org.eclipse.core.runtime.preferences.InstanceScope; 24 25 import org.eclipse.swt.SWT; 26 import org.eclipse.swt.events.SelectionEvent; 27 import org.eclipse.swt.events.SelectionListener; 28 import org.eclipse.swt.graphics.FontMetrics; 29 import org.eclipse.swt.graphics.GC; 30 import org.eclipse.swt.graphics.Image; 31 import org.eclipse.swt.graphics.RGB; 32 import org.eclipse.swt.layout.GridData; 33 import org.eclipse.swt.layout.GridLayout; 34 import org.eclipse.swt.widgets.Button; 35 import org.eclipse.swt.widgets.Composite; 36 import org.eclipse.swt.widgets.Control; 37 import org.eclipse.swt.widgets.Label; 38 39 40 import org.eclipse.ui.editors.text.EditorsUI; 41 42 import org.eclipse.jface.dialogs.Dialog; 43 import org.eclipse.jface.preference.ColorSelector; 44 import org.eclipse.jface.preference.PreferenceConverter; 45 import org.eclipse.jface.resource.JFaceResources; 46 import org.eclipse.jface.viewers.ArrayContentProvider; 47 import org.eclipse.jface.viewers.ComboViewer; 48 import org.eclipse.jface.viewers.ISelectionChangedListener; 49 import org.eclipse.jface.viewers.IStructuredContentProvider; 50 import org.eclipse.jface.viewers.IStructuredSelection; 51 import org.eclipse.jface.viewers.LabelProvider; 52 import org.eclipse.jface.viewers.SelectionChangedEvent; 53 import org.eclipse.jface.viewers.StructuredSelection; 54 import org.eclipse.jface.viewers.StructuredViewer; 55 import org.eclipse.jface.viewers.TableViewer; 56 import org.eclipse.jface.viewers.Viewer; 57 import org.eclipse.ui.texteditor.AnnotationPreference; 58 import org.eclipse.ui.texteditor.MarkerAnnotationPreferences; 59 60 67 class LinkedModeConfigurationBlock implements IPreferenceConfigurationBlock { 68 69 private static final String EXIT= "org.eclipse.ui.internal.workbench.texteditor.link.exit"; private static final String TARGET= "org.eclipse.ui.internal.workbench.texteditor.link.target"; private static final String MASTER= "org.eclipse.ui.internal.workbench.texteditor.link.master"; private static final String SLAVE= "org.eclipse.ui.internal.workbench.texteditor.link.slave"; 74 private static final class ListItem { 75 final String label; 76 final Image image; 77 final String colorKey; 78 final String highlightKey; 79 final String overviewRulerKey; 80 final String textStyleKey; 81 final String textKey; 82 final String verticalRulerKey; 83 final List validStyles; 84 85 ListItem(String label, Image image, String colorKey, String textKey, String overviewRulerKey, String highlightKey, String verticalRulerKey, String textStyleKey, List validStyles) { 86 this.label= label; 87 this.image= image; 88 this.colorKey= colorKey; 89 this.highlightKey= highlightKey; 90 this.overviewRulerKey= overviewRulerKey; 91 this.textKey= textKey; 92 this.textStyleKey= textStyleKey; 93 this.verticalRulerKey= verticalRulerKey; 94 this.validStyles= validStyles; 95 } 96 } 97 98 private static final class ItemContentProvider implements IStructuredContentProvider { 99 100 public Object [] getElements(Object inputElement) { 101 return (ListItem[]) inputElement; 102 } 103 104 public void dispose() { 105 } 106 107 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 108 } 109 } 110 111 private final class ItemLabelProvider extends LabelProvider { 112 113 public String getText(Object element) { 114 return ((ListItem) element).label; 115 } 116 } 117 118 private static class ArrayLabelProvider extends LabelProvider { 119 public String getText(Object element) { 120 return ((String []) element)[0].toString(); 121 } 122 } 123 124 final static String [] HIGHLIGHT= new String [] {TextEditorMessages.LinkedModeConfigurationBlock_HIGHLIGHT, "unused"}; final static String [] UNDERLINE= new String [] {TextEditorMessages.LinkedModeConfigurationBlock_UNDERLINE, AnnotationPreference.STYLE_UNDERLINE}; 126 final static String [] BOX= new String [] {TextEditorMessages.LinkedModeConfigurationBlock_BOX, AnnotationPreference.STYLE_BOX}; 127 final static String [] DASHED_BOX= new String [] {TextEditorMessages.LinkedModeConfigurationBlock_DASHED_BOX, AnnotationPreference.STYLE_DASHED_BOX}; 128 final static String [] IBEAM= new String [] {TextEditorMessages.LinkedModeConfigurationBlock_IBEAM, AnnotationPreference.STYLE_IBEAM}; 129 final static String [] SQUIGGLES= new String [] {TextEditorMessages.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 protected static final int INDENT= 20; 141 private OverlayPreferenceStore fStore; 142 143 private ArrayList fMasterSlaveListeners= new ArrayList (); 144 145 private OverlayPreferenceStore getPreferenceStore() { 146 return fStore; 147 } 148 149 public LinkedModeConfigurationBlock(OverlayPreferenceStore store) { 150 fStore= store; 151 final MarkerAnnotationPreferences prefs= EditorsPlugin.getDefault().getMarkerAnnotationPreferences(); 152 getPreferenceStore().addKeys(createOverlayStoreKeys(prefs)); 153 fListModel= createAnnotationTypeListModel(prefs); 154 } 155 156 private OverlayPreferenceStore.OverlayKey[] createOverlayStoreKeys(MarkerAnnotationPreferences preferences) { 157 ArrayList overlayKeys= new ArrayList (); 158 159 Iterator e= preferences.getAnnotationPreferences().iterator(); 160 while (e.hasNext()) { 161 AnnotationPreference info= (AnnotationPreference) e.next(); 162 163 if (isLinkedModeAnnotation(info)) { 164 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, info.getColorPreferenceKey())); 165 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, info.getTextPreferenceKey())); 166 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, info.getTextStylePreferenceKey())); 167 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, info.getHighlightPreferenceKey())); 168 } 169 } 170 171 OverlayPreferenceStore.OverlayKey[] keys= new OverlayPreferenceStore.OverlayKey[overlayKeys.size()]; 172 overlayKeys.toArray(keys); 173 return keys; 174 } 175 176 private boolean isLinkedModeAnnotation(AnnotationPreference info) { 177 final Object type= info.getAnnotationType(); 178 return type.equals(MASTER) 179 || (type.equals(SLAVE)) 180 || (type.equals(TARGET)) 181 || (type.equals(EXIT)); 182 } 183 184 private ListItem[] createAnnotationTypeListModel(MarkerAnnotationPreferences preferences) { 185 ArrayList listModelItems= new ArrayList (); 186 Iterator e= preferences.getAnnotationPreferences().iterator(); 187 188 while (e.hasNext()) { 189 AnnotationPreference info= (AnnotationPreference) e.next(); 190 if (isLinkedModeAnnotation(info)) { 191 String label= info.getPreferenceLabel(); 192 List styles= getStyles(info.getAnnotationType()); 193 listModelItems.add(new ListItem(label, null, info.getColorPreferenceKey(), info.getTextPreferenceKey(), info.getOverviewRulerPreferenceKey(), info.getHighlightPreferenceKey(), info.getVerticalRulerPreferenceKey(), info.getTextStylePreferenceKey(), styles)); 194 } 195 } 196 197 ListItem[] items= new ListItem[listModelItems.size()]; 198 listModelItems.toArray(items); 199 return items; 200 } 201 202 203 private List getStyles(Object type) { 204 if (type.equals(MASTER)) 205 return Arrays.asList(new String [][] {BOX, DASHED_BOX, HIGHLIGHT, UNDERLINE, SQUIGGLES}); 206 if (type.equals(SLAVE)) 207 return Arrays.asList(new String [][] {BOX, DASHED_BOX, HIGHLIGHT, UNDERLINE, SQUIGGLES}); 208 if (type.equals(TARGET)) 209 return Arrays.asList(new String [][] {BOX, DASHED_BOX, HIGHLIGHT, UNDERLINE, SQUIGGLES}); 210 if (type.equals(EXIT)) 211 return Arrays.asList(new String [][] {IBEAM}); 212 return new ArrayList (); 213 } 214 215 221 public Control createControl(Composite parent) { 222 OverlayPreferenceStore store= getPreferenceStore(); 223 store.load(); 224 store.start(); 225 226 initializeDialogUnits(parent); 227 228 Composite composite= new Composite(parent, SWT.NONE); 229 GridLayout layout= new GridLayout(); 230 layout.marginHeight= 0; 231 layout.marginWidth= 0; 232 composite.setLayout(layout); 233 234 Label label= new Label(composite, SWT.LEFT); 235 label.setText(TextEditorMessages.LinkedModeConfigurationBlock_annotationPresentationOptions); 236 GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL); 237 label.setLayoutData(gd); 238 239 Composite editorComposite= new Composite(composite, SWT.NONE); 240 layout= new GridLayout(); 241 layout.numColumns= 2; 242 layout.marginHeight= 0; 243 layout.marginWidth= 0; 244 editorComposite.setLayout(layout); 245 gd= new GridData(SWT.FILL, SWT.FILL, true, true); 246 editorComposite.setLayoutData(gd); 247 248 fAnnotationTypeViewer= new TableViewer(editorComposite, SWT.SINGLE | SWT.V_SCROLL | SWT.BORDER); 249 fAnnotationTypeViewer.setLabelProvider(new ItemLabelProvider()); 250 fAnnotationTypeViewer.setContentProvider(new ItemContentProvider()); 251 gd= new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false); 252 gd.heightHint= convertHeightInCharsToPixels(5); 253 fAnnotationTypeViewer.getControl().setLayoutData(gd); 254 255 Composite optionsComposite= new Composite(editorComposite, SWT.NONE); 256 layout= new GridLayout(); 257 layout.marginHeight= 0; 258 layout.marginWidth= 0; 259 layout.numColumns= 2; 260 optionsComposite.setLayout(layout); 261 optionsComposite.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false)); 262 263 265 fShowInTextCheckBox= new Button(optionsComposite, SWT.CHECK); 266 fShowInTextCheckBox.setText(TextEditorMessages.LinkedModeConfigurationBlock_labels_showIn); 267 gd= new GridData(GridData.FILL_HORIZONTAL); 268 gd.horizontalAlignment= GridData.BEGINNING; 269 fShowInTextCheckBox.setLayoutData(gd); 270 271 fDecorationViewer= new ComboViewer(optionsComposite, SWT.READ_ONLY); 272 fDecorationViewer.setContentProvider(new ArrayContentProvider()); 273 fDecorationViewer.setLabelProvider(new ArrayLabelProvider()); 274 gd= new GridData(GridData.FILL_HORIZONTAL); 275 gd.horizontalAlignment= GridData.BEGINNING; 276 fDecorationViewer.getControl().setLayoutData(gd); 277 fDecorationViewer.setInput(new Object [] {HIGHLIGHT, SQUIGGLES, BOX, DASHED_BOX, UNDERLINE, IBEAM}); 278 279 label= new Label(optionsComposite, SWT.LEFT); 280 label.setText(TextEditorMessages.LinkedModeConfigurationBlock_color); 281 gd= new GridData(); 282 gd.horizontalAlignment= GridData.BEGINNING; 283 label.setLayoutData(gd); 284 285 fAnnotationForegroundColorEditor= new ColorSelector(optionsComposite); 286 Button foregroundColorButton= fAnnotationForegroundColorEditor.getButton(); 287 gd= new GridData(GridData.FILL_HORIZONTAL); 288 gd.horizontalAlignment= GridData.BEGINNING; 289 foregroundColorButton.setLayoutData(gd); 290 291 createDependency(fShowInTextCheckBox, new Control[] {label, foregroundColorButton}); 292 293 fAnnotationTypeViewer.addSelectionChangedListener(new ISelectionChangedListener() { 294 public void selectionChanged(SelectionChangedEvent event) { 295 handleAnnotationListSelection(); 296 } 297 }); 298 299 fShowInTextCheckBox.addSelectionListener(new SelectionListener() { 300 public void widgetDefaultSelected(SelectionEvent e) { 301 } 303 304 public void widgetSelected(SelectionEvent e) { 305 ListItem item= getSelectedItem(); 306 final boolean value= fShowInTextCheckBox.getSelection(); 307 if (value) { 308 String [] decoration= (String []) ((IStructuredSelection) fDecorationViewer.getSelection()).getFirstElement(); 310 if (HIGHLIGHT.equals(decoration)) 311 getPreferenceStore().setValue(item.highlightKey, true); 312 else 313 getPreferenceStore().setValue(item.textKey, true); 314 } else { 315 getPreferenceStore().setValue(item.textKey, false); 317 getPreferenceStore().setValue(item.highlightKey, false); 318 } 319 getPreferenceStore().setValue(item.textKey, value); 320 updateDecorationViewer(item, false); 321 } 322 }); 323 324 foregroundColorButton.addSelectionListener(new SelectionListener() { 325 public void widgetDefaultSelected(SelectionEvent e) { 326 } 328 329 public void widgetSelected(SelectionEvent e) { 330 ListItem item= getSelectedItem(); 331 PreferenceConverter.setValue(getPreferenceStore(), item.colorKey, fAnnotationForegroundColorEditor.getColorValue()); 332 } 333 }); 334 335 fDecorationViewer.addSelectionChangedListener(new ISelectionChangedListener() { 336 337 340 public void selectionChanged(SelectionChangedEvent event) { 341 String [] decoration= (String []) ((IStructuredSelection) fDecorationViewer.getSelection()).getFirstElement(); 342 ListItem item= getSelectedItem(); 343 344 if (fShowInTextCheckBox.getSelection()) { 345 if (HIGHLIGHT.equals(decoration)) { 346 getPreferenceStore().setValue(item.highlightKey, true); 347 getPreferenceStore().setValue(item.textKey, false); 348 getPreferenceStore().setValue(item.textStyleKey, AnnotationPreference.STYLE_NONE); 349 } else { 350 getPreferenceStore().setValue(item.highlightKey, false); 351 getPreferenceStore().setValue(item.textKey, true); 352 getPreferenceStore().setValue(item.textStyleKey, decoration[1]); 353 } 354 } 355 } 356 }); 357 358 return composite; 359 360 } 361 362 377 protected int convertWidthInCharsToPixels(int chars) { 378 if (fFontMetrics == null) 380 return 0; 381 return Dialog.convertWidthInCharsToPixels(fFontMetrics, chars); 382 } 383 384 399 protected int convertHeightInCharsToPixels(int chars) { 400 if (fFontMetrics == null) 402 return 0; 403 return Dialog.convertHeightInCharsToPixels(fFontMetrics, chars); 404 } 405 416 protected void initializeDialogUnits(Control testControl) { 417 GC gc = new GC(testControl); 419 gc.setFont(JFaceResources.getDialogFont()); 420 fFontMetrics = gc.getFontMetrics(); 421 gc.dispose(); 422 } 423 424 private void handleAnnotationListSelection() { 425 ListItem item= getSelectedItem(); 426 427 RGB rgb= PreferenceConverter.getColor(getPreferenceStore(), item.colorKey); 428 fAnnotationForegroundColorEditor.setColorValue(rgb); 429 430 boolean highlight= item.highlightKey == null ? false : getPreferenceStore().getBoolean(item.highlightKey); 431 boolean showInText = item.textKey == null ? false : getPreferenceStore().getBoolean(item.textKey); 432 fShowInTextCheckBox.setSelection(showInText || highlight); 433 434 updateDecorationViewer(item, true); 435 } 436 437 440 public void initialize() { 441 initializeFields(); 442 443 fAnnotationTypeViewer.setInput(fListModel); 444 fAnnotationTypeViewer.getControl().getDisplay().asyncExec(new Runnable () { 445 public void run() { 446 if (fAnnotationTypeViewer != null && !fAnnotationTypeViewer.getControl().isDisposed()) { 447 fAnnotationTypeViewer.setSelection(new StructuredSelection(fListModel[0])); 448 initializeFields(); 449 } 450 } 451 }); 452 } 453 454 private ListItem getSelectedItem() { 455 return (ListItem) ((IStructuredSelection) fAnnotationTypeViewer.getSelection()).getFirstElement(); 456 } 457 458 private void updateDecorationViewer(ListItem item, boolean changed) { 459 final boolean enabled= fShowInTextCheckBox.getSelection() && !(item.highlightKey == null && item.textStyleKey == null); 462 fDecorationViewer.getControl().setEnabled(enabled); 463 464 if (changed) { 465 String [] selection= null; 466 ArrayList list= new ArrayList (); 467 468 list.addAll(item.validStyles); 469 470 if (getPreferenceStore().getBoolean(item.highlightKey)) 471 selection= HIGHLIGHT; 472 473 if (selection == null) { 475 String val= getPreferenceStore().getString(item.textStyleKey); 476 for (Iterator iter= list.iterator(); iter.hasNext();) { 477 String [] elem= (String []) iter.next(); 478 if (elem[1].equals(val)) { 479 selection= elem; 480 break; 481 } 482 } 483 } 484 485 fDecorationViewer.setInput(list.toArray(new Object [list.size()])); 486 if (selection == null) 487 selection= (String []) list.get(0); 488 fDecorationViewer.setSelection(new StructuredSelection((Object ) selection), true); 489 } 490 } 491 492 493 public void performOk() { 494 getPreferenceStore().propagate(); 495 496 try { 497 Platform.getPreferencesService().getRootNode().node(InstanceScope.SCOPE).node(EditorsUI.PLUGIN_ID).flush(); 498 } catch (BackingStoreException e) { 499 EditorsPlugin.log(e); 500 } 501 } 502 503 504 public void performDefaults() { 505 getPreferenceStore().loadDefaults(); 506 507 511 handleAnnotationListSelection(); 512 513 initializeFields(); 514 } 515 516 public void dispose() { 517 OverlayPreferenceStore store= getPreferenceStore(); 518 if (store != null) { 519 store.stop(); 520 } 521 } 522 523 protected void createDependency(final Button master, final Control slave) { 524 createDependency(master, new Control[] {slave}); 525 } 526 527 protected void createDependency(final Button master, final Control[] slaves) { 528 Assert.isTrue(slaves.length > 0); 529 indent(slaves[0]); 530 SelectionListener listener= new SelectionListener() { 531 public void widgetSelected(SelectionEvent e) { 532 boolean state= master.getSelection(); 533 for (int i= 0; i < slaves.length; i++) { 534 slaves[i].setEnabled(state); 535 } 536 } 537 538 public void widgetDefaultSelected(SelectionEvent e) {} 539 }; 540 master.addSelectionListener(listener); 541 fMasterSlaveListeners.add(listener); 542 } 543 544 protected static void indent(Control control) { 545 ((GridData) control.getLayoutData()).horizontalIndent+= INDENT; 546 } 547 548 private void initializeFields() { 549 550 Iterator iter= fMasterSlaveListeners.iterator(); 552 while (iter.hasNext()) { 553 SelectionListener listener= (SelectionListener)iter.next(); 554 listener.widgetSelected(null); 555 } 556 } 557 558 561 public boolean canPerformOk() { 562 return true; 563 } 564 565 } 566 | Popular Tags |