KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > internal > ComparePreferencePage


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 package org.eclipse.compare.internal;
12
13 import java.io.*;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.ui.*;
19 import org.eclipse.ui.dialogs.PreferenceLinkArea;
20 import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;
21 import org.eclipse.ui.texteditor.AbstractTextEditor;
22
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.events.*;
25 import org.eclipse.swt.graphics.Image;
26 import org.eclipse.swt.layout.*;
27 import org.eclipse.swt.widgets.*;
28
29 import org.eclipse.jface.dialogs.Dialog;
30 import org.eclipse.jface.preference.*;
31 import org.eclipse.jface.util.*;
32
33 import org.eclipse.compare.*;
34 import org.eclipse.compare.contentmergeviewer.TextMergeViewer;
35 import org.eclipse.compare.structuremergeviewer.*;
36
37
38 public class ComparePreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
39     
40     class FakeInput implements ITypedElement, IEncodedStreamContentAccessor {
41         static final String JavaDoc UTF_16= "UTF-16"; //$NON-NLS-1$
42
String JavaDoc fContent;
43         
44         FakeInput(String JavaDoc name) {
45             fContent= loadPreviewContentFromFile(name);
46         }
47         public Image getImage() {
48             return null;
49         }
50         public String JavaDoc getName() {
51             return "no name"; //$NON-NLS-1$
52
}
53         public String JavaDoc getType() {
54             return "no type"; //$NON-NLS-1$
55
}
56         public InputStream getContents() {
57             return new ByteArrayInputStream(Utilities.getBytes(fContent, UTF_16));
58         }
59         public String JavaDoc getCharset() {
60             return UTF_16;
61         }
62     }
63
64     private static final String JavaDoc PREFIX= CompareUIPlugin.PLUGIN_ID + "."; //$NON-NLS-1$
65
public static final String JavaDoc OPEN_STRUCTURE_COMPARE= PREFIX + "OpenStructureCompare"; //$NON-NLS-1$
66
public static final String JavaDoc USE_OUTLINE_VIEW= PREFIX + "UseOutlineView"; //$NON-NLS-1$
67
public static final String JavaDoc SYNCHRONIZE_SCROLLING= PREFIX + "SynchronizeScrolling"; //$NON-NLS-1$
68
public static final String JavaDoc SHOW_PSEUDO_CONFLICTS= PREFIX + "ShowPseudoConflicts"; //$NON-NLS-1$
69
public static final String JavaDoc INITIALLY_SHOW_ANCESTOR_PANE= PREFIX + "InitiallyShowAncestorPane"; //$NON-NLS-1$
70
public static final String JavaDoc PREF_SAVE_ALL_EDITORS= PREFIX + "SaveAllEditors"; //$NON-NLS-1$
71
public static final String JavaDoc SHOW_MORE_INFO= PREFIX + "ShowMoreInfo"; //$NON-NLS-1$
72
public static final String JavaDoc IGNORE_WHITESPACE= PREFIX + "IgnoreWhitespace"; //$NON-NLS-1$
73

74     //public static final String USE_SPLINES= PREFIX + "UseSplines"; //$NON-NLS-1$
75
public static final String JavaDoc USE_SINGLE_LINE= PREFIX + "UseSingleLine"; //$NON-NLS-1$
76
public static final String JavaDoc HIGHLIGHT_TOKEN_CHANGES= PREFIX + "HighlightTokenChanges"; //$NON-NLS-1$
77
//public static final String USE_RESOLVE_UI= PREFIX + "UseResolveUI"; //$NON-NLS-1$
78
public static final String JavaDoc PATH_FILTER= PREFIX + "PathFilter"; //$NON-NLS-1$
79

