annotate java/src/uk/ac/qmul/eecs/ccmi/gui/DiagramEventSource.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.gui;
f@0 20
f@0 21 /**
f@0 22 * This class identifies the source of a diagram event, that is an event generated by an action
f@0 23 * in the diagram such as for instance a node insertion, deletion or renaming. The class carries informations
f@0 24 * about how the event was generated (from the tree, from the graph etc.) and if the event was
f@0 25 * generated by the local user or another user co-editing the diagram. In either case an id of the
f@0 26 * user is conveyed as well. An id is just a String each user assigns to themselves through a user
f@0 27 * interface panel.
f@0 28 */
f@0 29 public class DiagramEventSource {
f@0 30 /**
f@0 31 * An enumeration of the different ways an event can be generated. NONE is for when the
f@0 32 * information is not relevant (as normally no event listener will be triggered by the event).
f@0 33 * PERS is for actions triggered when rebuilding a diagram from a ccmi file, so not as a consequence
f@0 34 * of a direct user action.
f@0 35 */
f@0 36 public static enum Type{
f@0 37 TREE,
f@0 38 GRPH, // graph
f@0 39 HAPT, // haptics
f@0 40 PERS, // persistence
f@0 41 NONE;
f@0 42 }
f@0 43
f@0 44 /* constructor used only by the static event sources */
f@0 45 private DiagramEventSource(Type type){
f@0 46 this.type = type;
f@0 47 local = true;
f@0 48 }
f@0 49
f@0 50 /**
f@0 51 * Creates a new DiagramEventSource out of a previous one (normally one of the static default sources).
f@0 52 * The type of the new object will be the same as the one passed as argument. Object created through
f@0 53 * this constructor are marked as non local
f@0 54 *
f@0 55 * @see #isLocal()
f@0 56 * @param eventSource an instance of this class
f@0 57 */
f@0 58 public DiagramEventSource(DiagramEventSource eventSource){
f@0 59 this.type = eventSource.type;
f@0 60 local = false;
f@0 61 }
f@0 62
f@0 63 /**
f@0 64 * Returns true if the event is local, that is it's has been generated from an action of
f@0 65 * the local user and not from a message coming from the server.
f@0 66 *
f@0 67 * @return {@code true} if the event has been generated by the local user
f@0 68 */
f@0 69 public boolean isLocal(){
f@0 70 return local;
f@0 71 }
f@0 72
f@0 73 /**
f@0 74 * Returns a copy of this event source that is marked as local.
f@0 75 *
f@0 76 * @return a local copy of this event source
f@0 77 * @see #isLocal()
f@0 78 */
f@0 79 public DiagramEventSource getLocalSource(){
f@0 80 return new DiagramEventSource(type);
f@0 81 }
f@0 82
f@0 83 /**
f@0 84 * Returns the name of the diagram where the event happened, that has this
f@0 85 * object as source.
f@0 86 *
f@0 87 * @return the name of the diagram
f@0 88 */
f@0 89 public String getDiagramName(){
f@0 90 return diagramName;
f@0 91 }
f@0 92
f@0 93 /**
f@0 94 * Sets the name of the diagram where the event happened, that has this
f@0 95 * object as source.
f@0 96 *
f@0 97 * @param diagramName the name of the diagram
f@0 98 */
f@0 99 public void setDiagramName(String diagramName){
f@0 100 this.diagramName = diagramName;
f@0 101 }
f@0 102
f@0 103 /**
f@0 104 * The String representation of this object is the concatenation of the type
f@0 105 * and the ID.
f@0 106 *
f@0 107 * @return a String representing this object
f@0 108 */
f@0 109 @Override
f@0 110 public String toString(){
f@0 111 return (local ? ISLOCAL_CHAR : ISNOTLOCAL_CHAR )+type.toString();
f@0 112 }
f@0 113
f@0 114 /**
f@0 115 * Returns an instance of this class out of a string representation, such as
f@0 116 * returned by {@code toString()}
f@0 117 * @param s a String representation of a {@code DiagramEventSource} instance, such as
f@0 118 * returned by {@code toString()}
f@0 119 * @return a new instance of {@code DiagramEventSource}
f@0 120 */
f@0 121 public static DiagramEventSource valueOf(String s){
f@0 122 Type t = Type.valueOf(s.substring(1, 5));
f@0 123 DiagramEventSource toReturn = new DiagramEventSource(t);
f@0 124 toReturn.local = (s.charAt(0) == ISLOCAL_CHAR) ? true : false;
f@0 125 return toReturn;
f@0 126 }
f@0 127
f@0 128 private boolean local;
f@0 129 public final Type type;
f@0 130 private String diagramName;
f@0 131 private static char ISLOCAL_CHAR = 'L';
f@0 132 private static char ISNOTLOCAL_CHAR = 'R';
f@0 133
f@0 134 /** Source for events triggered by the local user through the tree. These static sources
f@0 135 * are used when the diagram is not shared with any other user. When it is, a new DiagramEventSource
f@0 136 * will be created, which includes informations about the id and locality of the user who generated the event
f@0 137 */
f@0 138 public static DiagramEventSource TREE = new DiagramEventSource(Type.TREE);
f@0 139 /** Source for events triggered by the local user through the graph */
f@0 140 public static DiagramEventSource GRPH = new DiagramEventSource(Type.GRPH);
f@0 141 /** Source for events triggered by the local user through the haptic device */
f@0 142 public static DiagramEventSource HAPT = new DiagramEventSource(Type.HAPT);
f@0 143 /** Source for events triggered by the local user when opening a file */
f@0 144 public static DiagramEventSource PERS = new DiagramEventSource(Type.PERS);
f@0 145 /** Source for events not relevant to model listeners */
f@0 146 public static DiagramEventSource NONE = new DiagramEventSource(Type.NONE);
f@0 147
f@0 148 }