001 // GraphLab Project: http://graphlab.sharif.edu 002 // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology 003 // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/ 004 package graphlab.graph.graph; 005 006 import graphlab.graph.atributeset.VertexAttrSet; 007 import graphlab.graph.event.VertexModelListener; 008 import graphlab.graph.old.GShape; 009 import graphlab.graph.old.GStroke; 010 import graphlab.library.BaseVertex; 011 import graphlab.library.BaseVertexProperties; 012 import graphlab.platform.attribute.AttributeSet; 013 014 import java.awt.*; 015 import java.awt.geom.Rectangle2D; 016 import java.util.HashMap; 017 import java.util.Map; 018 019 020 /** 021 * Authors: Azin Azadi,Roozbeh 022 */ 023 024 025 public class VertexModel extends BaseVertex { 026 027 public VertexModelListener view;// = emptyListener; 028 029 //todo(bug): VertexModel is dependent on Fast Renderer! 030 public GShape shape = FastRenderer.defaultVertexShape; 031 032 GStroke shapeStroke = FastRenderer.defaultBorderStroke; 033 034 public boolean isSelected = false; 035 String label = ""; 036 Point center = new Point(); 037 // private static GraphPoint dp=new GraphPoint(100,100); 038 //represents the location of the vertex on the graph, this location will not changed by zooming and similar operations, the location only change by moving the vertex 039 private GraphPoint location = new GraphPoint(100, 100); 040 041 // @UserModifiableProperty(name="defaultShapeDimension") 042 private GraphPoint shapeSize = new GraphPoint(FastRenderer.defaultShapeDimension.getHeight(), FastRenderer.defaultShapeDimension.getWidth()); 043 044 /** 045 * the location of the lable relative to the center of the edge 046 */ 047 048 private GraphPoint labelLocation = new GraphPoint(0, 0); 049 050 //________________________ Userdefined Attributes _________________________________ 051 /** 052 * This is a place to put custom attributes in the vertex, It will be shown in property editor and editable 053 */ 054 private HashMap<String, Object> userDefinedAttributes = null; 055 056 /** 057 * these attributed will be added to each vertice's userDefinedAttributes on constructing time. 058 */ 059 private static HashMap<String, Object> globalUserDefinedAttributes = null; 060 061 062 /** 063 * sets and stores a user defined attribute for the vertex. here you can put any attribute you like that are not available 064 * in the standard attributes. your attributes will be editable in property editor part of GUI. 065 * Use this method carefully. user defined attributes are stored in HashMap and bad use of them will cause memory leak in large graphs 066 * 067 * @param name 068 * @param value 069 */ 070 public void setUserDefinedAttribute(String name, Object value) { 071 if (userDefinedAttributes == null) { 072 userDefinedAttributes = new HashMap<String, Object>(); 073 } 074 userDefinedAttributes.put(name, value); 075 } 076 077 /** 078 * returns the specified user defined attribute, or null if it does not exists. 079 * 080 * @param name 081 * @return 082 */ 083 public <t> t getUserDefinedAttribute(String name) { 084 if (userDefinedAttributes == null) 085 return null; 086 return (t) userDefinedAttributes.get(name); 087 } 088 089 /** 090 * removes the given attribute from the list of user defined attributes 091 * 092 * @param name 093 */ 094 public void removeUserDefinedAttribute(String name) { 095 userDefinedAttributes.remove(name); 096 if (userDefinedAttributes.size() == 0) 097 userDefinedAttributes = null; 098 } 099 100 /** 101 * @return a HashMap containing all user defined attributes. 102 */ 103 public HashMap<String, Object> getUserDefinedAttributes() { 104 return userDefinedAttributes; 105 } 106 107 108 /** 109 * sets and stores a global user defined attribute for the vertex. this attributes will be added to each vertex on 110 * constructing time using setUserDefinedAttribute method. 111 * <p/> 112 * note that this method only affects the afterward created vertices, and current vertices will not affected by this method. 113 */ 114 public static void addGlobalUserDefinedAttribute(String name, Object defaultvalue) { 115 if (globalUserDefinedAttributes == null) { 116 globalUserDefinedAttributes = new HashMap<String, Object>(); 117 } 118 globalUserDefinedAttributes.put(name, defaultvalue); 119 } 120 121 /** 122 * @see VertexModel#addGlobalUserDefinedAttribute 123 */ 124 public static void removeGlobalUserDefinedAttribute(String name) { 125 globalUserDefinedAttributes.remove(name); 126 if (globalUserDefinedAttributes.size() == 0) 127 globalUserDefinedAttributes = null; 128 } 129 130 { 131 //default constructor 132 if (globalUserDefinedAttributes != null) { 133 userDefinedAttributes = new HashMap<String, Object>(); 134 userDefinedAttributes.putAll(globalUserDefinedAttributes); 135 } 136 } 137 138 /** 139 * copy constructor 140 * creates a copy (clone) of v 141 * 142 * @param v 143 */ 144 public VertexModel(VertexModel v) { 145 super(); 146 this.label = v.label; 147 this.location = v.location; 148 this.shape = v.shape; 149 this.shapeSize = v.shapeSize; 150 this.shapeStroke = v.shapeStroke; 151 this.labelLocation = v.labelLocation; 152 // this.labelHandler = new GLabelModel("", null); 153 //copies all attributes from second edge to first edge 154 AttributeSet a = new VertexAttrSet(v); 155 AttributeSet b = new VertexAttrSet(this); 156 Map<String, Object> map = a.getAttrs(); 157 for (String name : map.keySet()) { 158 b.put(name, map.get(name)); 159 } 160 } 161 162 public VertexModel() { 163 super(); 164 } 165 166 public VertexModel getCopy() { 167 return new VertexModel(this); 168 } 169 170 public String toString() { 171 return String.valueOf(getId()); 172 } 173 174 public void setMark(boolean mark) { 175 super.setMark(mark); 176 fireModelListenerChanged(); 177 } 178 179 public void setVertexListener(VertexModelListener listener) { 180 view = listener; 181 // if (listener == null) 182 // view = emptyListener; 183 } 184 185 public void setProp(BaseVertexProperties prop) { 186 super.setProp(prop); 187 fireModelListenerChanged(); 188 } 189 190 private void fireModelListenerChanged() { 191 if (view != null) 192 view.repaint(this); 193 } 194 195 /** 196 * @return the center point of the vertex 197 */ 198 public Point getCenter() { 199 center.x = (int) (shapeSize.x / 2); 200 center.y = (int) (shapeSize.y / 2); 201 return center; 202 } 203 204 public void setLocation(Point p) { 205 setLocation(new GraphPoint(p.x,p.y)); 206 } 207 public void setLocation(GraphPoint p) { 208 this.location = p; 209 if (view != null) 210 view.updateLocation(this, location); 211 } 212 213 /** 214 * @return the location of the vertex. it is the center of vertex 215 */ 216 public GraphPoint getLocation() { 217 return location; 218 } 219 220 public String getLabel() { 221 return label; 222 } 223 224 public void setLabel(String label) { 225 this.label = label; 226 fireModelListenerChanged(); 227 // todo 228 // if (labelHandler != null) 229 // labelHandler.setText(label); 230 } 231 232 public void setShape(GShape shape) { 233 this.shape = shape; 234 fireModelListenerChanged(); 235 } 236 237 public GShape getShape() { 238 return shape; 239 } 240 241 public void setShapeStroke(GStroke stroke) { 242 this.shapeStroke = stroke; 243 fireModelListenerChanged(); 244 } 245 246 public GStroke getShapeStroke() { 247 return shapeStroke; 248 } 249 250 public void setSize(GraphPoint size) { 251 this.shapeSize = size; 252 if (view != null) 253 view.updateSize(this, size); 254 fireModelListenerChanged(); 255 } 256 257 public GraphPoint getSize() { 258 return shapeSize; 259 } 260 261 262 public Rectangle2D.Double getBounds() { 263 return new Rectangle2D.Double(location.x - center.x, location.y - center.y, shapeSize.x, shapeSize.y); 264 } 265 266 public boolean isSelected() { 267 return isSelected; 268 } 269 270 public void setSelected(boolean selected) { 271 isSelected = selected; 272 fireModelListenerChanged(); 273 } 274 275 public void repaint() { 276 view.repaint(this); 277 } 278 279 public void setLabelLocation(GraphPoint graphPoint) { 280 this.labelLocation = graphPoint; 281 if (view != null) 282 repaint(); 283 } 284 285 public GraphPoint getLabelLocation() { 286 return labelLocation; 287 } 288 289 public void setColor(int color) { 290 super.setColor(color); 291 if (view != null) 292 repaint(); 293 } 294 }