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.components.SeparatorAction;
029: import com.metaboss.applications.designstudio.constraintstable.ConstraintsViewPanel;
030: import com.metaboss.applications.designstudio.fieldstable.FieldsViewPanel;
031: import com.metaboss.applications.designstudio.propertiesdialogs.StructurePropertiesDialog;
032: import com.metaboss.sdlctools.models.metabossmodel.MetaBossModelPackage;
033: import com.metaboss.sdlctools.models.metabossmodel.ModelElement;
034: import com.metaboss.sdlctools.models.metabossmodel.ModelElementConstraint;
035: import com.metaboss.sdlctools.models.metabossmodel.ModelElementUtils;
036: import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.AbstractDataField;
037: import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.DataDictionary;
038: import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.DataDictionaryModelPackage;
039: import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.Namespace;
040: import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.Structure;
041: import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.StructureClass;
042: import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.StructureField;
043: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.Servicemodule;
044:
045: /* BOStructure user object class */
046:
047: public class StructureUserObject extends BaseUserObject {
048: public static final int ADD_FIELD = 1;
049: public static final int ADD_CONSTRAINT = 2;
050:
051: private Structure mStructure = null;
052: private AddNewFieldAction mAddNewFieldAction = new AddNewFieldAction();
053: private AddNewConstraint mAddNewConstraint = new AddNewConstraint();
054: private StudyDependenciesAction mShowDataTypeDependenciesAction = new StudyDependenciesAction();
055:
056: public StructureUserObject(Structure pStructure) {
057: super (pStructure, Application.STRUCTURE_ICON);
058: mStructure = pStructure;
059: }
060:
061: public StructureUserObject(Structure pStructure, String pCaption,
062: int pMode) {
063: super (pStructure, pCaption, pMode);
064: mStructure = pStructure;
065: }
066:
067: // create new structure module
068: public static StructureUserObject addNewStructure(
069: Servicemodule pServiceModule) throws Exception {
070: return (StructureUserObject) new StructureUserObject(null)
071: .addNewObject(getObjectPackage(pServiceModule),
072: pServiceModule.getStructures());
073: }
074:
075: public static StructureUserObject addNewStructure(
076: DataDictionary pDataDictionary) throws Exception {
077: return (StructureUserObject) new StructureUserObject(null)
078: .addNewObject(getObjectPackage(pDataDictionary),
079: pDataDictionary.getStructures());
080: }
081:
082: public static StructureUserObject addNewStructure(
083: Namespace pNamespace) throws Exception {
084: return (StructureUserObject) new StructureUserObject(null)
085: .addNewObject(getObjectPackage(pNamespace), pNamespace
086: .getStructures());
087: }
088:
089: public BaseUserObject createNewObject(MetaBossModelPackage pPackage) {
090: DataDictionaryModelPackage lDataDictionaryModelPackage = pPackage
091: .getDataDictionaryModel();
092: StructureClass lClass = lDataDictionaryModelPackage
093: .getStructure();
094: return new StructureUserObject(lClass.createStructure());
095: }
096:
097: // add extra properties tabs
098: public void addExtraPropertiesTabs(JTabbedPane pTabbedPane) {
099: pTabbedPane.insertTab("Constraints", null,
100: new ConstraintsViewPanel(mStructure), null, 1);
101: pTabbedPane.insertTab("Fields", null, new FieldsViewPanel(
102: mStructure), null, 1);
103: }
104:
105: public Structure getStructure() {
106: return mStructure;
107: }
108:
109: // return object root node captions
110: public String getRootNodeName() {
111: return Application.getString("structures_node");
112: }
113:
114: // convert tree element to an action code
115: public int convertCaptionToAction(String pCaption) {
116: if (pCaption.equals(Application.getString("fields_node")))
117: return ADD_FIELD;
118: else if (pCaption.equals(Application
119: .getString("constraints_node")))
120: return ADD_CONSTRAINT;
121: else
122: return super .convertCaptionToAction(pCaption);
123: }
124:
125: // fill actions list
126: public void fillActionsList(ArrayList pList) {
127: switch (mMode) {
128: case ALL_ACTIONS:
129: super .fillActionsList(pList);
130: pList.add(new SeparatorAction());
131: pList.add(mAddNewFieldAction);
132: pList.add(mAddNewConstraint);
133: pList.add(new SeparatorAction());
134: pList.add(mShowDataTypeDependenciesAction);
135: break;
136: case ADD_FIELD:
137: pList.add(mAddNewFieldAction);
138: break;
139: case ADD_CONSTRAINT:
140: pList.add(mAddNewConstraint);
141: break;
142: }
143: }
144:
145: // get object editor
146: public BasePropertiesDialog getObjectEditor() {
147: return new StructurePropertiesDialog();
148: }
149:
150: // add event
151: public void addField() throws Exception {
152: StructureFieldUserObject.addNewStructureField(mStructure);
153: }
154:
155: public void addNewConstraint() throws Exception {
156: ModelElementConstraintUserObject.addNewConstraint(mStructure);
157: }
158:
159: public Object[] getFieldDescriptors() {
160: ArrayList lList = new ArrayList();
161: try {
162: if (mStructure != null) {
163: SortedSet lFields = ModelElementUtils
164: .getSetOfModelElementsInAlphabeticalOrder(mStructure
165: .getFields());
166: for (Iterator lIterator = lFields.iterator(); lIterator
167: .hasNext();) {
168: StructureField lField = (StructureField) lIterator
169: .next();
170: String lType = "";
171: if (lField.getDataType() != null)
172: lType = lField.getDataType().getName();
173: else if (lField.getStructureType() != null)
174: lType = lField.getStructureType().getName();
175: lList.add(new FieldDescriptor(lField.getName(),
176: lType));
177: }
178: }
179: } catch (Exception e) {
180: e.printStackTrace();
181: }
182: return lList.toArray();
183: }
184:
185: // can duplicate model element
186: public boolean canDuplicate() {
187: return true;
188: }
189:
190: // duplicate model element
191: public ModelElement duplicate() throws Exception {
192: Structure lStructure = (Structure) Application.sModelRepository
193: .duplicateModelElement(mStructure);
194: lStructure.setServicemodule(mStructure.getServicemodule());
195: lStructure.setNamespace(mStructure.getNamespace());
196: return lStructure;
197: }
198:
199: // returns true if drop object could be accepted
200: public boolean canAcceptDropObject(BaseUserObject pUserObject,
201: boolean pCopy) {
202: // Can accept any abstract data field user object
203: return mStructure != null
204: && pUserObject != null
205: && (pUserObject instanceof AbstractDataFieldUserObject || pUserObject instanceof ModelElementConstraintUserObject);
206: }
207:
208: // acccept drop target in transaction
209: protected void transactionalAccept(BaseUserObject pUserObject,
210: boolean pCopy) throws Exception {
211: if (pUserObject != null) {
212: if (pUserObject instanceof StructureFieldUserObject) {
213: StructureFieldUserObject lUserObject = (StructureFieldUserObject) pUserObject;
214: StructureField lField = pCopy ? (StructureField) Application.sModelRepository
215: .duplicateModelElement(lUserObject.getField())
216: : lUserObject.getField();
217: lField.setOwnerStructure(mStructure);
218: } else if (pUserObject instanceof AbstractDataFieldUserObject) {
219: AbstractDataField lPrototype = ((AbstractDataFieldUserObject) pUserObject)
220: .getAbstractField();
221: // Create the object and copy all the contents from the prototype field
222: StructureField lField = ((DataDictionaryModelPackage) mStructure
223: .refImmediatePackage()).getStructureField()
224: .createStructureField();
225: lField.setOwnerStructure(mStructure);
226: lField.setName(lPrototype.getName());
227: lField.setDescription(lPrototype.getDescription());
228: lField.setArray(lPrototype.isArray());
229: lField.setDataType(lPrototype.getDataType());
230: lField.setStructureType(lPrototype.getStructureType());
231: // Delete prototype object if move was requested
232: if (pCopy == false) {
233: lPrototype.refDelete();
234: }
235: } else if (pUserObject instanceof ModelElementConstraintUserObject) {
236: ModelElementConstraintUserObject lUserObject = (ModelElementConstraintUserObject) pUserObject;
237: if (pCopy) {
238: ModelElementConstraint lConstraint = (ModelElementConstraint) Application.sModelRepository
239: .duplicateModelElement(lUserObject
240: .getConstraint());
241: lConstraint.setStructureOwner(mStructure);
242: } else {
243: // Constraint may have may kinds of owners
244: // We need to set all of them to null to avoid containment constraint violation
245: ModelElementConstraint lConstraint = lUserObject
246: .getConstraint();
247: lConstraint.setEntityOwner(null);
248: lConstraint.setOperationOwner(null);
249: lConstraint.setStructureOwner(mStructure);
250: }
251: }
252: }
253: }
254:
255: /* Actions */
256:
257: /* Add New Field Action Class */
258:
259: public class AddNewFieldAction extends BaseAction {
260: public AddNewFieldAction() {
261: super ("Add New Field", Application
262: .getAddIcon(Application.FIELDS_ICON));
263: }
264:
265: public void actionPerformed(ActionEvent e) {
266: try {
267: addField();
268: } catch (Throwable t) {
269: Application.processError(t);
270: }
271: }
272: }
273:
274: /* Add New Model Element Constraint Action */
275:
276: public class AddNewConstraint extends BaseAction {
277: public AddNewConstraint() {
278: super ("Add New Constraint", Application
279: .getAddIcon(Application.CONSTRAINT_ICON));
280: }
281:
282: public void actionPerformed(ActionEvent arg0) {
283: try {
284: addNewConstraint();
285: } catch (Exception e) {
286: e.printStackTrace();
287: }
288: }
289: }
290: }
|