001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.applications.designstudio.domainmodel;
016:
017: import java.awt.Point;
018: import java.util.ArrayList;
019: import java.util.Hashtable;
020: import java.util.Map;
021:
022: import org.jgraph.JGraph;
023: import org.jgraph.graph.CellMapper;
024: import org.jgraph.graph.DefaultGraphCell;
025: import org.jgraph.graph.EdgeView;
026: import org.jgraph.graph.Port;
027: import org.jgraph.graph.VertexView;
028:
029: import com.metaboss.applications.designstudio.Application;
030: import com.metaboss.applications.designstudio.BaseGraphModel;
031: import com.metaboss.applications.designstudio.BaseGraphPanel;
032: import com.metaboss.applications.designstudio.BaseUserObject;
033: import com.metaboss.applications.designstudio.components.DesignGraph;
034: import com.metaboss.applications.designstudio.components.VertexCell;
035: import com.metaboss.applications.designstudio.userobjects.AssociationUserObject;
036: import com.metaboss.applications.designstudio.userobjects.AttributeUserObject;
037: import com.metaboss.applications.designstudio.userobjects.EntityGeneralizationUserObject;
038: import com.metaboss.applications.designstudio.userobjects.EntityUserObject;
039: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.Association;
040: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.AssociationRole;
041: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.Attribute;
042: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.Domain;
043: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.Entity;
044: import com.metaboss.sdlctools.models.metabossmodel.visualmodel.DomainEntitiesDiagram;
045:
046: /* Domain diagram panel class */
047:
048: public class DomainViewPanel extends BaseGraphPanel {
049: public final static int ADD_ENTITY = 1;
050: public final static int ADD_COMPOSITION = 2;
051: public final static int ADD_AGGREGATION = 3;
052: public final static int ADD_GENERALIZATION = 4;
053:
054: public DomainViewPanel(DomainEntitiesDiagram pDiagram)
055: throws Exception {
056: super (pDiagram.getDomain(), pDiagram);
057:
058: mGraph.beginUpdate();
059: mLoading = true;
060: try {
061: mGraph.setVertexHaveFields(true);
062: mGraph.setShowVertexFields(false);
063: mViewFieldsAction.setChecked(false);
064: } finally {
065: mGraph.stopUpdate(true);
066: mLoading = false;
067: }
068: }
069:
070: public DesignGraph createGraph() throws Exception {
071: return new DesignGraph(mModel) {
072: protected VertexView createVertexView(JGraph graph,
073: CellMapper cm, Object v) {
074: if (((VertexCell) v).getUserObject() instanceof EntityUserObject)
075: return new EntityVertexView(v, this , cm);
076: else
077: return super .createVertexView(graph, cm, v);
078: }
079:
080: protected EdgeView createEdgeView(JGraph graph,
081: CellMapper mapper, Object cell) {
082: return new DomainEdgeView(cell, graph, mapper);
083: }
084: };
085: }
086:
087: public BaseGraphModel createModel() throws Exception {
088: return new DomainViewModel((DomainEntitiesDiagram) mDiagram);
089: }
090:
091: // connect Edges
092: public void connectEdges(Port pFrom, Port pTo) {
093: //if (pFrom==null || pTo==null) return;
094:
095: Entity lFromEntity = null;
096: Entity lToEntity = null;
097:
098: if (pFrom != null) {
099: Object lFromObject = ((DefaultGraphCell) pFrom)
100: .getUserObject();
101: if (lFromObject instanceof EntityUserObject)
102: lFromEntity = ((EntityUserObject) lFromObject)
103: .getEntity();
104: }
105: if (pTo != null) {
106: Object lToObject = ((DefaultGraphCell) pTo).getUserObject();
107: if (lToObject instanceof EntityUserObject)
108: lToEntity = ((EntityUserObject) lToObject).getEntity();
109: }
110:
111: AssociationUserObject lAssociation = null;
112:
113: switch (mState) {
114: case ADD_COMPOSITION:
115: try {
116: lAssociation = AssociationUserObject
117: .addNewComposisiton(getDomain(), lFromEntity,
118: lToEntity);
119: } catch (Exception e) {
120: Application.processError(e);
121: }
122: setSelection(NO_SELECTION);
123: mGraph.repaint();
124: break;
125:
126: case ADD_AGGREGATION:
127: try {
128: lAssociation = AssociationUserObject.addNewAggregation(
129: getDomain(), lFromEntity, lToEntity);
130: } catch (Exception e) {
131: Application.processError(e);
132: }
133: setSelection(NO_SELECTION);
134: mGraph.repaint();
135: break;
136:
137: case ADD_GENERALIZATION:
138: try {
139: EntityUserObject
140: .setNewSuperType(lFromEntity, lToEntity);
141: ((DomainViewModel) mModel).addGeneralization(
142: lFromEntity, null);
143: } catch (Exception e) {
144: Application.processError(e);
145: }
146: setSelection(NO_SELECTION);
147: mGraph.repaint();
148: break;
149: }
150:
151: if (lAssociation != null) {
152: try {
153: ((DomainViewModel) mModel).addAssociationCell(
154: lAssociation.getAssociation(), null, null);
155: } catch (Exception e) {
156: Application.processError(e);
157: }
158: }
159: }
160:
161: // return domain
162: public Domain getDomain() {
163: Object lObject = getBOObject();
164: if (lObject != null && lObject instanceof Domain)
165: return (Domain) lObject;
166: else
167: return null;
168: }
169:
170: protected void processObjectInserted(BaseUserObject pObject)
171: throws Exception {
172: Object[] cells = mModel.getUserObjectCells(pObject);
173: if (cells.length == 0) {
174: if (pObject instanceof EntityUserObject) {
175: Entity lEntity = ((EntityUserObject) pObject)
176: .getEntity();
177: if (lEntity != null
178: && lEntity.getDomain().equals(getDomain()))
179: ((DomainViewModel) mModel).addEntityCell(lEntity,
180: new Point(0, 0));
181: } else if (pObject instanceof AssociationUserObject) {
182: Association lAssociation = (Association) pObject
183: .getBOObject();
184: if (lAssociation != null
185: && lAssociation.getDomain().equals(getDomain()))
186: ((DomainViewModel) mModel).addAssociationCell(
187: lAssociation, null, null);
188: } else if (pObject instanceof AttributeUserObject) {
189: Entity lEntity = ((Attribute) pObject.getBOObject())
190: .getEntity();
191: if (lEntity != null)
192: updateObject(lEntity);
193: }
194: }
195: }
196:
197: protected void processObjectEdited(BaseUserObject pObject)
198: throws Exception {
199: Entity lEntity = null;
200: Association lAssociation = null;
201: Map lViewMap = new Hashtable();
202:
203: if (pObject instanceof EntityUserObject
204: || pObject instanceof EntityGeneralizationUserObject) {
205: lEntity = (Entity) pObject.getBOObject();
206: if (lEntity != null
207: && lEntity.getDomain().equals(getDomain())) {
208: ((DomainViewModel) mModel)
209: .checkGeneralizationsForEntity(lEntity,
210: lViewMap);
211: updateModel(lViewMap);
212: }
213: } else if (pObject instanceof AssociationUserObject) {
214: lAssociation = (Association) pObject.getBOObject();
215: if (lAssociation != null
216: && lAssociation.getDomain().equals(getDomain())) {
217: ((DomainViewModel) mModel).checkAssociation(
218: lAssociation, lViewMap);
219: updateModel(lViewMap);
220: }
221: } else if (pObject instanceof AttributeUserObject) {
222: lEntity = ((Attribute) pObject.getBOObject()).getEntity();
223: if (lEntity != null)
224: updateObject(lEntity);
225: }
226: super .processObjectEdited(pObject);
227: }
228:
229: protected void processObjectDeleted(
230: Application.ObjectChangedEvent event) throws Exception {
231: /*if (pObject instanceof AttributeNodeUserObject)
232: updateAll();
233: else*/
234: super .processObjectDeleted(event);
235: }
236:
237: protected void fillToolBarActions(ArrayList pActionsList) {
238: super .fillToolBarActions(pActionsList);
239: pActionsList.add(new AddEntityAction());
240: pActionsList.add(new AddCompositionAction());
241: pActionsList.add(new AddAgregationAction());
242: pActionsList.add(new AddGeneralizationAction());
243: }
244:
245: // process selection
246: protected boolean processSelection(int pSelection, int X, int Y) {
247: switch (pSelection) {
248: case ADD_ENTITY:
249: addNewEntity(X, Y);
250: return true;
251: case ADD_COMPOSITION:
252: case ADD_AGGREGATION:
253: case ADD_GENERALIZATION:
254: Application.showWarning(Application
255: .getString("selectvertex_message"));
256: return true;
257: }
258: return false;
259: }
260:
261: // insert entity vertex into the model
262: private void addNewEntity(int X, int Y) {
263: try {
264: EntityUserObject lEntityObject = EntityUserObject
265: .addNewEntity(getDomain());
266: if (lEntityObject != null)
267: ((DomainViewModel) mModel).addEntityCell(lEntityObject
268: .getEntity(), new Point(X, Y));
269: } catch (Exception e) {
270: Application.processError(e);
271: }
272: }
273:
274: protected void fillActions(ArrayList pActionList) {
275: super .fillActions(pActionList);
276: pActionList.add(mViewEdgesCardinalityAction);
277: pActionList.add(mViewEdgesEndsNamesAction);
278: }
279:
280: // check object excepted by current diagram
281: public boolean isObjectExcepted(BaseUserObject pObject) {
282: boolean lResult = false;
283: if (pObject != null
284: && mModel != null
285: && (pObject instanceof EntityUserObject || pObject instanceof AssociationUserObject)) {
286: DomainViewModel lModel = (DomainViewModel) mModel;
287: DefaultGraphCell lCell = lModel.findElementCell(pObject);
288: if (lCell == null) {
289: if (pObject instanceof EntityUserObject) {
290: EntityUserObject lEntityUserObject = (EntityUserObject) pObject;
291: Entity lEntity = lEntityUserObject.getEntity();
292: if (lEntity != null
293: && lEntity.getDomain().equals(
294: lModel.getDomain())) {
295: lResult = true;
296: }
297: } else if (pObject instanceof AssociationUserObject) {
298: AssociationUserObject lAssociationUserObject = (AssociationUserObject) pObject;
299: Association lAssociation = lAssociationUserObject
300: .getAssociation();
301: if (lAssociation != null
302: && lAssociation.getDomain().equals(
303: lModel.getDomain())) {
304: java.util.List lList = lAssociation.getRoles();
305: AssociationRole lRoleA = (lList != null && lList
306: .size() > 0) ? (AssociationRole) lList
307: .get(0) : null;
308: AssociationRole lRoleB = (lList != null && lList
309: .size() > 1) ? (AssociationRole) lList
310: .get(1) : null;
311: DefaultGraphCell lCellA = lModel
312: .findEntityCell(lRoleA.getEntity());
313: DefaultGraphCell lCellB = lModel
314: .findEntityCell(lRoleB.getEntity());
315:
316: lResult = (lCellA != null && lCellB != null);
317: }
318: }
319: }
320: }
321: return lResult;
322: }
323:
324: // insert new object into the diagram
325: public void dropObject(BaseUserObject pObject, Point aLocation) {
326: if (pObject == null || mModel == null)
327: return;
328:
329: DomainViewModel lModel = (DomainViewModel) mModel;
330: if (pObject instanceof EntityUserObject) {
331: EntityUserObject lEntityUserObject = (EntityUserObject) pObject;
332: try {
333: lModel.addEntityCell(lEntityUserObject.getEntity(),
334: aLocation);
335: } catch (Exception e) {
336: e.printStackTrace();
337: }
338: } else if (pObject instanceof AssociationUserObject) {
339: AssociationUserObject lAssociationUserObject = (AssociationUserObject) pObject;
340: try {
341: lModel.addAssociationCell(lAssociationUserObject
342: .getAssociation(), null, null);
343: } catch (Exception e) {
344: e.printStackTrace();
345: }
346: }
347: }
348:
349: /* Actions */
350:
351: /* Add Entuty Action */
352:
353: public class AddEntityAction extends BaseSelectionAction {
354: public AddEntityAction() {
355: super (ADD_ENTITY, "New Entity",
356: Application.GRAPH_ENTITY_ICON);
357: }
358: }
359:
360: /* Add Composition Action */
361:
362: public class AddCompositionAction extends BaseSelectionAction {
363: public AddCompositionAction() {
364: super (ADD_COMPOSITION, "New Composition",
365: Application.GRAPH_COMPOSITION_ICON);
366: }
367: }
368:
369: /* Add Agregation Action */
370:
371: public class AddAgregationAction extends BaseSelectionAction {
372: public AddAgregationAction() {
373: super (ADD_AGGREGATION, "New Agregation",
374: Application.GRAPH_AGREGATION_ICON);
375: }
376: }
377:
378: /* Add Generalization Action */
379:
380: public class AddGeneralizationAction extends BaseSelectionAction {
381: public AddGeneralizationAction() {
382: super (ADD_GENERALIZATION, "New Generalization",
383: Application.GRAPH_GENERALIZATION_ICON);
384: }
385: }
386: }
|