KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mc4j > console > swing > editor > jmx > CompositeDataEditor


1 /*
2  * Copyright 2002-2004 Greg Hinkle
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.mc4j.console.swing.editor.jmx;
18
19 import org.openide.ErrorManager;
20 import org.openide.explorer.propertysheet.PropertySheet;
21 import org.openide.nodes.AbstractNode;
22 import org.openide.nodes.Children;
23 import org.openide.nodes.Node;
24 import org.openide.nodes.PropertySupport;
25 import org.openide.nodes.Sheet;
26 import org.openide.windows.IOProvider;
27
28 import javax.management.openmbean.CompositeData JavaDoc;
29 import javax.management.openmbean.CompositeDataSupport JavaDoc;
30 import javax.management.openmbean.OpenDataException JavaDoc;
31 import javax.swing.*;
32 import java.awt.*;
33 import java.beans.PropertyEditorSupport JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.Set JavaDoc;
36
37
38 /**
39  * @author Greg Hinkle (ghinkle@users.sourceforge.net), January 2002
40  * @version $Revision: 570 $($Author: ghinkl $ / $Date: 2006-04-12 15:14:16 -0400 (Wed, 12 Apr 2006) $)
41  */

42 public class CompositeDataEditor extends PropertyEditorSupport JavaDoc {
43
44     public boolean supportsCustomEditor() {
45         return true;
46     }
47
48     public Component getCustomEditor() {
49         Object JavaDoc value = getValue();
50         if (value instanceof CompositeData JavaDoc) {
51             //return new CompositeDataPanel((CompositeData) value);
52
return new CompositeDataNodePanel((CompositeData JavaDoc) value, this);
53         }
54         return null;
55     }
56
57
58     public static class CompositeDataNodePanel extends JPanel {
59
60         public CompositeDataNodePanel(CompositeData JavaDoc data, CompositeDataEditor cde) {
61
62             setLayout(new BorderLayout());
63             PropertySheet sheet = new PropertySheet();
64
65             CompositeDataNode node = new CompositeDataNode(data, cde);
66
67             try {
68                 sheet.setNodes(new Node[]{node});
69             } catch (Exception JavaDoc e) {
70                 ErrorManager.getDefault().notify(e);
71             }
72             add(sheet, BorderLayout.CENTER);
73         }
74     }
75
76
77     public static class CompositeDataNode extends AbstractNode {
78         CompositeData JavaDoc data;
79         CompositeDataEditor cde;
80
81         public CompositeDataNode(CompositeData JavaDoc data, CompositeDataEditor cde) {
82             super(Children.LEAF);
83             this.data = data;
84             this.cde = cde;
85
86             setDisplayName(this.data.getCompositeType().getDescription());
87         }
88
89         public Node.PropertySet[] getPropertySets() {
90             Sheet.Set props = Sheet.createPropertiesSet();
91             props.setName("compositeItems");
92             props.setDisplayName("Composite Items");
93             props.setShortDescription("Item info that make up this composite data");
94
95             Set JavaDoc keySet = this.data.getCompositeType().keySet();
96             for (Iterator JavaDoc iterator = keySet.iterator(); iterator.hasNext();) {
97                 final String JavaDoc keyName = (String JavaDoc) iterator.next();
98                 Class JavaDoc dataClass = null;
99                 try {
100                     dataClass = Class.forName(this.data.getCompositeType().getType(keyName).getClassName());
101                 } catch (ClassNotFoundException JavaDoc cnfe) {
102                 }
103                 String JavaDoc description = this.data.getCompositeType().getDescription(keyName);
104                 props.put(new PropertySupport.ReadWrite(keyName,
105                     dataClass,
106                     keyName,
107                     description) {
108
109                     public Object JavaDoc getValue() {
110                         Object JavaDoc value = data.get(keyName);
111                         return value;
112                     }
113
114                     public void setValue(Object JavaDoc value) {
115                         CompositeDataSupport JavaDoc cds = null;
116                         try {
117                             cds = createCompositeData(value);
118                             data = cds;
119                             cde.setValue(data);
120                         } catch (OpenDataException JavaDoc e) {
121                             //ignore - we will just stay with the original data
122
IOProvider.getDefault().getIO("MC4J Errors", false).getOut().
123                                 println("CompositeDataSupport exception: " + e.getMessage());
124                         }
125                     }
126
127                     /**
128                      * @param value
129                      * @return
130                      * @throws OpenDataException
131                      */

132                     private CompositeDataSupport JavaDoc createCompositeData(Object JavaDoc value) throws OpenDataException JavaDoc {
133                         Set JavaDoc set = data.getCompositeType().keySet();
134                         int i = 0;
135                         Object JavaDoc[] newData = new Object JavaDoc[set.size()];
136                         String JavaDoc[] newDescriptions = new String JavaDoc[set.size()];
137                         for (Iterator JavaDoc iter = set.iterator(); iter.hasNext();) {
138                             String JavaDoc key = (String JavaDoc) iter.next();
139                             if (keyName.equals(key)) {
140                                 newData[i] = value;
141                             } else {
142                                 newData[i] = data.get(key);
143                                 ;
144                             }
145                             newDescriptions[i] = key;
146                             i++;
147                         }
148                         CompositeDataSupport JavaDoc cds = new CompositeDataSupport JavaDoc(data.getCompositeType(), newDescriptions, newData);
149                         return cds;
150                     }
151                 });
152
153             }
154             return new Node.PropertySet[]{props};
155         }
156
157     }
158
159 /*
160     // TODO GH: This is an alternate implementation using our own TreeTable instead
161     // of the NetBeans platform property explorer. It could be useful for
162     // nested composite datas as it supports a tree view of unlimited nesting
163     // whereas the above approach requires a new property explorer for each
164     // additional level. This stuff needs work though so I'm leaving it
165     // commented out for now.
166
167
168     public static class CompositeDataPanel extends JPanel {
169
170         private CompositeData data;
171         private JScrollPane scrollPane;
172         private JTreeTable treeTable;
173
174         public CompositeDataPanel(CompositeData data) {
175             this.data = data;
176
177             init();
178         }
179
180         private void init() {
181             treeTable = new JTreeTable(new CompositeDataTreeTableModel(data));
182
183             DefaultTableColumnModel cm = new DefaultTableColumnModel();
184             TableColumn cData, cType, cDesc;
185             cData = new TableColumn(0, 150);
186             cData.setHeaderValue("Data");
187             cType = new TableColumn(1,200);
188             cType.setHeaderValue("Type");
189             cDesc = new TableColumn(1,80);
190             cDesc.setHeaderValue("Description");
191
192             cm.addColumn(cData);
193             cm.addColumn(cType);
194             cm.addColumn(cDesc);
195
196             this.treeTable.setColumnModel(cm);
197
198             this.treeTable.getTree().setCellRenderer(new CustomTreeTableCellRenderer());
199
200
201             scrollPane = new JScrollPane(treeTable);
202
203             setLayout(new BorderLayout());
204             add(scrollPane, BorderLayout.CENTER);
205         }
206
207     }
208
209
210     public static class CompositeDataTreeTableModel extends AbstractTreeTableModel {
211
212         private CompositeData data;
213
214         public CompositeDataTreeTableModel(CompositeData data) {
215             super(new DataNode(null,data,null));
216             this.data = data;
217         }
218
219
220         public int getColumnCount() {
221             return COLUMN_NAMES.length;
222         }
223
224         private static final String[] COLUMN_NAMES =
225             { "Data", "Type", "Description" };
226
227         static protected Class[] cTypes = { TreeTableModel.class, String.class, String.class};
228
229         public String getColumnName(int column) {
230             return COLUMN_NAMES[column];
231         }
232
233         public Class getColumnClass(int column) {
234             return cTypes[column];
235         }
236
237         public Object getValueAt(Object node, int column) {
238             DataNode dataNode = (DataNode) node;
239             Object info = dataNode.getNode();
240             if (info instanceof CompositeData) {
241                 CompositeData d = (CompositeData) info;
242                 switch(column) {
243                     case 0:
244                         return "<composite>";
245                     case 1:
246                         return d.getCompositeType().getTypeName();
247                     case 2:
248                         return d.getCompositeType().getDescription();
249                 }
250             } else {
251                 switch(column) {
252                     case 0:
253                         return info;
254                     case 1:
255                         return dataNode.getNodeKey(); //info.getClass().getName();
256                     case 2:
257                         return dataNode.getNodeKey();
258                 }
259             }
260             return "";
261         }
262
263         public Object getChild(Object parent, int index) {
264             DataNode dataNode = (DataNode) parent;
265             if (dataNode.getNode() instanceof CompositeData) {
266                 CompositeData d = (CompositeData) dataNode.getNode();
267
268                 Object child = d.values().toArray()[index];
269                 String keyNode = (String) d.getCompositeType().keySet().toArray()[index];
270                 return new DataNode(dataNode.getNode(),child,keyNode);
271             }
272             return null;
273         }
274
275         public int getChildCount(Object parent) {
276             if (((DataNode)parent).getNode() instanceof CompositeData) {
277                 CompositeData d = (CompositeData) ((DataNode)parent).getNode();
278                 return d.values().size();
279             }
280
281             return 0;
282         }
283     }
284
285     public static class DataNode {
286         private Object parent;
287         private Object node;
288         private String nodeKey;
289         public DataNode(Object parent, Object node, String nodeKey) {
290             this.parent = parent;
291             this.node = node;
292             this.nodeKey = nodeKey;
293         }
294
295         public Object getNode() {
296             return node;
297         }
298
299         public Object getParent() {
300             return parent;
301         }
302
303         public String getNodeKey() {
304             return nodeKey;
305         }
306     }
307
308
309     public static class CustomTreeTableCellRenderer extends DefaultTreeCellRenderer {
310
311         public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
312
313             super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
314
315             if (value instanceof DataNode) {
316                 Object realValue = ((DataNode)value).getNode();
317                 setText((realValue==null)?"null":realValue.toString());
318
319 // if (node.isFolder()) {
320 // setIcon(folderIcon);
321 // } else {
322 // setIcon(documentIcon);
323 // }
324             }
325             return this;
326         }
327     }
328 */

329
330 }
331
Popular Tags