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.statesmodel;
016:
017: import java.awt.Color;
018: import java.awt.Point;
019: import java.util.Map;
020:
021: import javax.swing.Icon;
022: import javax.swing.ImageIcon;
023:
024: import org.jgraph.graph.DefaultEdge;
025: import org.jgraph.graph.DefaultGraphCell;
026: import org.jgraph.graph.DefaultPort;
027: import org.jgraph.graph.GraphConstants;
028:
029: import com.metaboss.applications.designstudio.Application;
030: import com.metaboss.applications.designstudio.BaseGraphModel;
031: import com.metaboss.applications.designstudio.icons.IconsHolder;
032: import com.metaboss.applications.designstudio.userobjects.StateUserObject;
033: import com.metaboss.applications.designstudio.userobjects.TransitionUserObject;
034: import com.metaboss.sdlctools.models.metabossmodel.ModelElement;
035: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.Entity;
036: import com.metaboss.sdlctools.models.metabossmodel.statemachinemodel.State;
037: import com.metaboss.sdlctools.models.metabossmodel.statemachinemodel.StateTypeEnum;
038: import com.metaboss.sdlctools.models.metabossmodel.statemachinemodel.Transition;
039: import com.metaboss.sdlctools.models.metabossmodel.visualmodel.DiagramModelElement;
040: import com.metaboss.sdlctools.models.metabossmodel.visualmodel.EntityStateDiagram;
041:
042: /* Entity States diagram Model class */
043:
044: public class StatesModel extends BaseGraphModel {
045: public static ImageIcon sInitialStateIcon = IconsHolder
046: .loadIcon("initialstate.gif");
047: public static ImageIcon sFinalStateIcon = IconsHolder
048: .loadIcon("finalstate.gif");
049:
050: /** Constructor. */
051: public StatesModel(EntityStateDiagram pDiagram) throws Exception {
052: super (pDiagram.getEntity(), pDiagram);
053: }
054:
055: // return entity
056: public Entity getEntity() {
057: Object lObject = getBOObject();
058: if (lObject != null && lObject instanceof Entity)
059: return (Entity) lObject;
060: else
061: return null;
062: }
063:
064: // get diagram interface
065: /*public Diagram getDiaram() throws Exception
066: {
067: Entity lEntity = getEntity();
068: if (lEntity!=null)
069: {
070: Object[] lDigrams = lEntity.getEntityStateDiagrams().toArray();
071: if (lDigrams.length>0)
072: return (Diagram)lDigrams[0];
073: else
074: {
075: Diagram lDiagram = null;
076: Application.beginTransaction();
077: try
078: {
079: lDiagram = getPackage().getVisualModel().getEntityStateDiagram().createEntityStateDiagram();
080: lDiagram.setName("EntityStateDiagram");
081: lEntity.getEntityStateDiagrams().add(lDiagram);
082: Application.commit();
083: }
084: finally
085: {
086: if (Application.isInTransaction()) Application.rollback();
087: }
088: return lDiagram;
089: }
090: }
091: return null;
092: }*/
093:
094: // load graph model
095: public void loadModel() throws Exception {
096: super .loadModel();
097:
098: EntityStateDiagram lDiagram = (EntityStateDiagram) mDiagram;
099: Object[] lElements = lDiagram.getElements().toArray();
100:
101: // States cells
102: for (int i = 0; i < lElements.length; i++) {
103: DiagramModelElement lElement = (DiagramModelElement) lElements[i];
104: ModelElement lModelElement = lElement.getModelElement();
105: if (lModelElement != null && lModelElement instanceof State) {
106: State lState = (State) lModelElement;
107: addStateCell(lState, getPosition(lState));
108: }
109: }
110: // Associations
111: for (int i = 0; i < lElements.length; i++) {
112: DiagramModelElement lElement = (DiagramModelElement) lElements[i];
113: ModelElement lModelElement = lElement.getModelElement();
114: if (lModelElement != null
115: && lModelElement instanceof Transition) {
116: Transition lTransition = (Transition) lModelElement;
117: addTransition(lTransition);
118: }
119: }
120: }
121:
122: // add state cell
123: public DefaultGraphCell addStateCell(State pState, Point lPosition)
124: throws Exception {
125: return addVertex(new StateUserObject(pState), lPosition, null,
126: createStateStyle(pState));
127: }
128:
129: // create State vertex Style
130: public Map createStateStyle(State pState) throws Exception {
131: Map lResult = getVertexStyle();
132: if (pState != null) {
133: Icon lIcon = getStateIcon(pState);
134: if (lIcon != null)
135: GraphConstants.setIcon(lResult, lIcon);
136: }
137: return lResult;
138: }
139:
140: public Icon getStateIcon(State pState) {
141: if (pState != null) {
142: if (pState.getType() == StateTypeEnum.FINAL)
143: return sFinalStateIcon;
144: else if (pState.getType() == StateTypeEnum.INITIAL)
145: return sInitialStateIcon;
146: }
147: return null;
148: }
149:
150: public void checkState(State pState) throws Exception {
151: DefaultGraphCell lCell = findElementCell(pState);
152: if (lCell != null) {
153: Icon lIcon = getStateIcon(pState);
154: if (lIcon != null)
155: GraphConstants.setIcon(lCell.getAttributes(), lIcon);
156: else
157: lCell.getAttributes().remove(GraphConstants.ICON);
158: }
159: }
160:
161: // add transition edge
162: public void addTransition(Transition pTransition) throws Exception {
163: TransitionUserObject lUserObject = new TransitionUserObject(
164: pTransition);
165: java.util.List lPoints = loadEdgePoints(lUserObject
166: .getBOObject());
167: String lFrom = (pTransition.getStartState() != null) ? pTransition
168: .getStartState().refMofId()
169: : null;
170: String lTo = (pTransition.getEndState() != null) ? pTransition
171: .getEndState().refMofId() : null;
172:
173: addEdge(lUserObject, null, lPoints, lFrom, lTo,
174: getAssociationStyle());
175: }
176:
177: // check transition
178: public void checkTransition(Transition pTransition, Map pViewMap)
179: throws Exception {
180: DefaultEdge lTransitionEdge = (DefaultEdge) findElementCell(pTransition);
181: //if (lTransitionEdge==null)
182: // addTransition(pTransition);
183: //else
184: if (lTransitionEdge != null) {
185: State lStartState = pTransition.getStartState();
186: State lEndState = pTransition.getEndState();
187: if (lStartState != null && lEndState != null) {
188: Object lFromPort = getPort(lStartState.refMofId());
189: Object lToPort = getPort(lEndState.refMofId());
190:
191: lTransitionEdge.setSource(lFromPort);
192: lTransitionEdge.setTarget(lToPort);
193:
194: java.util.List lList = GraphConstants
195: .clonePoints(GraphConstants
196: .getPoints(lTransitionEdge
197: .getAttributes()));
198: Point lPoint = GraphConstants
199: .getLabelPosition(lTransitionEdge
200: .getAttributes());
201: GraphConstants.setPoints(lTransitionEdge
202: .getAttributes(), lList);
203: GraphConstants.setLabelPosition(lTransitionEdge
204: .getAttributes(), lPoint);
205:
206: pViewMap.put(lTransitionEdge, lTransitionEdge
207: .getAttributes());
208: }
209: }
210: }
211:
212: public DefaultGraphCell findInitialState() {
213: for (int i = 0; i < getRootCount(); i++) {
214: DefaultGraphCell lObject = (DefaultGraphCell) getRootAt(i);
215: Object lCellUserObject = lObject.getUserObject();
216: if (lCellUserObject instanceof StateUserObject) {
217: State lState = ((StateUserObject) lCellUserObject)
218: .getState();
219: if (lState != null
220: && lState.getType() == StateTypeEnum.INITIAL)
221: return lObject;
222: }
223: }
224: return null;
225: }
226:
227: // process cell change
228: protected void processCellChange(DefaultGraphCell pCell) {
229: super .processCellChange(pCell);
230: if (pCell == null)
231: return;
232:
233: Object lChangedCellUserObject = pCell.getUserObject();
234: if (lChangedCellUserObject instanceof TransitionUserObject) {
235: Transition lTransition = ((TransitionUserObject) lChangedCellUserObject)
236: .getStateTransition();
237: Object lSourceStateObject = ((DefaultEdge) pCell)
238: .getSource();
239: Object lTargetStateObject = ((DefaultEdge) pCell)
240: .getTarget();
241: try {
242: State lStartState = null;
243: State lStopState = null;
244: if (lSourceStateObject != null) {
245: Object lObject = ((DefaultPort) lSourceStateObject)
246: .getUserObject();
247: if (lObject != null
248: && lObject instanceof StateUserObject)
249: lStartState = ((StateUserObject) lObject)
250: .getState();
251: }
252: if (lTargetStateObject != null) {
253: Object lObject = ((DefaultPort) lTargetStateObject)
254: .getUserObject();
255: if (lObject != null
256: && lObject instanceof StateUserObject)
257: lStopState = ((StateUserObject) lObject)
258: .getState();
259: }
260:
261: Application.beginTransaction();
262: try {
263: lTransition.setStartState(lStartState);
264: lTransition.setEndState(lStopState);
265: Application.commit();
266: Application
267: .fireRefreshDiagram(
268: mDiagram,
269: (TransitionUserObject) lChangedCellUserObject);
270: } finally {
271: Application.checkAndRollback();
272: }
273: } catch (Exception e) {
274: e.printStackTrace();
275: }
276: }
277: }
278:
279: public Map getVertexStyle() {
280: Map lStyle = super .getVertexStyle();
281: GraphConstants.setBackground(lStyle, Color.yellow);
282: return lStyle;
283: }
284:
285: /*protected Object[] getVertexesObjects()
286: {
287: Entity lEntity = getEntity();
288: if (lEntity!=null && getEntity().getStateMachine()!=null)
289: return getEntity().getStateMachine().getStates().toArray();
290: else
291: return null;
292: }*/
293: }
|