001: package com.xoetrope.carousel.visualizer;
002:
003: import com.xoetrope.data.pojo.XPojoModelVis;
004: import javax.swing.DefaultCellEditor;
005: import javax.swing.DefaultComboBoxModel;
006: import javax.swing.JComboBox;
007: import javax.swing.JTable;
008: import javax.swing.JTextField;
009: import javax.swing.table.AbstractTableModel;
010: import javax.swing.table.TableCellEditor;
011: import javax.swing.table.TableColumn;
012: import net.xoetrope.xui.data.XModel;
013:
014: /**
015: * A table for displaying model node attributes
016: */
017: public class XStructureTable extends JTable {
018: private static final int COL_ATTRIBUTE_WIDTH = 100;
019: private static final int COL_VALUE_WIDTH = 100;
020: private static final int COL_RUNTIME_WIDTH = 100;
021: private static String[] BOOLEAN_VALUES = { "true", "false" };
022:
023: private XStructureTableModel tableModel;
024: private DefaultCellEditor comboCellEditor;
025: private DefaultCellEditor textCellEditor;
026: private JComboBox comboBox;
027: private JTextField textField;
028:
029: /**
030: * Creates a new instance of XStructureTable
031: */
032: public XStructureTable() {
033: super ();
034: setAutoCreateColumnsFromModel(false);
035: setModel(tableModel = new XStructureTableModel());
036:
037: addColumn(new TableColumn(0, COL_ATTRIBUTE_WIDTH));
038: addColumn(new TableColumn(1, COL_VALUE_WIDTH));
039: addColumn(new TableColumn(2, COL_RUNTIME_WIDTH));
040:
041: comboCellEditor = new DefaultCellEditor(
042: comboBox = new JComboBox());
043: textCellEditor = new DefaultCellEditor(
044: textField = new JTextField());
045: }
046:
047: /**
048: * Sets the new model node
049: */
050: public void setModelNode(XModel model) {
051: tableModel.setModelNode(model);
052: revalidate();
053: repaint();
054: }
055:
056: public TableCellEditor getCellEditor(int row, int column) {
057: XModel modelNode = tableModel.getModelNode();
058: TableCellEditor editor = super .getCellEditor(row, column);
059: if ((column == XStructureTableModel.COL_VALUE)
060: && (modelNode instanceof XPojoModelVis)
061: && ((XPojoModelVis) modelNode).isAttribEditable(row)) {
062: XPojoModelVis modelVis = (XPojoModelVis) modelNode;
063:
064: switch (modelVis.getAttribType(row)) {
065:
066: case XPojoModelVis.ATTRIB_COMBO:
067: editor = comboCellEditor;
068: String[] vals = modelVis.getAttribAvailableValues(row);
069: if (vals == null)
070: vals = new String[0];
071: comboBox.setModel(new DefaultComboBoxModel(vals));
072: break;
073:
074: case XPojoModelVis.ATTRIB_FREETEXT:
075: editor = textCellEditor;
076: vals = modelVis.getAttribAvailableValues(row);
077: if (vals.length > 0)
078: textField.setText(vals[0]);
079: break;
080: }
081:
082: }
083: return editor;
084: }
085: }
086:
087: /**
088: * table model
089: */
090: class XStructureTableModel extends AbstractTableModel {
091: public static final int COL_ATTRIBUTE = 0;
092: public static final int COL_VALUE = 1;
093: public static final int COL_RUNTIME = 2;
094:
095: private XModel modelNode;
096:
097: public XStructureTableModel() {
098: super ();
099: modelNode = null;
100: }
101:
102: public XStructureTableModel(XModel aModel) {
103: super ();
104: modelNode = aModel;
105: }
106:
107: public void setModelNode(XModel aModel) {
108: modelNode = aModel;
109: }
110:
111: public XModel getModelNode() {
112: return modelNode;
113: }
114:
115: public int getRowCount() {
116: return (modelNode != null ? modelNode.getNumAttributes() : 0);
117: }
118:
119: public int getColumnCount() {
120: return 3;
121: }
122:
123: public Object getValueAt(int row, int column) {
124: if ((column < 0) || (column > 2))
125: return null;
126: if ((row < 0) || (row >= getRowCount()))
127: return null;
128:
129: switch (column) {
130: case COL_ATTRIBUTE:
131: try {
132: return modelNode.getAttribName(row);
133: } catch (Exception ex) {
134: return "";
135: }
136:
137: case COL_VALUE:
138: try {
139: return modelNode.getAttribValueAsString(row);
140: } catch (Exception ex) {
141: return "";
142: }
143:
144: case COL_RUNTIME:
145: try {
146: if ((modelNode instanceof XPojoModelVis)
147: && ((XPojoModelVis) modelNode)
148: .getAttribRuntime(row))
149: return "true";
150: else
151: return "false";
152: } catch (Exception ex) {
153: return "false";
154: }
155: }
156: return null;
157: }
158:
159: public void setValueAt(Object value, int row, int column) {
160: if ((column < 0) || (column > 2))
161: return;
162: if ((row < 0) || (row >= getRowCount()))
163: return;
164: if (column != COL_VALUE)
165: return;
166: if (modelNode instanceof XPojoModelVis)
167: ((XPojoModelVis) modelNode).setAttribValue(row, value);
168: }
169:
170: public String getColumnName(int column) {
171: switch (column) {
172: case COL_ATTRIBUTE:
173: return "attribute";
174: case COL_VALUE:
175: return "value";
176: case COL_RUNTIME:
177: return "runtime";
178: }
179: return null;
180: }
181:
182: public boolean isCellEditable(int row, int column) {
183: boolean editable = (column == COL_VALUE);
184: editable = (editable && (modelNode instanceof XPojoModelVis));
185: editable = (editable && ((XPojoModelVis) modelNode)
186: .isAttribEditable(row));
187: return editable;
188: }
189:
190: }
|