Source Code Cross Referenced for IndexBrowse.java in  » Database-Client » PKLite-SQL-Client » com » pk » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database Client » PKLite SQL Client » com.pk 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.