KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > editors > text > AnnotationsConfigurationBlock


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.internal.editors.text;
13
14 import com.ibm.icu.text.Collator;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Collections JavaDoc;
17 import java.util.Comparator JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.events.SelectionListener;
27 import org.eclipse.swt.graphics.Color;
28 import org.eclipse.swt.graphics.Image;
29 import org.eclipse.swt.graphics.ImageData;
30 import org.eclipse.swt.graphics.RGB;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.layout.GridLayout;
33 import org.eclipse.swt.widgets.Button;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Control;
36 import org.eclipse.swt.widgets.Display;
37 import org.eclipse.swt.widgets.Label;
38
39 import org.eclipse.core.runtime.Assert;
40
41 import org.eclipse.jface.preference.ColorSelector;
42 import org.eclipse.jface.preference.PreferenceConverter;
43 import org.eclipse.jface.resource.ImageDescriptor;
44 import org.eclipse.jface.resource.ImageRegistry;
45 import org.eclipse.jface.viewers.ArrayContentProvider;
46 import org.eclipse.jface.viewers.ComboViewer;
47 import org.eclipse.jface.viewers.IColorProvider;
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
58
59 import org.eclipse.ui.ISharedImages;
60 import org.eclipse.ui.PlatformUI;
61 import org.eclipse.ui.ide.IDE;
62 import org.eclipse.ui.texteditor.AnnotationPreference;
63 import org.eclipse.ui.texteditor.MarkerAnnotationPreferences;
64
65
66 /**
67  * Configures Annotation preferences.
68  *
69  * @since 3.0
70  */

71 class AnnotationsConfigurationBlock implements IPreferenceConfigurationBlock {
72     private static final class ListItem {
73         final String JavaDoc label;
74         final Image image;
75         final String JavaDoc colorKey;
76         final String JavaDoc highlightKey;
77         final String JavaDoc overviewRulerKey;
78         final String JavaDoc textStyleKey;
79         final String JavaDoc textKey;
80         final String JavaDoc verticalRulerKey;
81
82         ListItem(String JavaDoc label, Image image, String JavaDoc colorKey, String JavaDoc textKey, String JavaDoc overviewRulerKey, String JavaDoc highlightKey, String JavaDoc verticalRulerKey, String JavaDoc textStyleKey) {
83             this.label= label;
84             this.image= image;
85             this.colorKey= colorKey;
86             this.highlightKey= highlightKey;
87             this.overviewRulerKey= overviewRulerKey;
88             this.textKey= textKey;
89             this.textStyleKey= textStyleKey;
90             this.verticalRulerKey= verticalRulerKey;
91         }
92     }
93
94     private static final class ItemContentProvider implements IStructuredContentProvider {
95
96         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
97             return (ListItem[]) inputElement;
98         }
99
100         public void dispose() {
101         }
102
103         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
104         }
105     }
106
107     private final class ItemLabelProvider extends LabelProvider implements IColorProvider {
108
109         public String JavaDoc getText(Object JavaDoc element) {
110             return ((ListItem) element).label;
111         }
112
113
114         public Image getImage(Object JavaDoc element) {
115             ListItem item= (ListItem) element;
116             if (item.verticalRulerKey != null && fStore.getBoolean(item.verticalRulerKey))
117                 return item.image;
118
119             return null; // don't show icon if preference is not to show in vertical ruler
120
}
121
122
123         public Color getForeground(Object JavaDoc element) {
124             return null;
125         }
126
127         /*
128          * @see org.eclipse.jface.viewers.IColorProvider#getBackground(java.lang.Object)
129          */

130         public Color getBackground(Object JavaDoc element) {
131             String JavaDoc key= ((ListItem) element).highlightKey;
132             if (key != null && fStore.getBoolean(key)) {
133                 RGB color= PreferenceConverter.getColor(fStore, ((ListItem)element).colorKey);
134                 color= interpolate(color, new RGB(255, 255, 255), 0.6);
135                 return EditorsPlugin.getDefault().getSharedTextColors().getColor(color);
136             }
137             return null;
138         }
139
140         /**
141          * Returns a specification of a color that lies between the given
142          * foreground and background color using the given scale factor.
143          *
144          * @param fg the foreground color
145          * @param bg the background color
146          * @param scale the scale factor
147          * @return the interpolated color
148          */

149         private RGB interpolate(RGB fg, RGB bg, double scale) {
150             return new RGB(
151                 (int) ((1.0-scale) * fg.red + scale * bg.red),
152                 (int) ((1.0-scale) * fg.green + scale * bg.green),
153                 (int) ((1.0-scale) * fg.blue + scale * bg.blue)
154             );
155         }
156     }
157
158     private static class ArrayLabelProvider extends LabelProvider {
159         public String JavaDoc getText(Object JavaDoc element) {
160             return ((String JavaDoc[]) element)[0].toString();
161         }
162     }
163
164     /* copied from DefaultMarkerAnnotationAccess */
165     public static final String JavaDoc ERROR_SYSTEM_IMAGE= "error"; //$NON-NLS-1$
166
public static final String JavaDoc WARNING_SYSTEM_IMAGE= "warning"; //$NON-NLS-1$
167
public static final String JavaDoc INFO_SYSTEM_IMAGE= "info"; //$NON-NLS-1$
168
public static final String JavaDoc TASK_SYSTEM_IMAGE= "task"; //$NON-NLS-1$
169
public static final String JavaDoc BOOKMARK_SYSTEM_IMAGE= "bookmark"; //$NON-NLS-1$
170

