1 14 package org.jmanage.core.management.data; 15 16 import org.jmanage.util.display.Table; 17 18 import javax.management.openmbean.TabularData ; 19 import javax.management.openmbean.TabularType ; 20 import javax.management.openmbean.CompositeType ; 21 import javax.management.openmbean.CompositeData ; 22 import java.util.*; 23 24 30 public abstract class TabularDataFormat implements DataFormat { 31 32 public String format(Object data) { 33 TabularData tabularData = (TabularData ) data; 34 35 TabularType type = tabularData.getTabularType(); 36 CompositeType rowType = type.getRowType(); 37 38 39 Set itemNamesSet = rowType.keySet(); 40 String [] itemNames = 41 (String [])itemNamesSet.toArray(new String [itemNamesSet.size()]); 42 43 final List indexNamesList= tabularData.getTabularType().getIndexNames(); 44 final String [] indexNames = 45 (String [])indexNamesList.toArray(new String [indexNamesList.size()]); 46 47 48 for(int i=0; i<indexNames.length; i++){ 49 if(!itemNames[i].equals(indexNames[i])){ 50 int index = find(itemNames, indexNames[i]); 52 if(index != -1){ 53 itemNames[index] = itemNames[i]; 55 itemNames[i] = indexNames[i]; 56 } 57 } 58 } 59 60 61 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 compositeData = (CompositeData ) it.next(); 67 final Object [] indices = compositeData.getAll(indexNames); 68 tabularDataData[ii++] = new TabularDataData(indices, compositeData); 69 } 70 Arrays.sort(tabularDataData); 71 72 73 List rows = new LinkedList(); 74 for (int i=0; i < dataRowsCount; i++) { 75 CompositeData compositeData = tabularDataData[i].compositeData; 76 String [] itemValues = new String [itemNames.length]; 77 for (int j = 0; j < itemNames.length; j++) { 78 Object value = compositeData.get(itemNames[j]); 79 itemValues[j] = DataFormatUtil.format(value); 80 } 81 rows.add(itemValues); 82 } 83 84 85 Table table = getTable(); 86 table.setHeader(itemNames); 87 table.addRows(rows); 88 return table.draw(); 89 } 90 91 private int find(String [] array, String 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 private static class TabularDataData implements Comparable { 103 104 TabularDataData(final Object indices[], final CompositeData compositeData) { 105 this.indices = indices; 106 this.compositeData = compositeData; 107 } 108 109 public int compareTo(Object data2) { 110 for (int ii = 0; ii < indices.length; ++ii) { 111 final Object indexData = indices[ii]; 112 final Object indexData2 = ((TabularDataData)data2).indices[ii]; 113 if (indexData == null) 114 if (indexData2 == null) 115 continue; else 117 return (-1); final int comparison = ((indexData instanceof Comparable ) ? 119 ((Comparable ) 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 indices[]; 128 final CompositeData compositeData; 129 } 130 131 protected abstract Table getTable(); 132 } 133 134 | Popular Tags |