SimpleTree.java Sample

This topic inludes the source code for the SimpleTree.java Sample.

Sample Location

This sample is located in the following directory in your WebLogic Workshop installation:

BEA_HOME/weblogic81/samples/workshop/ExtensionDevKit/IdeDevKit/DragDropSimple/src/ideExtensions/dragDropSimple/

Sample Source Code


001 package ideExtensions.dragDropSimple;
002 
003 import javax.swing.JTree;
004 import javax.swing.tree.DefaultMutableTreeNode;
005 import javax.swing.tree.TreeSelectionModel;
006 
007 /**
008  * A Java Swing tree that displays example data for illustrating
009  * drag and drop in the WebLogic Workshop IDE. This class builds
010  * the tree using instances of DefaultMutableTreeNode, starting
011  * with an instance as the tree's root and adding children along the
012  * way. One useful aspect of this class is that it supports specifying
013  * instances of a "user object" that can live in each node to hold the
014  * node's data.
015  */
016 public class SimpleTree extends JTree
017 {
018     // Create the tree's root node.
019 
020     static DefaultMutableTreeNode top =
021             new DefaultMutableTreeNode("Item List");
022     /**
023      * Constructs an instance of the tree by starting with the root
024      * node and adding child nodes.
025      */
026     public SimpleTree()
027     {
028         super(top);
029         createNodes(top);
030 
031         // Only one node may be selected at a time.
032         this.getSelectionModel().setSelectionMode
033                 (TreeSelectionModel.SINGLE_TREE_SELECTION);
034     }
035 
036     /**
037      * A "user object" class to contain the data for each node in the tree.
038      * A new instance of this class is constructed for each data-carrying
039      * node in the tree.
040      */
041     public class ItemInfo
042     {
043         public String m_itemName;
044         public String m_itemDescription;
045 
046         // Constructs the object with simple data.
047         public ItemInfo(String item, String description)
048         {
049             m_itemName = item;
050             m_itemDescription = description;
051         }
052 
053         /**
054          * Called to retrieve the data that should be displayed as label
055          * for the tree node that this user object instance belongs to.
056          *
057          @return The label string.
058          */
059         public String toString()
060         {
061             return m_itemName;
062         }
063 
064         /**
065          * Gets a string containing the node's label and its description.
066          * This method is called to retrieve data for copying to a drop
067          * target.
068          *
069          @return The data for the node to which this user object
070          * belongs.
071          */
072         public String getItemData()
073         {
074             return "Item name: " + m_itemName + "\n" +
075                     "Item description: " + m_itemDescription;
076         }
077     }
078 
079     /**
080      * Builds the portion of the tree beneath the root by adding
081      * DefaultMutableTreeNode instances, each with separate
082      * instances of the ItemInfo user object. Two subcategories are
083      * also added. The nonsensical data represents item names and their
084      * descriptions.
085      *
086      @param top The tree's top node.
087      */
088     private void createNodes(DefaultMutableTreeNode top)
089     {
090         top.setUserObject(new ItemInfo
091                 ("All Items""A list of everything."));
092 
093         DefaultMutableTreeNode category = null;
094         DefaultMutableTreeNode item = null;
095 
096         category = new DefaultMutableTreeNode(new ItemInfo
097                 ("Category 1",
098                         "The first set of items."));
099         top.add(category);
100 
101         item = new DefaultMutableTreeNode(new ItemInfo
102                 ("flangie",
103                         "Fully-functional complement to our zinc-full protaic."));
104         category.add(item);
105 
106         item = new DefaultMutableTreeNode(new ItemInfo
107                 ("callioscim",
108                         "A must for any truly robust melianic hisc, whether " +
109                             "in or out of frigid environs."));
110         category.add(item);
111 
112         category = new DefaultMutableTreeNode(new ItemInfo
113                 ("Category 2",
114                         "The second set of items."));
115         top.add(category);
116 
117         item = new DefaultMutableTreeNode(new ItemInfo
118                 ("protaic",
119                         "Affix this to a flangie to see a manifold increase " +
120                             "in performant particulates."));
121         category.add(item);
122 
123         item = new DefaultMutableTreeNode(new ItemInfo
124                 ("melianic hisc",
125                         "Fully supports trending the callioscim to maximum " +
126                             "post-pattern joining."));
127         category.add(item);
128     }
129 }