1 11 package org.eclipse.compare.internal; 12 13 import java.io.*; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.Map ; 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 UTF_16= "UTF-16"; String fContent; 43 44 FakeInput(String name) { 45 fContent= loadPreviewContentFromFile(name); 46 } 47 public Image getImage() { 48 return null; 49 } 50 public String getName() { 51 return "no name"; } 53 public String getType() { 54 return "no type"; } 56 public InputStream getContents() { 57 return new ByteArrayInputStream(Utilities.getBytes(fContent, UTF_16)); 58 } 59 public String getCharset() { 60 return UTF_16; 61 } 62 } 63 64 private static final String PREFIX= CompareUIPlugin.PLUGIN_ID + "."; public static final String OPEN_STRUCTURE_COMPARE= PREFIX + "OpenStructureCompare"; public static final String USE_OUTLINE_VIEW= PREFIX + "UseOutlineView"; public static final String SYNCHRONIZE_SCROLLING= PREFIX + "SynchronizeScrolling"; public static final String SHOW_PSEUDO_CONFLICTS= PREFIX + "ShowPseudoConflicts"; public static final String INITIALLY_SHOW_ANCESTOR_PANE= PREFIX + "InitiallyShowAncestorPane"; public static final String PREF_SAVE_ALL_EDITORS= PREFIX + "SaveAllEditors"; public static final String SHOW_MORE_INFO= PREFIX + "ShowMoreInfo"; public static final String IGNORE_WHITESPACE= PREFIX + "IgnoreWhitespace"; 74 public static final String USE_SINGLE_LINE= PREFIX + "UseSingleLine"; public static final String HIGHLIGHT_TOKEN_CHANGES= PREFIX + "HighlightTokenChanges"; public static final String PATH_FILTER= PREFIX + "PathFilter"; 80 81 private TextMergeViewer fPreviewViewer; 82 private IPropertyChangeListener fPreferenceChangeListener; 83 private CompareConfiguration fCompareConfiguration; 84 private OverlayPreferenceStore fOverlayStore; 85 private Map fCheckBoxes= new HashMap (); 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_SINGLE_LINE), 105 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, HIGHLIGHT_TOKEN_CHANGES), 106 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_SINGLE_LINE, true); 123 store.setDefault(HIGHLIGHT_TOKEN_CHANGES, true); 124 store.setDefault(PATH_FILTER, ""); store.setDefault(AbstractTextEditor.PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT, true); 127 } 128 129 public ComparePreferencePage() { 130 131 133 setPreferenceStore(CompareUIPlugin.getDefault().getPreferenceStore()); 134 135 fOverlayStore= new OverlayPreferenceStore(getPreferenceStore(), fKeys); 136 fPreferenceChangeListener= new IPropertyChangeListener() { 137 public void propertyChange(PropertyChangeEvent event) { 138 String 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 (b)); 143 } 144 } 145 } 146 }; 147 fOverlayStore.addPropertyChangeListener(fPreferenceChangeListener); 148 } 149 150 153 public void init(IWorkbench workbench) { 154 } 156 157 160 public boolean performOk() { 161 fOverlayStore.propagate(); 162 editor.store(); 163 return true; 164 } 165 166 169 protected void performDefaults() { 170 171 fOverlayStore.loadDefaults(); 172 initializeFields(); 173 174 super.performDefaults(); 175 } 176 177 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 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")); item.setControl(createGeneralPage(folder)); 222 223 item= new TabItem(folder, SWT.NONE); 224 item.setText(Utilities.getString("ComparePreferencePage.textCompareTab.label")); 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); addCheckBox(composite, "ComparePreferencePage.structureOutline.label", USE_OUTLINE_VIEW, 0); addCheckBox(composite, "ComparePreferencePage.showMoreInfo.label", SHOW_MORE_INFO, 0); addCheckBox(composite, "ComparePreferencePage.ignoreWhitespace.label", IGNORE_WHITESPACE, 0); 244 new Label(composite, SWT.NONE); 246 247 addCheckBox(composite, "ComparePreferencePage.saveBeforePatching.label", PREF_SAVE_ALL_EDITORS, 0); 249 new Label(composite, SWT.NONE); 251 252 Label l= new Label(composite, SWT.WRAP); 253 l.setText(Utilities.getString("ComparePreferencePage.filter.description")); 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")); 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 filters= fFilters.getText(); 271 String 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); addCheckBox(composite, "ComparePreferencePage.initiallyShowAncestorPane.label", INITIALLY_SHOW_ANCESTOR_PANE, 0); addCheckBox(composite, "ComparePreferencePage.showPseudoConflicts.label", SHOW_PSEUDO_CONFLICTS, 0); 294 addCheckBox(composite, "ComparePreferencePage.useSingleLine.label", USE_SINGLE_LINE, 0); addCheckBox(composite, "ComparePreferencePage.highlightTokenChanges.label", HIGHLIGHT_TOKEN_CHANGES, 0); 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 [][] { 303 new String [] { CompareMessages.ComparePreferencePage_1, ICompareUIConstants.PREF_VALUE_PROMPT }, 304 new String [] { CompareMessages.ComparePreferencePage_2, ICompareUIConstants.PREF_VALUE_LOOP }, 305 new String [] { CompareMessages.ComparePreferencePage_3, ICompareUIConstants.PREF_VALUE_NEXT} 306 }, 307 radioGroup, true); 308 editor.setPreferenceStore(CompareUIPlugin.getDefault().getPreferenceStore()); 309 editor.fillIntoGrid(radioGroup, 1); 310 311 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")); 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"), (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")); 339 fCompareConfiguration.setLeftLabel(Utilities.getString("ComparePreferencePage.left.label")); fCompareConfiguration.setLeftEditable(false); 341 342 fCompareConfiguration.setRightLabel(Utilities.getString("ComparePreferencePage.right.label")); 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"), new FakeInput("ComparePreferencePage.previewLeft"), new FakeInput("ComparePreferencePage.previewRight") ) 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 e= fCheckBoxes.keySet().iterator(); 369 while (e.hasNext()) { 370 Button b= (Button) e.next(); 371 String key= (String ) 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 383 private Button addCheckBox(Composite parent, String labelKey, String key, int indentation) { 384 385 String 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 ) 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 loadPreviewContentFromFile(String key) { 411 412 String preview= Utilities.getString(key); 413 String separator= System.getProperty("line.separator"); StringBuffer buffer= new StringBuffer (); 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 |