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.swing.treetable;
043:
044: import java.util.StringTokenizer;
045: import java.util.Vector;
046: import java.util.HashMap;
047:
048: import javax.swing.Icon;
049: import javax.swing.ImageIcon;
050: import javax.swing.JLabel;
051: import javax.swing.JTree;
052: import javax.swing.event.EventListenerList;
053: import javax.swing.event.TreeModelEvent;
054: import javax.swing.event.TreeModelListener;
055: import javax.swing.tree.DefaultMutableTreeNode;
056: import javax.swing.tree.TreeModel;
057: import javax.swing.tree.TreePath;
058:
059: import org.netbeans.modules.uml.common.ETSystem;
060: import org.netbeans.modules.uml.core.configstringframework.ConfigStringHelper;
061: import org.netbeans.modules.uml.core.configstringframework.ConfigStringTranslator;
062: import org.netbeans.modules.uml.core.configstringframework.IConfigStringTranslator;
063: import org.netbeans.modules.uml.core.metamodel.core.foundation.IElement;
064: import org.netbeans.modules.uml.core.metamodel.core.foundation.IPresentationElement;
065: import org.netbeans.modules.uml.core.support.umlutils.IPropertyDefinition;
066: import org.netbeans.modules.uml.core.support.umlutils.IPropertyElement;
067: import org.netbeans.modules.uml.core.support.umlutils.PropertyElement;
068:
069: //import org.netbeans.modules.uml.ui.addins.webreport.WebRPTFormatDlg;
070:
071: public class RPTWizPropertyTreeTableModel implements TreeTableModel {
072:
073: // Names of the columns.
074: static protected String[] cNames = { " ", " ", " " };
075:
076: // Types of the columns.
077: static protected Class[] cTypes = { ImageIcon.class,
078: PropertyElement.class, String.class };
079:
080: private Vector<Object> m_Children = null;
081:
082: private HashMap<Object, Vector<Object>> m_BuiltChildren = new HashMap<Object, Vector<Object>>();
083:
084: private Vector<Icon> m_Icons = new Vector<Icon>();
085:
086: protected JDefaultMutableTreeNode root = null;
087:
088: private static JTreeTable treeTable = null;
089:
090: protected EventListenerList listenerList = new EventListenerList();
091: // private WebRPTFormatDlg m_editor = null;
092: private ConfigStringTranslator m_Translator = new ConfigStringTranslator();
093:
094: public RPTWizPropertyTreeTableModel() {
095: super ();
096:
097: }
098:
099: // public RPTWizPropertyTreeTableModel(JDefaultMutableTreeNode root, WebRPTFormatDlg editor) {
100: // super();
101: // this.root = root;
102: // m_editor = editor;
103: // }
104:
105: public RPTWizPropertyTreeTableModel(JDefaultMutableTreeNode root,
106: IPropertyElement ele) {
107: super ();
108: this .root = root;
109: JDefaultMutableTreeNode node = new JDefaultMutableTreeNode(ele,
110: true);
111: root.add(node);
112:
113: }
114:
115: public static void main(String[] args) {
116: }
117:
118: public int getColumnCount() {
119: return cNames.length;
120: }
121:
122: public String getColumnName(int column) {
123: return cNames[column];
124: }
125:
126: public Class getColumnClass(int column) {
127: if (column == 1) {
128: return this .getClass();
129: }
130: if (column <= cTypes.length)
131: return cTypes[column];
132: return null;
133: }
134:
135: public Object getValueAt(Object node, int column) {
136:
137: if (node instanceof JDefaultMutableTreeNode) {
138: JDefaultMutableTreeNode n = (JDefaultMutableTreeNode) node;
139: Object obj = n.getUserObject();
140:
141: if (obj instanceof IPropertyElement) {
142:
143: IPropertyElement ele = (IPropertyElement) n
144: .getUserObject();
145:
146: if (column == 1) {
147: IPropertyDefinition pDef = ele
148: .getPropertyDefinition();
149: if (pDef != null) {
150: if (n.isRoot()) {
151: return "testing Display Name";
152: // return m_editor.calculateTopElementName(ele);
153: } else {
154: return pDef.getDisplayName();
155: }
156: }
157: } else if (column == 2) {
158: String retValue = null;
159: String value = ele.getTranslatedValue();
160: retValue = value;
161:
162: try {
163: IPropertyDefinition pDef = ele
164: .getPropertyDefinition();
165: if (pDef != null) {
166: long mult = pDef.getMultiplicity();
167: if (mult > 1) {
168: //here we do not want to show anything
169: retValue = null;
170: } else {
171: int enumVal = Integer.valueOf(value)
172: .intValue();
173: //there is a possibility that enums are specified in definitions,
174: //in which case the value obtained will not be correct.
175: String enumValues = pDef
176: .getFromAttrMap("enumValues");
177: if (enumValues != null) {
178: StringTokenizer tokenizer = new StringTokenizer(
179: enumValues, "|");
180: int counter = 0;
181: while (tokenizer.hasMoreTokens()) {
182: String token = tokenizer
183: .nextToken();
184: if (token.equals(value)) {
185: enumVal = counter;
186: break;
187: }
188: counter++;
189: }
190: }
191:
192: //here we want to find out the enum value and show the
193: //translated value.
194: String values = pDef.getValidValues();
195: if (values != null
196: && values.indexOf("|") >= 0) {
197: StringTokenizer tokenizer = new StringTokenizer(
198: values, "|");
199: int j = 0;
200: while (tokenizer.hasMoreTokens()) {
201: String token = tokenizer
202: .nextToken();
203: if (j == enumVal) {
204: String transVal = m_Translator
205: .translate(pDef,
206: token);
207: retValue = transVal;
208: }
209: j++;
210: }
211: }
212: }
213: }
214: } catch (NumberFormatException e) {
215: }
216: return retValue;
217:
218: } else if (column == 0) {
219: Object obj1 = getRoot();
220: if (obj1 instanceof JDefaultMutableTreeNode) {
221: JDefaultMutableTreeNode root = (JDefaultMutableTreeNode) obj1;
222:
223: //We want to show images only if there is only one node selected.
224: int countChild = root.getChildCount();
225: if (countChild == 1) {
226: root = (JDefaultMutableTreeNode) root
227: .getChildAt(0);
228: int index = root.getIndex(n);
229: //ETSystem.out.println("Index for " + ele.getName() + " " + index);
230: // if (m_Icons.size() == 0)
231: // {
232: // m_Icons = m_editor.loadImages(root);
233: // }
234: // if (m_Icons != null && m_Icons.size() > index+1)
235: // {
236: // //check to see if we have filtered the list in any way
237: // if (m_editor.isShowingFilteredOnIcons() ||
238: // m_editor.isShowingComboFilteredList())
239: // {
240: // //an already showing filtered list, so need to
241: // // show only the first icon.
242: // if (index == -1 && n.equals(root))
243: // {
244: // return m_Icons.elementAt(index+1);
245: // }
246: // else
247: // {
248: // return null;
249: // }
250: // }
251: // else
252: // {
253: // //get the right icon.
254: // if (index == -1 && n.equals(root))
255: // {
256: // return m_Icons.elementAt(index+1);
257: // }
258: // else if (index >= 0)
259: // {
260: // return m_Icons.elementAt(index+1);
261: // }
262: // }
263: // }
264: // else
265: // {
266: // return null;
267: // }
268: }
269: }
270: }
271: }
272: }
273: return null;
274: }
275:
276: public boolean isCellEditable(Object node, int column) {
277: return true;
278: }
279:
280: public void setValueAt(Object aValue, Object node, int column) {
281: if (aValue != null && node != null) {
282: ETSystem.out.println("Calling setValueAt for " + column
283: + aValue.toString() + node.toString());
284: }
285: if (aValue instanceof JTreeTable.TreeTableCellRenderer) {
286: JTreeTable.TreeTableCellRenderer rend = (JTreeTable.TreeTableCellRenderer) aValue;
287:
288: }
289: //m_editor.processLastCell();
290: }
291:
292: public Object getChild(Object parent, int index) {
293: Object retObj = null;
294: if (parent != null) {
295: JDefaultMutableTreeNode node = (JDefaultMutableTreeNode) parent;
296: int count = node.getChildCount();
297: if (count > 0 && index <= count) {
298: retObj = node.getChildAt(index);
299: } else {
300: retObj = getChildren(parent)[index];
301: }
302: }
303: return retObj;
304: }
305:
306: protected Object[] getChildren(Object node) {
307: Object[] retObj = null;
308:
309: JDefaultMutableTreeNode n = (JDefaultMutableTreeNode) node;
310: int count = n.getChildCount();
311: if (count == 0) {
312: IPropertyElement ele = (IPropertyElement) n.getUserObject();
313: // Vector < Object > children = this.buildSubElementsThatNeedToDisplay(ele, n);
314: // retObj = children.toArray();
315: }
316:
317: return retObj;
318: }
319:
320: public void setChildren(Vector<Object> newChildren) {
321: m_Children.clear();
322: m_Children = newChildren;
323: }
324:
325: /* (non-Javadoc)
326: * @see javax.swing.tree.TreeModel#getChildCount(java.lang.Object)
327: */
328: public int getChildCount(Object parent) {
329: int retCount = 0;
330: if (parent != null) {
331: JDefaultMutableTreeNode node = (JDefaultMutableTreeNode) parent;
332: int count = node.getChildCount();
333: if (count > 0) {
334: retCount = count;
335: } else {
336: retCount = getChildren(parent).length;
337: }
338: }
339: return retCount;
340: }
341:
342: /* (non-Javadoc)
343: * @see javax.swing.tree.TreeModel#isLeaf(java.lang.Object)
344: */
345: public boolean isLeaf(Object node) {
346: if (node instanceof JDefaultMutableTreeNode) {
347: JDefaultMutableTreeNode n = (JDefaultMutableTreeNode) node;
348: Object obj = n.getUserObject();
349: if (obj != null && obj instanceof IPropertyElement) {
350: IPropertyElement ele = (IPropertyElement) obj;
351: IPropertyDefinition pDef = ele.getPropertyDefinition();
352: long mult = pDef.getMultiplicity();
353: String name = ele.getName();
354: Vector subDefs = pDef.getSubDefinitions();
355: int subDefCount = 0;
356: if (subDefs != null) {
357: subDefCount = subDefs.size();
358: }
359: if (mult <= 1) {
360: Vector elems = ele.getSubElements();
361: if (elems == null
362: || (elems != null && elems.size() == 0)) {
363: if (name != null && name.equals("dummy")
364: && subDefCount > 0) {
365: return false;
366: } else {
367: return true;
368: }
369: }
370: }
371: }
372: }
373: return false;
374: }
375:
376: /* (non-Javadoc)
377: * @see javax.swing.tree.TreeModel#valueForPathChanged(javax.swing.tree.TreePath, java.lang.Object)
378: */
379: public void valueForPathChanged(TreePath path, Object newValue) {
380:
381: }
382:
383: /* (non-Javadoc)
384: * @see javax.swing.tree.TreeModel#getIndexOfChild(java.lang.Object, java.lang.Object)
385: */
386: public int getIndexOfChild(Object parent, Object child) {
387: for (int i = 0; i < getChildCount(parent); i++) {
388: if (getChild(parent, i).equals(child)) {
389: return i;
390: }
391: }
392: return -1;
393: }
394:
395: /* (non-Javadoc)
396: * @see javax.swing.tree.TreeModel#addTreeModelListener(javax.swing.event.TreeModelListener)
397: */
398: public void addTreeModelListener(TreeModelListener l) {
399: listenerList.add(TreeModelListener.class, l);
400: }
401:
402: /* (non-Javadoc)
403: * @see javax.swing.tree.TreeModel#removeTreeModelListener(javax.swing.event.TreeModelListener)
404: */
405: public void removeTreeModelListener(TreeModelListener l) {
406: listenerList.remove(TreeModelListener.class, l);
407:
408: }
409:
410: /* (non-Javadoc)
411: * @see javax.swing.tree.TreeModel#getRoot()
412: */
413: public Object getRoot() {
414: return root;
415: }
416:
417: /*
418: * Notify all listeners that have registered interest for
419: * notification on this event type. The event instance
420: * is lazily created using the parameters passed into
421: * the fire method.
422: * @see EventListenerList
423: */
424: protected void fireTreeStructureChanged(Object source,
425: Object[] path, int[] childIndices, Object[] children) {
426: // Guaranteed to return a non-null array
427: Object[] listeners = listenerList.getListenerList();
428: TreeModelEvent e = null;
429: // Process the listeners last to first, notifying
430: // those that are interested in this event
431: for (int i = listeners.length - 2; i >= 0; i -= 2) {
432: if (listeners[i] == TreeModelListener.class) {
433: // Lazily create the event:
434: if (e == null)
435: e = new TreeModelEvent(source, path, childIndices,
436: children);
437: ((TreeModelListener) listeners[i + 1])
438: .treeStructureChanged(e);
439: }
440: }
441: }
442:
443: /*
444: * Notify all listeners that have registered interest for
445: * notification on this event type. The event instance
446: * is lazily created using the parameters passed into
447: * the fire method.
448: * @see EventListenerList
449: */
450: protected void fireTreeNodesChanged(Object source, Object[] path,
451: int[] childIndices, Object[] children) {
452: // Guaranteed to return a non-null array
453: Object[] listeners = listenerList.getListenerList();
454: TreeModelEvent e = null;
455: // Process the listeners last to first, notifying
456: // those that are interested in this event
457: for (int i = listeners.length - 2; i >= 0; i -= 2) {
458: if (listeners[i] == TreeModelListener.class) {
459: // Lazily create the event:
460: if (e == null)
461: e = new TreeModelEvent(source, path, childIndices,
462: children);
463: ((TreeModelListener) listeners[i + 1])
464: .treeNodesChanged(e);
465: }
466: }
467: }
468:
469: /*
470: * Notify all listeners that have registered interest for
471: * notification on this event type. The event instance
472: * is lazily created using the parameters passed into
473: * the fire method.
474: * @see EventListenerList
475: */
476: protected void fireTreeNodesInserted(Object source, Object[] path,
477: int[] childIndices, Object[] children) {
478: // Guaranteed to return a non-null array
479: Object[] listeners = listenerList.getListenerList();
480: TreeModelEvent e = null;
481: // Process the listeners last to first, notifying
482: // those that are interested in this event
483: for (int i = listeners.length - 2; i >= 0; i -= 2) {
484: if (listeners[i] == TreeModelListener.class) {
485: // Lazily create the event:
486: if (e == null)
487: e = new TreeModelEvent(source, path, childIndices,
488: children);
489: ((TreeModelListener) listeners[i + 1])
490: .treeNodesInserted(e);
491: }
492: }
493: }
494:
495: /*
496: * Notify all listeners that have registered interest for
497: * notification on this event type. The event instance
498: * is lazily created using the parameters passed into
499: * the fire method.
500: * @see EventListenerList
501: */
502: protected void fireTreeNodesRemoved(Object source, Object[] path,
503: int[] childIndices, Object[] children) {
504: // Guaranteed to return a non-null array
505: Object[] listeners = listenerList.getListenerList();
506: TreeModelEvent e = null;
507: // Process the listeners last to first, notifying
508: // those that are interested in this event
509: for (int i = listeners.length - 2; i >= 0; i -= 2) {
510: if (listeners[i] == TreeModelListener.class) {
511: // Lazily create the event:
512: if (e == null)
513: e = new TreeModelEvent(source, path, childIndices,
514: children);
515: ((TreeModelListener) listeners[i + 1])
516: .treeNodesRemoved(e);
517: }
518: }
519: }
520:
521: public void expand(int row, boolean val) {
522: treeTable.getTree().expandNode(row, val);
523:
524: }
525:
526: public void setTreeTable(JTreeTable tree) {
527: treeTable = tree;
528: }
529:
530: public Vector<Icon> getIcons() {
531: return m_Icons;
532: }
533:
534: public String calculateTopElementName(IPropertyElement pEle) {
535: String topName = "";
536: IPropertyDefinition pDef = pEle.getPropertyDefinition();
537: if (pDef != null) {
538: Object pDisp = pEle.getElement();
539: if (pDisp != null) {
540: IElement pElement = null;
541: if (pDisp instanceof IPresentationElement) {
542: IPresentationElement pPres = (IPresentationElement) pDisp;
543: pElement = pPres.getFirstSubject();
544: } else if (pDisp instanceof IElement) {
545: pElement = (IElement) pDisp;
546: }
547:
548: if (pElement != null) {
549: IConfigStringTranslator trans = ConfigStringHelper
550: .instance().getTranslator();
551: if (trans != null) {
552: String expandedEleType = pElement
553: .getExpandedElementType();
554:
555: if (expandedEleType.equals("AbortedFinalState")) {
556: topName = trans.translate(null,
557: "PSK_ABORTEDFINALSTATE");
558: } else if (expandedEleType.equals("FinalState")) {
559: topName = trans.translate(null,
560: "PSK_FINALSTATE");
561: } else if (expandedEleType
562: .equals("ChoicePseudoState")) {
563: topName = trans.translate(null,
564: "PSK_CHOICEPSUEDOSTATE");
565: } else if (expandedEleType
566: .equals("DeepHistoryState")) {
567: topName = trans.translate(null,
568: "PSK_DEEPHISTORY");
569: } else if (expandedEleType.equals("ForkState")) {
570: topName = trans.translate(null,
571: "PSKPSUEDOSTATE");
572: } else if (expandedEleType
573: .equals("InitialState")) {
574: topName = trans.translate(null,
575: "PSK_INITIALSTATE");
576: } else if (expandedEleType.equals("JoinState")) {
577: topName = trans.translate(null,
578: "PSK_JOINSTATE");
579: } else if (expandedEleType
580: .equals("JunctionState")) {
581: topName = trans.translate(null,
582: "PSK_JUNCTION");
583: } else if (expandedEleType
584: .equals("ShallowHistoryState")) {
585: topName = trans.translate(null,
586: "PSK_SHALLOWHISTORY");
587: } else if (expandedEleType
588: .equals("EntryPointState")) {
589: topName = trans.translate(null,
590: "PSK_ENTRYPOINT");
591: } else if (expandedEleType.equals("StopState")) {
592: topName = trans.translate(null,
593: "PSK_STOPSTATE");
594: } else if (expandedEleType
595: .equals("CompositeState")) {
596: topName = trans.translate(null,
597: "PSK_COMPOSITESTATE");
598: } else if (expandedEleType
599: .equals("SubmachineState")) {
600: topName = trans.translate(null,
601: "PSK_SUBMACHINESTATE");
602: } else if (expandedEleType
603: .equals("DesignPattern")) {
604: topName = trans.translate(null,
605: "PSK_DESIGNPATTERN");
606: } else {
607: topName = pDef.getDisplayName();
608: }
609: }
610: } else {
611: topName = pDef.getDisplayName();
612: }
613: }
614: }
615: return topName;
616: }
617:
618: }
|