171     private final static Map JavaDoc MAPPING;
172
173     static {
174         MAPPING= new HashMap JavaDoc();
175         MAPPING.put(ERROR_SYSTEM_IMAGE, ISharedImages.IMG_OBJS_ERROR_TSK);
176         MAPPING.put(WARNING_SYSTEM_IMAGE, ISharedImages.IMG_OBJS_WARN_TSK);
177         MAPPING.put(INFO_SYSTEM_IMAGE, ISharedImages.IMG_OBJS_INFO_TSK);
178         MAPPING.put(TASK_SYSTEM_IMAGE, IDE.SharedImages.IMG_OBJS_TASK_TSK);
179         MAPPING.put(BOOKMARK_SYSTEM_IMAGE, IDE.SharedImages.IMG_OBJS_BKMRK_TSK);
180     }
181
182     final static String JavaDoc[] HIGHLIGHT= new String JavaDoc[] {TextEditorMessages.AnnotationsConfigurationBlock_HIGHLIGHT, "not used"}; //$NON-NLS-1$
183
final static String JavaDoc[] UNDERLINE= new String JavaDoc[] {TextEditorMessages.AnnotationsConfigurationBlock_UNDERLINE, AnnotationPreference.STYLE_UNDERLINE};
184     final static String JavaDoc[] BOX= new String JavaDoc[] {TextEditorMessages.AnnotationsConfigurationBlock_BOX, AnnotationPreference.STYLE_BOX};
185     final static String JavaDoc[] DASHED_BOX= new String JavaDoc[] {TextEditorMessages.AnnotationsConfigurationBlock_DASHED_BOX, AnnotationPreference.STYLE_DASHED_BOX};
186     final static String JavaDoc[] IBEAM= new String JavaDoc[] {TextEditorMessages.AnnotationsConfigurationBlock_IBEAM, AnnotationPreference.STYLE_IBEAM};
187     final static String JavaDoc[] SQUIGGLES= new String JavaDoc[] {TextEditorMessages.AnnotationsConfigurationBlock_SQUIGGLES, AnnotationPreference.STYLE_SQUIGGLES};
188
189
190
191     private OverlayPreferenceStore fStore;
192     private ColorSelector fAnnotationForegroundColorEditor;
193
194     private Button fShowInTextCheckBox;
195     private Button fShowInOverviewRulerCheckBox;
196     private Button fShowInVerticalRulerCheckBox;
197
198     private StructuredViewer fAnnotationTypeViewer;
199     private final ListItem[] fListModel;
200
201     private ComboViewer fDecorationViewer;
202     private final Set JavaDoc fImageKeys= new HashSet JavaDoc();
203
204     public AnnotationsConfigurationBlock(OverlayPreferenceStore store) {
205         Assert.isNotNull(store);
206         MarkerAnnotationPreferences markerAnnotationPreferences= EditorsPlugin.getDefault().getMarkerAnnotationPreferences();
207         fStore= store;
208         fStore.addKeys(createOverlayStoreKeys(markerAnnotationPreferences));
209         fListModel= createAnnotationTypeListModel(markerAnnotationPreferences);
210     }
211
212     private OverlayPreferenceStore.OverlayKey[] createOverlayStoreKeys(MarkerAnnotationPreferences preferences) {
213
214         ArrayList JavaDoc overlayKeys= new ArrayList JavaDoc();
215         Iterator JavaDoc e= preferences.getAnnotationPreferences().iterator();
216
217         while (e.hasNext()) {
218             AnnotationPreference info= (AnnotationPreference) e.next();
219             overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, info.getColorPreferenceKey()));
220             overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, info.getTextPreferenceKey()));
221             if (info.getHighlightPreferenceKey() != null)
222                 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, info.getHighlightPreferenceKey()));
223             overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, info.getOverviewRulerPreferenceKey()));
224             if (info.getVerticalRulerPreferenceKey() != null)
225                 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, info.getVerticalRulerPreferenceKey()));
226             if (info.getTextStylePreferenceKey() != null)
227                 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, info.getTextStylePreferenceKey()));
228         }
229         OverlayPreferenceStore.OverlayKey[] keys= new OverlayPreferenceStore.OverlayKey[overlayKeys.size()];
230         overlayKeys.toArray(keys);
231         return keys;
232     }
233
234     /*
235      * @see org.eclipse.ui.internal.editors.text.IPreferenceConfigurationBlock#createControl(org.eclipse.swt.widgets.Composite)
236      */

