// XAAL toolkit // Copyright (C) 2009 Ville Karavirta // // 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 . package xaal.objects.animation.graphical; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.StringTokenizer; import xaal.objects.AddCoordinate; import xaal.objects.Xaal; import xaal.objects.animation.GraphicalOperation; import xaal.objects.graphical.Circle; import xaal.objects.graphical.Coordinate; import xaal.objects.graphical.GraphicalPrimitive; import xaal.objects.graphical.Line; import xaal.objects.graphical.Polyline; import xaal.objects.graphical.Rectangle; import xaal.objects.graphical.Text; public class MoveOperation extends GraphicalOperation implements AddCoordinate, Cloneable { public static final String MOVE_TYPE = "move"; public static final String TRANSLATE_TYPE = "translate"; private Coordinate coordinate; private String type = "move"; private Keypoints keyPoints; private GraphicalPrimitive along; public MoveOperation(Xaal xaalDoc) { super(xaalDoc); keyPoints = new Keypoints(); } public Object clone() throws CloneNotSupportedException { // WARNING: neither Keypoints nor GraphicalPrimitive is cloned MoveOperation copy = (MoveOperation) super.clone(); copy.coordinate = (Coordinate) coordinate.clone(); return copy; } /** * @return Returns the coordinate. */ public Coordinate getNewCoordinate() { return coordinate; } /** * @param coordinate * The coordinate to set. */ public void setNewCoordinate(Coordinate coordinate) { this.coordinate = coordinate; } public void addCoordinate(Coordinate c) { setNewCoordinate(c); } public GraphicalPrimitive getAlongObject() { return along; } public void setAlongObject(GraphicalPrimitive along) { this.along = along; } public void setType(String type) { if (type != null && (type.equalsIgnoreCase("move") || type .equalsIgnoreCase("translate"))) this.type = type; else { // TODO error handling, invalid type System.err.println("invalid move type " + type); } } public String getType() { return type; } public void addKeypoint(Coordinate c) { keyPoints.addCoordinate(c); } public Keypoints getKeypoints() { return keyPoints; } public static class Keypoints implements AddCoordinate { private List coordinates = new ArrayList(); public void addCoordinate(Coordinate c) { coordinates.add(c); } public Iterator getCoordinates() { return coordinates.iterator(); } } public void apply(HashMap state) { Iterator i = getGraphicals(); while (i.hasNext()) { GraphicalPrimitive gp = (GraphicalPrimitive) i.next(); if (gp instanceof Rectangle) { applyForRectangle((Rectangle) gp); } else if (gp instanceof Circle) { applyForCircle((Circle) gp); } else if (gp instanceof Text) { Text t = (Text) gp; Coordinate coord = t.getCoordinate(); coord.setX(coord.getX() + getNewCoordinate().getX()); coord.setY(coord.getY() + getNewCoordinate().getY()); } else if (gp instanceof Line) { Line t = (Line) gp; Coordinate start = t.getStartCoordinate(); Coordinate end = t.getEndCoordinate(); start.setX(start.getX() + getNewCoordinate().getX()); end.setX(end.getX() + getNewCoordinate().getX()); start.setY(start.getY() + getNewCoordinate().getY()); end.setY(end.getY() + getNewCoordinate().getY()); } else if (gp instanceof Polyline) { int dx = getNewCoordinate().getX(); int dy = getNewCoordinate().getY(); Polyline p = (Polyline) gp; Iterator it = p.getPoints(); while (it.hasNext()) { Coordinate point = (Coordinate) it.next(); point.setX(point.getX() + dx); point.setY(point.getY() + dy); } } else { System.out.println("Unknown graphical primitive in 'apply'" + " method of move operation: " + gp.getClass()); } } } private void applyForCircle(Circle gp) { Coordinate c = gp.getCenter(); if (type.equals(MOVE_TYPE)) { c.setX(getNewCoordinate().getX()); c.setY(getNewCoordinate().getY()); } else if (type.equals(TRANSLATE_TYPE)) { c.setX(c.getX() + getNewCoordinate().getAbsoluteX()); c.setY(c.getY() + getNewCoordinate().getAbsoluteY()); } } private void applyForRectangle(Rectangle r) { if (type.equals(MOVE_TYPE)) { Coordinate start = r.getStartCoordinate(); int dx = start.getX() - getNewCoordinate().getX(); int dy = start.getY() - getNewCoordinate().getY(); start.setX(getNewCoordinate().getX()); start.setY(getNewCoordinate().getY()); Coordinate end = r.getEndCoordinate(); end.setX(end.getX() + dx); end.setY(end.getY() + dy); } else if (type.equals(TRANSLATE_TYPE)) { if (getObjectData(r) == null) { Coordinate start = r.getStartCoordinate(); start.setX(start.getX() + getNewCoordinate().getAbsoluteX()); start.setY(start.getY() + getNewCoordinate().getAbsoluteY()); } else { StringTokenizer st = new StringTokenizer(getObjectData(r) .toString()); while (st.hasMoreTokens()) { try { int node = Integer.parseInt(st.nextToken()); if (node == 1) { Coordinate start = r.getStartCoordinate(); start.setX(start.getX() + getNewCoordinate().getAbsoluteX()); start.setY(start.getY() + getNewCoordinate().getAbsoluteY()); } else if (node == 2) { Coordinate end = r.getEndCoordinate(); end.setX(end.getX() + getNewCoordinate().getAbsoluteX()); end.setY(end.getY() + getNewCoordinate().getAbsoluteY()); } } catch (NumberFormatException e) { } } } } } }