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: /*
043: * StereotypeCustomizer.java
044: *
045: * Created on March 15, 2005, 9:20 AM
046: */
047:
048: package org.netbeans.modules.uml.propertysupport.customizers;
049:
050: import org.netbeans.modules.uml.core.metamodel.profiles.IStereotype;
051: import org.netbeans.modules.uml.core.support.umlutils.IPropertyDefinition;
052: import org.netbeans.modules.uml.core.support.umlutils.IPropertyElement;
053: import org.netbeans.modules.uml.core.support.umlutils.IPropertyElementManager;
054: import org.netbeans.modules.uml.propertysupport.DefinitionPropertyBuilder;
055: import org.netbeans.modules.uml.propertysupport.customizers.PropertyDataFormatter;
056: import org.netbeans.modules.uml.propertysupport.nodes.CustomPropertyEditor;
057:
058: import java.awt.Component;
059: import java.util.ArrayList;
060: import java.util.ResourceBundle;
061: import java.util.Vector;
062: import javax.swing.Box;
063:
064: import org.netbeans.modules.uml.core.metamodel.core.foundation.IElement;
065: import javax.swing.JPanel;
066: import javax.swing.JTextField;
067: import javax.swing.table.AbstractTableModel;
068: import javax.swing.table.TableCellRenderer;
069: import javax.swing.table.TableColumn;
070: import javax.swing.table.DefaultTableCellRenderer;
071: import javax.swing.DefaultCellEditor;
072: import javax.swing.event.ListSelectionEvent;
073: import javax.swing.event.ListSelectionListener;
074: import javax.swing.event.TableModelEvent;
075:
076: import org.openide.explorer.propertysheet.editors.EnhancedCustomPropertyEditor;
077: import org.openide.nodes.Node;
078: import org.openide.util.NbBundle;
079: import org.netbeans.modules.uml.core.support.Debug;
080:
081: /**
082: *
083: * @author Administrator
084: */
085: public class PropertyElementCustomizer extends JPanel implements
086: Customizer, EnhancedCustomPropertyEditor {
087: //private ResourceBundle mBundle = ResourceBundle.getBundle("org/netbeans/modules/uml/propertysupport/customizers/Bundle");
088: private ResourceBundle mBundle = NbBundle
089: .getBundle(PropertyElementCustomizer.class);
090:
091: private PropertyElementTabelModel mDataModel = new PropertyElementTabelModel();
092: // These members are not use anywhere in this class; hence, comment them out
093: //private ArrayList < String > mAddedStereotypes = new ArrayList < String >();
094: //private ArrayList < String > mRemovedStereotypes = new ArrayList < String >();
095: private Vector<IPropertyElement> localElement = new Vector<IPropertyElement>();
096:
097: private IPropertyElement mElement = null;
098: private IPropertyDefinition mDefinition = null;
099: private CustomPropertyEditor mEditor = null;
100:
101: /** Creates new form StereotypeCustomizer */
102: public PropertyElementCustomizer() {
103: this (null);
104: }
105:
106: public PropertyElementCustomizer(IPropertyElement element) {
107: initComponents();
108: mDataTable.getSelectionModel().addListSelectionListener(
109: new SelectionListener());
110: setElement(element, null);
111:
112: }
113:
114: public void setElement(IPropertyElement element,
115: IPropertyDefinition def) {
116: mElement = element;
117: mDefinition = def;
118: if (element != null) {
119: mDataModel = null;
120: initializeModel();
121: }
122: }
123:
124: public void setPropertySupport(CustomPropertyEditor editor) {
125: mEditor = editor;
126: }
127:
128: protected void notifyChanged() {
129: if (mEditor != null) {
130: mEditor.firePropertyChange();
131: }
132: }
133:
134: protected void initializeModel() {
135: if ((mElement != null) && (mDefinition != null)) {
136: mDataModel = new PropertyElementTabelModel();
137:
138: mDataModel
139: .addTableModelListener(new javax.swing.event.TableModelListener() {
140: public void tableChanged(
141: javax.swing.event.TableModelEvent evt) {
142: dataChangedPerformed(evt);
143: }
144: });
145:
146: TableCellRenderer renders[] = mDataModel.initializeModel();
147: mDataTable.setModel(mDataModel);
148: mDataTable.setDoubleBuffered(true);
149:
150: for (int index = 0; index < renders.length; index++) {
151: TableColumn col = mDataTable.getColumnModel()
152: .getColumn(index);
153: if (col != null) {
154: col.setCellRenderer(renders[index]);
155: //col.setCellEditor(new ListCellEditor());
156: if (renders[index] instanceof ListCellRender) {
157: //col.setCellEditor(new javax.swing.DefaultCellEditor((JComboBox)renders[index]));
158: col.setCellEditor(new ListCellEditor());
159: } else if (renders[index] instanceof TextCellRender) {
160: col.setCellEditor(new TextCellEditor());
161: }
162: }
163: }
164:
165: //ETList < String > initialStereotypes = mElement.getAppliedStereotypesAsString();
166: //ETList < Object > initialStereotypes = mElement.getAppliedStereotypes();
167: DefinitionPropertyBuilder builder = DefinitionPropertyBuilder
168: .instance();
169: Vector<IPropertyElement> initialData = mElement
170: .getSubElements();
171: if (initialData != null) {
172: for (IPropertyElement curType : initialData) {
173: IPropertyDefinition def = curType
174: .getPropertyDefinition();
175: if (def.isOnDemand() == true) {
176: builder.loadOnDemandProperties(curType);
177: }
178: // mDataModel.addPropertyElement(curType);
179: }
180: for (IPropertyElement curType : ((localElement.size() > 0) ? localElement
181: : initialData)) {
182: mDataModel.addPropertyElement(curType);
183: }
184: }
185: }
186: }
187:
188: /** This method is called from within the constructor to
189: * initialize the form.
190: * WARNING: Do NOT modify this code. The content of this method is
191: * always regenerated by the Form Editor.
192: */
193: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
194: private void initComponents() {
195: java.awt.GridBagConstraints gridBagConstraints;
196:
197: mPropertyData = new javax.swing.JScrollPane();
198: mDataTable = new javax.swing.JTable();
199: mControlPanel = new javax.swing.JPanel();
200: mAddBtn = new javax.swing.JButton();
201: mRemoveBtn = new javax.swing.JButton();
202: jLabel1 = new javax.swing.JLabel();
203:
204: setLayout(new java.awt.GridBagLayout());
205:
206: mDataTable.setModel(new javax.swing.table.DefaultTableModel(
207: new Object[][] { { null, null, null, null },
208: { null, null, null, null },
209: { null, null, null, null },
210: { null, null, null, null } }, new String[] {
211: "Title 1", "Title 2", "Title 3", "Title 4" }));
212: mPropertyData.setViewportView(mDataTable);
213: mDataTable.getAccessibleContext().setAccessibleName(
214: org.openide.util.NbBundle.getMessage(
215: PropertyElementCustomizer.class,
216: "ACSD_PropertyTable"));
217: mDataTable.getAccessibleContext().setAccessibleDescription(
218: org.openide.util.NbBundle.getMessage(
219: PropertyElementCustomizer.class,
220: "ACSD_PropertyTable"));
221: mDataTable.getAccessibleContext().setAccessibleParent(this );
222:
223: gridBagConstraints = new java.awt.GridBagConstraints();
224: gridBagConstraints.gridy = 1;
225: gridBagConstraints.gridheight = 3;
226: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
227: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
228: gridBagConstraints.weightx = 1.0;
229: gridBagConstraints.weighty = 1.0;
230: gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 0);
231: add(mPropertyData, gridBagConstraints);
232:
233: mControlPanel.setLayout(new java.awt.GridBagLayout());
234:
235: mControlPanel.setBorder(javax.swing.BorderFactory
236: .createEmptyBorder(5, 5, 5, 5));
237: gridBagConstraints = new java.awt.GridBagConstraints();
238: gridBagConstraints.gridx = 1;
239: gridBagConstraints.gridy = 3;
240: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
241: gridBagConstraints.weightx = 0.3;
242: gridBagConstraints.weighty = 1.0;
243: add(mControlPanel, gridBagConstraints);
244:
245: org.openide.awt.Mnemonics.setLocalizedText(mAddBtn,
246: org.openide.util.NbBundle.getMessage(
247: PropertyElementCustomizer.class, "BTN_ADD"));
248: mAddBtn.addActionListener(new java.awt.event.ActionListener() {
249: public void actionPerformed(java.awt.event.ActionEvent evt) {
250: mAddBtnActionPerformed(evt);
251: }
252: });
253:
254: gridBagConstraints = new java.awt.GridBagConstraints();
255: gridBagConstraints.gridx = 1;
256: gridBagConstraints.gridy = 1;
257: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
258: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
259: gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
260: add(mAddBtn, gridBagConstraints);
261: mAddBtn.getAccessibleContext().setAccessibleName(
262: org.openide.util.NbBundle.getMessage(
263: PropertyElementCustomizer.class, "BTN_ADD"));
264: mAddBtn.getAccessibleContext().setAccessibleDescription(
265: org.openide.util.NbBundle.getMessage(
266: PropertyElementCustomizer.class, "ASD_NA"));
267:
268: mControlPanel.add(Box.createVerticalStrut(5));
269: org.openide.awt.Mnemonics.setLocalizedText(mRemoveBtn,
270: org.openide.util.NbBundle.getMessage(
271: PropertyElementCustomizer.class, "BTN_REMOVE"));
272: mRemoveBtn.setEnabled(false);
273: mRemoveBtn
274: .addActionListener(new java.awt.event.ActionListener() {
275: public void actionPerformed(
276: java.awt.event.ActionEvent evt) {
277: mRemoveBtnActionPerformed(evt);
278: }
279: });
280:
281: gridBagConstraints = new java.awt.GridBagConstraints();
282: gridBagConstraints.gridx = 1;
283: gridBagConstraints.gridy = 2;
284: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
285: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
286: gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
287: add(mRemoveBtn, gridBagConstraints);
288: mRemoveBtn.getAccessibleContext().setAccessibleName(
289: org.openide.util.NbBundle.getMessage(
290: PropertyElementCustomizer.class, "BTN_REMOVE"));
291: mRemoveBtn.getAccessibleContext().setAccessibleDescription(
292: org.openide.util.NbBundle.getMessage(
293: PropertyElementCustomizer.class, "ASD_NA"));
294:
295: jLabel1.setLabelFor(mDataTable);
296: org.openide.awt.Mnemonics.setLocalizedText(jLabel1,
297: org.openide.util.NbBundle.getMessage(
298: PropertyElementCustomizer.class,
299: "LBL_PropertyValues"));
300: gridBagConstraints = new java.awt.GridBagConstraints();
301: gridBagConstraints.gridx = 0;
302: gridBagConstraints.gridy = 0;
303: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
304: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
305: gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 0);
306: add(jLabel1, gridBagConstraints);
307:
308: }// </editor-fold>//GEN-END:initComponents
309:
310: private void mRemoveBtnActionPerformed(
311: java.awt.event.ActionEvent evt)//GEN-FIRST:event_mRemoveBtnActionPerformed
312: {//GEN-HEADEREND:event_mRemoveBtnActionPerformed
313: int[] selectedRows = mDataTable.getSelectedRows();
314: if (selectedRows.length == 0) { // no thing is selected
315: return;
316: }
317: //Process last edited cell for subsequence row "add" (if any) works correctly
318: setLastEditedCell(true);
319: mDataModel.removeRows(selectedRows);
320: }//GEN-LAST:event_mRemoveBtnActionPerformed
321:
322: private void mAddBtnActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_mAddBtnActionPerformed
323: {//GEN-HEADEREND:event_mAddBtnActionPerformed
324: mDataModel.createNewProperty();
325: int rowIndex = mDataModel.getRowCount() - 1;
326: // higlight the newly added row
327: // mDataTable.setRowSelectionInterval(rowIndex, rowIndex);
328:
329: }//GEN-LAST:event_mAddBtnActionPerformed
330:
331: public void dataChangedPerformed(TableModelEvent evt) {
332: int type = evt.getType();
333:
334: // This is a hack to fix a problem described in bug ## 6274130
335: // That is... in Stereotypes, Remove tends to delete a wrong line.
336: // This block of code provide a fix for a table with 1 column whose cell editor
337: // is a combobox, like the Stereotypes customizer.
338: if (type == evt.DELETE
339: && mDataTable.getModel().getColumnCount() == 1) {
340: TableColumn column = mDataTable.getColumnModel().getColumn(
341: 0);
342: DefaultCellEditor cellEditor = (DefaultCellEditor) column
343: .getCellEditor();
344: java.awt.Component comp = cellEditor.getComponent();
345: javax.swing.JComboBox box = null;
346: if (comp instanceof javax.swing.JComboBox) {
347: box = (javax.swing.JComboBox) comp;
348: box.getModel().setSelectedItem("");
349: }
350: }
351: }
352:
353: ////////////////////////////////////////////////////////////////////////////
354: // EnhancedCustomPropertyEditor Implementation
355:
356: /**
357: * Get the customized property value. This implementation will
358: * return an array of property elements. Basically when this method
359: * gets called the user has pressed the OK button.
360: *
361: * @return the property value
362: * @exception IllegalStateException when the custom property editor does not contain a valid property value
363: * (and thus it should not be set)
364: */
365: public Object getPropertyValue() throws IllegalStateException {
366: Object retVal = null;
367: // This method is added to fix CR 6291552
368: setLastEditedCell(true);
369: mDataModel.saveModel();
370: mDataModel = null;
371:
372: IPropertyElementManager manager = mElement
373: .getPropertyElementManager();
374:
375: IPropertyElement parent = mElement.getParent();
376: manager.reloadElement(mElement.getElement(), mElement
377: .getPropertyDefinition(), mElement);
378: notifyChanged();
379: return retVal;
380: }
381:
382: public void setVisible(boolean aFlag) {
383: if ((aFlag == true) && (mDataModel == null)) {
384: initializeModel();
385: }
386:
387: super .setVisible(aFlag);
388: }
389:
390: // This method processes the value of the last cell beeing edited.
391: // The value of the cell is either saved to the data model or reset to empty string
392: // based on the "saved" flag.
393: private void setLastEditedCell(Boolean saved) {
394: int editingRow = mDataTable.getEditingRow();
395: int editingCol = mDataTable.getEditingColumn();
396: // if no cell is currently being edited, simply return.
397: if (editingRow < 0 || editingCol < 0)
398: return;
399: DefaultCellEditor cellEditor = (DefaultCellEditor) mDataTable
400: .getCellEditor(editingRow, editingCol);
401: Component editorComponent = cellEditor.getComponent();
402: if (editorComponent instanceof JTextField) {
403: String lastEditedText = ((JTextField) editorComponent)
404: .getText();
405: Debug.out.println("getLastUpdatedCell: lasteditedText="
406: + lastEditedText);
407: if (saved) // update the data model with the value
408: {
409: mDataTable.getModel().setValueAt(lastEditedText,
410: editingRow, editingCol);
411: cellEditor.stopCellEditing();
412: } else // reset the value to empty string.
413: {
414: ((JTextField) editorComponent).setText("");
415: cellEditor.cancelCellEditing();
416: }
417: }
418: editorComponent.transferFocus();
419: return;
420: }
421:
422: ///////////////////////////////////////////////////////////////////////////
423: // Helper Classes
424:
425: /**
426: * Property editor tabel model.
427: */
428: public class PropertyElementTabelModel extends AbstractTableModel {
429: private ArrayList<PropertyData> mData = new ArrayList<PropertyData>();
430: private ArrayList<PropertyData> mRemovedData = new ArrayList<PropertyData>();
431: private ArrayList<IPropertyDefinition> mColumnDefintions = new ArrayList<IPropertyDefinition>();
432:
433: //private IPropertyDefinition mDefinition = null;
434:
435: public PropertyElementTabelModel() {
436:
437: }
438:
439: public PropertyElementTabelModel(IElement element,
440: ArrayList<IStereotype> list) {
441: }
442:
443: public void saveModel() {
444: localElement.removeAllElements();
445: //printData(mData);
446: for (PropertyData data : mData) {
447: data.save();
448: localElement.add(data.getPropertyElement());
449: }
450:
451: for (PropertyData data : mRemovedData) {
452: data.remove();
453: }
454: }
455:
456: public void removeRow(int row) {
457: PropertyData data = mData.get(row);
458: mData.remove(row);
459: //fireTableRowsDeleted(row, row);
460:
461: mRemovedData.add(data);
462: }
463:
464: // This method removes multi selected row intervals.
465: // The indices of the selected rows are specified
466: // in the array rowIndices in an increasing order.
467: public void removeRows(int[] rowIndex) {
468: if (rowIndex == null || rowIndex.length == 0)
469: return;
470:
471: // extract the removed object by theis indices
472: PropertyData[] removedRows = new PropertyData[rowIndex.length];
473: for (int i = 0; i < rowIndex.length; i++) {
474: removedRows[i] = mData.get(rowIndex[i]);
475: }
476: // search for each removedRow in mData;
477: // if found, remove the row from mData
478: PropertyData target = null;
479: PropertyData aRow = null;
480: for (int i = 0; i < removedRows.length; i++) {
481: target = removedRows[i];
482: for (int j = 0; j < getRowCount(); j++) {
483: aRow = (PropertyData) mData.get(j);
484: if (aRow == target) // intentionally compare the objects' addresses
485: {
486: fireTableRowsDeleted(j, j);
487: mData.remove(aRow);
488: mRemovedData.add(aRow);
489: //fireTableRowsDeleted(j, j);
490: break;
491: }
492: }
493: }
494: }
495:
496: public TableCellRenderer[] initializeModel() {
497: ArrayList<TableCellRenderer> renders = new ArrayList<TableCellRenderer>();
498: if (mDefinition != null) {
499: Vector<IPropertyDefinition> columns = mDefinition
500: .getSubDefinitions();
501: for (IPropertyDefinition def : columns) {
502: if (def.getMultiplicity() == 1) {
503: mColumnDefintions.add(def);
504: switch (DefinitionPropertyBuilder.instance()
505: .getControlType(def)) {
506: case DefinitionPropertyBuilder.CONTROL_MULTIEDIT:
507: case DefinitionPropertyBuilder.CONTROL_EDIT:
508: renders.add(new TextCellRender());
509: break;
510: case DefinitionPropertyBuilder.CONTROL_BOOLEAN:
511: //renders.add(new TextCellRender());
512: break;
513: case DefinitionPropertyBuilder.CONTROL_COMBO:
514: renders.add(new ListCellRender());
515: break;
516: case DefinitionPropertyBuilder.CONTROL_LIST:
517: renders.add(new ListCellRender());
518: break;
519: case DefinitionPropertyBuilder.CONTROL_FONT:
520: break;
521: case DefinitionPropertyBuilder.CONTROL_COLOR:
522: break;
523: case DefinitionPropertyBuilder.CONTROL_FONTLIST:
524: break;
525: case DefinitionPropertyBuilder.CONTROL_COLORLIST:
526: break;
527: case DefinitionPropertyBuilder.CONTROL_CUSTOM:
528: break;
529: }
530: }
531: }
532: }
533:
534: TableCellRenderer[] retVal = new TableCellRenderer[renders
535: .size()];
536: renders.toArray(retVal);
537: return retVal;
538: }
539:
540: protected IPropertyDefinition getColumnDefinition(int column) {
541: IPropertyDefinition retVal = null;
542:
543: if (column < getColumnCount()) {
544: retVal = mColumnDefintions.get(column);
545: }
546:
547: return retVal;
548: }
549:
550: protected void printData(ArrayList<PropertyData> data) {
551: if (data == null)
552: Debug.out.println("Data is null");
553: else if (data.size() == 0)
554: Debug.out.println("Data is emty");
555: else {
556: IPropertyElement cellVal = null;
557: Debug.out.println("*****");
558: for (int i = 0; i < data.size(); i++) {
559: for (int j = 0; j < getColumnCount(); j++) {
560: cellVal = getPropertyElement(i, j);
561: Debug.out.println("[" + i + "," + j + "]: "
562: + cellVal.getValue());
563: }
564: }
565: Debug.out.println("*****");
566: }
567: }
568:
569: /////////////////////////////////////////////////////////////////////////
570: // TableModel implementation
571:
572: public boolean isCellEditable(int rowIndex, int columnIndex) {
573: return true;
574: }
575:
576: public int getColumnCount() {
577: return mColumnDefintions.size();
578: }
579:
580: public String getColumnName(int column) {
581: String retVal = "< UNKNOWN >";
582:
583: IPropertyDefinition def = getColumnDefinition(column);
584: if (def != null) {
585: //Fix CR 6274580: use the displayed name instead of
586: // the programmed name as the column header
587: //retVal = def.getName();
588: retVal = def.getDisplayName();
589: }
590:
591: return retVal;
592: }
593:
594: public int getRowCount() {
595: return mData.size();
596: }
597:
598: public void setValueAt(Object aValue, int row, int col) {
599: //System.out.println("setValueAt(" + row + ", " + col + ") : " + aValue);
600:
601: IPropertyElement element = getPropertyElement(row, col);
602: if ((element != null) && (aValue instanceof String)) {
603: if (col == 2) {
604: String value = PropertyDataFormatter
605: .translateToFullyQualifiedName((String) aValue);
606: element.setValue(value);
607: } else {
608: // Fixed IZ=102600
609: element.setValue((String) aValue);
610: }
611: }
612: }
613:
614: public Object getValueAt(int row, int col) {
615: IPropertyElement retVal = getPropertyElement(row, col);
616:
617: //System.out.println("getValueAt(" + row + ", " + col + ") : " + retVal.getValue());
618: //return retVal.getValue();
619: return retVal;
620: }
621:
622: protected IPropertyElement getPropertyElement(int row, int col) {
623: IPropertyElement retVal = null;
624:
625: if (row < getRowCount()) {
626: PropertyData data = mData.get(row);
627: if (col < getColumnCount()) {
628: IPropertyElement element = data
629: .getPropertyElement();
630: if (element != null) {
631: Vector<IPropertyElement> sub = element
632: .getSubElements();
633: if (sub != null) {
634: retVal = sub.get(col);
635: }
636: }
637: }
638: }
639:
640: return retVal;
641: }
642:
643: public void addPropertyElement(IPropertyElement value) {
644: mData.add(new PropertyData(value, null));
645: fireTableRowsInserted(getRowCount(), getRowCount());
646: }
647:
648: public void createNewProperty() {
649: DefinitionPropertyBuilder builder = DefinitionPropertyBuilder
650: .instance();
651:
652: // Steps
653: // 1) Given the property definitions build property elements - Done
654: // 2) Given property elements build Node.Property objects - Done
655: // 3) Make the property objects so they do not automatically persist - Done
656: // 4) Add properties to our data structure
657: IPropertyElement data = builder.retrievePropertyElement(
658: mDefinition, mElement);
659: //Node.Property[] properties = builder.buildProperties(data, false);
660: addPropertyElement(data);
661: }
662:
663: class PropertyData {
664: private IPropertyElement mPropertyElement = null;
665: private Node.Property[] mData = null;
666:
667: public PropertyData(IPropertyElement umlData,
668: Node.Property[] nbData) {
669: mPropertyElement = umlData;
670: mData = nbData;
671: }
672:
673: public IPropertyElement getPropertyElement() {
674: return mPropertyElement;
675: }
676:
677: public void save() {
678: IPropertyElement element = getPropertyElement();
679: if (element != null) {
680: save(element);
681: }
682: }
683:
684: protected void save(IPropertyElement element) {
685: if (element != null) {
686: String value = element.getValue();
687: String transValue = PropertyDataFormatter
688: .translateToFullyQualifiedName(value);
689: element.setValue(transValue);
690:
691: element.save();
692: Vector<IPropertyElement> children = element
693: .getSubElements();
694: for (IPropertyElement child : children) {
695: save(child);
696: }
697: }
698: }
699:
700: public void remove() {
701: IPropertyElement element = getPropertyElement();
702: if (element != null) {
703: element.remove();
704: }
705: }
706:
707: public Node.Property getData(int column) {
708: Node.Property retVal = null;
709:
710: if (column < mData.length) {
711: retVal = mData[column];
712: }
713:
714: return retVal;
715: }
716:
717: public int getNumberOfColumns() {
718: return mData.length;
719: }
720: }
721: }
722:
723: // Change the ListCellRender to the DefaultTableCellRenderer to
724: // inherit all the default behavors, like selection hilighting...
725: // This is to fix bug # 6274130.
726: // This method display the value as a string on text field.
727: public class ListCellRender extends DefaultTableCellRenderer {
728: protected void setValue(Object value) {
729: if (value instanceof IPropertyElement) {
730: IPropertyElement element = (IPropertyElement) value;
731: String cellValue = element.getValue();
732:
733: String transValue = cellValue == null ? "" : cellValue;
734: setText(PropertyDataFormatter
735: .translateFullyQualifiedName(transValue));
736: }
737: }
738:
739: }
740:
741: // Change the TextCellRender to the DefaultTableCellRenderer to
742: // inherit all the default behavors, like selection hilighting...
743: // This is to fix bug # 6274130.
744: // This method display the value as a string on text field.
745: public class TextCellRender extends DefaultTableCellRenderer {
746: protected void setValue(Object value) {
747: if (value instanceof IPropertyElement) {
748: IPropertyElement element = (IPropertyElement) value;
749: String cellValue = element.getValue();
750: setText(cellValue == null ? "" : cellValue);
751: }
752: }
753: }
754:
755: private class SelectionListener implements ListSelectionListener {
756: public void valueChanged(ListSelectionEvent e) {
757: mRemoveBtn
758: .setEnabled(mDataTable.getSelectedRows().length > 0);
759: }
760: }
761:
762: // Variables declaration - do not modify//GEN-BEGIN:variables
763: private javax.swing.JLabel jLabel1;
764: private javax.swing.JButton mAddBtn;
765: private javax.swing.JPanel mControlPanel;
766: private javax.swing.JTable mDataTable;
767: private javax.swing.JScrollPane mPropertyData;
768: private javax.swing.JButton mRemoveBtn;
769: // End of variables declaration//GEN-END:variables
770:
771: }
|