237     public Control createControl(Composite parent) {
238
239         PixelConverter pixelConverter= new PixelConverter(parent);
240
241         Composite composite= new Composite(parent, SWT.NULL);
242         GridLayout layout= new GridLayout();
243         layout.numColumns= 2;
244         layout.marginHeight= 0;
245         layout.marginWidth= 0;
246         composite.setLayout(layout);
247
248         Label label= new Label(composite, SWT.LEFT);
249         label.setText(TextEditorMessages.AnnotationsConfigurationBlock_annotationPresentationOptions);
250         GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
251         gd.horizontalSpan= 2;
252         label.setLayoutData(gd);
253
254         Composite editorComposite= new Composite(composite, SWT.NONE);
255         layout= new GridLayout();
256         layout.numColumns= 2;
257         layout.marginHeight= 0;
258         layout.marginWidth= 0;
259         editorComposite.setLayout(layout);
260         gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.FILL_VERTICAL);
261         gd.horizontalSpan= 2;
262         editorComposite.setLayoutData(gd);
263
264         fAnnotationTypeViewer= new TableViewer(editorComposite, SWT.SINGLE | SWT.V_SCROLL | SWT.BORDER | SWT.FULL_SELECTION);
265         fAnnotationTypeViewer.setLabelProvider(new ItemLabelProvider());
266         fAnnotationTypeViewer.setContentProvider(new ItemContentProvider());
267         gd= new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false);
268         gd.heightHint= pixelConverter.convertHeightInCharsToPixels(20);
269         fAnnotationTypeViewer.getControl().setLayoutData(gd);
270
271         Composite optionsComposite= new Composite(editorComposite, SWT.NONE);
272         layout= new GridLayout();
273         layout.marginHeight= 0;
274         layout.marginWidth= 0;
275         layout.numColumns= 2;
276         optionsComposite.setLayout(layout);
277         optionsComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
278
279         label= new Label(optionsComposite, SWT.LEFT);
280         label.setText(TextEditorMessages.AnnotationsConfigurationBlock_labels_showIn);
281         gd= new GridData(GridData.FILL_HORIZONTAL);
282         gd.horizontalAlignment= GridData.BEGINNING;
283         gd.horizontalSpan= 2;
284         label.setLayoutData(gd);
285
286         // we only allow to set either "show in text" or "highlight in text", but not both
287

