1 15 16 package org.eclipse.ant.internal.ui.editor.model; 17 18 import java.io.File ; 19 import java.net.MalformedURLException ; 20 import java.net.URL ; 21 import java.text.MessageFormat ; 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 26 import org.eclipse.ant.internal.ui.editor.outline.AntModel; 27 import org.eclipse.ant.internal.ui.editor.outline.IProblem; 28 import org.eclipse.ant.internal.ui.editor.outline.LocationProvider; 29 import org.eclipse.ant.internal.ui.editor.outline.XMLProblem; 30 import org.eclipse.ant.internal.ui.model.AntImageDescriptor; 31 import org.eclipse.ant.internal.ui.model.AntUIImages; 32 import org.eclipse.ant.internal.ui.model.AntUtil; 33 import org.eclipse.ant.internal.ui.model.IAntUIConstants; 34 import org.eclipse.core.resources.IFile; 35 import org.eclipse.core.runtime.IAdaptable; 36 import org.eclipse.core.runtime.Path; 37 import org.eclipse.core.runtime.Platform; 38 import org.eclipse.jface.resource.ImageDescriptor; 39 import org.eclipse.swt.graphics.Image; 40 41 45 public class AntElementNode implements IAdaptable { 46 47 51 protected int offset= -1; 52 53 57 protected int length= -1; 58 59 62 protected int selectionLength; 63 64 67 protected AntElementNode parent; 68 69 72 private AntElementNode importNode; 73 74 77 protected List childNodes= null; 78 79 82 protected String name; 83 84 92 private int problemSeverity= XMLProblem.NO_PROBLEM; 93 94 98 private String filePath; 99 100 103 private boolean isExternal = false; 104 105 108 private String fElementPath; 109 110 113 private String fElementIdentifier; 114 115 118 private IProblem fProblem; 119 120 123 private int fLine; 124 private int fColumn; 125 126 129 public AntElementNode(String aName) { 130 name = aName; 131 } 132 133 136 public AntElementNode() { 137 } 138 139 140 143 public String getName() { 144 return name; 145 } 146 147 148 155 public String getLabel() { 156 return getName(); 157 } 158 159 160 163 public List getChildNodes() { 164 return childNodes; 165 } 166 167 170 public List getDescendents() { 171 if (childNodes == null) { 172 return null; 173 } 174 List descendents= new ArrayList (); 175 determineDescendents(descendents, childNodes); 176 177 return descendents; 178 } 179 180 181 private void determineDescendents(List descendents, List childrenNodes) { 182 Iterator itr= childrenNodes.iterator(); 183 while (itr.hasNext()) { 184 AntElementNode element = (AntElementNode) itr.next(); 185 if (element.hasChildren()) { 186 determineDescendents(descendents, element.getChildNodes()); 187 } 188 descendents.add(element); 189 } 190 } 191 192 197 public AntElementNode getParentNode() { 198 return parent; 199 } 200 201 public AntProjectNode getProjectNode() { 202 AntElementNode projectParent= getParentNode(); 203 while (projectParent != null && !(projectParent instanceof AntProjectNode)) { 204 projectParent= projectParent.getParentNode(); 205 } 206 return (AntProjectNode)projectParent; 207 } 208 209 210 216 public void addChildNode(AntElementNode childElement) { 217 childElement.setParent(this); 218 if (childNodes == null) { 219 childNodes= new ArrayList (); 220 } 221 childNodes.add(childElement); 222 } 223 224 protected void setParent(AntElementNode node) { 225 parent= node; 226 } 227 228 232 public void setFilePath(String path) { 233 if (path == null) { 234 return; 235 } 236 URL url= null; 237 try { 238 url= new URL (path); 239 } catch (MalformedURLException e) { 240 filePath= path; 241 return; 242 } 243 filePath = new Path(new File (url.getPath()).getAbsolutePath()).toString(); 244 } 245 246 251 public String getFilePath() { 252 return filePath; 253 } 254 255 262 public int getOffset() { 263 return offset; 264 } 265 266 271 public void setOffset(int anOffset) { 272 offset = anOffset; 273 } 274 275 282 public int getLength() { 283 return length; 284 } 285 286 291 public void setLength(int aLength) { 292 length = aLength; 293 if (fProblem != null && fProblem instanceof XMLProblem) { 294 ((XMLProblem)fProblem).setLength(aLength); 295 fProblem= null; 296 } 297 } 298 299 302 public String toString() { 303 return "Ant Element Node: " + getLabel() + " Offset: " + getOffset() + " Length: " + getLength(); } 305 306 310 public boolean isErrorNode() { 311 return problemSeverity == XMLProblem.SEVERITY_ERROR || problemSeverity == XMLProblem.SEVERITY_FATAL_ERROR; 312 } 313 314 318 public boolean isWarningNode() { 319 return problemSeverity == XMLProblem.SEVERITY_WARNING; 320 } 321 322 326 public void setProblemSeverity(int severity) { 327 this.problemSeverity= severity; 328 } 329 330 335 public boolean isExternal() { 336 return isExternal; 337 } 338 339 342 public void setExternal(boolean isExternal) { 343 this.isExternal = isExternal; 344 } 345 346 private String getElementPath() { 347 if (fElementPath == null) { 348 StringBuffer buffer= new StringBuffer (getParentNode() != null ? getParentNode().getElementPath() : ""); buffer.append('/'); 350 buffer.append(getElementIdentifier()); 351 buffer.append('['); 352 buffer.append(getParentNode() != null ? getParentNode().getElementIndexOf(this) : 0); 353 buffer.append(']'); 354 355 fElementPath= buffer.toString(); 356 } 357 return fElementPath; 358 } 359 360 private String getElementIdentifier() { 361 if (fElementIdentifier == null) { 362 StringBuffer buffer= escape(new StringBuffer (getName() != null ? getName() : ""), '\\', "$/[]\\"); buffer.append('$'); 364 buffer.append(escape(new StringBuffer (getLabel() != null ? getLabel() : ""), '\\', "$/[]\\").toString()); 366 fElementIdentifier= buffer.toString(); 367 } 368 return fElementIdentifier; 369 } 370 371 private StringBuffer escape(StringBuffer sb, char esc, String special) { 372 for (int i= 0; i < sb.length(); i++) { 373 if (special.indexOf(sb.charAt(i)) >= 0) { 374 sb.insert(i++, esc); 375 } 376 } 377 378 return sb; 379 } 380 381 private int getElementIndexOf(AntElementNode child) { 382 if (getChildNodes() == null) { 383 return -1; 384 } 385 386 int result= -1; 387 388 Iterator iter= getChildNodes().iterator(); 389 AntElementNode current= null; 390 while (current != child && iter.hasNext()) { 391 current= (AntElementNode) iter.next(); 392 if (child.getElementIdentifier().equals(current.getElementIdentifier())) 393 result++; 394 } 395 396 if (current != child) { 397 return -1; 398 } 399 400 return result; 401 } 402 403 406 public boolean equals(Object o2) { 407 Object o1= this; 409 410 if (o1 == o2) { 411 return true; 412 } 413 if (o1 == null || o2 == null) { 414 return false; 415 } 416 if (!(o1 instanceof AntElementNode || o2 instanceof AntElementNode)) { 417 return o2.equals(o1); 418 } 419 if (!(o1 instanceof AntElementNode && o2 instanceof AntElementNode)) { 420 return false; 421 } 422 423 AntElementNode e1= (AntElementNode) o1; 424 AntElementNode e2= (AntElementNode) o2; 425 426 return e1.getElementPath().equals(e2.getElementPath()); 427 } 428 429 432 public int hashCode() { 433 435 return getElementPath().hashCode(); 436 } 437 438 442 public int getSelectionLength() { 443 return selectionLength; 444 } 445 446 public void setSelectionLength(int selectionLength) { 447 this.selectionLength= selectionLength; 448 } 449 450 456 public AntElementNode getNode(int sourceOffset) { 457 if (childNodes != null) { 458 for (Iterator iter = childNodes.iterator(); iter.hasNext(); ) { 459 AntElementNode node = (AntElementNode) iter.next(); 460 AntElementNode containingNode= node.getNode(sourceOffset); 461 if (containingNode != null) { 462 return containingNode; 463 } 464 } 465 } 466 if (length == -1 && offset <= sourceOffset && !isExternal()) { return this; 468 } 469 if (offset <= sourceOffset && sourceOffset <= (offset + length - 2)) { 470 return this; 471 } 472 473 return null; 474 } 475 476 public Image getImage() { 477 int flags = 0; 478 479 if (isErrorNode()) { 480 flags = flags | AntImageDescriptor.HAS_ERRORS; 481 } else if (isWarningNode()) { 482 flags = flags | AntImageDescriptor.HAS_WARNINGS; 483 } 484 if(importNode != null || isExternal()){ 485 flags = flags | AntImageDescriptor.IMPORTED; 486 } 487 ImageDescriptor base= getBaseImageDescriptor(); 488 return AntUIImages.getImage(new AntImageDescriptor(base, flags)); 489 } 490 491 protected ImageDescriptor getBaseImageDescriptor() { 492 return AntUIImages.getImageDescriptor(IAntUIConstants.IMG_TASK_PROPOSAL); 493 } 494 495 protected AntModel getAntModel() { 496 AntElementNode parentNode= getParentNode(); 497 while (!(parentNode instanceof AntProjectNode)) { 498 parentNode= parentNode.getParentNode(); 499 } 500 return parentNode.getAntModel(); 501 } 502 503 507 public void associatedProblem(IProblem problem) { 508 fProblem= problem; 509 } 510 511 protected void appendEntityName(StringBuffer displayName) { 512 String path= getFilePath(); 513 514 if (getImportNode() != null) { 515 displayName.append(MessageFormat.format(AntModelMessages.getString("AntElementNode.9"), new String []{getImportNode().getLabel()})); } else { 517 String entityName= getAntModel().getEntityName(path); 518 displayName.append(MessageFormat.format(AntModelMessages.getString("AntElementNode.9"), new String []{entityName})); } 520 } 521 522 public AntElementNode getImportNode() { 523 return importNode; 524 } 525 526 public void setImportNode(AntElementNode importNode) { 527 this.importNode = importNode; 528 } 529 530 public boolean hasChildren() { 531 if (childNodes == null) { 532 return false; 533 } 534 return !childNodes.isEmpty(); 535 } 536 537 public void reset() { 538 childNodes= null; 539 } 540 541 public void setExternalInfo(int line, int column) { 542 fLine= line; 543 fColumn= column; 544 } 545 546 public int[] getExternalInfo() { 547 return new int[] {fLine, fColumn}; 548 } 549 550 556 public IFile getIFile() { 557 if (isExternal()) { 558 return AntUtil.getFileForLocation(filePath, null); 559 } 560 return getBuildFileResource(); 561 } 562 563 569 public IFile getBuildFileResource() { 570 LocationProvider locationProvider= getAntModel().getLocationProvider(); 571 return locationProvider.getFile(); 572 } 573 574 577 public Object getAdapter(Class adapter) { 578 return Platform.getAdapterManager().getAdapter(this, adapter); 579 } 580 } | Popular Tags |