annotate java/src/uk/ac/qmul/eecs/ccmi/diagrammodel/DiagramTreeNode.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.ArrayList;
f@0 22 import java.util.Collections;
f@0 23 import java.util.List;
f@0 24
f@0 25 import javax.swing.tree.DefaultMutableTreeNode;
f@0 26
f@0 27 /**
f@0 28 * This class represent a general node in a TreeModel
f@0 29 *
f@0 30 */
f@0 31 @SuppressWarnings("serial")
f@0 32 public abstract class DiagramTreeNode extends DefaultMutableTreeNode {
f@0 33 /**
f@0 34 * Creates a tree node with the default user object. The default user object has no label. Therefore
f@0 35 * this node will have no label when displayed on a tree.
f@0 36 */
f@0 37 public DiagramTreeNode() {
f@0 38 super();
f@0 39 notes = "";
f@0 40 userObject = new UserObject();
f@0 41 setSuperClassUserObject(userObject);
f@0 42 bookmarkKeys = new ArrayList<String>();
f@0 43 }
f@0 44
f@0 45 /**
f@0 46 * Creates a tree node, holding the user object passed as argument. The label of the
f@0 47 * tree node will be the string returned by {@code userObject.toString()}
f@0 48 *
f@0 49 * @param userObject the user object for this tree node
f@0 50 *
f@0 51 * @see javax.swing.tree.DefaultMutableTreeNode
f@0 52 */
f@0 53 public DiagramTreeNode(Object userObject) {
f@0 54 this();
f@0 55 setUserObject(userObject);
f@0 56 }
f@0 57
f@0 58 /**
f@0 59 * Each DiagramModelTreeNode keeps track of the bookmarks it has been assigned. Bookmarks
f@0 60 * will affect how this tree node will be represented on a JTree: when a tree node is bookmarked
f@0 61 * an apex appears at the right of its name.
f@0 62 *
f@0 63 * @param key the bookmark
f@0 64 * @return true if this bookmark inner collection changed as a result of the call
f@0 65 */
f@0 66 boolean addBookmarkKey(String key){
f@0 67 return bookmarkKeys.add(key);
f@0 68 }
f@0 69
f@0 70 /**
f@0 71 * Removes a bookmark key from the inner collection.
f@0 72 *
f@0 73 * @param key the key to remove
f@0 74 * @return true if this bookmark inner collection changed as a result of the call
f@0 75 */
f@0 76 boolean removeBookmarkKey(String key){
f@0 77 return bookmarkKeys.remove(key);
f@0 78 }
f@0 79
f@0 80 /**
f@0 81 * Returns the the bookmark keys currently associated to this tree node in the tree model.
f@0 82 *
f@0 83 * @return a n unmodifiable list of strings used as keys for bookmarks
f@0 84 */
f@0 85 public List<String> getBookmarkKeys(){
f@0 86 return Collections.unmodifiableList(bookmarkKeys);
f@0 87 }
f@0 88
f@0 89 public String getNotes(){
f@0 90 return notes;
f@0 91 }
f@0 92
f@0 93 /**
f@0 94 * Set a note for this tree node. A Note is a text the user wants to attach to a tree node. Notes
f@0 95 * will affect how this tree node will be represented on a JTree: when a tree node is assigned a note
f@0 96 * a number sign (#) appears at the right of its name.
f@0 97 *
f@0 98 * @param note the text of the note
f@0 99 * @param source used by {@code DiagramElement} to trigger {@code ElementChangeEvents}
f@0 100 *
f@0 101 * @see DiagramElement#setNotes(String, Object)
f@0 102 */
f@0 103 protected void setNotes(String note, Object source){
f@0 104 this.notes = note;
f@0 105 }
f@0 106
f@0 107 @Override
f@0 108 public DiagramTreeNode getParent(){
f@0 109 return (DiagramTreeNode)super.getParent();
f@0 110 }
f@0 111
f@0 112 @Override
f@0 113 public DiagramTreeNode getChildAt(int i){
f@0 114 return (DiagramTreeNode)super.getChildAt(i);
f@0 115 }
f@0 116
f@0 117 @Override
f@0 118 public DiagramTreeNode getRoot(){
f@0 119 return (DiagramTreeNode)super.getRoot();
f@0 120 }
f@0 121
f@0 122 @Override
f@0 123 public void setUserObject(Object userObject){
f@0 124 ((UserObject)this.userObject).setObject(userObject);
f@0 125 }
f@0 126
f@0 127 @Override
f@0 128 public Object getUserObject(){
f@0 129 return userObject;
f@0 130 }
f@0 131
f@0 132 /**
f@0 133 * Return a String representing this object for this tree node in a way more suitable
f@0 134 * for a text to speech synthesizer to read, than toString().
f@0 135 *
f@0 136 * @return a String suitable for text to speech synthesis
f@0 137 */
f@0 138 public String spokenText(){
f@0 139 return ((UserObject)userObject).spokenText();
f@0 140 }
f@0 141
f@0 142 /**
f@0 143 * Returns a more detailed description of the tree node than {@link #spokenText()}.
f@0 144 *
f@0 145 * @return a description of the tree node
f@0 146 */
f@0 147 public String detailedSpokenText(){
f@0 148 return spokenText();
f@0 149 }
f@0 150
f@0 151 /**
f@0 152 * returns the tree node name "as it is", without any decoration such as notes, bookmarks or cardinality;
f@0 153 * unlike the String returned by toString.
f@0 154 *
f@0 155 * @return the tree node name
f@0 156 */
f@0 157 public String getName(){
f@0 158 return ((UserObject)userObject).getName();
f@0 159 }
f@0 160
f@0 161 @Override
f@0 162 public boolean isRoot(){
f@0 163 return false; // root node overwrites this method
f@0 164 }
f@0 165
f@0 166 @Override
f@0 167 public DiagramTreeNode getLastLeaf() {
f@0 168 return (DiagramTreeNode)super.getLastLeaf();
f@0 169 }
f@0 170
f@0 171 @Override
f@0 172 public DiagramTreeNode getNextLeaf() {
f@0 173 return (DiagramTreeNode)super.getNextLeaf();
f@0 174 }
f@0 175
f@0 176 @Override
f@0 177 public DiagramTreeNode getNextNode() {
f@0 178 return (DiagramTreeNode)super.getNextNode();
f@0 179 }
f@0 180
f@0 181 @Override
f@0 182 public DiagramTreeNode getNextSibling() {
f@0 183 return (DiagramTreeNode)super.getNextSibling();
f@0 184 }
f@0 185
f@0 186 @Override
f@0 187 public DiagramTreeNode getPreviousLeaf() {
f@0 188 return (DiagramTreeNode)super.getPreviousLeaf();
f@0 189 }
f@0 190
f@0 191 @Override
f@0 192 public DiagramTreeNode getPreviousNode() {
f@0 193 return (DiagramTreeNode)super.getPreviousNode();
f@0 194 }
f@0 195
f@0 196 @Override
f@0 197 public DiagramTreeNode getPreviousSibling() {
f@0 198 return (DiagramTreeNode)super.getPreviousSibling();
f@0 199 }
f@0 200
f@0 201 private void setSuperClassUserObject(Object u){
f@0 202 super.setUserObject(u);
f@0 203 }
f@0 204
f@0 205 private UserObject getUserObjectInstance(){
f@0 206 return new UserObject();
f@0 207 }
f@0 208
f@0 209 /**
f@0 210 * The bookmarks, involving this node, entered by the user in the DiagramTree this node belongs to.
f@0 211 */
f@0 212 protected List<String> bookmarkKeys;
f@0 213 /**
f@0 214 * The notes set by the user for this node.
f@0 215 */
f@0 216 protected String notes;
f@0 217 /* hides the DefaultMutableTreeNode protected field */
f@0 218 private Object userObject;
f@0 219 /**
f@0 220 * The character that is appended to the label of this node when the user enters some notes for it.
f@0 221 */
f@0 222 protected static final char NOTES_CHAR = '#';
f@0 223 /**
f@0 224 * The character that is appended to the label of this node when it's bookmarked by the user.
f@0 225 */
f@0 226 protected static final char BOOKMARK_CHAR = '\'';
f@0 227 /**
f@0 228 * The string that is appended to the spoken text of this node when the user enters some notes for it.
f@0 229 *
f@0 230 * @see #spokenText()
f@0 231 */
f@0 232 protected static final String BOOKMARK_SPEAK = ", bookmarked";
f@0 233 /**
f@0 234 * The string that is appended to the spoken text of this node when it's bookmarked by the user.
f@0 235 *
f@0 236 * @see #spokenText()
f@0 237 */
f@0 238 protected static final String NOTES_SPEAK = ", has notes";
f@0 239
f@0 240 @Override
f@0 241 public Object clone(){
f@0 242 DiagramTreeNode clone = (DiagramTreeNode )super.clone();
f@0 243 clone.notes = "";
f@0 244 clone.bookmarkKeys = new ArrayList<String>();
f@0 245 clone.userObject = clone.getUserObjectInstance();
f@0 246 clone.setSuperClassUserObject(clone.userObject);
f@0 247 return clone;
f@0 248 }
f@0 249
f@0 250 /* this works as a wrapper for the real user object in order to provide */
f@0 251 /* decoration on the treeNode label to signal and/or bookmarks */
f@0 252 private class UserObject {
f@0 253 private Object object;
f@0 254
f@0 255 public UserObject(){
f@0 256 object = "";
f@0 257 }
f@0 258 public void setObject(Object o){
f@0 259 this.object = o;
f@0 260 }
f@0 261
f@0 262 @Override
f@0 263 public boolean equals(Object o){
f@0 264 return this.object.equals(o);
f@0 265 }
f@0 266
f@0 267 @Override
f@0 268 public String toString(){
f@0 269 StringBuilder builder = new StringBuilder(object.toString());
f@0 270 if(!"".equals(notes)){
f@0 271 builder.append(NOTES_CHAR);
f@0 272 }
f@0 273 if(!bookmarkKeys.isEmpty())
f@0 274 builder.append(BOOKMARK_CHAR);
f@0 275 return builder.toString();
f@0 276 }
f@0 277
f@0 278 public String spokenText(){
f@0 279 StringBuilder builder = new StringBuilder(object.toString());
f@0 280 if(!"".equals(notes)){
f@0 281 builder.append(NOTES_SPEAK);
f@0 282 }
f@0 283 if(!bookmarkKeys.isEmpty())
f@0 284 builder.append(BOOKMARK_SPEAK);
f@0 285 return builder.toString();
f@0 286 }
f@0 287
f@0 288 public String getName(){
f@0 289 return object.toString();
f@0 290 }
f@0 291 }
f@0 292 }