KickJava   Java API By Example, From Geeks To Geeks.

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


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  * hiroyuki.inaba@jp.fujitsu.com (Hiroyuki Inaba) - https://bugs.eclipse.org/bugs/show_bug.cgi?id=82224
11  *******************************************************************************/

12
13 package org.eclipse.ui.internal.editors.text;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.SelectionEvent;
22 import org.eclipse.swt.events.SelectionListener;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Button;
26 import org.eclipse.swt.widgets.Combo;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.swt.widgets.Group;
30 import org.eclipse.swt.widgets.Label;
31
32 import org.eclipse.core.runtime.Assert;
33
34 import org.eclipse.jface.action.LegacyActionTools;
35 import org.eclipse.jface.preference.ColorSelector;
36 import org.eclipse.jface.preference.PreferenceConverter;
37
38 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
39 import org.eclipse.ui.texteditor.AnnotationPreference;
40 import org.eclipse.ui.texteditor.MarkerAnnotationPreferences;
41 import org.eclipse.ui.texteditor.quickdiff.QuickDiff;
42 import org.eclipse.ui.texteditor.quickdiff.ReferenceProviderDescriptor;
43 import org.eclipse.ui.texteditor.spelling.SpellingService;
44
45 /**
46  * Configures quick diff preferences
47  *
48  * @since 3.0
49  */

