001: // This file is part of KeY - Integrated Deductive Software Design
002: // Copyright (C) 2001-2007 Universitaet Karlsruhe, Germany
003: // Universitaet Koblenz-Landau, Germany
004: // Chalmers University of Technology, Sweden
005: //
006: // The KeY system is protected by the GNU General Public License.
007: // See LICENSE.TXT for details.
008: //
009: //
010:
011: package de.uka.ilkd.key.casetool.patternimplementor;
012:
013: import java.awt.BorderLayout;
014: import java.awt.Dimension;
015: import java.awt.FlowLayout;
016: import java.awt.event.ActionEvent;
017: import java.awt.event.ActionListener;
018: import java.util.Enumeration;
019: import java.util.Observable;
020: import java.util.Observer;
021:
022: import javax.swing.*;
023: import javax.swing.tree.DefaultMutableTreeNode;
024: import javax.swing.tree.TreePath;
025:
026: public class PatternMechanismUI implements ActionListener, Observer {
027:
028: private static final String OK = "Ok";
029:
030: private static final String PREVIEW = "Preview";
031:
032: private static final String CANCEL = "Cancel";
033:
034: PatternMechanism pm;
035:
036: // the main window, all parts except buttonPanel should be in scrollpanes
037: JDialog mainDialog;
038:
039: // the ui of the pattern
040: JPanel patternPanel;
041:
042: // a tree where it's possible to select which pattern to use
043: JTree patternSelectorTree;
044:
045: // apply, cancel, preview
046: JPanel buttonPanel;
047:
048: // the buttons in the buttonPanel
049: JButton okButton;
050:
051: JButton previewButton;
052:
053: JButton cancelButton;
054:
055: // shows information about the pattern
056: JComponent infoPanel;
057:
058: public PatternMechanismUI(PatternMechanism pm) {
059: setPatternMechanism(pm);
060: buildUI();
061: }
062:
063: public void setPatternMechanism(PatternMechanism pm) {
064: this .pm = pm;
065: pm.addObserver(this );
066: }
067:
068: private void buildUI() {
069: //System.out.println("buildUI");
070: if (mainDialog == null) {
071: patternSelectorTree = buildTree();
072:
073: buttonPanel = new JPanel();
074: buttonPanel.setLayout(new FlowLayout());
075:
076: okButton = new JButton(OK);
077: previewButton = new JButton(PREVIEW);
078: cancelButton = new JButton(CANCEL);
079: okButton.addActionListener(this );
080: previewButton.addActionListener(this );
081: cancelButton.addActionListener(this );
082:
083: buttonPanel.add(previewButton);
084: buttonPanel.add(okButton);
085: buttonPanel.add(cancelButton);
086:
087: mainDialog = new JDialog(new JFrame(),
088: "KeY Pattern Mechanism", true);
089:
090: mainDialog.setSize(new Dimension(700, 500));
091:
092: /*
093: * mainWindowDialog = new JDialog(new JFrame(), true);
094: * mainWindowDialog.getContentPane().add(mainFrame);
095: * mainWindowDialog.setVisible(true);
096: */
097: }
098:
099: mainDialog.getContentPane().removeAll();
100: mainDialog.getContentPane().setLayout(new BorderLayout());
101: mainDialog.getContentPane()
102: .add(new JScrollPane(patternSelectorTree),
103: BorderLayout.WEST);
104: mainDialog.getContentPane()
105: .add(buttonPanel, BorderLayout.SOUTH);
106:
107: mainDialog.getContentPane().add(new JScrollPane(centerPanel()),
108: BorderLayout.CENTER);
109:
110: //mainFrame.pack();
111: mainDialog.setVisible(true);
112: }
113:
114: public void update(Observable o, Object arg) {
115: //System.out.println("PMUI.update");
116: buildUI();
117: }
118:
119: private JPanel centerPanel() {
120: JPanel center = new JPanel();
121:
122: try {
123: patternPanel = new PIParameterGUIGroup(pm.getParameters());
124: center.setLayout(new BorderLayout());
125: center.add(patternPanel, BorderLayout.WEST);
126: infoPanel = new JTextArea(pm.getImplementation().toString());
127: ((JTextArea) infoPanel).setEditable(false);
128: center.add(infoPanel, BorderLayout.SOUTH);
129: } catch (Exception e) {
130: e.printStackTrace();
131:
132: JLabel label = new JLabel("No Patterns found");
133: center.add(label);
134: }
135:
136: return center;
137: }
138:
139: /**
140: * /-------------------------\ | pS-tree | patternPanel | | | | | | | |
141: * +---------------| | | infoPanel | | | | |-------------------------| |
142: * buttonPanel | | [Preview][Ok][Cancel] | \-------------------------/
143: */
144: public void actionPerformed(ActionEvent e) {
145: if (OK.equals(e.getActionCommand())) {
146: //System.out.println("Button pressed: " + OK);
147: //System.out.println(pm.getImplementation());
148: PIParameterGroup pg = pm.getParameters();
149: String[] constraint = new String[pg.size()];
150: for (int i = 0; i < constraint.length; i++) {
151: constraint[i] = pg.getValue(i);
152: //System.out.println("constraint[" + i + "] = " + constraint[i]);
153: }
154: mainDialog.dispose();
155:
156: //pm.setPattern(pm.getPatterns()[1]);
157: } else if (PREVIEW.equals(e.getActionCommand())) {
158: //System.out.println("Button pressed: " + PREVIEW);
159:
160: //infoPanel.setVisible(false);
161: ((JTextArea) infoPanel).setText(pm.getImplementation()
162: .toString());
163:
164: //((JTextArea)infoPanel).setEditable(false);
165: //infoPanel.setVisible(true);
166: } else if (CANCEL.equals(e.getActionCommand())) {
167: //System.out.println("Button pressed: " + CANCEL);
168: //System.out.println("Remove this exit feature");
169: //System.exit(0);
170: this .pm.cancel();
171: mainDialog.dispose();
172: }
173: }
174:
175: private JTree buildTree() {
176: //pm.getPatterns();
177: DefaultMutableTreeNode root = new DefaultMutableTreeNode(
178: "Design patterns");
179:
180: //DefaultMutableTreeNode selected;
181: TreePath actualPath = new TreePath(root);
182:
183: for (int i = 0; i < pm.getPatterns().length; i++) {
184: PMTreeNode node = new PMTreeNode(pm.getPatterns()[i]);
185:
186: //node.addTreeSelectionListener(pm);
187: String path = node.getpmPath();
188: DefaultMutableTreeNode lastNode = root;
189:
190: while (path.indexOf(':') != -1) {
191: //System.out.println("path " + path);
192: DefaultMutableTreeNode tmpnode = new DefaultMutableTreeNode(
193: path.substring(0, path.indexOf(':')));
194: path = path.substring(path.indexOf(':') + 1, path
195: .length());
196:
197: /*
198: * System.err.println("lastNode.getIndex(tmpnode) = " +
199: * lastNode.getIndex(tmpnode));
200: */
201: for (Enumeration e = lastNode.children(); e
202: .hasMoreElements();) {
203: DefaultMutableTreeNode child = (DefaultMutableTreeNode) e
204: .nextElement();
205:
206: if (child.toString().equals(tmpnode.toString())) {
207: tmpnode = child;
208: }
209: }
210:
211: lastNode.add(tmpnode);
212: lastNode = tmpnode;
213:
214: if (i == 0) {
215: actualPath = actualPath.pathByAddingChild(lastNode);
216: }
217: }
218:
219: lastNode.add(node);
220:
221: if (i == 0) {
222: actualPath = actualPath.pathByAddingChild(node);
223: }
224: }
225:
226: JTree tree = new JTree(root);
227: tree.setVisible(false);
228:
229: //System.out.println(actualPath);
230: //System.out.println("trying to set the path");
231: //System.out.println("old : " + tree.getSelectionPath());
232: tree.setSelectionPath(actualPath);
233:
234: //System.out.println("new : " + tree.getSelectionPath());
235: tree.setExpandsSelectedPaths(true);
236:
237: tree.setRootVisible(true);
238: tree.setVisible(true);
239:
240: tree.addTreeSelectionListener(pm);
241:
242: return tree;
243: }
244: }
|