001: package com.pk;
002:
003: import javax.swing.*;
004: import javax.swing.event.*;
005: import java.awt.*;
006: import java.awt.event.*;
007: import javax.swing.border.*;
008: import javax.swing.tree.*;
009: import javax.swing.table.*;
010: import java.sql.*;
011:
012: class PackageBrowse extends JFrame implements ActionListener,
013: TreeSelectionListener {
014:
015: /**
016: *
017: */
018: private static final long serialVersionUID = 2959819232816106291L;
019:
020: public PackageBrowse(MyInternalFrame mif, Connection connection,
021: String userID) {
022: super ("Package Browser");
023:
024: this .mif = mif;
025: this .connection = connection;
026: this .userID = userID;
027:
028: setBounds(0, 0, 400, 400);
029: setDefaultCloseOperation(DISPOSE_ON_CLOSE);
030: addWindowListener(new WindowHandler());
031:
032: status.setBorder(BorderFactory
033: .createBevelBorder(BevelBorder.LOWERED));
034: getContentPane().add(status, BorderLayout.SOUTH);
035:
036: // Create tree to go in left split pane
037: dbNode = new DefaultMutableTreeNode("No database");
038: dbTreeModel = new DefaultTreeModel(dbNode);
039: dbTree = new JTree(dbTreeModel);
040: treePane = new JScrollPane(dbTree);
041: treePane.setBorder(BorderFactory
042: .createLineBorder(Color.darkGray));
043:
044: table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
045: table.setShowGrid(false);
046: table.setFont(new Font("Default", Font.BOLD, 12));
047: table
048: .setPreferredScrollableViewportSize(new Dimension(500,
049: 70));
050: table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
051: tablePane = new JScrollPane(table);
052: tablePane.setBorder(BorderFactory
053: .createLineBorder(Color.darkGray));
054:
055: JSplitPane splitpane = new JSplitPane(
056: JSplitPane.HORIZONTAL_SPLIT, true, // Continuous relayout
057: treePane, // Left pane content
058: tablePane); // Right pane content
059: getContentPane().add(splitpane, BorderLayout.CENTER);
060: splitpane.setDividerLocation(150); // Left pane 150 pixels wide
061:
062: dbTree.addTreeSelectionListener(this );
063:
064: pack();
065:
066: setVisible(true); // Set window visible
067: show(); // Display the window
068:
069: }
070:
071: private String userID = null;
072:
073: private String url = null;
074:
075: MyInternalFrame mif;
076: Connection connection;
077: Statement statement;
078:
079: // private String[] drivers = { // Oracle driver
080: // "sun.jdbc.odbc.JdbcOdbcDriver", // ODBC bridge
081: // "com.imaginary.sql.msql.MsqlDriver" // mSQL driver
082: // };
083:
084: public void actionPerformed(ActionEvent e) {
085: // Object source = e.getSource(); // Get source of the event
086: }
087:
088: public void openConnection() {
089: try {
090: status.setText("Database connection established");
091:
092: statement = connection.createStatement(); // Create statement for query
093:
094: dbNode = new DefaultMutableTreeNode(url); // Root node is URL
095: dbTreeModel.setRoot(dbNode); // Set root in model
096: setupTree(connection.getMetaData()); // Set up tree with metadata
097:
098: treePane.setBorder(BorderFactory.createTitledBorder(
099: BorderFactory.createLineBorder(Color.darkGray),
100: //url,
101: userID.toUpperCase(), TitledBorder.CENTER,
102: TitledBorder.DEFAULT_POSITION));
103: dbTree.setRootVisible(true); // Now show the root node
104: dbTreeModel.reload(); // Get the tree redisplayed
105:
106: } catch (SQLException sqle) {
107: status.setText(sqle.getMessage()); // Display first message
108: do // loop through exceptions
109: {
110: System.err.println("Exception occurred:\nMessage: "
111: + sqle.getMessage());
112: System.err.println("SQL state: " + sqle.getSQLState());
113: System.err.println("Vendor code: "
114: + sqle.getErrorCode() + "\n----------------");
115: } while ((sqle = sqle.getNextException()) != null);
116: }
117: }
118:
119: private void setupTree(DatabaseMetaData metadata)
120: throws SQLException {
121: String[] tableTypes = { "PACKAGE" }; // We want only tables
122:
123: String catalog = connection.getCatalog();
124: ResultSet tables = metadata.getTables(// Get the tables info
125: catalog, userID.toUpperCase(), null, tableTypes);
126:
127: String tableName; // Stores a table name
128: DefaultMutableTreeNode tableNode; // Stores a tree node for a table
129: while (tables.next()) // For each table
130: {
131: //tableName = tables.getString("TABLE_NAME"); // get the table name
132: tableName = tables.getString(3); // get the table name
133: tableNode = new DefaultMutableTreeNode(tableName);
134: dbNode.add(tableNode); // Add the node to the tree
135:
136: // Get all the columns for the current table
137: //ResultSet columnNames = metadata.getColumns(null, null, tableName, null);
138: ResultSet columnNames = statement
139: .executeQuery("SELECT OBJECT_NAME FROM USER_OBJECTS "
140: + "WHERE OBJECT_NAME = '"
141: + tableName
142: + "' "
143: + "AND OBJECT_TYPE = 'PACKAGE BODY'");
144:
145: //System.out.println("User ID is "+userID+"\n");
146:
147: // Add nodes for the columns as children of the table node
148: while (columnNames.next())
149: tableNode.add(new DefaultMutableTreeNode(columnNames
150: .getString("OBJECT_NAME")));
151:
152: }
153: }
154:
155: public void valueChanged(TreeSelectionEvent e) {
156: TreePath[] paths = dbTree.getSelectionPaths();
157: if (paths == null)
158: return;
159:
160: boolean tableSelected = false; // Set true if a table is selected
161: String column; // Stores a column name from a path
162: String tableName; // Stores a table name from a path
163: String columnsParam = null; // Column names in SQL SELECT
164: String tableParam = null; // Table name in SQL SELECT
165: String message = null; // Message for status area
166: for (int j = 0; j < paths.length; j++) {
167: switch (paths[j].getPathCount()) {
168: case 2: // We have a table selected
169: tableParam = (String) (((DefaultMutableTreeNode) (paths[j]
170: .getPathComponent(1))).getUserObject());
171: columnsParam = "*"; // Select all columns
172: tableSelected = true; // Set flag for a table selected
173: message = "Complete " + tableParam
174: + " package displayed";
175: break;
176:
177: case 3: // Column selected
178: tableName = (String) (((DefaultMutableTreeNode) (paths[j]
179: .getPathComponent(1))).getUserObject());
180: if (tableParam == null)
181: tableParam = tableName;
182:
183: else if (tableParam != tableName)
184: break;
185: column = (String) (((DefaultMutableTreeNode) (paths[j]
186: .getPathComponent(2))).getUserObject());
187: if (columnsParam == null) // If no previous columns
188: columnsParam = column; // add the column
189: else
190: // otherwise
191: columnsParam += "," + column; // we need a comma too
192: message = columnsParam + " displayed from "
193: + tableParam + " package.";
194: break;
195: }
196: if (tableSelected) // If a table was selected
197: break; // we are done
198: }
199: try {
200: // Display the columns and change the scroll pane border
201: if (tableSelected) {
202:
203: tableModel.setResultSet(statement.executeQuery(mif
204: .getConnectionInformation()
205: .getDatabaseDialect().getPackageInfoSQLString(
206: tableParam,
207: mif.getConnectionInformation())));
208: DefaultTableColumnModel tcm = (DefaultTableColumnModel) table
209: .getColumnModel();
210: TableColumn tc = tcm.getColumn(0);
211: tc.setWidth(400);
212: tc.setResizable(true);
213: } else {
214: tableModel
215: .setResultSet(statement
216: .executeQuery("SELECT DECODE(UPPER(TRIM(TEXT)),'END;',TEXT,SUBSTR(TEXT,1,LENGTH(TEXT)-1)) AS \"PACKAGE BODY SOURCE\" FROM USER_SOURCE "
217: + "WHERE TYPE='PACKAGE BODY' "
218: + "AND NAME='"
219: + tableParam
220: + "'" + "ORDER BY LINE"));
221: DefaultTableColumnModel tcm = (DefaultTableColumnModel) table
222: .getColumnModel();
223: TableColumn tc = tcm.getColumn(0);
224: tc.setWidth(400);
225: tc.setResizable(true);
226: }
227:
228: tablePane.setBorder(BorderFactory.createTitledBorder(
229: BorderFactory.createLineBorder(Color.darkGray),
230: tableParam, TitledBorder.CENTER,
231: TitledBorder.DEFAULT_POSITION));
232: } catch (SQLException sqle) {
233: message = "Selection event Error\n" + sqle.getMessage();
234: System.err.println(message);
235: }
236: if (message != null)
237: status.setText(message);
238: }
239:
240: // Inner class defining handler for window events
241: class WindowHandler extends WindowAdapter {
242: public void windowOpened(WindowEvent e) {
243: openConnection();
244: }
245:
246: // Handler for window closing event
247: public void windowClosing(WindowEvent e) {
248: mif.packageBrowseFlag = 0;
249: }
250: }
251:
252: JTextArea status = new JTextArea(3, 30);
253:
254: DefaultMutableTreeNode dbNode; // Root node for the database tree
255: DefaultTreeModel dbTreeModel; // Model for the database metadata
256: JTree dbTree; // Tree to display the metadata
257: JScrollPane treePane; // Scroll pane holding the tree
258:
259: JScrollPane tablePane; // Scroll pane holding the table
260: ResultsModel tableModel = new ResultsModel();
261: JTable table = new JTable(tableModel);
262:
263: }
|