50 class QuickDiffConfigurationBlock implements IPreferenceConfigurationBlock {
51
52     private OverlayPreferenceStore fStore;
53
54     private Map JavaDoc fCheckBoxes= new HashMap JavaDoc();
55     private SelectionListener fCheckBoxListener= new SelectionListener() {
56         public void widgetDefaultSelected(SelectionEvent e) {
57         }
58         public void widgetSelected(SelectionEvent e) {
59             Button button= (Button) e.widget;
60             fStore.setValue((String JavaDoc) fCheckBoxes.get(button), button.getSelection());
61         }
62     };
63
64     /**
65      * List for the reference provider default.
66      * @since 3.0
67      */

68     private Combo fQuickDiffProviderCombo;
69     /**
70      * The reference provider default's list model.
71      * @since 3.0
72      */

73     private String JavaDoc[][] fQuickDiffProviderListModel;
74     /**
75      * The quick diff color model.
76      * @since 3.0
77      */

78     private String JavaDoc[][] fQuickDiffModel;
79     /**
80      * The color editors for quick diff.
81      * @since 3.0
82      */

83     private ColorSelector[] fQuickDiffColorEditors;
84     /**
85      * The checkbox for the quick diff overview ruler property.
86      * @since 3.0
87      */

88     private Button fQuickDiffOverviewRulerCheckBox;
89
90     /**
91      * The enablement checkbox.
92      * @since 3.3.
93      */

94     private Button fEnablementCheckbox;
95
96     
97     public QuickDiffConfigurationBlock(OverlayPreferenceStore store) {
98         Assert.isNotNull(store);
99         fStore= store;
100         MarkerAnnotationPreferences markerAnnotationPreferences= EditorsPlugin.getDefault().getMarkerAnnotationPreferences();
101         fStore.addKeys(createOverlayStoreKeys(markerAnnotationPreferences));
102         fQuickDiffModel= createQuickDiffModel(markerAnnotationPreferences);
103         fQuickDiffProviderListModel= createQuickDiffReferenceListModel();
104     }
105
106     private OverlayPreferenceStore.OverlayKey[] createOverlayStoreKeys(MarkerAnnotationPreferences preferences) {
107         ArrayList JavaDoc overlayKeys= new ArrayList JavaDoc();
108
109         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, AbstractDecoratedTextEditorPreferenceConstants.QUICK_DIFF_ALWAYS_ON));
110         overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, AbstractDecoratedTextEditorPreferenceConstants.QUICK_DIFF_DEFAULT_PROVIDER));
111
112         Iterator JavaDoc e= preferences.getAnnotationPreferences().iterator();
113         while (e.hasNext()) {
114             AnnotationPreference info= (AnnotationPreference) e.next();
115
116             if (info.getAnnotationType().equals("org.eclipse.ui.workbench.texteditor.quickdiffChange") //$NON-NLS-1$
117
|| (info.getAnnotationType().equals("org.eclipse.ui.workbench.texteditor.quickdiffAddition")) //$NON-NLS-1$
118
|| (info.getAnnotationType().equals("org.eclipse.ui.workbench.texteditor.quickdiffDeletion")) //$NON-NLS-1$
119
) {
120                 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, info.getColorPreferenceKey()));
121                 overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, info.getOverviewRulerPreferenceKey()));
122             }
123         }
124
125         OverlayPreferenceStore.OverlayKey[] keys= new OverlayPreferenceStore.OverlayKey[overlayKeys.size()];
126         overlayKeys.toArray(keys);
127         return keys;
128     }
129
130     private String JavaDoc[][] createQuickDiffModel(MarkerAnnotationPreferences preferences) {
131         String JavaDoc[][] items= new String JavaDoc[3][];
132
133         Iterator JavaDoc e= preferences.getAnnotationPreferences().iterator();
134         while (e.hasNext()) {
135             AnnotationPreference info= (AnnotationPreference) e.next();
136             if (info.getAnnotationType().equals("org.eclipse.ui.workbench.texteditor.quickdiffChange")) //$NON-NLS-1$
137
items[0]= new String JavaDoc[] { info.getColorPreferenceKey(), info.getOverviewRulerPreferenceKey(), TextEditorMessages.QuickDiffConfigurationBlock_changeColor };
138             else if (info.getAnnotationType().equals("org.eclipse.ui.workbench.texteditor.quickdiffAddition")) //$NON-NLS-1$
139
items[1]= new String JavaDoc[] { info.getColorPreferenceKey(), info.getOverviewRulerPreferenceKey(), TextEditorMessages.QuickDiffConfigurationBlock_additionColor };
140             else if (info.getAnnotationType().equals("org.eclipse.ui.workbench.texteditor.quickdiffDeletion")) //$NON-NLS-1$
141
items[2]= new String JavaDoc[] { info.getColorPreferenceKey(), info.getOverviewRulerPreferenceKey(), TextEditorMessages.QuickDiffConfigurationBlock_deletionColor };
142         }
143         return items;
144     }
145
146     private String JavaDoc[][] createQuickDiffReferenceListModel() {
147         java.util.List JavaDoc descriptors= new QuickDiff().getReferenceProviderDescriptors();
148         ArrayList JavaDoc listModelItems= new ArrayList JavaDoc();
149         for (Iterator JavaDoc it= descriptors.iterator(); it.hasNext();) {
150             ReferenceProviderDescriptor descriptor= (ReferenceProviderDescriptor) it.next();
151             String JavaDoc label= LegacyActionTools.removeMnemonics(descriptor.getLabel());
152             listModelItems.add(new String JavaDoc[] { descriptor.getId(), label });
153         }
154         String JavaDoc[][] items= new String JavaDoc[listModelItems.size()][];
155         listModelItems.toArray(items);
156         return items;
157     }
158
159     private Button addCheckBox(Composite parent, String JavaDoc label, String JavaDoc key, int indentation) {
160         Button checkBox= new Button(parent, SWT.CHECK);
161         checkBox.setText(label);
162
163         GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
164         gd.horizontalIndent= indentation;
165         gd.horizontalSpan= 2;
166         checkBox.setLayoutData(gd);
167         checkBox.addSelectionListener(fCheckBoxListener);
168
169         fCheckBoxes.put(checkBox, key);
170
171         return checkBox;
172     }
173
174     /**
175      * Creates page for hover preferences.
176      *
177      * @param parent the parent composite
178      * @return the created child composite
179      */

