# 6
plz find my code :::: It of nearly 400 lines
/*
* Created by JFormDesigner on Mon Feb 05 10:48:09 GMT+05:30 2007
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.tree.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.*;
import javax.swing.event.*;
/**
* @author Appu b
*/
public class MainFrame extends JFrame {
public MainFrame() {
initComponents();
}
class Closer1 extends WindowAdapter {
public void windowClosing (WindowEvent event) {
System.exit (0);
}
}
class Closer2 implements ActionListener {
public void actionPerformed (ActionEvent event) {
System.exit (0);
}
}
DefaultMutableTreeNode child1 =null;
private void sub_nodes(DefaultMutableTreeNode child1)
{
DefaultMutableTreeNode subchild1 = new DefaultMutableTreeNode("Queues");
DefaultMutableTreeNode subchild2 = new DefaultMutableTreeNode("Channels");
DefaultMutableTreeNode subchild3 = new DefaultMutableTreeNode("Client Connections");
DefaultMutableTreeNode subchild4 = new DefaultMutableTreeNode("Process Definitions");
DefaultMutableTreeNode subchild5 = new DefaultMutableTreeNode("Namelists");
child1.add(subchild1);
child1.add(subchild2);
child1.add(subchild3);
child1.add(subchild4);
child1.add(subchild5);
}
public void showContextMenuTree(MouseEvent me, JTree comp) {
Point pt = SwingUtilities.convertPoint(me.getComponent(),me.getPoint(),comp);
JPopupMenu popup = new JPopupMenu();
int x = pt.x;
int y = pt.y;
//System.out.println("Here 111");
if(x <=48 && y <=16) // This condition will popup the menu whenever we click on 'Root node'
{
ActionListener menuListener = new ActionListener() {
public void actionPerformed(ActionEvent event) {
if(event.getActionCommand().equals("Add Queue Manager"))
add_manager();
}
};
JMenuItem item;
popup.add(item = new JMenuItem("Add Queue Manager", KeyEvent.VK_A));
item.addActionListener(menuListener);
popup.show(comp, x,y);
}
}
private void tree_data_display()
{
root = new DefaultMutableTreeNode("Root");
try
{
BufferedReader in2 = new BufferedReader(new FileReader("DataFile.txt"));
StringTokenizer st = null;
String qmname = "",str = "";
while((str = in2.readLine()) != null)
{
st = new StringTokenizer(str,"~");
qmname = st.nextToken();
child1 = new DefaultMutableTreeNode(qmname);
sub_nodes(child1);
root.add(child1);
}
}catch(Exception ex){System.out.println(ex);}
treeModel = new DefaultTreeModel(root);
tree = new JTree(treeModel);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.addTreeSelectionListener(new TreeSelectionListener()
{
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)
tree.getLastSelectedPathComponent();
if (node == null) return;
Object nodeInfo = node.getUserObject();
if (node.isLeaf()) {
//System.out.print(node.toString());
if(node.toString().equals("Queues"))
{
scrollPane2.setViewportView(new TreeDemo());
panel2.add(scrollPane2, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0), 0, 0));
{
panel2.setLayout(new GridBagLayout());
((GridBagLayout)panel2.getLayout()).columnWidths = new int[] {517, 0};
((GridBagLayout)panel2.getLayout()).rowHeights = new int[] {493, 0};
((GridBagLayout)panel2.getLayout()).columnWeights = new double[] {0.0, 1.0E-4};
((GridBagLayout)panel2.getLayout()).rowWeights = new double[] {0.0, 1.0E-4};
}
splitPane1.setRightComponent(panel2);
}
}
else
{
}
}
});
tree.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
//if (e.isPopupTrigger())
if (e.getButton() == 3) // right click value is 3
{
if (SwingUtilities.isRightMouseButton(e)) {
TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
tree.setSelectionPath(selPath);
showContextMenuTree(e, tree);
}
}
if (e.isPopupTrigger())
add_manager();
}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
});
getContentPane().add(new JScrollPane(tree));
panel1.setLayout(new GridBagLayout());
((GridBagLayout)panel1.getLayout()).columnWidths = new int[] {151, 0};
((GridBagLayout)panel1.getLayout()).rowHeights = new int[] {496, 0};
((GridBagLayout)panel1.getLayout()).columnWeights = new double[] {0.0, 1.0E-4};
((GridBagLayout)panel1.getLayout()).rowWeights = new double[] {0.0, 1.0E-4};
scrollPane1.setViewportView(tree);
panel1.add(scrollPane1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0), 0, 0));
splitPane1.setLeftComponent(panel1);
}
void add_manager()
{
//createQM myDialog = new createQM(f, true);
tree_data_display();
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
// Generated using JFormDesigner Evaluation license - Appu b
f=new JFrame();
menuPanel = new JPanel();
splitPane1 = new JSplitPane();
panel1 = new JPanel();
scrollPane1 = new JScrollPane();
scrollPane2 = new JScrollPane();
//tree1 = new JTree();
panel2 = new JPanel();
//tree_data_display();
//======== this ========
contentPane = getContentPane();
contentPane.setLayout(new GridBagLayout());
((GridBagLayout)contentPane.getLayout()).columnWidths = new int[] {680, 0};
((GridBagLayout)contentPane.getLayout()).rowHeights = new int[] {0, 246, 0};
((GridBagLayout)contentPane.getLayout()).columnWeights = new double[] {0.0, 1.0E-4};
((GridBagLayout)contentPane.getLayout()).rowWeights = new double[] {0.0, 0.0, 1.0E-4};
//======== menuPanel ========
{
menuPanel.setLayout(new GridBagLayout());
//((GridBagLayout)menuPanel.getLayout()).columnWidths = new int[] {676, 0};
//((GridBagLayout)menuPanel.getLayout()).rowHeights = new int[] {0};
//((GridBagLayout)menuPanel.getLayout()).columnWeights = new double[] {0.0, 1.0E-4};
//((GridBagLayout)menuPanel.getLayout()).rowWeights = new double[] {1.0E-4};
Handler = new Closer1 ();
exitHandler = new Closer2();
menuBar = new JMenuBar();
//Build the first menu.
fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
menuBar.add(fileMenu);
categoriesMenu = new JMenu("Categories");
categoriesMenu.setMnemonic(KeyEvent.VK_C);
fileMenu.add(categoriesMenu);
//a group of JMenuItems
categoriesMenuItem1 = new JMenuItem("Add Queue Manager", KeyEvent.VK_A);
categoriesMenuItem1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
//categoriesMenuItem1.addActionListener(QMHandler);
categoriesMenuItem1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//f.enable(false);
//new createQM();
add_manager();
//System.out.println("hi ::: "+f);
}
});
categoriesMenu.add(categoriesMenuItem1);
categoriesMenu.addSeparator();
categoriesMenuItem2 = new JMenuItem("New Category", KeyEvent.VK_W);
categoriesMenuItem2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, ActionEvent.ALT_MASK));
categoriesMenu.add(categoriesMenuItem2);
categoriesMenuItem3 = new JMenuItem("Modify Category", KeyEvent.VK_M);
categoriesMenuItem3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_3, ActionEvent.ALT_MASK));
categoriesMenuItem3.setEnabled(false);
categoriesMenu.add(categoriesMenuItem3);
categoriesMenuItem4 = new JMenuItem("Delete Category", KeyEvent.VK_D);
categoriesMenuItem4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_4, ActionEvent.ALT_MASK));
categoriesMenuItem4.setEnabled(false);
categoriesMenu.add(categoriesMenuItem4);
queueManagerMenuItem = new JMenuItem("Queue Managers");
queueManagerMenuItem.setMnemonic(KeyEvent.VK_M);
queueManagerMenuItem.setEnabled(false);
fileMenu.add(queueManagerMenuItem);
queueMenuItem = new JMenuItem("Queues");
queueMenuItem.setMnemonic(KeyEvent.VK_Q);
queueMenuItem.setEnabled(false);
fileMenu.add(queueMenuItem);
channelMenuItem = new JMenuItem("Channels");
channelMenuItem.setMnemonic(KeyEvent.VK_H);
channelMenuItem.setEnabled(false);
fileMenu.add(channelMenuItem);
clientConnectionMenuItem = new JMenuItem("Client Connections");
clientConnectionMenuItem.setMnemonic(KeyEvent.VK_L);
clientConnectionMenuItem.setEnabled(false);
fileMenu.add(clientConnectionMenuItem);
processDefinitionMenuItem = new JMenuItem("Process Definitions");
processDefinitionMenuItem.setMnemonic(KeyEvent.VK_P);
processDefinitionMenuItem.setEnabled(false);
fileMenu.add(processDefinitionMenuItem);
nameListsMenuItem = new JMenuItem("Name lists");
nameListsMenuItem.setMnemonic(KeyEvent.VK_N);
nameListsMenuItem.setEnabled(false);
fileMenu.add(nameListsMenuItem);
fileMenu.addSeparator();
exitMenuItem = new JMenuItem("Exit");
exitMenuItem.setMnemonic(KeyEvent.VK_N);
exitMenuItem.addActionListener(exitHandler);
fileMenu.add(exitMenuItem);
//Build second menu in the menu bar.
editMenu = new JMenu("Edit");
editMenu.setMnemonic(KeyEvent.VK_E);
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuPanel.add(menuBar);
}
contentPane.add(menuPanel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.VERTICAL,
new Insets(0, 0, 0, 0), 0, 0));
//======== splitPane1 ========
{
tree_data_display();
}
//======== panel2 ========
{
panel2.setLayout(new GridBagLayout());
((GridBagLayout)panel2.getLayout()).columnWidths = new int[] {517, 0};
((GridBagLayout)panel2.getLayout()).rowHeights = new int[] {493, 0};
((GridBagLayout)panel2.getLayout()).columnWeights = new double[] {0.0, 1.0E-4};
((GridBagLayout)panel2.getLayout()).rowWeights = new double[] {0.0, 1.0E-4};
}
splitPane1.setRightComponent(panel2);
contentPane.add(splitPane1, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
new Insets(0, 0, 0, 0), 0, 0));
f.setTitle ("WMQ Sample");
f.getContentPane().setLayout(new FlowLayout());
f.setSize(700,550);
f.addWindowListener (Handler);
f.getContentPane().add(contentPane);
f.setVisible(true);
//f.show();
//pack();
//setLocationRelativeTo(getOwner());
}
public static void main (String args[]) throws Exception {
///String lnfName = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
//UIManager.setLookAndFeel(lnfName);
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
System.err.println("Couldn't use system look and feel.");
}
//SwingUtilities.updateComponentTreeUI();
new MainFrame ();
}
DefaultMutableTreeNode root;
DefaultTreeModel treeModel;
private JPanel menuPanel;
private JSplitPane splitPane1;
private JPanel panel1;
private JScrollPane scrollPane1;
private JScrollPane scrollPane2;
private JTree tree;
private JPanel panel2;
public Closer1 Handler;
public Closer2 exitHandler;
//public QM QMHandler;
public JFrame f;
public JMenuBar menuBar;
public JMenu menu, submenu;
public JMenu fileMenu;
public JMenu editMenu;
public JMenu viewMenu;
public JMenu toolMenu;
public JMenuItem menuItem;
public JMenu categoriesMenu;
public JMenuItem categoriesMenuItem1;
public JMenuItem categoriesMenuItem2;
public JMenuItem categoriesMenuItem3;
public JMenuItem categoriesMenuItem4;
public JMenuItem queueManagerMenuItem;
public JMenuItem queueMenuItem;
public JMenuItem channelMenuItem;
public JMenuItem clientConnectionMenuItem;
public JMenuItem processDefinitionMenuItem;
public JMenuItem nameListsMenuItem;
public JMenuItem exitMenuItem;
public JMenuItem quitMenuItem;
public Container contentPane;
}