80     
81     private TextMergeViewer fPreviewViewer;
82     private IPropertyChangeListener fPreferenceChangeListener;
83     private CompareConfiguration fCompareConfiguration;
84     private OverlayPreferenceStore fOverlayStore;
85     private Map JavaDoc fCheckBoxes= new HashMap JavaDoc();
86     private Text fFilters;
87     private SelectionListener fCheckBoxListener;
88
89
90     public final OverlayPreferenceStore.OverlayKey[] fKeys= new OverlayPreferenceStore.OverlayKey[] {
91         new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, OPEN_STRUCTURE_COMPARE),
92         new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, USE_OUTLINE_VIEW),
93         new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, SYNCHRONIZE_SCROLLING),
94         new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, SHOW_PSEUDO_CONFLICTS),
95         new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, INITIALLY_SHOW_ANCESTOR_PANE),
96         new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, SHOW_MORE_INFO),
97         new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, IGNORE_WHITESPACE),
98         new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PREF_SAVE_ALL_EDITORS),
99         
100         new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND),
101         new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT),
102         
103         //new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, USE_SPLINES),
104
new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, USE_SINGLE_LINE),
105         new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, HIGHLIGHT_TOKEN_CHANGES),
106         //new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, USE_RESOLVE_UI),
107
new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, PATH_FILTER),
108     };
109     private RadioGroupFieldEditor editor;
110     
111     
112     public static void initDefaults(IPreferenceStore store) {
113         store.setDefault(OPEN_STRUCTURE_COMPARE, true);
114         store.setDefault(USE_OUTLINE_VIEW, false);
115         store.setDefault(SYNCHRONIZE_SCROLLING, true);
116         store.setDefault(SHOW_PSEUDO_CONFLICTS, false);
117         store.setDefault(INITIALLY_SHOW_ANCESTOR_PANE, false);
118         store.setDefault(SHOW_MORE_INFO, false);
119         store.setDefault(IGNORE_WHITESPACE, false);
120         store.setDefault(PREF_SAVE_ALL_EDITORS, false);
121         //store.setDefault(USE_SPLINES, false);
122
store.setDefault(USE_SINGLE_LINE, true);
123         store.setDefault(HIGHLIGHT_TOKEN_CHANGES, true);
124         //store.setDefault(USE_RESOLVE_UI, false);
125
store.setDefault(PATH_FILTER, ""); //$NON-NLS-1$
126
store.setDefault(AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT, true);
127     }
128
129     public ComparePreferencePage() {
130         
131         //setDescription(Utilities.getString("ComparePreferencePage.description")); //$NON-NLS-1$
132

133         setPreferenceStore(CompareUIPlugin.getDefault().getPreferenceStore());
134         
135         fOverlayStore= new OverlayPreferenceStore(getPreferenceStore(), fKeys);
136         fPreferenceChangeListener= new IPropertyChangeListener() {
137             public void propertyChange(PropertyChangeEvent event) {
138                 String JavaDoc key= event.getProperty();
139                 if (key.equals(INITIALLY_SHOW_ANCESTOR_PANE)) {
140                     boolean b= fOverlayStore.getBoolean(INITIALLY_SHOW_ANCESTOR_PANE);
141                     if (fCompareConfiguration != null) {
142                         fCompareConfiguration.setProperty(INITIALLY_SHOW_ANCESTOR_PANE, new Boolean JavaDoc(b));
143                     }
144                 }
145             }
146         };
147         fOverlayStore.addPropertyChangeListener(fPreferenceChangeListener);
148     }
149     
150     /*
151      * @see IWorkbenchPreferencePage#init()
152      */

153     public void init(IWorkbench workbench) {
154         // empty
155
}
156
157     /*
158      * @see PreferencePage#performOk()
159      */

160     public boolean performOk() {
161         fOverlayStore.propagate();
162         editor.store();
163         return true;
164     }
165     
166     /*
167      * @see PreferencePage#performDefaults()
168      */

169     protected void performDefaults() {
170         
171         fOverlayStore.loadDefaults();
172         initializeFields();
173         
174         super.performDefaults();
175     }
176     
177     /*
178      * @see DialogPage#dispose()
179      */

