001: /*
002: * argun 1.0
003: * Web 2.0 delivery framework
004: * Copyright (C) 2007 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.web.webdiagram;
024:
025: import java.awt.Color;
026: import java.awt.event.ActionEvent;
027: import java.net.URL;
028: import java.util.Iterator;
029:
030: import javax.swing.AbstractAction;
031: import javax.swing.ImageIcon;
032: import javax.swing.JMenu;
033: import javax.swing.JOptionPane;
034: import javax.swing.JPopupMenu;
035: import javax.swing.JToolBar;
036: import javax.swing.tree.TreeNode;
037:
038: import org.jgraph.graph.CellHandle;
039: import org.jgraph.graph.CellViewFactory;
040: import org.jgraph.graph.CellViewRenderer;
041: import org.jgraph.graph.DefaultPort;
042: import org.jgraph.graph.EdgeView;
043: import org.jgraph.graph.GraphContext;
044: import org.jgraph.graph.PortRenderer;
045: import org.jgraph.graph.PortView;
046:
047: import biz.hammurapi.diagram.DiagramApplet;
048: import biz.hammurapi.diagram.DiagramEdge;
049: import biz.hammurapi.diagram.DiagramModel;
050:
051: public class WebDiagramModel extends DiagramModel {
052:
053: private transient DiagramApplet applet;
054:
055: public WebDiagramModel(DiagramApplet applet,
056: boolean isCompositeService) {
057: super (true);
058: this .applet = applet;
059: this .isCompositeService = isCompositeService;
060: }
061:
062: private boolean isCompositeService;
063:
064: /**
065: * Model mode - Composite Service (true) or Web diagram (false)
066: * @return
067: */
068: public boolean isCompositeService() {
069: return isCompositeService;
070: }
071:
072: public boolean acceptsSource(Object edge, Object port) {
073: return edge instanceof DiagramEdge
074: && ((DiagramEdge) edge).acceptsSource(port);
075: }
076:
077: public boolean acceptsTarget(Object edge, Object port) {
078: return edge instanceof DiagramEdge
079: && ((DiagramEdge) edge).acceptsTarget(port);
080: }
081:
082: public Activity createPage() {
083: return new Activity(this , null);
084: }
085:
086: public Entity createEntity() {
087: return new Entity(this , null);
088: }
089:
090: public DiagramEdge createEdge(DefaultPort source, DefaultPort target) {
091: if (source == null || target == null
092: || source.getParent() == target.getParent()) {
093: return null;
094: }
095:
096: TreeNode sourceParent = source.getParent();
097: TreeNode targetParent = target.getParent();
098:
099: if (sourceParent instanceof Operation
100: && source.getUserObject() == null) {
101: if (targetParent instanceof Service) {
102: return new ServiceCall(this , null);
103: }
104:
105: if (targetParent instanceof Entity) {
106: return new Update(this , null);
107: }
108: }
109:
110: if (sourceParent instanceof Start) {
111: if (targetParent instanceof Stop
112: || (targetParent instanceof Service && Service.INPUT_PORT
113: .equals(target.getUserObject()))) {
114: return new ServiceFlow(this , null);
115: }
116: }
117:
118: if (sourceParent instanceof Service) {
119: if (targetParent instanceof Stop
120: || (targetParent instanceof Service && Service.INPUT_PORT
121: .equals(target.getUserObject()))) {
122: if (Service.OUTPUT_PORT.equals(source.getUserObject())) {
123: return new ServiceFlow(this , null);
124: }
125:
126: if (Service.ERROR_PORT.equals(source.getUserObject())) {
127: return new ErrorFlow(this , null);
128: }
129: }
130: }
131:
132: if (sourceParent instanceof Activity) {
133: if (targetParent instanceof Activity) {
134: if (source.getUserObject() == null
135: && target.getUserObject() == null) {
136: return new Link(this , null);
137: }
138:
139: if (Activity.CHILD_PORT.equals(source.getUserObject())
140: && Activity.PARENT_PORT.equals(target
141: .getUserObject())) {
142: return new Reference(this , null);
143: }
144:
145: if (Activity.CHILD_PORT.equals(target.getUserObject())
146: && Activity.PARENT_PORT.equals(source
147: .getUserObject())) {
148: Iterator it = source.getEdges().iterator();
149: while (it.hasNext()) {
150: Object edge = it.next();
151: if (edge instanceof Inheritance) {
152: return null;
153: }
154: }
155: return new Inheritance(this , null);
156: }
157: }
158: } else if (sourceParent instanceof Entity) {
159: if (targetParent instanceof Operation) {
160: if (target.getUserObject() == null) {
161: return new Query(this , null);
162: }
163: } else if (targetParent instanceof Entity) {
164: return new Association(this , null);
165: }
166: } else if (sourceParent instanceof Role) {
167: if (targetParent instanceof Role) {
168: Iterator it = source.getEdges().iterator();
169: while (it.hasNext()) {
170: Object next = it.next();
171: if (next instanceof RoleGeneralization
172: && ((RoleGeneralization) next).getTarget() == target) {
173: return null;
174: }
175: }
176: return new RoleGeneralization(this , null);
177: }
178:
179: if (targetParent instanceof Operation
180: && target.getUserObject() == null) {
181: Iterator it = source.getEdges().iterator();
182: while (it.hasNext()) {
183: Object next = it.next();
184: if (next instanceof OperationPermission
185: && ((OperationPermission) next).getTarget() == target) {
186: return null;
187: }
188: }
189: return new OperationPermission(this , null);
190: }
191:
192: if (targetParent instanceof Entity) {
193: Iterator it = source.getEdges().iterator();
194: while (it.hasNext()) {
195: Object next = it.next();
196: if (next instanceof EntityPermission
197: && ((EntityPermission) next).getTarget() == target) {
198: return null;
199: }
200: }
201: return new EntityPermission(this , null);
202: }
203:
204: }
205:
206: return null;
207: }
208:
209: public DiagramApplet getApplet() {
210: return applet;
211: }
212:
213: public void populatePopupMenu(final DiagramApplet applet,
214: final java.awt.Point pt, JPopupMenu menu) {
215: JMenu subMenu = new JMenu("New");
216: menu.add(subMenu);
217:
218: if (!isCompositeService) {
219: URL activityUrl = getClass().getClassLoader().getResource(
220: "biz/hammurapi/diagram/images/activity.gif");
221: ImageIcon activityIcon = new ImageIcon(activityUrl);
222: subMenu.add(new AbstractAction("Activity", activityIcon) {
223: public void actionPerformed(ActionEvent ev) {
224: applet.insert(pt, new Activity(
225: WebDiagramModel.this , null));
226: }
227: });
228: }
229:
230: URL entityUrl = getClass().getClassLoader().getResource(
231: "biz/hammurapi/diagram/images/entity.gif");
232: ImageIcon entityIcon = new ImageIcon(entityUrl);
233: subMenu.add(new AbstractAction("Entity", entityIcon) {
234: public void actionPerformed(ActionEvent ev) {
235: applet.insert(pt,
236: new Entity(WebDiagramModel.this , null));
237: }
238: });
239:
240: if (!isCompositeService) {
241: URL roleUrl = getClass().getClassLoader().getResource(
242: "biz/hammurapi/diagram/images/role.gif");
243: ImageIcon roleIcon = new ImageIcon(roleUrl);
244: subMenu.add(new AbstractAction("Role", roleIcon) {
245: public void actionPerformed(ActionEvent ev) {
246: applet.insert(pt, new Role(WebDiagramModel.this ,
247: null));
248: }
249: });
250: }
251:
252: URL serviceUrl = getClass().getClassLoader().getResource(
253: "biz/hammurapi/diagram/images/service.gif");
254: ImageIcon serviceIcon = new ImageIcon(serviceUrl);
255: subMenu.add(new AbstractAction("Service", serviceIcon) {
256: public void actionPerformed(ActionEvent ev) {
257: applet.insert(pt, new Service(WebDiagramModel.this ,
258: null));
259: }
260: });
261:
262: }
263:
264: public void populateToolbar(final DiagramApplet applet,
265: JToolBar toolbar) {
266: if (!isCompositeService) {
267: // Activity
268: URL activityUrl = getClass().getClassLoader().getResource(
269: "biz/hammurapi/diagram/images/activity.gif");
270: ImageIcon activityIcon = new ImageIcon(activityUrl);
271: toolbar.add(new AbstractAction("Insert activity",
272: activityIcon) {
273: public void actionPerformed(ActionEvent e) {
274: applet.insert(new java.awt.Point(20, 20),
275: new Activity(WebDiagramModel.this , null)); // TODO - iterate over cells to avoid inserting step over other steps.
276: }
277: });
278: }
279:
280: // Entity
281: URL entityUrl = getClass().getClassLoader().getResource(
282: "biz/hammurapi/diagram/images/entity.gif");
283: ImageIcon entityIcon = new ImageIcon(entityUrl);
284: toolbar.add(new AbstractAction("Insert entity", entityIcon) {
285: public void actionPerformed(ActionEvent e) {
286: applet.insert(new java.awt.Point(20, 20), new Entity(
287: WebDiagramModel.this , null)); // TODO - iterate over cells to avoid inserting step over other steps.
288: }
289: });
290:
291: if (!isCompositeService) {
292: // Role
293: URL roleUrl = getClass().getClassLoader().getResource(
294: "biz/hammurapi/diagram/images/role.gif");
295: ImageIcon roleIcon = new ImageIcon(roleUrl);
296: toolbar.add(new AbstractAction("Insert role", roleIcon) {
297: public void actionPerformed(ActionEvent e) {
298: applet.insert(new java.awt.Point(20, 20), new Role(
299: WebDiagramModel.this , null)); // TODO - iterate over cells to avoid inserting step over other steps.
300: }
301: });
302: }
303:
304: // Service
305: URL serviceUrl = getClass().getClassLoader().getResource(
306: "biz/hammurapi/diagram/images/service.gif");
307: ImageIcon serviceIcon = new ImageIcon(serviceUrl);
308: toolbar.add(new AbstractAction("Insert service", serviceIcon) {
309: public void actionPerformed(ActionEvent e) {
310: applet.insert(new java.awt.Point(20, 20), new Service(
311: WebDiagramModel.this , null)); // TODO - iterate over cells to avoid inserting step over other steps.
312: }
313: });
314: }
315:
316: private static transient PortRenderer customRenderer = new PortRenderer() {
317:
318: {
319: setForeground(Color.RED);
320: }
321: };
322:
323: public CellViewFactory getCellViewFactory() {
324: return new biz.hammurapi.diagram.DiagramCellViewFactory() {
325:
326: protected PortView createPortView(Object cell) {
327: PortView ret = new PortView(cell) {
328:
329: public CellViewRenderer getRenderer() {
330: return cell instanceof DefaultPort
331: && Service.ERROR_PORT
332: .equals(((DefaultPort) cell)
333: .getUserObject()) ? customRenderer
334: : super .getRenderer();
335: }
336: };
337: return ret;
338: }
339:
340: protected EdgeView createEdgeView(Object cell) {
341:
342: return new EdgeView(cell) {
343:
344: /**
345: * Returns a cell handle for the view.
346: */
347: public CellHandle getHandle(GraphContext context) {
348: return new DiagramEdgeHandle(this , context);
349: }
350:
351: };
352: }
353: };
354:
355: }
356:
357: public boolean canDelete(Object[] selectionCells) {
358: for (int i = 0; i < selectionCells.length; ++i) {
359: if (selectionCells[i] instanceof Start
360: || selectionCells[i] instanceof Stop) {
361: return false;
362: }
363: }
364:
365: return super .canDelete(selectionCells);
366: }
367:
368: public void remove(Object[] roots) {
369: for (int i = 0; i < roots.length; ++i) {
370: if (roots[i] instanceof Start || roots[i] instanceof Stop) {
371: JOptionPane.showMessageDialog(getApplet(),
372: "Start and end nodes cannot be deleted",
373: "Warning", JOptionPane.WARNING_MESSAGE);
374: return;
375: }
376: }
377:
378: super .remove(roots);
379: }
380:
381: public void loadDefault() {
382: if (isCompositeService) {
383: applet.insert(new java.awt.Point(20, 20), new Start(
384: WebDiagramModel.this , null));
385: applet.insert(new java.awt.Point(200, 20), new Stop(
386: WebDiagramModel.this, null));
387: }
388: }
389:
390: }
|