package com.jotuntech.sketcher.server.command; import java.nio.ByteBuffer; import com.jotuntech.sketcher.common.BitmapLayer; import com.jotuntech.sketcher.common.Layer; import com.jotuntech.sketcher.common.User; import com.jotuntech.sketcher.server.AccessController; import com.jotuntech.sketcher.server.Command; import com.jotuntech.sketcher.server.CommandEntry; import com.jotuntech.sketcher.server.Connection; import com.jotuntech.sketcher.server.Server; public class CreateLayerCommand implements Command { int layerKey; int type; String name; public CreateLayerCommand() { } public CreateLayerCommand(int layerKey, int type, String name) { this.layerKey = layerKey; this.type = type; this.name = name; } public int perform(Server server, Connection connection) { User user = connection.getUser(); if(user == null) { /** Connections with users only */ return Connection.SEND_NONE; } if(user.isViewer()) { return Connection.SEND_NONE; } /** Check access controller */ AccessController controller = server.getAccessController(); if(controller != null) { if(!controller.isLayerActionAllowed(server, user)) { connection.getSendQueue().offer(new CommandEntry(0, new ServerMessageCommand("You are not allowed to do that."))); return Connection.SEND_NONE; } } Layer l = new BitmapLayer(name); layerKey = server.getCanvas().getLayerMap().put(l); return Connection.SEND_ALL; } public void decode(ByteBuffer in) { layerKey = in.getInt(); type = in.get(); StringBuffer nameBuffer = new StringBuffer(); while(in.remaining() >= 2) { nameBuffer.append(in.getChar()); } name = nameBuffer.toString(); } public void encode(ByteBuffer out) { out.putInt(layerKey); out.put((byte) type); for(int i = 0; i < name.length(); i++) { out.putChar(name.charAt(i)); } } }