view java/src/uk/ac/qmul/eecs/ccmi/network/Command.java @ 0:78b7fc5391a2

first import, outcome of NIME 2014 hackaton
author Fiore Martin <f.martin@qmul.ac.uk>
date Tue, 08 Jul 2014 16:28:59 +0100
parents
children
line wrap: on
line source
/*  
 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
  
 Copyright (C) 2011  Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/  

package uk.ac.qmul.eecs.ccmi.network;

import uk.ac.qmul.eecs.ccmi.gui.DiagramEventSource;
import uk.ac.qmul.eecs.ccmi.utils.CharEscaper;
import uk.ac.qmul.eecs.ccmi.utils.InteractionLog;

/**
 * A command message that triggers an update in the model of the target diagram. Possible updates
 * are those listed in the {@code MessageName} enum.  
 * Command messages are issued by both clients and server. Clients commands are sent after a
 * user action. When the server receives a command it broadcasts it to all the clients but the one
 * from which the command was from. 
 *
 */
public class Command extends Message {
	public Command(Name name, String diagram, Object[] args, long timestamp, DiagramEventSource source){
		super(timestamp,diagram,source);
		this.name = name;
		this.args = args;
	}
	
	public Command(Name name, String diagram, Object[] args, DiagramEventSource source){
		super(diagram,source);
		this.name = name;
		this.args = args;
	}
	
	public Command(Name name, String diagram, long timestamp, DiagramEventSource source){
		this(name, diagram, source);
	}
	
	public Command(Name name, String diagram, DiagramEventSource source){
		this(name, diagram, new Object[]{},source);
	}
	
	@Override
	public DiagramEventSource getSource(){
		return (DiagramEventSource)super.getSource();
	}
	
	@Override
	public Name getName() {
		return name;
	}

	public Object getArgAt(int index) {
		return args[index];
	}
	
	public Object[] getArgs(){
		return args;
	}

	public int getArgNum(){
		return args.length;
	}
	

	/**
	 * Utility method to log, through the interaction log, the receipt of a command 
	 * @param cmd the received command 
	 * @param action a further description of the action that triggered this command 
	 */
	public static void log(Command cmd, String action){
		if(cmd.getName() != Command.Name.LOCAL && cmd.getName() != Command.Name.BEND 
				&& cmd.getName() != Command.Name.TRANSLATE_EDGE && cmd.getName() != Command.Name.TRANSLATE_NODE){
			StringBuilder builder = new StringBuilder(cmd.getName().toString());
			builder.append(' ').append(cmd.getDiagram());
			for(int i=0; i<cmd.getArgNum();i++){
				builder.append(' ').append(cmd.getArgAt(i));
			}
			/* replace newlines for notes so that the log has them in one line only */
			if(cmd.getName() == Command.Name.SET_NOTES){
				InteractionLog.log("SERVER", action, CharEscaper.replaceNewline(builder.toString()));
				return;
			}
			InteractionLog.log("SERVER", action, builder.toString());
		}
	}
	
	public static Name valueOf(String n){
		Name name = Name.NONE;
		try {
			name = Name.valueOf(n);
		}catch(IllegalArgumentException iae){
			iae.printStackTrace();
		}
		return name;
	}
	
	private Name name;
	private Object[] args;
	
	public static enum Name implements Message.MessageName {
		NONE,
		LIST,
		GET,
		LOCAL,
		INSERT_EDGE,
		INSERT_NODE,
		REMOVE_NODE,
		REMOVE_EDGE,
		SET_NODE_NAME,
		SET_EDGE_NAME,
		SET_PROPERTY,
		SET_PROPERTIES,
		CLEAR_PROPERTIES,
		SET_NOTES,
		ADD_PROPERTY,
		REMOVE_PROPERTY,
		SET_MODIFIERS,
		SET_ENDDESCRIPTION,
		SET_ENDLABEL,
		TRANSLATE_NODE,
		TRANSLATE_EDGE,
		BEND,
		STOP_EDGE_MOVE,
		STOP_NODE_MOVE,
		/**
		 * not a proper command, only used for awareness on node selection for edge creation.
		 */
		SELECT_NODE_FOR_EDGE_CREATION,
		/**
		 * not a proper command, only used for awareness on node un-selection for edge creation.
		 */
		UNSELECT_NODE_FOR_EDGE_CREATION;
	}	
}