KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > designer > view > MultiTypeTableView


1 /*
2  * (c) Rob Gordon 2005.
3  */

4 package org.oddjob.designer.view;
5
6 import java.awt.BorderLayout JavaDoc;
7 import java.awt.Component JavaDoc;
8 import java.awt.Container JavaDoc;
9 import java.awt.Dimension JavaDoc;
10 import java.awt.GridBagConstraints JavaDoc;
11 import java.awt.Insets JavaDoc;
12 import java.util.Observable JavaDoc;
13 import java.util.Observer JavaDoc;
14
15 import javax.swing.AbstractCellEditor JavaDoc;
16 import javax.swing.DefaultCellEditor JavaDoc;
17 import javax.swing.JComboBox JavaDoc;
18 import javax.swing.JLabel JavaDoc;
19 import javax.swing.JPanel JavaDoc;
20 import javax.swing.JScrollPane JavaDoc;
21 import javax.swing.JTable JavaDoc;
22 import javax.swing.JTextField JavaDoc;
23 import javax.swing.event.TableModelEvent JavaDoc;
24 import javax.swing.table.AbstractTableModel JavaDoc;
25 import javax.swing.table.TableCellEditor JavaDoc;
26 import javax.swing.table.TableCellRenderer JavaDoc;
27 import javax.swing.table.TableColumn JavaDoc;
28
29 import org.oddjob.designer.Looks;
30 import org.oddjob.designer.model.DesignDefinition;
31 import org.oddjob.designer.model.DesignElement;
32 import org.oddjob.designer.model.MultiTypeTable;
33
34 /**
35  * This class is capable of representing a DesignElement which consists of
36  * multiple child DesignElements of different types.
37  */

