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.BorderLayout;
018: import java.awt.Color;
019: import java.awt.event.ActionEvent;
020: import java.awt.event.MouseAdapter;
021: import java.awt.event.MouseEvent;
022: import java.util.ArrayList;
023:
024: import javax.swing.GrayFilter;
025: import javax.swing.Icon;
026: import javax.swing.ImageIcon;
027: import javax.swing.JPanel;
028: import javax.swing.JPopupMenu;
029: import javax.swing.JTabbedPane;
030: import javax.swing.event.ChangeEvent;
031: import javax.swing.event.ChangeListener;
032: import javax.swing.plaf.IconUIResource;
033:
034: import com.metaboss.applications.designstudio.Application;
035: import com.metaboss.applications.designstudio.BaseAction;
036: import com.metaboss.applications.designstudio.BaseUserObject;
037: import com.metaboss.applications.designstudio.Application.ObjectChangedEvent;
038: import com.metaboss.applications.designstudio.Application.OnErrorsFoundEvent;
039: import com.metaboss.applications.designstudio.Application.OnModelChangedEvent;
040: import com.metaboss.applications.designstudio.Application.RefreshEvent;
041: import com.metaboss.applications.designstudio.userobjects.ModelUserObject;
042: import com.metaboss.sdlctools.models.metabossmodel.MetaBossModelPackage;
043:
044: /* tree view panel class */
045:
046: public class DesignTreePanel extends JPanel {
047: private ArrayList mTabs = new ArrayList();
048: private ArrayList mLastSelected = new ArrayList();
049: private JTabbedPane mTabControl = new JMyTabbedPane();
050: private BorderLayout mLayout = null;
051:
052: public DesignTreePanel() throws Exception {
053: setLayout(mLayout = new BorderLayout());
054:
055: add(mTabControl, null);
056: mTabControl.setBorder(null);
057: mTabControl.setFont(Application.DEFAULT_FONT);
058:
059: createTabs();
060: addEventsListeners();
061: }
062:
063: // add new object to the tree
064: public void addNewObject(BaseUserObject pObject) {
065: DesignTreeTabDescriptor lDescriptor = findTabDescriptorByPackage(pObject
066: .getPackage());
067: if (lDescriptor != null)
068: lDescriptor.addNewObject(pObject);
069: }
070:
071: // process object moved
072: public void objectMoved(BaseUserObject pObject) {
073: DesignTreeTabDescriptor lDescriptor = findTabDescriptorByPackage(pObject
074: .getPackage());
075: if (lDescriptor != null)
076: lDescriptor.processObjectMoved(pObject);
077: }
078:
079: // remove tree node
080: public void removeNode(BaseUserObject pObject) {
081: for (int i = 0; i < mTabs.size(); i++) {
082: DesignTreeTabDescriptor lDescriptor = getTabDescriptor(i);
083: if (lDescriptor != null)
084: lDescriptor.removeNode(pObject);
085: }
086: }
087:
088: public void removeNode(String pID) {
089: for (int i = 0; i < mTabs.size(); i++) {
090: DesignTreeTabDescriptor lDescriptor = getTabDescriptor(i);
091: if (lDescriptor != null)
092: lDescriptor.removeNode(pID);
093: }
094: }
095:
096: public void objectChanged(BaseUserObject pObject) {
097: if (pObject != null) {
098: DesignTreeTabDescriptor lDescriptor = findTabDescriptorByPackage(pObject
099: .getPackage());
100: if (lDescriptor != null)
101: lDescriptor.processObjectChanged(pObject);
102: }
103: }
104:
105: public void objectSelected(BaseUserObject pObject) {
106: if (pObject != null && !pObject.equals(getCurrentUserObject())) {
107: DesignTreeTabDescriptor lDescriptor = findTabDescriptorByPackage(pObject
108: .getPackage());
109: if (lDescriptor != null)
110: lDescriptor.processObjectSelected(pObject);
111: }
112: }
113:
114: // load tree
115: public void reLoadTree() {
116: saveLastSelected();
117: createTabs();
118: for (int i = 0; i < mTabs.size(); i++) {
119: DesignTreeTabDescriptor lDescriptor = getTabDescriptor(i);
120: if (lDescriptor != null) {
121: lDescriptor.reLoadModel();
122: String lLastID = getLastSelectedID(lDescriptor
123: .getCaption());
124: if (lLastID != null)
125: lDescriptor.selectID(lLastID);
126: }
127: }
128: }
129:
130: // get current tab descriptor
131: public DesignTreeTabDescriptor getCurentTabDescriptor() {
132: return getTabDescriptor(mTabControl.getSelectedIndex());
133: }
134:
135: // get current package
136: public ModelUserObject getCurrentPackage() {
137: DesignTreeTabDescriptor lDescriptor = getTabDescriptor(mTabControl
138: .getSelectedIndex());
139: return (lDescriptor != null) ? lDescriptor.getCurrentPackage()
140: : null;
141: }
142:
143: // get current user object
144: public BaseUserObject getCurrentUserObject() {
145: DesignTreeTabDescriptor lDescriptor = getTabDescriptor(mTabControl
146: .getSelectedIndex());
147: return (lDescriptor != null) ? lDescriptor
148: .getCurrentUserObject() : null;
149: }
150:
151: // open Model
152: public void openModel(ModelUserObject pModel) {
153: if (pModel != null) {
154: DesignTreeTabDescriptor lDescriptor = findTabDescriptorByPackage(pModel
155: .getPackage());
156: if (lDescriptor == null) {
157: lDescriptor = new DesignTreeTabDescriptor(pModel);
158: mTabs.add(lDescriptor);
159: lDescriptor.addTab(mTabControl);
160: } else {
161: int lIndex = mTabControl.indexOfTab(pModel
162: .getModelName());
163: if (lIndex > -1)
164: mTabControl.setSelectedIndex(lIndex);
165: }
166: }
167: }
168:
169: // close model
170: public void closeModel(ModelUserObject pModel) {
171: if (pModel == null)
172: return;
173: DesignTreeTabDescriptor lDescriptor = findTabDescriptorByPackage(pModel
174: .getPackage());
175: if (lDescriptor == null)
176: return;
177:
178: int lIndex = mTabControl.indexOfTab(lDescriptor.getCaption());
179: if (lIndex > -1)
180: mTabControl.removeTabAt(lIndex);
181: mTabs.remove(lDescriptor);
182: }
183:
184: // process object index changed
185: public void processObjectIndexChanged(BaseUserObject pUserObject,
186: int pNewIndex, int pOldIndex) {
187: if (pUserObject != null) {
188: DesignTreeTabDescriptor lDescriptor = findTabDescriptorByPackage(pUserObject
189: .getPackage());
190: if (lDescriptor != null)
191: lDescriptor.processIndexChanged(pUserObject, pNewIndex,
192: pOldIndex);
193: }
194: }
195:
196: // find dscriptor by associatied package
197: private DesignTreeTabDescriptor findTabDescriptorByPackage(
198: MetaBossModelPackage pPackage) {
199: if (pPackage != null) {
200: for (int i = 0; i < mTabs.size(); i++) {
201: DesignTreeTabDescriptor lDescriptor = getTabDescriptor(i);
202: if (lDescriptor != null
203: && lDescriptor.isPackageIncluded(pPackage))
204: return lDescriptor;
205: }
206: }
207: return null;
208: }
209:
210: private void createTabs() {
211: DesignTreeTabDescriptor lOldDescriptor = getCurentTabDescriptor();
212: String lCaption = (lOldDescriptor != null) ? lOldDescriptor
213: .getCaption() : null;
214:
215: ModelUserObject lDesignLib = null;
216: ModelUserObject lTechLib = null;
217:
218: mTabControl.removeAll();
219: mTabs.clear();
220:
221: for (int i = 0; i < Application.sModels.size(); i++) {
222: ModelUserObject lModel = Application.getModel(i);
223: if (lModel != null) {
224: if (!lModel.isSystem()) {
225: DesignTreeTabDescriptor lDescriptor = new DesignTreeTabDescriptor(
226: lModel);
227: mTabs.add(lDescriptor);
228: lDescriptor.addTab(mTabControl);
229: } else if (lModel.isDesignLib())
230: lDesignLib = lModel;
231: else if (lModel.isTechLib())
232: lTechLib = lModel;
233: }
234: }
235:
236: if (lDesignLib != null || lTechLib != null) {
237: DesignTreeTabDescriptor lDescriptor = new DesignTreeTabDescriptor(
238: lDesignLib, lTechLib);
239: mTabs.add(lDescriptor);
240: lDescriptor.addTab(mTabControl);
241: }
242:
243: boolean lFound = false;
244: if (lCaption != null) {
245: int lIndex = findTabDescriptorByCaption(lCaption);
246: if (lIndex > -1) {
247: mTabControl.setSelectedIndex(lIndex);
248: lFound = true;
249: }
250: }
251: if (!lFound && mTabControl.getTabCount() > 0)
252: mTabControl.setSelectedIndex(0);
253: }
254:
255: // save selected elements before tree refreshing
256: private void saveLastSelected() {
257: mLastSelected.clear();
258: for (int i = 0; i < mTabs.size(); i++) {
259: DesignTreeTabDescriptor lDescriptor = getTabDescriptor(i);
260: if (lDescriptor != null) {
261: BaseUserObject lObject = lDescriptor
262: .getCurrentUserObject();
263: if (lObject != null)
264: mLastSelected.add(new DescriptorInfo(lDescriptor
265: .getCaption(), lObject.getID()));
266: }
267: }
268: }
269:
270: private String getLastSelectedID(String pCaption) {
271: for (int i = 0; i < mLastSelected.size(); i++) {
272: DescriptorInfo lInfo = (DescriptorInfo) mLastSelected
273: .get(i);
274: if (lInfo != null && lInfo.mCaption.equals(pCaption))
275: return lInfo.mLastID;
276: }
277: return null;
278: }
279:
280: // add events listeners
281: private void addEventsListeners() {
282: Application
283: .addRefreshListener(new Application.RefreshListener() {
284: public void refresh(RefreshEvent event) {
285: try {
286: reLoadTree();
287: } catch (Throwable t) {
288: t.printStackTrace();
289: }
290: }
291: });
292: Application
293: .addOnModelChangedListener(new Application.OnModelChangedListener() {
294: public void modelChanged(OnModelChangedEvent event) {
295: switch (event.getEventSource()) {
296: case OnModelChangedEvent.MODEL_OPENED:
297: openModel(event.getModel());
298: break;
299:
300: case OnModelChangedEvent.MODEL_CLOSED:
301: closeModel(event.getModel());
302: break;
303: }
304: }
305: });
306: Application
307: .addObjectChanged(new Application.ObjectChangedListener() {
308: public void onObjectChanged(ObjectChangedEvent event) {
309: switch (event.getEventSource()) {
310: case ObjectChangedEvent.ET_EDITED:
311: checkTabName(event.getUserObject());
312: break;
313: }
314: }
315: });
316: Application
317: .addOnErrorsFoundListener(new Application.OnErrorsFoundListener() {
318: public void errorsFound(OnErrorsFoundEvent event) {
319: reloadTabIcons();
320: }
321: });
322:
323: mTabControl.addChangeListener(new ChangeListener() {
324: public void stateChanged(ChangeEvent e) {
325: Application.fireOnCurrentModelChanged();
326: }
327: });
328:
329: mTabControl.addMouseListener(new MouseAdapter() {
330: public void mousePressed(MouseEvent e) {
331: if (e.isPopupTrigger()) {
332: showPopUp(e.getX(), e.getY());
333: }
334: super .mousePressed(e);
335: }
336:
337: public void mouseReleased(MouseEvent e) {
338: if (e.isPopupTrigger()) {
339: showPopUp(e.getX(), e.getY());
340: }
341: super .mouseReleased(e);
342: }
343: });
344: }
345:
346: // get tab descriptor
347: private DesignTreeTabDescriptor getTabDescriptor(int pIndex) {
348: DesignTreeTabDescriptor lResult = null;
349: try {
350: lResult = (DesignTreeTabDescriptor) mTabs.get(pIndex);
351: } catch (Exception e) {
352: lResult = null;
353: }
354: return lResult;
355: }
356:
357: // find tab descriptor by root object
358: private DesignTreeTabDescriptor findTabDescriptor(
359: BaseUserObject pRootObject) {
360: for (int i = 0; i < mTabs.size(); i++) {
361: DesignTreeTabDescriptor lDescriptor = (DesignTreeTabDescriptor) mTabs
362: .get(i);
363: if (lDescriptor.getRootUserObject().equals(pRootObject))
364: return lDescriptor;
365: }
366: return null;
367: }
368:
369: // find tab descriptor by caption
370: private int findTabDescriptorByCaption(String pCaption) {
371: for (int i = 0; i < mTabs.size(); i++) {
372: DesignTreeTabDescriptor lDescriptor = (DesignTreeTabDescriptor) mTabs
373: .get(i);
374: if (lDescriptor.getCaption().equals(pCaption))
375: return i;
376: }
377: return -1;
378: }
379:
380: // установтиь название закладки
381: private void checkTabName(BaseUserObject pObject) {
382: DesignTreeTabDescriptor lDescriptor = findTabDescriptor(pObject);
383: if (lDescriptor != null)
384: lDescriptor.setTab(mTabControl);
385: }
386:
387: // view popup menu
388: private void showPopUp(int X, int Y) {
389: DesignTreeTabDescriptor lDescriptor = getCurentTabDescriptor();
390: if (lDescriptor != null) {
391: JPopupMenu lMenu = new JPopupMenu();
392: lMenu.add(new ValidateDescriptorAction(lDescriptor));
393: if (lDescriptor.isModified())
394: lMenu.add(new SaveDescriptorAction(lDescriptor));
395: if (!lDescriptor.isSystem()) {
396: lMenu.add(new EditPropertiesAction(lDescriptor));
397: lMenu.addSeparator();
398: lMenu.add(new CloseDescriptorAction(lDescriptor));
399: }
400: lMenu.show(this , X, Y);
401: }
402: }
403:
404: private void reloadTabIcons() {
405: for (int i = 0; i < mTabs.size(); i++) {
406: DesignTreeTabDescriptor lDescriptor = (DesignTreeTabDescriptor) mTabs
407: .get(i);
408: if (lDescriptor != null) {
409: lDescriptor.setTabIcon(mTabControl);
410: lDescriptor.fillWarnings();
411: }
412: }
413: }
414:
415: /* Auxilary classes */
416:
417: public class JMyTabbedPane extends JTabbedPane {
418: public Color getForegroundAt(int index) {
419: if (index != getSelectedIndex())
420: return Color.darkGray;
421: //return super.getForegroundAt(index);
422: else
423: return super .getForegroundAt(index);
424: //return Color.blue;
425: }
426:
427: public Icon getIconAt(int index) {
428: if (index != getSelectedIndex()) {
429: Icon lIcon = new IconUIResource(new ImageIcon(
430: GrayFilter
431: .createDisabledImage(((ImageIcon) super
432: .getIconAt(index)).getImage())));
433: //return getDisabledIconAt(index);
434: return lIcon;
435: } else
436: return super .getIconAt(index);
437: }
438: }
439:
440: public class DescriptorInfo {
441: public String mCaption = null;
442: public String mLastID = null;
443:
444: public DescriptorInfo(String pCaption, String pLastID) {
445: mCaption = pCaption;
446: mLastID = pLastID;
447: }
448: }
449:
450: /* Actions */
451:
452: /* Validate Descriptor Action */
453:
454: public class ValidateDescriptorAction extends BaseAction {
455: private DesignTreeTabDescriptor mDescriptor = null;
456:
457: public ValidateDescriptorAction(
458: DesignTreeTabDescriptor pDescriptor) {
459: super ("Validate", Application.VALIDATE_ICON);
460: mDescriptor = pDescriptor;
461: }
462:
463: public void actionPerformed(ActionEvent arg0) {
464: Object[] lPackages = mDescriptor.getPackages();
465: for (int i = 0; i < lPackages.length; i++)
466: Application
467: .validateModel((ModelUserObject) lPackages[i]);
468: }
469: }
470:
471: /* Save Descriptor Action */
472:
473: public class SaveDescriptorAction extends BaseAction {
474: private DesignTreeTabDescriptor mDescriptor = null;
475:
476: public SaveDescriptorAction(DesignTreeTabDescriptor pDescriptor) {
477: super ("Save", Application.SAVE_ICON);
478: mDescriptor = pDescriptor;
479: }
480:
481: public void actionPerformed(ActionEvent arg0) {
482: try {
483: Object[] lPackages = mDescriptor.getPackages();
484: for (int i = 0; i < lPackages.length; i++)
485: Application
486: .saveOneModel((ModelUserObject) lPackages[i]);
487: } catch (Exception e) {
488: Application.processError(e);
489: }
490: }
491: }
492:
493: /* Close Descriptor Action */
494:
495: public class CloseDescriptorAction extends BaseAction {
496: private DesignTreeTabDescriptor mDescriptor = null;
497:
498: public CloseDescriptorAction(DesignTreeTabDescriptor pDescriptor) {
499: super ("Close", null);
500: mDescriptor = pDescriptor;
501: }
502:
503: public void actionPerformed(ActionEvent arg0) {
504: Application.closeModel(mDescriptor.getCurrentPackage(),
505: false, true);
506: }
507: }
508:
509: /* Edit Properties Action */
510:
511: public class EditPropertiesAction extends BaseAction {
512: private DesignTreeTabDescriptor mDescriptor = null;
513:
514: public EditPropertiesAction(DesignTreeTabDescriptor pDescriptor) {
515: super (Application.getString("editproperties_action"),
516: Application.EDIT_ICON);
517: mDescriptor = pDescriptor;
518: }
519:
520: public void actionPerformed(ActionEvent arg0) {
521: if (mDescriptor != null)
522: mDescriptor.editProperties();
523: }
524: }
525: }
|