001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.editors.text;
011:
012: import com.ibm.icu.text.Collator;
013: import java.util.ArrayList;
014: import java.util.Collections;
015: import java.util.Comparator;
016: import java.util.HashMap;
017: import java.util.HashSet;
018: import java.util.Iterator;
019: import java.util.Map;
020: import java.util.Set;
021:
022: import org.eclipse.swt.SWT;
023: import org.eclipse.swt.events.SelectionEvent;
024: import org.eclipse.swt.events.SelectionListener;
025: import org.eclipse.swt.graphics.Color;
026: import org.eclipse.swt.graphics.Image;
027: import org.eclipse.swt.graphics.ImageData;
028: import org.eclipse.swt.graphics.RGB;
029: import org.eclipse.swt.layout.GridData;
030: import org.eclipse.swt.layout.GridLayout;
031: import org.eclipse.swt.widgets.Button;
032: import org.eclipse.swt.widgets.Composite;
033: import org.eclipse.swt.widgets.Control;
034: import org.eclipse.swt.widgets.Display;
035: import org.eclipse.swt.widgets.Label;
036:
037: import org.eclipse.core.runtime.Assert;
038:
039: import org.eclipse.jface.preference.ColorSelector;
040: import org.eclipse.jface.preference.PreferenceConverter;
041: import org.eclipse.jface.resource.ImageDescriptor;
042: import org.eclipse.jface.resource.ImageRegistry;
043: import org.eclipse.jface.viewers.ArrayContentProvider;
044: import org.eclipse.jface.viewers.ComboViewer;
045: import org.eclipse.jface.viewers.IColorProvider;
046: import org.eclipse.jface.viewers.ISelectionChangedListener;
047: import org.eclipse.jface.viewers.IStructuredContentProvider;
048: import org.eclipse.jface.viewers.IStructuredSelection;
049: import org.eclipse.jface.viewers.LabelProvider;
050: import org.eclipse.jface.viewers.SelectionChangedEvent;
051: import org.eclipse.jface.viewers.StructuredSelection;
052: import org.eclipse.jface.viewers.StructuredViewer;
053: import org.eclipse.jface.viewers.TableViewer;
054: import org.eclipse.jface.viewers.Viewer;
055:
056: import org.eclipse.ui.ISharedImages;
057: import org.eclipse.ui.PlatformUI;
058: import org.eclipse.ui.ide.IDE;
059: import org.eclipse.ui.texteditor.AnnotationPreference;
060: import org.eclipse.ui.texteditor.MarkerAnnotationPreferences;
061:
062: /**
063: * Configures Annotation preferences.
064: *
065: * @since 3.0
066: */
067: class AnnotationsConfigurationBlock implements
068: IPreferenceConfigurationBlock {
069: private static final class ListItem {
070: final String label;
071: final Image image;
072: final String colorKey;
073: final String highlightKey;
074: final String overviewRulerKey;
075: final String textStyleKey;
076: final String textKey;
077: final String verticalRulerKey;
078:
079: ListItem(String label, Image image, String colorKey,
080: String textKey, String overviewRulerKey,
081: String highlightKey, String verticalRulerKey,
082: String textStyleKey) {
083: this .label = label;
084: this .image = image;
085: this .colorKey = colorKey;
086: this .highlightKey = highlightKey;
087: this .overviewRulerKey = overviewRulerKey;
088: this .textKey = textKey;
089: this .textStyleKey = textStyleKey;
090: this .verticalRulerKey = verticalRulerKey;
091: }
092: }
093:
094: private static final class ItemContentProvider implements
095: IStructuredContentProvider {
096:
097: public Object[] getElements(Object inputElement) {
098: return (ListItem[]) inputElement;
099: }
100:
101: public void dispose() {
102: }
103:
104: public void inputChanged(Viewer viewer, Object oldInput,
105: Object newInput) {
106: }
107: }
108:
109: private final class ItemLabelProvider extends LabelProvider
110: implements IColorProvider {
111:
112: public String getText(Object element) {
113: return ((ListItem) element).label;
114: }
115:
116: public Image getImage(Object element) {
117: ListItem item = (ListItem) element;
118: if (item.verticalRulerKey != null
119: && fStore.getBoolean(item.verticalRulerKey))
120: return item.image;
121:
122: return null; // don't show icon if preference is not to show in vertical ruler
123: }
124:
125: public Color getForeground(Object element) {
126: return null;
127: }
128:
129: /*
130: * @see org.eclipse.jface.viewers.IColorProvider#getBackground(java.lang.Object)
131: */
132: public Color getBackground(Object element) {
133: String key = ((ListItem) element).highlightKey;
134: if (key != null && fStore.getBoolean(key)) {
135: RGB color = PreferenceConverter.getColor(fStore,
136: ((ListItem) element).colorKey);
137: color = interpolate(color, new RGB(255, 255, 255), 0.6);
138: return EditorsPlugin.getDefault().getSharedTextColors()
139: .getColor(color);
140: }
141: return null;
142: }
143:
144: /**
145: * Returns a specification of a color that lies between the given
146: * foreground and background color using the given scale factor.
147: *
148: * @param fg the foreground color
149: * @param bg the background color
150: * @param scale the scale factor
151: * @return the interpolated color
152: */
153: private RGB interpolate(RGB fg, RGB bg, double scale) {
154: return new RGB((int) ((1.0 - scale) * fg.red + scale
155: * bg.red), (int) ((1.0 - scale) * fg.green + scale
156: * bg.green), (int) ((1.0 - scale) * fg.blue + scale
157: * bg.blue));
158: }
159: }
160:
161: private static class ArrayLabelProvider extends LabelProvider {
162: public String getText(Object element) {
163: return ((String[]) element)[0].toString();
164: }
165: }
166:
167: /* copied from DefaultMarkerAnnotationAccess */
168: public static final String ERROR_SYSTEM_IMAGE = "error"; //$NON-NLS-1$
169: public static final String WARNING_SYSTEM_IMAGE = "warning"; //$NON-NLS-1$
170: public static final String INFO_SYSTEM_IMAGE = "info"; //$NON-NLS-1$
171: public static final String TASK_SYSTEM_IMAGE = "task"; //$NON-NLS-1$
172: public static final String BOOKMARK_SYSTEM_IMAGE = "bookmark"; //$NON-NLS-1$
173:
174: private final static Map MAPPING;
175:
176: static {
177: MAPPING = new HashMap();
178: MAPPING.put(ERROR_SYSTEM_IMAGE,
179: ISharedImages.IMG_OBJS_ERROR_TSK);
180: MAPPING.put(WARNING_SYSTEM_IMAGE,
181: ISharedImages.IMG_OBJS_WARN_TSK);
182: MAPPING.put(INFO_SYSTEM_IMAGE, ISharedImages.IMG_OBJS_INFO_TSK);
183: MAPPING.put(TASK_SYSTEM_IMAGE,
184: IDE.SharedImages.IMG_OBJS_TASK_TSK);
185: MAPPING.put(BOOKMARK_SYSTEM_IMAGE,
186: IDE.SharedImages.IMG_OBJS_BKMRK_TSK);
187: }
188:
189: final static String[] HIGHLIGHT = new String[] {
190: TextEditorMessages.AnnotationsConfigurationBlock_HIGHLIGHT,
191: "not used" }; //$NON-NLS-1$
192: final static String[] UNDERLINE = new String[] {
193: TextEditorMessages.AnnotationsConfigurationBlock_UNDERLINE,
194: AnnotationPreference.STYLE_UNDERLINE };
195: final static String[] BOX = new String[] {
196: TextEditorMessages.AnnotationsConfigurationBlock_BOX,
197: AnnotationPreference.STYLE_BOX };
198: final static String[] DASHED_BOX = new String[] {
199: TextEditorMessages.AnnotationsConfigurationBlock_DASHED_BOX,
200: AnnotationPreference.STYLE_DASHED_BOX };
201: final static String[] IBEAM = new String[] {
202: TextEditorMessages.AnnotationsConfigurationBlock_IBEAM,
203: AnnotationPreference.STYLE_IBEAM };
204: final static String[] SQUIGGLES = new String[] {
205: TextEditorMessages.AnnotationsConfigurationBlock_SQUIGGLES,
206: AnnotationPreference.STYLE_SQUIGGLES };
207:
208: private OverlayPreferenceStore fStore;
209: private ColorSelector fAnnotationForegroundColorEditor;
210:
211: private Button fShowInTextCheckBox;
212: private Button fShowInOverviewRulerCheckBox;
213: private Button fShowInVerticalRulerCheckBox;
214:
215: private StructuredViewer fAnnotationTypeViewer;
216: private final ListItem[] fListModel;
217:
218: private ComboViewer fDecorationViewer;
219: private final Set fImageKeys = new HashSet();
220:
221: public AnnotationsConfigurationBlock(OverlayPreferenceStore store) {
222: Assert.isNotNull(store);
223: MarkerAnnotationPreferences markerAnnotationPreferences = EditorsPlugin
224: .getDefault().getMarkerAnnotationPreferences();
225: fStore = store;
226: fStore
227: .addKeys(createOverlayStoreKeys(markerAnnotationPreferences));
228: fListModel = createAnnotationTypeListModel(markerAnnotationPreferences);
229: }
230:
231: private OverlayPreferenceStore.OverlayKey[] createOverlayStoreKeys(
232: MarkerAnnotationPreferences preferences) {
233:
234: ArrayList overlayKeys = new ArrayList();
235: Iterator e = preferences.getAnnotationPreferences().iterator();
236:
237: while (e.hasNext()) {
238: AnnotationPreference info = (AnnotationPreference) e.next();
239: overlayKeys.add(new OverlayPreferenceStore.OverlayKey(
240: OverlayPreferenceStore.STRING, info
241: .getColorPreferenceKey()));
242: overlayKeys.add(new OverlayPreferenceStore.OverlayKey(
243: OverlayPreferenceStore.BOOLEAN, info
244: .getTextPreferenceKey()));
245: if (info.getHighlightPreferenceKey() != null)
246: overlayKeys.add(new OverlayPreferenceStore.OverlayKey(
247: OverlayPreferenceStore.BOOLEAN, info
248: .getHighlightPreferenceKey()));
249: overlayKeys.add(new OverlayPreferenceStore.OverlayKey(
250: OverlayPreferenceStore.BOOLEAN, info
251: .getOverviewRulerPreferenceKey()));
252: if (info.getVerticalRulerPreferenceKey() != null)
253: overlayKeys.add(new OverlayPreferenceStore.OverlayKey(
254: OverlayPreferenceStore.BOOLEAN, info
255: .getVerticalRulerPreferenceKey()));
256: if (info.getTextStylePreferenceKey() != null)
257: overlayKeys.add(new OverlayPreferenceStore.OverlayKey(
258: OverlayPreferenceStore.STRING, info
259: .getTextStylePreferenceKey()));
260: }
261: OverlayPreferenceStore.OverlayKey[] keys = new OverlayPreferenceStore.OverlayKey[overlayKeys
262: .size()];
263: overlayKeys.toArray(keys);
264: return keys;
265: }
266:
267: /*
268: * @see org.eclipse.ui.internal.editors.text.IPreferenceConfigurationBlock#createControl(org.eclipse.swt.widgets.Composite)
269: */
270: public Control createControl(Composite parent) {
271:
272: PixelConverter pixelConverter = new PixelConverter(parent);
273:
274: Composite composite = new Composite(parent, SWT.NULL);
275: GridLayout layout = new GridLayout();
276: layout.numColumns = 2;
277: layout.marginHeight = 0;
278: layout.marginWidth = 0;
279: composite.setLayout(layout);
280:
281: Label label = new Label(composite, SWT.LEFT);
282: label
283: .setText(TextEditorMessages.AnnotationsConfigurationBlock_annotationPresentationOptions);
284: GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
285: gd.horizontalSpan = 2;
286: label.setLayoutData(gd);
287:
288: Composite editorComposite = new Composite(composite, SWT.NONE);
289: layout = new GridLayout();
290: layout.numColumns = 2;
291: layout.marginHeight = 0;
292: layout.marginWidth = 0;
293: editorComposite.setLayout(layout);
294: gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL
295: | GridData.FILL_VERTICAL);
296: gd.horizontalSpan = 2;
297: editorComposite.setLayoutData(gd);
298:
299: fAnnotationTypeViewer = new TableViewer(editorComposite,
300: SWT.SINGLE | SWT.V_SCROLL | SWT.BORDER
301: | SWT.FULL_SELECTION);
302: fAnnotationTypeViewer.setLabelProvider(new ItemLabelProvider());
303: fAnnotationTypeViewer
304: .setContentProvider(new ItemContentProvider());
305: gd = new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false);
306: gd.heightHint = pixelConverter.convertHeightInCharsToPixels(20);
307: fAnnotationTypeViewer.getControl().setLayoutData(gd);
308:
309: Composite optionsComposite = new Composite(editorComposite,
310: SWT.NONE);
311: layout = new GridLayout();
312: layout.marginHeight = 0;
313: layout.marginWidth = 0;
314: layout.numColumns = 2;
315: optionsComposite.setLayout(layout);
316: optionsComposite
317: .setLayoutData(new GridData(GridData.FILL_BOTH));
318:
319: label = new Label(optionsComposite, SWT.LEFT);
320: label
321: .setText(TextEditorMessages.AnnotationsConfigurationBlock_labels_showIn);
322: gd = new GridData(GridData.FILL_HORIZONTAL);
323: gd.horizontalAlignment = GridData.BEGINNING;
324: gd.horizontalSpan = 2;
325: label.setLayoutData(gd);
326:
327: // we only allow to set either "show in text" or "highlight in text", but not both
328:
329: fShowInTextCheckBox = new Button(optionsComposite, SWT.CHECK);
330: fShowInTextCheckBox
331: .setText(TextEditorMessages.AnnotationsConfigurationBlock_showInText);
332: gd = new GridData(GridData.FILL_HORIZONTAL);
333: gd.horizontalAlignment = GridData.BEGINNING;
334: gd.horizontalIndent = 20;
335: fShowInTextCheckBox.setLayoutData(gd);
336:
337: fDecorationViewer = new ComboViewer(optionsComposite,
338: SWT.READ_ONLY);
339: fDecorationViewer
340: .setContentProvider(new ArrayContentProvider());
341: fDecorationViewer.setLabelProvider(new ArrayLabelProvider());
342: gd = new GridData(GridData.FILL_HORIZONTAL);
343: gd.horizontalAlignment = GridData.BEGINNING;
344: fDecorationViewer.getControl().setLayoutData(gd);
345: fDecorationViewer.setInput(new Object[] { HIGHLIGHT, SQUIGGLES,
346: BOX, DASHED_BOX, UNDERLINE, IBEAM });
347:
348: fShowInOverviewRulerCheckBox = new Button(optionsComposite,
349: SWT.CHECK);
350: fShowInOverviewRulerCheckBox
351: .setText(TextEditorMessages.AnnotationsConfigurationBlock_showInOverviewRuler);
352: gd = new GridData(GridData.FILL_HORIZONTAL);
353: gd.horizontalAlignment = GridData.BEGINNING;
354: gd.horizontalSpan = 2;
355: gd.horizontalIndent = 20;
356: fShowInOverviewRulerCheckBox.setLayoutData(gd);
357:
358: fShowInVerticalRulerCheckBox = new Button(optionsComposite,
359: SWT.CHECK);
360: fShowInVerticalRulerCheckBox
361: .setText(TextEditorMessages.AnnotationsConfigurationBlock_showInVerticalRuler);
362: gd = new GridData(GridData.FILL_HORIZONTAL);
363: gd.horizontalAlignment = GridData.BEGINNING;
364: gd.horizontalSpan = 2;
365: gd.horizontalIndent = 20;
366: fShowInVerticalRulerCheckBox.setLayoutData(gd);
367:
368: label = new Label(optionsComposite, SWT.LEFT);
369: label
370: .setText(TextEditorMessages.AnnotationsConfigurationBlock_color);
371: gd = new GridData();
372: gd.horizontalAlignment = GridData.BEGINNING;
373: gd.horizontalIndent = 20;
374: label.setLayoutData(gd);
375:
376: fAnnotationForegroundColorEditor = new ColorSelector(
377: optionsComposite);
378: Button foregroundColorButton = fAnnotationForegroundColorEditor
379: .getButton();
380: gd = new GridData(GridData.FILL_HORIZONTAL);
381: gd.horizontalAlignment = GridData.BEGINNING;
382: foregroundColorButton.setLayoutData(gd);
383:
384: fAnnotationTypeViewer
385: .addSelectionChangedListener(new ISelectionChangedListener() {
386: public void selectionChanged(
387: SelectionChangedEvent event) {
388: handleAnnotationListSelection();
389: }
390: });
391:
392: fShowInTextCheckBox
393: .addSelectionListener(new SelectionListener() {
394: public void widgetDefaultSelected(SelectionEvent e) {
395: // do nothing
396: }
397:
398: public void widgetSelected(SelectionEvent e) {
399: ListItem item = getSelectedItem();
400: final boolean value = fShowInTextCheckBox
401: .getSelection();
402: if (value) {
403: // enable whatever is in the combo
404: String[] decoration = (String[]) ((IStructuredSelection) fDecorationViewer
405: .getSelection()).getFirstElement();
406: if (HIGHLIGHT.equals(decoration))
407: fStore
408: .setValue(item.highlightKey,
409: true);
410: else
411: fStore.setValue(item.textKey, true);
412: } else {
413: // disable both
414: if (item.textKey != null)
415: fStore.setValue(item.textKey, false);
416: if (item.highlightKey != null)
417: fStore.setValue(item.highlightKey,
418: false);
419: }
420: fStore.setValue(item.textKey, value);
421: updateDecorationViewer(item, false);
422: fAnnotationTypeViewer.refresh(item);
423: }
424: });
425:
426: fShowInOverviewRulerCheckBox
427: .addSelectionListener(new SelectionListener() {
428: public void widgetDefaultSelected(SelectionEvent e) {
429: // do nothing
430: }
431:
432: public void widgetSelected(SelectionEvent e) {
433: ListItem item = getSelectedItem();
434: fStore.setValue(item.overviewRulerKey,
435: fShowInOverviewRulerCheckBox
436: .getSelection());
437: fAnnotationTypeViewer.refresh(item);
438: }
439: });
440:
441: fShowInVerticalRulerCheckBox
442: .addSelectionListener(new SelectionListener() {
443: public void widgetDefaultSelected(SelectionEvent e) {
444: // do nothing
445: }
446:
447: public void widgetSelected(SelectionEvent e) {
448: ListItem item = getSelectedItem();
449: fStore.setValue(item.verticalRulerKey,
450: fShowInVerticalRulerCheckBox
451: .getSelection());
452: fAnnotationTypeViewer.refresh(item);
453: }
454: });
455:
456: foregroundColorButton
457: .addSelectionListener(new SelectionListener() {
458: public void widgetDefaultSelected(SelectionEvent e) {
459: // do nothing
460: }
461:
462: public void widgetSelected(SelectionEvent e) {
463: ListItem item = getSelectedItem();
464: PreferenceConverter.setValue(fStore,
465: item.colorKey,
466: fAnnotationForegroundColorEditor
467: .getColorValue());
468: fAnnotationTypeViewer.refresh(item);
469: }
470: });
471:
472: fDecorationViewer
473: .addSelectionChangedListener(new ISelectionChangedListener() {
474:
475: /*
476: * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
477: */
478: public void selectionChanged(
479: SelectionChangedEvent event) {
480: String[] decoration = (String[]) ((IStructuredSelection) fDecorationViewer
481: .getSelection()).getFirstElement();
482: ListItem item = getSelectedItem();
483:
484: if (fShowInTextCheckBox.getSelection()) {
485: if (HIGHLIGHT.equals(decoration)) {
486: fStore
487: .setValue(item.highlightKey,
488: true);
489: if (item.textKey != null) {
490: fStore
491: .setValue(item.textKey,
492: false);
493: if (item.textStyleKey != null)
494: fStore
495: .setValue(
496: item.textStyleKey,
497: AnnotationPreference.STYLE_NONE);
498: }
499: } else {
500: if (item.highlightKey != null)
501: fStore.setValue(item.highlightKey,
502: false);
503: if (item.textKey != null) {
504: fStore.setValue(item.textKey, true);
505: if (item.textStyleKey != null)
506: fStore.setValue(
507: item.textStyleKey,
508: decoration[1]);
509: }
510: }
511: }
512:
513: fAnnotationTypeViewer.refresh(item);
514: }
515: });
516:
517: composite.layout();
518: return composite;
519: }
520:
521: /*
522: * @see org.eclipse.ui.internal.editors.text.IPreferenceConfigurationBlock#canPerformOk()
523: */
524: public boolean canPerformOk() {
525: return true;
526: }
527:
528: /*
529: * @see PreferencePage#performOk()
530: */
531: public void performOk() {
532: }
533:
534: /*
535: * @see PreferencePage#performDefaults()
536: */
537: public void performDefaults() {
538: fStore.loadDefaults();
539: fAnnotationTypeViewer.refresh();
540: handleAnnotationListSelection();
541: }
542:
543: private void handleAnnotationListSelection() {
544: ListItem item = getSelectedItem();
545:
546: RGB rgb = PreferenceConverter.getColor(fStore, item.colorKey);
547: fAnnotationForegroundColorEditor.setColorValue(rgb);
548:
549: boolean highlight = item.highlightKey == null ? false : fStore
550: .getBoolean(item.highlightKey);
551: boolean showInText = item.textKey == null ? false : fStore
552: .getBoolean(item.textKey);
553: fShowInTextCheckBox.setSelection(showInText || highlight);
554:
555: updateDecorationViewer(item, true);
556:
557: fShowInOverviewRulerCheckBox.setSelection(fStore
558: .getBoolean(item.overviewRulerKey));
559:
560: if (item.verticalRulerKey != null) {
561: fShowInVerticalRulerCheckBox.setSelection(fStore
562: .getBoolean(item.verticalRulerKey));
563: fShowInVerticalRulerCheckBox.setEnabled(true);
564: } else {
565: fShowInVerticalRulerCheckBox.setSelection(true);
566: fShowInVerticalRulerCheckBox.setEnabled(false);
567: }
568: }
569:
570: public void initialize() {
571:
572: fAnnotationTypeViewer.setInput(fListModel);
573: fAnnotationTypeViewer.getControl().getDisplay().asyncExec(
574: new Runnable() {
575: public void run() {
576: if (fAnnotationTypeViewer != null
577: && !fAnnotationTypeViewer.getControl()
578: .isDisposed()) {
579: fAnnotationTypeViewer
580: .setSelection(new StructuredSelection(
581: fListModel[0]));
582: }
583: }
584: });
585:
586: }
587:
588: private ListItem[] createAnnotationTypeListModel(
589: MarkerAnnotationPreferences preferences) {
590: ArrayList listModelItems = new ArrayList();
591: Iterator e = preferences.getAnnotationPreferences().iterator();
592:
593: while (e.hasNext()) {
594: AnnotationPreference info = (AnnotationPreference) e.next();
595: if (info.isIncludeOnPreferencePage()) {
596: String label = info.getPreferenceLabel();
597: if (containsMoreThanOne(preferences
598: .getAnnotationPreferences().iterator(), label))
599: label += " (" + info.getAnnotationType() + ")"; //$NON-NLS-1$//$NON-NLS-2$
600:
601: Image image = getImage(info);
602:
603: listModelItems.add(new ListItem(label, image, info
604: .getColorPreferenceKey(), info
605: .getTextPreferenceKey(), info
606: .getOverviewRulerPreferenceKey(), info
607: .getHighlightPreferenceKey(), info
608: .getVerticalRulerPreferenceKey(), info
609: .getTextStylePreferenceKey()));
610: }
611: }
612:
613: Comparator comparator = new Comparator() {
614: /*
615: * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
616: */
617: public int compare(Object o1, Object o2) {
618: if (!(o2 instanceof ListItem))
619: return -1;
620: if (!(o1 instanceof ListItem))
621: return 1;
622:
623: String label1 = ((ListItem) o1).label;
624: String label2 = ((ListItem) o2).label;
625:
626: return Collator.getInstance().compare(label1, label2);
627:
628: }
629: };
630: Collections.sort(listModelItems, comparator);
631:
632: ListItem[] items = new ListItem[listModelItems.size()];
633: listModelItems.toArray(items);
634: return items;
635: }
636:
637: /**
638: * Returns the image for the given annotation and the given annotation preferences or
639: * <code>null</code> if there is no such image.
640: *
641: * @param preference the annotation preference
642: * @return the image or <code>null</code>
643: * @since 3.1
644: */
645: private Image getImage(AnnotationPreference preference) {
646:
647: ImageRegistry registry = EditorsPlugin.getDefault()
648: .getImageRegistry();
649:
650: String annotationType = (String) preference.getAnnotationType();
651: if (annotationType == null)
652: return null;
653:
654: String customImage = annotationType
655: + "__AnnotationsConfigurationBlock_Image"; //$NON-NLS-1$
656:
657: Image image;
658: image = registry.get(customImage);
659: if (image != null)
660: return image;
661:
662: image = registry.get(annotationType);
663: if (image == null) {
664: ImageDescriptor descriptor = preference
665: .getImageDescriptor();
666: if (descriptor != null) {
667: registry.put(annotationType, descriptor);
668: image = registry.get(annotationType);
669: } else {
670: String key = translateSymbolicImageName(preference
671: .getSymbolicImageName());
672: if (key != null) {
673: ISharedImages sharedImages = PlatformUI
674: .getWorkbench().getSharedImages();
675: image = sharedImages.getImage(key);
676: }
677: }
678: }
679:
680: if (image == null)
681: return image;
682:
683: // create custom image
684: final int SIZE = 16; // square images
685: ImageData data = image.getImageData();
686: Image copy;
687: if (data.height > SIZE || data.width > SIZE) {
688: // scale down to icon size
689: copy = new Image(Display.getCurrent(), data.scaledTo(SIZE,
690: SIZE));
691: } else {
692: // don't scale up, but rather copy into the middle and mark everything else transparent
693: ImageData mask = data.getTransparencyMask();
694: ImageData resized = new ImageData(SIZE, SIZE, data.depth,
695: data.palette);
696: ImageData resizedMask = new ImageData(SIZE, SIZE,
697: mask.depth, mask.palette);
698:
699: int xo = Math.max(0, (SIZE - data.width) / 2);
700: int yo = Math.max(0, (SIZE - data.height) / 2);
701:
702: for (int y = 0; y < SIZE; y++) {
703: for (int x = 0; x < SIZE; x++) {
704: if (y >= yo && x >= xo && y < yo + data.height
705: && x < xo + data.width) {
706: resized.setPixel(x, y, data.getPixel(x - xo, y
707: - yo));
708: resizedMask.setPixel(x, y, mask.getPixel(
709: x - xo, y - yo));
710: }
711: }
712: }
713:
714: copy = new Image(Display.getCurrent(), resized, resizedMask);
715: }
716:
717: fImageKeys.add(customImage);
718: registry.put(customImage, copy);
719: return copy;
720: }
721:
722: /**
723: * Translates the given symbolic image name into the according symbolic image name
724: * the {@link org.eclipse.ui.ISharedImages} understands.
725: *
726: * @param symbolicImageName the symbolic system image name to be translated
727: * @return the shared image name
728: * @since 3.1
729: */
730: private String translateSymbolicImageName(String symbolicImageName) {
731: return (String) MAPPING.get(symbolicImageName);
732: }
733:
734: private boolean containsMoreThanOne(
735: Iterator annotationPrefernceIterator, String label) {
736: if (label == null)
737: return false;
738:
739: int count = 0;
740: while (annotationPrefernceIterator.hasNext()) {
741: if (label
742: .equals(((AnnotationPreference) annotationPrefernceIterator
743: .next()).getPreferenceLabel()))
744: count++;
745:
746: if (count == 2)
747: return true;
748: }
749: return false;
750: }
751:
752: /*
753: * @see IPreferenceConfigurationBlock#dispose()
754: */
755: public void dispose() {
756: ImageRegistry registry = EditorsPlugin.getDefault()
757: .getImageRegistry();
758:
759: for (Iterator it = fImageKeys.iterator(); it.hasNext();) {
760: String string = (String) it.next();
761: registry.remove(string);
762: }
763:
764: fImageKeys.clear();
765: }
766:
767: private ListItem getSelectedItem() {
768: return (ListItem) ((IStructuredSelection) fAnnotationTypeViewer
769: .getSelection()).getFirstElement();
770: }
771:
772: private void updateDecorationViewer(ListItem item, boolean changed) {
773: // decoration selection: if the checkbox is enabled, there is
774: // only one case where the combo is not enabled: if both the highlight and textStyle keys are null
775: final boolean enabled = fShowInTextCheckBox.getSelection()
776: && !(item.highlightKey == null && item.textStyleKey == null);
777: fDecorationViewer.getControl().setEnabled(enabled);
778:
779: if (changed) {
780: String[] selection = null;
781: ArrayList list = new ArrayList();
782:
783: // highlighting
784: if (item.highlightKey != null) {
785: list.add(HIGHLIGHT);
786: if (fStore.getBoolean(item.highlightKey))
787: selection = HIGHLIGHT;
788: }
789:
790: // legacy default= squiggly lines
791: list.add(SQUIGGLES);
792:
793: // advanced styles
794: if (item.textStyleKey != null) {
795: list.add(UNDERLINE);
796: list.add(BOX);
797: list.add(DASHED_BOX);
798: list.add(IBEAM);
799: }
800:
801: // set selection
802: if (selection == null) {
803: String val = item.textStyleKey == null ? SQUIGGLES[1]
804: : fStore.getString(item.textStyleKey);
805: for (Iterator iter = list.iterator(); iter.hasNext();) {
806: String[] elem = (String[]) iter.next();
807: if (elem[1].equals(val)) {
808: selection = elem;
809: break;
810: }
811: }
812: }
813:
814: fDecorationViewer.setInput(list.toArray(new Object[list
815: .size()]));
816: if (selection != null)
817: fDecorationViewer.setSelection(new StructuredSelection(
818: (Object) selection), true);
819: }
820: }
821: }
|