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.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.Port;
026: import org.jgraph.graph.VertexView;
027:
028: import com.metaboss.applications.designstudio.Application;
029: import com.metaboss.applications.designstudio.BaseGraphModel;
030: import com.metaboss.applications.designstudio.BaseGraphPanel;
031: import com.metaboss.applications.designstudio.BaseUserObject;
032: import com.metaboss.applications.designstudio.Application.ObjectChangedEvent;
033: import com.metaboss.applications.designstudio.components.DesignGraph;
034: import com.metaboss.applications.designstudio.components.VertexCell;
035: import com.metaboss.applications.designstudio.userobjects.StateUserObject;
036: import com.metaboss.applications.designstudio.userobjects.TransitionUserObject;
037: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.Entity;
038: import com.metaboss.sdlctools.models.metabossmodel.statemachinemodel.State;
039: import com.metaboss.sdlctools.models.metabossmodel.statemachinemodel.StateMachine;
040: import com.metaboss.sdlctools.models.metabossmodel.statemachinemodel.StateType;
041: import com.metaboss.sdlctools.models.metabossmodel.statemachinemodel.StateTypeEnum;
042: import com.metaboss.sdlctools.models.metabossmodel.statemachinemodel.Transition;
043: import com.metaboss.sdlctools.models.metabossmodel.visualmodel.EntityStateDiagram;
044:
045: /* Entity States diagram container panel class */
046:
047: public class StatesPanel extends BaseGraphPanel {
048: public final static int ADD_STATE = 1;
049: public final static int ADD_STARTSTATE = 2;
050: public final static int ADD_FINALSTATE = 3;
051: public final static int ADD_TRANSITION = 4;
052:
053: private AddStartStateAction mAddStartStateAction;
054:
055: public StatesPanel(EntityStateDiagram pDiagram) throws Exception {
056: super (pDiagram.getEntity(), pDiagram);
057:
058: mGraph.beginUpdate();
059: try {
060: mGraph.setShowClassVertex(false);
061: mGraph.setShowEdgesLabels(false);
062: mViewEdgesCaptionsAction.setChecked(false);
063: } finally {
064: mGraph.stopUpdate(false);
065: }
066: setActionsState();
067: }
068:
069: public DesignGraph createGraph() throws Exception {
070: return new DesignGraph(mModel) {
071: protected VertexView createVertexView(JGraph graph,
072: CellMapper cm, Object v) {
073: if (((VertexCell) v).getUserObject() instanceof StateUserObject)
074: return new StateVertexView(v, this , cm);
075: else
076: return super .createVertexView(graph, cm, v);
077: }
078: };
079: }
080:
081: public BaseGraphModel createModel() throws Exception {
082: return new StatesModel((EntityStateDiagram) mDiagram);
083: }
084:
085: // return entity
086: public Entity getEntity() {
087: Object lObject = getBOObject();
088: if (lObject != null && lObject instanceof Entity)
089: return (Entity) lObject;
090: else
091: return null;
092: }
093:
094: public StateMachine getStateMachine() {
095: Entity lEntity = getEntity();
096: return (lEntity != null) ? lEntity.getStateMachine() : null;
097: }
098:
099: // connect Edges
100: public void connectEdges(Port pFrom, Port pTo) {
101: State lFromState = null;
102: State lToState = null;
103:
104: if (pFrom != null) {
105: Object lFromObject = ((DefaultGraphCell) pFrom)
106: .getUserObject();
107: if (lFromObject instanceof StateUserObject)
108: lFromState = ((StateUserObject) lFromObject).getState();
109: }
110: if (pTo != null) {
111: Object lToObject = ((DefaultGraphCell) pTo).getUserObject();
112: if (lToObject instanceof StateUserObject)
113: lToState = ((StateUserObject) lToObject).getState();
114: }
115:
116: TransitionUserObject lTransition = null;
117: switch (mState) {
118: case ADD_TRANSITION:
119: try {
120: lTransition = TransitionUserObject.addNewTransition(
121: getStateMachine(), lFromState, lToState);
122: } catch (Exception e) {
123: Application.processError(e);
124: }
125: setSelection(NO_SELECTION);
126: mGraph.repaint();
127: break;
128: }
129:
130: if (lTransition != null) {
131: try {
132: ((StatesModel) mModel).addTransition(lTransition
133: .getStateTransition());
134: } catch (Exception e) {
135: Application.processError(e);
136: }
137: }
138: }
139:
140: protected void processObjectInserted(BaseUserObject pObject)
141: throws Exception {
142: Object[] cells = mModel.getUserObjectCells(pObject);
143: if (cells.length == 0) {
144: if (pObject instanceof StateUserObject) {
145: State lState = ((StateUserObject) pObject).getState();
146: if (lState != null
147: && lState.getStateMachine().equals(
148: getStateMachine()))
149: ((StatesModel) mModel).addStateCell(lState,
150: new Point(0, 0));
151: } else if (pObject instanceof TransitionUserObject) {
152: Transition lTransition = (Transition) pObject
153: .getBOObject();
154: if (lTransition != null
155: && lTransition.getStateMachine().equals(
156: getStateMachine()))
157: ((StatesModel) mModel).addTransition(lTransition);
158: }
159: }
160: setActionsState();
161: }
162:
163: protected void processObjectEdited(BaseUserObject pObject)
164: throws Exception {
165: if (pObject instanceof TransitionUserObject) {
166: Transition lTransition = (Transition) pObject.getBOObject();
167: if (lTransition != null
168: && lTransition.getStateMachine().equals(
169: getStateMachine())) {
170: Map lViewMap = new Hashtable();
171: ((StatesModel) mModel).checkTransition(lTransition,
172: lViewMap);
173: updateModel(lViewMap);
174: }
175: } else if (pObject instanceof StateUserObject) {
176: ((StatesModel) mModel)
177: .checkState(((StateUserObject) pObject).getState());
178: setActionsState();
179: }
180: super .processObjectEdited(pObject);
181: }
182:
183: protected void processObjectDeleted(ObjectChangedEvent event)
184: throws Exception {
185: super .processObjectDeleted(event);
186: setActionsState();
187: }
188:
189: protected void fillToolBarActions(ArrayList pActionsList) {
190: super .fillToolBarActions(pActionsList);
191: pActionsList.add(new AddStateAction());
192: mAddStartStateAction = new AddStartStateAction();
193: pActionsList.add(mAddStartStateAction);
194: //pActionsList.add(new AddStartStateAction());
195: pActionsList.add(new AddFinalStateAction());
196: pActionsList.add(new AddTransitionAction());
197: }
198:
199: // process selection
200: protected boolean processSelection(int pSelection, int X, int Y) {
201: switch (pSelection) {
202: case ADD_STATE:
203: addNewState(X, Y);
204: return true;
205: case ADD_STARTSTATE:
206: addNewExtraState(X, Y, StateTypeEnum.INITIAL);
207: return true;
208: case ADD_FINALSTATE:
209: addNewExtraState(X, Y, StateTypeEnum.FINAL);
210: return true;
211: case ADD_TRANSITION:
212: Application.showWarning(Application
213: .getString("selectvertex_message"));
214: return true;
215: }
216: return false;
217: }
218:
219: // insert entity vertex into the model
220: private void addNewState(int X, int Y) {
221: try {
222: StateMachine lStateMachine = getStateMachine();
223: if (lStateMachine != null) {
224: StateUserObject lObject = StateUserObject
225: .addNewState(getStateMachine());
226: if (lObject != null)
227: ((StatesModel) mModel).addStateCell(lObject
228: .getState(), new Point(X, Y));
229: }
230: } catch (Exception e) {
231: Application.processError(e);
232: }
233: }
234:
235: private void addNewExtraState(int X, int Y, StateType pType) {
236: try {
237: StateUserObject lObject = StateUserObject.createNewState(
238: getStateMachine(), pType);
239: if (lObject != null)
240: ((StatesModel) mModel).addStateCell(lObject.getState(),
241: new Point(X, Y));
242: } catch (Exception e) {
243: Application.processError(e);
244: }
245: }
246:
247: private void setActionsState() {
248: if (!mLoading && mAddStartStateAction != null) {
249: DefaultGraphCell lCell = ((StatesModel) mModel)
250: .findInitialState();
251: mAddStartStateAction.setEnabled(lCell == null);
252: }
253: }
254:
255: // check object excepted by current diagram
256: public boolean isObjectExcepted(BaseUserObject pObject) {
257: boolean lResult = false;
258: if (pObject != null
259: && mModel != null
260: && (pObject instanceof StateUserObject || pObject instanceof TransitionUserObject)) {
261: StatesModel lModel = (StatesModel) mModel;
262: DefaultGraphCell lCell = lModel.findElementCell(pObject);
263: if (lCell == null) {
264: if (pObject instanceof StateUserObject) {
265: StateUserObject lStateUserObject = (StateUserObject) pObject;
266: State lState = ((StateUserObject) lStateUserObject)
267: .getState();
268: if (lState != null
269: && lModel.getEntity().equals(
270: lState.getStateMachine()
271: .getEntity())) {
272: lResult = true;
273: if (lState.getType() == StateTypeEnum.INITIAL) {
274: DefaultGraphCell lStartCell = lModel
275: .findInitialState();
276: if (lStartCell != null)
277: lResult = false;
278: }
279: }
280: } else if (pObject instanceof TransitionUserObject) {
281: TransitionUserObject lTransitionUserObject = (TransitionUserObject) pObject;
282: Transition lTransition = lTransitionUserObject
283: .getStateTransition();
284: if (lTransition != null
285: && lModel.getEntity().equals(
286: lTransition.getStateMachine()
287: .getEntity())) {
288: DefaultGraphCell lCellA = lModel
289: .findElementCell(lTransition
290: .getStartState());
291: DefaultGraphCell lCellB = lModel
292: .findElementCell(lTransition
293: .getEndState());
294: lResult = (lCellA != null && lCellB != null);
295: }
296: }
297: }
298: }
299: return lResult;
300: }
301:
302: // insert new object into the diagram
303: public void dropObject(BaseUserObject pObject, Point aLocation) {
304: if (pObject == null || mModel == null)
305: return;
306:
307: StatesModel lModel = (StatesModel) mModel;
308: if (pObject instanceof StateUserObject) {
309: StateUserObject lStateUserObject = (StateUserObject) pObject;
310: try {
311: lModel.addStateCell(lStateUserObject.getState(),
312: aLocation);
313: } catch (Exception e) {
314: e.printStackTrace();
315: }
316: } else if (pObject instanceof TransitionUserObject) {
317: TransitionUserObject lTransitionUserObject = (TransitionUserObject) pObject;
318: try {
319: lModel.addTransition(lTransitionUserObject
320: .getStateTransition());
321: } catch (Exception e) {
322: e.printStackTrace();
323: }
324: }
325: }
326:
327: /* Actions */
328:
329: /* Add State Action */
330:
331: public class AddStateAction extends BaseSelectionAction {
332: public AddStateAction() {
333: super (ADD_STATE, "New State", Application.STATE_ICON);
334: }
335: }
336:
337: /* Add Start State Action */
338:
339: public class AddStartStateAction extends BaseSelectionAction {
340: public AddStartStateAction() {
341: super (ADD_STARTSTATE, "New Initial State",
342: Application.GRAPH_INITIALSTATE_ICON);
343: }
344: }
345:
346: /* Add Final State Action */
347:
348: public class AddFinalStateAction extends BaseSelectionAction {
349: public AddFinalStateAction() {
350: super (ADD_FINALSTATE, "New Final State",
351: Application.GRAPH_FINALSTATE_ICON);
352: }
353: }
354:
355: /* Add Transition Action */
356:
357: public class AddTransitionAction extends BaseSelectionAction {
358: public AddTransitionAction() {
359: super (ADD_TRANSITION, "New Transition",
360: Application.GRAPH_ASSOCIATION_ICON);
361: }
362: }
363: }
|