KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * (c) Rob Gordon 2005.
3  */

4 package org.oddjob.designer.view;
5
6 import java.awt.BorderLayout JavaDoc;
7 import java.awt.Component JavaDoc;
8 import java.awt.Container JavaDoc;
9 import java.awt.Dimension JavaDoc;
10 import java.awt.GridBagConstraints JavaDoc;
11 import java.awt.GridBagLayout JavaDoc;
12 import java.awt.event.ActionEvent JavaDoc;
13
14 import javax.swing.AbstractAction JavaDoc;
15 import javax.swing.Action JavaDoc;
16 import javax.swing.JMenu JavaDoc;
17 import javax.swing.JMenuItem JavaDoc;
18 import javax.swing.JPanel JavaDoc;
19 import javax.swing.JPopupMenu JavaDoc;
20 import javax.swing.JScrollPane JavaDoc;
21 import javax.swing.JSplitPane JavaDoc;
22 import javax.swing.JTree JavaDoc;
23 import javax.swing.event.TreeModelEvent JavaDoc;
24 import javax.swing.event.TreeModelListener JavaDoc;
25 import javax.swing.event.TreeSelectionEvent JavaDoc;
26 import javax.swing.event.TreeSelectionListener JavaDoc;
27 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
28 import javax.swing.tree.DefaultTreeModel JavaDoc;
29 import javax.swing.tree.TreePath JavaDoc;
30 import javax.swing.tree.TreeSelectionModel JavaDoc;
31
32 import org.oddjob.designer.Looks;
33 import org.oddjob.designer.model.DesignDefinition;
34 import org.oddjob.designer.model.DesignElement;
35 import org.oddjob.designer.model.DesignElementType;
36 import org.oddjob.designer.model.StandardForm;
37 import org.oddjob.designer.model.MultiTypeTree;
38
39 /**
40  * Creates a Swing Tree view of a MultTypeTable model.
41  */

