view java/src/uk/ac/qmul/eecs/ccmi/network/DiagramEventActionSource.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;

/**
 * This class represent a source of an editing action. An editing action is initiated when 
 * the user gets the lock on a certain element and terminates when the user after changing 
 * the diagram model somehow yields the lock back to the server.   
 *
 */
public class DiagramEventActionSource extends DiagramEventSource{

	public long getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(long timestamp) {
		this.timestamp = timestamp;
	}

	public DiagramEventActionSource (DiagramEventSource eventSource, Command.Name cmd, long elementID, String elementName) {
		super(eventSource);
		this.cmd = cmd;
		this.elementID = elementID;
		this.saveID = elementID; 
		this.elementName = elementName;
		userName = AwarenessMessage.getDefaultUserName();
		timestamp = System.currentTimeMillis();
	}

	public Command.Name getCmd() {
		return cmd;
	}
	
	public long getElementID(){
		return elementID;
	}
	
	public void setElementID(long ID){
		elementID = ID;
	}
	
	long getSaveID(){
		return saveID;
	}
	
	public String getElementName(){
		return elementName;
	}
	
	public void setElementName(String elementName){
		this.elementName = elementName;
	}
	
	public void setUserName(String name){
		this.userName = name;
	}
	
	public String getUserName(){
		return userName;
	}
	
	/**
	 * 
	 * The local user never gets this informations from itself are they are attached to 
	 * AwernessMessages they receive only from other users. Therefore instances of this class
	 * are never considered local. 
	 */
	@Override
	public boolean isLocal(){
		return false;
	}
	
	/**
	 * Returns an instance of {@code DiagramEventActionSource} out of a 
	 * String passed as argument
	 * @param s a string representation of a  {@code DiagramEventActionSource} instance, as 
	 * returned by toString.  
	 * @return an instance of  {@code DiagramEventActionSource}
	 */
	public static DiagramEventActionSource valueOf(String s){
		if(s.isEmpty())
			return NULL;
		String[] strings = s.split(SEPARATOR); 
		long id = Long.parseLong(strings[1]);
		String elementName = strings[2];
		long timestamp = Long.parseLong(strings[3]);
		DiagramEventSource eventSource = DiagramEventSource.valueOf(strings[5]);
		DiagramEventActionSource toReturn = new DiagramEventActionSource(eventSource,Command.Name.valueOf(strings[0]),id,elementName);
		toReturn.setUserName(strings[4]);
		toReturn.setTimestamp(timestamp);
		return toReturn;
	}

	/**
	 * Encodes this object into a String. the encoding is done by concatenating the command name 
	 * with the string representation of the event source. the command name is encoded in a fixed length
	 * string and padded with white spaces if such length is greater than the command name's.
	 */
	@Override
	public String toString(){
		StringBuilder builder = new StringBuilder(cmd.name());
		builder.append(SEPARATOR);
		builder.append(elementID);
		builder.append(SEPARATOR);
		builder.append(elementName);
		builder.append(SEPARATOR);
		builder.append(timestamp);
		builder.append(SEPARATOR);
		builder.append(userName);
		builder.append(SEPARATOR);
		builder.append(super.toString());
		return builder.toString();
	}
	
	
	
	private String userName;
	private Command.Name cmd;
	private long elementID;
	private long saveID;
	private String elementName;
	private static final String SEPARATOR = "\n";
	private long timestamp;
	
	public static DiagramEventActionSource NULL = new DiagramEventActionSource(DiagramEventSource.NONE,Command.Name.NONE,-1,""){
		@Override
		public String toString(){
			return "";
		}
	};
	
}