1 11 package org.eclipse.compare; 12 13 import java.util.*; 14 15 import org.eclipse.compare.internal.*; 16 import org.eclipse.compare.structuremergeviewer.Differencer; 17 import org.eclipse.compare.structuremergeviewer.ICompareInput; 18 import org.eclipse.core.runtime.ListenerList; 19 import org.eclipse.jface.preference.IPreferenceStore; 20 import org.eclipse.jface.resource.ImageDescriptor; 21 import org.eclipse.jface.util.IPropertyChangeListener; 22 import org.eclipse.jface.util.PropertyChangeEvent; 23 import org.eclipse.jface.viewers.*; 24 import org.eclipse.swt.graphics.Image; 25 26 44 public class CompareConfiguration { 45 46 49 public static final String IGNORE_WHITESPACE= "IGNORE_WHITESPACE"; 53 public static final String SHOW_PSEUDO_CONFLICTS= "SHOW_PSEUDO_CONFLICTS"; 58 public static final String USE_OUTLINE_VIEW= "USE_OUTLINE_VIEW"; 60 private static ImageDescriptor[] fgImages= new ImageDescriptor[16]; 61 private static Object fgDummy= new Object (); 62 private static HashMap fgMap= new HashMap(20); 63 private static boolean fLeftIsLocal= true; 64 65 static { 66 if (fLeftIsLocal) { 67 fgImages[Differencer.ADDITION]= CompareUIPlugin.getImageDescriptor("ovr16/del_ov.gif"); fgImages[Differencer.LEFT + Differencer.ADDITION]= CompareUIPlugin.getImageDescriptor("ovr16/r_inadd_ov.gif"); fgImages[Differencer.RIGHT + Differencer.ADDITION]= CompareUIPlugin.getImageDescriptor("ovr16/r_outadd_ov.gif"); 71 fgImages[Differencer.DELETION]= CompareUIPlugin.getImageDescriptor("ovr16/add_ov.gif"); fgImages[Differencer.LEFT + Differencer.DELETION]= CompareUIPlugin.getImageDescriptor("ovr16/r_indel_ov.gif"); fgImages[Differencer.RIGHT + Differencer.DELETION]= CompareUIPlugin.getImageDescriptor("ovr16/r_outdel_ov.gif"); 75 fgImages[Differencer.LEFT + Differencer.CHANGE]= CompareUIPlugin.getImageDescriptor("ovr16/r_inchg_ov.gif"); fgImages[Differencer.RIGHT + Differencer.CHANGE]= CompareUIPlugin.getImageDescriptor("ovr16/r_outchg_ov.gif"); } else { 78 fgImages[Differencer.ADDITION]= CompareUIPlugin.getImageDescriptor("ovr16/add_ov.gif"); fgImages[Differencer.LEFT + Differencer.ADDITION]= CompareUIPlugin.getImageDescriptor("ovr16/inadd_ov.gif"); fgImages[Differencer.RIGHT + Differencer.ADDITION]= CompareUIPlugin.getImageDescriptor("ovr16/outadd_ov.gif"); 82 fgImages[Differencer.DELETION]= CompareUIPlugin.getImageDescriptor("ovr16/del_ov.gif"); fgImages[Differencer.LEFT + Differencer.DELETION]= CompareUIPlugin.getImageDescriptor("ovr16/indel_ov.gif"); fgImages[Differencer.RIGHT + Differencer.DELETION]= CompareUIPlugin.getImageDescriptor("ovr16/outdel_ov.gif"); 86 fgImages[Differencer.LEFT + Differencer.CHANGE]= CompareUIPlugin.getImageDescriptor("ovr16/inchg_ov.gif"); fgImages[Differencer.RIGHT + Differencer.CHANGE]= CompareUIPlugin.getImageDescriptor("ovr16/outchg_ov.gif"); } 89 90 fgImages[Differencer.CONFLICTING + Differencer.ADDITION]= CompareUIPlugin.getImageDescriptor("ovr16/confadd_ov.gif"); fgImages[Differencer.CONFLICTING + Differencer.DELETION]= CompareUIPlugin.getImageDescriptor("ovr16/confdel_ov.gif"); fgImages[Differencer.CONFLICTING + Differencer.CHANGE]= CompareUIPlugin.getImageDescriptor("ovr16/confchg_ov.gif"); } 94 95 private IPreferenceStore fPreferenceStore; 96 private ListenerList fListeners= new ListenerList(); 97 private HashMap fProperties= new HashMap(); 98 private boolean fLeftEditable= true; 99 private boolean fRightEditable= true; 100 private String fAncestorLabel; 101 private String fLeftLabel; 102 private String fRightLabel; 103 private Image fAncestorImage; 104 private Image fRightImage; 105 private Image fLeftImage; 106 private Image[] fImages= new Image[16]; 107 private ICompareContainer fContainer; 108 private DefaultLabelProvider labelProvider = new DefaultLabelProvider(); 109 110 private class DefaultLabelProvider extends LabelProvider implements ICompareInputLabelProvider, ILabelProviderListener { 111 private Map labelProviders = new HashMap(); 112 private ICompareInputLabelProvider defaultLabelProvider; 113 public Image getAncestorImage(Object input) { 114 ICompareInputLabelProvider provider = getLabelProvider(input); 115 if (provider != null) { 116 Image image = provider.getAncestorImage(input); 117 if (image != null) 118 return image; 119 } 120 return fAncestorImage; 121 } 122 public String getAncestorLabel(Object input) { 123 ICompareInputLabelProvider provider = getLabelProvider(input); 124 if (provider != null) { 125 String label = provider.getAncestorLabel(input); 126 if (label != null) 127 return label; 128 } 129 return fAncestorLabel; 130 } 131 public Image getLeftImage(Object input) { 132 ICompareInputLabelProvider provider = getLabelProvider(input); 133 if (provider != null) { 134 Image image = provider.getLeftImage(input); 135 if (image != null) 136 return image; 137 } 138 return fLeftImage; 139 } 140 public String getLeftLabel(Object input) { 141 ICompareInputLabelProvider provider = getLabelProvider(input); 142 if (provider != null) { 143 String label = provider.getLeftLabel(input); 144 if (label != null) 145 return label; 146 } 147 return fLeftLabel; 148 } 149 public Image getRightImage(Object input) { 150 ICompareInputLabelProvider provider = getLabelProvider(input); 151 if (provider != null) { 152 Image image = provider.getRightImage(input); 153 if (image != null) 154 return image; 155 } 156 return fRightImage; 157 } 158 public String getRightLabel(Object input) { 159 ICompareInputLabelProvider provider = getLabelProvider(input); 160 if (provider != null) { 161 String label = provider.getRightLabel(input); 162 if (label != null) 163 return label; 164 } 165 return fRightLabel; 166 } 167 public ICompareInputLabelProvider getLabelProvider(Object input) { 168 ICompareInputLabelProvider lp = (ICompareInputLabelProvider)labelProviders.get(input); 169 if (lp == null) 170 return defaultLabelProvider; 171 return lp; 172 } 173 public void setLabelProvider(ICompareInput input, ICompareInputLabelProvider labelProvider) { 174 ICompareInputLabelProvider old = (ICompareInputLabelProvider)labelProviders.get(input); 175 if (old != null) 176 old.removeListener(this); 177 labelProviders.put(input, labelProvider); 178 labelProvider.addListener(this); 179 } 180 public Image getImage(Object element) { 181 ICompareInputLabelProvider provider = getLabelProvider(element); 182 if (provider != null) { 183 Image image = provider.getImage(element); 184 if (image != null) 185 return image; 186 } 187 if (element instanceof ICompareInput) { 188 ICompareInput ci = (ICompareInput) element; 189 Image image = ci.getImage(); 190 if (image != null) 191 return image; 192 } 193 return super.getImage(element); 194 } 195 public String getText(Object element) { 196 ICompareInputLabelProvider provider = getLabelProvider(element); 197 if (provider != null) { 198 String label = provider.getText(element); 199 if (label != null) 200 return label; 201 } 202 if (element instanceof ICompareInput) { 203 ICompareInput ci = (ICompareInput) element; 204 String label = ci.getName(); 205 if (label != null) 206 return label; 207 } 208 return super.getText(element); 209 } 210 211 public void dispose() { 212 for (Iterator iterator = labelProviders.values().iterator(); iterator.hasNext();) { 213 ICompareInputLabelProvider lp = (ICompareInputLabelProvider) iterator.next(); 214 lp.removeListener(this); 215 } 216 if (defaultLabelProvider != null) 217 defaultLabelProvider.removeListener(this); 218 defaultLabelProvider = null; 219 labelProviders.clear(); 220 } 221 222 public void labelProviderChanged(LabelProviderChangedEvent event) { 223 fireLabelProviderChanged(new LabelProviderChangedEvent(this, event.getElements())); 224 } 225 public void setDefaultLabelProvider(ICompareInputLabelProvider labelProvider) { 226 if (defaultLabelProvider != null) 227 defaultLabelProvider.removeListener(this); 228 defaultLabelProvider = labelProvider; 229 if (defaultLabelProvider != null) 230 defaultLabelProvider.addListener(this); 231 } 232 } 233 234 244 public CompareConfiguration(IPreferenceStore prefStore) { 245 246 setProperty("LEFT_IS_LOCAL", Boolean.valueOf(fLeftIsLocal)); 248 fPreferenceStore= prefStore; 249 if (fPreferenceStore != null) { 250 boolean b= fPreferenceStore.getBoolean(ComparePreferencePage.INITIALLY_SHOW_ANCESTOR_PANE); 251 setProperty(ICompareUIConstants.PROP_ANCESTOR_VISIBLE, new Boolean (b)); 252 253 b= fPreferenceStore.getBoolean(ComparePreferencePage.IGNORE_WHITESPACE); 254 setProperty(CompareConfiguration.IGNORE_WHITESPACE, new Boolean (b)); 255 } 256 } 257 258 264 public CompareConfiguration() { 265 this(CompareUIPlugin.getDefault().getPreferenceStore()); 266 } 267 268 273 public IPreferenceStore getPreferenceStore() { 274 return fPreferenceStore; 275 } 276 277 288 public Image getImage(int kind) { 289 if (fImages == null) 290 return null; 292 Image image= fImages[kind & 15]; 293 if (image == null) { 294 ImageDescriptor id= fgImages[kind & 15]; 295 if (id != null) 296 image= id.createImage(); 297 fImages[kind & 15]= image; 298 } 299 return image; 300 } 301 302 315 public Image getImage(Image base, int kind) { 316 317 Object key= base; 318 if (key == null) 319 key= fgDummy; 320 321 kind &= 15; 322 323 Image[] a= (Image[]) fgMap.get(key); 324 if (a == null) { 325 a= new Image[16]; 326 fgMap.put(key, a); 327 } 328 Image b= a[kind]; 329 if (b == null) { 330 b= new DiffImage(base, fgImages[kind], ICompareUIConstants.COMPARE_IMAGE_WIDTH, !fLeftIsLocal).createImage(); 331 CompareUI.disposeOnShutdown(b); 332 a[kind]= b; 333 } 334 return b; 335 } 336 337 342 public void dispose() { 343 if (fImages != null) { 344 for (int i= 0; i < fImages.length; i++){ 345 Image image= fImages[i]; 346 if (image != null && !image.isDisposed()) 347 image.dispose(); 348 } 349 } 350 fImages= null; 351 labelProvider.dispose(); 352 } 353 354 361 private void fireChange(String propertyName, Object oldValue, Object newValue) { 362 PropertyChangeEvent event= null; 363 Object [] listeners= fListeners.getListeners(); 364 if (listeners != null) { 365 for (int i= 0; i < listeners.length; i++) { 366 IPropertyChangeListener l= (IPropertyChangeListener) listeners[i]; 367 if (event == null) 368 event= new PropertyChangeEvent(this, propertyName, oldValue, newValue); 369 l.propertyChange(event); 370 } 371 } 372 } 373 374 377 public void addPropertyChangeListener(IPropertyChangeListener listener) { 378 fListeners.add(listener); 379 } 380 381 384 public void removePropertyChangeListener(IPropertyChangeListener listener) { 385 fListeners.remove(listener); 386 } 387 388 396 public void setProperty(String key, Object newValue) { 397 Object oldValue= fProperties.get(key); 398 fProperties.put(key, newValue); 399 if (oldValue == null || !oldValue.equals(newValue)) 400 fireChange(key, oldValue, newValue); 401 } 402 403 410 public Object getProperty(String key) { 411 return fProperties.get(key); 412 } 413 414 416 425 public void setAncestorLabel(String label) { 426 fAncestorLabel= label; 427 } 428 429 436 public String getAncestorLabel(Object element) { 437 return labelProvider.getAncestorLabel(element); 438 } 439 440 450 public void setAncestorImage(Image image) { 451 fAncestorImage= image; 452 } 453 454 461 public Image getAncestorImage(Object element) { 462 return labelProvider.getAncestorImage(element); 463 } 464 465 467 472 public void setLeftEditable(boolean editable) { 473 fLeftEditable= editable; 474 } 475 476 481 public boolean isLeftEditable() { 482 return fLeftEditable; 483 } 484 485 494 public void setLeftLabel(String label) { 495 fLeftLabel= label; 496 } 497 498 505 public String getLeftLabel(Object element) { 506 return labelProvider.getLeftLabel(element); 507 } 508 509 519 public void setLeftImage(Image image) { 520 fLeftImage= image; 521 } 522 523 530 public Image getLeftImage(Object element) { 531 return labelProvider.getLeftImage(element); 532 } 533 534 536 541 public void setRightEditable(boolean editable) { 542 fRightEditable= editable; 543 } 544 545 550 public boolean isRightEditable() { 551 return fRightEditable; 552 } 553 554 563 public void setRightLabel(String label) { 564 fRightLabel= label; 565 } 566 567 574 public String getRightLabel(Object element) { 575 return labelProvider.getRightLabel(element); 576 } 577 578 588 public void setRightImage(Image image) { 589 fRightImage= image; 590 } 591 592 599 public Image getRightImage(Object element) { 600 return labelProvider.getRightImage(element); 601 } 602 603 608 public ICompareContainer getContainer() { 609 if (fContainer == null) { 610 fContainer= new CompareContainer(); 612 } 613 return fContainer; 614 } 615 616 621 public void setContainer(ICompareContainer container) { 622 fContainer = container; 623 } 624 625 638 public ICompareInputLabelProvider getLabelProvider() { 639 return labelProvider; 640 } 641 642 651 public void setLabelProvider(ICompareInput input, ICompareInputLabelProvider labelProvider) { 652 this.labelProvider.setLabelProvider(input, labelProvider); 653 } 654 655 667 public void setDefaultLabelProvider(ICompareInputLabelProvider labelProvider) { 668 this.labelProvider.setDefaultLabelProvider(labelProvider); 669 } 670 671 } 672 673 | Popular Tags |