fiore@0: /* fiore@0: CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool fiore@0: fiore@0: Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/) fiore@0: fiore@0: This program is free software: you can redistribute it and/or modify fiore@0: it under the terms of the GNU General Public License as published by fiore@0: the Free Software Foundation, either version 3 of the License, or fiore@0: (at your option) any later version. fiore@0: fiore@0: This program is distributed in the hope that it will be useful, fiore@0: but WITHOUT ANY WARRANTY; without even the implied warranty of fiore@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fiore@0: GNU General Public License for more details. fiore@0: fiore@0: You should have received a copy of the GNU General Public License fiore@0: along with this program. If not, see . fiore@0: */ fiore@0: package uk.ac.qmul.eecs.ccmi.gui; fiore@0: fiore@0: import java.awt.event.ActionEvent; fiore@0: import java.awt.event.InputEvent; fiore@0: import java.awt.event.KeyEvent; fiore@0: import java.awt.event.MouseEvent; fiore@0: import java.io.InputStream; fiore@0: import java.text.MessageFormat; fiore@0: import java.util.ArrayList; fiore@0: import java.util.List; fiore@0: import java.util.ResourceBundle; fiore@0: fiore@0: import javax.swing.AbstractAction; fiore@0: import javax.swing.JOptionPane; fiore@0: import javax.swing.JTree; fiore@0: import javax.swing.KeyStroke; fiore@0: import javax.swing.event.TreeModelEvent; fiore@0: import javax.swing.event.TreeModelListener; fiore@0: import javax.swing.tree.DefaultMutableTreeNode; fiore@0: import javax.swing.tree.TreeNode; fiore@0: import javax.swing.tree.TreePath; fiore@0: import javax.swing.tree.TreeSelectionModel; fiore@0: fiore@0: import uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramElement; fiore@3: import uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramTreeNode; fiore@0: import uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramNode; fiore@0: import uk.ac.qmul.eecs.ccmi.diagrammodel.EdgeReferenceMutableTreeNode; fiore@0: import uk.ac.qmul.eecs.ccmi.diagrammodel.NodeReferenceMutableTreeNode; fiore@0: import uk.ac.qmul.eecs.ccmi.diagrammodel.TreeModel; fiore@0: import uk.ac.qmul.eecs.ccmi.diagrammodel.TypeMutableTreeNode; fiore@3: import uk.ac.qmul.eecs.ccmi.network.Command; fiore@3: import uk.ac.qmul.eecs.ccmi.network.DiagramEventActionSource; fiore@0: import uk.ac.qmul.eecs.ccmi.sound.PlayerListener; fiore@0: import uk.ac.qmul.eecs.ccmi.sound.SoundEvent; fiore@0: import uk.ac.qmul.eecs.ccmi.sound.SoundFactory; fiore@0: import uk.ac.qmul.eecs.ccmi.speech.Narrator; fiore@0: import uk.ac.qmul.eecs.ccmi.speech.NarratorFactory; fiore@0: import uk.ac.qmul.eecs.ccmi.speech.SpeechUtilities; fiore@0: import uk.ac.qmul.eecs.ccmi.utils.InteractionLog; fiore@0: fiore@3: fiore@0: @SuppressWarnings("serial") fiore@0: public class DiagramTree extends JTree { fiore@3: /** fiore@3: * Creates a new diagram tree. The model of this tree is set to the tree model fiore@3: * held by the instance of {@code Diagram} passed as argument. The model is retrieved by a call fiore@3: * to {@code getTreeModel()} on the diagram. fiore@3: *

fiore@3: * The tree doesn't allow interaction via the mouse. It can be navigated via the keyboard using fiore@3: * the arrow keys. When a node is selected, cursoring up and down allows the user to go through fiore@3: * all the sibling of the selected node. Cursoring right will expand the selected node (if it has children) fiore@3: * and select its first child. Cursoring left will collapse a node and select its father. All the motions fiore@3: * trigger a text to speech utterance (possibly accompanied by sound) about the new selected node. fiore@3: * fiore@3: * @param diagram a reference to the diagram holding the tree model for this tree. fiore@3: */ fiore@0: public DiagramTree(Diagram diagram){ fiore@0: super(diagram.getTreeModel()); fiore@0: this.diagram = diagram; fiore@0: resources = ResourceBundle.getBundle(EditorFrame.class.getName()); fiore@0: fiore@3: TreePath rootPath = new TreePath((DiagramTreeNode)diagram.getTreeModel().getRoot()); fiore@0: setSelectionPath(rootPath); fiore@0: collapsePath(rootPath); fiore@0: selectedNodes = new ArrayList(); fiore@0: setEditable(false); fiore@0: getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); fiore@0: overwriteTreeKeystrokes(); fiore@0: /* don't use the swing focus system as we provide one on our own */ fiore@0: setFocusTraversalKeysEnabled(false); fiore@3: getAccessibleContext().setAccessibleName("tree"); fiore@0: } fiore@0: fiore@0: @SuppressWarnings("unchecked") fiore@0: @Override fiore@0: public TreeModel getModel(){ fiore@0: return (TreeModel)super.getModel(); fiore@0: } fiore@0: fiore@3: /** fiore@3: * @see javax.swing.JTree#setModel(javax.swing.tree.TreeModel) fiore@3: * fiore@5: * @param newModel the new model for this tree fiore@3: */ fiore@0: public void setModel(TreeModel newModel){ fiore@3: DiagramTreeNode selectedTreeNode = (DiagramTreeNode)getSelectionPath().getLastPathComponent(); fiore@0: super.setModel(newModel); fiore@0: collapseRow(0); fiore@0: setSelectionPath(new TreePath(selectedTreeNode.getPath())); fiore@0: } fiore@0: fiore@3: /** fiore@3: * Set a new diagram for this tree. As a result of this call the tree model fiore@3: * of this tree will be set to the model return by {@code diagram.getTreeModel()} fiore@3: * fiore@3: * @param diagram the new diagram for this tree fiore@3: */ fiore@0: public void setDiagram(Diagram diagram){ fiore@0: this.diagram = diagram; fiore@0: setModel(diagram.getTreeModel()); fiore@0: } fiore@0: fiore@3: private void selectNode(final Node n){ fiore@0: selectedNodes.add(n); fiore@0: treeModel.valueForPathChanged(new TreePath(n.getPath()),n.getName()); fiore@0: fiore@0: SoundFactory.getInstance().play(SoundEvent.OK,new PlayerListener(){ fiore@0: @Override fiore@0: public void playEnded() { fiore@0: NarratorFactory.getInstance().speak(MessageFormat.format(resources.getString("speech.node_selected"),n.spokenText())); fiore@0: } fiore@0: }); fiore@0: InteractionLog.log(INTERACTIONLOG_SOURCE,"node selected for edge",n.getName()); fiore@0: } fiore@0: fiore@3: private void unselectNode(final Node n){ fiore@0: selectedNodes.remove(n); fiore@0: treeModel.valueForPathChanged(new TreePath(n.getPath()),n.getName()); fiore@0: fiore@0: SoundFactory.getInstance().play(SoundEvent.OK,new PlayerListener(){ fiore@0: @Override fiore@0: public void playEnded() { fiore@0: NarratorFactory.getInstance().speak(MessageFormat.format(resources.getString("speech.node_unselected"),n.spokenText())); fiore@0: } fiore@0: }); fiore@0: InteractionLog.log(INTERACTIONLOG_SOURCE,"node unselected for edge",DiagramElement.toLogString(n)); fiore@0: } fiore@0: fiore@3: /** fiore@3: * Returns an array containing the references to all the nodes that have so far been selected fiore@3: * for edge creation. A new array is created each time this method is called. fiore@3: * fiore@3: * @return an array of nodes fiore@3: */ fiore@0: public DiagramNode[] getSelectedNodes(){ fiore@0: DiagramNode[] array = new DiagramNode[selectedNodes.size()]; fiore@0: return selectedNodes.toArray(array); fiore@0: } fiore@0: fiore@3: /** fiore@3: * Makes all the nodes selected for edge creation unselected. This method should fiore@3: * be called after an edge has been created, to get the user restart fiore@3: * go over the selection process again. fiore@3: * fiore@3: */ fiore@0: public void clearNodeSelections(){ fiore@0: ArrayList tempList = new ArrayList(selectedNodes); fiore@0: selectedNodes.clear(); fiore@0: for(Node n : tempList){ fiore@0: treeModel.valueForPathChanged(new TreePath(n.getPath()),n.getName()); fiore@3: diagram.getModelUpdater().yieldLock(n, Lock.MUST_EXIST, new DiagramEventActionSource(DiagramEventSource.TREE,Command.Name.INSERT_EDGE,n.getId(),n.getName())); fiore@0: } fiore@0: } fiore@0: fiore@3: /** fiore@3: * Returns a string for a text to speech synthesizer, describing the currently selected fiore@3: * tree node. The one that is at the end of the current selection path. fiore@3: * fiore@3: * @return a description string suitable for text to speech synthesis fiore@3: */ fiore@0: public String currentPathSpeech(){ fiore@0: TreePath path = getSelectionPath(); fiore@3: DiagramTreeNode selectedPathTreeNode = (DiagramTreeNode)path.getLastPathComponent(); fiore@0: if(selectedNodes.contains(selectedPathTreeNode)) fiore@0: /* add information about the fact that the node is selected */ fiore@0: return MessageFormat.format(resources.getString("speech.node_selected"), selectedPathTreeNode.spokenText()); fiore@0: else fiore@0: return selectedPathTreeNode.spokenText(); fiore@0: } fiore@0: fiore@0: /** fiore@3: * Changes the selected tree path from the current to one defined by fiore@3: * the {@code JumpTo enum} fiore@3: * fiore@3: * @see JumpTo fiore@3: * fiore@3: * @param jumpTo a {@code JumpTo enum} fiore@0: */ fiore@0: public void jump(JumpTo jumpTo){ fiore@0: final Narrator narrator = NarratorFactory.getInstance(); fiore@0: TreePath oldPath; fiore@0: switch(jumpTo){ fiore@0: case REFERENCE : fiore@0: oldPath = getSelectionPath(); fiore@3: DiagramTreeNode selectedTreeNode = (DiagramTreeNode)oldPath.getLastPathComponent(); fiore@0: if(selectedTreeNode instanceof NodeReferenceMutableTreeNode){ fiore@0: final Node n = (Node)((NodeReferenceMutableTreeNode)selectedTreeNode).getNode(); fiore@0: setSelectionPath(new TreePath(n.getPath())); fiore@3: SoundFactory.getInstance().play(SoundEvent.JUMP, new PlayerListener(){ fiore@0: @Override fiore@0: public void playEnded() { fiore@0: narrator.speak(MessageFormat.format(resources.getString("speech.jump"),n.spokenText())); fiore@0: } fiore@3: }); fiore@0: }else if(selectedTreeNode instanceof EdgeReferenceMutableTreeNode){ fiore@0: final Edge e = (Edge)((EdgeReferenceMutableTreeNode)selectedTreeNode).getEdge(); fiore@0: setSelectionPath(new TreePath(e.getPath())); fiore@3: SoundFactory.getInstance().play(SoundEvent.JUMP,new PlayerListener(){ fiore@0: @Override fiore@0: public void playEnded() { fiore@0: narrator.speak(MessageFormat.format(resources.getString("speech.jump"),e.spokenText())); fiore@0: } fiore@3: }); fiore@0: } fiore@0: /* assume the referee has only root in common with the reference and collapse everything up to the root (excluded) */ fiore@3: collapseAll(selectedTreeNode, (DiagramTreeNode)selectedTreeNode.getPath()[1]); fiore@0: break; fiore@0: case ROOT : fiore@3: final DiagramTreeNode from =(DiagramTreeNode)getSelectionPath().getLastPathComponent(); fiore@0: setSelectionRow(0); fiore@0: collapseAll(from,from.getRoot()); fiore@3: SoundFactory.getInstance().play(SoundEvent.JUMP, new PlayerListener(){ fiore@0: @Override fiore@0: public void playEnded() { fiore@0: narrator.speak(MessageFormat.format(resources.getString("speech.jump"),from.getRoot().spokenText())); fiore@0: } fiore@3: }); fiore@0: break; fiore@3: // case TYPE : // jumps to the ancestor type node of the current node, never used fiore@3: // oldPath = getSelectionPath(); fiore@3: // int index = 0; fiore@3: // Object[] pathComponents = oldPath.getPath(); fiore@3: // for(int i=0;i= 2) fiore@3: collapseAll((DiagramTreeNode)oldPath.getLastPathComponent(), (DiagramTreeNode)initialValue); fiore@3: SoundFactory.getInstance().play(SoundEvent.JUMP, new PlayerListener(){ fiore@0: @Override fiore@0: public void playEnded() { fiore@0: narrator.speak(MessageFormat.format(resources.getString("speech.jump"),selectedValue)); fiore@0: } fiore@3: }); fiore@0: break; fiore@0: case BOOKMARK : fiore@0: TreeModel treeModel = getModel(); fiore@0: fiore@0: if(treeModel.getBookmarks().size() == 0){ fiore@0: SoundFactory.getInstance().play(SoundEvent.ERROR ,new PlayerListener(){ fiore@0: @Override fiore@0: public void playEnded() { fiore@2: narrator.speak(resources.getString("speech.no_bookmarks")); fiore@0: } fiore@0: }); fiore@0: InteractionLog.log(INTERACTIONLOG_SOURCE,"no bookmarks available",""); fiore@0: return; fiore@0: } fiore@0: fiore@0: String[] bookmarkArray = new String[treeModel.getBookmarks().size()]; fiore@0: bookmarkArray = treeModel.getBookmarks().toArray(bookmarkArray); fiore@0: fiore@0: InteractionLog.log(INTERACTIONLOG_SOURCE,"open select bookmark dialog",""); fiore@0: String bookmark = (String)SpeechOptionPane.showSelectionDialog( fiore@0: JOptionPane.getFrameForComponent(this), fiore@0: "Select bookmark", fiore@0: bookmarkArray, fiore@0: bookmarkArray[0] fiore@0: ); fiore@0: fiore@0: if(bookmark != null){ fiore@0: oldPath = getSelectionPath(); fiore@3: DiagramTreeNode treeNode = treeModel.getBookmarkedTreeNode(bookmark); fiore@3: collapseAll((DiagramTreeNode)oldPath.getLastPathComponent(), (DiagramTreeNode)treeModel.getRoot()); fiore@0: setSelectionPath(new TreePath(treeNode.getPath())); fiore@0: final String currentPathSpeech = currentPathSpeech(); fiore@3: SoundFactory.getInstance().play(SoundEvent.JUMP, new PlayerListener(){ fiore@0: @Override fiore@0: public void playEnded() { fiore@0: narrator.speak(currentPathSpeech); fiore@0: } fiore@3: }); fiore@0: InteractionLog.log(INTERACTIONLOG_SOURCE,"bookmark selected",bookmark); fiore@0: }else{ fiore@0: /* it speaks anyway, as we set up the speech in the EditorFrame class. no need to use the narrator then */ fiore@0: SoundFactory.getInstance().play(SoundEvent.CANCEL); fiore@0: InteractionLog.log(INTERACTIONLOG_SOURCE,"cancel select bookmark dialog",""); fiore@0: return; fiore@0: } fiore@0: break; fiore@0: fiore@0: } fiore@3: InteractionLog.log(INTERACTIONLOG_SOURCE,"jumped to "+jumpTo.toString(),((DiagramTreeNode)getSelectionPath().getLastPathComponent()).getName()); fiore@0: } fiore@0: fiore@3: /** fiore@3: * Changes the selected tree path from the current to the one from the root fiore@3: * to the {@code Diagramelement} passed as argument. Note that a {@code Diagramelement} fiore@3: * is also an instance of {@code DuagramTreeNode} and it's placed in a {@code TreeModel} fiore@3: * when it's inserted into a {@code DiagramModel} fiore@3: * fiore@3: * @param de the diagram element to be selected on the tree fiore@3: */ fiore@0: public void jumpTo(final DiagramElement de){ fiore@0: TreePath oldPath = getSelectionPath(); fiore@3: collapseAll((DiagramTreeNode)oldPath.getLastPathComponent(),de); fiore@0: setSelectionPath(new TreePath(de.getPath())); fiore@0: SoundFactory.getInstance().play( SoundEvent.JUMP, new PlayerListener(){ fiore@0: @Override fiore@0: public void playEnded() { fiore@0: NarratorFactory.getInstance().speak(MessageFormat.format(resources.getString("speech.jump"),de.spokenText())); fiore@0: } fiore@0: }); fiore@0: } fiore@0: fiore@0: /* collapse all the nodes in the path from "from" to "to" upwards(with the same direction as going from a leaf to the root)*/ fiore@3: private void collapseAll(DiagramTreeNode from, DiagramTreeNode to){ fiore@3: DiagramTreeNode currentNode = from; fiore@0: while(currentNode.getParent() != null && currentNode != to){ fiore@0: currentNode = currentNode.getParent(); fiore@0: collapsePath(new TreePath(currentNode.getPath())); fiore@0: } fiore@0: } fiore@0: fiore@3: /** fiore@3: * Mouse events are ignored by this tree. This is just a blank method. fiore@3: * fiore@3: * @param e a mouse event fiore@3: */ fiore@0: @Override fiore@0: protected void processMouseEvent(MouseEvent e){ fiore@0: //do nothing as the tree does not have to be editable with mouse fiore@0: } fiore@0: fiore@3: /** fiore@3: * Allows only cursor keys, tab key, delete, and actions (CTRL+something) fiore@3: * fiore@3: * @param e a key event fiore@3: */ fiore@2: @Override fiore@2: protected void processKeyEvent(KeyEvent e){ fiore@2: /* allow only cursor keys, tab key, delete, and actions (CTRL+something) */ fiore@2: if(e.getKeyChar() == KeyEvent.CHAR_UNDEFINED fiore@2: || e.getKeyCode() == KeyEvent.VK_TAB fiore@2: || e.getKeyCode() == KeyEvent.VK_SPACE fiore@2: || e.isControlDown() fiore@2: || e.isAltDown()) fiore@2: super.processKeyEvent(e); fiore@2: } fiore@2: fiore@0: private void overwriteTreeKeystrokes() { fiore@0: /* overwrite the keys. up and down arrow are overwritten so that it loops when the top and the */ fiore@0: /* bottom are reached rather than getting stuck */ fiore@0: fiore@0: /* Overwrite keystrokes up,down,left,right arrows and space, shift, ctrl */ fiore@0: getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,0),"down"); fiore@0: getActionMap().put("down", new AbstractAction(){ fiore@0: @Override fiore@0: public void actionPerformed(ActionEvent evt) { fiore@3: DiagramTreeNode treeNode = (DiagramTreeNode)getLastSelectedPathComponent(); fiore@0: /* look if we've got a sibling node after (we are not at the bottom) */ fiore@3: DiagramTreeNode nextTreeNode = treeNode.getNextSibling(); fiore@0: SoundEvent loop = null; fiore@0: if(nextTreeNode == null){ fiore@3: DiagramTreeNode parent = treeNode.getParent(); fiore@0: if(parent == null) /* root node, just stay there */ fiore@0: nextTreeNode = treeNode; fiore@0: else /* loop = go to first child of own parent */ fiore@3: nextTreeNode = (DiagramTreeNode)parent.getFirstChild(); fiore@0: loop = SoundEvent.LIST_BOTTOM_REACHED; fiore@0: } fiore@0: setSelectionPath(new TreePath(nextTreeNode.getPath())); fiore@0: final InputStream finalSound = getTreeNodeSound(nextTreeNode); fiore@0: final String currentPathSpeech = currentPathSpeech(); fiore@0: SoundFactory.getInstance().play(loop, new PlayerListener(){ fiore@0: public void playEnded() { fiore@0: SoundFactory.getInstance().play(finalSound); fiore@0: NarratorFactory.getInstance().speak(currentPathSpeech); fiore@0: } fiore@0: }); fiore@0: InteractionLog.log(INTERACTIONLOG_SOURCE,"move down",nextTreeNode.toString()); fiore@0: }}); fiore@0: fiore@0: getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_UP,0),"up"); fiore@0: getActionMap().put("up", new AbstractAction(){ fiore@0: @Override fiore@0: public void actionPerformed(ActionEvent evt) { fiore@3: DiagramTreeNode treeNode = (DiagramTreeNode)getLastSelectedPathComponent(); fiore@3: DiagramTreeNode previousTreeNode = treeNode.getPreviousSibling(); fiore@0: SoundEvent loop = null; fiore@0: if(previousTreeNode == null){ fiore@3: DiagramTreeNode parent = treeNode.getParent(); fiore@0: if(parent == null) /* root node */ fiore@0: previousTreeNode = treeNode; fiore@0: else fiore@3: previousTreeNode = (DiagramTreeNode)parent.getLastChild(); fiore@0: loop = SoundEvent.LIST_TOP_REACHED; fiore@0: } fiore@0: setSelectionPath(new TreePath(previousTreeNode.getPath())); fiore@0: final InputStream finalSound = getTreeNodeSound(previousTreeNode); fiore@0: final String currentPathSpeech = currentPathSpeech(); fiore@0: SoundFactory.getInstance().play(loop, new PlayerListener(){ fiore@0: public void playEnded() { fiore@0: SoundFactory.getInstance().play(finalSound); fiore@0: NarratorFactory.getInstance().speak(currentPathSpeech); fiore@0: } fiore@0: }); fiore@0: InteractionLog.log(INTERACTIONLOG_SOURCE,"move up",previousTreeNode.toString()); fiore@0: }}); fiore@0: fiore@0: getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,0),"right"); fiore@0: getActionMap().put("right", new AbstractAction(){ fiore@0: @Override fiore@0: public void actionPerformed(ActionEvent evt) { fiore@0: TreePath path = getSelectionPath(); fiore@3: DiagramTreeNode treeNode = (DiagramTreeNode)path.getLastPathComponent(); fiore@0: if(treeNode.isLeaf()){ fiore@0: notifyBorderReached(treeNode); fiore@0: InteractionLog.log(INTERACTIONLOG_SOURCE,"move right","border reached"); fiore@0: } fiore@0: else{ fiore@0: expandPath(path); fiore@3: setSelectionPath(new TreePath(((DiagramTreeNode)treeNode.getFirstChild()).getPath())); fiore@0: final String currentPathSpeech = currentPathSpeech(); fiore@0: SoundFactory.getInstance().play(SoundEvent.TREE_NODE_EXPAND,new PlayerListener(){ fiore@0: @Override fiore@0: public void playEnded() { fiore@0: NarratorFactory.getInstance().speak(currentPathSpeech); fiore@0: } fiore@0: }); fiore@3: InteractionLog.log(INTERACTIONLOG_SOURCE,"move right",((DiagramTreeNode)treeNode.getFirstChild()).toString()); fiore@0: } fiore@0: } fiore@0: }); fiore@0: fiore@0: getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,0),"left"); fiore@0: getActionMap().put("left", new AbstractAction(){ fiore@0: @Override fiore@0: public void actionPerformed(ActionEvent evt) { fiore@0: TreePath path = getSelectionPath(); fiore@3: DiagramTreeNode treeNode = (DiagramTreeNode)path.getLastPathComponent(); fiore@3: DiagramTreeNode parent = treeNode.getParent(); fiore@0: if(parent == null){/* root node */ fiore@0: notifyBorderReached(treeNode); fiore@0: InteractionLog.log(INTERACTIONLOG_SOURCE,"move left","border reached"); fiore@0: } fiore@0: else{ fiore@3: TreePath newPath = new TreePath(((DiagramTreeNode)parent).getPath()); fiore@0: setSelectionPath(newPath); fiore@0: collapsePath(newPath); fiore@0: final String currentPathSpeech = currentPathSpeech(); fiore@0: SoundFactory.getInstance().play(SoundEvent.TREE_NODE_COLLAPSE,new PlayerListener(){ fiore@0: @Override fiore@0: public void playEnded() { fiore@0: NarratorFactory.getInstance().speak(currentPathSpeech); fiore@0: } fiore@0: }); fiore@3: InteractionLog.log(INTERACTIONLOG_SOURCE,"move left",((DiagramTreeNode)parent).toString()); fiore@0: } fiore@0: } fiore@0: }); fiore@0: fiore@0: getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE,0),"space"); fiore@0: getActionMap().put("space",new AbstractAction(){ fiore@0: @Override fiore@0: public void actionPerformed(ActionEvent arg0) { fiore@0: NarratorFactory.getInstance().speak(currentPathSpeech()); fiore@0: InteractionLog.log(INTERACTIONLOG_SOURCE,"info requested",""); fiore@0: } fiore@0: }); fiore@0: fiore@0: getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE,InputEvent.CTRL_DOWN_MASK),"ctrlspace"); fiore@0: getActionMap().put("ctrlspace",new AbstractAction(){ fiore@0: @Override fiore@0: public void actionPerformed(ActionEvent arg0) { fiore@0: /*//this code snippet reads out the whole path from the root to the selected node fiore@0: * StringBuilder builder = new StringBuilder(); fiore@0: * TreePath path = getSelectionPath(); fiore@0: * for(Object o : path.getPath()){ fiore@0: * builder.append(((DiagramModelTreeNode)o).spokenText()); fiore@0: * builder.append(", "); fiore@0: * } fiore@0: * Narrator.getInstance().speak(builder.toString(), null); fiore@0: */ fiore@0: TreePath path = getSelectionPath(); fiore@3: DiagramTreeNode treeNode = (DiagramTreeNode)path.getLastPathComponent(); fiore@0: NarratorFactory.getInstance().speak(treeNode.detailedSpokenText()); fiore@0: InteractionLog.log(INTERACTIONLOG_SOURCE,"detailed info requested",""); fiore@0: } fiore@0: }); fiore@0: fiore@0: getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_SHIFT,InputEvent.SHIFT_DOWN_MASK),"shift"); fiore@0: getActionMap().put("shift",new AbstractAction(){ fiore@0: @Override fiore@0: public void actionPerformed(ActionEvent evt) { fiore@0: if(getSelectionPath().getLastPathComponent() instanceof Node){ fiore@0: Node node = (Node)getSelectionPath().getLastPathComponent(); fiore@0: if(selectedNodes.contains(node)){ fiore@0: unselectNode(node); fiore@3: diagram.getModelUpdater().yieldLock(node, Lock.MUST_EXIST,new DiagramEventActionSource(DiagramEventSource.TREE,Command.Name.UNSELECT_NODE_FOR_EDGE_CREATION,node.getId(),node.getName())); fiore@3: }else{ fiore@3: if(!diagram.getModelUpdater().getLock(node, Lock.MUST_EXIST,new DiagramEventActionSource(DiagramEventSource.TREE,Command.Name.SELECT_NODE_FOR_EDGE_CREATION,node.getId(),node.getName()))){ fiore@0: InteractionLog.log(INTERACTIONLOG_SOURCE,"Could not get lock on node fro edge creation selection",DiagramElement.toLogString(node)); fiore@0: SpeechOptionPane.showMessageDialog( fiore@0: SpeechOptionPane.getFrameForComponent(DiagramTree.this), fiore@0: resources.getString("dialog.lock_failure.must_exist"), fiore@0: SpeechOptionPane.INFORMATION_MESSAGE); fiore@0: SoundFactory.getInstance().play(SoundEvent.MESSAGE_OK, new PlayerListener(){ fiore@0: @Override fiore@0: public void playEnded() { fiore@0: NarratorFactory.getInstance().speak(currentPathSpeech()); fiore@0: } fiore@0: }); fiore@0: return; fiore@0: } fiore@0: selectNode(node); fiore@0: } fiore@0: } fiore@0: } fiore@0: }); fiore@0: fiore@0: getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_CONTROL,InputEvent.CTRL_DOWN_MASK),"ctrldown"); fiore@0: getActionMap().put("ctrldown",SpeechUtilities.getShutUpAction()); fiore@0: fiore@0: /* make the tree ignore the page up and page down keys */ fiore@0: getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP,0),"none"); fiore@0: getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN,0),"none"); fiore@0: } fiore@0: fiore@3: private static InputStream getTreeNodeSound(DiagramTreeNode node){ fiore@0: InputStream sound = null; fiore@0: TreeNode[] newPath = node.getPath(); fiore@0: if(!node.isRoot()){ fiore@0: if(node.getChildCount() > 0){ // check whether it's the folder containing Node/Edge references fiore@0: if(node.getChildAt(0) instanceof EdgeReferenceMutableTreeNode){ fiore@0: sound = ((EdgeReferenceMutableTreeNode)node.getChildAt(0)).getEdge().getSound(); fiore@0: }else{ fiore@0: sound = ((TypeMutableTreeNode)newPath[1]).getPrototype().getSound(); fiore@0: } fiore@0: }else{ fiore@0: if(node instanceof NodeReferenceMutableTreeNode){ fiore@0: sound = ((NodeReferenceMutableTreeNode)node).getNode().getSound(); fiore@0: }else if(node instanceof EdgeReferenceMutableTreeNode){ fiore@0: sound = ((EdgeReferenceMutableTreeNode)node).getNode().getSound(); fiore@0: }else{ fiore@0: sound = ((TypeMutableTreeNode)newPath[1]).getPrototype().getSound(); fiore@0: } fiore@0: } fiore@0: } fiore@0: return sound; fiore@0: } fiore@0: fiore@0: @Override fiore@0: public void setSelectionPath(TreePath path){ fiore@0: super.setSelectionPath(path); fiore@0: scrollPathToVisible(path); fiore@0: } fiore@0: fiore@3: private void notifyBorderReached(DiagramTreeNode n) { fiore@0: SoundFactory.getInstance().play(SoundEvent.ERROR); fiore@0: } fiore@0: fiore@0: @Override fiore@0: public String convertValueToText(Object value, fiore@0: boolean selected, fiore@0: boolean expanded, fiore@0: boolean leaf, fiore@0: int row, fiore@0: boolean hasFocus){ fiore@0: StringBuilder builder = new StringBuilder(super.convertValueToText(value, selected, expanded, leaf, row, hasFocus)); fiore@0: if(selectedNodes != null) fiore@0: if(selectedNodes.contains(value)){ fiore@0: builder.insert(0, SELECTED_NODE_MARK_BEGIN); fiore@0: builder.append(SELECTED_NODE_MARK_END); fiore@0: } fiore@0: return builder.toString(); fiore@0: } fiore@0: fiore@0: @Override fiore@0: protected TreeModelListener createTreeModelListener(){ fiore@0: return new DiagramTreeModelHandler(); fiore@0: } fiore@0: fiore@0: fiore@0: private List selectedNodes; fiore@0: private Diagram diagram; fiore@0: private ResourceBundle resources; fiore@0: private static final char SELECTED_NODE_MARK_BEGIN = '<'; fiore@0: private static final char SELECTED_NODE_MARK_END = '>'; fiore@0: private static final String INTERACTIONLOG_SOURCE = "TREE"; fiore@3: /** fiore@3: * A list of possible destination for a jump (a change of the selected path without fiore@3: * using the navigation arrow keys) fiore@3: */ fiore@3: public static enum JumpTo { fiore@3: /** fiore@3: * if the current selection is a edge/node reference tree node, the jump destination fiore@3: * is the referee tree node (see {@link uk.ac.qmul.eecs.ccmi.diagrammodel.NodeReferenceMutableTreeNode} and fiore@3: * {@link uk.ac.qmul.eecs.ccmi.diagrammodel.EdgeReferenceMutableTreeNode }) fiore@3: */ fiore@3: REFERENCE, fiore@3: /** fiore@3: * the destination is the root of the diagram fiore@3: */ fiore@3: ROOT, fiore@3: /** fiore@3: * the destination will be a node or edge type selected fiore@3: * (via a selection dialog) by the user fiore@3: */ fiore@3: SELECTED_TYPE, fiore@3: /** fiore@3: * the destination will be a bookmark selected (via a selection dialog) by the user fiore@3: */ fiore@3: BOOKMARK} fiore@0: fiore@0: /* the methods of the TreeModelHandler are overwritten in order to provide a consistent way fiore@0: * of updating the tree selection upon tree change. Bear in mind that the tree can possibly be changed fiore@0: * by another peer on a network, and therefore not only as a response to a user's action. fiore@0: * The algorithm works as follows (being A the tree node selected before any handler method M being called): fiore@0: * fiore@0: * if A ain't deleted as a result of M : do nothing fiore@0: * if A's deleted as a result of M's execution : say A was the n-th sibling select the new n-th sibling fiore@0: * or, if now the sibling nodes are less than n, select the one with highest index fiore@0: * if no sibling nodes are still connected to the tree select the parent or the closest ancestor connected to the tree fiore@0: */ fiore@0: private class DiagramTreeModelHandler extends JTree.TreeModelHandler{ fiore@0: fiore@0: @Override fiore@0: public void treeStructureChanged(final TreeModelEvent e) { fiore@0: /* check first if what we're removing is in the selection path */ fiore@0: TreePath path = e.getTreePath(); fiore@0: boolean isInSelectionPath = false; fiore@0: for(Object t : getSelectionPath().getPath()){ fiore@0: if(path.getLastPathComponent() == t){ fiore@0: isInSelectionPath = true; fiore@0: break; fiore@0: } fiore@0: } fiore@0: fiore@0: if(isInSelectionPath){ fiore@0: Object[] pathArray = getSelectionPath().getPath(); fiore@0: DefaultMutableTreeNode root = (DefaultMutableTreeNode)getModel().getRoot(); fiore@0: /* go along the path from the selected node to the root looking for a node * fiore@0: * attached to the tree or with sibling nodes attached to the tree */ fiore@0: for(int i=pathArray.length-1;i>=0;i--){ fiore@3: DiagramTreeNode onPathTreeNode = (DiagramTreeNode)pathArray[i]; fiore@0: if(onPathTreeNode.isNodeRelated(root)){// if can reach the root from here a.k.a. the node is still part of the tree fiore@0: super.treeStructureChanged(e); fiore@0: setSelectionPath(new TreePath(onPathTreeNode.getPath())); fiore@0: break; fiore@0: }else{ fiore@0: /* check sibling nodes*/ fiore@3: DefaultMutableTreeNode parent = (DiagramTreeNode)pathArray[i-1]; fiore@0: if(parent.isNodeRelated(root) && parent.getChildCount() > 0){ fiore@0: super.treeStructureChanged(e); fiore@0: setSelectionPath(new TreePath(((DefaultMutableTreeNode)parent.getLastChild()).getPath())); fiore@0: break; fiore@0: } fiore@0: } fiore@0: } fiore@0: }else fiore@0: super.treeStructureChanged(e); fiore@0: repaint(); fiore@0: } fiore@0: fiore@0: @Override fiore@0: public void treeNodesChanged(final TreeModelEvent e){ fiore@0: TreePath path = getSelectionPath(); fiore@0: super.treeNodesChanged(e); fiore@0: setSelectionPath(path); fiore@0: } fiore@0: fiore@0: @Override fiore@0: public void treeNodesRemoved(final TreeModelEvent e){ fiore@0: /* check first if what we're removing is in the selecton path */ fiore@0: TreePath path = e.getTreePath(); fiore@3: DiagramTreeNode removedTreeNode = (DiagramTreeNode)e.getChildren()[0]; fiore@0: boolean isInSelectionPath = false; fiore@0: for(Object t : getSelectionPath().getPath()){ fiore@0: if(removedTreeNode == t){ fiore@0: isInSelectionPath = true; fiore@0: break; fiore@0: } fiore@0: } fiore@3: DiagramTreeNode parentTreeNode = (DiagramTreeNode)path.getLastPathComponent(); fiore@0: /* update the selection only if the tree node involved is in the selection path * fiore@0: * this always holds true for tree nodes deleted from the tree */ fiore@0: if(isInSelectionPath){ fiore@0: if(e.getSource() instanceof TreeModel){ fiore@0: /* update the path only if the node has been removed from the tree or * fiore@0: * if the currently selected tree node is going to be removed by this action * fiore@0: * Need to call collapsePath only if the source of the deletion is the tree * fiore@0: * as otherwise the selected node is always a leaf */ fiore@0: collapsePath(path); fiore@0: setSelectionPath(path); fiore@0: }else{ fiore@0: /* if we deleted from another source, then select the first non null node in the path * fiore@0: * including the deleted node. E.g. if we're deleting the first child of a parent * fiore@0: * and the node has siblings than the new first sibling will be selected */ fiore@0: int limitForParentDeletion = (parentTreeNode instanceof Edge) ? 1 : 0; // an edge with one node is to be deleted fiore@0: if(parentTreeNode.getChildCount() > limitForParentDeletion){ fiore@3: setSelectionPath(new TreePath(((DiagramTreeNode)parentTreeNode.getChildAt( fiore@0: /* select the n-th sibling node (see algorithm description above or the highest index sibling node */ fiore@0: Math.min(e.getChildIndices()[0],parentTreeNode.getChildCount()-1) fiore@0: )).getPath())); fiore@0: }else{ fiore@0: /* the deleted node had no siblings, thus select the node checking from the parent up in the path to the first still existing node */ fiore@0: Object[] pathArray = path.getPath(); fiore@0: for(int i=path.getPathCount()-1;i>=0;i--){ fiore@3: DiagramTreeNode itr = (DiagramTreeNode)pathArray[i]; fiore@0: if(itr.getPath()[0] == getModel().getRoot()){ fiore@0: TreePath newPath = new TreePath(itr.getPath()); fiore@0: setSelectionPath(newPath); fiore@0: collapsePath(newPath); fiore@0: break; fiore@0: } fiore@0: } fiore@0: } fiore@0: } fiore@0: }else fiore@0: super.treeNodesRemoved(e); fiore@0: fiore@0: /* if the node was selected for edge creation, then remove it from the list */ fiore@3: DiagramTreeNode removedNode = (DiagramTreeNode)e.getChildren()[0]; fiore@0: selectedNodes.remove(removedNode); fiore@0: } fiore@0: } fiore@0: }