// 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.graphical; import java.util.HashMap; import xaal.objects.AddCoordinate; import xaal.objects.StyledObject; import xaal.objects.Xaal; import xaal.objects.XaalObject; import xaal.util.XaalConstants; /** * @author vkaravir * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */ public abstract class GraphicalPrimitive extends XaalObject implements StyledObject, AddCoordinate, Comparable { private int depth = Integer.MAX_VALUE; private Style style; private double opacity = XaalConstants.DEFAULT_PROPERTY_OPACITY; private boolean hidden = XaalConstants.getDefaultHidden(); private double scale; private HashMap properties; /** * */ public GraphicalPrimitive(Xaal xaalDoc) { super(xaalDoc); scale = 1.0; style = new Style(); } public GraphicalPrimitive(Xaal xaalDoc, String id) { this(xaalDoc); this.setId(id); } public Object clone() throws CloneNotSupportedException { GraphicalPrimitive copy = (GraphicalPrimitive) super.clone(); if (style != null) copy.style = (Style) style.clone(); return copy; } /** * @return Returns the depth. */ public int getDepth() { return depth; } /** * @param depth The depth to set. */ public void setDepth(int depth) { if (depth < 0) { // TODO common error handling/logging depth = 0; } this.depth = depth; } /** * @return Returns the style. */ public Style getStyle() { return style; } /** * @param style The style to set. */ public void setStyle(Style style) { this.style = style; } public void setStyle(String s) { // TODO IMPORTANT } /** * @return Returns the hidden. */ public boolean isHidden() { return hidden; } /** * @param hidden The hidden to set. */ public void setHidden(boolean hidden) { this.hidden = hidden; } /** * @return Returns the opacity. */ public double getOpacity() { return opacity; } /** * @param opacity The opacity to set. */ public void setOpacity(double opacity) { if (opacity < 0 || opacity > 1) { // TODO centralized error handling } else { this.opacity = opacity; } } public boolean equalsIgnoreId(GraphicalPrimitive gp) { return (gp.getStyle().equalsIgnoreId(getStyle()) && gp.getOpacity() == getOpacity() && gp.isHidden() == isHidden()); } public abstract void addCoordinate(Coordinate c); public void addCoordinate(int x, int y) { addCoordinate(new Coordinate(getXaal(), x, y)); } public void setScale(double d) { scale = d; } public double getScale() { return scale; } public void setProperty(String name, String value) { if (this.properties == null) { this.properties = new HashMap(); } this.properties.put(name, value); } public String getProperty(String name) { if (this.properties == null) { return null; } return this.properties.get(name); } public boolean hasProperty(String name) { if (this.properties == null) { return false; } return this.properties.containsKey(name); } /** * Compares the GraphicalPrimitives by their depth. Primitives * nearer the foreground are considered larger. * * @see java.lang.Comparable#compareTo(java.lang.Object) */ public int compareTo(GraphicalPrimitive gp) { if (getDepth() < gp.getDepth()) { return 1; } else if (getDepth() > gp.getDepth()) { return -1; } else { return 0; } } }