annotate java/src/uk/ac/qmul/eecs/ccmi/network/Command.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
f@0 20 package uk.ac.qmul.eecs.ccmi.network;
f@0 21
f@0 22 import uk.ac.qmul.eecs.ccmi.gui.DiagramEventSource;
f@0 23 import uk.ac.qmul.eecs.ccmi.utils.CharEscaper;
f@0 24 import uk.ac.qmul.eecs.ccmi.utils.InteractionLog;
f@0 25
f@0 26 /**
f@0 27 * A command message that triggers an update in the model of the target diagram. Possible updates
f@0 28 * are those listed in the {@code MessageName} enum.
f@0 29 * Command messages are issued by both clients and server. Clients commands are sent after a
f@0 30 * user action. When the server receives a command it broadcasts it to all the clients but the one
f@0 31 * from which the command was from.
f@0 32 *
f@0 33 */
f@0 34 public class Command extends Message {
f@0 35 public Command(Name name, String diagram, Object[] args, long timestamp, DiagramEventSource source){
f@0 36 super(timestamp,diagram,source);
f@0 37 this.name = name;
f@0 38 this.args = args;
f@0 39 }
f@0 40
f@0 41 public Command(Name name, String diagram, Object[] args, DiagramEventSource source){
f@0 42 super(diagram,source);
f@0 43 this.name = name;
f@0 44 this.args = args;
f@0 45 }
f@0 46
f@0 47 public Command(Name name, String diagram, long timestamp, DiagramEventSource source){
f@0 48 this(name, diagram, source);
f@0 49 }
f@0 50
f@0 51 public Command(Name name, String diagram, DiagramEventSource source){
f@0 52 this(name, diagram, new Object[]{},source);
f@0 53 }
f@0 54
f@0 55 @Override
f@0 56 public DiagramEventSource getSource(){
f@0 57 return (DiagramEventSource)super.getSource();
f@0 58 }
f@0 59
f@0 60 @Override
f@0 61 public Name getName() {
f@0 62 return name;
f@0 63 }
f@0 64
f@0 65 public Object getArgAt(int index) {
f@0 66 return args[index];
f@0 67 }
f@0 68
f@0 69 public Object[] getArgs(){
f@0 70 return args;
f@0 71 }
f@0 72
f@0 73 public int getArgNum(){
f@0 74 return args.length;
f@0 75 }
f@0 76
f@0 77
f@0 78 /**
f@0 79 * Utility method to log, through the interaction log, the receipt of a command
f@0 80 * @param cmd the received command
f@0 81 * @param action a further description of the action that triggered this command
f@0 82 */
f@0 83 public static void log(Command cmd, String action){
f@0 84 if(cmd.getName() != Command.Name.LOCAL && cmd.getName() != Command.Name.BEND
f@0 85 && cmd.getName() != Command.Name.TRANSLATE_EDGE && cmd.getName() != Command.Name.TRANSLATE_NODE){
f@0 86 StringBuilder builder = new StringBuilder(cmd.getName().toString());
f@0 87 builder.append(' ').append(cmd.getDiagram());
f@0 88 for(int i=0; i<cmd.getArgNum();i++){
f@0 89 builder.append(' ').append(cmd.getArgAt(i));
f@0 90 }
f@0 91 /* replace newlines for notes so that the log has them in one line only */
f@0 92 if(cmd.getName() == Command.Name.SET_NOTES){
f@0 93 InteractionLog.log("SERVER", action, CharEscaper.replaceNewline(builder.toString()));
f@0 94 return;
f@0 95 }
f@0 96 InteractionLog.log("SERVER", action, builder.toString());
f@0 97 }
f@0 98 }
f@0 99
f@0 100 public static Name valueOf(String n){
f@0 101 Name name = Name.NONE;
f@0 102 try {
f@0 103 name = Name.valueOf(n);
f@0 104 }catch(IllegalArgumentException iae){
f@0 105 iae.printStackTrace();
f@0 106 }
f@0 107 return name;
f@0 108 }
f@0 109
f@0 110 private Name name;
f@0 111 private Object[] args;
f@0 112
f@0 113 public static enum Name implements Message.MessageName {
f@0 114 NONE,
f@0 115 LIST,
f@0 116 GET,
f@0 117 LOCAL,
f@0 118 INSERT_EDGE,
f@0 119 INSERT_NODE,
f@0 120 REMOVE_NODE,
f@0 121 REMOVE_EDGE,
f@0 122 SET_NODE_NAME,
f@0 123 SET_EDGE_NAME,
f@0 124 SET_PROPERTY,
f@0 125 SET_PROPERTIES,
f@0 126 CLEAR_PROPERTIES,
f@0 127 SET_NOTES,
f@0 128 ADD_PROPERTY,
f@0 129 REMOVE_PROPERTY,
f@0 130 SET_MODIFIERS,
f@0 131 SET_ENDDESCRIPTION,
f@0 132 SET_ENDLABEL,
f@0 133 TRANSLATE_NODE,
f@0 134 TRANSLATE_EDGE,
f@0 135 BEND,
f@0 136 STOP_EDGE_MOVE,
f@0 137 STOP_NODE_MOVE,
f@0 138 /**
f@0 139 * not a proper command, only used for awareness on node selection for edge creation.
f@0 140 */
f@0 141 SELECT_NODE_FOR_EDGE_CREATION,
f@0 142 /**
f@0 143 * not a proper command, only used for awareness on node un-selection for edge creation.
f@0 144 */
f@0 145 UNSELECT_NODE_FOR_EDGE_CREATION;
f@0 146 }
f@0 147 }