42 public class MultiTypeTreeView implements ViewProducer {
43
44     private final MultiTypeTree designDefinition;
45     
46     private final DefaultMutableTreeNode JavaDoc top = new DefaultMutableTreeNode JavaDoc();
47     private final DefaultTreeModel JavaDoc model = new DefaultTreeModel JavaDoc(top);
48
49     /**
50      * Constructor.
51      *
52      * @param designDefinition The MultiTypeTable model.
53      */

54     public MultiTypeTreeView(MultiTypeTree designDefinition) {
55         this.designDefinition = designDefinition;
56         
57         top.setUserObject(designDefinition.getDesignElement());
58         buildTree(top);
59     }
60
61     /**
62      * Recursive method to build the tree.
63      *
64      * @param tn The curent TreeNode.
65      */

66     void buildTree(DefaultMutableTreeNode JavaDoc tn) {
67         DesignElementType de = (DesignElementType) tn.getUserObject();
68         DesignElementType[] children = de.children();
69         for (int i = 0; i < children.length; ++i) {
70             DefaultMutableTreeNode JavaDoc ctn = new DefaultMutableTreeNode JavaDoc();
71             ctn.setUserObject(children[i]);
72             tn.add(ctn);
73             buildTree(ctn);
74         }
75     }
76
77     /*
78      * (non-Javadoc)
79      * @see org.oddjob.designer.view.ViewProducer#dialog()
80      */

81     public Component JavaDoc dialog() {
82         return group();
83     }
84     
85     /*
86      * (non-Javadoc)
87      * @see org.oddjob.designer.view.ViewProducer#group()
88      */

89     public Component JavaDoc group() {
90         ValuesPanel mainForm = new ValuesPanel();
91         return mainForm;
92     }
93
94     /* (non-Javadoc)
95      * @see org.oddjob.designer.view.ViewProducer#detailEdit(org.oddjob.designer.view.ActionWrapper)
96      */

97     public Component JavaDoc detailEdit() {
98         return ViewHelper.createDetailButton(designDefinition);
99     }
100     
101     /*
102      * (non-Javadoc)
103      * @see org.oddjob.designer.view.ViewProducer#cell()
104      */

105     public Component JavaDoc cell() {
106         return ViewHelper.createDetailButton(designDefinition);
107     }
108         
109     /* (non-Javadoc)
110      * @see org.oddjob.designer.view.ViewProducer#inline(java.awt.Container, int, int, boolean)
111      */

112     public int inline(Container JavaDoc container, int row, int column,
113             boolean selectionInGroup) {
114         throw new UnsupportedOperationException JavaDoc("Can't imagine a tree ever being used inline!");
115     }
116
117     /* (non-Javadoc)
118      * @see org.oddjob.designer.view.ViewProducer#setEnabled(boolean)
119      */

120     public void setEnabled(boolean enabled) {
121         throw new UnsupportedOperationException JavaDoc("Can't imagine a tree ever being used in a selection!");
122     }
123     
124     /**
125      * The TreePanel.
126      *
127      */

128     class ValuesPanel extends JPanel JavaDoc {
129
130         private JScrollPane JavaDoc detailScroll;
131         
132         ValuesPanel() {
133             final PopupListener popupListener = new PopupListener();
134             
135             detailScroll = new JScrollPane JavaDoc();
136             detailScroll.setPreferredSize(new Dimension JavaDoc(
137                     Looks.DETAIL_FORM_WIDTH, Looks.DESIGNER_HEIGHT));
138             
139             final JTree JavaDoc tree = new JTree JavaDoc(model);
140             tree.setShowsRootHandles(true);
141             tree.setExpandsSelectedPaths(true);
142             model.addTreeModelListener(new TreeModelListener JavaDoc() {
143                 public void treeNodesChanged(TreeModelEvent JavaDoc e) {
144                 }
145                 public void treeNodesInserted(TreeModelEvent JavaDoc e) {
146                     TreePath JavaDoc p = e.getTreePath().pathByAddingChild(e.getChildren()[0]);
147                     tree.scrollPathToVisible(p);
148                 }
149                 public void treeNodesRemoved(TreeModelEvent JavaDoc e) {
150                     tree.setSelectionPath(null);
151                 }
152                 public void treeStructureChanged(TreeModelEvent JavaDoc e) {
153                 }
154             });
155             
156             tree.getSelectionModel().setSelectionMode
157                 (TreeSelectionModel.SINGLE_TREE_SELECTION);
158
159             tree.addTreeSelectionListener(new TreeSelectionListener JavaDoc() {
160                 public void valueChanged(TreeSelectionEvent JavaDoc event) {
161                     JTree JavaDoc tree = (JTree JavaDoc)event.getSource();
162                     DefaultMutableTreeNode JavaDoc tn = (DefaultMutableTreeNode JavaDoc) tree.getLastSelectedPathComponent();
163                     if (tn == null) {
164                         popupListener.setPopup(null);
165                         detailScroll.setViewportView(new JPanel JavaDoc());
166                     } else {
167                         popupListener.setPopup(generatePopup(tn));
168
169                         JPanel JavaDoc detailView = new JPanel JavaDoc();
170                         detailView.setLayout(new GridBagLayout JavaDoc());
171                         GridBagConstraints JavaDoc c = new GridBagConstraints JavaDoc();
172                         c.anchor = GridBagConstraints.FIRST_LINE_START;
173                         c.fill = GridBagConstraints.NONE;
174                         c.gridx = 0;
175                         c.gridy = 0;
176                         c.weightx = 1.0;
177                         c.weighty = 1.0;
178
179                         DesignDefinition dd = null;
180                         if (tn != top) {
181                             DesignElement de = (DesignElement) tn.getUserObject();
182                             dd = de.detail();
183                         }
184                         else {
185                             dd = designDefinition.getDetail();
186                         }
187                         if (dd != null) {
188                             detailView.add(FormFactory.createForm(
189                                     new StandardForm(dd.getTitle())
190                                     .addGroup(dd)), c);
191                             detailScroll.setViewportView(detailView);
192                         }
193                     }
194                     detailScroll.validate();
195                     detailScroll.repaint();
196                 }
197             });
198             tree.addMouseListener(popupListener);
199             
200             setLayout(new BorderLayout JavaDoc());
201             JScrollPane JavaDoc treeScroll = new JScrollPane JavaDoc();
202             treeScroll.setPreferredSize(new Dimension JavaDoc(
203                     Looks.DESIGNER_TREE_WIDTH, Looks.DESIGNER_HEIGHT));
204             
205             treeScroll.setViewportView(tree);
206
207             JSplitPane JavaDoc split = new JSplitPane JavaDoc(JSplitPane.HORIZONTAL_SPLIT,
208                 treeScroll, detailScroll);
209
210             add(split);
211         }
212     }
213
214     
215     JPopupMenu JavaDoc generatePopup(DefaultMutableTreeNode JavaDoc tn) {
216         JMenu JavaDoc menu = new JMenu JavaDoc();
217         DesignElementType de = (DesignElementType) tn.getUserObject();
218         final String JavaDoc[] choices = de.supportedTypes();
219         for (int i = 0; i < choices.length; ++i) {
220             menu.add(new JMenuItem JavaDoc(new AddAction(tn, choices[i])));
221         }
222         if (tn != top) {
223             menu.add(new JMenuItem JavaDoc(new DeleteAction(tn)));
224         }
225         return menu.getPopupMenu();
226     }
227         
228     class AddAction extends AbstractAction JavaDoc {
229         final DefaultMutableTreeNode JavaDoc tn;
230         final String JavaDoc type;
231         AddAction(DefaultMutableTreeNode JavaDoc tn, String JavaDoc type) {
232             this.tn = tn;
233             this.type = type;
234             putValue(Action.NAME, "Add " + type);
235         }
236         public void actionPerformed(ActionEvent JavaDoc e) {
237             DesignElementType de = (DesignElementType) tn.getUserObject();
238             DesignElementType nde = de.createType(type);
239             de.addChild(nde);
240             DefaultMutableTreeNode JavaDoc ntn= new DefaultMutableTreeNode JavaDoc(nde);
241             model.insertNodeInto(ntn, tn, tn.getChildCount());
242         }
243     }
244
245     class DeleteAction extends AbstractAction JavaDoc {
246         final DefaultMutableTreeNode JavaDoc tn;
247         DeleteAction(DefaultMutableTreeNode JavaDoc tn) {
248             this.tn = tn;
249             putValue(Action.NAME, "Delete");
250         }
251         public void actionPerformed(ActionEvent JavaDoc e) {
252             int index = tn.getParent().getIndex(tn);
253             DesignElementType parentDE = (DesignElementType)
254              ((DefaultMutableTreeNode JavaDoc)tn.getParent()).getUserObject();
255             parentDE.removeChild(index);
256             model.removeNodeFromParent(tn);
257         }
258     }
259
260 }
261
Popular Tags