annotate java/src/uk/ac/qmul/eecs/ccmi/diagrammodel/DiagramEdge.java @ 8:ea7885bd9bff tip

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