288         fShowInTextCheckBox= new Button(optionsComposite, SWT.CHECK);
289         fShowInTextCheckBox.setText(TextEditorMessages.AnnotationsConfigurationBlock_showInText);
290         gd= new GridData(GridData.FILL_HORIZONTAL);
291         gd.horizontalAlignment= GridData.BEGINNING;
292         gd.horizontalIndent= 20;
293         fShowInTextCheckBox.setLayoutData(gd);
294
295         fDecorationViewer= new ComboViewer(optionsComposite, SWT.READ_ONLY);
296         fDecorationViewer.setContentProvider(new ArrayContentProvider());
297         fDecorationViewer.setLabelProvider(new ArrayLabelProvider());
298         gd= new GridData(GridData.FILL_HORIZONTAL);
299         gd.horizontalAlignment= GridData.BEGINNING;
300         fDecorationViewer.getControl().setLayoutData(gd);
301         fDecorationViewer.setInput(new Object JavaDoc[] {HIGHLIGHT, SQUIGGLES, BOX, DASHED_BOX, UNDERLINE, IBEAM});
302
303         fShowInOverviewRulerCheckBox= new Button(optionsComposite, SWT.CHECK);
304         fShowInOverviewRulerCheckBox.setText(TextEditorMessages.AnnotationsConfigurationBlock_showInOverviewRuler);
305         gd= new GridData(GridData.FILL_HORIZONTAL);
306         gd.horizontalAlignment= GridData.BEGINNING;
307         gd.horizontalSpan= 2;
308         gd.horizontalIndent= 20;
309         fShowInOverviewRulerCheckBox.setLayoutData(gd);
310
311         fShowInVerticalRulerCheckBox= new Button(optionsComposite, SWT.CHECK);
312         fShowInVerticalRulerCheckBox.setText(TextEditorMessages.AnnotationsConfigurationBlock_showInVerticalRuler);
313         gd= new GridData(GridData.FILL_HORIZONTAL);
314         gd.horizontalAlignment= GridData.BEGINNING;
315         gd.horizontalSpan= 2;
316         gd.horizontalIndent= 20;
317         fShowInVerticalRulerCheckBox.setLayoutData(gd);
318
319         label= new Label(optionsComposite, SWT.LEFT);
320         label.setText(TextEditorMessages.AnnotationsConfigurationBlock_color);
321         gd= new GridData();
322         gd.horizontalAlignment= GridData.BEGINNING;
323         gd.horizontalIndent= 20;
324         label.setLayoutData(gd);
325
326         fAnnotationForegroundColorEditor= new ColorSelector(optionsComposite);
327         Button foregroundColorButton= fAnnotationForegroundColorEditor.getButton();
328         gd= new GridData(GridData.FILL_HORIZONTAL);
329         gd.horizontalAlignment= GridData.BEGINNING;
330         foregroundColorButton.setLayoutData(gd);
331
332         fAnnotationTypeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
333             public void selectionChanged(SelectionChangedEvent event) {
334                 handleAnnotationListSelection();
335             }
336         });
337
338         fShowInTextCheckBox.addSelectionListener(new SelectionListener() {
339             public void widgetDefaultSelected(SelectionEvent e) {
340                 // do nothing
341
}
342
343             public void widgetSelected(SelectionEvent e) {
344                 ListItem item= getSelectedItem();
345                 final boolean value= fShowInTextCheckBox.getSelection();
346                 if (value) {
347                     // enable whatever is in the combo
348
String JavaDoc[] decoration= (String JavaDoc[]) ((IStructuredSelection) fDecorationViewer.getSelection()).getFirstElement();
349                     if (HIGHLIGHT.equals(decoration))
350                         fStore.setValue(item.highlightKey, true);
351                     else
352                         fStore.setValue(item.textKey, true);
353                 } else {
354                     // disable both
355
if (item.textKey != null)
356                         fStore.setValue(item.textKey, false);
357                     if (item.highlightKey != null)
358                         fStore.setValue(item.highlightKey, false);
359                 }
360                 fStore.setValue(item.textKey, value);
361                 updateDecorationViewer(item, false);
362                 fAnnotationTypeViewer.refresh(item);
363             }
364         });
365
366         fShowInOverviewRulerCheckBox.addSelectionListener(new SelectionListener() {
367             public void widgetDefaultSelected(SelectionEvent e) {
368                 // do nothing
369
}
370
371             public void widgetSelected(SelectionEvent e) {
372                 ListItem item= getSelectedItem();
373                 fStore.setValue(item.overviewRulerKey, fShowInOverviewRulerCheckBox.getSelection());
374                 fAnnotationTypeViewer.refresh(item);
375             }
376         });
377
378         fShowInVerticalRulerCheckBox.addSelectionListener(new SelectionListener() {
379             public void widgetDefaultSelected(SelectionEvent e) {
380                 // do nothing
381
}
382
383             public void widgetSelected(SelectionEvent e) {
384                 ListItem item= getSelectedItem();
385                 fStore.setValue(item.verticalRulerKey, fShowInVerticalRulerCheckBox.getSelection());
386                 fAnnotationTypeViewer.refresh(item);
387             }
388         });
389
390         foregroundColorButton.addSelectionListener(new SelectionListener() {
391             public void widgetDefaultSelected(SelectionEvent e) {
392                 // do nothing
393
}
394
395             public void widgetSelected(SelectionEvent e) {
396                 ListItem item= getSelectedItem();
397                 PreferenceConverter.setValue(fStore, item.colorKey, fAnnotationForegroundColorEditor.getColorValue());
398                 fAnnotationTypeViewer.refresh(item);
399             }
400         });
401
402         fDecorationViewer.addSelectionChangedListener(new ISelectionChangedListener() {
403
404             /*
405              * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
406              */

407             public void selectionChanged(SelectionChangedEvent event) {
408                 String JavaDoc[] decoration= (String JavaDoc[]) ((IStructuredSelection) fDecorationViewer.getSelection()).getFirstElement();
409                 ListItem item= getSelectedItem();
410
411                 if (fShowInTextCheckBox.getSelection()) {
412                     if (HIGHLIGHT.equals(decoration)) {
413                         fStore.setValue(item.highlightKey, true);
414                         if (item.textKey != null) {
415                             fStore.setValue(item.textKey, false);
416                             if (item.textStyleKey != null)
417                                 fStore.setValue(item.textStyleKey, AnnotationPreference.STYLE_NONE);
418                         }
419                     } else {
420                         if (item.highlightKey != null)
421                             fStore.setValue(item.highlightKey, false);
422                         if (item.textKey != null) {
423                             fStore.setValue(item.textKey, true);
424                             if (item.textStyleKey != null)
425                                 fStore.setValue(item.textStyleKey, decoration[1]);
426                         }
427                     }
428                 }
429
430                 fAnnotationTypeViewer.refresh(item);
431             }
432         });
433
434         composite.layout();
435         return composite;
436     }
437
438     /*
439      * @see org.eclipse.ui.internal.editors.text.IPreferenceConfigurationBlock#canPerformOk()
440      */

