001: /*
002: This source file is part of Smyle, a database library.
003: For up-to-date information, see http://www.drjava.de/smyle
004: Copyright (C) 2001 Stefan Reich (doc@drjava.de)
005:
006: This library is free software; you can redistribute it and/or
007: modify it under the terms of the GNU Lesser General Public
008: License as published by the Free Software Foundation; either
009: version 2.1 of the License, or (at your option) any later version.
010:
011: This library is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public
017: License along with this library; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019:
020: For full license text, see doc/license/lgpl.txt in this distribution
021: */
022:
023: package drjava.smyle.browser;
024:
025: import java.util.*;
026: import java.io.*;
027: import java.awt.*;
028: import javax.swing.*;
029: import javax.swing.event.*;
030: import javax.swing.text.*;
031: import javax.swing.text.html.*;
032: import javax.swing.table.*;
033: import drjava.smyle.*;
034: import org.artsProject.mcop.*;
035: import org.artsProject.mcop.core.*;
036: import java.lang.Object;
037:
038: class StoreView extends JPanel {
039: JSplitPane split, hsplit;
040: JPanel detailPanel = new JPanel(new BorderLayout());
041: JPanel dataPanel = new JPanel(new BorderLayout());
042: JScrollPane detailScroll, dataScroll;
043: JList tables;
044: JTable data;
045: Snapshot snapshot;
046: String tableName;
047:
048: static Component multilineLabel(String text) {
049: Box box = Box.createVerticalBox();
050: while (text.length() != 0) {
051: int i = text.indexOf('\n');
052: if (i < 0)
053: i = text.length();
054: box.add(new JLabel(i == 0 ? " " : text.substring(0, i)));
055: text = text.substring(Math.min(text.length(), i + 1));
056: }
057: return box;
058: }
059:
060: class DataModel extends AbstractTableModel {
061: UntypedTable table;
062: TypeDef schema;
063:
064: DataModel(UntypedTable table) {
065: this .table = table;
066: this .schema = table.getSchema();
067: }
068:
069: public int getRowCount() {
070: return table.size();
071: }
072:
073: public int getColumnCount() {
074: return schema.contents.size();
075: }
076:
077: public Object getValueAt(int row, int col) {
078: try {
079: return table.untypedElementAt(row)[col];
080: } catch (Exception e) {
081: //e.printStackTrace();
082: return e.toString();
083: }
084: }
085:
086: public String getColumnName(int i) {
087: return schema.contents.get(i).name;
088: }
089: }
090:
091: void setDetail(Component detail) {
092: detailPanel.removeAll();
093: detailPanel.add(BorderLayout.CENTER, detail);
094: detailScroll.validate();
095: }
096:
097: void updateDetail() {
098: StringBuffer buf = new StringBuffer();
099: if (tableName != null) {
100: UntypedTable table = snapshot.getUntypedTable(tableName);
101: buf.append("Rows: " + table.size() + "\n\n");
102: TypeDef schema = table.getSchema();
103: if (schema != null) {
104: buf.append("struct " + schema.name + " {\n");
105: for (int i = 0; i < schema.contents.size(); i++) {
106: TypeComponent c = schema.contents.get(i);
107: buf.append(" ");
108: for (Iterator hints = c.hints.iterator(); hints
109: .hasNext();)
110: buf.append(hints.next()).append(' ');
111: buf.append(c.type + " " + c.name + ";\n");
112: }
113: buf.append("}");
114: }
115: }
116: setDetail(multilineLabel(buf.toString()));
117: }
118:
119: void setData(JTable t) {
120: data = t;
121: dataScroll = new JScrollPane(data);
122: dataPanel.removeAll();
123: dataPanel.add(BorderLayout.CENTER, dataScroll);
124: dataPanel.validate();
125: }
126:
127: void updateData() {
128: JTable data = new JTable();
129:
130: if (tableName != null) {
131: UntypedTable table = snapshot.getUntypedTable(tableName);
132: data.setModel(new DataModel(table));
133: }
134:
135: setData(data);
136: }
137:
138: void update() {
139: String s = (String) tables.getSelectedValue();
140: if (MCOP.equals(tableName, s))
141: return;
142: tableName = s;
143: updateDetail();
144: updateData();
145: }
146:
147: StoreView(Store store) {
148: snapshot = store.snapshot();
149:
150: Vector<String> v = new Vector<String>();
151: v.addAll(snapshot.getTableNames());
152: tables = new JList((Vector) v);
153: if (!v.isEmpty())
154: tables.setSelectedIndex(0);
155: /*for (Iterator<String> i = snapshot.getTableNames().iterator(); i.hasNext(); ) {
156: String name = i.next();
157: UntypedTable table = snapshot.getUntypedTable(name);
158: tables.add(name);
159: }*/
160:
161: tables.addListSelectionListener(new ListSelectionListener() {
162: public void valueChanged(ListSelectionEvent evt) {
163: update();
164: }
165: });
166:
167: JScrollPane tablesScroll = new JScrollPane(tables);
168: tablesScroll.setBorder(BorderFactory.createTitledBorder(
169: BorderFactory.createEtchedBorder(), "Tables"));
170: detailScroll = new JScrollPane(detailPanel);
171: detailScroll.setBorder(BorderFactory.createTitledBorder(
172: BorderFactory.createEtchedBorder(), "Table Detail"));
173:
174: dataPanel.setBorder(BorderFactory.createTitledBorder(
175: BorderFactory.createEtchedBorder(), "Table Data"));
176:
177: setData(new JTable());
178:
179: update();
180:
181: split = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
182: split.setLeftComponent(tablesScroll);
183: split.setRightComponent(detailScroll);
184:
185: hsplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
186: hsplit.setLeftComponent(split);
187: hsplit.setRightComponent(dataPanel);
188: hsplit.setDividerLocation(160);
189: hsplit.validate();
190:
191: setLayout(new BorderLayout());
192: add(hsplit);
193: }
194: }
|