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.session;
025:
026: import jacareto.replay.Replay;
027: import jacareto.replay.event.ReplayEvent;
028: import jacareto.replay.event.ReplayListener;
029: import jacareto.struct.Structure;
030: import jacareto.struct.StructureElement;
031: import jacareto.struct.StructureTree;
032: import jacareto.system.Environment;
033: import jacareto.system.EnvironmentMember;
034: import jacareto.toolkit.HashtableChangeEvent;
035: import jacareto.toolkit.HashtableChangeListener;
036:
037: import java.awt.event.KeyEvent;
038: import java.awt.event.KeyListener;
039:
040: import javax.swing.tree.TreePath;
041:
042: /**
043: * A listener which will be notified of events on the session's structure tree or events which
044: * should cause changed on the structure tree.
045: *
046: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
047: * @version 1.0
048: */
049: public class StructureTreeListener extends EnvironmentMember implements
050: KeyListener, ReplayListener, HashtableChangeListener {
051: /** The session. */
052: private Session session;
053:
054: /** Whether or not the actual position should be shown in the tree during replay. */
055: private boolean showReplayPosition;
056:
057: /**
058: * Creates a new listener instance
059: *
060: * @param env the env object
061: * @param session the session
062: */
063: public StructureTreeListener(Environment env, Session session) {
064: super (env);
065: this .session = session;
066: customization.addHashtableChangeListener(this );
067: applyCustomization();
068: }
069:
070: /**
071: * Called when a key has been pressed on the structure tree.
072: *
073: * @param event DOCUMENT ME!
074: */
075: public void keyPressed(KeyEvent event) {
076: if (event.getKeyCode() == KeyEvent.VK_DELETE) {
077: session.removeSelectedRecordables();
078: }
079: }
080:
081: /**
082: * Does nothing.
083: *
084: * @param event DOCUMENT ME!
085: */
086: public void keyReleased(KeyEvent event) {
087: }
088:
089: /**
090: * Does nothing.
091: *
092: * @param event DOCUMENT ME!
093: */
094: public void keyTyped(KeyEvent event) {
095: }
096:
097: /**
098: * Called when replay state has changed
099: *
100: * @param event DOCUMENT ME!
101: */
102: public void replayStateChanged(ReplayEvent event) {
103: StructureTree tree = session.getStructureTree();
104:
105: if (showReplayPosition && (tree != null)) {
106: int ID = event.getID();
107: Replay replay = event.getReplay();
108: StructureElement node = replay.getActualElement();
109:
110: if (node != null) {
111: if (node != null) {
112: if (ID == ReplayEvent.ACTUAL_ELEMENT_CHANGED) {
113: tree.clearSelection();
114: scrollToAndSelect(tree, node);
115: } else if (ID == ReplayEvent.STARTED) {
116: tree.clearSelection();
117: tree.treeDidChange();
118: } else if (ID == ReplayEvent.STOPPED) {
119: scrollToAndSelect(tree, node);
120: } else if (ID == ReplayEvent.PAUSED) {
121: scrollToAndSelect(tree, node);
122: } else if (ID == ReplayEvent.CONTINUED) {
123: tree.clearSelection();
124: scrollToAndSelect(tree, node);
125: }
126: }
127: }
128: }
129: }
130:
131: /**
132: * Scrolls the structure tree to the tree element containing the given node and selects it. If
133: * the element is in the visible area of the tree, the visualization will not be scrolled. If
134: * it is not in the visible area, the tree element will be scrolled to the first position of
135: * the visible area (Buggy; does not work at the moment!). If the tree element is not visible,
136: * the first parent which is visible will be marked.
137: *
138: * @param tree the tree
139: * @param node the node to be selected
140: */
141: private void scrollToAndSelect(StructureTree tree,
142: StructureElement node) {
143: System.out.println("scrollToAndSelect");
144:
145: StructureElement tmp = node;
146: StructureElement rootNode = (StructureElement) tree.getModel()
147: .getRoot();
148: Structure structure = node.getStructure();
149:
150: while (tmp != rootNode) {
151: StructureElement parent = (StructureElement) tmp
152: .getParent();
153:
154: if (parent != null) {
155: if (tree.isExpanded(structure.getTreePath(parent))) {
156: break;
157: } else {
158: tmp = parent;
159: }
160: } else {
161: tmp = rootNode;
162: }
163: }
164:
165: TreePath path = structure.getTreePath(tmp);
166:
167: if (!tree.isPathSelected(path)) {
168: tree.setSelectionPath(path);
169: tree.scrollPathToVisible(path);
170: }
171: }
172:
173: /**
174: * Invoked when the customization has changed.
175: *
176: * @param event the hashtable change event
177: */
178: public void hashtableChanged(HashtableChangeEvent event) {
179: applyCustomization();
180: }
181:
182: /**
183: * Applies the customization.
184: */
185: private void applyCustomization() {
186: showReplayPosition = customization.getBoolean(
187: "Replay.DisplayActualPosition", false);
188: }
189: }
|