home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 1998 October
/
PCWorld_1998-10_cd.bin
/
software
/
prehled
/
inprise
/
JSAMPLES.Z
/
DBBrowserBean.java
< prev
next >
Wrap
Text File
|
1998-05-08
|
7KB
|
209 lines
/*
* Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
*
* This SOURCE CODE FILE, which has been provided by Borland as part
* of a Borland product for use ONLY by licensed users of the product,
* includes CONFIDENTIAL and PROPRIETARY information of Borland.
*
* USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS
* OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
* THE PRODUCT.
*
* IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
* COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
* OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
* OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
* OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
* OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
* CODE FILE.
*/
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.sql.dataset.*;
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();
boolean addNotifyCalled = false;
public static final String TABLE_SELECTION_EVENT = "TableSelectionChanged";
public DBBrowserBean() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
private 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();
}
public void addNotify() {
super.addNotify();
if (!addNotifyCalled) {
addNotifyCalled = true;
if (database1 != null)
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 SelectableItemPainter(
new CompositeItemPainter(new ImageItemPainter(treeControl1, Alignment.LEFT),
new TextItemPainter()))));
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);
}
}