38 public class MultiTypeTableView implements ViewProducer {
39
40     private final ValuesTableModel tm = new ValuesTableModel();
41     private final MultiTypeTable designDefinition;
42     
43     private final boolean namedValues;
44     
45     private Component JavaDoc component;
46     
47     public MultiTypeTableView(MultiTypeTable designDefinition) {
48         this.designDefinition = designDefinition;
49         this.namedValues = designDefinition.isNamed();
50         this.component = component();
51         designDefinition.addObserver(new Observer JavaDoc() {
52             public void update(Observable JavaDoc o, Object JavaDoc arg) {
53                 tm.fireTableChanged(new TableModelEvent JavaDoc(tm));
54             }
55         });
56     }
57
58     public boolean isNamedValues() {
59         return namedValues;
60     }
61     
62     public Component JavaDoc dialog() {
63         return group();
64     }
65     
66     public Component JavaDoc group() {
67         JPanel JavaDoc form = new JPanel JavaDoc();
68         
69         form.setLayout(new BorderLayout JavaDoc());
70         form.setBorder(Looks.groupBorder(designDefinition.getTitle()));
71         form.add(component, BorderLayout.CENTER);
72         return form;
73     }
74
75     /* (non-Javadoc)
76      * @see org.oddjob.designer.view.ViewProducer#detailEdit(org.oddjob.designer.view.ActionWrapper)
77      */

78     public Component JavaDoc detailEdit() {
79         return ViewHelper.createDetailButton(designDefinition);
80     }
81     
82     /* (non-Javadoc)
83      * @see org.oddjob.designer.view.ViewProducer#cell()
84      */

85     public Component JavaDoc cell() {
86         return ViewHelper.createDetailButton(designDefinition);
87     }
88
89     /* (non-Javadoc)
90      * @see org.oddjob.designer.view.ViewProducer#inline(java.awt.Container, int, int, boolean)
91      */

92     public int inline(Container JavaDoc container, int row, int column,
93             boolean selectionInGroup) {
94         
95         GridBagConstraints JavaDoc c = new GridBagConstraints JavaDoc();
96
97         c.weightx = 1.0;
98         c.weighty = 0.0;
99         
100         c.fill = GridBagConstraints.HORIZONTAL;
101         c.anchor = GridBagConstraints.NORTHWEST;
102         
103         c.gridx = column;
104         c.gridy = row;
105         
106         c.gridwidth = GridBagConstraints.REMAINDER;
107                 
108         c.insets = new Insets JavaDoc(3, 3, 3, 3);
109
110         container.add(group(), c);
111         
112         return row + 1;
113     }
114     
115     /* (non-Javadoc)
116      * @see org.oddjob.designer.view.ViewProducer#setEnabled(boolean)
117      */

118     public void setEnabled(boolean enabled) {
119         component.setEnabled(enabled);
120         if (!enabled) {
121             while (designDefinition.childCount() > 0) {
122                 designDefinition.removeChild(0);
123             }
124         }
125     }
126
127     /**
128      * Creates the table component.
129      *
130      * @return The table.
131      */

132     private Component JavaDoc component() {
133         JTable JavaDoc table = new JTable JavaDoc(tm);
134         table.setRowMargin(2);
135         setPreferredTableSize(table);
136         
137         TableColumn JavaDoc typeCol = table.getColumnModel().getColumn(0);
138         
139         JComboBox JavaDoc comboBox = new JComboBox JavaDoc();
140         comboBox.addItem("");
141         String JavaDoc[] types = designDefinition.getSupportedTypes();
142         for (int i = 0; i < types.length; ++i) {
143             comboBox.addItem(types[i]);
144         }
145         typeCol.setCellEditor(new DefaultCellEditor JavaDoc(comboBox));
146
147         TableColumn JavaDoc valueCol = table.getColumnModel().getColumn(
148                 tm.getColumnCount() - 1);
149         valueCol.setCellEditor(new DialogEditor());
150         valueCol.setCellRenderer(new DialogRenderer());
151
152         JScrollPane JavaDoc jsp = new JScrollPane JavaDoc(table);
153         return jsp;
154     }
155         
156     private void setPreferredTableSize(JTable JavaDoc table){
157         int height = 0;
158         for(int row=0; row < designDefinition.getVisibleRows(); row++)
159             height += table.getRowHeight(row);
160      
161         table.setPreferredScrollableViewportSize(new Dimension JavaDoc(
162                 Looks.DETAIL_USABLE_WIDTH - 100,
163                 height));
164     }
165             
166     class ValuesTableModel extends AbstractTableModel JavaDoc {
167
168         String JavaDoc[] unnamedHeaders = { "Type", "Value" };
169         String JavaDoc[] namedHeaders = { "Type", "Name", "Value" };
170
171         public String JavaDoc getColumnName(int c) {
172             if (isNamedValues()) {
173                 return namedHeaders[c];
174             } else {
175                 return unnamedHeaders[c];
176             }
177         }
178
179         public int getColumnCount() {
180             return isNamedValues() ? 3 : 2;
181         }
182
183         public int getRowCount() {
184             return designDefinition.childCount() + 1;
185         }
186
187         public boolean isCellEditable(int rowIndex, int columnIndex) {
188             if (columnIndex == 0) {
189                 return true;
190             }
191             if (!"".equals(getValueAt(rowIndex, 0))) {
192                 return true;
193             }
194             return false;
195         }
196
197         public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
198             if (rowIndex == getRowCount() - 1) {
199                 return "";
200             }
201             
202             if (columnIndex == 0) {
203                 return designDefinition.getChildType(rowIndex);
204             } else if (columnIndex == 1) {
205                 if (isNamedValues()) {
206                     return designDefinition.getChildName(rowIndex);
207                 } else {
208                     return designDefinition.getChildValue(rowIndex);
209                 }
210             } else if (columnIndex == 2) {
211                 return designDefinition.getChildValue(rowIndex);
212             } else {
213                 throw new RuntimeException JavaDoc("This should be impossible!");
214             }
215         }
216
217         public void setValueAt(Object JavaDoc value, int rowIndex, int columnIndex) {
218             if (columnIndex == 0) {
219                 String JavaDoc type = (String JavaDoc) value;
220                 if (rowIndex < getRowCount() - 1) {
221                     String JavaDoc oldType = designDefinition.getChildType(rowIndex);
222                     if (type.equals(oldType)) {
223                         // if the type hasn't changed then nothing to do.
224
return;
225                     }
226                     else {
227                         designDefinition.removeChild(rowIndex);
228                     }
229                 }
230                 if ("".equals(type)) {
231                     return;
232                 }
233                 designDefinition.insertChild(rowIndex, type);
234             } else if (columnIndex == 1) {
235                 if (isNamedValues()) {
236                     designDefinition.setChildName(rowIndex, (String JavaDoc)value);
237                 }
238             } else if (columnIndex == 2) {
239                 // don't do anything - the render takes care of the display.
240
} else {
241                 throw new RuntimeException JavaDoc("This should be impossible!");
242             }
243         }
244
245     }
246     
247     public class DialogEditor extends AbstractCellEditor JavaDoc
248     implements TableCellEditor JavaDoc {
249         DesignElement value;
250
251         //Implement the one CellEditor method that AbstractCellEditor doesn't.
252
public Object JavaDoc getCellEditorValue() {
253             return value;
254         }
255
256         //Implement the one method defined by TableCellEditor.
257
public Component JavaDoc getTableCellEditorComponent(JTable JavaDoc table,
258                 Object JavaDoc value, boolean isSelected, int row, int column) {
259             this.value = (DesignElement) value;
260             return generateRenderer((DesignElement) value);
261         }
262     }
263
264     class DialogRenderer implements TableCellRenderer JavaDoc {
265         public Component JavaDoc getTableCellRendererComponent(JTable JavaDoc table,
266                 Object JavaDoc value, boolean isSelected, boolean hasFocus, int row,
267                 int column) {
268             if (value instanceof String JavaDoc) {
269                 return new JLabel JavaDoc(value.toString());
270             } else {
271                 return generateRenderer((DesignElement) value);
272             }
273         }
274     }
275     
276         
277     static Component JavaDoc generateRenderer(DesignElement de) {
278         if (de.hasDetail()) {
279             return ViewFactory.create(de.detail()).cell();
280         }
281         JTextField JavaDoc nothing = new JTextField JavaDoc();
282         nothing.setEnabled(false);
283         return nothing;
284     }
285     
286 }
287
Popular Tags