001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.cleverphl.gui;
025:
026: import jacareto.cleverphl.CleverPHL;
027: import jacareto.cleverphl.session.Session;
028: import jacareto.cleverphl.session.SessionListEvent;
029: import jacareto.cleverphl.session.SessionListListener;
030: import jacareto.comp.Components;
031: import jacareto.test.Test;
032: import jacareto.toolkit.awt.RatioLayout;
033:
034: import java.awt.Component;
035: import java.awt.event.ActionEvent;
036: import java.awt.event.ActionListener;
037: import java.awt.event.WindowEvent;
038: import java.awt.event.WindowListener;
039:
040: import java.util.Enumeration;
041: import java.util.Hashtable;
042: import java.util.Iterator;
043: import java.util.Vector;
044:
045: import javax.swing.JButton;
046: import javax.swing.JCheckBoxMenuItem;
047: import javax.swing.JPanel;
048: import javax.swing.JScrollPane;
049: import javax.swing.tree.DefaultMutableTreeNode;
050: import javax.swing.tree.TreePath;
051:
052: /**
053: * Frame for adding a whole tree of compnents as a tree of tests
054: *
055: * @author uak juri <a href="mailto:frommer@rbg.informatik.tu-darmstadt.de">Juri Frommer</a>
056: * @version 1.2
057: */
058: public class InsertTestIntoTreeFrame extends CleverPHLFrame implements
059: SessionListListener, WindowListener {
060: /** The Hashtable of components tree visualizations. */
061: private Hashtable visualizations;
062:
063: /** The actual visualization. */
064: private ComponentsTreeVisualization actualVisualization;
065:
066: /** The vector of the (sub)tests of the tree elements to test. */
067: private Vector tests = new Vector();
068:
069: /**
070: * Creates a new Insert Tests frame.
071: *
072: * @param cleverPHL the CleverPHL instance
073: */
074: public InsertTestIntoTreeFrame(CleverPHL cleverPHL) {
075: super (cleverPHL, "CleverPHL.InsertTestIntoTreeFrame", null);
076:
077: visualizations = new Hashtable();
078: actualVisualization = null;
079:
080: setTitle("Insert Tests");
081: addWindowListener(this );
082:
083: // Let's draw the components tree
084: // getting the actual components of the session
085: //Components comps = cleverPHL.getComponentsFrame().getComponentsTreeVisualization().getTreeComponents();
086: // and add this window to the tree of components which should be ignored
087: //comps.addTree(this, false, false);
088: // now display the window
089: //visualize(comps);
090: //displayFrame();//actualVisualization);
091: //setLocation (300, 100);
092: //setToPreferredSize (400, 400);
093: cleverPHL.getSessionList().addSessionListListener(this );
094: }
095:
096: /**
097: * Invoked when the session list has changed.
098: *
099: * @param event the session list event
100: */
101: public void sessionListChanged(SessionListEvent event) {
102: Session session = event.getSession();
103: Components components = session.getComponents();
104:
105: int ID = event.getID();
106:
107: if (ID == SessionListEvent.SESSION_ADDED) {
108: if (!visualizations.containsKey(components)) {
109: visualizations.put(components,
110: new ComponentsTreeVisualization(null, null,
111: components));
112: }
113: } else if (ID == SessionListEvent.SESSION_REMOVED) {
114: if (visualizations.containsKey(components)) {
115: visualizations.remove(components);
116: }
117: } else if (ID == SessionListEvent.NEW_ACTUAL_SESSION) {
118: visualize(components);
119: }
120: }
121:
122: /**
123: * Changes the components visualization
124: *
125: * @param components the components to visualize
126: */
127: private void visualize(Components components) {
128: /*
129: if (!visualizations.containsKey(components))
130: {
131: visualizations.put(components, new ComponentsTreeVisualization(components));
132: }
133: if (actualVisualization != null)
134: {
135: getContentPane().removeAll();
136: }
137: displayFrame(components);
138: */
139: if (!visualizations.containsKey(components)) {
140: visualizations.put(components,
141: new ComponentsTreeVisualization(null, null,
142: components));
143: }
144:
145: if (actualVisualization != null) {
146: getContentPane().removeAll();
147: }
148:
149: actualVisualization = (ComponentsTreeVisualization) visualizations
150: .get(components);
151: displayFrame(); //actualVisualization);
152: validate();
153: repaint();
154: }
155:
156: /**
157: * Displays the frame with the specified components in it.
158: */
159: private void displayFrame() //ComponentsTreeVisualization visualization)
160: {
161: //actualVisualization = (ComponentsTreeVisualization) visualizations.get(components);
162: // create the two buttons "insert" and "clear"
163: JPanel buttonPanel = new JPanel();
164: JButton insertButton = new JButton();
165: insertButton.setText("Insert");
166: insertButton.addActionListener(new ActionListener() {
167: public void actionPerformed(ActionEvent e) {
168: TreePath[] tp = getComponentsTree().getSelectionPaths();
169:
170: if (tp != null) {
171: traverseTree((DefaultMutableTreeNode) tp[0]
172: .getLastPathComponent());
173:
174: for (int i = 1; i < tp.length; i++) {
175: if (!tp[i].isDescendant(tp[i - 1])) {
176: traverseTree((DefaultMutableTreeNode) tp[i]
177: .getLastPathComponent());
178: }
179: }
180: }
181: }
182: });
183:
184: buttonPanel.add(insertButton);
185:
186: // get the tree visualization and create a JScrollPanel out of it
187: JScrollPane treePane = new JScrollPane(actualVisualization);
188:
189: //wheel scrolling is supported since Jdk 1.4
190: //treePane.setWheelScrollingEnabled(true);
191: // setting the layout...
192: getContentPane().setLayout(new RatioLayout());
193:
194: //...and adding the button and the tree pane
195: getContentPane().add("0.0,0.0;1.0,0.9", treePane);
196: getContentPane().add("0.1,0.9;0.80,0.1", buttonPanel);
197:
198: setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
199:
200: /*
201: this.validate();
202: this.show();
203: */
204: }
205:
206: /**
207: * Updates the frame
208: */
209: public void update() {
210: if (actualVisualization != null) {
211: getContentPane().removeAll();
212: actualVisualization.updateTree();
213: displayFrame();
214: validate();
215: repaint();
216: }
217: }
218:
219: /**
220: * Closes the frame.
221: */
222: public void close() {
223: setVisible(false);
224: ((JCheckBoxMenuItem) cleverPHL.getMainFrame()
225: .getCleverPHLMenuBar().getMenuItem(
226: "CleverPHL.Menu.ViewInsertTestIntoTreeWindow"))
227: .setSelected(false);
228: }
229:
230: /**
231: * Traverses a (sub)tree of components with <code>node</code> as root element and inserts this
232: * tree as a tree of tests in the actual sessions record.
233: *
234: * @param node of the component tree for which to insert the tests
235: */
236: private void traverseTree(DefaultMutableTreeNode node) {
237: Session session = cleverPHL.getSessionList().getActual();
238: Components components = cleverPHL.getComponentsFrame()
239: .getComponentsTreeVisualization().getTreeComponents();
240: Component component;
241:
242: if (node.getParent() != null) {
243: component = components.getComponent(getComponentPath(node));
244: components.toFront(component);
245:
246: Test tmpTest = session.getTestLoader().getTest(component);
247:
248: if (tmpTest != null) {
249: tests.add(recurseTree(node, tmpTest));
250: } else {
251: cleverPHL
252: .getLogger()
253: .info(
254: cleverPHL
255: .getLanguage()
256: .getString(
257: "CleverPHL.Menu.InsertTestIntoTreeFrame.NotInserted"));
258: cleverPHL.getLogger().info(getComponentPath(node));
259:
260: if (!node.isLeaf()) {
261: cleverPHL
262: .getLogger()
263: .info(
264: cleverPHL
265: .getLanguage()
266: .getString(
267: "CleverPHL.Menu.InsertTestIntoTreeFrame.Recursing"));
268:
269: Enumeration children = node.children();
270:
271: while (children.hasMoreElements()) {
272: traverseTree((DefaultMutableTreeNode) children
273: .nextElement());
274: }
275: }
276: }
277: } else //we are in the root element of all components
278: {
279: Enumeration children = node.children();
280:
281: while (children.hasMoreElements()) {
282: traverseTree((DefaultMutableTreeNode) children
283: .nextElement());
284: }
285: }
286:
287: Iterator testIter = tests.iterator();
288:
289: while (testIter.hasNext()) {
290: Test test = (Test) testIter.next();
291: session.insertRecordable(test);
292: cleverPHL
293: .getLogger()
294: .info(
295: cleverPHL
296: .getLanguage()
297: .getString(
298: "CleverPHL.Menu.InsertTestIntoTreeFrame.Inserted"));
299: }
300:
301: if (!tests.isEmpty()) {
302: session.restructureRecord();
303: tests.clear();
304: }
305: }
306:
307: /**
308: * Recurses a (sub)tree, starting with <code>node</code> as root node and adds the tests of its
309: * children to the corresponding root node <code>test</code> of the test tree
310: *
311: * @param node the node of the component tree for which to insert the tests.
312: * @param test the node of the test tree to which the subtests shoud be inserted
313: *
314: * @return <code>test</code> with added subtests
315: */
316: private Test recurseTree(DefaultMutableTreeNode node, Test test) {
317: Session session = cleverPHL.getSessionList().getActual();
318: Components components = cleverPHL.getComponentsFrame()
319: .getComponentsTreeVisualization().getTreeComponents();
320: Component component;
321:
322: Enumeration children = node.children();
323:
324: while (children.hasMoreElements()) {
325: DefaultMutableTreeNode child = (DefaultMutableTreeNode) children
326: .nextElement();
327: component = components
328: .getComponent(getComponentPath(child));
329:
330: Test tmpTest = session.getTestLoader().getTest(component);
331:
332: if (tmpTest != null) {
333: test.addChild(recurseTree(child, tmpTest));
334: } else {
335: cleverPHL
336: .getLogger()
337: .info(
338: cleverPHL
339: .getLanguage()
340: .getString(
341: "CleverPHL.Menu.InsertTestIntoTreeFrame.NotInserted"));
342: cleverPHL.getLogger().info(getComponentPath(child));
343: cleverPHL
344: .getLogger()
345: .info(
346: cleverPHL
347: .getLanguage()
348: .getString(
349: "CleverPHL.Menu.InsertTestIntoTreeFrame.Recursing"));
350:
351: Hashtable grandTests = new Hashtable();
352: Enumeration grandchildren = child.children();
353:
354: while (grandchildren.hasMoreElements()) {
355: DefaultMutableTreeNode grandchild = (DefaultMutableTreeNode) grandchildren
356: .nextElement();
357: component = components
358: .getComponent(getComponentPath(child));
359: tmpTest = session.getTestLoader()
360: .getTest(component);
361:
362: if (tmpTest != null) {
363: grandTests.put(grandchild, tmpTest);
364: }
365: }
366:
367: Enumeration grandEnum = grandTests.keys();
368:
369: while (grandEnum.hasMoreElements()) {
370: Object grandchild = grandEnum.nextElement();
371: test.addChild(recurseTree(
372: (DefaultMutableTreeNode) grandchild,
373: (Test) grandTests.get(grandchild)));
374: }
375: }
376: }
377:
378: return test;
379: }
380:
381: /**
382: * Build the component name path for <code>node</code>
383: *
384: * @param node the node for which to buid the compnent name path
385: *
386: * @return the component name path
387: */
388: private String getComponentPath(DefaultMutableTreeNode node) {
389: String componentPath = "";
390:
391: if (node != null) {
392: componentPath = node.getUserObject().toString();
393:
394: DefaultMutableTreeNode helper = node;
395:
396: while (helper.getParent() != null) {
397: helper = (DefaultMutableTreeNode) helper.getParent();
398:
399: if (helper.getParent() != null) {
400: componentPath = helper.getUserObject().toString()
401: + "." + componentPath;
402: }
403: }
404:
405: return componentPath;
406: }
407:
408: return componentPath;
409: }
410:
411: /**
412: * Does nothing.
413: *
414: * @param w DOCUMENT ME!
415: */
416: public void windowActivated(WindowEvent w) {
417: }
418:
419: /**
420: * Does nothing.
421: *
422: * @param w DOCUMENT ME!
423: */
424: public void windowClosed(WindowEvent w) {
425: }
426:
427: /**
428: * Does nothing.
429: *
430: * @param w DOCUMENT ME!
431: */
432: public void windowDeactivated(WindowEvent w) {
433: }
434:
435: /**
436: * Does nothing.
437: *
438: * @param w DOCUMENT ME!
439: */
440: public void windowDeiconified(WindowEvent w) {
441: }
442:
443: /**
444: * Does nothing.
445: *
446: * @param w DOCUMENT ME!
447: */
448: public void windowIconified(WindowEvent w) {
449: }
450:
451: /**
452: * Does nothing.
453: *
454: * @param w DOCUMENT ME!
455: */
456: public void windowOpened(WindowEvent w) {
457: }
458:
459: /**
460: * Exits the system.
461: *
462: * @param w DOCUMENT ME!
463: */
464: public void windowClosing(WindowEvent w) {
465: close();
466: }
467:
468: /**
469: * DOCUMENT ME!
470: *
471: * @return ComponentsTreeVisualization
472: */
473: public ComponentsTreeVisualization getComponentsTree() {
474: return actualVisualization;
475: }
476:
477: /**
478: * DOCUMENT ME!
479: *
480: * @param visualization
481: */
482: public void setComponentsTree(
483: ComponentsTreeVisualization visualization) {
484: actualVisualization = visualization;
485: }
486: }
|