From db058bb8257e2bc2e16974b30bb8d991eb35dc39 Mon Sep 17 00:00:00 2001 From: Sergey Vasiliev Date: Wed, 9 Nov 2005 14:45:15 +0300 Subject: [PATCH] refactored jsf tree structure to DomElements tree structure --- .../com/intellij/util/xml/tree/DomElementNode.java | 31 +++++++++++ .../util/xml/tree/DomElementsGroupNode.java | 60 ++++++++++++++++++++ .../util/xml/tree/DomModelTreeStructure.java | 20 +++++++ .../intellij/util/xml/tree/DomModelTreeView.java | 64 ++++++++++++++++++++++ .../intellij/util/xml/tree/GenericValueNode.java | 43 +++++++++++++++ 5 files changed, 218 insertions(+) create mode 100644 source/com/intellij/util/xml/tree/DomElementNode.java create mode 100644 source/com/intellij/util/xml/tree/DomElementsGroupNode.java create mode 100644 source/com/intellij/util/xml/tree/DomModelTreeStructure.java create mode 100644 source/com/intellij/util/xml/tree/DomModelTreeView.java create mode 100644 source/com/intellij/util/xml/tree/GenericValueNode.java diff --git a/source/com/intellij/util/xml/tree/DomElementNode.java b/source/com/intellij/util/xml/tree/DomElementNode.java new file mode 100644 index 0000000000..b04f80f8d2 --- /dev/null +++ b/source/com/intellij/util/xml/tree/DomElementNode.java @@ -0,0 +1,31 @@ +package com.intellij.util.xml.tree; + +import jetbrains.fabrique.ui.treeStructure.SimpleNode; +import com.intellij.util.xml.DomElement; + +import javax.swing.*; + +abstract public class DomElementNode extends SimpleNode { + protected DomElementNode() { + super(); + } + + public DomElementNode(final SimpleNode parent) { + super(parent); + } + + protected SimpleDomElementNode getDomElementNode(final DomElement domElement, final String tagName, final SimpleNode parentNode) { + return new SimpleDomElementNode(domElement, tagName, parentNode); + }; + + abstract public String getNodeName(); + + public Icon getNodeIcon() { + return null; + }; + + protected String getPropertyName(String tagName) { + //todo use name policy + return tagName.replaceAll("-", " "); + } +} diff --git a/source/com/intellij/util/xml/tree/DomElementsGroupNode.java b/source/com/intellij/util/xml/tree/DomElementsGroupNode.java new file mode 100644 index 0000000000..1a584adb90 --- /dev/null +++ b/source/com/intellij/util/xml/tree/DomElementsGroupNode.java @@ -0,0 +1,60 @@ +package com.intellij.util.xml.tree; + +import jetbrains.fabrique.ui.treeStructure.SimpleNode; +import com.intellij.openapi.util.text.StringUtil; +import com.intellij.openapi.diagnostic.Logger; +import com.intellij.ui.SimpleTextAttributes; +import com.intellij.util.xml.DomElement; + +import java.lang.reflect.Method; +import java.util.List; +import java.util.ArrayList; +import java.awt.*; + +public class DomElementsGroupNode extends DomElementNode { + Logger LOG = Logger.getInstance(DomModelTreeStructure.class.getName()); + + private DomElement myModelElement; + private Method myMethod; + private String myChildrenTagName; + + public DomElementsGroupNode(final DomElement modelElement, Method method) { + myModelElement = modelElement; + myMethod = method; + + myChildrenTagName = modelElement.getMethodsInfo().getTagName(method); + } + + public SimpleNode[] getChildren() { + List children = new ArrayList(); + try { + final List objects = (List)myMethod.invoke(myModelElement, new Object[0]); + for (DomElement domElement : objects) { + children.add(getDomElementNode(domElement, myChildrenTagName, this)); + } + } + catch (Exception e) { + LOG.error(e); + } + return children.toArray(new SimpleNode[children.size()]); + } + + public Object[] getEqualityObjects() { + return new Object[0]; + } + + protected boolean doUpdate() { + setUniformIcon(getNodeIcon()); + + clearColoredText(); + addColoredFragment(getNodeName(), new SimpleTextAttributes(Font.BOLD, Color.black)); + final int childrenCount = getChildren().length; + addColoredFragment(" (" + childrenCount + ')', new SimpleTextAttributes(Font.ITALIC, Color.gray)); + + return super.doUpdate(); + } + + public String getNodeName() { + return getPropertyName(StringUtil.pluralize(myChildrenTagName)); + } +} diff --git a/source/com/intellij/util/xml/tree/DomModelTreeStructure.java b/source/com/intellij/util/xml/tree/DomModelTreeStructure.java new file mode 100644 index 0000000000..99f3ea436b --- /dev/null +++ b/source/com/intellij/util/xml/tree/DomModelTreeStructure.java @@ -0,0 +1,20 @@ +package com.intellij.util.xml.tree; + +import com.intellij.util.xml.DomElement; +import jetbrains.fabrique.ui.treeStructure.SimpleTreeStructure; + +public class DomModelTreeStructure extends SimpleTreeStructure { + private SimpleDomElementNode myRoot; + + public DomModelTreeStructure(DomElement rootElement) { + myRoot = createRoot(rootElement); + } + + protected SimpleDomElementNode createRoot(DomElement rootElement) { + return new SimpleDomElementNode(rootElement); + } + + public SimpleDomElementNode getRootElement() { + return myRoot; + } +} \ No newline at end of file diff --git a/source/com/intellij/util/xml/tree/DomModelTreeView.java b/source/com/intellij/util/xml/tree/DomModelTreeView.java new file mode 100644 index 0000000000..7464f75de9 --- /dev/null +++ b/source/com/intellij/util/xml/tree/DomModelTreeView.java @@ -0,0 +1,64 @@ +package com.intellij.util.xml.tree; + +import com.intellij.openapi.project.Project; +import com.intellij.ui.components.panels.Wrapper; +import com.intellij.util.ui.tree.TreeUtil; +import com.intellij.util.xml.DomManager; +import com.intellij.util.xml.DomEventListener; +import com.intellij.util.xml.DomElement; +import com.intellij.util.xml.events.DomEvent; +import jetbrains.fabrique.ui.treeStructure.SimpleTree; +import jetbrains.fabrique.ui.treeStructure.SimpleTreeBuilder; +import jetbrains.fabrique.ui.treeStructure.WeightBasedComparator; +import jetbrains.fabrique.ui.treeStructure.SimpleTreeStructure; + +import javax.swing.*; +import javax.swing.tree.DefaultMutableTreeNode; +import javax.swing.tree.DefaultTreeModel; + +public class DomModelTreeView extends Wrapper { + private SimpleTree myTree; + + private DomElement myRootDomElement; + private SimpleTreeBuilder myBuilder; + + public DomModelTreeView(Project project, DomElement rootElement) { + myRootDomElement = rootElement; + + myTree = new SimpleTree(new DefaultTreeModel(new DefaultMutableTreeNode())); + myTree.setRootVisible(false); + myTree.setShowsRootHandles(true); + + ToolTipManager.sharedInstance().registerComponent(myTree); + TreeUtil.installActions(myTree); + + final SimpleTreeStructure treeStructure = myRootDomElement != null ? getTreeStructure(myRootDomElement) : new SimpleTreeStructure() { + public Object getRootElement() { + return null; + } + }; + + myBuilder = new SimpleTreeBuilder(myTree, (DefaultTreeModel) myTree.getModel(), treeStructure, WeightBasedComparator.INSTANCE); + myBuilder.initRoot(); + + add(myTree); + + DomManager.getDomManager(project).addDomEventListener(new DomEventListener() { + public void eventOccured(DomEvent event) { + super.eventOccured(event); + + myBuilder.updateFromRoot(); + } + }); + } + + protected SimpleTreeStructure getTreeStructure(final DomElement rootDomElement) { + return new DomModelTreeStructure(rootDomElement); + } + + public SimpleTreeBuilder getBuilder() { + return myBuilder; + } + +} + diff --git a/source/com/intellij/util/xml/tree/GenericValueNode.java b/source/com/intellij/util/xml/tree/GenericValueNode.java new file mode 100644 index 0000000000..da29ab3bd1 --- /dev/null +++ b/source/com/intellij/util/xml/tree/GenericValueNode.java @@ -0,0 +1,43 @@ +package com.intellij.util.xml.tree; + +import jetbrains.fabrique.ui.treeStructure.SimpleNode; +import com.intellij.util.xml.GenericValue; +import com.intellij.ui.SimpleTextAttributes; + +public class GenericValueNode extends DomElementNode { + protected GenericValue myModelElement; + protected String myTagName; + + public GenericValueNode(final GenericValue modelElement, final String tagName, SimpleNode parent) { + super(parent); + + myModelElement = modelElement; + myTagName = tagName == null ? "unknown" : tagName; + } + + public String getNodeName() { + return getPropertyName(myTagName); + } + + protected boolean doUpdate() { + setUniformIcon(getNodeIcon()); + clearColoredText(); + if (myModelElement.getStringValue() != null) { + addColoredFragment(getNodeName(), SimpleTextAttributes.REGULAR_ATTRIBUTES); + addColoredFragment("=", SimpleTextAttributes.REGULAR_ATTRIBUTES); + addColoredFragment("\"" + myModelElement.getStringValue() + "\"", SimpleTextAttributes.EXCLUDED_ATTRIBUTES); + } else { + addColoredFragment(getNodeName(), SimpleTextAttributes.GRAYED_ATTRIBUTES); + } + + return super.doUpdate(); + } + + public SimpleNode[] getChildren() { + return new SimpleNode[0]; + } + + public Object[] getEqualityObjects() { + return new Object[0]; + } +} -- 2.11.4.GIT