home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / VCAFE.3.0A / Sample.bin / SampleTreeFrame.java < prev    next >
Text File  |  1998-09-28  |  11KB  |  310 lines

  1. /*
  2.   * This is a GUI version of the SampleTree sample from Sun.
  3.   * It demonstrates:
  4.   * - dynamicaly loading children refer to DynamicTreeNode.
  5.   * - adding/removing/inserting/reloading tree nodes. 
  6.   * - Using a non-default JTreeModel, stringTreeModel.
  7.   * - AJM
  8. */
  9.  
  10. import com.sun.java.swing.*;
  11. import com.sun.java.swing.tree.*;
  12. import java.awt.*;
  13. import com.symantec.itools.swing.models.StringTreeModel;
  14.  
  15. public class SampleTreeFrame extends com.sun.java.swing.JFrame
  16. {
  17.     ButtonGroup group = new ButtonGroup();
  18.  
  19.     public SampleTreeFrame()
  20.     {
  21.         // This code is automatically generated by Visual Cafe when you add
  22.         // components to the visual environment. It instantiates and initializes
  23.         // the components. To modify the code, only use code syntax that matches
  24.         // what Visual Cafe can generate, or Visual Cafe may be unable to back
  25.         // parse your Java file into its visual environment.
  26.         //{{INIT_CONTROLS
  27.         setJMenuBar(JMenuBar1);
  28.         setTitle("Sample Tree");
  29.         getContentPane().setLayout(new BorderLayout(0,0));
  30.         getContentPane().setFont(new Font("Dialog", Font.PLAIN, 0));
  31.         setSize(420,329);
  32.         setVisible(false);
  33.         {
  34.             String[] tempString = new String[6];
  35.             tempString[0] = "one";
  36.             tempString[1] = " two";
  37.             tempString[2] = " three";
  38.             tempString[3] = "four";
  39.             tempString[4] = " five";
  40.             tempString[5] = "  six";
  41.             stringTreeModel1.setItems(tempString);
  42.         }
  43.         //$$ stringTreeModel1.move(72,348);
  44.         JTree1.setModel(stringTreeModel1);
  45.         getContentPane().add("Center", JTree1);
  46.         JTree1.setBounds(0,0,420,329);
  47.         //$$ JMenuBar1.move(24,348);
  48.         fileMenu.setText("File");
  49.         fileMenu.setActionCommand("File");
  50.         fileMenu.setMnemonic((int)'F');
  51.         JMenuBar1.add(fileMenu);
  52.         exitMenu.setText("Exit");
  53.         exitMenu.setActionCommand("Exit");
  54.         exitMenu.setMnemonic((int)'E');
  55.         fileMenu.add(exitMenu);
  56.         treeMenu.setText("Tree");
  57.         treeMenu.setActionCommand("Tree");
  58.         treeMenu.setMnemonic((int)'T');
  59.         JMenuBar1.add(treeMenu);
  60.         treeOptionsMenu.setText("Tree Options");
  61.         treeOptionsMenu.setActionCommand("Tree Options");
  62.         treeOptionsMenu.setMnemonic((int)'O');
  63.         treeMenu.add(treeOptionsMenu);
  64.         showHandles.setText("Show Handles");
  65.         showHandles.setActionCommand("Show Handles");
  66.         showHandles.setMnemonic((int)'H');
  67.         treeOptionsMenu.add(showHandles);
  68.         showRoot.setText("Show Root");
  69.         showRoot.setActionCommand("Show Root");
  70.         showRoot.setSelected(true);
  71.         showRoot.setMnemonic((int)'R');
  72.         treeOptionsMenu.add(showRoot);
  73.         editableNodes.setText("Editable");
  74.         editableNodes.setActionCommand("editable");
  75.         editableNodes.setMnemonic((int)'E');
  76.         treeOptionsMenu.add(editableNodes);
  77.         treeSelectionMenu.setText("Tree Selection Style");
  78.         treeSelectionMenu.setActionCommand("jmenu");
  79.         treeSelectionMenu.setMnemonic((int)'S');
  80.         treeMenu.add(treeSelectionMenu);
  81.         singleNode.setText("Single Node");
  82.         singleNode.setActionCommand("jradioButtonMenuItem");
  83.         singleNode.setSelected(true);
  84.         singleNode.setMnemonic((int)'S');
  85.         treeSelectionMenu.add(singleNode);
  86.         contiguousNodes.setText("Contiguous Nodes");
  87.         contiguousNodes.setActionCommand("jradioButtonMenuItem");
  88.         contiguousNodes.setMnemonic((int)'C');
  89.         treeSelectionMenu.add(contiguousNodes);
  90.         discontiguousNodes.setText("Discontiguous Nodes");
  91.         discontiguousNodes.setActionCommand("jradioButtonMenuItem");
  92.         discontiguousNodes.setMnemonic((int)'D');
  93.         treeSelectionMenu.add(discontiguousNodes);
  94.         addNode.setText("Add Node");
  95.         addNode.setActionCommand("Add Node");
  96.         addNode.setMnemonic((int)'A');
  97.         treeMenu.add(addNode);
  98.         insertNode.setText("Insert Node");
  99.         insertNode.setActionCommand("Insert Node");
  100.         insertNode.setMnemonic((int)'I');
  101.         treeMenu.add(insertNode);
  102.         reloadNode.setText("Reload Node");
  103.         reloadNode.setActionCommand("Reload Tree");
  104.         reloadNode.setMnemonic((int)'L');
  105.         treeMenu.add(reloadNode);
  106.         removeNode.setText("Remove Node");
  107.         removeNode.setActionCommand("Remove Node");
  108.         removeNode.setMnemonic((int)'M');
  109.         treeMenu.add(removeNode);
  110.         //}}
  111.         
  112.         group.add(singleNode);
  113.         group.add(contiguousNodes);
  114.         group.add(discontiguousNodes);
  115.         
  116.         JTree1.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
  117.  
  118.         //{{INIT_MENUS
  119.         //}}
  120.     
  121.         //{{REGISTER_LISTENERS
  122.         SymWindow aSymWindow = new SymWindow();
  123.         this.addWindowListener(aSymWindow);
  124.         SymAction lSymAction = new SymAction();
  125.         exitMenu.addActionListener(lSymAction);
  126.         SymItem lSymItem = new SymItem();
  127.         showHandles.addItemListener(lSymItem);
  128.         showRoot.addItemListener(lSymItem);
  129.         editableNodes.addItemListener(lSymItem);
  130.         singleNode.addItemListener(lSymItem);
  131.         contiguousNodes.addItemListener(lSymItem);
  132.         discontiguousNodes.addItemListener(lSymItem);
  133.         addNode.addActionListener(lSymAction);
  134.         insertNode.addActionListener(lSymAction);
  135.         reloadNode.addActionListener(lSymAction);
  136.         removeNode.addActionListener(lSymAction);
  137.         //}}
  138.     }
  139.  
  140.     public SampleTreeFrame(String sTitle)
  141.     {
  142.         this();
  143.         setTitle(sTitle);
  144.     }
  145.  
  146.     static public void main(String args[])
  147.     {
  148.         SampleTreeFrame jf = new SampleTreeFrame();
  149.         jf.setLocation(50, 50 );
  150.         jf.setVisible(true);
  151.     }
  152.  
  153.     //{{DECLARE_CONTROLS
  154.     com.symantec.itools.swing.models.StringTreeModel stringTreeModel1 = new com.symantec.itools.swing.models.StringTreeModel();
  155.     com.sun.java.swing.JTree JTree1 = new com.sun.java.swing.JTree();
  156.     com.sun.java.swing.JMenuBar JMenuBar1 = new com.sun.java.swing.JMenuBar();
  157.     com.sun.java.swing.JMenu fileMenu = new com.sun.java.swing.JMenu();
  158.     com.sun.java.swing.JMenuItem exitMenu = new com.sun.java.swing.JMenuItem();
  159.     com.sun.java.swing.JMenu treeMenu = new com.sun.java.swing.JMenu();
  160.     com.sun.java.swing.JMenu treeOptionsMenu = new com.sun.java.swing.JMenu();
  161.     com.sun.java.swing.JCheckBoxMenuItem showHandles = new com.sun.java.swing.JCheckBoxMenuItem();
  162.     com.sun.java.swing.JCheckBoxMenuItem showRoot = new com.sun.java.swing.JCheckBoxMenuItem();
  163.     com.sun.java.swing.JCheckBoxMenuItem editableNodes = new com.sun.java.swing.JCheckBoxMenuItem();
  164.     com.sun.java.swing.JMenu treeSelectionMenu = new com.sun.java.swing.JMenu();
  165.     com.sun.java.swing.JRadioButtonMenuItem singleNode = new com.sun.java.swing.JRadioButtonMenuItem();
  166.     com.sun.java.swing.JRadioButtonMenuItem contiguousNodes = new com.sun.java.swing.JRadioButtonMenuItem();
  167.     com.sun.java.swing.JRadioButtonMenuItem discontiguousNodes = new com.sun.java.swing.JRadioButtonMenuItem();
  168.     com.sun.java.swing.JMenuItem addNode = new com.sun.java.swing.JMenuItem();
  169.     com.sun.java.swing.JMenuItem insertNode = new com.sun.java.swing.JMenuItem();
  170.     com.sun.java.swing.JMenuItem reloadNode = new com.sun.java.swing.JMenuItem();
  171.     com.sun.java.swing.JMenuItem removeNode = new com.sun.java.swing.JMenuItem();
  172.     //}}
  173.  
  174.     //{{DECLARE_MENUS
  175.     //}}
  176.  
  177.  
  178.     class SymWindow extends java.awt.event.WindowAdapter
  179.     {
  180.         public void windowClosing(java.awt.event.WindowEvent event)
  181.         {
  182.             Object object = event.getSource();
  183.             if (object == SampleTreeFrame.this)
  184.                 SampleTreeFrame_windowClosing(event);
  185.         }
  186.     }
  187.  
  188.     void SampleTreeFrame_windowClosing(java.awt.event.WindowEvent event)
  189.     {
  190.         System.exit(0);
  191.     }
  192.  
  193.     class SymAction implements java.awt.event.ActionListener
  194.     {
  195.         public void actionPerformed(java.awt.event.ActionEvent event)
  196.         {
  197.             Object object = event.getSource();
  198.             if (object == exitMenu)
  199.                 exitMenu_actionPerformed(event);
  200.             else if (object == addNode)
  201.                 addNode_actionPerformed(event);
  202.             else if (object == insertNode)
  203.                 insertNode_actionPerformed(event);
  204.             else if (object == reloadNode)
  205.                 reloadNode_actionPerformed(event);
  206.             else if (object == removeNode)
  207.                 removeNode_actionPerformed(event);
  208.         }
  209.     }
  210.  
  211.     void exitMenu_actionPerformed(java.awt.event.ActionEvent event)
  212.     {
  213.         System.exit(0);
  214.     }
  215.  
  216.     class SymItem implements java.awt.event.ItemListener
  217.     {
  218.         public void itemStateChanged(java.awt.event.ItemEvent event)
  219.         {
  220.             Object object = event.getSource();
  221.             if (object == showHandles)
  222.                 showHandles_itemStateChanged(event);
  223.             else if (object == showRoot)
  224.                 showRoot_itemStateChanged(event);
  225.             else if (object == editableNodes)
  226.                 editableNodes_itemStateChanged(event);
  227.             else if (object == singleNode)
  228.                 singleNode_itemStateChanged(event);
  229.             else if (object == contiguousNodes)
  230.                 contiguousNodes_itemStateChanged(event);
  231.             else if (object == discontiguousNodes)
  232.                 discontiguousNodes_itemStateChanged(event);
  233.         }
  234.     }
  235.  
  236.     void showHandles_itemStateChanged(java.awt.event.ItemEvent event)
  237.     {
  238.         JTree1.setShowsRootHandles(showHandles.isSelected());
  239.     }
  240.  
  241.     void showRoot_itemStateChanged(java.awt.event.ItemEvent event)
  242.     {
  243.         JTree1.setRootVisible(showRoot.isSelected());
  244.     }
  245.  
  246.     void editableNodes_itemStateChanged(java.awt.event.ItemEvent event)
  247.     {
  248.         JTree1.setEditable(editableNodes.isSelected());
  249.     }
  250.  
  251.     void singleNode_itemStateChanged(java.awt.event.ItemEvent event)
  252.     {
  253.         if(singleNode.isSelected())
  254.         {
  255.             JTree1.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
  256.         }
  257.     }
  258.  
  259.     void contiguousNodes_itemStateChanged(java.awt.event.ItemEvent event)
  260.     {
  261.         if(contiguousNodes.isSelected())
  262.         {
  263.             JTree1.getSelectionModel().setSelectionMode(TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
  264.         }
  265.     }
  266.  
  267.     void discontiguousNodes_itemStateChanged(java.awt.event.ItemEvent event)
  268.     {
  269.         if(discontiguousNodes.isSelected())
  270.         {
  271.             JTree1.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
  272.         }
  273.     }
  274.  
  275.     int addCount=0;
  276.     void addNode_actionPerformed(java.awt.event.ActionEvent event)
  277.     {
  278.         MutableTreeNode selNode=(MutableTreeNode)(JTree1.getSelectionPath().getLastPathComponent());
  279.         MutableTreeNode parent=(selNode!=null) ? (MutableTreeNode)(selNode.getParent()) : (MutableTreeNode)(stringTreeModel1.getRoot());
  280.         int index = (selNode!=null) ? parent.getIndex(selNode) + 1 : parent.getChildCount();
  281.         stringTreeModel1.insertNodeInto(new DefaultMutableTreeNode("Added node #"+ ++addCount), parent, index );
  282.     }
  283.  
  284.     int insertCount=0;
  285.     void insertNode_actionPerformed(java.awt.event.ActionEvent event)
  286.     {
  287.         MutableTreeNode selNode=(MutableTreeNode)(JTree1.getSelectionPath().getLastPathComponent());
  288.         MutableTreeNode parent=(selNode!=null) ? (MutableTreeNode)(selNode.getParent()) : (MutableTreeNode)(stringTreeModel1.getRoot());
  289.         int index = (selNode!=null) ? parent.getIndex(selNode) : parent.getChildCount();
  290.         stringTreeModel1.insertNodeInto(new DefaultMutableTreeNode("Inseted node #"+ ++insertCount), parent, index );
  291.     }
  292.  
  293.     void reloadNode_actionPerformed(java.awt.event.ActionEvent event)
  294.     {
  295.         MutableTreeNode selNode=(MutableTreeNode)(JTree1.getSelectionPath().getLastPathComponent());
  296.         if(selNode!=null)
  297.         {
  298.             stringTreeModel1.reload(selNode);
  299.         }
  300.     }
  301.  
  302.     void removeNode_actionPerformed(java.awt.event.ActionEvent event)
  303.     {
  304.         MutableTreeNode selNode=(MutableTreeNode)(JTree1.getSelectionPath().getLastPathComponent());
  305.         if ((selNode !=null) && (selNode!=stringTreeModel1.getRoot()))
  306.         {
  307.             stringTreeModel1.removeNodeFromParent(selNode);
  308.         }
  309.     }
  310. }