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