Source Code Cross Referenced for AbstractRefactoringCommand.java in  » Database-Client » squirrel-sql-2.6.5a » net » sourceforge » squirrel_sql » plugins » refactoring » commands » 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 » squirrel sql 2.6.5a » net.sourceforge.squirrel_sql.plugins.refactoring.commands 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package net.sourceforge.squirrel_sql.plugins.refactoring.commands;
002:
003:        import java.awt.event.ActionEvent;
004:        import java.awt.event.ActionListener;
005:        import java.sql.SQLException;
006:        import java.util.ArrayList;
007:
008:        import net.sourceforge.squirrel_sql.client.gui.db.ColumnDetailDialog;
009:        import net.sourceforge.squirrel_sql.client.gui.db.ColumnListDialog;
010:        import net.sourceforge.squirrel_sql.client.gui.mainframe.MainFrame;
011:        import net.sourceforge.squirrel_sql.client.session.DefaultSQLExecuterHandler;
012:        import net.sourceforge.squirrel_sql.client.session.ISession;
013:        import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
014:        import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
015:        import net.sourceforge.squirrel_sql.fw.sql.ITableInfo;
016:        import net.sourceforge.squirrel_sql.fw.sql.PrimaryKeyInfo;
017:        import net.sourceforge.squirrel_sql.fw.sql.SQLDatabaseMetaData;
018:        import net.sourceforge.squirrel_sql.fw.sql.TableColumnInfo;
019:        import net.sourceforge.squirrel_sql.fw.util.ICommand;
020:        import net.sourceforge.squirrel_sql.plugins.refactoring.gui.DropTableDialog;
021:
022:        public abstract class AbstractRefactoringCommand implements  ICommand {
023:
024:            /** Current session */
025:            protected ISession _session;
026:
027:            /** Selected table(s) */
028:            protected final IDatabaseObjectInfo[] _info;
029:
030:            protected ColumnListDialog columnListDialog = null;
031:
032:            protected ColumnDetailDialog columnDetailDialog = null;
033:
034:            protected DropTableDialog dropTableDialog = null;
035:
036:            protected String pkName = null;
037:
038:            public AbstractRefactoringCommand(ISession session,
039:                    IDatabaseObjectInfo[] info) {
040:                if (session == null) {
041:                    throw new IllegalArgumentException(
042:                            "ISession cannot be null");
043:                }
044:                if (info == null) {
045:                    throw new IllegalArgumentException(
046:                            "IDatabaseObjectInfo[] cannot be null");
047:                }
048:                _session = session;
049:                _info = info;
050:            }
051:
052:            protected void showColumnListDialog(ActionListener oklistener,
053:                    ActionListener showSqlListener, int mode)
054:                    throws SQLException {
055:
056:                ITableInfo ti = (ITableInfo) _info[0];
057:                TableColumnInfo[] columns = getTableColumns(ti, mode);
058:
059:                //Show the user a dialog with a list of columns and ask them to select
060:                // one or more columns to drop
061:                if (columnListDialog == null) {
062:                    columnListDialog = new ColumnListDialog(columns, mode);
063:                    columnListDialog.addColumnSelectionListener(oklistener);
064:                    columnListDialog.addEditSQLListener(new EditSQLListener());
065:                    columnListDialog.addShowSQLListener(showSqlListener);
066:                    MainFrame mainFrame = _session.getApplication()
067:                            .getMainFrame();
068:                    columnListDialog.setLocationRelativeTo(mainFrame);
069:                    columnListDialog.setMultiSelection();
070:                }
071:                columnListDialog.setTableName(ti.getQualifiedName());
072:                if (mode == ColumnListDialog.ADD_PRIMARY_KEY_MODE) {
073:                    // Set a default primary key name based on the name of the table
074:                    String pkName = "PK_"
075:                            + columns[0].getTableName().toUpperCase();
076:                    columnListDialog.setPrimaryKeyName(pkName);
077:                }
078:                if (mode == ColumnListDialog.DROP_PRIMARY_KEY_MODE) {
079:                    SQLDatabaseMetaData md = _session.getSQLConnection()
080:                            .getSQLMetaData();
081:                    PrimaryKeyInfo[] infos = md.getPrimaryKey(ti);
082:                    String pkName = infos[0].getSimpleName();
083:                    columnListDialog.setPrimaryKeyName(pkName);
084:                }
085:                columnListDialog.setVisible(true);
086:            }
087:
088:            protected void showDropTableDialog(ActionListener oklistener,
089:                    ActionListener showSqlListener) {
090:                if (dropTableDialog == null) {
091:                    ITableInfo[] tableInfos = new ITableInfo[_info.length];
092:                    for (int i = 0; i < tableInfos.length; i++) {
093:                        tableInfos[i] = (ITableInfo) _info[i];
094:                    }
095:                    dropTableDialog = new DropTableDialog(tableInfos);
096:                    dropTableDialog.addExecuteListener(oklistener);
097:                    dropTableDialog.addShowSQLListener(showSqlListener);
098:                    dropTableDialog.addEditSQLListener(new EditSQLListener());
099:                    MainFrame mainFrame = _session.getApplication()
100:                            .getMainFrame();
101:                    dropTableDialog.setLocationRelativeTo(mainFrame);
102:                }
103:                dropTableDialog.setVisible(true);
104:            }
105:
106:            protected void setPKName(String pkName) {
107:                this .pkName = pkName;
108:            }
109:
110:            protected String getPKName() {
111:                return this .pkName;
112:            }
113:
114:            private TableColumnInfo[] getTableColumns(ITableInfo ti, int mode)
115:                    throws SQLException {
116:                SQLDatabaseMetaData md = _session.getSQLConnection()
117:                        .getSQLMetaData();
118:                if (mode == ColumnListDialog.DROP_PRIMARY_KEY_MODE) {
119:                    ArrayList<TableColumnInfo> result = new ArrayList<TableColumnInfo>();
120:                    PrimaryKeyInfo[] pkCols = md.getPrimaryKey(ti);
121:                    TableColumnInfo[] colInfos = md.getColumnInfo(ti);
122:                    for (int i = 0; i < pkCols.length; i++) {
123:                        PrimaryKeyInfo pkInfo = pkCols[i];
124:                        setPKName(pkInfo.getSimpleName());
125:                        String pkColName = pkInfo.getQualifiedColumnName();
126:                        for (int j = 0; j < colInfos.length; j++) {
127:                            TableColumnInfo colInfo = colInfos[j];
128:                            if (colInfo.getSimpleName().equals(pkColName)) {
129:                                result.add(colInfo);
130:                            }
131:                        }
132:                    }
133:                    return result.toArray(new TableColumnInfo[result.size()]);
134:                }
135:                return _session.getSQLConnection().getSQLMetaData()
136:                        .getColumnInfo(ti);
137:            }
138:
139:            protected class CommandExecHandler extends
140:                    DefaultSQLExecuterHandler {
141:                private boolean exceptionEncountered = false;
142:
143:                public CommandExecHandler(ISession session) {
144:                    super (session);
145:                }
146:
147:                /* (non-Javadoc)
148:                 * @see net.sourceforge.squirrel_sql.client.session.DefaultSQLExecuterHandler#sqlExecutionException(java.lang.Throwable, java.lang.String)
149:                 */
150:                public void sqlExecutionException(Throwable th,
151:                        String postErrorString) {
152:                    super .sqlExecutionException(th, postErrorString);
153:                    setExceptionEncountered(true);
154:                }
155:
156:                /**
157:                 * @param exceptionEncountered the exceptionEncountered to set
158:                 */
159:                public void setExceptionEncountered(boolean exceptionEncountered) {
160:                    this .exceptionEncountered = exceptionEncountered;
161:                }
162:
163:                /**
164:                 * @return the exceptionEncountered
165:                 */
166:                public boolean exceptionEncountered() {
167:                    return exceptionEncountered;
168:                }
169:            }
170:
171:            /**
172:             * The subclass should implement this so that the action listeners can get
173:             * the SQL 
174:             * @param listener TODO
175:             * @return
176:             */
177:            protected abstract void getSQLFromDialog(SQLResultListener listener);
178:
179:            /**
180:             *                      
181:             *                      
182:             *
183:             */
184:            protected class EditSQLListener implements  ActionListener,
185:                    SQLResultListener {
186:
187:                /* (non-Javadoc)
188:                 * @see net.sourceforge.squirrel_sql.plugins.refactoring.commands.SQLResultListener#finished(java.lang.String[])
189:                 */
190:                public void finished(String[] sqls) {
191:                    if (sqls == null) {
192:                        return;
193:                    }
194:                    final StringBuffer sql = new StringBuffer();
195:                    for (int i = 0; i < sqls.length; i++) {
196:                        sql.append(sqls[i]);
197:                        if (i < sqls.length) {
198:                            sql.append("\n\n");
199:                        }
200:                    }
201:                    GUIUtils.processOnSwingEventThread(new Runnable() {
202:                        public void run() {
203:                            if (columnListDialog != null) {
204:                                columnListDialog.setVisible(false);
205:                            }
206:                            if (columnDetailDialog != null) {
207:                                columnDetailDialog.setVisible(false);
208:                            }
209:                            if (dropTableDialog != null) {
210:                                dropTableDialog.setVisible(false);
211:                            }
212:                            _session.getSQLPanelAPIOfActiveSessionWindow()
213:                                    .appendSQLScript(sql.toString(), true);
214:                            _session
215:                                    .selectMainTab(ISession.IMainPanelTabIndexes.SQL_TAB);
216:                        }
217:                    });
218:                }
219:
220:                public void actionPerformed(ActionEvent e) {
221:                    final SQLResultListener listener = this ;
222:                    _session.getApplication().getThreadPool().addTask(
223:                            new Runnable() {
224:                                public void run() {
225:                                    getSQLFromDialog(listener);
226:                                }
227:                            });
228:                }
229:
230:            }
231:
232:            protected boolean tableHasPrimaryKey() throws SQLException {
233:                if (!(_info[0] instanceof  ITableInfo)) {
234:                    return false;
235:                }
236:                ITableInfo ti = (ITableInfo) _info[0];
237:                SQLDatabaseMetaData md = _session.getSQLConnection()
238:                        .getSQLMetaData();
239:                PrimaryKeyInfo[] pks = md.getPrimaryKey(ti);
240:                return (pks != null && pks.length > 0);
241:            }
242:
243:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.