001: package org.osbl.client.wings.devtools;
002:
003: import org.wings.*;
004: import org.wings.border.SLineBorder;
005: import org.wings.table.SDefaultTableCellRenderer;
006: import org.osbl.client.wings.shell.*;
007: import org.osbl.client.wings.form.DelegateEnvironment;
008: import org.osbl.client.ClientServiceProvider;
009: import org.wingx.XScrollPane;
010: import org.wingx.XTable;
011:
012: import javax.swing.event.ListSelectionEvent;
013: import javax.swing.event.ListSelectionListener;
014: import javax.swing.table.AbstractTableModel;
015: import java.awt.Color;
016: import java.beans.*;
017: import java.lang.reflect.Method;
018: import java.util.*;
019:
020: /**
021: * @author hengels
022: * @version $Revision$
023: */
024: public class ClientServiceBrowser extends SPanel implements Application {
025: protected Environment environment = new DelegateEnvironment(true);
026:
027: SList beansList = new SList();
028: PropertiesTableModel model = new PropertiesTableModel();
029: XTable propertiesTable = new XTable(model);
030: private ClientServiceProvider serviceProvider = Client
031: .getInstance().getServiceProvider();
032:
033: public ClientServiceBrowser() {
034: super .setLayout(new SBorderLayout());
035:
036: beansList.setListData(serviceProvider.getServiceNames());
037: beansList.setSelectionMode(SList.SINGLE_SELECTION);
038: beansList.addListSelectionListener(new ListSelectionListener() {
039: public void valueChanged(ListSelectionEvent e) {
040: String name = (String) beansList.getSelectedValue();
041: Object bean = serviceProvider.getService(name);
042: try {
043: PropertyDescriptor[] descriptors = Introspector
044: .getBeanInfo(bean.getClass())
045: .getPropertyDescriptors();
046: List list = new LinkedList();
047: for (int i = 0; i < descriptors.length; i++) {
048: PropertyDescriptor descriptor = descriptors[i];
049: if (descriptor.getReadMethod() != null)
050: list.add(descriptor);
051: }
052: model.setBean(bean);
053: model
054: .setDescriptors((PropertyDescriptor[]) list
055: .toArray(new PropertyDescriptor[list
056: .size()]));
057: } catch (IntrospectionException e1) {
058: e1.printStackTrace();
059: model.setDescriptors(new PropertyDescriptor[0]);
060: }
061: }
062: });
063:
064: propertiesTable.setEditable(false);
065: propertiesTable.setDefaultRenderer(new ValueCellRenderer());
066:
067: XScrollPane scrollPane = new XScrollPane(propertiesTable);
068: add(beansList, SBorderLayout.NORTH);
069: add(scrollPane, SBorderLayout.CENTER);
070:
071: environment.setContentPane(this );
072: environment
073: .setTitle("org.osbl.client.wings.devtools.clientServiceBrowser");
074: }
075:
076: public Environment getEnvironment() {
077: return environment;
078: }
079:
080: class PropertiesTableModel extends AbstractTableModel {
081: Object bean;
082: PropertyDescriptor[] descriptors = new PropertyDescriptor[0];
083:
084: public void setBean(Object bean) {
085: this .bean = bean;
086: fireTableDataChanged();
087: }
088:
089: public void setDescriptors(PropertyDescriptor[] descriptors) {
090: this .descriptors = descriptors;
091: fireTableStructureChanged();
092: }
093:
094: public int getRowCount() {
095: return descriptors.length;
096: }
097:
098: public int getColumnCount() {
099: return 2;
100: }
101:
102: public String getColumnName(int column) {
103: return new String[] { "name", "value" }[column];
104: }
105:
106: public Object getValueAt(int rowIndex, int columnIndex) {
107: switch (columnIndex) {
108: case 0:
109: return descriptors[rowIndex].getName();
110: case 1:
111: PropertyDescriptor descriptor = descriptors[rowIndex];
112: Method getter = descriptor.getReadMethod();
113: try {
114: return getter.invoke(bean);
115: } catch (Exception e) {
116: e.printStackTrace();
117: return e.getMessage();
118: }
119: default:
120: return "";
121: }
122: }
123: }
124:
125: private class ValueCellRenderer extends SDefaultTableCellRenderer {
126: public ValueCellRenderer() {
127: setStyle("SLabel wrap");
128: }
129:
130: public SComponent getTableCellRendererComponent(STable table,
131: Object value, boolean selected, int row, int col) {
132: if (value instanceof Throwable) {
133: Throwable throwable = (Throwable) value;
134: StringBuffer buffer = new StringBuffer(throwable
135: .getMessage());
136: buffer.append("\n");
137: for (int i = 0; i < throwable.getStackTrace().length; i++) {
138: StackTraceElement traceElement = throwable
139: .getStackTrace()[i];
140: buffer.append(traceElement.toString());
141: buffer.append("\n");
142: }
143: setText(buffer.toString());
144: } else
145: setText("" + value);
146:
147: return this;
148: }
149: }
150: }
|