You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TreeViewBuildConfigEditor.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * Helen Hawkins Converted to new interface (bug 148190)
  13. * ******************************************************************/
  14. package org.aspectj.ajde.ui.swing;
  15. import java.awt.BorderLayout;
  16. import java.awt.Color;
  17. import java.awt.Component;
  18. import java.awt.Dimension;
  19. import java.awt.Font;
  20. import java.awt.event.ActionEvent;
  21. import java.awt.event.MouseAdapter;
  22. import java.awt.event.MouseEvent;
  23. import java.io.IOException;
  24. import java.util.ArrayList;
  25. import java.util.Iterator;
  26. import javax.swing.BoxLayout;
  27. import javax.swing.Icon;
  28. import javax.swing.JButton;
  29. import javax.swing.JCheckBox;
  30. import javax.swing.JLabel;
  31. import javax.swing.JPanel;
  32. import javax.swing.JScrollPane;
  33. import javax.swing.JTree;
  34. import javax.swing.tree.DefaultMutableTreeNode;
  35. import javax.swing.tree.DefaultTreeCellRenderer;
  36. import javax.swing.tree.DefaultTreeModel;
  37. import javax.swing.tree.TreePath;
  38. import org.aspectj.ajde.Ajde;
  39. import org.aspectj.ajde.ui.BuildConfigEditor;
  40. import org.aspectj.ajde.ui.BuildConfigModel;
  41. import org.aspectj.ajde.ui.BuildConfigNode;
  42. import org.aspectj.ajde.ui.InvalidResourceException;
  43. import org.aspectj.asm.IProgramElement;
  44. import org.aspectj.bridge.IMessage;
  45. import org.aspectj.bridge.Message;
  46. /**
  47. * UI for editing build configuration (".lst") files via a graphical tree-based
  48. * representation.
  49. *
  50. * @author Mik Kersten
  51. */
  52. public class TreeViewBuildConfigEditor extends JPanel implements BuildConfigEditor {
  53. private static final long serialVersionUID = 8071799814661969685L;
  54. private ConfigTreeNode root;
  55. // private ConfigTreeNode currNode;
  56. private BuildConfigModel model = null;
  57. private static java.util.List selectedEntries = new ArrayList();
  58. // private String configFile = null;
  59. // private File sourcePath = null;
  60. //private BuildConfigModelBuilder configTreeBuilder = new BuildConfigModelBuilder();
  61. BorderLayout borderLayout1 = new BorderLayout();
  62. JPanel jPanel1 = new JPanel();
  63. JLabel jLabel1 = new JLabel();
  64. JPanel jPanel2 = new JPanel();
  65. JButton cancel_button = new JButton();
  66. BorderLayout borderLayout2 = new BorderLayout();
  67. JButton save_button = new JButton();
  68. JScrollPane jScrollPane = new JScrollPane();
  69. JTree buildConfig_tree = new JTree();
  70. public void openFile(String configFile) throws IOException, InvalidResourceException {
  71. try {
  72. if (configFile == null) {
  73. Message msg = new Message("No structure is selected for editing.",IMessage.ERROR,null,null);
  74. Ajde.getDefault().getMessageHandler().handleMessage(msg);
  75. return;
  76. }
  77. // this.configFile = configFile;
  78. // sourcePath = new File(new File(configFile).getParent());
  79. jbInit();
  80. jLabel1.setText(" Build configuration: " + configFile);
  81. model = Ajde.getDefault().getBuildConfigManager().buildModel(configFile);
  82. root = buildTree((BuildConfigNode)model.getRoot());
  83. buildConfig_tree.setModel(new DefaultTreeModel(root));
  84. buildConfig_tree.addMouseListener(new ConfigFileMouseAdapter(buildConfig_tree));
  85. buildConfig_tree.setCellRenderer(new ConfigTreeCellRenderer());
  86. for (int j = 0; j < buildConfig_tree.getRowCount(); j++) {
  87. buildConfig_tree.expandPath(buildConfig_tree.getPathForRow(j));
  88. }
  89. } catch(Exception e) {
  90. Message msg = new Message("Could not open file.",IMessage.ERROR,e,null);
  91. Ajde.getDefault().getMessageHandler().handleMessage(msg);
  92. }
  93. }
  94. private ConfigTreeNode buildTree(BuildConfigNode node) {
  95. ConfigTreeNode treeNode = new ConfigTreeNode(node);
  96. for (Iterator it = node.getChildren().iterator(); it.hasNext(); ) {
  97. BuildConfigNode childNode = (BuildConfigNode)it.next();
  98. treeNode.add(buildTree(childNode));
  99. }
  100. return treeNode;
  101. }
  102. private void saveModel() {
  103. Ajde.getDefault().getBuildConfigManager().writeModel(model);
  104. }
  105. private void jbInit() throws Exception {
  106. this.setLayout(borderLayout1);
  107. jLabel1.setFont(new java.awt.Font("Dialog", 0, 11));
  108. jLabel1.setMaximumSize(new Dimension(80, 30));
  109. jLabel1.setMinimumSize(new Dimension(80, 20));
  110. jLabel1.setPreferredSize(new Dimension(80, 20));
  111. jLabel1.setText("Config File Editor");
  112. cancel_button.setFont(new java.awt.Font("Dialog", 0, 11));
  113. cancel_button.setMaximumSize(new Dimension(73, 20));
  114. cancel_button.setMinimumSize(new Dimension(73, 20));
  115. cancel_button.setPreferredSize(new Dimension(73, 20));
  116. cancel_button.setText("Cancel");
  117. cancel_button.addActionListener(new java.awt.event.ActionListener() {
  118. public void actionPerformed(ActionEvent e) {
  119. cancel_button_actionPerformed(e);
  120. }
  121. });
  122. jPanel1.setLayout(borderLayout2);
  123. save_button.setText("Save");
  124. save_button.addActionListener(new java.awt.event.ActionListener() {
  125. public void actionPerformed(ActionEvent e) {
  126. save_button_actionPerformed(e);
  127. }
  128. });
  129. save_button.setPreferredSize(new Dimension(73, 20));
  130. save_button.setMinimumSize(new Dimension(73, 20));
  131. save_button.setMaximumSize(new Dimension(73, 20));
  132. save_button.setFont(new java.awt.Font("Dialog", 0, 11));
  133. this.add(jPanel1, BorderLayout.NORTH);
  134. jPanel1.add(jPanel2, BorderLayout.EAST);
  135. jPanel2.add(save_button, null);
  136. //jPanel2.add(cancel_button, null);
  137. jPanel1.add(jLabel1, BorderLayout.CENTER);
  138. this.add(jScrollPane, BorderLayout.CENTER);
  139. jScrollPane.getViewport().add(buildConfig_tree, null);
  140. }
  141. private class ConfigTreeNode extends DefaultMutableTreeNode {
  142. private static final long serialVersionUID = 1L;
  143. public JCheckBox checkBox = null;
  144. public BuildConfigNode modelNode;
  145. public ConfigTreeNode(BuildConfigNode modelNode) {
  146. super(modelNode.getName(), true);
  147. this.modelNode = modelNode;
  148. checkBox = new JCheckBox();
  149. }
  150. public BuildConfigNode getModelNode() {
  151. return modelNode;
  152. }
  153. public void setModelNode(BuildConfigNode modelNode) {
  154. this.modelNode = modelNode;
  155. }
  156. }
  157. private class ConfigFileMouseAdapter extends MouseAdapter {
  158. private JTree tree = null;
  159. final JCheckBox checkBoxProto = new JCheckBox();
  160. final int width = checkBoxProto.getPreferredSize().width;
  161. public ConfigFileMouseAdapter(JTree tree) {
  162. super();
  163. this.tree = tree;
  164. }
  165. public void mousePressed(MouseEvent e) {
  166. int x = e.getX();
  167. int y = e.getY();
  168. TreePath path = tree.getClosestPathForLocation(x,y);
  169. ConfigTreeNode node = (ConfigTreeNode)path.getLastPathComponent();
  170. // if (isCheckBox(x, tree.getPathBounds(path).x)) {
  171. if (node.checkBox.isSelected()) {
  172. node.getModelNode().setActive(false);
  173. node.checkBox.setSelected(false);
  174. } else {
  175. node.getModelNode().setActive(true);
  176. node.checkBox.setSelected(true);
  177. }
  178. ((DefaultTreeModel)tree.getModel()).nodeChanged(node);
  179. if (node.getModelNode().getName() != null) {
  180. if (node.checkBox.isSelected()) {
  181. selectedEntries.add(node.getModelNode().getName());
  182. } else {
  183. selectedEntries.remove(node.getModelNode().getName());
  184. }
  185. }
  186. super.mousePressed(e);
  187. }
  188. boolean isCheckBox(int x, int x_) {
  189. int d = x - x_;
  190. return (d < width) && (d > 0);
  191. }
  192. }
  193. static class ConfigTreeCellRenderer extends DefaultTreeCellRenderer {
  194. private static final long serialVersionUID = -3120665318910899066L;
  195. public Component getTreeCellRendererComponent(JTree tree,
  196. Object value,
  197. boolean sel,
  198. boolean expanded,
  199. boolean leaf,
  200. int row,
  201. boolean hasFocus) {
  202. super.getTreeCellRendererComponent(tree, value, sel, expanded,
  203. leaf, row, hasFocus);
  204. JPanel p = new JPanel();
  205. p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
  206. p.setBackground(Color.white);
  207. //if (leaf) {
  208. setFont(new Font("Dialog", Font.PLAIN, 11));
  209. final JCheckBox cbox = ((ConfigTreeNode)value).checkBox;
  210. cbox.setBackground(Color.white);
  211. if (row != 0) {
  212. p.add(cbox);
  213. }
  214. ConfigTreeNode ctn = (ConfigTreeNode)value;
  215. //if (TreeViewBuildConfigEditor.selectedEntries.contains(ctn.getSourceFile())) {
  216. if (ctn.getModelNode().isActive()) {
  217. cbox.setSelected(true);
  218. }
  219. if (!ctn.getModelNode().isValidResource()) {
  220. ctn.checkBox.setEnabled(false);
  221. }
  222. //}
  223. BuildConfigNode.Kind kind = ctn.getModelNode().getBuildConfigNodeKind();
  224. if (kind.equals(BuildConfigNode.Kind.FILE_ASPECTJ)) {
  225. setIcon(Ajde.getDefault().getIconRegistry().getStructureSwingIcon(IProgramElement.Kind.FILE_ASPECTJ));
  226. } else if (kind.equals(BuildConfigNode.Kind.FILE_JAVA)) {
  227. setIcon(Ajde.getDefault().getIconRegistry().getStructureSwingIcon(IProgramElement.Kind.FILE_JAVA));
  228. } else if (kind.equals(BuildConfigNode.Kind.FILE_LST)) {
  229. setIcon(Ajde.getDefault().getIconRegistry().getStructureSwingIcon(IProgramElement.Kind.FILE_LST));
  230. } else if (kind.equals(BuildConfigNode.Kind.DIRECTORY)) {
  231. setIcon(Ajde.getDefault().getIconRegistry().getStructureSwingIcon(IProgramElement.Kind.PACKAGE));
  232. } else {
  233. setIcon((Icon)Ajde.getDefault().getIconRegistry().getIcon(IProgramElement.Kind.ERROR).getIconResource());
  234. p.remove(cbox);
  235. }
  236. // if (ctn.getModelNode().getResourcePath() != null) {
  237. // if (ctn.getModelNode().getResourcePath().endsWith(".java")) {
  238. // this.setIcon(AjdeUIManager.getDefault().getIconRegistry().getStructureSwingIcon(ProgramElementNode.Kind.CLASS));
  239. // } else if (ctn.getModelNode().getResourcePath().endsWith(".aj")) {
  240. // this.setIcon(AjdeUIManager.getDefault().getIconRegistry().getStructureSwingIcon(ProgramElementNode.Kind.ASPECT));
  241. // } else {
  242. // this.setIcon(AjdeUIManager.getDefault().getIconRegistry().getStructureSwingIcon(ProgramElementNode.Kind.PACKAGE));
  243. // }
  244. // }
  245. p.add(this);
  246. return p;
  247. }
  248. }
  249. void cancel_button_actionPerformed(ActionEvent e) {
  250. //resetEditorFrame();
  251. }
  252. void save_button_actionPerformed(ActionEvent e) {
  253. saveModel();
  254. //resetEditorFrame();
  255. }
  256. // private void resetEditorFrame() {
  257. // BrowserManager.getDefault().resetEditorFrame();
  258. // }
  259. }