1 19 package org.lucane.applications.whiteboard.graph; 20 21 import java.awt.Color ; 22 import java.util.Date ; 23 import java.util.HashMap ; 24 import java.util.Map ; 25 26 import org.jgraph.JGraph; 27 import org.jgraph.cellview.JGraphDiamondView; 28 import org.jgraph.cellview.JGraphEllipseView; 29 import org.jgraph.cellview.JGraphMultilineView; 30 import org.jgraph.cellview.JGraphRoundRectView; 31 import org.jgraph.graph.AttributeMap; 32 import org.jgraph.graph.CellMapper; 33 import org.jgraph.graph.CellView; 34 import org.jgraph.graph.DefaultGraphCell; 35 import org.jgraph.graph.DefaultGraphModel; 36 import org.jgraph.graph.GraphConstants; 37 import org.jgraph.graph.GraphModel; 38 import org.jgraph.graph.GraphUndoManager; 39 import org.jgraph.graph.ParentMap; 40 import org.jgraph.graph.Port; 41 import org.jgraph.graph.VertexView; 42 import org.lucane.applications.whiteboard.graph.cells.DiamondCell; 43 import org.lucane.applications.whiteboard.graph.cells.EllipseCell; 44 import org.lucane.applications.whiteboard.graph.cells.ImageCell; 45 import org.lucane.applications.whiteboard.graph.cells.RoundRectangleCell; 46 import org.lucane.applications.whiteboard.graph.cells.TextCell; 47 48 public class MyGraph extends JGraph 49 { 50 private GraphUndoManager undoManager; 51 private Color cellBorder = Color.BLACK; 52 private Color cellBackground = null; 53 private int lineWidth = 1; 54 private float[] dashPattern = new float[] { 1, 0 }; 55 private Object [] clipboard = new Object [0]; 56 57 public MyGraph(GraphModel model) 58 { 59 super(model); 60 this.undoManager = new GraphUndoManager(); 61 model.addUndoableEditListener(undoManager); 62 } 63 64 public void setModel(GraphModel model) 65 { 66 super.setModel(model); 67 this.undoManager = new GraphUndoManager(); 68 model.addUndoableEditListener(undoManager); 69 } 70 71 public void resetUndoManager() 72 { 73 this.undoManager = new GraphUndoManager(); 74 this.getModel().addUndoableEditListener(undoManager); 75 } 76 77 protected VertexView createVertexView(JGraph graph, CellMapper cm, Object v ) 78 { 79 if (v instanceof EllipseCell) 80 return new JGraphEllipseView(v, this, cm); 81 else if (v instanceof DiamondCell) 82 return new JGraphDiamondView(v, this, cm); 83 else if (v instanceof RoundRectangleCell) 84 return new JGraphRoundRectView(v, this, cm); 85 else if (v instanceof ImageCell) 86 return new ScaledVertexView(v, this, cm); 87 else if ((v instanceof TextCell) && ((TextCell) v).isMultiLined()) 88 return new JGraphMultilineView(v, this, cm); 89 return super.createVertexView(graph, cm, v); 90 } 91 92 public Object [] getAllCells() 93 { 94 return getDescendants(getRoots()); 95 } 96 97 public boolean isGroup(Object cell) 98 { 99 CellView view = getGraphLayoutCache().getMapping(cell, false); 101 if (view != null) 102 return !view.isLeaf(); 103 104 return false; 105 } 106 107 public void setCellBorder(Color color) 108 { 109 this.cellBorder = color == null ? Color.BLACK : color; 110 111 Object [] cells = getSelectionCells(); 112 HashMap changes = new HashMap (); 113 for(int i=0;i<cells.length;i++) 114 { 115 DefaultGraphCell cell = (DefaultGraphCell)cells[i]; 116 AttributeMap attr = new AttributeMap(); 117 GraphConstants.setBorderColor(attr, cellBorder); 118 GraphConstants.setLineColor(attr, cellBorder); 119 changes.put(cell, attr); 120 } 121 getModel().edit(changes, null, null, null); 122 } 123 124 public Color getCellBorder() 125 { 126 return this.cellBorder; 127 } 128 129 public void setCellBackground(Color color) 130 { 131 this.cellBackground = color; 132 133 Object [] cells = getSelectionCells(); 134 HashMap changes = new HashMap (); 135 for(int i=0;i<cells.length;i++) 136 { 137 DefaultGraphCell cell = (DefaultGraphCell)cells[i]; 138 AttributeMap attr = new AttributeMap(); 139 GraphConstants.setOpaque(attr, cellBackground!=null); 140 if(cellBackground != null) 141 GraphConstants.setBackground(attr, cellBackground); 142 changes.put(cell, attr); 143 } 144 getModel().edit(changes, null, null, null); 145 } 146 147 public Color getCellBackground() 148 { 149 return this.cellBackground; 150 } 151 152 public void setLineWidth(int w) 153 { 154 this.lineWidth = w; 155 Object [] cells = getSelectionCells(); 156 HashMap changes = new HashMap (); 157 for(int i=0;i<cells.length;i++) 158 { 159 DefaultGraphCell cell = (DefaultGraphCell)cells[i]; 160 AttributeMap attr = new AttributeMap(); 161 GraphConstants.setLineWidth(attr, lineWidth); 162 changes.put(cell, attr); 163 } 164 getModel().edit(changes, null, null, null); 165 } 166 167 public int getLineWidth() 168 { 169 return this.lineWidth; 170 } 171 172 public void setDashPattern(float[] pattern) 173 { 174 this.dashPattern = pattern; 175 Object [] cells = getSelectionCells(); 176 HashMap changes = new HashMap (); 177 for(int i=0;i<cells.length;i++) 178 { 179 DefaultGraphCell cell = (DefaultGraphCell)cells[i]; 180 AttributeMap attr = new AttributeMap(); 181 GraphConstants.setDashPattern(attr, pattern); 182 changes.put(cell, attr); 183 } 184 getModel().edit(changes, null, null, null); 185 } 186 187 public float[] getDashPattern() 188 { 189 return this.dashPattern; 190 } 191 192 public void cut() 193 { 194 Object [] cells = getSelectionCells(); 195 if (cells != null) 196 cells = DefaultGraphModel.getDescendants(getModel(), cells).toArray(); 197 198 199 this.copy(); 200 getModel().remove(cells); 201 } 202 203 public void copy() 204 { 205 Object [] cells = getSelectionCells(); 206 clipboard = GraphUtils.cloneCells(this, cells); 207 System.out.println("copy: " + clipboard.length); 208 } 209 210 public boolean paste() 211 { 212 if(clipboard.length == 0) 213 return false; 214 215 Object [] copy = GraphUtils.cloneCells(this, clipboard); 216 GraphUtils.changeTimeStamp(copy); 217 ParentMap parents = GraphUtils.createParentMap(copy); 218 Map attrs = GraphUtils.createAttributeMap(copy); 219 220 getModel().insert(copy, attrs, null, parents, null); 221 System.out.println("paste: " + clipboard.length); 222 return true; 223 } 224 225 public boolean undo() 226 { 227 if(undoManager.canUndo()) 228 { 229 undoManager.undo(getModel()); 230 return true; 231 } 232 return false; 233 } 234 235 public boolean redo() 236 { 237 if(undoManager.canRedo()) 238 { 239 undoManager.redo(getModel()); 240 return true; 241 } 242 return false; 243 } 244 } | Popular Tags |