KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > preferences > LinkedModeConfigurationBlock


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.jdt.internal.ui.preferences;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
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 /**
62  * Configures the linked mode preferences. The preferences belong to
63  * org.eclipse.ui.editors. However, as they are chiefly used in the java editor,
64  * we keep the preferences here for the time being.
65  *
66  * @since 3.1
67  */

68 class LinkedModeConfigurationBlock extends AbstractConfigurationBlock {
69
70     private static final String JavaDoc EXIT= "org.eclipse.jdt.ui.link.exit"; //$NON-NLS-1$
71
private static final String JavaDoc TARGET= "org.eclipse.jdt.ui.link.target"; //$NON-NLS-1$
72
private static final String JavaDoc MASTER= "org.eclipse.jdt.ui.link.master"; //$NON-NLS-1$
73
private static final String JavaDoc SLAVE= "org.eclipse.jdt.ui.link.slave"; //$NON-NLS-1$
74

75     private static final class ListItem {
76         final String JavaDoc label;
77         final Image image;
78         final String JavaDoc colorKey;
79         final String JavaDoc highlightKey;
80         final String JavaDoc overviewRulerKey;
81         final String JavaDoc textStyleKey;
82         final String JavaDoc textKey;
83         final String JavaDoc verticalRulerKey;
84         final List JavaDoc validStyles;
85
86         ListItem(String JavaDoc label, Image image, String JavaDoc colorKey, String JavaDoc textKey, String JavaDoc overviewRulerKey, String JavaDoc highlightKey, String JavaDoc verticalRulerKey, String JavaDoc textStyleKey, List JavaDoc 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 JavaDoc[] getElements(Object JavaDoc inputElement) {
102             return (ListItem[]) inputElement;
103         }
104
105         public void dispose() {
106         }
107
108         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
109         }
110     }
111     
112     private final class ItemLabelProvider extends LabelProvider {
113         
114         public String JavaDoc getText(Object JavaDoc element) {
115             return ((ListItem) element).label;
116         }
117     }
118     
119     private static class ArrayLabelProvider extends LabelProvider {
120         public String JavaDoc getText(Object JavaDoc element) {
121             return ((String JavaDoc[]) element)[0].toString();
122         }
123     }
124     
125     final static String JavaDoc[] HIGHLIGHT= new String JavaDoc[] {PreferencesMessages.LinkedModeConfigurationBlock_HIGHLIGHT, "unused"}; //$NON-NLS-1$
126
final static String JavaDoc[] UNDERLINE= new String JavaDoc[] {PreferencesMessages.LinkedModeConfigurationBlock_UNDERLINE, AnnotationPreference.STYLE_UNDERLINE};
127     final static String JavaDoc[] BOX= new String JavaDoc[] {PreferencesMessages.LinkedModeConfigurationBlock_BOX, AnnotationPreference.STYLE_BOX};
128     final static String JavaDoc[] IBEAM= new String JavaDoc[] {PreferencesMessages.LinkedModeConfigurationBlock_IBEAM, AnnotationPreference.STYLE_IBEAM};
129     final static String JavaDoc[] SQUIGGLES= new String JavaDoc[] {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 JavaDoc overlayKeys= new ArrayList JavaDoc();
150
151         Iterator JavaDoc 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 JavaDoc 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 JavaDoc listModelItems= new ArrayList JavaDoc();
178         Iterator JavaDoc e= preferences.getAnnotationPreferences().iterator();
179         
180         while (e.hasNext()) {
181             AnnotationPreference info= (AnnotationPreference) e.next();
182             if (isLinkedModeAnnotation(info)) {
183                 String JavaDoc label= info.getPreferenceLabel();
184                 List JavaDoc 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 JavaDoc getStyles(Object JavaDoc type) {
196         if (type.equals(MASTER))
197             return Arrays.asList(new String JavaDoc[][] {BOX, HIGHLIGHT, UNDERLINE, SQUIGGLES});
198         if (type.equals(SLAVE))
199             return Arrays.asList(new String JavaDoc[][] {BOX, HIGHLIGHT, UNDERLINE, SQUIGGLES});
200         if (type.equals(TARGET))
201             return Arrays.asList(new String JavaDoc[][] {BOX, HIGHLIGHT, UNDERLINE, SQUIGGLES});
202         if (type.equals(EXIT))
203             return Arrays.asList(new String JavaDoc[][] {IBEAM});
204         return new ArrayList JavaDoc();
205     }
206
207     /**
208      * Creates page for hover preferences.
209      *
210      * @param parent the parent composite
211      * @return the control for the preference page
212      */

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         // we only allow to set either "show in text" or "highlight in text", but not both
254

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 JavaDoc[] {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                 // do nothing
292
}
293             
294             public void widgetSelected(SelectionEvent e) {
295                 ListItem item= getSelectedItem();
296                 final boolean value= fShowInTextCheckBox.getSelection();
297                 if (value) {
298                     // enable whatever is in the combo
299
String JavaDoc[] decoration= (String JavaDoc[]) ((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                     // disable both
306
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                 // do nothing
317
}
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             /*
328              * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
329              */

330             public void selectionChanged(SelectionChangedEvent event) {
331                 String JavaDoc[] decoration= (String JavaDoc[]) ((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     /**
353      * Returns the number of pixels corresponding to the width of the given
354      * number of characters.
355      * <p>
356      * This method may only be called after <code>initializeDialogUnits</code>
357      * has been called.
358      * </p>
359      * <p>
360      * Clients may call this framework method, but should not override it.
361      * </p>
362      *
363      * @param chars
364      * the number of characters
365      * @return the number of pixels
366      */

367     protected int convertWidthInCharsToPixels(int chars) {
368         // test for failure to initialize for backward compatibility
369
if (fFontMetrics == null)
370             return 0;
371         return Dialog.convertWidthInCharsToPixels(fFontMetrics, chars);
372     }
373
374     /**
375      * Returns the number of pixels corresponding to the height of the given
376      * number of characters.
377      * <p>
378      * This method may only be called after <code>initializeDialogUnits</code>
379      * has been called.
380      * </p>
381      * <p>
382      * Clients may call this framework method, but should not override it.
383      * </p>
384      *
385      * @param chars
386      * the number of characters
387      * @return the number of pixels
388      */

389     protected int convertHeightInCharsToPixels(int chars) {
390         // test for failure to initialize for backward compatibility
391
if (fFontMetrics == null)
392             return 0;
393         return Dialog.convertHeightInCharsToPixels(fFontMetrics, chars);
394     }
395     /**
396      * Initializes the computation of horizontal and vertical dialog units based
397      * on the size of current font.
398      * <p>
399      * This method must be called before any of the dialog unit based conversion
400      * methods are called.
401      * </p>
402      *
403      * @param testControl
404      * a control from which to obtain the current font
405      */

406     protected void initializeDialogUnits(Control testControl) {
407         // Compute and store a font metric
408
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     /*
428      * @see org.eclipse.jdt.internal.ui.preferences.IPreferenceConfigurationBlock#initialize()
429      */

430     public void initialize() {
431         super.initialize();
432         
433         fAnnotationTypeViewer.setInput(fListModel);
434         fAnnotationTypeViewer.getControl().getDisplay().asyncExec(new Runnable JavaDoc() {
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         // decoration selection: if the checkbox is enabled, there is
450
// only one case where the combo is not enabled: if both the highlight and textStyle keys are null
451
final boolean enabled= fShowInTextCheckBox.getSelection() && !(item.highlightKey == null && item.textStyleKey == null);
452         fDecorationViewer.getControl().setEnabled(enabled);
453         
454         if (changed) {
455             String JavaDoc[] selection= null;
456             ArrayList JavaDoc list= new ArrayList JavaDoc();
457             
458             list.addAll(item.validStyles);
459             
460             if (getPreferenceStore().getBoolean(item.highlightKey))
461                 selection= HIGHLIGHT;
462             
463             // set selection
464
if (selection == null) {
465                 String JavaDoc val= getPreferenceStore().getString(item.textStyleKey);
466                 for (Iterator JavaDoc iter= list.iterator(); iter.hasNext();) {
467                     String JavaDoc[] elem= (String JavaDoc[]) iter.next();
468                     if (elem[1].equals(val)) {
469                         selection= elem;
470                         break;
471                     }
472                 }
473             }
474             
475             fDecorationViewer.setInput(list.toArray(new Object JavaDoc[list.size()]));
476             if (selection == null)
477                 selection= (String JavaDoc[]) list.get(0);
478             fDecorationViewer.setSelection(new StructuredSelection((Object JavaDoc) 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         /*
500          * Only call super after updating fShowInTextCheckBox, so that
501          * the master-slave dependencies get properly updated.
502          */

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