001: /*
002: * Lucane - a collaborative platform
003: * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package org.lucane.applications.whiteboard.graph;
020:
021: import java.awt.Color;
022: import java.util.Date;
023: import java.util.HashMap;
024: import java.util.Map;
025:
026: import org.jgraph.JGraph;
027: import org.jgraph.cellview.JGraphDiamondView;
028: import org.jgraph.cellview.JGraphEllipseView;
029: import org.jgraph.cellview.JGraphMultilineView;
030: import org.jgraph.cellview.JGraphRoundRectView;
031: import org.jgraph.graph.AttributeMap;
032: import org.jgraph.graph.CellMapper;
033: import org.jgraph.graph.CellView;
034: import org.jgraph.graph.DefaultGraphCell;
035: import org.jgraph.graph.DefaultGraphModel;
036: import org.jgraph.graph.GraphConstants;
037: import org.jgraph.graph.GraphModel;
038: import org.jgraph.graph.GraphUndoManager;
039: import org.jgraph.graph.ParentMap;
040: import org.jgraph.graph.Port;
041: import org.jgraph.graph.VertexView;
042: import org.lucane.applications.whiteboard.graph.cells.DiamondCell;
043: import org.lucane.applications.whiteboard.graph.cells.EllipseCell;
044: import org.lucane.applications.whiteboard.graph.cells.ImageCell;
045: import org.lucane.applications.whiteboard.graph.cells.RoundRectangleCell;
046: import org.lucane.applications.whiteboard.graph.cells.TextCell;
047:
048: public class MyGraph extends JGraph {
049: private GraphUndoManager undoManager;
050: private Color cellBorder = Color.BLACK;
051: private Color cellBackground = null;
052: private int lineWidth = 1;
053: private float[] dashPattern = new float[] { 1, 0 };
054: private Object[] clipboard = new Object[0];
055:
056: public MyGraph(GraphModel model) {
057: super (model);
058: this .undoManager = new GraphUndoManager();
059: model.addUndoableEditListener(undoManager);
060: }
061:
062: public void setModel(GraphModel model) {
063: super .setModel(model);
064: this .undoManager = new GraphUndoManager();
065: model.addUndoableEditListener(undoManager);
066: }
067:
068: public void resetUndoManager() {
069: this .undoManager = new GraphUndoManager();
070: this .getModel().addUndoableEditListener(undoManager);
071: }
072:
073: protected VertexView createVertexView(JGraph graph, CellMapper cm,
074: Object v) {
075: if (v instanceof EllipseCell)
076: return new JGraphEllipseView(v, this , cm);
077: else if (v instanceof DiamondCell)
078: return new JGraphDiamondView(v, this , cm);
079: else if (v instanceof RoundRectangleCell)
080: return new JGraphRoundRectView(v, this , cm);
081: else if (v instanceof ImageCell)
082: return new ScaledVertexView(v, this , cm);
083: else if ((v instanceof TextCell)
084: && ((TextCell) v).isMultiLined())
085: return new JGraphMultilineView(v, this , cm);
086: return super .createVertexView(graph, cm, v);
087: }
088:
089: public Object[] getAllCells() {
090: return getDescendants(getRoots());
091: }
092:
093: public boolean isGroup(Object cell) {
094: // Map the Cell to its View
095: CellView view = getGraphLayoutCache().getMapping(cell, false);
096: if (view != null)
097: return !view.isLeaf();
098:
099: return false;
100: }
101:
102: public void setCellBorder(Color color) {
103: this .cellBorder = color == null ? Color.BLACK : color;
104:
105: Object[] cells = getSelectionCells();
106: HashMap changes = new HashMap();
107: for (int i = 0; i < cells.length; i++) {
108: DefaultGraphCell cell = (DefaultGraphCell) cells[i];
109: AttributeMap attr = new AttributeMap();
110: GraphConstants.setBorderColor(attr, cellBorder);
111: GraphConstants.setLineColor(attr, cellBorder);
112: changes.put(cell, attr);
113: }
114: getModel().edit(changes, null, null, null);
115: }
116:
117: public Color getCellBorder() {
118: return this .cellBorder;
119: }
120:
121: public void setCellBackground(Color color) {
122: this .cellBackground = color;
123:
124: Object[] cells = getSelectionCells();
125: HashMap changes = new HashMap();
126: for (int i = 0; i < cells.length; i++) {
127: DefaultGraphCell cell = (DefaultGraphCell) cells[i];
128: AttributeMap attr = new AttributeMap();
129: GraphConstants.setOpaque(attr, cellBackground != null);
130: if (cellBackground != null)
131: GraphConstants.setBackground(attr, cellBackground);
132: changes.put(cell, attr);
133: }
134: getModel().edit(changes, null, null, null);
135: }
136:
137: public Color getCellBackground() {
138: return this .cellBackground;
139: }
140:
141: public void setLineWidth(int w) {
142: this .lineWidth = w;
143: Object[] cells = getSelectionCells();
144: HashMap changes = new HashMap();
145: for (int i = 0; i < cells.length; i++) {
146: DefaultGraphCell cell = (DefaultGraphCell) cells[i];
147: AttributeMap attr = new AttributeMap();
148: GraphConstants.setLineWidth(attr, lineWidth);
149: changes.put(cell, attr);
150: }
151: getModel().edit(changes, null, null, null);
152: }
153:
154: public int getLineWidth() {
155: return this .lineWidth;
156: }
157:
158: public void setDashPattern(float[] pattern) {
159: this .dashPattern = pattern;
160: Object[] cells = getSelectionCells();
161: HashMap changes = new HashMap();
162: for (int i = 0; i < cells.length; i++) {
163: DefaultGraphCell cell = (DefaultGraphCell) cells[i];
164: AttributeMap attr = new AttributeMap();
165: GraphConstants.setDashPattern(attr, pattern);
166: changes.put(cell, attr);
167: }
168: getModel().edit(changes, null, null, null);
169: }
170:
171: public float[] getDashPattern() {
172: return this .dashPattern;
173: }
174:
175: public void cut() {
176: Object[] cells = getSelectionCells();
177: if (cells != null)
178: cells = DefaultGraphModel.getDescendants(getModel(), cells)
179: .toArray();
180:
181: this .copy();
182: getModel().remove(cells);
183: }
184:
185: public void copy() {
186: Object[] cells = getSelectionCells();
187: clipboard = GraphUtils.cloneCells(this , cells);
188: System.out.println("copy: " + clipboard.length);
189: }
190:
191: public boolean paste() {
192: if (clipboard.length == 0)
193: return false;
194:
195: Object[] copy = GraphUtils.cloneCells(this , clipboard);
196: GraphUtils.changeTimeStamp(copy);
197: ParentMap parents = GraphUtils.createParentMap(copy);
198: Map attrs = GraphUtils.createAttributeMap(copy);
199:
200: getModel().insert(copy, attrs, null, parents, null);
201: System.out.println("paste: " + clipboard.length);
202: return true;
203: }
204:
205: public boolean undo() {
206: if (undoManager.canUndo()) {
207: undoManager.undo(getModel());
208: return true;
209: }
210: return false;
211: }
212:
213: public boolean redo() {
214: if (undoManager.canRedo()) {
215: undoManager.redo(getModel());
216: return true;
217: }
218: return false;
219: }
220: }
|