StructureView updating fixed

This commit is contained in:
Olesya Smirnova
2005-02-08 21:19:17 +03:00
parent 140187dbfb
commit 15b9bbdaae
11 changed files with 84 additions and 145 deletions
@@ -38,6 +38,8 @@ import com.intellij.navigation.ItemPresentation;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.Iconable;
import com.intellij.psi.PsiElement;
import com.intellij.psi.SmartPointerManager;
import com.intellij.psi.SmartPsiElementPointer;
import javax.swing.*;
import java.util.ArrayList;
@@ -45,12 +47,20 @@ import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public abstract class PsiTreeElementBase implements StructureViewTreeElement, ItemPresentation {
public abstract class PsiTreeElementBase<Value extends PsiElement> implements StructureViewTreeElement, ItemPresentation {
private final SmartPsiElementPointer mySmartPsiElementPointer;
protected PsiTreeElementBase(PsiElement psiElement) {
mySmartPsiElementPointer = SmartPointerManager.getInstance(psiElement.getProject()).createSmartPsiElementPointer(psiElement);
}
public ItemPresentation getPresentation() {
return this;
}
public abstract PsiElement getElement();
public final Value getElement(){
return (Value)mySmartPsiElementPointer.getElement();
}
public Icon getIcon(boolean open) {
final PsiElement element = getElement();
@@ -92,20 +102,4 @@ public abstract class PsiTreeElementBase implements StructureViewTreeElement, It
}
public abstract StructureViewTreeElement[] getChildrenBase();
public int hashCode() {
if (getElement() == null) {
return 0;
} else {
return getElement().hashCode();
}
}
public boolean equals(Object object) {
if (object instanceof PsiTreeElementBase) {
return Comparing.equal(getElement(), ((PsiTreeElementBase)object).getElement());
} else {
return false;
}
}
}
@@ -7,12 +7,9 @@ import com.intellij.psi.scope.util.PsiScopesUtil;
import java.util.*;
public class JavaClassTreeElement extends JavaClassTreeElementBase {
private final PsiClass myClass;
public class JavaClassTreeElement extends JavaClassTreeElementBase<PsiClass> {
public JavaClassTreeElement(PsiClass aClass, boolean inherited) {
super(inherited);
myClass = aClass;
super(inherited,aClass);
}
public StructureViewTreeElement[] getChildrenBase() {
@@ -23,17 +20,17 @@ public class JavaClassTreeElement extends JavaClassTreeElementBase {
public Collection<StructureViewTreeElement> getClassChildren() {
ArrayList<StructureViewTreeElement> array = new ArrayList<StructureViewTreeElement>();
List<PsiElement> ownChildren = Arrays.asList(myClass.getChildren());
List<PsiElement> ownChildren = Arrays.asList(getElement().getChildren());
ArrayList<PsiElement> inherited = new ArrayList<PsiElement>();
for (Iterator<PsiElement> iterator = ownChildren.iterator(); iterator.hasNext();) {
PsiElement psiElement = iterator.next();
if (psiElement.isValid()) inherited.add(psiElement);
}
PsiScopesUtil.processScope(myClass, new AddAllMembersProcessor(inherited, myClass, new AddAllMembersProcessor.MemberFilter() {
PsiScopesUtil.processScope(getElement(), new AddAllMembersProcessor(inherited, getElement(), new AddAllMembersProcessor.MemberFilter() {
protected boolean isVisible(PsiModifierListOwner member) {
return true;
}
}), PsiSubstitutor.UNKNOWN, null, myClass);
}), PsiSubstitutor.UNKNOWN, null, getElement());
for (Iterator iterator = inherited.iterator(); iterator.hasNext();) {
PsiElement child = (PsiElement)iterator.next();
@@ -61,15 +58,11 @@ public class JavaClassTreeElement extends JavaClassTreeElementBase {
}
public String getPresentableText() {
return myClass.getName();
}
public PsiElement getElement() {
return myClass;
return getElement().getName();
}
public boolean isPublic() {
if (myClass.getParent() instanceof PsiFile){
if (getElement().getParent() instanceof PsiFile){
return true;
} else {
return super.isPublic();
@@ -1,21 +1,17 @@
package com.intellij.ide.structureView.impl.java;
import com.intellij.ide.util.treeView.smartTree.TreeElement;
import com.intellij.ide.structureView.impl.common.PsiTreeElementBase;
import com.intellij.navigation.ItemPresentation;
import com.intellij.openapi.util.Iconable;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiModifier;
import com.intellij.psi.PsiModifierListOwner;
import com.intellij.psi.util.PsiUtil;
import javax.swing.*;
public abstract class JavaClassTreeElementBase extends PsiTreeElementBase implements
public abstract class JavaClassTreeElementBase<Value extends PsiElement> extends PsiTreeElementBase<Value> implements
AccessLevelProvider {
protected final boolean myIsInherited;
protected JavaClassTreeElementBase(boolean isInherited) {
protected JavaClassTreeElementBase(boolean isInherited, PsiElement element) {
super(element);
myIsInherited = isInherited;
}
@@ -24,7 +20,7 @@ public abstract class JavaClassTreeElementBase extends PsiTreeElementBase implem
}
public boolean isPublic() {
PsiElement element = getElement();
Value element = getElement();
if (element instanceof PsiModifierListOwner) {
return ((PsiModifierListOwner)element).hasModifierProperty(PsiModifier.PUBLIC);
}
@@ -1,69 +1,31 @@
package com.intellij.ide.structureView.impl.java;
import com.intellij.ide.structureView.StructureViewTreeElement;
import com.intellij.ide.structureView.impl.common.PsiTreeElementBase;
import com.intellij.navigation.ItemPresentation;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiJavaFile;
import com.intellij.openapi.util.Comparing;
import javax.swing.*;
import java.util.ArrayList;
public class JavaFileTreeElement implements StructureViewTreeElement, ItemPresentation {
private final PsiJavaFile myFile;
public class JavaFileTreeElement extends PsiTreeElementBase<PsiJavaFile> implements ItemPresentation {
public JavaFileTreeElement(PsiJavaFile file) {
myFile = file;
super(file);
}
public StructureViewTreeElement[] getChildren() {
PsiClass[] classes = myFile.getClasses();
public String getPresentableText() {
return getElement().getName();
}
public StructureViewTreeElement[] getChildrenBase() {
PsiClass[] classes = getElement().getClasses();
ArrayList<StructureViewTreeElement> result = new ArrayList<StructureViewTreeElement>();
for (int i = 0; i < classes.length; i++) {
PsiClass aClass = classes[i];
result.add(new JavaClassTreeElement(aClass, false));
}
return result.toArray(new StructureViewTreeElement[result.size()]);
}
public ItemPresentation getPresentation() {
return this;
}
public Icon getIcon(boolean open) {
return myFile.getVirtualFile().getFileType().getIcon();
}
public String getLocationString() {
return null;
}
public String getPresentableText() {
return myFile.getName();
}
public Object getValue() {
return myFile;
}
public String toString() {
return getPresentableText();
}
public int hashCode() {
if (myFile == null) {
return 0;
} else {
return myFile.hashCode();
}
}
public boolean equals(Object object) {
if (object instanceof JavaFileTreeElement) {
return Comparing.equal(myFile, ((JavaFileTreeElement)object).myFile);
} else {
return false;
}
}
}
@@ -2,16 +2,12 @@ package com.intellij.ide.structureView.impl.java;
import com.intellij.ide.structureView.StructureViewTreeElement;
import com.intellij.navigation.ItemPresentation;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiField;
public class PsiFieldTreeElement extends JavaClassTreeElementBase{
private final PsiField myField;
public class PsiFieldTreeElement extends JavaClassTreeElementBase<PsiField>{
public PsiFieldTreeElement(PsiField field, boolean isInherited) {
super(isInherited);
myField = field;
}
super(isInherited,field);
}
public StructureViewTreeElement[] getChildrenBase() {
return new StructureViewTreeElement[0];
@@ -21,15 +17,11 @@ public class PsiFieldTreeElement extends JavaClassTreeElementBase{
return this;
}
public PsiElement getElement() {
return myField;
}
public String getPresentableText() {
return myField.getName();
return getElement().getName();
}
public PsiField getField() {
return myField;
return getElement();
}
}
@@ -2,17 +2,13 @@ package com.intellij.ide.structureView.impl.java;
import com.intellij.ide.structureView.StructureViewTreeElement;
import com.intellij.navigation.ItemPresentation;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiSubstitutor;
import com.intellij.psi.util.PsiFormatUtil;
public class PsiMethodTreeElement extends JavaClassTreeElementBase {
private final PsiMethod myMethod;
public class PsiMethodTreeElement extends JavaClassTreeElementBase<PsiMethod> {
public PsiMethodTreeElement(PsiMethod method, boolean isInherited) {
super(isInherited);
myMethod = method;
super(isInherited,method);
}
public StructureViewTreeElement[] getChildrenBase() {
@@ -23,19 +19,15 @@ public class PsiMethodTreeElement extends JavaClassTreeElementBase {
return this;
}
public PsiElement getElement() {
return myMethod;
}
public String getPresentableText() {
return PsiFormatUtil.formatMethod(
myMethod,
getElement(),
PsiSubstitutor.EMPTY, PsiFormatUtil.SHOW_NAME | PsiFormatUtil.SHOW_TYPE | PsiFormatUtil.TYPE_AFTER | PsiFormatUtil.SHOW_PARAMETERS,
PsiFormatUtil.SHOW_TYPE
);
}
public PsiMethod getMethod() {
return myMethod;
return getElement();
}
}
@@ -33,30 +33,23 @@ package com.intellij.ide.structureView.impl.xml;
import com.intellij.ide.structureView.StructureViewTreeElement;
import com.intellij.ide.structureView.impl.common.PsiTreeElementBase;
import com.intellij.psi.PsiElement;
import com.intellij.psi.xml.XmlDocument;
import com.intellij.psi.xml.XmlFile;
public class XmlFileTreeElement extends PsiTreeElementBase {
private final XmlFile myFile;
public class XmlFileTreeElement extends PsiTreeElementBase<XmlFile> {
public XmlFileTreeElement(XmlFile file) {
myFile = file;
super(file);
}
public StructureViewTreeElement[] getChildrenBase() {
XmlDocument document = myFile.getDocument();
XmlDocument document = getElement().getDocument();
if (document != null) {
return new StructureViewTreeElement[]{new XmlTagTreeElement(document.getRootTag())};
}
return new StructureViewTreeElement[0];
}
public PsiElement getElement() {
return myFile;
}
public String getPresentableText() {
return myFile.getName();
return getElement().getName();
}
}
@@ -33,19 +33,16 @@ package com.intellij.ide.structureView.impl.xml;
import com.intellij.ide.structureView.StructureViewTreeElement;
import com.intellij.ide.structureView.impl.common.PsiTreeElementBase;
import com.intellij.psi.PsiElement;
import com.intellij.psi.xml.XmlTag;
import com.intellij.psi.xml.XmlAttribute;
import com.intellij.psi.xml.XmlTag;
public class XmlTagTreeElement extends PsiTreeElementBase{
private final XmlTag myTag;
public class XmlTagTreeElement extends PsiTreeElementBase<XmlTag>{
public XmlTagTreeElement(XmlTag tag) {
myTag = tag;
super(tag);
}
public StructureViewTreeElement[] getChildrenBase() {
XmlTag[] subTags = myTag.getSubTags();
XmlTag[] subTags = getElement().getSubTags();
StructureViewTreeElement[] result = new StructureViewTreeElement[subTags.length];
for (int i = 0; i < result.length; i++) {
result[i] = new XmlTagTreeElement(subTags[i]);
@@ -53,14 +50,10 @@ public class XmlTagTreeElement extends PsiTreeElementBase{
return result;
}
public PsiElement getElement() {
return myTag;
}
public String getPresentableText() {
final StringBuffer buffer = new StringBuffer();
buffer.append(myTag.getName());
final XmlAttribute[] attributes = myTag.getAttributes();
buffer.append(getElement().getName());
final XmlAttribute[] attributes = getElement().getAttributes();
for (int i = 0; i < attributes.length; i++) {
appendAttribute(attributes[i], buffer);
}
@@ -1,13 +1,15 @@
package com.intellij.ide.util.treeView.smartTree;
import com.intellij.ide.structureView.StructureViewTreeElement;
import com.intellij.ide.util.treeView.AbstractTreeNode;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Comparing;
import java.util.*;
public abstract class CachingChildrenTreeNode <Value> extends AbstractTreeNode<Value> {
private List<CachingChildrenTreeNode> myChildren;
private List<CachingChildrenTreeNode> myOldChildren = null;
protected List<CachingChildrenTreeNode> myOldChildren = null;
protected final TreeModel myTreeModel;
public CachingChildrenTreeNode(Project project, Value value, TreeModel treeModel) {
@@ -174,17 +176,37 @@ public abstract class CachingChildrenTreeNode <Value> extends AbstractTreeNode<V
if (myOldChildren != null) {
for (Iterator<CachingChildrenTreeNode> iterator = myOldChildren.iterator(); iterator.hasNext();) {
CachingChildrenTreeNode oldInstance = iterator.next();
if (myChildren.contains(oldInstance)) {
final int newIndex = myChildren.indexOf(oldInstance);
final int newIndex = getIndexOfPointerToTheSameValue(oldInstance);
if (newIndex >= 0) {
final CachingChildrenTreeNode newInstance = myChildren.get(newIndex);
oldInstance.copyFromNew(newInstance);
newInstance.copyFromNewInstance(oldInstance);
oldInstance.setValue(newInstance.getValue());
myChildren.set(newIndex, oldInstance);
}
}
}
}
protected abstract void copyFromNew(final CachingChildrenTreeNode newInstance);
private int getIndexOfPointerToTheSameValue(final CachingChildrenTreeNode oldInstance) {
for (int i = 0; i < myChildren.size(); i++) {
CachingChildrenTreeNode newInstance = myChildren.get(0);
if (newInstance instanceof TreeElementWrapper) {
final StructureViewTreeElement newElement = ((StructureViewTreeElement)newInstance.getValue());
if (oldInstance instanceof TreeElementWrapper) {
final StructureViewTreeElement oldElement = ((StructureViewTreeElement)oldInstance.getValue());
if (newElement.getValue() != null) {
if (Comparing.equal(newElement.getValue(), oldElement.getValue())) return i;
}
}
} else {
if (newInstance.equals(oldInstance)) return i;
}
}
return -1;
}
protected abstract void copyFromNewInstance(final CachingChildrenTreeNode newInstance);
protected abstract void performTreeActions();
@@ -9,7 +9,7 @@ class GroupWrapper extends CachingChildrenTreeNode<Group> {
super(project, value, treeModel);
}
public void copyFromNew(final CachingChildrenTreeNode newInstance) {
public void copyFromNewInstance(final CachingChildrenTreeNode newInstance) {
clearChildren();
setChildren(newInstance.getChildren());
synchronizeChildren();
@@ -1,6 +1,7 @@
package com.intellij.ide.util.treeView.smartTree;
import com.intellij.ide.projectView.PresentationData;
import com.intellij.ide.structureView.StructureViewTreeElement;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
@@ -9,8 +10,7 @@ class TreeElementWrapper extends CachingChildrenTreeNode<TreeElement>{
super(project, value, treeModel);
}
public void copyFromNew(final CachingChildrenTreeNode newInstance) {
public void copyFromNewInstance(final CachingChildrenTreeNode oldInstance) {
}
public void initChildren() {
@@ -27,7 +27,9 @@ class TreeElementWrapper extends CachingChildrenTreeNode<TreeElement>{
}
public void update(PresentationData presentation) {
presentation.updateFrom(getValue().getPresentation());
if (((StructureViewTreeElement)getValue()).getValue() != null){
presentation.updateFrom(getValue().getPresentation());
}
}
protected void performTreeActions() {