annotate java/src/uk/ac/qmul/eecs/ccmi/diagrammodel/DiagramEdge.java @ 1:e3935c01cde2 tip

moved license of PdPersistenceManager to the beginning of the file
author Fiore Martin <f.martin@qmul.ac.uk>
date Tue, 08 Jul 2014 19:52:03 +0100
parents 78b7fc5391a2
children
rev   line source
f@0 1 /*
f@0 2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
f@0 3
f@0 4 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
f@0 5
f@0 6 This program is free software: you can redistribute it and/or modify
f@0 7 it under the terms of the GNU General Public License as published by
f@0 8 the Free Software Foundation, either version 3 of the License, or
f@0 9 (at your option) any later version.
f@0 10
f@0 11 This program is distributed in the hope that it will be useful,
f@0 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
f@0 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f@0 14 GNU General Public License for more details.
f@0 15
f@0 16 You should have received a copy of the GNU General Public License
f@0 17 along with this program. If not, see <http://www.gnu.org/licenses/>.
f@0 18 */
f@0 19 package uk.ac.qmul.eecs.ccmi.diagrammodel;
f@0 20
f@0 21 import java.util.LinkedHashMap;
f@0 22 import java.util.List;
f@0 23 import java.util.Map;
f@0 24
f@0 25 /**
f@0 26 * This class represent an edge in the diagram model. Note that this class is
f@0 27 * a tree node.
f@0 28 *
f@0 29 */
f@0 30 @SuppressWarnings("serial")
f@0 31 public abstract class DiagramEdge extends DiagramElement {
f@0 32 /**
f@0 33 *
f@0 34 * @param type the type of the edge
f@0 35 * @param availableEndDescriptions the end descriptions this edge allows
f@0 36 */
f@0 37 public DiagramEdge(String type, String[] availableEndDescriptions){
f@0 38 setType(type);
f@0 39 this.availableEndDescriptions = availableEndDescriptions;
f@0 40 endLabels = new LinkedHashMap<DiagramNode,String>();
f@0 41 endDescriptions = new LinkedHashMap<DiagramNode,Integer>();
f@0 42 }
f@0 43
f@0 44 /**
f@0 45 * Returns a more detailed description of the edge than {@link #spokenText()}.
f@0 46 * the description includes which nodes this edge is connecting
f@0 47 *
f@0 48 * @return a description of the edge
f@0 49 */
f@0 50 @Override
f@0 51 public String detailedSpokenText(){
f@0 52 final String and = " and ";
f@0 53 StringBuilder b = new StringBuilder(getType());
f@0 54 b.append(' ').append(spokenText());
f@0 55 b.append(". Connecting ");
f@0 56 for(int i=0; i<getChildCount();i++){
f@0 57 b.append(((DiagramTreeNode)getChildAt(i)).getName());
f@0 58 b.append(and);
f@0 59 }
f@0 60 // remove the last " and "
f@0 61 b.delete(b.length()-and.length(), b.length());
f@0 62 return b.toString();
f@0 63 }
f@0 64
f@0 65 /**
f@0 66 * Set a label related to a node. On a graphical representation of the diagram
f@0 67 * the label would be put in proximity of the node.
f@0 68 *
f@0 69 * @param n the node,at whose end the label is located
f@0 70 * @param label the label
f@0 71 * @param source the source of the action that triggered this method
f@0 72 */
f@0 73 public void setEndLabel(DiagramNode n, String label, Object source){
f@0 74 if(label == null)
f@0 75 label = "";
f@0 76 endLabels.put(n, label);
f@0 77 notifyChange(new ElementChangedEvent(this,n,"endLabel",source));
f@0 78 }
f@0 79
f@0 80 /**
f@0 81 * Returns the end label related to a node. On a graphical representation of the diagram
f@0 82 * the label would be put in proximity of the node.
f@0 83 *
f@0 84 * @param n the node, at whose end the label is located
f@0 85 *
f@0 86 * @return the label at the specified end
f@0 87 */
f@0 88 public String getEndLabel(DiagramNode n){
f@0 89 String s = endLabels.get(n);
f@0 90 if(s == null)
f@0 91 return "";
f@0 92 return s;
f@0 93 }
f@0 94
f@0 95 /**
f@0 96 * Returns an array with all the available end description this edge can be
f@0 97 * assigned.
f@0 98 * @return an array of string available end description
f@0 99 */
f@0 100 public String[] getAvailableEndDescriptions(){
f@0 101 return this.availableEndDescriptions;
f@0 102 }
f@0 103
f@0 104 /**
f@0 105 * Set a string describing the end related to a node. On a visual diagram this
f@0 106 * corresponds to an arrow.
f@0 107 *
f@0 108 * @param n the node at the edge end whose description will be set
f@0 109 * @param index an index of the array returned by getAvailableEndDescriptions(). The
f@0 110 * edge end description will be set with the string at that position in the array.
f@0 111 * if index is equal to NO_END_DESCRIPTION_INDEX, then the description will be set
f@0 112 * as the empty string.
f@0 113 * @param source the source of the action that triggered this method
f@0 114 *
f@0 115 */
f@0 116 public void setEndDescription(DiagramNode n, int index, Object source){
f@0 117 endDescriptions.put(n, index);
f@0 118 notifyChange(new ElementChangedEvent(this,n,"arrowHead",source));
f@0 119 }
f@0 120
f@0 121 /**
f@0 122 * Returns a string describing the end related to a node. On a visual diagram this
f@0 123 * corresponds to an arrow.
f@0 124 *
f@0 125 * @param n the node at the edge end whose description will be returned
f@0 126 * @return a description string
f@0 127 */
f@0 128 public String getEndDescription(DiagramNode n){
f@0 129 Integer index = endDescriptions.get(n);
f@0 130 if(index == null || index == NO_END_DESCRIPTION_INDEX)
f@0 131 return "";
f@0 132 return availableEndDescriptions[endDescriptions.get(n)];
f@0 133 }
f@0 134
f@0 135 /**
f@0 136 * Returns the connected node at the specified index
f@0 137 * @param index an index into node's list
f@0 138 * @return the connected node at the specified index
f@0 139 */
f@0 140 public abstract DiagramNode getNodeAt(int index);
f@0 141
f@0 142 /**
f@0 143 * Returns the number of nodes this edge is connecting
f@0 144 * @return the number of nodes this edge is connecting
f@0 145 */
f@0 146 public abstract int getNodesNum();
f@0 147
f@0 148 /**
f@0 149 * Removes a node from this edge. On a graphical representation of the diagram
f@0 150 * this would mean that the node is no longer connected to the other nodes via this edge.
f@0 151 * @param n the node to be removed
f@0 152 * @param source the source of this action
f@0 153 * @return true if the inner collection changed as a result of the call
f@0 154 */
f@0 155 public abstract boolean removeNode(DiagramNode n, Object source);
f@0 156
f@0 157 /**
f@0 158 * Connect a list of nodes with this edge
f@0 159 * @param nodes a list of nodes to connect
f@0 160 * @throws ConnectNodesException if the number of nodes in the list is different
f@0 161 * from the number allowed by this edge.
f@0 162 */
f@0 163 public abstract void connect(List<DiagramNode> nodes) throws ConnectNodesException;
f@0 164
f@0 165 /**
f@0 166 * An index to be passed to {@link #setEndDescription(DiagramNode, int, Object)} in order
f@0 167 * to set the edge with no description at the end related to that diagram node
f@0 168 */
f@0 169 public static int NO_END_DESCRIPTION_INDEX = -1;
f@0 170 private Map<DiagramNode,String> endLabels;
f@0 171 private Map<DiagramNode,Integer> endDescriptions;
f@0 172 private String[] availableEndDescriptions;
f@0 173 }