1 12 13 package org.eclipse.ui.internal.editors.text; 14 15 import java.util.ArrayList ; 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.Map ; 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 50 class QuickDiffConfigurationBlock implements IPreferenceConfigurationBlock { 51 52 private OverlayPreferenceStore fStore; 53 54 private Map fCheckBoxes= new HashMap (); 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 ) fCheckBoxes.get(button), button.getSelection()); 61 } 62 }; 63 64 68 private Combo fQuickDiffProviderCombo; 69 73 private String [][] fQuickDiffProviderListModel; 74 78 private String [][] fQuickDiffModel; 79 83 private ColorSelector[] fQuickDiffColorEditors; 84 88 private Button fQuickDiffOverviewRulerCheckBox; 89 90 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 overlayKeys= new ArrayList (); 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 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") || (info.getAnnotationType().equals("org.eclipse.ui.workbench.texteditor.quickdiffAddition")) || (info.getAnnotationType().equals("org.eclipse.ui.workbench.texteditor.quickdiffDeletion")) ) { 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 [][] createQuickDiffModel(MarkerAnnotationPreferences preferences) { 131 String [][] items= new String [3][]; 132 133 Iterator 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")) items[0]= new String [] { info.getColorPreferenceKey(), info.getOverviewRulerPreferenceKey(), TextEditorMessages.QuickDiffConfigurationBlock_changeColor }; 138 else if (info.getAnnotationType().equals("org.eclipse.ui.workbench.texteditor.quickdiffAddition")) items[1]= new String [] { info.getColorPreferenceKey(), info.getOverviewRulerPreferenceKey(), TextEditorMessages.QuickDiffConfigurationBlock_additionColor }; 140 else if (info.getAnnotationType().equals("org.eclipse.ui.workbench.texteditor.quickdiffDeletion")) items[2]= new String [] { info.getColorPreferenceKey(), info.getOverviewRulerPreferenceKey(), TextEditorMessages.QuickDiffConfigurationBlock_deletionColor }; 142 } 143 return items; 144 } 145 146 private String [][] createQuickDiffReferenceListModel() { 147 java.util.List descriptors= new QuickDiff().getReferenceProviderDescriptors(); 148 ArrayList listModelItems= new ArrayList (); 149 for (Iterator it= descriptors.iterator(); it.hasNext();) { 150 ReferenceProviderDescriptor descriptor= (ReferenceProviderDescriptor) it.next(); 151 String label= LegacyActionTools.removeMnemonics(descriptor.getLabel()); 152 listModelItems.add(new String [] { descriptor.getId(), label }); 153 } 154 String [][] items= new String [listModelItems.size()][]; 155 listModelItems.toArray(items); 156 return items; 157 } 158 159 private Button addCheckBox(Composite parent, String label, String 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 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 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 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 } 257 258 public void widgetSelected(SelectionEvent e) { 259 String 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 } 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 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 label= fQuickDiffProviderListModel[i][1]; 344 fQuickDiffProviderCombo.add(label); 345 } 346 fQuickDiffProviderCombo.getDisplay().asyncExec(new Runnable () { 347 public void run() { 348 updateProviderList(); 349 } 350 }); 351 352 initializeFields(); 353 } 354 355 private void initializeFields() { 356 Iterator e= fCheckBoxes.keySet().iterator(); 357 while (e.hasNext()) { 358 Button b= (Button) e.next(); 359 String key= (String ) 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 391 public void dispose() { 392 } 393 } 394 | Popular Tags |