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