180     public void dispose() {
181                 
182         if (fOverlayStore != null) {
183             if (fPreferenceChangeListener != null) {
184                 fOverlayStore.removePropertyChangeListener(fPreferenceChangeListener);
185                 fPreferenceChangeListener= null;
186             }
187             fOverlayStore.stop();
188             fOverlayStore= null;
189         }
190         
191         super.dispose();
192     }
193
194     static public boolean getSaveAllEditors() {
195         IPreferenceStore store= CompareUIPlugin.getDefault().getPreferenceStore();
196         return store.getBoolean(PREF_SAVE_ALL_EDITORS);
197     }
198     
199     static public void setSaveAllEditors(boolean value) {
200         IPreferenceStore store= CompareUIPlugin.getDefault().getPreferenceStore();
201         store.setValue(PREF_SAVE_ALL_EDITORS, value);
202     }
203
204     /*
205      * @see PreferencePage#createContents(Composite)
206      */

207     protected Control createContents(Composite parent) {
208         
209         PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, ICompareContextIds.COMPARE_PREFERENCE_PAGE);
210         
211         fOverlayStore.load();
212         fOverlayStore.start();
213         
214         TabFolder folder= new TabFolder(parent, SWT.NONE);
215         folder.setLayout(new TabFolderLayout());
216         folder.setLayoutData(new GridData(GridData.FILL_BOTH));
217         
218         TabItem item= new TabItem(folder, SWT.NONE);
219         item.setText(Utilities.getString("ComparePreferencePage.generalTab.label")); //$NON-NLS-1$
220
//item.setImage(JavaPluginImages.get(JavaPluginImages.IMG_OBJS_CFILE));
221
item.setControl(createGeneralPage(folder));
222         
223         item= new TabItem(folder, SWT.NONE);
224         item.setText(Utilities.getString("ComparePreferencePage.textCompareTab.label")); //$NON-NLS-1$
225
//item.setImage(JavaPluginImages.get(JavaPluginImages.IMG_OBJS_CFILE));
226
item.setControl(createTextComparePage(folder));
227         
228         initializeFields();
229         Dialog.applyDialogFont(folder);
230         return folder;
231     }
232     
233     private Control createGeneralPage(Composite parent) {
234         Composite composite= new Composite(parent, SWT.NULL);
235         GridLayout layout= new GridLayout();
236         layout.numColumns= 1;
237         composite.setLayout(layout);
238                 
239         addCheckBox(composite, "ComparePreferencePage.structureCompare.label", OPEN_STRUCTURE_COMPARE, 0); //$NON-NLS-1$
240
addCheckBox(composite, "ComparePreferencePage.structureOutline.label", USE_OUTLINE_VIEW, 0); //$NON-NLS-1$
241
addCheckBox(composite, "ComparePreferencePage.showMoreInfo.label", SHOW_MORE_INFO, 0); //$NON-NLS-1$
242
addCheckBox(composite, "ComparePreferencePage.ignoreWhitespace.label", IGNORE_WHITESPACE, 0); //$NON-NLS-1$
243

244         // a spacer
245
new Label(composite, SWT.NONE);
246
247         addCheckBox(composite, "ComparePreferencePage.saveBeforePatching.label", PREF_SAVE_ALL_EDITORS, 0); //$NON-NLS-1$
248

249         // a spacer
250
new Label(composite, SWT.NONE);
251         
252         Label l= new Label(composite, SWT.WRAP);
253         l.setText(Utilities.getString("ComparePreferencePage.filter.description")); //$NON-NLS-1$
254

255         Composite c2= new Composite(composite, SWT.NONE);
256         c2.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
257         layout= new GridLayout(2, false);
258         layout.marginWidth= 0;
259         c2.setLayout(layout);
260         
261         l= new Label(c2, SWT.NONE);
262         l.setText(Utilities.getString("ComparePreferencePage.filter.label")); //$NON-NLS-1$
263

264         fFilters= new Text(c2, SWT.BORDER);
265         fFilters.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
266         fFilters.setText(fOverlayStore.getString(PATH_FILTER));
267         fFilters.addModifyListener(
268             new ModifyListener() {
269                 public void modifyText(ModifyEvent e) {
270                     String JavaDoc filters= fFilters.getText();
271                     String JavaDoc message= CompareFilter.validateResourceFilters(filters);
272                     setValid(message == null);
273                     setMessage(null);
274                     setErrorMessage(message);
275                     fOverlayStore.setValue(PATH_FILTER, filters);
276                 }
277             }
278         );
279         
280         return composite;
281     }
282     
283     private Control createTextComparePage(Composite parent) {
284         
285         Composite composite= new Composite(parent, SWT.NULL);
286         GridLayout layout= new GridLayout();
287         layout.numColumns= 1;
288         composite.setLayout(layout);
289                 
290         addCheckBox(composite, "ComparePreferencePage.synchronizeScrolling.label", SYNCHRONIZE_SCROLLING, 0); //$NON-NLS-1$
291
addCheckBox(composite, "ComparePreferencePage.initiallyShowAncestorPane.label", INITIALLY_SHOW_ANCESTOR_PANE, 0); //$NON-NLS-1$
292
addCheckBox(composite, "ComparePreferencePage.showPseudoConflicts.label", SHOW_PSEUDO_CONFLICTS, 0); //$NON-NLS-1$
293

294         //addCheckBox(composite, "ComparePreferencePage.useSplines.label", USE_SPLINES, 0); //$NON-NLS-1$
295
addCheckBox(composite, "ComparePreferencePage.useSingleLine.label", USE_SINGLE_LINE, 0); //$NON-NLS-1$
296
addCheckBox(composite, "ComparePreferencePage.highlightTokenChanges.label", HIGHLIGHT_TOKEN_CHANGES, 0); //$NON-NLS-1$
297
//addCheckBox(composite, "ComparePreferencePage.useResolveUI.label", USE_RESOLVE_UI, 0); //$NON-NLS-1$
298

299         Composite radioGroup = new Composite(composite, SWT.NULL);
300         radioGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
301         editor = new RadioGroupFieldEditor(ICompareUIConstants.PREF_NAVIGATION_END_ACTION, CompareMessages.ComparePreferencePage_0, 1,
302                 new String JavaDoc[][] {
303                     new String JavaDoc[] { CompareMessages.ComparePreferencePage_1, ICompareUIConstants.PREF_VALUE_PROMPT },
304                     new String JavaDoc[] { CompareMessages.ComparePreferencePage_2, ICompareUIConstants.PREF_VALUE_LOOP },
305                     new String JavaDoc[] { CompareMessages.ComparePreferencePage_3, ICompareUIConstants.PREF_VALUE_NEXT}
306                 },
307         radioGroup, true);
308         editor.setPreferenceStore(CompareUIPlugin.getDefault().getPreferenceStore());
309         editor.fillIntoGrid(radioGroup, 1);
310         
311         // a spacer
312
Label separator= new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
313         separator.setVisible(false);
314         
315         Label previewLabel= new Label(composite, SWT.NULL);
316         previewLabel.setText(Utilities.getString("ComparePreferencePage.preview.label")); //$NON-NLS-1$
317

318         Control previewer= createPreviewer(composite);
319         GridData gd= new GridData(GridData.FILL_BOTH);
320         gd.widthHint= convertWidthInCharsToPixels(60);
321         gd.heightHint= convertHeightInCharsToPixels(13);
322         previewer.setLayoutData(gd);
323         
324         PreferenceLinkArea area = new PreferenceLinkArea(composite, SWT.NONE,
325                 "org.eclipse.ui.preferencePages.ColorsAndFonts", Utilities.getString("ComparePreferencePage.colorAndFontLink"), //$NON-NLS-1$ //$NON-NLS-2$
326
(IWorkbenchPreferenceContainer) getContainer(), null);
327
328         GridData data= new GridData(SWT.FILL, SWT.CENTER, false, false);
329         area.getControl().setLayoutData(data);
330         
331         return composite;
332     }
333     
334     private Control createPreviewer(Composite parent) {
335                 
336         fCompareConfiguration= new CompareConfiguration(fOverlayStore);
337         fCompareConfiguration.setAncestorLabel(Utilities.getString("ComparePreferencePage.ancestor.label")); //$NON-NLS-1$
338

339         fCompareConfiguration.setLeftLabel(Utilities.getString("ComparePreferencePage.left.label")); //$NON-NLS-1$
340
fCompareConfiguration.setLeftEditable(false);
341         
342         fCompareConfiguration.setRightLabel(Utilities.getString("ComparePreferencePage.right.label")); //$NON-NLS-1$
343
fCompareConfiguration.setRightEditable(false);
344         
345         fPreviewViewer= new TextMergeViewer(parent, SWT.BORDER, fCompareConfiguration);
346         
347         fPreviewViewer.setInput(
348             new DiffNode(Differencer.CONFLICTING,
349                 new FakeInput("ComparePreferencePage.previewAncestor"), //$NON-NLS-1$
350
new FakeInput("ComparePreferencePage.previewLeft"), //$NON-NLS-1$
351
new FakeInput("ComparePreferencePage.previewRight") //$NON-NLS-1$
352
)
353         );
354
355         Control c= fPreviewViewer.getControl();
356         c.addDisposeListener(new DisposeListener() {
357             public void widgetDisposed(DisposeEvent e) {
358                 if (fCompareConfiguration != null)
359                     fCompareConfiguration.dispose();
360             }
361         });
362         
363         return c;
364     }
365             
366     private void initializeFields() {
367         
368         Iterator JavaDoc e= fCheckBoxes.keySet().iterator();
369         while (e.hasNext()) {
370             Button b= (Button) e.next();
371             String JavaDoc key= (String JavaDoc) fCheckBoxes.get(b);
372             b.setSelection(fOverlayStore.getBoolean(key));
373         }
374         
375         if (fFilters != null)
376             fFilters.setText(fOverlayStore.getString(PATH_FILTER));
377         
378         editor.load();
379     }
380
381     // overlay stuff
382

