1 15 16 package org.eclipse.ant.internal.ui.model; 17 18 import java.io.File ; 19 import java.net.MalformedURLException ; 20 import java.net.URL ; 21 import com.ibm.icu.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.AntImageDescriptor; 27 import org.eclipse.ant.internal.ui.AntUIImages; 28 import org.eclipse.ant.internal.ui.AntUtil; 29 import org.eclipse.ant.internal.ui.IAntUIConstants; 30 import org.eclipse.core.resources.IFile; 31 import org.eclipse.core.runtime.IAdaptable; 32 import org.eclipse.core.runtime.Path; 33 import org.eclipse.core.runtime.Platform; 34 import org.eclipse.jface.resource.ImageDescriptor; 35 import org.eclipse.jface.text.IRegion; 36 import org.eclipse.swt.graphics.Image; 37 38 42 public class AntElementNode implements IAdaptable { 43 44 48 protected int fOffset= -1; 49 50 54 protected int fLength= -1; 55 56 59 protected int fSelectionLength; 60 61 64 protected AntElementNode fParent; 65 66 69 private AntElementNode fImportNode; 70 71 74 protected List fChildNodes= null; 75 76 79 protected String fName; 80 81 89 private int fProblemSeverity= AntModelProblem.NO_PROBLEM; 90 91 private String fProblemMessage= null; 92 93 97 private String fFilePath; 98 99 102 private boolean fIsExternal = false; 103 104 107 private String fElementPath; 108 109 112 private String fElementIdentifier; 113 114 117 private IProblem fProblem; 118 119 122 private int fIndex= 0; 123 124 127 private int fLine; 128 private int fColumn; 129 130 133 public AntElementNode(String aName) { 134 fName = aName; 135 } 136 137 public AntElementNode() { 138 } 139 140 143 public String getName() { 144 return fName; 145 } 146 147 154 public String getLabel() { 155 return getName(); 156 } 157 158 161 public List getChildNodes() { 162 return fChildNodes; 163 } 164 165 170 public AntElementNode getParentNode() { 171 return fParent; 172 } 173 174 public AntProjectNode getProjectNode() { 175 AntElementNode projectParent= getParentNode(); 176 while (projectParent != null && !(projectParent instanceof AntProjectNode)) { 177 projectParent= projectParent.getParentNode(); 178 } 179 return (AntProjectNode)projectParent; 180 } 181 182 187 public void addChildNode(AntElementNode childElement) { 188 childElement.setParent(this); 189 synchronized (this) { 190 if (fChildNodes == null) { 191 fChildNodes= new ArrayList (); 192 } 193 fChildNodes.add(childElement); 194 childElement.setIndex(fChildNodes.size() - 1); 195 } 196 } 197 198 private void setIndex(int index) { 199 fIndex= index; 200 } 201 202 protected void setParent(AntElementNode node) { 203 fParent= node; 204 } 205 206 210 public void setFilePath(String path) { 211 if (path == null) { 212 return; 213 } 214 URL url= null; 215 try { 216 url= new URL (path); 217 } catch (MalformedURLException e) { 218 fFilePath= path; 219 return; 220 } 221 fFilePath = new Path(new File (url.getPath()).getAbsolutePath()).toString(); 222 } 223 224 229 public String getFilePath() { 230 return fFilePath; 231 } 232 233 240 public int getOffset() { 241 return fOffset; 242 } 243 244 249 public void setOffset(int anOffset) { 250 fOffset = anOffset; 251 } 252 253 260 public int getLength() { 261 return fLength; 262 } 263 264 269 public void setLength(int aLength) { 270 fLength = aLength; 271 if (fProblem != null && fProblem instanceof AntModelProblem) { 272 ((AntModelProblem)fProblem).setLength(aLength); 273 } 274 } 275 276 279 public String toString() { 280 return "Ant Element Node: " + getLabel() + " Offset: " + getOffset() + " Length: " + getLength(); } 282 283 287 public boolean isErrorNode() { 288 return fProblemSeverity == AntModelProblem.SEVERITY_ERROR || fProblemSeverity == AntModelProblem.SEVERITY_FATAL_ERROR; 289 } 290 291 295 public boolean isWarningNode() { 296 return fProblemSeverity == AntModelProblem.SEVERITY_WARNING; 297 } 298 299 303 public void setProblemSeverity(int severity) { 304 fProblemSeverity= severity; 305 } 306 307 312 public boolean isExternal() { 313 return fIsExternal; 314 } 315 316 319 public void setExternal(boolean isExternal) { 320 fIsExternal = isExternal; 321 } 322 323 329 public String getElementPath() { 330 if (fElementPath == null) { 331 StringBuffer buffer= new StringBuffer (); 332 String buildFileName= getProjectNode().getBuildFileName(); 333 if (buildFileName != null) { 334 buffer.append(buildFileName); 335 } 336 buffer.append(getParentNode() != null ? getParentNode().getElementPath() : ""); buffer.append('/'); 338 buffer.append(getElementIdentifier()); 339 buffer.append('['); 340 buffer.append(fIndex); 341 buffer.append(']'); 342 343 fElementPath= buffer.toString(); 344 } 345 return fElementPath; 346 } 347 348 private String getElementIdentifier() { 349 if (fElementIdentifier == null) { 350 StringBuffer buffer= escape(new StringBuffer (getName() != null ? getName() : ""), '\\', "$/[]\\"); buffer.append('$'); 352 buffer.append(escape(new StringBuffer (getLabel() != null ? getLabel() : ""), '\\', "$/[]\\").toString()); 354 fElementIdentifier= buffer.toString(); 355 } 356 return fElementIdentifier; 357 } 358 359 private StringBuffer escape(StringBuffer sb, char esc, String special) { 360 for (int i= 0; i < sb.length(); i++) { 361 if (special.indexOf(sb.charAt(i)) >= 0) { 362 sb.insert(i++, esc); 363 } 364 } 365 366 return sb; 367 } 368 369 391 394 public boolean equals(Object o2) { 395 Object o1= this; 396 397 if (o1 == o2) { 398 return true; 399 } 400 if (o2 == null) { 401 return false; 402 } 403 if (!(o1 instanceof AntElementNode || o2 instanceof AntElementNode)) { 404 return o2.equals(o1); 405 } 406 if (!(o1 instanceof AntElementNode && o2 instanceof AntElementNode)) { 407 return false; 408 } 409 410 AntElementNode e1= (AntElementNode) o1; 411 AntElementNode e2= (AntElementNode) o2; 412 413 return e1.getElementPath().equals(e2.getElementPath()); 414 } 415 416 419 public int hashCode() { 420 return getElementPath().hashCode(); 421 } 422 423 427 public int getSelectionLength() { 428 return fSelectionLength; 429 } 430 431 public void setSelectionLength(int selectionLength) { 432 this.fSelectionLength= selectionLength; 433 } 434 435 441 public AntElementNode getNode(int sourceOffset) { 442 synchronized (this) { 443 if (fChildNodes != null) { 444 for (Iterator iter = fChildNodes.iterator(); iter.hasNext(); ) { 445 AntElementNode node = (AntElementNode) iter.next(); 446 AntElementNode containingNode= node.getNode(sourceOffset); 447 if (containingNode != null) { 448 return containingNode; 449 } 450 } 451 } 452 } 453 if (fLength == -1 && fOffset <= sourceOffset && !isExternal()) { return this; 455 } 456 if (fOffset <= sourceOffset && sourceOffset <= (fOffset + fLength - 2)) { 457 return this; 458 } 459 460 return null; 461 } 462 463 public Image getImage() { 464 int flags = 0; 465 466 if (isErrorNode()) { 467 flags = flags | AntImageDescriptor.HAS_ERRORS; 468 } else if (isWarningNode()) { 469 flags = flags | AntImageDescriptor.HAS_WARNINGS; 470 } 471 if(fImportNode != null || isExternal()){ 472 flags = flags | AntImageDescriptor.IMPORTED; 473 } 474 ImageDescriptor base= getBaseImageDescriptor(); 475 return AntUIImages.getImage(new AntImageDescriptor(base, flags)); 476 } 477 478 protected ImageDescriptor getBaseImageDescriptor() { 479 return AntUIImages.getImageDescriptor(IAntUIConstants.IMG_TASK_PROPOSAL); 480 } 481 482 protected IAntModel getAntModel() { 483 AntElementNode parentNode= getParentNode(); 484 while (!(parentNode instanceof AntProjectNode)) { 485 parentNode= parentNode.getParentNode(); 486 } 487 return parentNode.getAntModel(); 488 } 489 490 494 public void associatedProblem(IProblem problem) { 495 fProblem= problem; 496 } 497 498 public IProblem getProblem() { 499 return fProblem; 500 } 501 502 protected void appendEntityName(StringBuffer displayName) { 503 String path= getFilePath(); 504 505 if (getImportNode() != null) { 506 displayName.append(MessageFormat.format(AntModelMessages.AntElementNode_9, new String []{getImportNode().getLabel()})); 507 } else { 508 String entityName= getAntModel().getEntityName(path); 509 displayName.append(MessageFormat.format(AntModelMessages.AntElementNode_9, new String []{entityName})); 510 } 511 } 512 513 public AntElementNode getImportNode() { 514 return fImportNode; 515 } 516 517 public void setImportNode(AntElementNode importNode) { 518 fImportNode = importNode; 519 } 520 521 public boolean hasChildren() { 522 if (fChildNodes == null) { 523 return false; 524 } 525 return !fChildNodes.isEmpty(); 526 } 527 528 public void reset() { 529 fChildNodes= null; 530 } 531 532 public void setExternalInfo(int line, int column) { 533 fLine= line; 534 fColumn= column; 535 } 536 537 public int[] getExternalInfo() { 538 return new int[] {fLine, fColumn}; 539 } 540 541 547 public IFile getIFile() { 548 if (isExternal()) { 549 return AntUtil.getFileForLocation(fFilePath, null); 550 } 551 return getBuildFileResource(); 552 } 553 554 560 public IFile getBuildFileResource() { 561 LocationProvider locationProvider= getAntModel().getLocationProvider(); 562 return locationProvider.getFile(); 563 } 564 565 568 public Object getAdapter(Class adapter) { 569 return Platform.getAdapterManager().getAdapter(this, adapter); 570 } 571 572 578 public boolean isStructuralNode() { 579 return true; 580 } 581 582 587 public boolean collapseProjection() { 588 return false; 589 } 590 591 public void dispose() { 592 getAntModel().dispose(); 593 } 594 595 601 public String getReferencedElement(int offset) { 602 return null; 603 } 604 605 public String getProblemMessage() { 606 return fProblemMessage; 607 } 608 609 public void setProblemMessage(String problemMessage) { 610 fProblemMessage = problemMessage; 611 } 612 613 619 public boolean containsOccurrence(String identifier) { 620 return false; 621 } 622 623 628 public String getOccurrencesIdentifier() { 629 return getLabel(); 630 } 631 632 639 public boolean isRegionPotentialReference(IRegion region) { 640 return region.getOffset() >= fOffset; 641 } 642 643 public List computeIdentifierOffsets(String identifier) { 644 return null; 645 } 646 647 654 public boolean isFromDeclaration(IRegion region) { 655 return false; 656 } 657 658 protected boolean checkReferenceRegion(IRegion region, String textToSearch, String attributeName) { 659 int attributeOffset= textToSearch.indexOf(attributeName); 660 while (attributeOffset > 0 && !Character.isWhitespace(textToSearch.charAt(attributeOffset - 1))) { 661 attributeOffset= textToSearch.indexOf(attributeName, attributeOffset + 1); 662 } 663 if (attributeOffset != -1) { 664 attributeOffset+= attributeName.length(); 665 int attributeOffsetEnd = textToSearch.indexOf('"', attributeOffset); 666 attributeOffsetEnd = textToSearch.indexOf('"', attributeOffsetEnd + 1); 667 return region.getOffset() >= getOffset() + attributeOffset && (region.getOffset() + region.getLength()) <= getOffset() + attributeOffsetEnd; 668 } 669 return false; 670 } 671 } | Popular Tags |