Source Code Cross Referenced for SQLFrame.java in  » Database-Client » JSqlTool » org » jsqltool » gui » 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 » JSqlTool » org.jsqltool.gui 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.jsqltool.gui;
002:
003:        import javax.swing.*;
004:        import javax.swing.table.*;
005:        import javax.swing.event.*;
006:        import java.awt.*;
007:        import org.jsqltool.conn.DbConnectionUtil;
008:        import org.jsqltool.conn.DbConnection;
009:        import java.awt.event.*;
010:        import org.jsqltool.gui.panel.*;
011:        import org.jsqltool.model.*;
012:        import java.util.*;
013:        import org.jsqltool.MainApp;
014:        import java.sql.*;
015:        import org.jsqltool.utils.Options;
016:        import org.jsqltool.utils.ImageLoader;
017:        import org.jsqltool.gui.graphics.SQLTextArea;
018:        import java.io.*;
019:
020:        /**
021:         * <p>Title: JSqlTool Project</p>
022:         * <p>Description: Window used to write and execute a SQL script.
023:         * </p>
024:         * <p>Copyright: Copyright (C) 2006 Mauro Carniel</p>
025:         *
026:         * <p> This file is part of JSqlTool project.
027:         * This library is free software; you can redistribute it and/or
028:         * modify it under the terms of the (LGPL) Lesser General Public
029:         * License as published by the Free Software Foundation;
030:         *
031:         *                GNU LESSER GENERAL PUBLIC LICENSE
032:         *                 Version 2.1, February 1999
033:         *
034:         * This library is distributed in the hope that it will be useful,
035:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
036:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
037:         * Library General Public License for more details.
038:         *
039:         * You should have received a copy of the GNU Library General Public
040:         * License along with this library; if not, write to the Free
041:         * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
042:         *
043:         *       The author may be contacted at:
044:         *           maurocarniel@tin.it</p>
045:         *
046:         * @author Mauro Carniel
047:         * @version 1.0
048:         */
049:        public class SQLFrame extends JInternalFrame implements  DbConnWindow,
050:                TableModelListener {
051:
052:            private DbConnectionUtil dbConnUtil = null;
053:            JPanel mainPanel = new JPanel();
054:            JPanel buttonsPanel = new JPanel();
055:            BorderLayout borderLayout1 = new BorderLayout();
056:            JSplitPane splitPane = new JSplitPane();
057:            JPanel statePanel = new JPanel();
058:            SQLTextArea editor = new SQLTextArea();
059:            DataPanel dataPanel = new DataPanel();
060:
061:            JButton executeButton = new JButton();
062:            JButton executeHistoryButton = new JButton();
063:            JButton explainPlanButton = new JButton();
064:            JButton importSQLButton = new JButton();
065:            ImageIcon executeImage;
066:            ImageIcon executeHistoryImage;
067:            ImageIcon explainPlanImage;
068:            ImageIcon importSQLImage;
069:            FlowLayout flowLayout1 = new FlowLayout();
070:            JLabel statusLabel = new JLabel();
071:            BorderLayout borderLayout2 = new BorderLayout();
072:            private String lastSQL = null;
073:            private JFrame parent = null;
074:            JWindow tableMenu = new JWindow();
075:            JWindow colMenu = new JWindow();
076:            private boolean dotPressed = false;
077:            JList tables = new JList();
078:            JList cols = new JList();
079:            JScrollPane tableScrollPane = new JScrollPane(tables);
080:            JScrollPane colScrollPane = new JScrollPane(cols);
081:            DefaultListModel tablesModel = new DefaultListModel();
082:            DefaultListModel colsModel = new DefaultListModel();
083:
084:            public SQLFrame(JFrame parent, DbConnectionUtil dbConnUtil) {
085:                super (Options.getInstance().getResource("sql editor") + " - "
086:                        + dbConnUtil.getDbConnection().getName(), true, true,
087:                        true, true);
088:                this .dbConnUtil = dbConnUtil;
089:                this .dataPanel = new DataPanel(dbConnUtil, this );
090:                this .parent = parent;
091:                try {
092:                    jbInit();
093:                    new Thread() {
094:                        public void run() {
095:                            initialize();
096:                            try {
097:                                sleep(500);
098:                            } catch (InterruptedException ex) {
099:                            }
100:                            editor.requestFocus();
101:                        }
102:                    }.start();
103:
104:                } catch (Exception e) {
105:                    e.printStackTrace();
106:                }
107:            }
108:
109:            /**
110:             * Prepare a tables combo-box.
111:             */
112:            private void initialize() {
113:                // create the tables combo-box...
114:                java.util.List tablesList = dbConnUtil.getTables(dbConnUtil
115:                        .getDbConnection().getCatalog(), "TABLE");
116:                String tableName = null;
117:                for (int i = 0; i < tablesList.size(); i++) {
118:                    tableName = tablesList.get(i).toString();
119:                    tablesModel.addElement(tableName);
120:                }
121:                tables.setModel(tablesModel);
122:                tables.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
123:                tables.revalidate();
124:                tables.addMouseListener(new MouseAdapter() {
125:                    public void mouseClicked(MouseEvent e) {
126:                        int pos = editor.getCaretPosition();
127:                        editor.setText(editor.getText().substring(0, pos)
128:                                + tables.getSelectedValue()
129:                                + editor.getText().substring(pos));
130:                        tableMenu.setVisible(false);
131:                        editor.requestFocus();
132:                        editor
133:                                .setCaretPosition(pos
134:                                        + tables.getSelectedValue().toString()
135:                                                .length());
136:                    }
137:                });
138:                tables.addListSelectionListener(new ListSelectionListener() {
139:                    public void valueChanged(ListSelectionEvent e) {
140:                        try {
141:                            tables.scrollRectToVisible(tables.getCellBounds(
142:                                    tables.getSelectedIndex(), tables
143:                                            .getSelectedIndex()));
144:                        } catch (Exception ex) {
145:                        }
146:                    }
147:                });
148:                tableMenu.getContentPane().add(tableScrollPane,
149:                        BorderLayout.CENTER);
150:
151:                // create the columns combo-box...
152:                cols.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
153:                cols.addMouseListener(new MouseAdapter() {
154:                    public void mouseClicked(MouseEvent e) {
155:                        int pos = editor.getCaretPosition();
156:                        editor.setText(editor.getText().substring(0, pos)
157:                                + cols.getSelectedValue()
158:                                + editor.getText().substring(pos));
159:                        colMenu.setVisible(false);
160:                        editor.requestFocus();
161:                        editor.setCaretPosition(pos
162:                                + cols.getSelectedValue().toString().length());
163:                    }
164:                });
165:                cols.addListSelectionListener(new ListSelectionListener() {
166:                    public void valueChanged(ListSelectionEvent e) {
167:                        try {
168:                            cols.scrollRectToVisible(cols.getCellBounds(cols
169:                                    .getSelectedIndex(), cols
170:                                    .getSelectedIndex()));
171:                        } catch (Exception ex) {
172:                        }
173:                    }
174:                });
175:
176:                colMenu.getContentPane()
177:                        .add(colScrollPane, BorderLayout.CENTER);
178:            }
179:
180:            /**
181:             * Method called when user press the "." key.
182:             * @param tableName table name to use to fetch its columns
183:             */
184:            private void initCols(String tableName) {
185:                // populate the columns combo-box...
186:                if (tableName.indexOf(".") > -1)
187:                    tableName = tableName.substring(tableName.indexOf(".") + 1);
188:                TableModel colsList = dbConnUtil.getTableColumns(tableName);
189:                colsModel.removeAllElements();
190:                for (int i = 0; i < colsList.getRowCount(); i++) {
191:                    String colName = colsList.getValueAt(i, 0).toString();
192:                    colsModel.addElement(colName);
193:                }
194:                cols.setModel(colsModel);
195:                cols.revalidate();
196:                cols.repaint();
197:            }
198:
199:            public SQLFrame() {
200:                this (null, null);
201:            }
202:
203:            public DbConnectionUtil getDbConnectionUtil() {
204:                return dbConnUtil;
205:            }
206:
207:            private void jbInit() throws Exception {
208:                mainPanel.setLayout(borderLayout1);
209:                splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
210:                statePanel.setBorder(BorderFactory.createEtchedBorder());
211:                statePanel.setMinimumSize(new Dimension(14, 24));
212:                statePanel.setPreferredSize(new Dimension(14, 24));
213:                statePanel.setLayout(borderLayout2);
214:                editor.setFont(new java.awt.Font("Monospaced", 0, 11));
215:                editor.setPreferredSize(new Dimension(372, 150));
216:                editor.setText("");
217:                editor.addFocusListener(new SQLFrame_editor_focusAdapter(this ));
218:                editor.addMouseListener(new SQLFrame_editor_mouseAdapter(this ));
219:                editor.addKeyListener(new SQLFrame_editor_keyAdapter(this ));
220:                buttonsPanel.setLayout(flowLayout1);
221:                flowLayout1.setAlignment(FlowLayout.LEFT);
222:                flowLayout1.setHgap(0);
223:                executeButton
224:                        .addActionListener(new SQLFrame_executeButton_actionAdapter(
225:                                this ));
226:                executeHistoryButton
227:                        .addActionListener(new SQLFrame_executeHistoryButton_actionAdapter(
228:                                this ));
229:                explainPlanButton
230:                        .addActionListener(new SQLFrame_explainPlanButton_actionAdapter(
231:                                this ));
232:                importSQLButton
233:                        .addActionListener(new SQLFrame_importSQLButton_actionAdapter(
234:                                this ));
235:                this .getContentPane().add(mainPanel, BorderLayout.CENTER);
236:                mainPanel.add(buttonsPanel, BorderLayout.NORTH);
237:                mainPanel.add(splitPane, BorderLayout.CENTER);
238:                mainPanel.add(statePanel, BorderLayout.SOUTH);
239:                statePanel.add(statusLabel, BorderLayout.CENTER);
240:                splitPane.add(editor, JSplitPane.TOP);
241:                splitPane.add(dataPanel, JSplitPane.BOTTOM);
242:                splitPane.setDividerLocation(150);
243:
244:                executeButton.setBorder(null);
245:                executeButton.setPreferredSize(new Dimension(24, 24));
246:                executeHistoryButton.setBorder(null);
247:                executeHistoryButton.setPreferredSize(new Dimension(24, 24));
248:                explainPlanButton.setBorder(null);
249:                explainPlanButton.setPreferredSize(new Dimension(24, 24));
250:                importSQLButton.setBorder(null);
251:                importSQLButton.setPreferredSize(new Dimension(24, 24));
252:                executeImage = ImageLoader.getInstance().getIcon("execute.gif");
253:                executeHistoryImage = ImageLoader.getInstance().getIcon(
254:                        "executehistory.gif");
255:                explainPlanImage = ImageLoader.getInstance().getIcon(
256:                        "explainplan.gif");
257:                executeButton.setIcon(executeImage);
258:                importSQLImage = ImageLoader.getInstance().getIcon(
259:                        "importscript.gif");
260:                executeButton.setToolTipText(Options.getInstance().getResource(
261:                        "execute shell content (or selected content)"));
262:                executeButton.setMaximumSize(new Dimension(24, 24));
263:                executeHistoryButton.setIcon(executeHistoryImage);
264:                executeHistoryButton.setToolTipText(Options.getInstance()
265:                        .getResource("old sql statements"));
266:                executeHistoryButton.setMaximumSize(new Dimension(24, 24));
267:                explainPlanButton.setIcon(explainPlanImage);
268:                explainPlanButton.setToolTipText(Options.getInstance()
269:                        .getResource("explain plan"));
270:                explainPlanButton.setMaximumSize(new Dimension(24, 24));
271:                importSQLButton.setIcon(importSQLImage);
272:                importSQLButton.setToolTipText(Options.getInstance()
273:                        .getResource("importsql.text"));
274:                importSQLButton.setMaximumSize(new Dimension(24, 24));
275:                buttonsPanel.add(executeButton, null);
276:                buttonsPanel.add(executeHistoryButton, null);
277:                buttonsPanel.add(explainPlanButton, null);
278:                buttonsPanel.add(importSQLButton, null);
279:            }
280:
281:            /**
282:             * This fine grain notification tells listeners the exact range
283:             * of cells, rows, or columns that changed.
284:             */
285:            public void tableChanged(TableModelEvent e) {
286:
287:            }
288:
289:            void executeButton_actionPerformed(ActionEvent e) {
290:                if (editor.getSelectedText() != null)
291:                    executeSQL(editor.getSelectedText());
292:                else if (editor.getText().trim().length() > 0)
293:                    executeSQL(editor.getText());
294:            }
295:
296:            void executeHistoryButton_actionPerformed(ActionEvent e) {
297:                SQLStatementRecallDialog f = new SQLStatementRecallDialog(
298:                        parent, dbConnUtil, this );
299:                f.setVisible(true);
300:                editor.requestFocus();
301:            }
302:
303:            private String parseRows(String sql) {
304:                lastSQL = sql;
305:                String temp = "";
306:                String newsql = "";
307:                StringTokenizer st = new StringTokenizer(sql, "\n");
308:                boolean isComment = false;
309:                while (st.hasMoreTokens()) {
310:                    temp = st.nextToken();
311:                    if (temp.indexOf("*/") != -1) {
312:                        temp = " " + temp.substring(temp.indexOf("*/") + 2);
313:                        isComment = false;
314:                    }
315:                    if (temp.indexOf("--") != -1)
316:                        temp = temp.substring(0, temp.indexOf("--"));
317:                    if (temp.indexOf("/*") != -1) {
318:                        newsql += " " + temp.substring(0, temp.indexOf("/*"));
319:                        isComment = true;
320:                    } else if (!isComment)
321:                        newsql += " " + temp;
322:                    if (temp.indexOf("*/") != -1) {
323:                        newsql += " " + temp.substring(temp.indexOf("*/") + 2);
324:                        isComment = false;
325:                    }
326:                }
327:                newsql = newsql.trim();
328:                if (newsql.endsWith(";"))
329:                    newsql = newsql.substring(0, newsql.length() - 1);
330:                return newsql;
331:            }
332:
333:            /**
334:             * Execute the sql with parameters.
335:             */
336:            public final void executeSQLWithValues(String newsql, Vector values) {
337:                if (newsql.trim().toUpperCase().startsWith("SELECT ")) {
338:                    long time = System.currentTimeMillis();
339:                    dataPanel.setQuery(newsql, values);
340:                    dataPanel.getTable().setShowGrid(true);
341:                    ((CustomTableModel) dataPanel.getTableModel())
342:                            .setEditMode(CustomTableModel.DETAIL_REC);
343:                    statusLabel.setText(Options.getInstance().getResource(
344:                            "query execution")
345:                            + ": "
346:                            + (System.currentTimeMillis() - time)
347:                            + " ms");
348:                } else {
349:                    statusLabel.setText(dbConnUtil.executeStmt(newsql, values)
350:                            + " "
351:                            + Options.getInstance().getResource(
352:                                    "records processed."));
353:                }
354:
355:                String oldQuery = newsql.replace('\n', ' ');
356:                if (!dbConnUtil.getDbConnection().getOldQueries().contains(
357:                        oldQuery)) {
358:                    // save query in query history...
359:                    dbConnUtil.getDbConnection().getOldQueries().add(oldQuery);
360:                    dbConnUtil.saveProfile(true);
361:                }
362:            }
363:
364:            private void executeSQL(String sql) {
365:                String newsql = parseRows(sql);
366:
367:                // parse sql to find out parameters...
368:                Vector values = new Vector();
369:                ParametersDialog dialog = new ParametersDialog(parent, this ,
370:                        newsql, values);
371:            }
372:
373:            void editor_keyPressed(KeyEvent e) {
374:                try {
375:                    dotPressed = false;
376:                    if (e.getKeyCode() == e.VK_F9) {
377:                        executeButton_actionPerformed(null);
378:                    }
379:                    if (e.getKeyCode() == e.VK_SPACE && e.isControlDown()
380:                            && !tableMenu.isVisible()) {
381:                        e.consume();
382:                        // view popup menu containing tables list...
383:                        tableMenu
384:                                .setLocation(editor.modelToView(editor
385:                                        .getCaretPosition()).x,
386:                                        editor.modelToView(editor
387:                                                .getCaretPosition()).y + 150);
388:                        tableMenu.setSize(300, 150);
389:                        tableMenu.setVisible(true);
390:                        tables.requestFocus();
391:                        tables.setSelectedIndex(0);
392:                    } else if (tableMenu.isVisible()) {
393:                        e.consume();
394:                        if (e.getKeyCode() == e.VK_UP
395:                                && tables.getSelectedIndex() > 0)
396:                            tables
397:                                    .setSelectedIndex(tables.getSelectedIndex() - 1);
398:                        else if (e.getKeyCode() == e.VK_DOWN
399:                                && tables.getSelectedIndex() < tablesModel
400:                                        .getSize() - 1)
401:                            tables
402:                                    .setSelectedIndex(tables.getSelectedIndex() + 1);
403:                        else if (e.getKeyCode() == e.VK_PAGE_UP) {
404:                            int i = tables.getSelectedIndex();
405:                            int w = tables.getLastVisibleIndex()
406:                                    - tables.getFirstVisibleIndex() + 1;
407:                            i = i - w;
408:                            if (i < 0)
409:                                i = 0;
410:                            tables.setSelectedIndex(i);
411:                        } else if (e.getKeyCode() == e.VK_PAGE_DOWN) {
412:                            int i = tables.getSelectedIndex();
413:                            int w = tables.getLastVisibleIndex()
414:                                    - tables.getFirstVisibleIndex() + 1;
415:                            i = i + w;
416:                            if (i - 1 > tables.getModel().getSize())
417:                                i = tables.getModel().getSize() - 1;
418:                            tables.setSelectedIndex(i);
419:                        } else if (e.getKeyCode() == e.VK_ESCAPE) {
420:                            tableMenu.setVisible(false);
421:                            editor.requestFocus();
422:                        } else if (e.getKeyCode() == e.VK_ENTER) {
423:                            int pos = editor.getCaretPosition();
424:                            editor.setText(editor.getText().substring(0, pos)
425:                                    + tables.getSelectedValue()
426:                                    + editor.getText().substring(pos));
427:                            tableMenu.setVisible(false);
428:                            editor.requestFocus();
429:                            editor.setCaretPosition(pos
430:                                    + tables.getSelectedValue().toString()
431:                                            .length());
432:                        } else {
433:                            for (int i = 0; i < tables.getModel().getSize(); i++)
434:                                if (tables.getModel().getElementAt(i)
435:                                        .toString().toLowerCase().startsWith(
436:                                                String.valueOf(e.getKeyChar())
437:                                                        .toLowerCase())) {
438:                                    tables.setSelectedIndex(i);
439:                                    break;
440:                                }
441:                            e.consume();
442:                        }
443:                    } else if (e.getKeyChar() == '.' && !colMenu.isVisible()) {
444:                        dotPressed = true;
445:                        Thread t = new Thread() {
446:                            public void run() {
447:                                try {
448:                                    sleep(500);
449:                                } catch (InterruptedException ex) {
450:                                }
451:                                if (dotPressed) {
452:                                    try {
453:                                        // view popup menu containing columns list...
454:                                        String text = editor.getText();
455:                                        int start = 0;
456:                                        while (start < text.length()
457:                                                && text.indexOf(" ", start + 1) > -1) {
458:                                            if (text.indexOf(" ", start + 1) < editor
459:                                                    .getCaretPosition()) {
460:                                                start = text.indexOf(" ",
461:                                                        start + 1);
462:                                            } else {
463:                                                break;
464:                                            }
465:                                        }
466:                                        String table = text.substring(start,
467:                                                editor.getCaretPosition() - 1);
468:                                        if (table.indexOf(",") != -1)
469:                                            table = table.substring(table
470:                                                    .indexOf(",") + 1);
471:                                        initCols(table);
472:
473:                                        colMenu
474:                                                .setLocation(
475:                                                        editor
476:                                                                .modelToView(editor
477:                                                                        .getCaretPosition()).x,
478:                                                        editor
479:                                                                .modelToView(editor
480:                                                                        .getCaretPosition()).y + 150);
481:                                        colMenu.setSize(300, 150);
482:                                        colMenu.setVisible(true);
483:                                        cols.requestFocus();
484:                                        cols.setSelectedIndex(0);
485:
486:                                    } catch (Exception ex) {
487:                                    }
488:                                }
489:                            }
490:                        };
491:                        t.start();
492:                    } else if (colMenu.isVisible()) {
493:                        e.consume();
494:                        if (e.getKeyCode() == e.VK_UP
495:                                && cols.getSelectedIndex() > 0)
496:                            cols.setSelectedIndex(cols.getSelectedIndex() - 1);
497:                        else if (e.getKeyCode() == e.VK_DOWN
498:                                && cols.getSelectedIndex() < colsModel
499:                                        .getSize() - 1)
500:                            cols.setSelectedIndex(cols.getSelectedIndex() + 1);
501:                        else if (e.getKeyCode() == e.VK_PAGE_UP) {
502:                            int i = cols.getSelectedIndex();
503:                            int w = cols.getLastVisibleIndex()
504:                                    - cols.getFirstVisibleIndex() + 1;
505:                            i = i - w;
506:                            if (i < 0)
507:                                i = 0;
508:                            cols.setSelectedIndex(i);
509:                        } else if (e.getKeyCode() == e.VK_PAGE_DOWN) {
510:                            int i = cols.getSelectedIndex();
511:                            int w = cols.getLastVisibleIndex()
512:                                    - cols.getFirstVisibleIndex() + 1;
513:                            i = i + w;
514:                            if (i - 1 > cols.getModel().getSize())
515:                                i = cols.getModel().getSize() - 1;
516:                            cols.setSelectedIndex(i);
517:                        } else if (e.getKeyCode() == e.VK_ESCAPE) {
518:                            colMenu.setVisible(false);
519:                            editor.requestFocus();
520:                        } else if (e.getKeyCode() == e.VK_ENTER) {
521:                            int pos = editor.getCaretPosition();
522:                            editor.setText(editor.getText().substring(0, pos)
523:                                    + cols.getSelectedValue()
524:                                    + editor.getText().substring(pos));
525:                            colMenu.setVisible(false);
526:                            editor.requestFocus();
527:                            editor.setCaretPosition(pos
528:                                    + cols.getSelectedValue().toString()
529:                                            .length());
530:                        } else {
531:                            for (int i = 0; i < cols.getModel().getSize(); i++)
532:                                if (cols.getModel().getElementAt(i).toString()
533:                                        .toLowerCase().startsWith(
534:                                                String.valueOf(e.getKeyChar())
535:                                                        .toLowerCase())) {
536:                                    cols.setSelectedIndex(i);
537:                                    break;
538:                                }
539:                            e.consume();
540:                        }
541:                    }
542:
543:                } catch (Exception ex) {
544:                    ex.printStackTrace();
545:                }
546:            }
547:
548:            public final void setEditorContent(String content) {
549:                editor.setText(content);
550:            }
551:
552:            void explainPlanButton_actionPerformed(ActionEvent e) {
553:                String newsql = parseRows(editor.getText());
554:                long time = System.currentTimeMillis();
555:                String id = "EP_" + String.valueOf(Math.random() * time);
556:                if (id.length() > 16)
557:                    id = id.substring(0, 16);
558:                if (dbConnUtil.getDbConnection().getDbType() == DbConnection.ORACLE_TYPE)
559:                    // ORACLE database type...
560:                    try {
561:                        Statement stmt = dbConnUtil.getConn().createStatement();
562:                        stmt.execute("EXPLAIN PLAN SET STATEMENT_ID = '"
563:                                + id
564:                                + "' INTO "
565:                                + Options.getInstance()
566:                                        .getOracleExplainPlanTable() + " FOR "
567:                                + newsql);
568:                        stmt.close();
569:                        dataPanel
570:                                .setQuery(
571:                                        "SELECT LPAD(' ',2*(LEVEL-1))||operation operation, options,"
572:                                                + "object_name, position "
573:                                                + "FROM "
574:                                                + Options
575:                                                        .getInstance()
576:                                                        .getOracleExplainPlanTable()
577:                                                + " "
578:                                                + "START WITH id = 0 AND statement_id = '"
579:                                                + id
580:                                                + "'"
581:                                                + "CONNECT BY PRIOR id = parent_id AND statement_id = '"
582:                                                + id + "'", new Vector());
583:                        dataPanel.getTable().setShowGrid(false);
584:                        ((CustomTableModel) dataPanel.getTableModel())
585:                                .setEditMode(CustomTableModel.DETAIL_REC);
586:                    } catch (SQLException ex) {
587:                    }
588:                else
589:                    JOptionPane
590:                            .showMessageDialog(
591:                                    parent,
592:                                    Options
593:                                            .getInstance()
594:                                            .getResource(
595:                                                    "feature not supported for this database type."),
596:                                    Options.getInstance().getResource(
597:                                            "attention"),
598:                                    JOptionPane.WARNING_MESSAGE);
599:            }
600:
601:            void editor_mouseClicked(MouseEvent e) {
602:                tableMenu.setVisible(false);
603:                colMenu.setVisible(false);
604:
605:                if (SwingUtilities.isRightMouseButton(e)) {
606:                    // view popup menu...
607:                    JPopupMenu menu = new JPopupMenu();
608:                    JMenuItem formatCodeOnOneRowMenu = new JMenuItem(Options
609:                            .getInstance().getResource("format sql on one row"));
610:                    formatCodeOnOneRowMenu
611:                            .addActionListener(new ActionListener() {
612:                                public void actionPerformed(ActionEvent ev) {
613:                                    try {
614:                                        String sql = editor.getText();
615:                                        int pos = 0;
616:                                        while (pos < sql.length()) {
617:                                            if (sql.charAt(pos) == '\n') {
618:                                                if (pos < sql.length() - 1) {
619:                                                    sql = sql.substring(0, pos)
620:                                                            + sql
621:                                                                    .substring(pos + 1);
622:                                                } else {
623:                                                    sql = sql.substring(0, pos);
624:                                                }
625:                                            } else {
626:                                                pos++;
627:                                            }
628:                                        }
629:                                        editor.setText(sql);
630:                                    } catch (Exception ex) {
631:                                        ex.printStackTrace();
632:                                    }
633:                                }
634:                            });
635:                    JMenuItem formatCodeOnMoreRowsMenu = new JMenuItem(Options
636:                            .getInstance().getResource(
637:                                    "format sql on more rows"));
638:                    formatCodeOnMoreRowsMenu
639:                            .addActionListener(new ActionListener() {
640:                                public void actionPerformed(ActionEvent ev) {
641:                                    try {
642:                                        String sql = editor.getText();
643:                                        int count = 0;
644:                                        int pos = 0;
645:                                        int apixCount = 0;
646:                                        while (pos < sql.length()) {
647:                                            if (sql.charAt(pos) == '\'') {
648:                                                apixCount++;
649:                                                if (pos > 0
650:                                                        && sql.charAt(pos - 1) == '\'')
651:                                                    apixCount = apixCount - 2;
652:                                            }
653:                                            if (count > 80) {
654:                                                if ((sql.charAt(pos) == ' ' || sql
655:                                                        .charAt(pos) == ',')
656:                                                        && apixCount % 2 == 0) {
657:                                                    count = 0;
658:                                                    if (pos < sql.length() - 1)
659:                                                        sql = sql.substring(0,
660:                                                                pos + 1)
661:                                                                + "\n"
662:                                                                + sql
663:                                                                        .substring(pos + 1);
664:                                                    else
665:                                                        sql = sql.substring(0,
666:                                                                pos);
667:                                                }
668:                                            }
669:                                            pos++;
670:                                            count++;
671:                                        }
672:
673:                                        editor.setText(sql);
674:                                    } catch (Exception ex) {
675:                                        ex.printStackTrace();
676:                                    }
677:                                }
678:                            });
679:                    menu.add(formatCodeOnOneRowMenu);
680:                    menu.add(formatCodeOnMoreRowsMenu);
681:                    menu.show(e.getComponent(), e.getX(), e.getY());
682:
683:                }
684:
685:            }
686:
687:            void editor_focusLost(FocusEvent e) {
688:                tableMenu.setVisible(false);
689:                colMenu.setVisible(false);
690:            }
691:
692:            void editor_keyTyped(KeyEvent e) {
693:                if (colMenu.isVisible())
694:                    e.consume();
695:                else if (tableMenu.isVisible())
696:                    e.consume();
697:            }
698:
699:            /**
700:             * Import a SQL Script from file and execute it in batch mode.
701:             */
702:            public void importSQLButton_actionPerformed(ActionEvent e) {
703:                final JFileChooser f = new JFileChooser(".");
704:                f.setDialogTitle(Options.getInstance().getResource(
705:                        "import sql script"));
706:                int res = f.showDialog(parent, Options.getInstance()
707:                        .getResource("import file"));
708:                if (res == f.APPROVE_OPTION) {
709:                    ProgressDialog.getInstance().startProgress();
710:                    new Thread() {
711:                        public void run() {
712:                            importSQL(f.getSelectedFile());
713:                        }
714:                    }.start();
715:                }
716:            }
717:
718:            /**
719:             * Import the selected file.
720:             * @param file file to import
721:             */
722:            private void importSQL(File file) {
723:                Statement stmt = null;
724:                try {
725:                    BufferedReader br = new BufferedReader(
726:                            new InputStreamReader(new FileInputStream(file)));
727:                    StringBuffer sql = new StringBuffer();
728:                    String line = null;
729:                    stmt = dbConnUtil.getConn().createStatement();
730:                    int count = 0;
731:                    while ((line = br.readLine()) != null) {
732:                        if (line.endsWith(";")) {
733:                            sql.append(line.substring(0, line.length() - 1));
734:                            stmt.addBatch(sql.toString());
735:                            sql.delete(0, sql.length());
736:                            count++;
737:                            if (count > 1000) {
738:                                count = 0;
739:                                stmt.executeBatch();
740:                            }
741:                        } else {
742:                            sql.append(" ");
743:                            sql.append(line);
744:                        }
745:                    }
746:                    if (sql.length() > 0)
747:                        stmt.addBatch(sql.toString());
748:                    stmt.executeBatch();
749:                    br.close();
750:                    ProgressDialog.getInstance().stopProgress();
751:                    JOptionPane.showMessageDialog(parent, Options.getInstance()
752:                            .getResource(
753:                                    Options.getInstance().getResource(
754:                                            "loading completed.")), Options
755:                            .getInstance().getResource("loading script file"),
756:                            JOptionPane.INFORMATION_MESSAGE);
757:
758:                } catch (Exception ex) {
759:                    ex.printStackTrace();
760:                    ProgressDialog.getInstance().stopProgress();
761:                    JOptionPane.showMessageDialog(parent, Options.getInstance()
762:                            .getResource(
763:                                    Options.getInstance().getResource(
764:                                            "error while loading script file:")
765:                                            + "\n" + ex.getMessage()), Options
766:                            .getInstance().getResource("error"),
767:                            JOptionPane.ERROR_MESSAGE);
768:                } finally {
769:                    try {
770:                        if (stmt != null) {
771:                            stmt.close();
772:                        }
773:                    } catch (Exception ex1) {
774:                        ex1.printStackTrace();
775:                    }
776:                }
777:            }
778:
779:        }
780:
781:        class SQLFrame_executeButton_actionAdapter implements 
782:                java.awt.event.ActionListener {
783:            SQLFrame adaptee;
784:
785:            SQLFrame_executeButton_actionAdapter(SQLFrame adaptee) {
786:                this .adaptee = adaptee;
787:            }
788:
789:            public void actionPerformed(ActionEvent e) {
790:                adaptee.executeButton_actionPerformed(e);
791:            }
792:        }
793:
794:        class SQLFrame_executeHistoryButton_actionAdapter implements 
795:                java.awt.event.ActionListener {
796:            SQLFrame adaptee;
797:
798:            SQLFrame_executeHistoryButton_actionAdapter(SQLFrame adaptee) {
799:                this .adaptee = adaptee;
800:            }
801:
802:            public void actionPerformed(ActionEvent e) {
803:                adaptee.executeHistoryButton_actionPerformed(e);
804:            }
805:        }
806:
807:        class SQLFrame_editor_keyAdapter extends java.awt.event.KeyAdapter {
808:            SQLFrame adaptee;
809:
810:            SQLFrame_editor_keyAdapter(SQLFrame adaptee) {
811:                this .adaptee = adaptee;
812:            }
813:
814:            public void keyPressed(KeyEvent e) {
815:                adaptee.editor_keyPressed(e);
816:            }
817:
818:            public void keyTyped(KeyEvent e) {
819:                adaptee.editor_keyTyped(e);
820:            }
821:        }
822:
823:        class SQLFrame_explainPlanButton_actionAdapter implements 
824:                java.awt.event.ActionListener {
825:            SQLFrame adaptee;
826:
827:            SQLFrame_explainPlanButton_actionAdapter(SQLFrame adaptee) {
828:                this .adaptee = adaptee;
829:            }
830:
831:            public void actionPerformed(ActionEvent e) {
832:                adaptee.explainPlanButton_actionPerformed(e);
833:            }
834:        }
835:
836:        class SQLFrame_importSQLButton_actionAdapter implements 
837:                java.awt.event.ActionListener {
838:            SQLFrame adaptee;
839:
840:            SQLFrame_importSQLButton_actionAdapter(SQLFrame adaptee) {
841:                this .adaptee = adaptee;
842:            }
843:
844:            public void actionPerformed(ActionEvent e) {
845:                adaptee.importSQLButton_actionPerformed(e);
846:            }
847:        }
848:
849:        class SQLFrame_editor_mouseAdapter extends java.awt.event.MouseAdapter {
850:            SQLFrame adaptee;
851:
852:            SQLFrame_editor_mouseAdapter(SQLFrame adaptee) {
853:                this .adaptee = adaptee;
854:            }
855:
856:            public void mouseClicked(MouseEvent e) {
857:                adaptee.editor_mouseClicked(e);
858:            }
859:        }
860:
861:        class SQLFrame_editor_focusAdapter extends java.awt.event.FocusAdapter {
862:            SQLFrame adaptee;
863:
864:            SQLFrame_editor_focusAdapter(SQLFrame adaptee) {
865:                this .adaptee = adaptee;
866:            }
867:
868:            public void focusLost(FocusEvent e) {
869:                adaptee.editor_focusLost(e);
870:            }
871:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.