441     public boolean canPerformOk() {
442         return true;
443     }
444
445     /*
446      * @see PreferencePage#performOk()
447      */

448     public void performOk() {
449     }
450
451     /*
452      * @see PreferencePage#performDefaults()
453      */

454     public void performDefaults() {
455         fStore.loadDefaults();
456         fAnnotationTypeViewer.refresh();
457         handleAnnotationListSelection();
458     }
459
460     private void handleAnnotationListSelection() {
461         ListItem item= getSelectedItem();
462
463         RGB rgb= PreferenceConverter.getColor(fStore, item.colorKey);
464         fAnnotationForegroundColorEditor.setColorValue(rgb);
465
466         boolean highlight= item.highlightKey == null ? false : fStore.getBoolean(item.highlightKey);
467         boolean showInText = item.textKey == null ? false : fStore.getBoolean(item.textKey);
468         fShowInTextCheckBox.setSelection(showInText || highlight);
469
470         updateDecorationViewer(item, true);
471
472         fShowInOverviewRulerCheckBox.setSelection(fStore.getBoolean(item.overviewRulerKey));
473
474         if (item.verticalRulerKey != null) {
475             fShowInVerticalRulerCheckBox.setSelection(fStore.getBoolean(item.verticalRulerKey));
476             fShowInVerticalRulerCheckBox.setEnabled(true);
477         } else {
478             fShowInVerticalRulerCheckBox.setSelection(true);
479             fShowInVerticalRulerCheckBox.setEnabled(false);
480         }
481     }
482
483
484
485     public void initialize() {
486
487         fAnnotationTypeViewer.setInput(fListModel);
488         fAnnotationTypeViewer.getControl().getDisplay().asyncExec(new Runnable JavaDoc() {
489             public void run() {
490                 if (fAnnotationTypeViewer != null && !fAnnotationTypeViewer.getControl().isDisposed()) {
491                     fAnnotationTypeViewer.setSelection(new StructuredSelection(fListModel[0]));
492                 }
493             }
494         });
495
496     }
497
498     private ListItem[] createAnnotationTypeListModel(MarkerAnnotationPreferences preferences) {
499         ArrayList JavaDoc listModelItems= new ArrayList JavaDoc();
500         Iterator JavaDoc e= preferences.getAnnotationPreferences().iterator();
501
502         while (e.hasNext()) {
503             AnnotationPreference info= (AnnotationPreference) e.next();
504             if (info.isIncludeOnPreferencePage()) {
505                 String JavaDoc label= info.getPreferenceLabel();
506                 if (containsMoreThanOne(preferences.getAnnotationPreferences().iterator(), label))
507                     label += " (" + info.getAnnotationType() + ")"; //$NON-NLS-1$//$NON-NLS-2$
508

509                 Image image= getImage(info);
510
511                 listModelItems.add(new ListItem(label, image, info.getColorPreferenceKey(), info.getTextPreferenceKey(), info.getOverviewRulerPreferenceKey(), info.getHighlightPreferenceKey(), info.getVerticalRulerPreferenceKey(), info.getTextStylePreferenceKey()));
512             }
513         }
514
515         Comparator JavaDoc comparator= new Comparator JavaDoc() {
516             /*
517              * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
518              */

519             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
520                 if (!(o2 instanceof ListItem))
521                     return -1;
522                 if (!(o1 instanceof ListItem))
523                     return 1;
524
525                 String JavaDoc label1= ((ListItem)o1).label;
526                 String JavaDoc label2= ((ListItem)o2).label;
527
528                 return Collator.getInstance().compare(label1, label2);
529
530             }
531         };
532         Collections.sort(listModelItems, comparator);
533
534         ListItem[] items= new ListItem[listModelItems.size()];
535         listModelItems.toArray(items);
536         return items;
537     }
538
539     /**
540      * Returns the image for the given annotation and the given annotation preferences or
541      * <code>null</code> if there is no such image.
542      *
543      * @param preference the annotation preference
544      * @return the image or <code>null</code>
545      * @since 3.1
546      */

