001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.uml.ui.controls.newdialog;
043:
044: import java.awt.Color;
045: import java.awt.Component;
046: import java.awt.Dimension;
047: import java.awt.GridBagConstraints;
048: import java.awt.GridBagLayout;
049: import java.awt.Insets;
050: import java.io.File;
051: import java.util.List;
052: import java.util.Vector;
053:
054: import javax.swing.Icon;
055: import javax.swing.ImageIcon;
056: import javax.swing.JLabel;
057: import javax.swing.JList;
058: import javax.swing.JPanel;
059: import javax.swing.JSplitPane;
060: import javax.swing.JTree;
061: import javax.swing.ListCellRenderer;
062: import javax.swing.ListSelectionModel;
063: import javax.swing.event.TreeSelectionEvent;
064: import javax.swing.event.TreeSelectionListener;
065: import javax.swing.tree.DefaultMutableTreeNode;
066: import javax.swing.tree.DefaultTreeModel;
067: import javax.swing.tree.TreeNode;
068: import javax.swing.tree.TreePath;
069: import javax.swing.tree.TreeSelectionModel;
070:
071: import org.dom4j.Document;
072:
073: import org.netbeans.modules.uml.core.metamodel.core.foundation.ConfigManager;
074: import org.netbeans.modules.uml.core.metamodel.core.foundation.IConfigManager;
075: import org.netbeans.modules.uml.core.support.umlsupport.ProductRetriever;
076: import org.netbeans.modules.uml.core.support.umlsupport.XMLManip;
077: import org.netbeans.modules.uml.ui.support.commonresources.CommonResourceManager;
078: import org.netbeans.modules.uml.ui.support.wizard.IWizardSheet;
079: import org.netbeans.modules.uml.ui.support.wizard.WizardInteriorPage;
080:
081: /**
082: * @author sumitabhk
083: *
084: */
085: public class ElementSelectionUI extends WizardInteriorPage implements
086: INewDialogUI {
087: private static final String PG_CAPTION = NewDialogResources
088: .getString("NewDiagramUI.NEWWIZARD_CAPTION");
089: private static final String PG_TITLE = NewDialogResources
090: .getString("IDS_TYPESELECTION");
091: private static final String PG_SUBTITLE = NewDialogResources
092: .getString("IDS_TYPESELECTIONHELP");
093:
094: private JList m_TabPane = null;
095: private JTree m_Tree = null;
096: private JPanel treePanel = null;
097: private JPanel listPanel = null;
098: private JSplitPane jSplitPane = null;
099: private Document m_doc = null;
100:
101: /**
102: *
103: */
104: // public ElementSelectionUI()
105: // {
106: // super();
107: // initializeControls();
108: // }
109: public ElementSelectionUI(IWizardSheet parent, String caption,
110: String headerTitle, String headerSubTitle) {
111: super (parent, caption, headerTitle, headerSubTitle);
112: createUI();
113: }
114:
115: public ElementSelectionUI(IWizardSheet parent) {
116: this (parent, PG_CAPTION, PG_TITLE, PG_SUBTITLE);
117: }
118:
119: protected void createUI() {
120: super .createUI();
121:
122: JPanel topPane = new JPanel();
123:
124: IConfigManager conMan = ProductRetriever.retrieveProduct()
125: .getConfigManager();
126: String fileName = conMan.getDefaultConfigLocation();
127: fileName += "NewDialogDefinitions.etc";
128: m_doc = XMLManip.getDOMDocument(fileName);
129: org.dom4j.Node node = m_doc
130: .selectSingleNode("//PropertyDefinitions/PropertyDefinition");
131: m_TabPane = new JList();
132: if (node != null) {
133: org.dom4j.Element elem = (org.dom4j.Element) node;
134: String name = elem.attributeValue("name");
135:
136: Vector elements = new Vector();
137: List nodeList = m_doc
138: .selectNodes("//PropertyDefinition/aDefinition");
139: if (m_TabPane != null) {
140: int count = nodeList.size();
141: for (int i = 0; i < count; i++) {
142: org.dom4j.Element subNode = (org.dom4j.Element) nodeList
143: .get(i);
144: String subName = subNode.attributeValue("name");
145: String imageName = subNode.attributeValue("image");
146: File file = new File(imageName);
147:
148: //ETSystem.out.println(fileName + file.exists());
149: Icon retIcon = CommonResourceManager.instance()
150: .getIconForFile(imageName);
151:
152: JLabel label = new JLabel(subName);
153: label.setIcon(retIcon);
154:
155: //add workspace node
156: elements.add(label);
157: }
158: }
159:
160: m_TabPane.setListData(elements);
161: m_TabPane.setSelectedIndex(0);
162: }
163:
164: GridBagConstraints gridBagConstraints = new GridBagConstraints();
165: gridBagConstraints.fill = GridBagConstraints.BOTH;
166: topPane.add(m_TabPane, gridBagConstraints);
167: //m_Tree.setSelectionRow(0);
168:
169: pnlContents.setLayout(new GridBagLayout());
170: pnlContents.setBackground(Color.white);
171: pnlContents.add(topPane, new GridBagConstraints(0, 0, 1, 1,
172: 1.0, 1.0, GridBagConstraints.CENTER,
173: GridBagConstraints.BOTH, new Insets(12, 9, 11, 10),
174: 204, 289));
175: }
176:
177: protected boolean onInitDialog() {
178: return super .onInitDialog();
179: }
180:
181: public void onWizardBack() {
182: //super.onWizardBack();
183: }
184:
185: public void onWizardNext() {
186: IWizardSheet parent = getParentSheet();
187: if (m_TabPane != null) {
188: Object val = m_TabPane.getSelectedValue();
189: if (val != null && val instanceof String) {
190: if (parent instanceof JDefaultNewDialog) {
191: String str = (String) val;
192: if (val.equals("Workspace")) {
193: //add next page as workspace page.
194: ((JDefaultNewDialog) parent).setNextPage(
195: new NewWorkspaceUI(parent),
196: "Create Workspace");
197: } else if (val.equals("Project")) {
198: //add next page as workspace page.
199: ((JDefaultNewDialog) parent).setNextPage(
200: new NewProjectUI(parent),
201: "Create Project");
202: } else if (val.equals("Diagram")) {
203: //add next page as workspace page.
204: ((JDefaultNewDialog) parent).setNextPage(
205: new NewDiagramSelectionUI(parent),
206: "Select Diagram Type");
207: ((JDefaultNewDialog) parent).setNextPage(
208: new NewDiagramUI(parent),
209: "Create Diagram");
210: } else if (val.equals("Element")) {
211: //add next page as workspace page.
212: ((JDefaultNewDialog) parent).setNextPage(
213: new NewElementSelectionUI(parent),
214: "Select Element Type");
215: ((JDefaultNewDialog) parent).setNextPage(
216: new NewElementSelectionUI(parent),
217: "Create Project");
218: } else if (val.equals("Package")) {
219: //add next page as workspace page.
220: ((JDefaultNewDialog) parent).setNextPage(
221: new NewPackageUI(parent),
222: "Create Package");
223: }
224: }
225: }
226: }
227: super .onWizardNext();
228: }
229:
230: /* (non-Javadoc)
231: * @see org.netbeans.modules.uml.ui.controls.newdialog.INewDialogUI#nextButtonClicked()
232: */
233: public JPanel nextButtonClicked() {
234: // TODO Auto-generated method stub
235: //need to show the user right dialog based on what is selected.
236: JPanel retPanel = showWindowForUserInput();
237: return retPanel;
238: }
239:
240: /* (non-Javadoc)
241: * @see org.netbeans.modules.uml.ui.controls.newdialog.INewDialogUI#finishButtonClicked()
242: */
243: public INewDialogTabDetails finishButtonClicked() {
244: // TODO Auto-generated method stub
245: return null;
246: }
247:
248: protected void initializeControls() {
249: m_TabPane = new JList();
250: m_TabPane.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
251: m_TabPane.setCellRenderer(new ElementListCellRenderer());
252: m_TabPane.setPreferredSize(new Dimension(200, 200));
253: m_TabPane.setLayout(new GridBagLayout());
254:
255: IConfigManager conMan = ProductRetriever.retrieveProduct()
256: .getConfigManager();
257: String fileName = conMan.getDefaultConfigLocation();
258: fileName += "NewDialogDefinitions.etc";
259: m_doc = XMLManip.getDOMDocument(fileName);
260: org.dom4j.Node node = m_doc
261: .selectSingleNode("//PropertyDefinitions/PropertyDefinition");
262: if (node != null) {
263: org.dom4j.Element elem = (org.dom4j.Element) node;
264: String name = elem.attributeValue("name");
265: DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode(
266: name);
267: NewDialogTreeModel model = new NewDialogTreeModel(treeNode);
268: m_Tree = new JTree(model);
269: m_Tree.getSelectionModel().setSelectionMode(
270: TreeSelectionModel.SINGLE_TREE_SELECTION);
271: m_Tree.setShowsRootHandles(true);
272: m_Tree
273: .addTreeSelectionListener(new ElementTreeSelectionListener());
274: }
275:
276: jSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
277: m_Tree, m_TabPane);
278: setPreferredSize(new Dimension(400, 400));
279: jSplitPane.setDividerLocation(200);
280:
281: GridBagConstraints gridBagConstraints = new GridBagConstraints();
282: gridBagConstraints.fill = GridBagConstraints.BOTH;
283: add(jSplitPane, gridBagConstraints);
284: doLayout();
285: m_Tree.setSelectionRow(0);
286: }
287:
288: private JPanel showWindowForUserInput() {
289: JPanel retPanel = null;
290:
291: String selOnTab = getSelectionOnTabPane();
292: String selOnTree = getSelectionOnTree();
293:
294: // if (selOnTree.equals("Workspace"))
295: // {
296: // NewWorkspaceUI newUI = new NewWorkspaceUI();
297: // newUI.setElementType(selOnTab);
298: // retPanel = newUI;
299: // }
300: // else if (selOnTree.equals("Project"))
301: // {
302: // NewProjectUI newUI = new NewProjectUI();
303: // newUI.setElementType(selOnTab);
304: // retPanel = newUI;
305: // }
306: // else if (selOnTree.equals("Package"))
307: // {
308: // NewPackageUI newUI = new NewPackageUI();
309: // newUI.setElementType(selOnTab);
310: // retPanel = newUI;
311: // }
312: // else if (selOnTree.equals("Diagram"))
313: // {
314: // NewDiagramUI newUI = new NewDiagramUI();
315: // newUI.setElementType(selOnTab);
316: // retPanel = newUI;
317: // }
318: // else if (selOnTree.equals("Element"))
319: // {
320: // NewElementUI newUI = new NewElementUI();
321: // newUI.setElementType(selOnTab);
322: // retPanel = newUI;
323: // }
324:
325: return retPanel;
326: }
327:
328: public String getSelectionOnTabPane() {
329: try {
330: return (String) m_TabPane.getSelectedValue();
331: } catch (Exception e) {
332: //do nothing, as when nothing is selected this will throw.
333: }
334: return "";
335: }
336:
337: public String getSelectionOnTree() {
338: String str = null;
339: TreePath path = m_Tree.getSelectionPath();
340: if (path != null) {
341: Object obj = m_Tree.getLastSelectedPathComponent();
342: if (obj instanceof DefaultMutableTreeNode) {
343: DefaultMutableTreeNode node = (DefaultMutableTreeNode) obj;
344: Object objStr = node.getUserObject();
345: if (objStr != null) {
346: str = (String) objStr;
347: }
348: }
349: }
350: return str;
351: }
352:
353: private class ElementTreeSelectionListener implements
354: TreeSelectionListener {
355:
356: /* (non-Javadoc)
357: * @see javax.swing.event.TreeSelectionListener#valueChanged(javax.swing.event.TreeSelectionEvent)
358: */
359: public void valueChanged(TreeSelectionEvent arg0) {
360: // TODO Auto-generated method stub
361: Object obj = arg0.getSource();
362: if (obj != null && obj instanceof JTree) {
363: JTree tree = (JTree) obj;
364: TreePath path = tree.getSelectionPath();
365: if (path != null) {
366: int i = m_Tree.getRowForPath(path);
367: Object selObj = m_Tree
368: .getLastSelectedPathComponent();
369: if (selObj instanceof DefaultMutableTreeNode) {
370: DefaultMutableTreeNode node = (DefaultMutableTreeNode) selObj;
371: Object objStr = node.getUserObject();
372: if (objStr != null) {
373: populateRightHandSide((String) objStr);
374: }
375: }
376: //ETSystem.out.println("Selected row = " + i);
377: }
378: //path.get
379:
380: }
381: }
382:
383: /**
384: * @param string
385: */
386: private void populateRightHandSide(String str) {
387: Vector elements = getValidItemsToDisplay(str);
388: if (elements != null) {
389: m_TabPane.setListData(elements);
390: m_TabPane.setSelectedIndex(0);
391: }
392: }
393:
394: /**
395: * @param str
396: * @return
397: */
398: private Vector<String> getValidItemsToDisplay(String str) {
399: Vector<String> retObj = new Vector<String>();
400: // TODO Auto-generated method stub
401:
402: List list = m_doc
403: .selectNodes("//PropertyDefinition/aDefinition[@name='"
404: + str + "']/aDefinition");
405: if (list != null) {
406: int count = list.size();
407: for (int i = 0; i < count; i++) {
408: org.dom4j.Element ele = (org.dom4j.Element) list
409: .get(i);
410: String name = ele.attributeValue("name");
411: if (name != null)
412: retObj.add(name);
413: else
414: retObj.add("");
415: }
416: }
417:
418: // if (str.equals("Workspace"))
419: // {
420: // retObj.add("Workspace");
421: // }
422: // else if (str.equals("Project"))
423: // {
424: // retObj.add("Project");
425: // }
426: // else if (str.equals("Package"))
427: // {
428: // retObj.add("Package");
429: // }
430: // else if (str.equals("Diagram"))
431: // {
432: // retObj.add("Class Diagram");
433: // retObj.add("Sequence Diagram");
434: // retObj.add("Activity Diagram");
435: // retObj.add("Collaboration Diagram");
436: // retObj.add("Component Diagram");
437: // retObj.add("Deployment Diagram");
438: // retObj.add("State Diagram");
439: // retObj.add("Use Case Diagram");
440: // }
441: // else if (str.equals("Element"))
442: // {
443: // retObj.add("Interface");
444: // retObj.add("Class");
445: // retObj.add("Actor");
446: // retObj.add("Attribute");
447: // retObj.add("Operation");
448: // }
449: // else
450: // {
451: // retObj.add(" ");
452: // }
453: return retObj;
454: }
455: }
456:
457: public class NewDialogTreeModel extends DefaultTreeModel {
458:
459: /**
460: *
461: */
462: public NewDialogTreeModel(TreeNode node) {
463: super (node);
464: buildChildren();
465:
466: //collapse the root node
467: }
468:
469: /**
470: *
471: */
472: private void buildChildren() {
473: // TODO Auto-generated method stub
474: Object obj = this .getRoot();
475: if (obj instanceof DefaultMutableTreeNode) {
476: DefaultMutableTreeNode root = (DefaultMutableTreeNode) obj;
477:
478: List list = m_doc
479: .selectNodes("//PropertyDefinition/aDefinition");
480: if (list != null) {
481: int count = list.size();
482: for (int i = 0; i < count; i++) {
483: org.dom4j.Element node = (org.dom4j.Element) list
484: .get(i);
485: String name = node.attributeValue("name");
486:
487: //add workspace node
488: DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(
489: name);
490: root.add(newNode);
491: }
492: }
493: //add workspace node
494: // DefaultMutableTreeNode wksNode = new DefaultMutableTreeNode("Workspace");
495: // DefaultMutableTreeNode prjNode = new DefaultMutableTreeNode("Project");
496: // DefaultMutableTreeNode pkgNode = new DefaultMutableTreeNode("Package");
497: // DefaultMutableTreeNode diaNode = new DefaultMutableTreeNode("Diagram");
498: // DefaultMutableTreeNode eleNode = new DefaultMutableTreeNode("Element");
499: //
500: // root.add(wksNode);
501: // root.add(prjNode);
502: // root.add(pkgNode);
503: // root.add(diaNode);
504: // root.add(eleNode);
505: }
506: }
507: }
508:
509: class ElementListCellRenderer extends JLabel implements
510: ListCellRenderer {
511: public Icon getImageIcon(int index) {
512: Icon retIcon = null;
513: int i = index + 1;
514: String str = "//PropertyDefinition/aDefinition[@name='"
515: + getSelectionOnTree() + "']/aDefinition[" + i
516: + "]";
517: org.dom4j.Node node = m_doc.selectSingleNode(str);
518: //ETSystem.out.println("Searching for " + str);
519: if (node.getNodeType() == org.dom4j.Element.ELEMENT_NODE) {
520: org.dom4j.Element elem = (org.dom4j.Element) node;
521: String fileName = elem.attributeValue("image");
522: File file = new File(fileName);
523:
524: //ETSystem.out.println(fileName + file.exists());
525: retIcon = CommonResourceManager.instance()
526: .getIconForFile(fileName);
527: }
528: return retIcon;
529: }
530:
531: public Component getListCellRendererComponent(JList list,
532: Object value, // value to display
533: int index, // cell index
534: boolean isSelected, // is the cell selected
535: boolean cellHasFocus) // the list and the cell have the focus
536: {
537: String s = value.toString();
538: setText(s);
539: setIcon(getImageIcon(index));
540: if (isSelected) {
541: setBackground(list.getSelectionBackground());
542: setForeground(list.getSelectionForeground());
543: } else {
544: setBackground(list.getBackground());
545: setForeground(list.getForeground());
546: }
547: setEnabled(list.isEnabled());
548: setFont(list.getFont());
549: setOpaque(true);
550: return this ;
551: }
552: }
553:
554: /* (non-Javadoc)
555: * @see org.netbeans.modules.uml.ui.controls.newdialog.INewDialogUI#getHelpText()
556: */
557: public String getHelpText() {
558: return NewDialogResources.getString("IDS_ITEMTOCREATE");
559: }
560:
561: /* (non-Javadoc)
562: * @see org.netbeans.modules.uml.ui.controls.newdialog.INewDialogUI#getHelpIcon()
563: */
564: public Icon getHelpIcon() {
565: return null;
566: }
567:
568: public boolean enableNextButton() {
569: String selOnTab = getSelectionOnTabPane();
570: if (selOnTab == null || selOnTab.length() == 0) {
571: return false;
572: }
573: return true;
574: }
575: }
|