Thread: [Mc4j-cvs] mc4j/src/org/mc4j/console/dashboard/components OperationResultTableComponent.java,1.3,1.4
Brought to you by:
ghinkl
[Mc4j-cvs] mc4j/src/org/mc4j/console/dashboard/components OperationResultTableComponent.java,1.3,1.4
From: Greg H. <gh...@us...> - 2006-04-19 03:36:29
|
Update of /cvsroot/mc4j/mc4j/src/org/mc4j/console/dashboard/components In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6628a/src/org/mc4j/console/dashboard/components Modified Files: OperationResultTableComponent.java Log Message: Rewrote the table model Index: OperationResultTableComponent.java =================================================================== RCS file: /cvsroot/mc4j/mc4j/src/org/mc4j/console/dashboard/components/OperationResultTableComponent.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** OperationResultTableComponent.java 17 Apr 2006 12:01:27 -0000 1.3 --- OperationResultTableComponent.java 19 Apr 2006 03:36:22 -0000 1.4 *************** *** 22,50 **** import org.jdesktop.swingx.decorator.Highlighter; import org.jdesktop.swingx.decorator.HighlighterPipeline; import org.mc4j.console.dashboard.context.OgnlHelper; - import org.mc4j.ems.connection.bean.EmsBean; import org.mc4j.ems.connection.bean.attribute.EmsAttribute; import org.mc4j.ems.connection.bean.operation.EmsOperation; import javax.swing.*; import java.awt.BorderLayout; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; - import java.util.Iterator; import java.util.List; import java.util.Map; /** * @author Greg Hinkle (gh...@us...), January 2004 * @version $Revision$($Author$ / $Date$) */ ! public class OperationResultTableComponent extends AttributeTableComponent implements BeanComponent { - protected EmsBean emsBean; - protected String operationName; - protected String attributeKeyListName; protected String rowKey; ! protected EmsOperation operation; --- 22,60 ---- import org.jdesktop.swingx.decorator.Highlighter; import org.jdesktop.swingx.decorator.HighlighterPipeline; + import org.mc4j.console.dashboard.DashboardComponent; import org.mc4j.console.dashboard.context.OgnlHelper; import org.mc4j.ems.connection.bean.attribute.EmsAttribute; import org.mc4j.ems.connection.bean.operation.EmsOperation; import javax.swing.*; + import javax.swing.table.AbstractTableModel; + import javax.swing.table.DefaultTableCellRenderer; + import javax.swing.table.TableColumn; import java.awt.BorderLayout; + import java.awt.Component; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.Map; + import java.util.Enumeration; /** + * This is a weird little table. It uses a collection attribute as a list + * of keys to a operation that takes a string parameter. Each name in the + * collection attribute represents a row. We then grab fields of the returned + * value objects for the columns. + * <p/> + * It also has a simpler version where it expects only an operation that + * returns a collection that is then rowed the same way. + * * @author Greg Hinkle (gh...@us...), January 2004 * @version $Revision$($Author$ / $Date$) */ ! public class OperationResultTableComponent extends JPanel implements DashboardComponent { protected String rowKey; ! protected List<String> fieldNames = new ArrayList<String>(); ! protected EmsAttribute keyAttribute; protected EmsOperation operation; *************** *** 52,68 **** protected String keyLabel; - protected List currentKeys = new ArrayList(); ! public void setBean(EmsBean emsBean) { ! this.emsBean = emsBean; ! } ! public void setAttributeKeyListName(String attributeKeyListName) { ! this.attributeKeyListName = attributeKeyListName; } ! public void setOperationName(String operationName) { ! this.operationName = operationName; } --- 62,76 ---- protected String keyLabel; protected List currentKeys = new ArrayList(); ! protected JXTable table; ! protected OperationResultsTableModel tableModel; ! public void setFieldName(String fieldName) { ! this.fieldNames.add(fieldName); } ! public void setKeyAttribute(EmsAttribute keyAttribute) { ! this.keyAttribute = keyAttribute; } *************** *** 71,74 **** --- 79,83 ---- } + public void setRowKey(String rowKey) { this.rowKey = rowKey; *************** *** 76,91 **** protected Object execute(String key) { - try { ! ! final Object[] parameterValues = new Object[]{key}; ! final String[] parameterTypes = new String[]{"java.lang.String"}; ! ! Object result = null; ! ! // EmsOperation operation = emsBean.getOperation(operationName); ! result = operation.invoke(parameterValues); ! ! return result; } catch (Exception e) { e.printStackTrace(); --- 85,90 ---- protected Object execute(String key) { try { ! return operation.invoke(key); } catch (Exception e) { e.printStackTrace(); *************** *** 94,189 **** } - public void refresh() { - - // if (this.tableModel instanceof AttributeTableModel) - // ((AttributeTableModel) this.tableModel).resetChangeList(); - - Collection keysCollection = null; - if (attributeKeyListName != null) - keysCollection = getKeys(); - else - keysCollection = (Collection) operation.invoke(); - - - - - int row = 0; - for (Iterator iterator = keysCollection.iterator(); iterator.hasNext();) { - Object key = iterator.next(); - - Object value = null; - - if (this.rowKey != null) { - value = key; - try { - key = OgnlHelper.getValue(this.rowKey, null, value, null); - } catch (OgnlException e) { - e.printStackTrace(); - } - } - - if (!currentKeys.contains(key)) { - currentKeys.add(key); - tableModel.incrementRowCount(); - } - row = currentKeys.indexOf(key); - - if (attributeKeyListName != null) - value = execute((String) key); - - - int col = 0; - for (Iterator iterator1 = attributeNames.iterator(); iterator1.hasNext();) { - String attributeName = (String) iterator1.next(); - - Object colValue = null; - if (attributeName.equals("KEY")) { - colValue = key; - } else { ! try { ! colValue = OgnlHelper.getValue(attributeName,null,value,null); ! } catch (OgnlException e) { ! e.printStackTrace(); ! } ! /* ! try { ! BeanInfo info = Introspector.getBeanInfo(value.getClass()); ! PropertyDescriptor[] props = info.getPropertyDescriptors(); ! PropertyDescriptor prop = null; ! for (int i = 0; i < props.length; i++) { ! if (props[i].getName().equals(attributeName)) { ! prop = props[i]; ! break; ! } ! } ! if (prop != null) { ! Object attrObject = prop.getReadMethod().invoke(value, new Object[]{}); ! colValue = (attrObject != null ? attrObject.toString() : "null"); ! } else { ! colValue = "unknown attribute"; ! } ! ! ! } catch (IllegalAccessException e) { ! e.printStackTrace(); ! } catch (InvocationTargetException e) { ! e.printStackTrace(); ! } catch (IntrospectionException e) { ! e.printStackTrace(); ! }*/ ! } ! if ((colValue != null) && !colValue.equals(this.tableModel.getValueAt(row, col))) ! this.tableModel.setValueAt(colValue, row, col); ! ! col++; ! } ! } if (firstLoad) { this.table.setHorizontalScrollEnabled(false); this.table.packAll(); firstLoad = false; } --- 93,111 ---- } ! public void refresh() { ! this.tableModel.refresh(); if (firstLoad) { + this.table.setRolloverEnabled(true); + for (Enumeration e = this.table.getColumnModel().getColumns(); e.hasMoreElements();) { + TableColumn col = (TableColumn) e.nextElement(); + col.setMaxWidth(400); + } this.table.setHorizontalScrollEnabled(false); this.table.packAll(); firstLoad = false; + this.table.doLayout(); } *************** *** 191,202 **** } ! private Collection getKeys() { ! EmsAttribute keyAttribute = emsBean.getAttribute(attributeKeyListName); ! keyAttribute.refresh(); ! Object keysValue = keyAttribute.getValue(); ! Collection keysCollection = null; if (keysValue instanceof Collection) { ! keysCollection = (Collection) keysValue; } else if (keysValue.getClass().isArray()) { keysCollection = Arrays.asList((String[]) keysValue); --- 113,122 ---- } ! private List getKeys() { ! Object keysValue = keyAttribute.refresh(); ! List keysCollection = null; if (keysValue instanceof Collection) { ! keysCollection = new ArrayList((Collection) keysValue); } else if (keysValue.getClass().isArray()) { keysCollection = Arrays.asList((String[]) keysValue); *************** *** 215,229 **** this.setOpaque(false); - Object[] columnNames = attributeNames.toArray(new String[attributeNames.size()]); - - if (keyLabel != null && attributeNames.contains("KEY")) { - columnNames[attributeNames.indexOf("KEY")] = keyLabel; - } - this.tableModel = new AttributeTableModel(columnNames,0); this.table = new JXTable(tableModel); this.table.setColumnControlVisible(true); this.table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); --- 135,149 ---- this.setOpaque(false); + this.tableModel = new OperationResultsTableModel(); //AttributeTableModel(columnNames, 0); + this.tableModel.refresh(); this.table = new JXTable(tableModel); this.table.setColumnControlVisible(true); + this.table.setRowHeightEnabled(true); + int keyIndex = this.fieldNames.indexOf("KEY"); + if (keyIndex >= 0) + this.table.getColumnExt(keyIndex).setCellRenderer(new MultiRowTableCellRenderer()); this.table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); *************** *** 241,250 **** } ! public void setContext(Map context) { ! // if (this.emsBean == null) { ! // throw new IllegalStateException("AttributeTableComponent: You must specify an appropriate [bean] attribute."); ! // } init(); } --- 161,255 ---- } + public static class MultiRowTableCellRenderer extends DefaultTableCellRenderer { ! public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { ! if (value instanceof String) { ! String val = (String) value; ! if (val.contains("\n")) { ! val = "<html>" + val.replace("\n","<br>") + "</html>"; ! setToolTipText(val); ! } ! } ! return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); ! } ! } ! ! public class OperationResultsTableModel extends AbstractTableModel { ! List<String> keys; ! List rows; ! ! List<Integer> rowHeights = new ArrayList<Integer>(); ! ! int y = 24; //table.getFontMetrics(table.getFont()).getHeight(); ! ! boolean initial = true; + public void refresh() { + if (keyAttribute != null) { + keys = getKeys(); + rows = new ArrayList(); + int i = 0; + for (Object key : keys) { + rows.add(operation.invoke(key)); + + if (initial && table != null) { + String keyString = (String) key; + final int lines = keyString.split("\n").length; + final int row = i++; + // SwingUtilities.invokeLater(new Runnable() { + // public void run() { + // table.setRowHeight(row,lines*y); + // } + // }); + } + } + } else { + rows = operation.getParameters(); + } + if (table != null) { + // table.updateViewSizeSequence(); + initial = false; + } + } + + + public int getRowCount() { + return rows.size(); + } + + public int getColumnCount() { + return fieldNames.size(); + } + + public String getColumnName(int column) { + String field = fieldNames.get(column); + if ("KEY".equals(field) && keyLabel != null) + return keyLabel; + else + return field; + } + + public Object getValueAt(int rowIndex, int columnIndex) { + Object row = rows.get(rowIndex); + + String field = fieldNames.get(columnIndex); + if (keys != null && field.equals("KEY")) { + String keyValue = keys.get(rowIndex); + // if (table.getRowHeight(rowIndex) < 20){ + // table.setRowHeight(rowIndex,150); + // } + return keyValue; + } else { + try { + return OgnlHelper.getValue(field, null, row, null); + } catch (OgnlException e) { + return e.getMessage(); + } + } + } + } + + + public void setContext(Map context) { init(); } |