547     private Image getImage(AnnotationPreference preference) {
548
549         ImageRegistry registry= EditorsPlugin.getDefault().getImageRegistry();
550
551         String JavaDoc annotationType= (String JavaDoc) preference.getAnnotationType();
552         if (annotationType == null)
553             return null;
554
555         String JavaDoc customImage= annotationType + "__AnnotationsConfigurationBlock_Image"; //$NON-NLS-1$
556

557         Image image;
558         image= registry.get(customImage);
559         if (image != null)
560             return image;
561
562         image= registry.get(annotationType);
563         if (image == null) {
564             ImageDescriptor descriptor= preference.getImageDescriptor();
565             if (descriptor != null) {
566                 registry.put(annotationType, descriptor);
567                 image= registry.get(annotationType);
568             } else {
569                 String JavaDoc key= translateSymbolicImageName(preference.getSymbolicImageName());
570                 if (key != null) {
571                     ISharedImages sharedImages= PlatformUI.getWorkbench().getSharedImages();
572                     image= sharedImages.getImage(key);
573                 }
574             }
575         }
576
577         if (image == null)
578             return image;
579
580         // create custom image
581
final int SIZE= 16; // square images
582
ImageData data= image.getImageData();
583         Image copy;
584         if (data.height > SIZE || data.width > SIZE) {
585             // scale down to icon size
586
copy= new Image(Display.getCurrent(), data.scaledTo(SIZE, SIZE));
587         } else {
588             // don't scale up, but rather copy into the middle and mark everything else transparent
589
ImageData mask= data.getTransparencyMask();
590             ImageData resized= new ImageData(SIZE, SIZE, data.depth, data.palette);
591             ImageData resizedMask= new ImageData(SIZE, SIZE, mask.depth, mask.palette);
592
593             int xo= Math.max(0, (SIZE - data.width) / 2);
594             int yo= Math.max(0, (SIZE - data.height) / 2);
595
596             for (int y= 0; y < SIZE; y++) {
597                 for (int x= 0; x < SIZE; x++) {
598                     if (y >= yo && x >= xo && y < yo + data.height && x < xo + data.width) {
599                         resized.setPixel(x, y, data.getPixel(x - xo, y - yo));
600                         resizedMask.setPixel(x, y, mask.getPixel(x - xo, y - yo));
601                     }
602                 }
603             }
604
605             copy= new Image(Display.getCurrent(), resized, resizedMask);
606         }
607
608         fImageKeys.add(customImage);
609         registry.put(customImage, copy);
610         return copy;
611     }
612
613     /**
614      * Translates the given symbolic image name into the according symbolic image name
615      * the {@link org.eclipse.ui.ISharedImages} understands.
616      *
617      * @param symbolicImageName the symbolic system image name to be translated
618      * @return the shared image name
619      * @since 3.1
620      */

