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.ComponentHandler;
031: import jacareto.comp.Components;
032: import jacareto.comp.ComponentsEvent;
033: import jacareto.comp.ComponentsListener;
034: import jacareto.comp.glasspanes.ComponentSelectionPane;
035: import jacareto.comp.glasspanes.GlassPaneCompound;
036: import jacareto.comp.glasspanes.GlassPaneManager;
037: import jacareto.system.Environment;
038: import jacareto.system.Language;
039: import jacareto.test.Test;
040: import jacareto.test.TestLoader;
041: import jacareto.toolkit.swing.PopupListener;
042:
043: import java.awt.Component;
044: import java.awt.event.WindowEvent;
045: import java.awt.event.WindowListener;
046:
047: import java.util.Arrays;
048: import java.util.Hashtable;
049: import java.util.Vector;
050:
051: import javax.swing.JCheckBoxMenuItem;
052: import javax.swing.JPopupMenu;
053: import javax.swing.JScrollPane;
054: import javax.swing.JTabbedPane;
055: import javax.swing.event.ChangeEvent;
056: import javax.swing.event.ChangeListener;
057: import javax.swing.event.ListSelectionEvent;
058: import javax.swing.event.ListSelectionListener;
059: import javax.swing.event.TreeSelectionEvent;
060: import javax.swing.event.TreeSelectionListener;
061: import javax.swing.tree.DefaultMutableTreeNode;
062: import javax.swing.tree.TreePath;
063: import javax.swing.tree.TreeSelectionModel;
064:
065: /**
066: * The components frame of CleverPHL.
067: *
068: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
069: * @version 1.01
070: */
071: public class ComponentsFrame extends CleverPHLFrame implements
072: SessionListListener, WindowListener, ListSelectionListener,
073: TreeSelectionListener, ChangeListener, ComponentsListener {
074: /** The tree tab. */
075: public static final int TREE_TAB = 0;
076:
077: /** The table tab. */
078: public static final int TABLE_TAB = 1;
079:
080: /** The Hashtable of components tree visualizations. */
081: private Hashtable visualizations;
082:
083: /** The hashtable of component tables. */
084: private Hashtable tables;
085:
086: /** The actual visualization. */
087: private ComponentsTreeVisualization actualVisualization;
088:
089: /** The actual components table. */
090: private ComponentsTable actualTable;
091:
092: /** The language instance. */
093: private Language language;
094:
095: /** The environment instance. */
096: private Environment env;
097:
098: /** The tabbed pane. */
099: private JTabbedPane tabbedPane;
100:
101: /** The popup menu. */
102: private JPopupMenu componentsSelectionPopup;
103:
104: /** The popup listener. */
105: private PopupListener popupListener;
106:
107: /** The actual components instance. */
108: private Components actualComponents;
109:
110: /**
111: * Creates a new message frame.
112: *
113: * @param cleverPHL the CleverPHL instance
114: */
115: public ComponentsFrame(CleverPHL cleverPHL) {
116: super (cleverPHL, "CleverPHL.ComponentsFrame",
117: "CleverPHL.Menu.ViewComponentsWindow");
118:
119: language = cleverPHL.getLanguage();
120: env = cleverPHL.getEnvironment();
121:
122: visualizations = new Hashtable();
123: tables = new Hashtable();
124: actualVisualization = null;
125: actualTable = null;
126:
127: setTitle(language.getString("CleverPHL.ComponentsFrame.Title"));
128: addWindowListener(this );
129:
130: createMenu("CleverPHL.Menu.ComponentsFrame.MenuBar");
131:
132: cleverPHL.getSessionList().addSessionListListener(this );
133:
134: componentsSelectionPopup = (JPopupMenu) getCleverPHLMenuBar()
135: .getMenuComponentFromCustomization(
136: "CleverPHL.Menu.ComponentSelectionPopup");
137: popupListener = new PopupListener(componentsSelectionPopup);
138:
139: //setLocation (300, 100);
140: //setToPreferredSize (300, 300);
141: setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
142:
143: // updateMenuItems (false);
144: }
145:
146: /**
147: * Returns the name of the selected component.
148: *
149: * @return DOCUMENT ME!
150: */
151: public String getSelectedComponentName() {
152: String result = null;
153: int actualTabIndex = tabbedPane.getSelectedIndex();
154:
155: if ((actualTabIndex == 0) && (actualVisualization != null)) {
156: TreePath[] tp = actualVisualization.getSelectionPaths();
157:
158: if (tp != null) {
159: DefaultMutableTreeNode node = (DefaultMutableTreeNode) tp[0]
160: .getLastPathComponent();
161: result = actualVisualization.getComponentPath(node);
162: }
163: } else if ((actualTabIndex == 1) && (actualTable != null)) {
164: int selectedIndex = actualTable.getSelectedRow();
165: result = (String) actualTable.getModel().getValueAt(
166: selectedIndex, 0);
167: }
168:
169: return result;
170: }
171:
172: /**
173: * Called when the component tree has changed.
174: *
175: * @param event DOCUMENT ME!
176: */
177: public void componentTreeChanged(ComponentsEvent event) {
178: addPopupMenus((Components) event.getSource());
179:
180: //update ();
181: }
182:
183: /**
184: * Called when a user defined name has been set.
185: *
186: * @param event DOCUMENT ME!
187: */
188: public void userDefinedNameSet(ComponentsEvent event) {
189: }
190:
191: /**
192: * Invoked when the session list has changed.
193: *
194: * @param event the session list event
195: */
196: public void sessionListChanged(SessionListEvent event) {
197: Session session = event.getSession();
198: Components components = session.getComponents();
199: int ID = event.getID();
200:
201: if (ID == SessionListEvent.SESSION_ADDED) {
202: if (!visualizations.containsKey(components)) {
203: ComponentsTreeVisualization tree = new ComponentsTreeVisualization(
204: env, session, components);
205: TreeSelectionModel treeSelectionModel = tree
206: .getSelectionModel();
207: tree.addMouseListener(popupListener);
208: addPopupMenus(components);
209:
210: //treeSelectionModel.setSelectionMode (TreeSelectionModel.SINGLE_TREE_SELECTION);
211: treeSelectionModel.addTreeSelectionListener(this );
212:
213: ComponentsTable table = new ComponentsTable(env,
214: session, components);
215: visualizations.put(components, tree);
216: tables.put(components, table);
217: table.addMouseListener(popupListener);
218: table.getSelectionModel()
219: .addListSelectionListener(this );
220: updateMenuItems(false);
221: }
222: } else if (ID == SessionListEvent.SESSION_REMOVED) {
223: if (visualizations.containsKey(components)) {
224: ComponentsTreeVisualization tree = (ComponentsTreeVisualization) visualizations
225: .get(components);
226: tree.removeMouseListener(popupListener);
227:
228: ComponentsTable table = (ComponentsTable) tables
229: .get(components);
230: table.removeMouseListener(popupListener);
231:
232: visualizations.remove(components);
233: tables.remove(components);
234: table.getSelectionModel().removeListSelectionListener(
235: this );
236: tree.getSelectionModel().removeTreeSelectionListener(
237: this );
238: }
239: } else if (ID == SessionListEvent.NEW_ACTUAL_SESSION) {
240: if (tabbedPane != null) {
241: tabbedPane.removeChangeListener(this );
242: }
243:
244: visualize(components, session);
245: updateMenuItems(false);
246: tabbedPane.addChangeListener(this );
247: }
248: }
249:
250: /**
251: * Adds a popup menu to all component selection panes.
252: *
253: * @param components the components containing the windows with the component selection panes.
254: */
255: private void addPopupMenus(Components components) {
256: GlassPaneManager glassPaneManager = components
257: .getGlassPaneManager();
258: GlassPaneCompound[] compounds = glassPaneManager
259: .getGlassPaneCompounds();
260:
261: for (int i = 0; i < compounds.length; i++) {
262: ComponentSelectionPane pane = (ComponentSelectionPane) compounds[i]
263: .getGlassPane(GlassPaneCompound.COMPONENT_SELECTION_PANE);
264: pane.addMouseListener(popupListener);
265: }
266: }
267:
268: /**
269: * Returns the number of the actually shown tab.
270: *
271: * @return DOCUMENT ME!
272: */
273: public int getTab() {
274: return tabbedPane.getSelectedIndex();
275: }
276:
277: /**
278: * Inserts tests for the selected elements.
279: *
280: * @param isRecursive DOCUMENT ME!
281: * @param defaultCorrect DOCUMENT ME!
282: * @param defaultIgnore DOCUMENT ME!
283: */
284: public void insertTests(boolean isRecursive,
285: boolean defaultCorrect, boolean defaultIgnore) {
286: int actualTabIndex = getTab();
287:
288: if ((actualTabIndex == TREE_TAB)
289: && (actualVisualization != null)) {
290: actualVisualization.insertTests(isRecursive,
291: defaultCorrect, defaultIgnore);
292: } else if ((actualTabIndex == TABLE_TAB)
293: && (actualTable != null)) {
294: actualTable.insertTests(defaultCorrect, defaultIgnore);
295: }
296:
297: /*ComponentSelection selection = actualComponents.getComponentSelection ();
298:
299: Vector selectedComponents = new Vector (Arrays.asList (selection.getComponents()));
300: Session session = cleverPHL.getSessionList().getActual ();
301: Iterator rootIterator = actualComponents.getIncludedRoots().iterator();
302: Vector tests = new Vector (5,5);
303: while (rootIterator.hasNext()) {
304: insertTestsForSelectedComponents ((Component) rootIterator.next(), session.getTestLoader(), selectedComponents, isRecursive, defaultCorrect, defaultIgnore, tests);
305: }
306:
307: Iterator it = tests.iterator ();
308: while (it.hasNext()) {
309: session.insertRecordable ((Test) it.next());
310: }
311: if (! tests.isEmpty()) {
312: session.restructureRecord ();
313: } */
314: }
315:
316: private void insertTestsForSelectedComponents(Component root,
317: TestLoader testLoader, Vector selectedComponents,
318: boolean isRecursive, boolean defaultCorrect,
319: boolean defaultIgnore, Vector result) {
320: if (selectedComponents.contains(root)) {
321: if (isRecursive) {
322: Test tmpTest = getRecursiveTest(root, testLoader,
323: defaultCorrect, defaultIgnore);
324:
325: if (tmpTest != null) {
326: result.add(tmpTest);
327:
328: return;
329: }
330: } else {
331: Test tmpTest = testLoader.getTest(root);
332:
333: if (tmpTest != null) {
334: tmpTest.setCorrecting(defaultCorrect);
335: tmpTest.setIgnoring(defaultIgnore);
336: result.add(tmpTest);
337: }
338: }
339: }
340:
341: ComponentHandler compHandler = actualComponents
342: .getFirstComponentHandler(root);
343: Component[] children = compHandler.getChildren(root);
344:
345: for (int i = 0; i < children.length; i++) {
346: if (selectedComponents.contains(root) && isRecursive) {
347: // parent has not been inserted because there is no test for the parent. So the children must be added to the
348: // selected components
349: selectedComponents.add(children[i]);
350: }
351:
352: insertTestsForSelectedComponents(children[i], testLoader,
353: selectedComponents, isRecursive, defaultCorrect,
354: defaultIgnore, result);
355: }
356: }
357:
358: private Test getRecursiveTest(Component root,
359: TestLoader testLoader, boolean defaultCorrect,
360: boolean defaultIgnore) {
361: Test result = testLoader.getTest(root);
362:
363: if (result == null) {
364: return null;
365: }
366:
367: result.setCorrecting(defaultCorrect);
368: result.setIgnoring(defaultIgnore);
369:
370: ComponentHandler compHandler = actualComponents
371: .getFirstComponentHandler(root);
372: Vector children = new Vector(Arrays.asList(compHandler
373: .getChildren(root)));
374:
375: while (!children.isEmpty()) {
376: Component child = (Component) children.get(0);
377: Test test = getRecursiveTest(child, testLoader,
378: defaultCorrect, defaultIgnore);
379:
380: if (test != null) {
381: result.addChild(test);
382: } else {
383: ComponentHandler childCompHandler = actualComponents
384: .getFirstComponentHandler(child);
385: Component[] grandChildren = childCompHandler
386: .getChildren(child);
387:
388: if (grandChildren.length > 0) {
389: children.addAll(Arrays.asList(grandChildren));
390: }
391: }
392:
393: children.remove(child);
394: }
395:
396: return result;
397: }
398:
399: /**
400: * Changes the components visualization
401: *
402: * @param components the components to visualize
403: * @param session DOCUMENT ME!
404: */
405: private void visualize(Components components, Session session) {
406: if (!visualizations.containsKey(components)) {
407: visualizations.put(components,
408: new ComponentsTreeVisualization(env, session,
409: components));
410: tables.put(components, new ComponentsTable(env, session,
411: components));
412: }
413:
414: if (actualVisualization != null) {
415: getContentPane().removeAll();
416: actualVisualization.removeMouseListener(popupListener);
417: actualTable.removeMouseListener(popupListener);
418: }
419:
420: actualVisualization = (ComponentsTreeVisualization) visualizations
421: .get(components);
422: actualTable = (ComponentsTable) tables.get(components);
423:
424: if (actualComponents != null) {
425: actualComponents.removeComponentsListener(this );
426: }
427:
428: actualVisualization.addMouseListener(popupListener);
429: actualTable.addMouseListener(popupListener);
430:
431: actualComponents = components;
432: components.addComponentsListener(this );
433:
434: tabbedPane = new JTabbedPane();
435: tabbedPane.addTab(language
436: .getString("CleverPHL.ComponentsFrame.TreeTab"),
437: new JScrollPane(actualVisualization));
438: tabbedPane.addTab(language
439: .getString("CleverPHL.ComponentsFrame.TableTab"),
440: new JScrollPane(actualTable));
441: getContentPane().add(tabbedPane);
442: validate();
443: repaint();
444: }
445:
446: /**
447: * Invoked when the session list has changed.
448: */
449: public void update() {
450: if (actualVisualization != null) {
451: getContentPane().removeAll();
452: actualVisualization.updateTree();
453: addPopupMenus(cleverPHL.getSessionList().getActual()
454: .getComponents());
455:
456: actualTable.update();
457: tabbedPane = new JTabbedPane();
458: tabbedPane.addTab(language
459: .getString("CleverPHL.ComponentsFrame.TreeTab"),
460: new JScrollPane(actualVisualization));
461: tabbedPane.addTab(language
462: .getString("CleverPHL.ComponentsFrame.TableTab"),
463: new JScrollPane(actualTable));
464: getContentPane().add(tabbedPane);
465: validate();
466: repaint();
467: }
468: }
469:
470: /**
471: * Does nothing.
472: *
473: * @param w DOCUMENT ME!
474: */
475: public void windowActivated(WindowEvent w) {
476: }
477:
478: /**
479: * Does nothing.
480: *
481: * @param w DOCUMENT ME!
482: */
483: public void windowClosed(WindowEvent w) {
484: }
485:
486: /**
487: * Does nothing.
488: *
489: * @param w DOCUMENT ME!
490: */
491: public void windowDeactivated(WindowEvent w) {
492: }
493:
494: /**
495: * Does nothing.
496: *
497: * @param w DOCUMENT ME!
498: */
499: public void windowDeiconified(WindowEvent w) {
500: }
501:
502: /**
503: * Does nothing.
504: *
505: * @param w DOCUMENT ME!
506: */
507: public void windowIconified(WindowEvent w) {
508: }
509:
510: /**
511: * Does nothing.
512: *
513: * @param w DOCUMENT ME!
514: */
515: public void windowOpened(WindowEvent w) {
516: }
517:
518: /**
519: * Exits the system.
520: *
521: * @param w DOCUMENT ME!
522: */
523: public void windowClosing(WindowEvent w) {
524: close();
525: }
526:
527: /**
528: * Called when the list selection has changed.
529: *
530: * @param e DOCUMENT ME!
531: */
532: public void valueChanged(ListSelectionEvent e) {
533: updateMenuItemsForTableTab();
534: }
535:
536: /**
537: * Called when the list selection has changed.
538: *
539: * @param e DOCUMENT ME!
540: */
541: public void valueChanged(TreeSelectionEvent e) {
542: updateMenuItemsForTreeTab();
543: }
544:
545: private void updateMenuItemsForTreeTab() {
546: updateMenuItems(true);
547:
548: boolean isComponentSelected = actualVisualization
549: .getSelectionCount() > 0;
550: CleverPHLMenuBar menuBar = cleverPHL.getComponentsFrame()
551: .getCleverPHLMenuBar();
552: menuBar.setMenuItemEnabled(
553: "CleverPHL.Menu.InsertTestsForSelectedComponents",
554: isComponentSelected);
555:
556: //menuBar.setMenuItemEnabled ("CleverPHL.Menu.EnableComponents", isComponentSelected);
557: //menuBar.setMenuItemEnabled ("CleverPHL.Menu.DisableComponents", isComponentSelected);
558: menuBar.setMenuItemEnabled("CleverPHL.Menu.EnableComponents",
559: true);
560: menuBar.setMenuItemEnabled("CleverPHL.Menu.DisableComponents",
561: true);
562: menuBar.setMenuItemEnabled("CleverPHL.Menu.HideComponents",
563: true);
564:
565: if (actualVisualization.getSelectionCount() != 1) {
566: cleverPHL.getComponentsFrame().getCleverPHLMenuBar()
567: .setMenuItemEnabled(
568: "CleverPHL.Menu.SetUserDefinedName", false);
569: } else {
570: TreePath[] paths = actualVisualization.getSelectionPaths();
571:
572: if (paths[0].getLastPathComponent() == actualVisualization
573: .getModel().getRoot()) {
574: menuBar.setMenuItemEnabled(
575: "CleverPHL.Menu.SetUserDefinedName", false);
576: menuBar
577: .setMenuItemEnabled(
578: "CleverPHL.Menu.InsertTestsForSelectedComponents",
579: false);
580:
581: //menuBar.setMenuItemEnabled ("CleverPHL.Menu.EnableComponents", false);
582: //menuBar.setMenuItemEnabled ("CleverPHL.Menu.DisableComponents", false);
583: menuBar.setMenuItemEnabled(
584: "CleverPHL.Menu.EnableComponents", true);
585: menuBar.setMenuItemEnabled(
586: "CleverPHL.Menu.DisableComponents", true);
587: menuBar.setMenuItemEnabled(
588: "CleverPHL.Menu.HideComponents", true);
589: }
590: }
591: }
592:
593: private void updateMenuItemsForTableTab() {
594: updateMenuItems(true);
595:
596: if (actualTable.getSelectedRowCount() != 1) {
597: cleverPHL.getComponentsFrame().getCleverPHLMenuBar()
598: .setMenuItemEnabled(
599: "CleverPHL.Menu.SetUserDefinedName", false);
600: }
601: }
602:
603: /**
604: * Called when another tab has been selected.
605: *
606: * @param event DOCUMENT ME!
607: */
608: public void stateChanged(ChangeEvent event) {
609: int tab = getTab();
610:
611: if (tab == TREE_TAB) {
612: updateMenuItemsForTreeTab();
613: } else {
614: updateMenuItemsForTableTab();
615: }
616: }
617:
618: public void updateMenuItems(boolean isSelected) {
619: cleverPHL
620: .getComponentsFrame()
621: .getCleverPHLMenuBar()
622: .setMenuItemEnabled(
623: "CleverPHL.Menu.SetUserDefinedName", isSelected);
624: cleverPHL.getComponentsFrame().getCleverPHLMenuBar()
625: .setMenuItemEnabled(
626: "CleverPHL.Menu.InsertTestIntoRecord",
627: isSelected);
628: }
629:
630: /**
631: * Closes the frame.
632: */
633: public void close() {
634: setVisible(false);
635:
636: JCheckBoxMenuItem item = ((JCheckBoxMenuItem) cleverPHL
637: .getMainFrame().getCleverPHLMenuBar().getMenuItem(
638: "CleverPHL.Menu.ViewComponentsWindow"));
639: item.setSelected(false);
640: }
641:
642: /**
643: * DOCUMENT ME!
644: *
645: * @return ComponentsTreeVisualization
646: */
647: public ComponentsTreeVisualization getComponentsTreeVisualization() {
648: return actualVisualization;
649: }
650:
651: /**
652: * DOCUMENT ME!
653: *
654: * @param visualization
655: */
656: public void setComponentsTreeVisualization(
657: ComponentsTreeVisualization visualization) {
658: actualVisualization = visualization;
659: }
660: }
|