// 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; import java.awt.Rectangle; /** * @author vkaravir * */ public abstract class XaalObject { private Xaal xaalDoc; private String id; private static int count = 1; public XaalObject(Xaal xaalDoc) { this.xaalDoc = xaalDoc; } public Xaal getXaal() { return xaalDoc; } /** * Return the id of this object. If no id has been set, this method returns * a generated, unique id. * * @return Returns the id. */ public String getId() { if (id == null) { id = generateId(); } return id; } /** * @param id The id to set. */ public void setId(String id) { this.id = id; } /** * Generates a unique id for this graphical primitive. * * @return A unique id. */ protected String generateId() { String cName = getClass().getName(); return cName.substring(cName.lastIndexOf('.') + 1) + count++; } public abstract Rectangle getBounds(); public int[] getAnchorCoordinate(String anchor) { Rectangle r = getBounds(); if (anchor.equalsIgnoreCase("NW")) { return new int[] {r.x, r.y}; } else if (anchor.equalsIgnoreCase("N")) { return new int[] {r.x + r.width/2, r.y}; } else if (anchor.equalsIgnoreCase("NE")) { return new int[] {r.x + r.width, r.y}; } else if (anchor.equalsIgnoreCase("E")) { return new int[] {r.x + r.width, r.y + r.height/2}; } else if (anchor.equalsIgnoreCase("SE")) { return new int[] {r.x + r.width, r.y + r.height}; } else if (anchor.equalsIgnoreCase("S")) { return new int[] {r.x + r.width/2, r.y + r.height}; } else if (anchor.equalsIgnoreCase("SW")) { return new int[] {r.x, r.y + r.height}; } else if (anchor.equalsIgnoreCase("W")) { return new int[] {r.x, r.y + r.height/2}; } else if (anchor.equalsIgnoreCase("C")) { return new int[] {r.x + r.width/2, r.y + r.height/2}; } else if (anchor.equalsIgnoreCase("CENTER")) { return new int[] {r.x + r.width/2, r.y + r.height/2}; } else { } return new int[] {0,0}; } }