KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > ajde > ui > swing > TreeViewBuildConfigEditor


1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the IDE support for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  *
22  * Contributor(s):
23  */

24
25
26 package org.aspectj.ajde.ui.swing;
27
28 import javax.swing.*;
29 import javax.swing.tree.*;
30 import java.awt.*;
31 import java.awt.event.*;
32 import java.util.*;
33 import org.aspectj.ajde.*;
34 import javax.swing.event.*;
35 import java.io.*;
36
37 import org.aspectj.ajde.ui.*;
38 import org.aspectj.ajde.internal.*;
39 import org.aspectj.asm.*;
40 /**
41  * UI for editing build configuration (".lst") files via a graphical tree-based
42  * representation.
43  *
44  * @author Mik Kersten
45  */

46 public class TreeViewBuildConfigEditor extends JPanel implements BuildConfigEditor {
47
48     private ConfigTreeNode root;
49     private ConfigTreeNode currNode;
50     private BuildConfigModel model = null;
51     
52     private static java.util.List JavaDoc selectedEntries = new ArrayList();
53     private String JavaDoc configFile = null;
54     private File sourcePath = null;
55     //private BuildConfigModelBuilder configTreeBuilder = new BuildConfigModelBuilder();
56

57     BorderLayout borderLayout1 = new BorderLayout();
58     JPanel jPanel1 = new JPanel();
59     JLabel jLabel1 = new JLabel();
60     JPanel jPanel2 = new JPanel();
61     JButton cancel_button = new JButton();
62     BorderLayout borderLayout2 = new BorderLayout();
63     JButton save_button = new JButton();
64     JScrollPane jScrollPane = new JScrollPane();
65     JTree buildConfig_tree = new JTree();
66
67     public void openFile(String JavaDoc configFile) throws IOException, InvalidResourceException {
68         try {
69             if (configFile == null) {
70                 Ajde.getDefault().getErrorHandler().handleError("No structure is selected for editing.");
71                 return;
72             }
73             this.configFile = configFile;
74             sourcePath = new File(new File(configFile).getParent());
75             jbInit();
76             jLabel1.setText(" Build configuration: " + configFile);
77
78             model = Ajde.getDefault().getConfigurationManager().buildModel(configFile);
79             root = buildTree((BuildConfigNode)model.getRoot());
80
81             buildConfig_tree.setModel(new DefaultTreeModel(root));
82             buildConfig_tree.addMouseListener(new ConfigFileMouseAdapter(buildConfig_tree));
83             buildConfig_tree.setCellRenderer(new ConfigTreeCellRenderer());
84
85             for (int j = 0; j < buildConfig_tree.getRowCount(); j++) {
86                 buildConfig_tree.expandPath(buildConfig_tree.getPathForRow(j));
87             }
88         } catch(Exception JavaDoc e) {
89             Ajde.getDefault().getErrorHandler().handleError("Could not open file.", e);
90         }
91     }
92     
93     private ConfigTreeNode buildTree(BuildConfigNode node) {
94         ConfigTreeNode treeNode = new ConfigTreeNode(node);
95         for (Iterator it = node.getChildren().iterator(); it.hasNext(); ) {
96             BuildConfigNode childNode = (BuildConfigNode)it.next();
97             treeNode.add(buildTree(childNode));
98         }
99         return treeNode;
100     }
101     
102     private void saveModel() {
103         Ajde.getDefault().getConfigurationManager().writeModel(model);
104     }
105
106     private void jbInit() throws Exception JavaDoc {
107         this.setLayout(borderLayout1);
108         jLabel1.setFont(new java.awt.Font JavaDoc("Dialog", 0, 11));
109         jLabel1.setMaximumSize(new Dimension(80, 30));
110         jLabel1.setMinimumSize(new Dimension(80, 20));
111         jLabel1.setPreferredSize(new Dimension(80, 20));
112         jLabel1.setText("Config File Editor");
113         cancel_button.setFont(new java.awt.Font JavaDoc("Dialog", 0, 11));
114         cancel_button.setMaximumSize(new Dimension(73, 20));
115         cancel_button.setMinimumSize(new Dimension(73, 20));
116         cancel_button.setPreferredSize(new Dimension(73, 20));
117         cancel_button.setText("Cancel");
118         cancel_button.addActionListener(new java.awt.event.ActionListener JavaDoc() {
119             public void actionPerformed(ActionEvent e) {
120                 cancel_button_actionPerformed(e);
121             }
122         });
123         jPanel1.setLayout(borderLayout2);
124         save_button.setText("Save");
125         save_button.addActionListener(new java.awt.event.ActionListener JavaDoc() {
126             public void actionPerformed(ActionEvent e) {
127                 save_button_actionPerformed(e);
128             }
129         });
130         save_button.setPreferredSize(new Dimension(73, 20));
131         save_button.setMinimumSize(new Dimension(73, 20));
132         save_button.setMaximumSize(new Dimension(73, 20));
133         save_button.setFont(new java.awt.Font JavaDoc("Dialog", 0, 11));
134         this.add(jPanel1, BorderLayout.NORTH);
135         jPanel1.add(jPanel2, BorderLayout.EAST);
136         jPanel2.add(save_button, null);
137         //jPanel2.add(cancel_button, null);
138
jPanel1.add(jLabel1, BorderLayout.CENTER);
139         this.add(jScrollPane, BorderLayout.CENTER);
140         jScrollPane.getViewport().add(buildConfig_tree, null);
141     }
142
143     private class ConfigTreeNode extends DefaultMutableTreeNode {
144         public JCheckBox checkBox = null;
145         public BuildConfigNode modelNode;
146
147         public ConfigTreeNode(BuildConfigNode modelNode) {
148             super(modelNode.getName(), true);
149             this.modelNode = modelNode;
150             checkBox = new JCheckBox();
151         }
152         public BuildConfigNode getModelNode() {
153             return modelNode;
154         }
155
156         public void setModelNode(BuildConfigNode modelNode) {
157             this.modelNode = modelNode;
158         }
159
160     }
161
162     private class ConfigFileMouseAdapter extends MouseAdapter {
163         private JTree tree = null;
164         final JCheckBox checkBoxProto = new JCheckBox();
165         final int width = checkBoxProto.getPreferredSize().width;
166
167         public ConfigFileMouseAdapter(JTree tree) {
168             super();
169             this.tree = tree;
170         }
171
172         public void mousePressed(MouseEvent e) {
173             int x = e.getX();
174             int y = e.getY();
175             TreePath path = tree.getClosestPathForLocation(x,y);
176             ConfigTreeNode node = (ConfigTreeNode)path.getLastPathComponent();
177
178             // if (isCheckBox(x, tree.getPathBounds(path).x)) {
179
if (node.checkBox.isSelected()) {
180                 node.getModelNode().setActive(false);
181                 node.checkBox.setSelected(false);
182             } else {
183                 node.getModelNode().setActive(true);
184                 node.checkBox.setSelected(true);
185             }
186             
187             ((DefaultTreeModel)tree.getModel()).nodeChanged(node);
188             if (node.getModelNode().getName() != null) {
189                 if (node.checkBox.isSelected()) {
190                     selectedEntries.add(node.getModelNode().getName());
191                 } else {
192                     selectedEntries.remove(node.getModelNode().getName());
193                 }
194             }
195             super.mousePressed(e);
196
197         }
198
199         boolean isCheckBox(int x, int x_) {
200             int d = x - x_;
201             return (d < width) && (d > 0);
202         }
203     }
204
205     static class ConfigTreeCellRenderer extends DefaultTreeCellRenderer {
206         public Component getTreeCellRendererComponent(JTree tree,
207                                                       Object JavaDoc value,
208                                                       boolean sel,
209                                                       boolean expanded,
210                                                       boolean leaf,
211                                                       int row,
212                                                       boolean hasFocus) {
213             super.getTreeCellRendererComponent(tree, value, sel, expanded,
214                                                leaf, row, hasFocus);
215             JPanel p = new JPanel();
216             p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
217             p.setBackground(Color.white);
218             //if (leaf) {
219
setFont(new Font("Dialog", Font.PLAIN, 11));
220             final JCheckBox cbox = ((ConfigTreeNode)value).checkBox;
221             cbox.setBackground(Color.white);
222             if (row != 0) {
223                 p.add(cbox);
224             }
225
226             ConfigTreeNode ctn = (ConfigTreeNode)value;
227             //if (TreeViewBuildConfigEditor.selectedEntries.contains(ctn.getSourceFile())) {
228
if (ctn.getModelNode().isActive()) {
229                 cbox.setSelected(true);
230             }
231             
232             if (!ctn.getModelNode().isValidResource()) {
233                 ctn.checkBox.setEnabled(false);
234             }
235             
236             //}
237
BuildConfigNode.Kind kind = ctn.getModelNode().getBuildConfigNodeKind();
238             if (kind.equals(BuildConfigNode.Kind.FILE_ASPECTJ)) {
239                 setIcon(AjdeUIManager.getDefault().getIconRegistry().getStructureSwingIcon(ProgramElementNode.Kind.FILE_ASPECTJ));
240             } else if (kind.equals(BuildConfigNode.Kind.FILE_JAVA)) {
241                 setIcon(AjdeUIManager.getDefault().getIconRegistry().getStructureSwingIcon(ProgramElementNode.Kind.FILE_JAVA));
242             } else if (kind.equals(BuildConfigNode.Kind.FILE_LST)) {
243                 setIcon(AjdeUIManager.getDefault().getIconRegistry().getStructureSwingIcon(ProgramElementNode.Kind.FILE_LST));
244             } else if (kind.equals(BuildConfigNode.Kind.DIRECTORY)) {
245                 setIcon(AjdeUIManager.getDefault().getIconRegistry().getStructureSwingIcon(ProgramElementNode.Kind.PACKAGE));
246             } else {
247                 setIcon((Icon)AjdeUIManager.getDefault().getIconRegistry().getStructureIcon(ProgramElementNode.Kind.ERROR).getIconResource());
248                 p.remove(cbox);
249             }
250            
251 // if (ctn.getModelNode().getResourcePath() != null) {
252
// if (ctn.getModelNode().getResourcePath().endsWith(".java")) {
253
// this.setIcon(AjdeUIManager.getDefault().getIconRegistry().getStructureSwingIcon(ProgramElementNode.Kind.CLASS));
254
// } else if (ctn.getModelNode().getResourcePath().endsWith(".aj")) {
255
// this.setIcon(AjdeUIManager.getDefault().getIconRegistry().getStructureSwingIcon(ProgramElementNode.Kind.ASPECT));
256
// } else {
257
// this.setIcon(AjdeUIManager.getDefault().getIconRegistry().getStructureSwingIcon(ProgramElementNode.Kind.PACKAGE));
258
// }
259
// }
260

261             p.add(this);
262             return p;
263         }
264     }
265
266     void cancel_button_actionPerformed(ActionEvent e) {
267         //resetEditorFrame();
268
}
269
270     void save_button_actionPerformed(ActionEvent e) {
271         saveModel();
272         //resetEditorFrame();
273
}
274
275 // private void resetEditorFrame() {
276
// BrowserManager.getDefault().resetEditorFrame();
277
// }
278
}
279
280
Popular Tags