KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > core > management > data > TabularDataFormat


1 /**
2  * Copyright (c) 2004-2005 jManage.org
3  *
4  * This is a free software; you can redistribute it and/or
5  * modify it under the terms of the license at
6  * http://www.jmanage.org.
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  * See the License for the specific language governing permissions and
12  * limitations under the License.
13  */

14 package org.jmanage.core.management.data;
15
16 import org.jmanage.util.display.Table;
17
18 import javax.management.openmbean.TabularData JavaDoc;
19 import javax.management.openmbean.TabularType JavaDoc;
20 import javax.management.openmbean.CompositeType JavaDoc;
21 import javax.management.openmbean.CompositeData JavaDoc;
22 import java.util.*;
23
24 /**
25  *
26  * <p>
27  * Date: Oct 1, 2005
28  * @author Rakesh Kalra
29  */

30 public abstract class TabularDataFormat implements DataFormat {
31
32     public String JavaDoc format(Object JavaDoc data) {
33         TabularData JavaDoc tabularData = (TabularData JavaDoc) data;
34
35         TabularType JavaDoc type = tabularData.getTabularType();
36         CompositeType JavaDoc rowType = type.getRowType();
37
38         /* get the header and index names */
39         Set itemNamesSet = rowType.keySet();
40         String JavaDoc[] itemNames =
41                 (String JavaDoc[])itemNamesSet.toArray(new String JavaDoc[itemNamesSet.size()]);
42
43         final List indexNamesList= tabularData.getTabularType().getIndexNames();
44         final String JavaDoc[] indexNames =
45               (String JavaDoc[])indexNamesList.toArray(new String JavaDoc[indexNamesList.size()]);
46
47         /* move index names at the begining of the item names (if not the case)*/
48         for(int i=0; i<indexNames.length; i++){
49             if(!itemNames[i].equals(indexNames[i])){
50                 // find where is the indexNames[i]
51
int index = find(itemNames, indexNames[i]);
52                 if(index != -1){
53                     // swap
54
itemNames[index] = itemNames[i];
55                     itemNames[i] = indexNames[i];
56                 }
57             }
58         }
59
60         /* sort the CompositeData objects by the index values */
61         /* Sorting logic contributed by Jess Holle (SF id: jess_hole)*/
62         final int dataRowsCount = tabularData.size();
63         final TabularDataData tabularDataData[] = new TabularDataData[dataRowsCount];
64         int ii = 0;
65         for (Iterator it = tabularData.values().iterator(); it.hasNext();) {
66             CompositeData JavaDoc compositeData = (CompositeData JavaDoc) it.next();
67             final Object JavaDoc[] indices = compositeData.getAll(indexNames);
68             tabularDataData[ii++] = new TabularDataData(indices, compositeData);
69         }
70         Arrays.sort(tabularDataData);
71
72         /* get the rows */
73         List rows = new LinkedList();
74         for (int i=0; i < dataRowsCount; i++) {
75             CompositeData JavaDoc compositeData = tabularDataData[i].compositeData;
76             String JavaDoc[] itemValues = new String JavaDoc[itemNames.length];
77             for (int j = 0; j < itemNames.length; j++) {
78                 Object JavaDoc value = compositeData.get(itemNames[j]);
79                 itemValues[j] = DataFormatUtil.format(value);
80             }
81             rows.add(itemValues);
82         }
83
84         /* draw the table */
85         Table table = getTable();
86         table.setHeader(itemNames);
87         table.addRows(rows);
88         return table.draw();
89     }
90
91     private int find(String JavaDoc[] array, String JavaDoc item){
92         for(int i=0; i<array.length; i++){
93             if(array[i].equals(item)){
94                 return i;
95             }
96         }
97         return -1;
98     }
99
100
101     // provided by Jess Hole
102
private static class TabularDataData implements Comparable JavaDoc {
103
104         TabularDataData(final Object JavaDoc indices[], final CompositeData JavaDoc compositeData) {
105             this.indices = indices;
106             this.compositeData = compositeData;
107         }
108
109         public int compareTo(Object JavaDoc data2) {
110             for (int ii = 0; ii < indices.length; ++ii) {
111                 final Object JavaDoc indexData = indices[ii];
112                 final Object JavaDoc indexData2 = ((TabularDataData)data2).indices[ii];
113                 if (indexData == null)
114                     if (indexData2 == null)
115                         continue; // two nulls are always considered equal
116
else
117                         return (-1); // we'll always consider a null less than a non-null
118
final int comparison = ((indexData instanceof Comparable JavaDoc) ?
119                         ((Comparable JavaDoc) indexData).compareTo(indexData2)
120                         : indexData.toString().compareTo(indexData2.toString()));
121                 if (comparison != 0)
122                     return (comparison);
123             }
124             return (0);
125         }
126
127         final Object JavaDoc indices[];
128         final CompositeData JavaDoc compositeData;
129     }
130
131     protected abstract Table getTable();
132 }
133
134
Popular Tags