home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-07-17 | 6.0 KB | 179 lines |
-
- package borland.samples.beans.dbbrowserbean;
-
- import java.awt.*;
- import java.awt.event.*;
- import java.sql.*;
- import borland.jbcl.layout.*;
- import borland.jbcl.control.*;
- import borland.jbcl.dataset.*;
- import borland.jbcl.model.*;
- import borland.jbcl.view.*;
- import borland.jbcl.model.*;
- import borland.jbcl.util.*;
- import COM.objectspace.jgl.*;
-
- public class DBBrowserBean extends BeanPanel implements BlackBox {
- BevelPanel bevelPanel1 = new BevelPanel();
- BorderLayout borderLayout1 = new BorderLayout();
- BorderLayout borderLayout2 = new BorderLayout();
- TreeControl treeControl1 = new TreeControl();
- Database database1 = new Database();
- String table = new String();
-
- public static final String TABLE_SELECTION_EVENT = "TableSelectionChanged";
-
- public DBBrowserBean() {
- try {
- jbInit();
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- public void jbInit() throws Exception{
- treeControl1.setEditInPlace(false);
- treeControl1.setStyle(borland.jbcl.view.TreeView.STYLE_ARROWS);
- treeControl1.addSelectionListener(new DBBrowserBean_treeControl1_selectionAdapter(this));
- treeControl1.addMouseListener(new DBBrowserBean_treeControl1_mouseAdapter(this));
- treeControl1.setExpandByDefault(false);
- bevelPanel1.setLayout(borderLayout2);
- this.setLayout(borderLayout1);
- this.add(bevelPanel1, BorderLayout.CENTER);
- bevelPanel1.add(treeControl1, BorderLayout.CENTER);
- }
-
- public String getTableName() {return this.table;}
- public Database getDatabase() {return this.database1;}
- public void setDatabase(Database db) {
- this.database1 = db;
- this.setupTree();
- }
-
- private void setupTree() {
- String[] tableArray = {"",""};
- Image imageUserTable = ImageLoader.loadFromResource("table.gif", this);
- Image imageSystemTable = ImageLoader.loadFromResource("systable.gif", this);
- Image imageView = ImageLoader.loadFromResource("view.gif", this);
- ImageLoader.waitForImage(this, imageUserTable);
- ImageLoader.waitForImage(this, imageSystemTable);
- ImageLoader.waitForImage(this, imageView);
-
- treeControl1.setViewManager( new BasicViewManager(
- new CompositeItemPainter(new ImageItemPainter(treeControl1, Alignment.LEFT),
- new SelectableTextItemPainter())));
-
- int i = 0;
- if (database1 != null) {
- try {
- DatabaseMetaData d1 = database1.getMetaData();
- GraphLocation root = treeControl1.setRoot(new Pair(null, d1.getURL()));
- GraphLocation user = treeControl1.addChild(root, new Pair(null, "User Tables"));
- GraphLocation system = treeControl1.addChild(root, new Pair(null, "System Tables"));
- GraphLocation view = treeControl1.addChild(root, new Pair(null, "Views"));
- treeControl1.expand(root);
-
- tableArray = this.getTables(database1, "TABLE");
- for (i=0;i<tableArray.length;i++) {
- treeControl1.addChild(user, new Pair(imageUserTable, tableArray[i]));
- }
-
- tableArray = this.getTables(database1, "SYSTEM TABLE");
- for (i=0;i<tableArray.length;i++) {
- treeControl1.addChild(system, new Pair(imageSystemTable, tableArray[i]));
- }
-
- tableArray = this.getTables(database1, "VIEW");
- for (i=0;i<tableArray.length;i++) {
- treeControl1.addChild(view, new Pair(imageView, tableArray[i]));
- }
- }
- catch(DataSetException de) {System.out.println(de);}
- catch(java.sql.SQLException se) {System.out.println(se);}
- catch(Exception e) {System.out.println(e);}
- }
- }
-
- private String[] getTables(Database d, String type) throws Exception {
- DatabaseMetaData mData;
- String[] tableArray = {"",""};
- int nTables = 0;
- int nColumns = 0;
- int i = 0;
- String x[] = {type};
-
- mData = d.getMetaData();
- ResultSet resultSet = mData.getTables( null, null, null, x);
- while(resultSet.next())
- nTables++;
-
- resultSet = mData.getTables( null, null, null, x);
- ResultSetMetaData rsmd = resultSet.getMetaData();
- nColumns = rsmd.getColumnCount();
-
- tableArray = new String[nTables];
- while(resultSet.next()) {
- tableArray[i] = resultSet.getString(3);
- i++;
- }
- return tableArray;
- }
-
- /***
- * Expand and contract the tree with a single click
- */
- void treeControl1_mouseClicked(MouseEvent e) {
- GraphLocation[] selections = treeControl1.getSelection().getAll();
- if (e.getClickCount() == 2) {
- if (selections[0].hasChildren() == borland.jbcl.util.TriState.YES) {
- if (treeControl1.isExpanded(selections[0]))
- treeControl1.collapse(selections[0]);
- else
- treeControl1.expand(selections[0]);
- }
- }
- }
-
- /***
- * process the action event here for table name change
- * use can use the ActionPerformed and check the following syntax:
- * Event e
- * if ( e.getActionCommand() == "TableSelectionChanged") {
- * // Your code here
- * }
- */
- void treeControl1_selectionChanged(GraphSelectionEvent e) {
- GraphLocation[] selections = treeControl1.getSelection().getAll();
- if (selections[0].hasChildren() == borland.jbcl.util.TriState.NO) {
- this.table = ((Pair) treeControl1.get(selections[0])).second.toString();
- processActionEvent(new ActionEvent(this,ActionEvent.ACTION_PERFORMED, TABLE_SELECTION_EVENT));
- }
- }
- }
-
- class DBBrowserBean_treeControl1_mouseAdapter extends java.awt.event.MouseAdapter {
- DBBrowserBean adaptee;
-
- DBBrowserBean_treeControl1_mouseAdapter(DBBrowserBean adaptee) {
- this.adaptee = adaptee;
- }
-
- public void mouseClicked(MouseEvent e) {
- adaptee.treeControl1_mouseClicked(e);
- }
- }
-
- class DBBrowserBean_treeControl1_selectionAdapter extends borland.jbcl.model.GraphSelectionAdapter {
- DBBrowserBean adaptee;
-
- DBBrowserBean_treeControl1_selectionAdapter(DBBrowserBean adaptee) {
- this.adaptee = adaptee;
- }
-
- public void selectionChanged(GraphSelectionEvent e) {
- adaptee.treeControl1_selectionChanged(e);
- }
- }
-
-