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.userobjects;
016:
017: import java.awt.event.ActionEvent;
018: import java.util.ArrayList;
019: import java.util.Iterator;
020: import java.util.SortedSet;
021:
022: import javax.swing.JTabbedPane;
023:
024: import com.metaboss.applications.designstudio.Application;
025: import com.metaboss.applications.designstudio.BaseAction;
026: import com.metaboss.applications.designstudio.BasePropertiesDialog;
027: import com.metaboss.applications.designstudio.BaseUserObject;
028: import com.metaboss.applications.designstudio.attributestable.AttributesViewPanel;
029: import com.metaboss.applications.designstudio.auxilarydialogs.DiagramSelectDialog;
030: import com.metaboss.applications.designstudio.components.SeparatorAction;
031: import com.metaboss.applications.designstudio.constraintstable.ConstraintsViewPanel;
032: import com.metaboss.applications.designstudio.propertiesdialogs.EntityPropertiesDialog;
033: import com.metaboss.applications.designstudio.propertiesview.PropertiesTableModel;
034: import com.metaboss.applications.designstudio.statesmodel.StatesPanel;
035: import com.metaboss.sdlctools.models.metabossmodel.MetaBossModelPackage;
036: import com.metaboss.sdlctools.models.metabossmodel.ModelElement;
037: import com.metaboss.sdlctools.models.metabossmodel.ModelElementConstraint;
038: import com.metaboss.sdlctools.models.metabossmodel.ModelElementUtils;
039: import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.DataType;
040: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.Attribute;
041: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.Domain;
042: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.Entity;
043: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.EntityClass;
044: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.Selector;
045: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.SystemImplementationModelPackage;
046: import com.metaboss.sdlctools.models.metabossmodel.visualmodel.EntityStateDiagram;
047:
048: /* BOentity object user class */
049:
050: public class EntityUserObject extends BaseUserObject {
051: public static final int ADD_ATTRIBUTE = 2;
052: public static final int ADD_PRIMARYKEY = 3;
053: public static final int ADD_SELECTOR = 5;
054: public static final int ADD_CONSTRAINT = 6;
055: public static final int ADD_DIAGRAM = 7;
056:
057: private Entity mEntity = null;
058: private EntityStateDiagramAction mEntityStateDiagramAction = new EntityStateDiagramAction();
059: private AddNewAssociationRoleAction mAddNewAssociationRoleAction = new AddNewAssociationRoleAction();
060: private AddNewAttributeAction mAddNewAttributeAction = new AddNewAttributeAction();
061: private AddNewPrimaryKeyAction mAddNewPrimaryKeyAction = new AddNewPrimaryKeyAction();
062: private AddNewSelectorAction mAddNewSelectorAction = new AddNewSelectorAction();
063: private AddNewStateMachineAction mAddNewStateMachineAction = new AddNewStateMachineAction();
064: private AddNewConstraint mAddNewConstraint = new AddNewConstraint();
065: private AddDiagramAction mAddDiagramAction = new AddDiagramAction();
066: private StudyDependenciesAction mShowDataTypeDependenciesAction = new StudyDependenciesAction();
067:
068: public EntityUserObject(Entity pEntity) {
069: super (pEntity, Application.ENTITY_ICON);
070: mEntity = pEntity;
071: }
072:
073: public EntityUserObject(Entity pEntity, String pCaption, int pMode) {
074: super (pEntity, pCaption, pMode);
075: mEntity = pEntity;
076: }
077:
078: // create new Entity
079: public static EntityUserObject addNewEntity(Domain pDomain)
080: throws Exception {
081: return (EntityUserObject) new EntityUserObject(null)
082: .addNewObject(getObjectPackage(pDomain), pDomain
083: .getEntities());
084: }
085:
086: public BaseUserObject createNewObject(MetaBossModelPackage pPackage) {
087: SystemImplementationModelPackage lSystemImplementationModelPackage = pPackage
088: .getEnterpriseModel().getSystemImplementationModel();
089: EntityClass lClass = lSystemImplementationModelPackage
090: .getEntity();
091: return new EntityUserObject(lClass.createEntity());
092: }
093:
094: // set new supertype for BOEntity
095: public static void setNewSuperType(Entity pEntity, Entity pSuperType)
096: throws Exception {
097: if (pEntity == null)
098: return;
099:
100: Application.beginTransaction();
101: try {
102: pEntity.setSupertype(pSuperType);
103: Application.commit();
104: } finally {
105: Application.checkAndRollback();
106: }
107: }
108:
109: public Entity getEntity() {
110: return mEntity;
111: }
112:
113: // return object root node captions
114: public String getRootNodeName() {
115: return Application.getString("entities_node");
116: }
117:
118: // convert tree element to an action code
119: public int convertCaptionToAction(String pCaption) {
120: if (pCaption.equals(Application.getString("attributes_node")))
121: return ADD_ATTRIBUTE;
122: else if (pCaption.equals(Application
123: .getString("selectors_node")))
124: return ADD_SELECTOR;
125: else if (pCaption.equals(Application
126: .getString("constraints_node")))
127: return ADD_CONSTRAINT;
128: else if (pCaption
129: .equals(Application.getString("diagrams_node")))
130: return ADD_DIAGRAM;
131: else
132: return super .convertCaptionToAction(pCaption);
133: }
134:
135: // view structure diagram panel
136: public void viewEntityModelDiagram() {
137: if (mEntity == null)
138: return;
139:
140: try {
141: SortedSet lDiagrams = ModelElementUtils
142: .getSetOfModelElementsInAlphabeticalOrder(mEntity
143: .getEntityStateDiagrams());
144: if (lDiagrams != null) {
145: if (lDiagrams.size() == 0) {
146: EntityStateDiagramUserObject lDiagramObject = EntityStateDiagramUserObject
147: .addNewEntityStatesDiagram(mEntity);
148: if (lDiagramObject != null)
149: lDiagramObject.viewDiagram();
150: } else if (lDiagrams.size() == 1) {
151: EntityStateDiagram lDiagram = (EntityStateDiagram) lDiagrams
152: .first();
153: Application.fireShowContainer(new StatesPanel(
154: lDiagram), this ,
155: Application.STATESDIAGRAM_ICON);
156: } else {
157: ArrayList lDiagramsArray = new ArrayList();
158: for (Iterator lIterator = lDiagrams.iterator(); lIterator
159: .hasNext();) {
160: EntityStateDiagram lDiagram = (EntityStateDiagram) lIterator
161: .next();
162: lDiagramsArray
163: .add(new EntityStateDiagramUserObject(
164: lDiagram));
165: }
166: DiagramSelectDialog lDialog = new DiagramSelectDialog(
167: Application.mainFrame, lDiagramsArray);
168: if (lDialog.getModalResult() == DiagramSelectDialog.MR_OK) {
169: for (int i = 0; i < lDiagramsArray.size(); i++) {
170: EntityStateDiagramUserObject lEntityStateDiagramUserObject = (EntityStateDiagramUserObject) lDiagramsArray
171: .get(i);
172: Application
173: .fireShowContainer(
174: new StatesPanel(
175: lEntityStateDiagramUserObject
176: .getEntityStateDiagram()),
177: this ,
178: Application.STATESDIAGRAM_ICON);
179: }
180: }
181: }
182: }
183: } catch (Exception e) {
184: e.printStackTrace();
185: }
186: }
187:
188: // load object properties into grid control
189: public void loadObjectProperties(PropertiesTableModel pModel)
190: throws Exception {
191: super .loadObjectProperties(pModel);
192: if (pModel == null || mEntity == null)
193: return;
194:
195: pModel.AddProperty("Plural Name", mEntity.getPluralName());
196: if (mEntity.getStereotype() != null)
197: pModel.AddProperty("Stereotype", mEntity.getStereotype()
198: .toString());
199: addModelElement(pModel, "Supertype", mEntity.getSupertype());
200: pModel.AddProperty("Abstract", boolToString(mEntity
201: .isAbstract()));
202: pModel.AddProperty("Final", boolToString(mEntity.isFinal()));
203: try {
204: pModel.AddProperty("Modifiable", boolToString(mEntity
205: .isModifiable()));
206: } catch (Exception e) {
207: e.printStackTrace();
208: }
209:
210: addModelElement(pModel, "Collection Contains Flag Datatype",
211: mEntity.getCollectionContainsFlagDataType());
212: addModelElement(pModel, "Collection Empty Flag Datatype",
213: mEntity.getCollectionEmptyFlagDataType());
214: addModelElement(pModel, "Collection Offset Datatype", mEntity
215: .getCollectionOffsetDataType());
216: addModelElement(pModel, "Collection Size Datatype", mEntity
217: .getCollectionSizeDataType());
218: addModelElement(pModel, "InstanceId Datatype", mEntity
219: .getInstanceIdDataType());
220: addModelElement(pModel, "Ordering Instruction Datatype",
221: mEntity.getOrderingInstructionDataType());
222: addModelElement(pModel, "State Datatype", mEntity
223: .getStateDataType());
224: addModelElement(pModel, "Subset Size Datatype", mEntity
225: .getSubsetSizeDataType());
226: addModelElement(pModel, "VersionId Datatype", mEntity
227: .getVersionIdDataType());
228: }
229:
230: // add extra properties tabs
231: public void addExtraPropertiesTabs(JTabbedPane pTabbedPane) {
232: super .addExtraPropertiesTabs(pTabbedPane);
233: pTabbedPane.insertTab("Attributes", null,
234: new AttributesViewPanel(mEntity), null, 1);
235: pTabbedPane.insertTab("Constraints", null,
236: new ConstraintsViewPanel(mEntity), null, 2);
237: }
238:
239: // fill actions list
240: public void fillActionsList(ArrayList pList) {
241: switch (mMode) {
242: case ALL_ACTIONS:
243: super .fillActionsList(pList);
244: pList.add(new SeparatorAction());
245: if (mEntity != null && mEntity.getStateMachine() != null) {
246: pList.add(mEntityStateDiagramAction);
247: pList.add(new SeparatorAction());
248: }
249: //???pList.add(mAddNewAssociationRoleAction);
250: pList.add(mAddNewAttributeAction);
251: //???pList.add(mAddNewPrimaryKeyAction);
252: pList.add(mAddNewSelectorAction);
253: pList.add(mAddNewConstraint);
254: if (mEntity != null && mEntity.getStateMachine() == null)
255: pList.add(mAddNewStateMachineAction);
256: pList.add(mAddDiagramAction);
257: pList.add(new SeparatorAction());
258: pList.add(mShowDataTypeDependenciesAction);
259: break;
260: case ADD_ATTRIBUTE:
261: pList.add(mAddNewAttributeAction);
262: break;
263: case ADD_PRIMARYKEY:
264: //???pList.add(mAddNewPrimaryKeyAction);
265: break;
266: case ADD_SELECTOR:
267: pList.add(mAddNewSelectorAction);
268: break;
269: case ADD_CONSTRAINT:
270: pList.add(mAddNewConstraint);
271: break;
272: case ADD_DIAGRAM:
273: pList.add(mAddDiagramAction);
274: break;
275: }
276: }
277:
278: public BasePropertiesDialog getObjectEditor() {
279: return new EntityPropertiesDialog();
280: }
281:
282: // add new Association Role
283: public void addNewAssociationRole() throws Exception {
284: AssociationRoleUserObject.addNewAssociationRole(mEntity);
285: }
286:
287: // add new child attribute
288: public void addNewAttribute() throws Exception {
289: AttributeUserObject.addNewAttribute(mEntity);
290: }
291:
292: // add new selector
293: public void addNewSelector() throws Exception {
294: SelectorUserObject.addNewSelector(mEntity);
295: }
296:
297: // add new primary key
298: public void addNewPrimaryKey() throws Exception {
299: PrimaryKeyElementUserObject.addNewPrimaryKeyElement(mEntity);
300: }
301:
302: // add new state machine
303: public void addNewStateMachine() throws Exception {
304: StateMachineUserObject.addNewStateMachine(mEntity);
305: }
306:
307: // add new Model Element Constraint
308: public void addNewConstraint() throws Exception {
309: ModelElementConstraintUserObject.addNewConstraint(mEntity);
310: }
311:
312: // add new diagram
313: protected void addNewDiagram() {
314: try {
315: EntityStateDiagramUserObject
316: .addNewEntityStatesDiagram(mEntity);
317: } catch (Throwable t) {
318: Application.processError(t);
319: }
320: }
321:
322: public Object[] getFieldDescriptors() {
323: ArrayList lList = new ArrayList();
324: try {
325: if (mEntity != null) {
326: SortedSet lFields = ModelElementUtils
327: .getSetOfModelElementsInAlphabeticalOrder(mEntity
328: .getAttributes());
329: for (Iterator lIterator = lFields.iterator(); lIterator
330: .hasNext();) {
331: Attribute lField = (Attribute) lIterator.next();
332: DataType lDataType = lField.getDataType();
333: lList.add(new FieldDescriptor(lField.getName(),
334: (lDataType != null) ? lDataType.getName()
335: : null));
336: }
337: }
338: } catch (Exception e) {
339: e.printStackTrace();
340: }
341: return lList.toArray();
342: }
343:
344: // can duplicate model element
345: public boolean canDuplicate() {
346: return true;
347: }
348:
349: // duplicate model element
350: public ModelElement duplicate() throws Exception {
351: Entity lEntity = (Entity) Application.sModelRepository
352: .duplicateModelElement(mEntity);
353: lEntity.setDomain(mEntity.getDomain());
354: return lEntity;
355: }
356:
357: // returns true if drop object could be accepted
358: public boolean canAcceptDropObject(BaseUserObject pUserObject) {
359: if (pUserObject != null) {
360: if ((pUserObject instanceof AttributeUserObject && (mMode == BaseUserObject.ALL_ACTIONS || mMode == EntityUserObject.ADD_ATTRIBUTE))
361: || (pUserObject instanceof ModelElementConstraintUserObject && (mMode == BaseUserObject.ALL_ACTIONS || mMode == EntityUserObject.ADD_CONSTRAINT))
362: || (pUserObject instanceof SelectorUserObject && (mMode == BaseUserObject.ALL_ACTIONS || mMode == EntityUserObject.ADD_SELECTOR))) {
363: //return pUserObject.canDuplicate();
364: return true;
365: }
366: }
367: return false;
368: }
369:
370: // acccept drop target in transaction
371: protected void transactionalAccept(BaseUserObject pUserObject,
372: boolean pCopy) throws Exception {
373: if (pUserObject != null) {
374: if (pUserObject instanceof AttributeUserObject) {
375: AttributeUserObject lUserObject = (AttributeUserObject) pUserObject;
376: Attribute lSourceAttribute = lUserObject.getAttribute();
377: Attribute lTargetAttribute = pCopy ? (Attribute) Application.sModelRepository
378: .duplicateModelElement(lSourceAttribute)
379: : lSourceAttribute;
380: lTargetAttribute.setEntity(mEntity);
381: } else if (pUserObject instanceof SelectorUserObject) {
382: SelectorUserObject lUserObject = (SelectorUserObject) pUserObject;
383: Selector lSourceSelector = lUserObject.getSelector();
384: Selector lTargetSelector = pCopy ? (Selector) Application.sModelRepository
385: .duplicateModelElement(lSourceSelector)
386: : lSourceSelector;
387: lTargetSelector.setEntity(mEntity);
388: } else if (pUserObject instanceof ModelElementConstraintUserObject) {
389: ModelElementConstraintUserObject lUserObject = (ModelElementConstraintUserObject) pUserObject;
390: if (pCopy) {
391: ModelElementConstraint lConstraint = (ModelElementConstraint) Application.sModelRepository
392: .duplicateModelElement(lUserObject
393: .getConstraint());
394: lConstraint.setEntityOwner(mEntity);
395: } else {
396: // Constraint may have may kinds of owners
397: // We need to set all of them to null to avoid containment constraint violation
398: ModelElementConstraint lConstraint = lUserObject
399: .getConstraint();
400: lConstraint.setOperationOwner(null);
401: lConstraint.setStructureOwner(null);
402: lConstraint.setEntityOwner(mEntity);
403: }
404: }
405: }
406: }
407:
408: /* Actions */
409:
410: /* Entity State Machine Model */
411:
412: public class EntityStateDiagramAction extends BaseAction {
413: public EntityStateDiagramAction() {
414: super (Application.getString("view_entitystatesdiagram"),
415: Application.STATESDIAGRAM_ICON);
416: }
417:
418: public void actionPerformed(ActionEvent arg0) {
419: viewEntityModelDiagram();
420: }
421: }
422:
423: /* Add New Association Role Action Class */
424:
425: public class AddNewAssociationRoleAction extends BaseAction {
426: public AddNewAssociationRoleAction() {
427: super ("Add New Association Role", Application
428: .getAddIcon(Application.ASSOCIATIONROLE_ICON));
429: }
430:
431: public void actionPerformed(ActionEvent arg0) {
432: try {
433: addNewAssociationRole();
434: } catch (Exception e) {
435: e.printStackTrace();
436: }
437: }
438: }
439:
440: /* Add New Attribute Action Class */
441:
442: public class AddNewAttributeAction extends BaseAction {
443: public AddNewAttributeAction() {
444: super ("Add New Attribute", Application
445: .getAddIcon(Application.ATTRIBUTE_ICON));
446: }
447:
448: public void actionPerformed(ActionEvent arg0) {
449: try {
450: addNewAttribute();
451: } catch (Exception e) {
452: e.printStackTrace();
453: }
454: }
455: }
456:
457: /* Add New Primary Key Action Class */
458:
459: public class AddNewPrimaryKeyAction extends BaseAction {
460: public AddNewPrimaryKeyAction() {
461: super ("Add New Primary Key Element", Application
462: .getAddIcon(Application.PRIMARYKEY_ICON));
463: }
464:
465: public void actionPerformed(ActionEvent arg0) {
466: try {
467: addNewPrimaryKey();
468: } catch (Exception e) {
469: e.printStackTrace();
470: }
471: }
472: }
473:
474: /* Add New Selector Action Class */
475:
476: public class AddNewSelectorAction extends BaseAction {
477: public AddNewSelectorAction() {
478: super ("Add New Selector", Application
479: .getAddIcon(Application.SELECTOR_ICON));
480: }
481:
482: public void actionPerformed(ActionEvent arg0) {
483: try {
484: addNewSelector();
485: } catch (Exception e) {
486: e.printStackTrace();
487: }
488: }
489: }
490:
491: /* Add New State Machine Action */
492:
493: public class AddNewStateMachineAction extends BaseAction {
494: public AddNewStateMachineAction() {
495: super ("Add New State Machine", Application
496: .getAddIcon(Application.STATEMACHINE_ICON));
497: }
498:
499: public void actionPerformed(ActionEvent arg0) {
500: try {
501: addNewStateMachine();
502: } catch (Exception e) {
503: e.printStackTrace();
504: }
505: }
506: }
507:
508: /* Add New Model Element Constraint Action */
509:
510: public class AddNewConstraint extends BaseAction {
511: public AddNewConstraint() {
512: super ("Add New Constraint", Application
513: .getAddIcon(Application.CONSTRAINT_ICON));
514: }
515:
516: public void actionPerformed(ActionEvent arg0) {
517: try {
518: addNewConstraint();
519: } catch (Exception e) {
520: e.printStackTrace();
521: }
522: }
523: }
524: }
|