1 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 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 selectedEntries = new ArrayList(); 53 private String configFile = null; 54 private File sourcePath = null; 55 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 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 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 { 107 this.setLayout(borderLayout1); 108 jLabel1.setFont(new java.awt.Font ("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 ("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 () { 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 () { 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 ("Dialog", 0, 11)); 134 this.add(jPanel1, BorderLayout.NORTH); 135 jPanel1.add(jPanel2, BorderLayout.EAST); 136 jPanel2.add(save_button, null); 137 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 (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 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 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 (ctn.getModelNode().isActive()) { 229 cbox.setSelected(true); 230 } 231 232 if (!ctn.getModelNode().isValidResource()) { 233 ctn.checkBox.setEnabled(false); 234 } 235 236 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 261 p.add(this); 262 return p; 263 } 264 } 265 266 void cancel_button_actionPerformed(ActionEvent e) { 267 } 269 270 void save_button_actionPerformed(ActionEvent e) { 271 saveModel(); 272 } 274 275 } 279 280 | Popular Tags |