home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Programming Languages Suite
/
ProgLangD.iso
/
VCAFE.3.0A
/
Sample.bin
/
SampleTreeFrame.java
< prev
next >
Wrap
Text File
|
1998-09-28
|
11KB
|
310 lines
/*
* This is a GUI version of the SampleTree sample from Sun.
* It demonstrates:
* - dynamicaly loading children refer to DynamicTreeNode.
* - adding/removing/inserting/reloading tree nodes.
* - Using a non-default JTreeModel, stringTreeModel.
* - AJM
*/
import com.sun.java.swing.*;
import com.sun.java.swing.tree.*;
import java.awt.*;
import com.symantec.itools.swing.models.StringTreeModel;
public class SampleTreeFrame extends com.sun.java.swing.JFrame
{
ButtonGroup group = new ButtonGroup();
public SampleTreeFrame()
{
// This code is automatically generated by Visual Cafe when you add
// components to the visual environment. It instantiates and initializes
// the components. To modify the code, only use code syntax that matches
// what Visual Cafe can generate, or Visual Cafe may be unable to back
// parse your Java file into its visual environment.
//{{INIT_CONTROLS
setJMenuBar(JMenuBar1);
setTitle("Sample Tree");
getContentPane().setLayout(new BorderLayout(0,0));
getContentPane().setFont(new Font("Dialog", Font.PLAIN, 0));
setSize(420,329);
setVisible(false);
{
String[] tempString = new String[6];
tempString[0] = "one";
tempString[1] = " two";
tempString[2] = " three";
tempString[3] = "four";
tempString[4] = " five";
tempString[5] = " six";
stringTreeModel1.setItems(tempString);
}
//$$ stringTreeModel1.move(72,348);
JTree1.setModel(stringTreeModel1);
getContentPane().add("Center", JTree1);
JTree1.setBounds(0,0,420,329);
//$$ JMenuBar1.move(24,348);
fileMenu.setText("File");
fileMenu.setActionCommand("File");
fileMenu.setMnemonic((int)'F');
JMenuBar1.add(fileMenu);
exitMenu.setText("Exit");
exitMenu.setActionCommand("Exit");
exitMenu.setMnemonic((int)'E');
fileMenu.add(exitMenu);
treeMenu.setText("Tree");
treeMenu.setActionCommand("Tree");
treeMenu.setMnemonic((int)'T');
JMenuBar1.add(treeMenu);
treeOptionsMenu.setText("Tree Options");
treeOptionsMenu.setActionCommand("Tree Options");
treeOptionsMenu.setMnemonic((int)'O');
treeMenu.add(treeOptionsMenu);
showHandles.setText("Show Handles");
showHandles.setActionCommand("Show Handles");
showHandles.setMnemonic((int)'H');
treeOptionsMenu.add(showHandles);
showRoot.setText("Show Root");
showRoot.setActionCommand("Show Root");
showRoot.setSelected(true);
showRoot.setMnemonic((int)'R');
treeOptionsMenu.add(showRoot);
editableNodes.setText("Editable");
editableNodes.setActionCommand("editable");
editableNodes.setMnemonic((int)'E');
treeOptionsMenu.add(editableNodes);
treeSelectionMenu.setText("Tree Selection Style");
treeSelectionMenu.setActionCommand("jmenu");
treeSelectionMenu.setMnemonic((int)'S');
treeMenu.add(treeSelectionMenu);
singleNode.setText("Single Node");
singleNode.setActionCommand("jradioButtonMenuItem");
singleNode.setSelected(true);
singleNode.setMnemonic((int)'S');
treeSelectionMenu.add(singleNode);
contiguousNodes.setText("Contiguous Nodes");
contiguousNodes.setActionCommand("jradioButtonMenuItem");
contiguousNodes.setMnemonic((int)'C');
treeSelectionMenu.add(contiguousNodes);
discontiguousNodes.setText("Discontiguous Nodes");
discontiguousNodes.setActionCommand("jradioButtonMenuItem");
discontiguousNodes.setMnemonic((int)'D');
treeSelectionMenu.add(discontiguousNodes);
addNode.setText("Add Node");
addNode.setActionCommand("Add Node");
addNode.setMnemonic((int)'A');
treeMenu.add(addNode);
insertNode.setText("Insert Node");
insertNode.setActionCommand("Insert Node");
insertNode.setMnemonic((int)'I');
treeMenu.add(insertNode);
reloadNode.setText("Reload Node");
reloadNode.setActionCommand("Reload Tree");
reloadNode.setMnemonic((int)'L');
treeMenu.add(reloadNode);
removeNode.setText("Remove Node");
removeNode.setActionCommand("Remove Node");
removeNode.setMnemonic((int)'M');
treeMenu.add(removeNode);
//}}
group.add(singleNode);
group.add(contiguousNodes);
group.add(discontiguousNodes);
JTree1.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
//{{INIT_MENUS
//}}
//{{REGISTER_LISTENERS
SymWindow aSymWindow = new SymWindow();
this.addWindowListener(aSymWindow);
SymAction lSymAction = new SymAction();
exitMenu.addActionListener(lSymAction);
SymItem lSymItem = new SymItem();
showHandles.addItemListener(lSymItem);
showRoot.addItemListener(lSymItem);
editableNodes.addItemListener(lSymItem);
singleNode.addItemListener(lSymItem);
contiguousNodes.addItemListener(lSymItem);
discontiguousNodes.addItemListener(lSymItem);
addNode.addActionListener(lSymAction);
insertNode.addActionListener(lSymAction);
reloadNode.addActionListener(lSymAction);
removeNode.addActionListener(lSymAction);
//}}
}
public SampleTreeFrame(String sTitle)
{
this();
setTitle(sTitle);
}
static public void main(String args[])
{
SampleTreeFrame jf = new SampleTreeFrame();
jf.setLocation(50, 50 );
jf.setVisible(true);
}
//{{DECLARE_CONTROLS
com.symantec.itools.swing.models.StringTreeModel stringTreeModel1 = new com.symantec.itools.swing.models.StringTreeModel();
com.sun.java.swing.JTree JTree1 = new com.sun.java.swing.JTree();
com.sun.java.swing.JMenuBar JMenuBar1 = new com.sun.java.swing.JMenuBar();
com.sun.java.swing.JMenu fileMenu = new com.sun.java.swing.JMenu();
com.sun.java.swing.JMenuItem exitMenu = new com.sun.java.swing.JMenuItem();
com.sun.java.swing.JMenu treeMenu = new com.sun.java.swing.JMenu();
com.sun.java.swing.JMenu treeOptionsMenu = new com.sun.java.swing.JMenu();
com.sun.java.swing.JCheckBoxMenuItem showHandles = new com.sun.java.swing.JCheckBoxMenuItem();
com.sun.java.swing.JCheckBoxMenuItem showRoot = new com.sun.java.swing.JCheckBoxMenuItem();
com.sun.java.swing.JCheckBoxMenuItem editableNodes = new com.sun.java.swing.JCheckBoxMenuItem();
com.sun.java.swing.JMenu treeSelectionMenu = new com.sun.java.swing.JMenu();
com.sun.java.swing.JRadioButtonMenuItem singleNode = new com.sun.java.swing.JRadioButtonMenuItem();
com.sun.java.swing.JRadioButtonMenuItem contiguousNodes = new com.sun.java.swing.JRadioButtonMenuItem();
com.sun.java.swing.JRadioButtonMenuItem discontiguousNodes = new com.sun.java.swing.JRadioButtonMenuItem();
com.sun.java.swing.JMenuItem addNode = new com.sun.java.swing.JMenuItem();
com.sun.java.swing.JMenuItem insertNode = new com.sun.java.swing.JMenuItem();
com.sun.java.swing.JMenuItem reloadNode = new com.sun.java.swing.JMenuItem();
com.sun.java.swing.JMenuItem removeNode = new com.sun.java.swing.JMenuItem();
//}}
//{{DECLARE_MENUS
//}}
class SymWindow extends java.awt.event.WindowAdapter
{
public void windowClosing(java.awt.event.WindowEvent event)
{
Object object = event.getSource();
if (object == SampleTreeFrame.this)
SampleTreeFrame_windowClosing(event);
}
}
void SampleTreeFrame_windowClosing(java.awt.event.WindowEvent event)
{
System.exit(0);
}
class SymAction implements java.awt.event.ActionListener
{
public void actionPerformed(java.awt.event.ActionEvent event)
{
Object object = event.getSource();
if (object == exitMenu)
exitMenu_actionPerformed(event);
else if (object == addNode)
addNode_actionPerformed(event);
else if (object == insertNode)
insertNode_actionPerformed(event);
else if (object == reloadNode)
reloadNode_actionPerformed(event);
else if (object == removeNode)
removeNode_actionPerformed(event);
}
}
void exitMenu_actionPerformed(java.awt.event.ActionEvent event)
{
System.exit(0);
}
class SymItem implements java.awt.event.ItemListener
{
public void itemStateChanged(java.awt.event.ItemEvent event)
{
Object object = event.getSource();
if (object == showHandles)
showHandles_itemStateChanged(event);
else if (object == showRoot)
showRoot_itemStateChanged(event);
else if (object == editableNodes)
editableNodes_itemStateChanged(event);
else if (object == singleNode)
singleNode_itemStateChanged(event);
else if (object == contiguousNodes)
contiguousNodes_itemStateChanged(event);
else if (object == discontiguousNodes)
discontiguousNodes_itemStateChanged(event);
}
}
void showHandles_itemStateChanged(java.awt.event.ItemEvent event)
{
JTree1.setShowsRootHandles(showHandles.isSelected());
}
void showRoot_itemStateChanged(java.awt.event.ItemEvent event)
{
JTree1.setRootVisible(showRoot.isSelected());
}
void editableNodes_itemStateChanged(java.awt.event.ItemEvent event)
{
JTree1.setEditable(editableNodes.isSelected());
}
void singleNode_itemStateChanged(java.awt.event.ItemEvent event)
{
if(singleNode.isSelected())
{
JTree1.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
}
}
void contiguousNodes_itemStateChanged(java.awt.event.ItemEvent event)
{
if(contiguousNodes.isSelected())
{
JTree1.getSelectionModel().setSelectionMode(TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
}
}
void discontiguousNodes_itemStateChanged(java.awt.event.ItemEvent event)
{
if(discontiguousNodes.isSelected())
{
JTree1.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
}
}
int addCount=0;
void addNode_actionPerformed(java.awt.event.ActionEvent event)
{
MutableTreeNode selNode=(MutableTreeNode)(JTree1.getSelectionPath().getLastPathComponent());
MutableTreeNode parent=(selNode!=null) ? (MutableTreeNode)(selNode.getParent()) : (MutableTreeNode)(stringTreeModel1.getRoot());
int index = (selNode!=null) ? parent.getIndex(selNode) + 1 : parent.getChildCount();
stringTreeModel1.insertNodeInto(new DefaultMutableTreeNode("Added node #"+ ++addCount), parent, index );
}
int insertCount=0;
void insertNode_actionPerformed(java.awt.event.ActionEvent event)
{
MutableTreeNode selNode=(MutableTreeNode)(JTree1.getSelectionPath().getLastPathComponent());
MutableTreeNode parent=(selNode!=null) ? (MutableTreeNode)(selNode.getParent()) : (MutableTreeNode)(stringTreeModel1.getRoot());
int index = (selNode!=null) ? parent.getIndex(selNode) : parent.getChildCount();
stringTreeModel1.insertNodeInto(new DefaultMutableTreeNode("Inseted node #"+ ++insertCount), parent, index );
}
void reloadNode_actionPerformed(java.awt.event.ActionEvent event)
{
MutableTreeNode selNode=(MutableTreeNode)(JTree1.getSelectionPath().getLastPathComponent());
if(selNode!=null)
{
stringTreeModel1.reload(selNode);
}
}
void removeNode_actionPerformed(java.awt.event.ActionEvent event)
{
MutableTreeNode selNode=(MutableTreeNode)(JTree1.getSelectionPath().getLastPathComponent());
if ((selNode !=null) && (selNode!=stringTreeModel1.getRoot()))
{
stringTreeModel1.removeNodeFromParent(selNode);
}
}
}