|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.modules.refactoring; import org.netbeans.jmi.javamodel.*; import org.netbeans.modules.javacore.internalapi.JavaMetamodel; import org.netbeans.modules.refactoring.api.RefactoringElement; import org.openide.text.PositionBounds; import javax.jmi.reflect.RefObject; public class WhereUsedElement implements RefactoringElement { private final String text; private final String displayText; private final ElementPartKind displayPart; protected final RefObject jmiObject; protected final Element feature; protected final ElementPartKind changePart; //protected final HashSet indexes; private int status; // caches private RefObject comp = null; private PositionBounds bounds = null; public WhereUsedElement(RefObject jmiObject, Element feature) { String sourceText = feature.getResource().getSourceText(); this.feature = feature; this.jmiObject = jmiObject; StringBuffer displayText = new StringBuffer(); final int startOffset, endOffset; int offset; Element displayElement = getDisplayElement(); if (displayElement instanceof CallableFeature || (displayElement instanceof JavaClass && !(displayElement instanceof TypeParameter)) || displayElement instanceof Resource) { startOffset = displayElement.getPartStartOffset(ElementPartKindEnum.HEADER); endOffset = displayElement.getPartEndOffset(ElementPartKindEnum.HEADER); } else { startOffset = displayElement.getStartOffset(); endOffset = displayElement.getEndOffset(); } offset = startOffset; if (feature instanceof Feature) { if (feature instanceof CallableFeature) { displayPart = ElementPartKindEnum.HEADER; changePart = null; offset = appendText(displayText, sourceText, feature, ElementPartKindEnum.NAME, offset); } else if (feature instanceof JavaClass) { displayPart = ElementPartKindEnum.HEADER; JavaClass jc = (JavaClass) feature; changePart = null; } else if (feature instanceof Field) { displayPart = null; changePart = null; offset = appendText(displayText, sourceText, feature, changePart, offset); } else if (feature instanceof Resource) { displayPart = ElementPartKindEnum.HEADER; changePart = null; } else { throw new RuntimeException("Unknown type of object: " + feature.getClass().getName()); //NOI18N } } else { if (feature instanceof Invocation) { changePart = ElementPartKindEnum.NAME; } else if (feature instanceof Parameter || feature instanceof TypeCast || feature instanceof ElementReference) { changePart = null; } else if (feature instanceof Import) { changePart = ElementPartKindEnum.NAME; } else if (feature instanceof Resource) { changePart = ElementPartKindEnum.HEADER; } else { throw new RuntimeException("Unknown type of object: " + feature.getClass().getName()); //NOI18N } displayPart = changePart; //indexes = null; offset = appendText(displayText, sourceText, feature, changePart, offset); } displayText.append(htmlize(sourceText.substring(offset, endOffset))); this.displayText = normalize(displayText.toString()); this.text = normalize(sourceText.substring(startOffset, endOffset)); if (JavaMetamodel.getManager().isElementGuarded(feature)) { status = RefactoringElement.GUARDED; } else { status = RefactoringElement.NORMAL; } this.getPosition(); } private Element getDisplayElement() { if (feature instanceof Resource) { return feature; } if (feature instanceof Import) { return feature; } Element result = feature; while (!((result instanceof ClassMember) || (result instanceof Resource) || (result instanceof Import) || (result.refImmediateComposite() instanceof StatementBlock))) { result = (Element) result.refImmediateComposite(); } return result; } private String normalize(String text) { StringBuffer result = new StringBuffer(text.length()); boolean whitespaces = false; for (int i = 0; i < text.length(); i++) { char ch = text.charAt(i); if (Character.isWhitespace(ch)) { if (!whitespaces) { whitespaces = true; result.append(' '); } } else { result.append(ch); whitespaces = false; } } return result.toString(); } private int appendText(StringBuffer result, String sourceText, Element feature, ElementPartKind part, int startOffset) { int offset = part != null ? feature.getPartStartOffset(part) : feature.getStartOffset(); result.append(htmlize(sourceText.substring(startOffset, offset))); result.append("");//NOI18N startOffset = offset; offset = part != null ? feature.getPartEndOffset(part) : feature.getEndOffset(); result.append(htmlize(sourceText.substring(startOffset, offset))); result.append("");//NOI18N return offset; } private String htmlize(String input) { String temp = org.openide.util.Utilities.replaceString(input, "<", "<"); temp = org.openide.util.Utilities.replaceString(temp, ">", ">"); return temp; } public String getDisplayText() { return displayText; } public Element getJavaElement() { if (comp == null) { comp = feature; while (!((comp instanceof Feature) || (comp instanceof Resource))) { comp = (RefObject) comp.refImmediateComposite(); } } return (Element) comp; } public PositionBounds getPosition() { if (bounds == null) { if (displayPart == null) { bounds = JavaMetamodel.getManager().getElementPosition(feature); } else { bounds = JavaMetamodel.getManager().getElementPartPosition(feature, displayPart, 0); } } return bounds; } public int getStatus() { return status; } public String getText() { return text; } public boolean isEnabled() { return true; } public void performChange() { } public void setEnabled(boolean enabled) { } public void undoChange() { } } |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.