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.designtree;
016:
017: import java.awt.Component;
018: import java.awt.dnd.DnDConstants;
019: import java.util.ArrayList;
020: import java.util.Collection;
021: import java.util.Iterator;
022:
023: import javax.swing.Icon;
024: import javax.swing.ImageIcon;
025: import javax.swing.JScrollPane;
026: import javax.swing.JTabbedPane;
027: import javax.swing.JTree;
028: import javax.swing.event.TreeSelectionEvent;
029: import javax.swing.event.TreeSelectionListener;
030: import javax.swing.tree.DefaultMutableTreeNode;
031: import javax.swing.tree.DefaultTreeCellRenderer;
032: import javax.swing.tree.DefaultTreeModel;
033: import javax.swing.tree.TreePath;
034: import javax.swing.tree.TreeSelectionModel;
035:
036: import com.metaboss.applications.designstudio.Application;
037: import com.metaboss.applications.designstudio.BaseUserObject;
038: import com.metaboss.applications.designstudio.Application.OnElementIndexChangedEvent;
039: import com.metaboss.applications.designstudio.userobjects.ModelUserObject;
040: import com.metaboss.applications.designstudio.userobjects.UserObjectFactory;
041: import com.metaboss.sdlctools.models.metabossmodel.MetaBossModelPackage;
042: import com.metaboss.sdlctools.models.metabossmodel.ModelElement;
043:
044: /* Design tree tab descriptor */
045:
046: public class DesignTreeTabDescriptor {
047: public static DefaultTreeCellRenderer sRenderer = new DesignTreeCellRenderer();
048:
049: // create tree component
050: public static JTree createTree(DefaultTreeModel pModel) {
051: JTree lTree = new DesignTree(pModel);
052: defineTree(lTree);
053: return lTree;
054: }
055:
056: public static void defineTree(JTree pTree) {
057: pTree.getSelectionModel().setSelectionMode(
058: TreeSelectionModel.SINGLE_TREE_SELECTION);
059: pTree.setShowsRootHandles(true);
060: pTree.setRootVisible(false);
061: pTree.setCellRenderer(sRenderer);
062:
063: pTree.addMouseListener(new TreeMouseAdapter(pTree));
064: pTree.addTreeSelectionListener(new TreeSelectionListener() {
065: public void valueChanged(TreeSelectionEvent e) {
066: TreePath selPath = e.getNewLeadSelectionPath();
067: if (selPath != null) {
068: DefaultMutableTreeNode lNode = (DefaultMutableTreeNode) selPath
069: .getLastPathComponent();
070: if (lNode != null
071: && lNode.getUserObject() != null
072: && lNode.getUserObject() instanceof BaseUserObject) {
073: BaseUserObject lObject = (BaseUserObject) lNode
074: .getUserObject();
075: Application.fireObjectSelected(lObject);
076: }
077: }
078: }
079: });
080: }
081:
082: private ModelUserObject mPackage = null;
083: private ModelUserObject mDesignLib = null;
084: private ModelUserObject mTechLib = null;
085: private JTree mTree = null;
086: private DesignTreeDragSource mDragSource = null;
087: private DesignTreeDropTarget mDropTarget = null;
088: private MetaBossPackageTreeModel mModel = new MetaBossPackageTreeModel();
089: private JScrollPane mScrollPane = null;
090: private String mCaption = null;
091:
092: public DesignTreeTabDescriptor(ModelUserObject pPackage) {
093: mPackage = pPackage;
094: mModel.loadModel(pPackage);
095: createTreeControl();
096: }
097:
098: public DesignTreeTabDescriptor(ModelUserObject pDesignLib,
099: ModelUserObject pTechLib) {
100: mCaption = Application.getString("library");
101: mDesignLib = pDesignLib;
102: mTechLib = pTechLib;
103:
104: mModel.loadModel(pDesignLib, pTechLib);
105: createTreeControl();
106: }
107:
108: // add tab
109: public void addTab(JTabbedPane pTabControl) {
110: if (mPackage != null)
111: pTabControl.addTab(mPackage.getModelName(), getIcon(),
112: mScrollPane, mPackage.getToolTip());
113: else
114: pTabControl.addTab(mCaption, getIcon(), mScrollPane,
115: mCaption);
116: pTabControl.setSelectedIndex(pTabControl.getTabCount() - 1);
117: }
118:
119: // reset tab properties
120: public void setTab(JTabbedPane pTabControl) {
121: int lIndex = pTabControl.indexOfComponent(getComponent());
122: if (lIndex > -1) {
123: if (mPackage != null) {
124: pTabControl.setTitleAt(lIndex, mPackage.getModelName());
125: pTabControl.setToolTipTextAt(lIndex, mPackage
126: .getToolTip());
127: } else {
128: pTabControl.setTitleAt(lIndex, mCaption);
129: pTabControl.setToolTipTextAt(lIndex, mCaption);
130: }
131: }
132: }
133:
134: public void setTabIcon(JTabbedPane pTabControl) {
135: int lIndex = pTabControl.indexOfComponent(getComponent());
136: if (lIndex > -1)
137: pTabControl.setIconAt(lIndex, getIcon());
138: }
139:
140: // reload model
141: public void reLoadModel() {
142: mModel.reLoadModel();
143: mModel.makeFirstLevelVisisble(mTree);
144: fillWarnings();
145: }
146:
147: public void selectID(String pID) {
148: DefaultMutableTreeNode lNode = mModel.findChildNodeByID(pID);
149: if (lNode != null)
150: mModel.selectNode(lNode, mTree);
151: }
152:
153: // add new object to the tree
154: public void addNewObject(BaseUserObject pObject) {
155: mModel.addNewObject(pObject, mTree);
156: }
157:
158: // remove node
159: public void removeNode(BaseUserObject pObject) {
160: mModel.removeNode(pObject);
161: }
162:
163: public void removeNode(String pID) {
164: mModel.removeNode(pID);
165: }
166:
167: public void processObjectSelected(BaseUserObject pObject) {
168: mModel.selectNode(pObject, mTree);
169: }
170:
171: public void processObjectChanged(BaseUserObject pObject) {
172: if (pObject != null) {
173: DefaultMutableTreeNode lNode = mModel
174: .findChildNodeByID(pObject.getID());
175: if (lNode != null)
176: mModel.nodeChanged(lNode);
177: }
178: }
179:
180: // process object moved
181: public void processObjectMoved(BaseUserObject pObject) {
182: if (pObject == null)
183: return;
184:
185: mModel.removeNode(pObject);
186: mModel.addNewObject(pObject, mTree);
187:
188: Collection lCollection = pObject.getBOObject()
189: .getCombinedContents();
190: if (lCollection != null && lCollection.size() > 0) {
191: for (Iterator lIterator = lCollection.iterator(); lIterator
192: .hasNext();) {
193: ModelElement lModelElement = (ModelElement) lIterator
194: .next();
195: BaseUserObject lObject = UserObjectFactory
196: .createUserObject(lModelElement);
197: if (lObject != null) {
198: mModel.removeNode(lObject);
199: mModel.addNewObject(lObject, mTree);
200: }
201: }
202: }
203: mModel.selectNode(pObject, mTree);
204: }
205:
206: // get current user object
207: public BaseUserObject getCurrentUserObject() {
208: DefaultMutableTreeNode lNode = (DefaultMutableTreeNode) mTree
209: .getLastSelectedPathComponent();
210: return (lNode != null) ? (BaseUserObject) lNode.getUserObject()
211: : null;
212: }
213:
214: // get root user object
215: public BaseUserObject getRootUserObject() {
216: DefaultMutableTreeNode lNode = mModel.getRootNode();
217: return (lNode != null) ? (BaseUserObject) lNode.getUserObject()
218: : null;
219: }
220:
221: public boolean isPackageIncluded(MetaBossModelPackage pPackage) {
222: if (pPackage != null) {
223: if (mPackage != null
224: && mPackage.getPackage().equals(pPackage))
225: return true;
226: if (mDesignLib != null
227: && mDesignLib.getPackage().equals(pPackage))
228: return true;
229: if (mTechLib != null
230: && mTechLib.getPackage().equals(pPackage))
231: return true;
232: }
233: return false;
234: }
235:
236: // return current package
237: public ModelUserObject getCurrentPackage() {
238: if (mPackage != null)
239: return mPackage;
240: else {
241: BaseUserObject lObject = getCurrentUserObject();
242: return (lObject != null) ? Application.getModel(lObject
243: .getPackage()) : mDesignLib;
244: }
245: }
246:
247: // return all packages
248: public Object[] getPackages() {
249: ArrayList lList = new ArrayList();
250:
251: if (mPackage != null)
252: lList.add(mPackage);
253: if (mDesignLib != null)
254: lList.add(mDesignLib);
255: if (mTechLib != null)
256: lList.add(mTechLib);
257:
258: return lList.toArray();
259: }
260:
261: // return tab caption
262: public String getCaption() {
263: return (mCaption != null) ? mCaption : mPackage.getModelName();
264: }
265:
266: public boolean isSystem() {
267: return (mPackage == null);
268: }
269:
270: public boolean isModified() {
271: if (mPackage != null && mPackage.isModified())
272: return true;
273: if (mDesignLib != null && mDesignLib.isModified())
274: return true;
275: if (mTechLib != null && mTechLib.isModified())
276: return true;
277: return false;
278: }
279:
280: public Component getComponent() {
281: return mScrollPane;
282: }
283:
284: // edit model properties
285: public void editProperties() {
286: BaseUserObject lObject = getRootUserObject();
287: if (lObject != null)
288: lObject.getEditAction().run();
289: }
290:
291: public Icon getIcon() {
292: Icon lResult = null;
293: if (mPackage != null) {
294: lResult = mPackage.getModelIcon();
295: if (Application.getModelErrorsCount(mPackage) > 0)
296: lResult = Application
297: .getWarningIcon((ImageIcon) lResult);
298: } else {
299: lResult = Application.LIBRARY_ICON;
300: if (Application.getModelErrorsCount(mDesignLib) > 0
301: || Application.getModelErrorsCount(mTechLib) > 0)
302: lResult = Application
303: .getWarningIcon((ImageIcon) lResult);
304: }
305: return lResult;
306: }
307:
308: public MetaBossPackageTreeModel getTreeModel() {
309: return mModel;
310: }
311:
312: // fill warnings info
313: public void fillWarnings() {
314: mModel.fillWarnings(!isErrorTab());
315: }
316:
317: public boolean isErrorTab() {
318: if (mPackage != null)
319: return (Application.getModelErrorsCount(mPackage) > 0);
320: else
321: return (Application.getModelErrorsCount(mDesignLib) > 0 || Application
322: .getModelErrorsCount(mTechLib) > 0);
323: }
324:
325: private void createTreeControl() {
326: mTree = createTree(mModel);
327: mScrollPane = new JScrollPane(mTree);
328: mScrollPane.setViewportBorder(null);
329:
330: mDragSource = new DesignTreeDragSource(mTree,
331: DnDConstants.ACTION_COPY_OR_MOVE);
332: mDropTarget = new DesignTreeDropTarget(mTree);
333:
334: mModel.makeFirstLevelVisisble(mTree);
335: }
336:
337: // process user object changed event
338: public void processIndexChanged(BaseUserObject pUserObject,
339: int pNewIndex, int pOldIndex) {
340: if (pUserObject != null && pNewIndex != pOldIndex) {
341: DefaultMutableTreeNode lNode = mModel
342: .findChildNodeByModelElement(pUserObject
343: .getBOObject());
344: if (lNode != null) {
345: DefaultMutableTreeNode lParentNode = (DefaultMutableTreeNode) lNode
346: .getParent();
347: if (lParentNode != null) {
348: mModel.removeNode(lNode);
349: mModel
350: .insertNodeInto(lNode, lParentNode,
351: pNewIndex);
352: mModel.selectNode(pUserObject, mTree);
353: }
354: }
355: }
356: }
357:
358: /* Auxilary classes */
359:
360: /* Element index changed event listener */
361:
362: public class UserObjectIndexChangedEventListener implements
363: Application.OnElemenIndexChangedEventListener {
364: public void indexChanged(OnElementIndexChangedEvent event) {
365: processIndexChanged(event.getUserObject(), event
366: .getNewIndex(), event.getOldIndex());
367: }
368: }
369: }
|