383     private Button addCheckBox(Composite parent, String JavaDoc labelKey, String JavaDoc key, int indentation) {
384         
385         String JavaDoc label= Utilities.getString(labelKey);
386                 
387         Button checkBox= new Button(parent, SWT.CHECK);
388         checkBox.setText(label);
389         
390         GridData gd= new GridData(GridData.FILL_HORIZONTAL);
391         gd.horizontalIndent= indentation;
392         gd.horizontalSpan= 2;
393         checkBox.setLayoutData(gd);
394         
395         if (fCheckBoxListener == null) {
396             fCheckBoxListener= new SelectionAdapter() {
397                 public void widgetSelected(SelectionEvent e) {
398                     Button button= (Button) e.widget;
399                     fOverlayStore.setValue((String JavaDoc) fCheckBoxes.get(button), button.getSelection());
400                 }
401             };
402         }
403         checkBox.addSelectionListener(fCheckBoxListener);
404         
405         fCheckBoxes.put(checkBox, key);
406         
407         return checkBox;
408     }
409     
410     private String JavaDoc loadPreviewContentFromFile(String JavaDoc key) {
411         
412         String JavaDoc preview= Utilities.getString(key);
413         String JavaDoc separator= System.getProperty("line.separator"); //$NON-NLS-1$
414
StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
415         for (int i= 0; i < preview.length(); i++) {
416             char c= preview.charAt(i);
417             if (c == '\n')
418                 buffer.append(separator);
419             else
420                 buffer.append(c);
421         }
422         return buffer.toString();
423     }
424 }
425
Popular Tags