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