621     private String JavaDoc translateSymbolicImageName(String JavaDoc symbolicImageName) {
622         return (String JavaDoc) MAPPING.get(symbolicImageName);
623     }
624
625     private boolean containsMoreThanOne(Iterator JavaDoc annotationPrefernceIterator, String JavaDoc label) {
626         if (label == null)
627             return false;
628
629         int count= 0;
630         while (annotationPrefernceIterator.hasNext()) {
631             if (label.equals(((AnnotationPreference)annotationPrefernceIterator.next()).getPreferenceLabel()))
632                 count++;
633
634             if (count == 2)
635                 return true;
636         }
637         return false;
638     }
639
640     /*
641      * @see IPreferenceConfigurationBlock#dispose()
642      */

643     public void dispose() {
644         ImageRegistry registry= EditorsPlugin.getDefault().getImageRegistry();
645
646         for (Iterator JavaDoc it= fImageKeys.iterator(); it.hasNext();) {
647             String JavaDoc string= (String JavaDoc) it.next();
648             registry.remove(string);
649         }
650
651         fImageKeys.clear();
652     }
653
654     private ListItem getSelectedItem() {
655         return (ListItem) ((IStructuredSelection) fAnnotationTypeViewer.getSelection()).getFirstElement();
656     }
657
658     private void updateDecorationViewer(ListItem item, boolean changed) {
659         // decoration selection: if the checkbox is enabled, there is
660
// only one case where the combo is not enabled: if both the highlight and textStyle keys are null
661
final boolean enabled= fShowInTextCheckBox.getSelection() && !(item.highlightKey == null && item.textStyleKey == null);
662         fDecorationViewer.getControl().setEnabled(enabled);
663
664         if (changed) {
665             String JavaDoc[] selection= null;
666             ArrayList JavaDoc list= new ArrayList JavaDoc();
667
668             // highlighting
669
if (item.highlightKey != null) {
670                 list.add(HIGHLIGHT);
671                 if (fStore.getBoolean(item.highlightKey))
672                     selection= HIGHLIGHT;
673             }
674
675             // legacy default= squiggly lines
676
list.add(SQUIGGLES);
677
678             // advanced styles
679
if (item.textStyleKey != null) {
680                 list.add(UNDERLINE);
681                 list.add(BOX);
682                 list.add(DASHED_BOX);
683                 list.add(IBEAM);
684             }
685
686             // set selection
687
if (selection == null) {
688                 String JavaDoc val= item.textStyleKey == null ? SQUIGGLES[1] : fStore.getString(item.textStyleKey);
689                 for (Iterator JavaDoc iter= list.iterator(); iter.hasNext();) {
690                     String JavaDoc[] elem= (String JavaDoc[]) iter.next();
691                     if (elem[1].equals(val)) {
692                         selection= elem;
693                         break;
694                     }
695                 }
696             }
697
698             fDecorationViewer.setInput(list.toArray(new Object JavaDoc[list.size()]));
699             if (selection != null)
700                 fDecorationViewer.setSelection(new StructuredSelection((Object JavaDoc) selection), true);
701         }
702     }
703 }
704
Popular Tags