180     public Control createControl(Composite parent) {
181         Composite composite= new Composite(parent, SWT.NONE);
182         GridLayout layout= new GridLayout();
183         layout.numColumns= 2;
184         layout.marginHeight= 0;
185         layout.marginWidth= 0;
186         composite.setLayout(layout);
187
188         String JavaDoc label= TextEditorMessages.QuickDiffConfigurationBlock_showForNewEditors;
189         fEnablementCheckbox= addCheckBox(composite, label, AbstractDecoratedTextEditorPreferenceConstants.QUICK_DIFF_ALWAYS_ON, 0);
190         fEnablementCheckbox.addSelectionListener(new SelectionListener() {
191             public void widgetSelected(SelectionEvent e) {
192                 boolean enabled= fEnablementCheckbox.getSelection();
193                 fStore.setValue(SpellingService.PREFERENCE_SPELLING_ENABLED, enabled);
194                 updateEnablement();
195             }
196
197             public void widgetDefaultSelected(SelectionEvent e) {
198             }
199         });
200         
201         addFiller(composite);
202
203         label= TextEditorMessages.QuickDiffConfigurationBlock_showInOverviewRuler;
204         fQuickDiffOverviewRulerCheckBox= new Button(composite, SWT.CHECK);
205         fQuickDiffOverviewRulerCheckBox.setText(label);
206
207         GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
208         gd.horizontalIndent= 0;
209         gd.horizontalSpan= 2;
210         gd.horizontalIndent= 10;
211         fQuickDiffOverviewRulerCheckBox.setLayoutData(gd);
212         fQuickDiffOverviewRulerCheckBox.addSelectionListener(new SelectionListener() {
213             public void widgetSelected(SelectionEvent e) {
214                 for (int i= 0; i < fQuickDiffModel.length; i++) {
215                     fStore.setValue(fQuickDiffModel[i][1], fQuickDiffOverviewRulerCheckBox.getSelection());
216                 }
217             }
218
219             public void widgetDefaultSelected(SelectionEvent e) {
220             }
221         });
222
223         // spacer
224
Label l= new Label(composite, SWT.LEFT );
225         gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
226         gd.horizontalSpan= 2;
227         gd.horizontalIndent= 10;
228         gd.heightHint= 5;
229         l.setLayoutData(gd);
230
231         Group group= new Group(composite, SWT.NONE);
232         group.setText(TextEditorMessages.QuickDiffConfigurationBlock_colorTitle);
233         layout= new GridLayout();
234         layout.numColumns= 2;
235         group.setLayout(layout);
236         gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
237         gd.horizontalSpan= 2;
238         gd.horizontalIndent= 10;
239         group.setLayoutData(gd);
240
241         fQuickDiffColorEditors= new ColorSelector[3];
242         for (int i= 0; i < fQuickDiffModel.length; i++) {
243             label= fQuickDiffModel[i][2];
244             l= new Label(group, SWT.LEFT);
245             l.setText(label);
246             final ColorSelector editor= new ColorSelector(group);
247             fQuickDiffColorEditors[i]= editor;
248             Button changeColorButton= editor.getButton();
249             gd= new GridData(GridData.FILL_HORIZONTAL);
250             gd.horizontalAlignment= GridData.BEGINNING;
251             changeColorButton.setLayoutData(gd);
252             final int index= i;
253             changeColorButton.addSelectionListener(new SelectionListener() {
254                 public void widgetDefaultSelected(SelectionEvent e) {
255                     // do nothing
256
}
257
258                 public void widgetSelected(SelectionEvent e) {
259                     String JavaDoc key= fQuickDiffModel[index][0];
260                     PreferenceConverter.setValue(fStore, key, editor.getColorValue());
261                 }
262             });
263         }
264
265         addFiller(composite);
266
267         l= new Label(composite, SWT.LEFT);
268         l.setText(TextEditorMessages.QuickDiffConfigurationBlock_referenceProviderTitle);
269         gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
270         gd.horizontalSpan= 2;
271         gd.horizontalIndent= 10;
272         l.setLayoutData(gd);
273
274         Composite editorComposite= new Composite(composite, SWT.NONE);
275         layout= new GridLayout();
276         layout.numColumns= 2;
277         layout.marginHeight= 0;
278         layout.marginWidth= 0;
279         editorComposite.setLayout(layout);
280         gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.FILL_VERTICAL);
281         gd.horizontalSpan= 2;
282         gd.horizontalIndent= 10;
283         editorComposite.setLayoutData(gd);
284
285         fQuickDiffProviderCombo= new Combo(editorComposite, SWT.DROP_DOWN | SWT.READ_ONLY);
286         gd= new GridData(GridData.VERTICAL_ALIGN_BEGINNING | GridData.FILL_HORIZONTAL);
287         fQuickDiffProviderCombo.setLayoutData(gd);
288
289         Composite stylesComposite= new Composite(editorComposite, SWT.NONE);
290         layout= new GridLayout();
291         layout.marginHeight= 0;
292         layout.marginWidth= 0;
293         layout.numColumns= 2;
294         stylesComposite.setLayout(layout);
295         stylesComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
296
297         fQuickDiffProviderCombo.addSelectionListener(new SelectionListener() {
298             public void widgetDefaultSelected(SelectionEvent e) {
299                 // do nothing
300
}
301
302             public void widgetSelected(SelectionEvent e) {
303                 int i= fQuickDiffProviderCombo.getSelectionIndex();
304                 fStore.setValue(AbstractDecoratedTextEditorPreferenceConstants.QUICK_DIFF_DEFAULT_PROVIDER, fQuickDiffProviderListModel[i][0]);
305             }
306
307         });
308
309         return composite;
310     }
311     
312     private void addFiller(Composite composite) {
313         PixelConverter pixelConverter= new PixelConverter(composite);
314         
315         Label filler= new Label(composite, SWT.LEFT );
316         GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
317         gd.horizontalSpan= 2;
318         gd.heightHint= pixelConverter.convertHeightInCharsToPixels(1) / 2;
319         filler.setLayoutData(gd);
320     }
321     
322     private void updateEnablement() {
323         final boolean enabled= fEnablementCheckbox.getSelection();
324         fQuickDiffOverviewRulerCheckBox.setEnabled(enabled);
325         fQuickDiffProviderCombo.setEnabled(enabled);
326         for (int i= 0; i < fQuickDiffColorEditors.length; i++)
327             fQuickDiffColorEditors[i].setEnabled(enabled);
328     }
329
330     private void updateProviderList() {
331         String JavaDoc defaultProvider= fStore.getString(AbstractDecoratedTextEditorPreferenceConstants.QUICK_DIFF_DEFAULT_PROVIDER);
332         for (int j= 0; j < fQuickDiffProviderListModel.length; j++) {
333             if (defaultProvider.equals(fQuickDiffProviderListModel[j][0])) {
334                 fQuickDiffProviderCombo.select(j);
335             }
336         }
337         fQuickDiffProviderCombo.redraw();
338     }
339
340     public void initialize() {
341
342         for (int i= 0; i < fQuickDiffProviderListModel.length; i++) {
343             String JavaDoc label= fQuickDiffProviderListModel[i][1];
344             fQuickDiffProviderCombo.add(label);
345         }
346         fQuickDiffProviderCombo.getDisplay().asyncExec(new Runnable JavaDoc() {
347             public void run() {
348                 updateProviderList();
349             }
350         });
351
352         initializeFields();
353     }
354
355     private void initializeFields() {
356         Iterator JavaDoc e= fCheckBoxes.keySet().iterator();
357         while (e.hasNext()) {
358             Button b= (Button) e.next();
359             String JavaDoc key= (String JavaDoc) fCheckBoxes.get(b);
360             b.setSelection(fStore.getBoolean(key));
361         }
362
363         updateQuickDiffControls();
364     }
365
366     public boolean canPerformOk() {
367         return true;
368     }
369
370     public void performOk() {
371     }
372
373     public void performDefaults() {
374         initializeFields();
375         updateProviderList();
376     }
377
378     private void updateQuickDiffControls() {
379         boolean quickdiffOverviewRuler= false;
380         for (int i= 0; i < fQuickDiffModel.length; i++) {
381             fQuickDiffColorEditors[i].setColorValue(PreferenceConverter.getColor(fStore, fQuickDiffModel[i][0]));
382             quickdiffOverviewRuler |= fStore.getBoolean(fQuickDiffModel[i][1]);
383         }
384         fQuickDiffOverviewRulerCheckBox.setSelection(quickdiffOverviewRuler);
385     }
386
387     /*
388      * @see org.eclipse.ui.internal.editors.text.IPreferenceConfigurationBlock#dispose()
389      * @since 3.0
390      */

391     public void dispose() {
392     }
393 }
394
Popular Tags