Commits

Rui Vieira committed 71d7fa1

Added JTree utility OTree subclass (and corresponding OTreeNode)

  • Participants
  • Parent commits 3c66976

Comments (0)

Files changed (3)

File src/main/java/org/ruivieira/orwell/components/tables/Column.java

+package org.ruivieira.orwell.components.tables;
+
+/**
+ * Author: rvieira
+ * Date: 14/11/11
+ * Time: 17:00
+ */
+public class Column {
+    private int index;
+    private String name;
+    private Class type;
+
+    public Column() {
+
+    }
+
+    public Column(int i, String name, Class type) {
+        this.index = i;
+        this.name = name;
+        this.type = type;
+
+    }
+
+    public int getIndex() {
+        return index;
+    }
+
+    public void setIndex(int index) {
+        this.index = index;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public Class getType() {
+        return type;
+    }
+
+    public void setType(Class type) {
+        this.type = type;
+    }
+
+}

File src/main/java/org/ruivieira/orwell/components/trees/OTree.java

+/*
+ * MyTree.java
+ *
+ */
+package org.ruivieira.orwell.components.trees;
+
+import javax.swing.*;
+import javax.swing.tree.DefaultMutableTreeNode;
+import javax.swing.tree.DefaultTreeCellRenderer;
+import javax.swing.tree.DefaultTreeModel;
+import javax.swing.tree.TreePath;
+
+/**
+ * @author Rui Vieira
+ */
+public class OTree extends JTree {
+
+    private DefaultTreeModel model;
+    private OTreeNode main_node;
+    private boolean keepExpandedAfterReload = true;
+    private DefaultTreeCellRenderer myCellRenderer = new DefaultTreeCellRenderer();
+
+    /**
+     * Creates a new instance of MyTree
+     */
+    public OTree() {
+        super();
+        clearNodes();
+        setCellRenderer(myCellRenderer);
+    }
+
+    public void clearNodes() {
+        setMainNode(new OTreeNode("root"));
+        setModel(new DefaultTreeModel(getMainNode().getNode()));
+        this.setModel(this.model);
+    }
+
+    public void renameRoot(String name) {
+        model = new DefaultTreeModel(getMainNode().renameNode(name));
+        this.setModel(this.model);
+    }
+
+    public void setMainNode(OTreeNode mainNode) {
+        this.main_node = mainNode;
+        this.model = new DefaultTreeModel(mainNode.getNode());
+        this.setModel(this.model);
+    }
+
+    public OTreeNode getMainNode() {
+        return main_node;
+    }
+
+    public void deleteNodePath(TreePath path) {
+        DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
+        node.removeFromParent();
+        model.reload();
+        if (keepExpandedAfterReload) {
+            expandPath(path.getParentPath());
+        }
+    }
+
+    public boolean isKeepExpandedAfterReload() {
+        return keepExpandedAfterReload;
+    }
+
+    public void setKeepExpandedAfterReload(boolean keepExpandedAfterReload) {
+        this.keepExpandedAfterReload = keepExpandedAfterReload;
+    }
+
+    public void setLeafIcon(ImageIcon icon) {
+        myCellRenderer.setLeafIcon(icon);
+        setCellRenderer(myCellRenderer);
+    }
+
+    public void setOpenIcon(ImageIcon icon) {
+        myCellRenderer.setOpenIcon(icon);
+        setCellRenderer(myCellRenderer);
+    }
+
+    public void setClosedIcon(ImageIcon icon) {
+        myCellRenderer.setClosedIcon(icon);
+        setCellRenderer(myCellRenderer);
+    }
+
+    public void reload() {
+        model.reload();
+    }
+}

File src/main/java/org/ruivieira/orwell/components/trees/OTreeNode.java

+/*
+ * MyTreeNode.java
+ *
+ */
+
+package org.ruivieira.orwell.components.trees;
+
+import javax.swing.tree.DefaultMutableTreeNode;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author Rui Vieira
+ */
+public class OTreeNode extends DefaultMutableTreeNode {
+
+    private DefaultMutableTreeNode node;
+    private String name;
+    private List<OTreeNode> childs = new ArrayList<OTreeNode>();
+
+    /**
+     * Creates a new instance of MyTreeNode
+     *
+     * @param name
+     */
+    public OTreeNode(String name) {
+        this.name = name;
+        this.node = new DefaultMutableTreeNode(name);
+
+    }
+
+    public DefaultMutableTreeNode getNode() {
+        return this.node;
+    }
+
+    public DefaultMutableTreeNode renameNode(String _name) {
+        name = _name;
+        node = new DefaultMutableTreeNode(_name);
+        for (int i = 0; i < childs.size(); i++) {
+            node.add(childs.get(i).getNode());
+        }
+        return node;
+    }
+
+    public void addChild(OTreeNode child) {
+        childs.add(child);
+        node.add(child.getNode());
+    }
+
+    public OTreeNode createChild(String name) {
+        OTreeNode newChild = new OTreeNode(name);
+        addChild(newChild);
+        return newChild;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public OTreeNode getChildNamed(String name) {
+        OTreeNode result = null;
+        for (int i = 0; i < childs.size(); i++) {
+            OTreeNode child = childs.get(i);
+            if (child.getName().equals(name)) {
+                result = child;
+            }
+
+        }
+        return result;
+    }
+
+
+}