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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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(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 (BuildConfigNode childNode : node.getChildren()) {
  97. treeNode.add(buildTree(childNode));
  98. }
  99. return treeNode;
  100. }
  101. private void saveModel() {
  102. Ajde.getDefault().getBuildConfigManager().writeModel(model);
  103. }
  104. private void jbInit() throws Exception {
  105. this.setLayout(borderLayout1);
  106. jLabel1.setFont(new java.awt.Font("Dialog", 0, 11));
  107. jLabel1.setMaximumSize(new Dimension(80, 30));
  108. jLabel1.setMinimumSize(new Dimension(80, 20));
  109. jLabel1.setPreferredSize(new Dimension(80, 20));
  110. jLabel1.setText("Config File Editor");
  111. cancel_button.setFont(new java.awt.Font("Dialog", 0, 11));
  112. cancel_button.setMaximumSize(new Dimension(73, 20));
  113. cancel_button.setMinimumSize(new Dimension(73, 20));
  114. cancel_button.setPreferredSize(new Dimension(73, 20));
  115. cancel_button.setText("Cancel");
  116. cancel_button.addActionListener(new java.awt.event.ActionListener() {
  117. public void actionPerformed(ActionEvent e) {
  118. cancel_button_actionPerformed(e);
  119. }
  120. });
  121. jPanel1.setLayout(borderLayout2);
  122. save_button.setText("Save");
  123. save_button.addActionListener(new java.awt.event.ActionListener() {
  124. public void actionPerformed(ActionEvent e) {
  125. save_button_actionPerformed(e);
  126. }
  127. });
  128. save_button.setPreferredSize(new Dimension(73, 20));
  129. save_button.setMinimumSize(new Dimension(73, 20));
  130. save_button.setMaximumSize(new Dimension(73, 20));
  131. save_button.setFont(new java.awt.Font("Dialog", 0, 11));
  132. this.add(jPanel1, BorderLayout.NORTH);
  133. jPanel1.add(jPanel2, BorderLayout.EAST);
  134. jPanel2.add(save_button, null);
  135. //jPanel2.add(cancel_button, null);
  136. jPanel1.add(jLabel1, BorderLayout.CENTER);
  137. this.add(jScrollPane, BorderLayout.CENTER);
  138. jScrollPane.getViewport().add(buildConfig_tree, null);
  139. }
  140. private static class ConfigTreeNode extends DefaultMutableTreeNode {
  141. private static final long serialVersionUID = 1L;
  142. public JCheckBox checkBox = null;
  143. public BuildConfigNode modelNode;
  144. public ConfigTreeNode(BuildConfigNode modelNode) {
  145. super(modelNode.getName(), true);
  146. this.modelNode = modelNode;
  147. checkBox = new JCheckBox();
  148. }
  149. public BuildConfigNode getModelNode() {
  150. return modelNode;
  151. }
  152. public void setModelNode(BuildConfigNode modelNode) {
  153. this.modelNode = modelNode;
  154. }
  155. }
  156. private static class ConfigFileMouseAdapter extends MouseAdapter {
  157. private JTree tree = null;
  158. final JCheckBox checkBoxProto = new JCheckBox();
  159. final int width = checkBoxProto.getPreferredSize().width;
  160. public ConfigFileMouseAdapter(JTree tree) {
  161. super();
  162. this.tree = tree;
  163. }
  164. public void mousePressed(MouseEvent e) {
  165. int x = e.getX();
  166. int y = e.getY();
  167. TreePath path = tree.getClosestPathForLocation(x,y);
  168. ConfigTreeNode node = (ConfigTreeNode)path.getLastPathComponent();
  169. // if (isCheckBox(x, tree.getPathBounds(path).x)) {
  170. if (node.checkBox.isSelected()) {
  171. node.getModelNode().setActive(false);
  172. node.checkBox.setSelected(false);
  173. } else {
  174. node.getModelNode().setActive(true);
  175. node.checkBox.setSelected(true);
  176. }
  177. ((DefaultTreeModel)tree.getModel()).nodeChanged(node);
  178. if (node.getModelNode().getName() != null) {
  179. if (node.checkBox.isSelected()) {
  180. selectedEntries.add(node.getModelNode().getName());
  181. } else {
  182. selectedEntries.remove(node.getModelNode().getName());
  183. }
  184. }
  185. super.mousePressed(e);
  186. }
  187. boolean isCheckBox(int x, int x_) {
  188. int d = x - x_;
  189. return (d < width) && (d > 0);
  190. }
  191. }
  192. static class ConfigTreeCellRenderer extends DefaultTreeCellRenderer {
  193. private static final long serialVersionUID = -3120665318910899066L;
  194. public Component getTreeCellRendererComponent(JTree tree,
  195. Object value,
  196. boolean sel,
  197. boolean expanded,
  198. boolean leaf,
  199. int row,
  200. boolean hasFocus) {
  201. super.getTreeCellRendererComponent(tree, value, sel, expanded,
  202. leaf, row, hasFocus);
  203. JPanel p = new JPanel();
  204. p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
  205. p.setBackground(Color.white);
  206. //if (leaf) {
  207. setFont(new Font("Dialog", Font.PLAIN, 11));
  208. final JCheckBox cbox = ((ConfigTreeNode)value).checkBox;
  209. cbox.setBackground(Color.white);
  210. if (row != 0) {
  211. p.add(cbox);
  212. }
  213. ConfigTreeNode ctn = (ConfigTreeNode)value;
  214. //if (TreeViewBuildConfigEditor.selectedEntries.contains(ctn.getSourceFile())) {
  215. if (ctn.getModelNode().isActive()) {
  216. cbox.setSelected(true);
  217. }
  218. if (!ctn.getModelNode().isValidResource()) {
  219. ctn.checkBox.setEnabled(false);
  220. }
  221. //}
  222. BuildConfigNode.Kind kind = ctn.getModelNode().getBuildConfigNodeKind();
  223. if (kind.equals(BuildConfigNode.Kind.FILE_ASPECTJ)) {
  224. setIcon(Ajde.getDefault().getIconRegistry().getStructureSwingIcon(IProgramElement.Kind.FILE_ASPECTJ));
  225. } else if (kind.equals(BuildConfigNode.Kind.FILE_JAVA)) {
  226. setIcon(Ajde.getDefault().getIconRegistry().getStructureSwingIcon(IProgramElement.Kind.FILE_JAVA));
  227. } else if (kind.equals(BuildConfigNode.Kind.FILE_LST)) {
  228. setIcon(Ajde.getDefault().getIconRegistry().getStructureSwingIcon(IProgramElement.Kind.FILE_LST));
  229. } else if (kind.equals(BuildConfigNode.Kind.DIRECTORY)) {
  230. setIcon(Ajde.getDefault().getIconRegistry().getStructureSwingIcon(IProgramElement.Kind.PACKAGE));
  231. } else {
  232. setIcon((Icon)Ajde.getDefault().getIconRegistry().getIcon(IProgramElement.Kind.ERROR).getIconResource());
  233. p.remove(cbox);
  234. }
  235. // if (ctn.getModelNode().getResourcePath() != null) {
  236. // if (ctn.getModelNode().getResourcePath().endsWith(".java")) {
  237. // this.setIcon(AjdeUIManager.getDefault().getIconRegistry().getStructureSwingIcon(ProgramElementNode.Kind.CLASS));
  238. // } else if (ctn.getModelNode().getResourcePath().endsWith(".aj")) {
  239. // this.setIcon(AjdeUIManager.getDefault().getIconRegistry().getStructureSwingIcon(ProgramElementNode.Kind.ASPECT));
  240. // } else {
  241. // this.setIcon(AjdeUIManager.getDefault().getIconRegistry().getStructureSwingIcon(ProgramElementNode.Kind.PACKAGE));
  242. // }
  243. // }
  244. p.add(this);
  245. return p;
  246. }
  247. }
  248. void cancel_button_actionPerformed(ActionEvent e) {
  249. //resetEditorFrame();
  250. }
  251. void save_button_actionPerformed(ActionEvent e) {
  252. saveModel();
  253. //resetEditorFrame();
  254. }
  255. // private void resetEditorFrame() {
  256. // BrowserManager.getDefault().resetEditorFrame();
  257. // }
  258. }