KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > designer > view > DesignerPanel


1 package org.oddjob.designer.view;
2
3 import java.awt.BorderLayout JavaDoc;
4 import java.awt.Dimension JavaDoc;
5 import java.awt.event.MouseAdapter JavaDoc;
6 import java.awt.event.MouseEvent JavaDoc;
7
8 import javax.swing.JPanel JavaDoc;
9 import javax.swing.JScrollPane JavaDoc;
10 import javax.swing.JSplitPane JavaDoc;
11 import javax.swing.JTree JavaDoc;
12 import javax.swing.event.TreeModelEvent JavaDoc;
13 import javax.swing.event.TreeModelListener JavaDoc;
14 import javax.swing.event.TreeSelectionEvent JavaDoc;
15 import javax.swing.event.TreeSelectionListener JavaDoc;
16 import javax.swing.tree.TreePath JavaDoc;
17 import javax.swing.tree.TreeSelectionModel JavaDoc;
18
19 import org.oddjob.designer.Looks;
20 import org.oddjob.designer.actions.DCTransferHandler;
21 import org.oddjob.designer.model.DesignTreeNode;
22 import org.oddjob.designer.model.DesignerModel;
23
24 /**
25  * The tree view.
26  *
27  * @author Rob Gordon
28  */

29
30 public class DesignerPanel extends JPanel JavaDoc {
31
32
33     private final JTree JavaDoc tree;
34
35     private final JScrollPane JavaDoc treeScroll;
36
37     private final DesignerMenuBar menuBar;
38
39     /**
40      * Constructor.
41      *
42      * @param start The starting point for the tree.
43      */

44     
45     public DesignerPanel(final DesignerModel model, DesignerMenuBar menuBar) {
46         // set up the menu
47
this.menuBar = menuBar;
48         
49         // create tree.
50
tree = new JTree JavaDoc(model.getTreeModel());
51         tree.setShowsRootHandles(true);
52         tree.setExpandsSelectedPaths(true);
53         tree.getModel().addTreeModelListener(new TreeModelListener JavaDoc() {
54             public void treeNodesChanged(TreeModelEvent JavaDoc e) {
55             }
56             public void treeNodesInserted(TreeModelEvent JavaDoc e) {
57                 TreePath JavaDoc p = e.getTreePath().pathByAddingChild(e.getChildren()[0]);
58                 tree.setSelectionPath(p);
59             }
60             public void treeNodesRemoved(TreeModelEvent JavaDoc e) {
61                 tree.setSelectionPath(null);
62             }
63             public void treeStructureChanged(TreeModelEvent JavaDoc e) {
64             }
65         });
66         
67         // drag and drop
68
tree.setDragEnabled(true);
69         DCTransferHandler transferHandler = new DCTransferHandler(model);
70         tree.setTransferHandler(transferHandler);
71         
72         tree.getSelectionModel().setSelectionMode
73             (TreeSelectionModel.SINGLE_TREE_SELECTION);
74
75         tree.addTreeSelectionListener(new TreeSelectionListener JavaDoc() {
76             public void valueChanged(TreeSelectionEvent JavaDoc event) {
77                 // change selection in the model
78
JTree JavaDoc tree = (JTree JavaDoc)event.getSource();
79                 DesignTreeNode node = (DesignTreeNode)tree.getLastSelectedPathComponent();
80                 model.setCurrentSelection(node);
81             }
82         });
83
84         tree.addMouseListener(new PopupListener());
85
86         setLayout(new BorderLayout JavaDoc());
87         treeScroll = new JScrollPane JavaDoc();
88         treeScroll.setPreferredSize(new Dimension JavaDoc(
89                 Looks.DESIGNER_TREE_WIDTH, Looks.DESIGNER_HEIGHT));
90         
91         treeScroll.setViewportView(tree);
92
93         // create detail pane
94
DesignerDetail dd = new DesignerDetail();
95         dd.setPreferredSize(new Dimension JavaDoc(
96                 Looks.DETAIL_FORM_WIDTH, Looks.DESIGNER_HEIGHT));
97         model.addObserver(dd);
98         
99         JSplitPane JavaDoc split = new JSplitPane JavaDoc(JSplitPane.HORIZONTAL_SPLIT,
100             treeScroll, dd);
101
102         add(split);
103         
104     }
105         
106     class PopupListener extends MouseAdapter JavaDoc {
107         public void mousePressed(MouseEvent JavaDoc e) {
108             maybeShowPopup(e);
109         }
110         public void mouseClicked(MouseEvent JavaDoc e) {
111             maybeShowPopup(e);
112         }
113         public void mouseReleased(MouseEvent JavaDoc e) {
114             maybeShowPopup(e);
115         }
116         
117         private void maybeShowPopup(MouseEvent JavaDoc e) {
118             if (!e.isPopupTrigger()) {
119                 return;
120             }
121             TreePath JavaDoc path = tree.getPathForLocation(e.getX(), e.getY());
122             tree.setSelectionPath(path);
123             if (menuBar.getPopupMenu() != null) {
124                 menuBar.getPopupMenu().show(e.getComponent(), e.getX(), e.getY());
125             }
126         }
127     }
128
129 }
130
Popular Tags