1 11 package org.eclipse.compare.structuremergeviewer; 12 13 import com.ibm.icu.text.MessageFormat; 14 15 import org.eclipse.swt.graphics.Image; 16 17 import org.eclipse.core.runtime.ListenerList; 18 19 import org.eclipse.compare.*; 20 import org.eclipse.compare.internal.Utilities; 21 22 37 public class DiffNode extends DiffContainer implements ITypedElement, ICompareInput { 38 39 private ITypedElement fAncestor; 40 private ITypedElement fLeft; 41 private ITypedElement fRight; 42 private boolean fDontExpand; 43 private ListenerList fListener; 44 private boolean fSwapSides; 45 46 47 56 public DiffNode(IDiffContainer parent, int kind, ITypedElement ancestor, ITypedElement left, ITypedElement right) { 57 this(parent, kind); 58 fAncestor= ancestor; 59 fLeft= left; 60 fRight= right; 61 } 62 63 70 public DiffNode(ITypedElement left, ITypedElement right) { 71 this(null, Differencer.CHANGE, null, left, right); 72 } 73 74 82 public DiffNode(int kind, ITypedElement ancestor, ITypedElement left, ITypedElement right) { 83 this(null, kind, ancestor, left, right); 84 } 85 86 91 public DiffNode(int kind) { 92 super(null, kind); 93 } 94 95 101 public DiffNode(IDiffContainer parent, int kind) { 102 super(parent, kind); 103 } 104 105 111 public void addCompareInputChangeListener(ICompareInputChangeListener listener) { 112 if (fListener == null) 113 fListener= new ListenerList(); 114 fListener.add(listener); 115 } 116 117 123 public void removeCompareInputChangeListener(ICompareInputChangeListener listener) { 124 if (fListener != null) { 125 fListener.remove(listener); 126 if (fListener.isEmpty()) 127 fListener= null; 128 } 129 } 130 131 134 protected void fireChange() { 135 if (fListener != null) { 136 Object [] listeners= fListener.getListeners(); 137 for (int i= 0; i < listeners.length; i++) 138 ((ICompareInputChangeListener) listeners[i]).compareInputChanged(this); 139 } 140 } 141 142 144 150 public boolean dontExpand() { 151 return fDontExpand; 152 } 153 154 160 public void setDontExpand(boolean dontExpand) { 161 fDontExpand= dontExpand; 162 } 163 164 170 public ITypedElement getId() { 171 if (fAncestor != null) 172 return fAncestor; 173 if (fRight != null) 174 return fRight; 175 return fLeft; 176 } 177 178 185 public String getName() { 186 String right= null; 187 if (fRight != null) 188 right= fRight.getName(); 189 190 String left= null; 191 if (fLeft != null) 192 left= fLeft.getName(); 193 194 if (right == null && left == null) { 195 if (fAncestor != null) 196 return fAncestor.getName(); 197 return Utilities.getString("DiffNode.noName"); } 199 200 if (right == null) 201 return left; 202 if (left == null) 203 return right; 204 205 if (right.equals(left)) 206 return right; 207 208 String s1; 209 String s2; 210 211 if (fSwapSides) { 212 s1= left; 213 s2= right; 214 } else { 215 s1= right; 216 s2= left; 217 } 218 219 String fmt= Utilities.getString("DiffNode.nameFormat"); return MessageFormat.format(fmt, new String [] { s1, s2 }); 221 } 222 223 void swapSides(boolean swap) { 224 fSwapSides= swap; 225 } 226 227 230 public Image getImage() { 231 ITypedElement id= getId(); 232 if (id != null) 233 return id.getImage(); 234 return null; 235 } 236 237 240 public String getType() { 241 ITypedElement id= getId(); 242 if (id != null) 243 return id.getType(); 244 return ITypedElement.UNKNOWN_TYPE; 245 } 246 247 253 public void setAncestor(ITypedElement ancestor) { 254 fAncestor= ancestor; 255 } 256 257 260 public ITypedElement getAncestor() { 261 return fAncestor; 262 } 263 264 269 public void setLeft(ITypedElement left) { 270 fLeft= left; 271 } 272 273 276 public ITypedElement getLeft() { 277 return fLeft; 278 } 279 280 285 public void setRight(ITypedElement right) { 286 fRight= right; 287 } 288 289 292 public ITypedElement getRight() { 293 return fRight; 294 } 295 296 299 public void copy(boolean leftToRight) { 300 302 IDiffContainer pa= getParent(); 303 if (pa instanceof ICompareInput) { 304 ICompareInput parent= (ICompareInput) pa; 305 Object dstParent= leftToRight ? parent.getRight() : parent.getLeft(); 306 307 if (dstParent instanceof IEditableContent) { 308 ITypedElement dst= leftToRight ? getRight() : getLeft(); 309 ITypedElement SRC= leftToRight ? getLeft() : getRight(); 310 dst= ((IEditableContent)dstParent).replace(dst, src); 311 if (leftToRight) 312 setRight(dst); 313 else 314 setLeft(dst); 315 316 318 fireChange(); 319 } 320 } 321 } 322 323 326 public int hashCode() { 327 String [] path= getPath(this, 0); 328 int hashCode= 1; 329 for (int i= 0; i < path.length; i++) { 330 String s= path[i]; 331 hashCode= (31*hashCode) + (s != null ? s.hashCode() : 0); 332 } 333 return hashCode; 334 } 335 336 339 public boolean equals(Object other) { 340 if (other != null && getClass() == other.getClass()) { 341 String [] path1= getPath(this, 0); 342 String [] path2= getPath((DiffNode) other, 0); 343 if (path1.length != path2.length) 344 return false; 345 for (int i= 0; i < path1.length; i++) 346 if (! path1[i].equals(path2[i])) 347 return false; 348 return true; 349 } 350 return super.equals(other); 351 } 352 353 private static String [] getPath(ITypedElement el, int level) { 354 String [] path= null; 355 if (el instanceof IDiffContainer) { 356 IDiffContainer parent= ((IDiffContainer)el).getParent(); 357 if (parent != null) 358 path= getPath(parent, level+1); 359 } 360 if (path == null) 361 path= new String [level+1]; 362 path[(path.length-1)-level]= el.getName(); 363 return path; 364 } 365 } 366 | Popular Tags |