Source Code Cross Referenced for ListActions.java in  » Database-Client » SQLMinus » isql » 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 » SQLMinus » isql 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package isql;
002:
003:        import java.io.*;
004:        import java.awt.*;
005:        import java.awt.event.ActionEvent;
006:        import java.text.*;
007:        import javax.swing.*;
008:        import javax.swing.KeyStroke;
009:        import javax.swing.SwingConstants;
010:        import javax.swing.text.*;
011:        import util.*;
012:        import java.util.ArrayList;
013:
014:        /**
015:         * Actions pertaining to the list, these all require a table to be
016:         * selected (barring refresh list) and they all require a connection.
017:         * @author  Rahul Kumar $Author: rahul_kumar $
018:         * @version $Id: ListActions.java,v 1.3 2004/01/26 15:25:44 rahul_kumar Exp rahul $
019:         */
020:        public class ListActions {
021:
022:            SQLForm _form = null;
023:
024:            /** constructor passing SQLForm
025:             */
026:            public ListActions(SQLForm form) {
027:                _form = form;
028:            }
029:
030:            public Action[] getActions() {
031:                return new Action[] { new ViewColumnsAction(_form),
032:                        new ViewPrimaryKeysAction(_form),
033:                        new ViewIndexesAction(_form),
034:                        new RefreshListAction(_form),
035:                        new CreateCreateScriptAction(_form),
036:                        new CreateInsertScriptAction(_form),
037:                        new ViewCountAction(_form),
038:                        new ViewDistinctAction(_form),
039:                        new MakeNotNullAction(_form),
040:                        new GetDuplicatesAction(_form),
041:                        new MakePrimaryAction(_form),
042:                        new FormatSQLAction(_form) };
043:            }
044:
045:            public static final String createCreateScriptAction = "create-createscript";
046:            public static final String createInsertScriptAction = "create-insertscript";
047:            public static final String viewColumnsAction = "view-columns";
048:            public static final String viewPrimaryKeysAction = "view-primarykeys";
049:            public static final String viewIndexesAction = "view-indexes";
050:            public static final String refreshListAction = "refresh-list";
051:            public static final String viewCountAction = "view-count";
052:            public static final String viewDistinctAction = "view-distinct";
053:            public static final String formatSQLAction = "format-sql";
054:            public static final String makeNotNullAction = "makeNotNullAction";
055:            public static final String getDuplicatesAction = "getDuplicatesAction";
056:            public static final String makePrimaryAction = "makePrimaryAction";
057:            // TODO also automcaticall should load into List menu in SQLForm
058:            public static final String makeIndexAction = "makeIndexAction";
059:
060:            public static class GetDuplicatesAction extends AbstractAction {
061:                public GetDuplicatesAction(SQLForm form) {
062:                    super (getDuplicatesAction);
063:                    this ._form = form;
064:                    //putValue("accelerator","Meta C");
065:                }
066:
067:                final SQLForm _form;
068:
069:                public void actionPerformed(ActionEvent e) {
070:                    String command = "select $clist, count(*) from $tn group by $clist having count(*)>1 ";
071:                    String[] commands = formatCommand(_form, command);
072:                    executeCommand(_form, commands, false);
073:                }
074:            }
075:
076:            public static class MakeNotNullAction extends AbstractAction {
077:                public MakeNotNullAction(SQLForm form) {
078:                    super (makeNotNullAction);
079:                    this ._form = form;
080:                    //putValue("accelerator","Meta C");
081:                }
082:
083:                final SQLForm _form;
084:
085:                public void actionPerformed(ActionEvent e) {
086:                    String command = "alter table $tn modify $cn $dt NOT NULL ";
087:                    String[] commands = formatCommand(_form, command);
088:                    executeCommand(_form, commands, true);
089:                }
090:            }
091:
092:            public static class MakePrimaryAction extends AbstractAction {
093:                public MakePrimaryAction(SQLForm form) {
094:                    super (makePrimaryAction);
095:                    this ._form = form;
096:                    //putValue("accelerator","Meta C");
097:                }
098:
099:                final SQLForm _form;
100:
101:                public void actionPerformed(ActionEvent e) {
102:                    String command = "alter table $tn add primary key ($clist)";
103:                    String[] commands = formatCommand(_form, command);
104:                    executeCommand(_form, commands, true);
105:                }
106:            }
107:
108:            public static class CreateCreateScriptAction extends AbstractAction {
109:                public CreateCreateScriptAction(SQLForm form) {
110:                    super (createCreateScriptAction);
111:                    this ._form = form;
112:                    putValue("accelerator", "Meta C");
113:                }
114:
115:                final SQLForm _form;
116:
117:                public void actionPerformed(ActionEvent e) {
118:                    String tname = getSelectedTableName(_form);
119:                    String fname = tname + ".sql";
120:                    if (tname != null) {
121:                        _form.myjdbc.createCreateScript(tname, fname);
122:                        _form.setErrorArea('\n' + "Written create script to "
123:                                + fname);
124:                    } else
125:                        _form.popup("Do select a table.");
126:                }
127:            }
128:
129:            public static class CreateInsertScriptAction extends AbstractAction {
130:                public CreateInsertScriptAction(SQLForm form) {
131:                    super (createInsertScriptAction);
132:                    this ._form = form;
133:                    putValue("accelerator", "Meta I");
134:                }
135:
136:                final SQLForm _form;
137:
138:                public void actionPerformed(ActionEvent e) {
139:                    String tname = getSelectedTableName(_form);
140:                    String fname = tname + ".sql";
141:                    if (tname != null) {
142:                        _form.myjdbc.createInsertScript(tname, fname, 0, 1000);
143:                        _form.setErrorArea('\n' + "Written insert script to "
144:                                + fname);
145:
146:                    } else
147:                        _form.popup("Do select a table.");
148:                }
149:            }
150:
151:            public static class ViewColumnsAction extends AbstractAction {
152:
153:                public ViewColumnsAction(SQLForm form) {
154:                    super (viewColumnsAction);
155:                    this ._form = form;
156:                    putValue("accelerator", "control C");
157:                }
158:
159:                final SQLForm _form;
160:
161:                public void actionPerformed(ActionEvent e) {
162:                    // The following somehow isnt working on Oracle
163:                    // _form.Run( "invoke getColumns(null,null,"+tname+"'%')" );
164:                    String tname = getSelectedTableName(_form);
165:                    if (tname != null) {
166:                        _form.show(_form.myjdbc.getColumnInfo(tname), tname);
167:                    } else
168:                        _form.popup("Do select a table.");
169:                }
170:            }
171:
172:            public static class ViewPrimaryKeysAction extends AbstractAction {
173:
174:                public ViewPrimaryKeysAction(SQLForm form) {
175:                    super (viewPrimaryKeysAction);
176:                    this ._form = form;
177:                }
178:
179:                final SQLForm _form;
180:
181:                public void actionPerformed(ActionEvent e) {
182:                    String tname = getSelectedTableName(_form);
183:                    if (tname != null) {
184:                        _form
185:                                .show(_form.myjdbc.getPrimaryKeyInfo(tname),
186:                                        tname);
187:                    } else
188:                        _form.popup("Do select a table.");
189:                }
190:            }
191:
192:            public static class ViewIndexesAction extends AbstractAction {
193:
194:                public ViewIndexesAction(SQLForm form) {
195:                    super (viewIndexesAction);
196:                    this ._form = form;
197:                    putValue("accelerator", "alt I");
198:                }
199:
200:                final SQLForm _form;
201:
202:                public void actionPerformed(ActionEvent e) {
203:                    String tname = getSelectedTableName(_form);
204:                    if (tname != null) {
205:                        _form.show(_form.myjdbc.getIndexInfo(tname), tname);
206:                    } else
207:                        _form.popup("Do select a table.");
208:                }
209:            }
210:
211:            public static String getSelectedTableName(SQLForm _form) {
212:                Object[] o = _form.tablePanel.getSelectedValues();
213:                if (o != null && o.length > 0)
214:                    return o[0].toString();
215:                else
216:                    return null;
217:            }
218:
219:            /** refreshes the table list. equivalent to calling "refresh tables"
220:             * in the input area.
221:             */
222:            public static class RefreshListAction extends AbstractAction {
223:
224:                public RefreshListAction(SQLForm form) {
225:                    super (refreshListAction);
226:                    this ._form = form;
227:                    putValue("accelerator", "Meta R");
228:                }
229:
230:                final SQLForm _form;
231:
232:                public void actionPerformed(ActionEvent e) {
233:                    _form.Run("refresh tables");
234:                }
235:            }
236:
237:            /** view count of records in selected table */
238:            public static class ViewCountAction extends AbstractAction {
239:
240:                public ViewCountAction(SQLForm form) {
241:                    super (viewCountAction);
242:                    this ._form = form;
243:                    putValue("accelerator", "alt C");
244:                }
245:
246:                final SQLForm _form;
247:
248:                public void actionPerformed(ActionEvent e) {
249:                    String tname = getSelectedTableName(_form);
250:                    if (tname != null) {
251:                        _form.Run("select '" + tname + "', count(*) from "
252:                                + tname);
253:                    } else
254:                        _form.popup("Do select a table.");
255:                }
256:            }
257:
258:            /** view distinct records in selected table for selected columns.
259:             */
260:            public static class ViewDistinctAction extends AbstractAction {
261:
262:                public ViewDistinctAction(SQLForm form) {
263:                    super (viewDistinctAction);
264:                    this ._form = form;
265:                    putValue("accelerator", "control D");
266:                }
267:
268:                final SQLForm _form;
269:
270:                public void actionPerformed(ActionEvent e) {
271:                    String tname = getSelectedTableName(_form);
272:                    Object[] o = _form.columnPanel.getSelectedValues();
273:                    String cnames = null;
274:                    if (o == null || o.length == 0)
275:                        cnames = " * ";
276:                    else
277:                        cnames = SQLForm.getDelimString(o, ",");
278:                    if (tname != null) {
279:                        _form.Run("select distinct " + cnames + " from "
280:                                + tname);
281:                    } else
282:                        _form.popup("Do select a table.");
283:                }
284:            }
285:
286:            /** this action will substitute table and column names in the 
287:             * sqlpattern variable, and then execute the same.
288:             * sample patterns are:
289:             * set sqlpattern select $clist, count(*) from $tn group by $clist
290:             * having count(*) > 1
291:             * set sqlpattern alter table $tn add primary key ($clist)
292:             *
293:             */
294:            public static class FormatSQLAction extends AbstractAction {
295:                public FormatSQLAction(SQLForm form) {
296:                    super (formatSQLAction);
297:                    this ._form = form;
298:                    //putValue("accelerator","");
299:                }
300:
301:                final SQLForm _form;
302:
303:                public void actionPerformed(ActionEvent e) {
304:                    String sqlpattern = null;
305:                    if ((sqlpattern = (String) _form.getAttribute("sqlpattern")) == null) {
306:                        sqlpattern = (String) _form
307:                                .getInput("Need a variable named sqlpattern to play with.");
308:                        if (sqlpattern == null)
309:                            return;
310:                        _form.setAttribute("sqlpattern", sqlpattern);
311:                    }
312:                    String[] commands = formatCommand(_form, sqlpattern);
313:                    executeCommand(_form, commands, true);
314:                }
315:            }
316:
317:            public static void executeCommand(SQLForm _form, String[] commands,
318:                    boolean promptuser) {
319:                if (commands == null)
320:                    return;
321:                String tmpsql;
322:                for (int i = 0; i < commands.length; i++) {
323:                    tmpsql = commands[i];
324:                    if (tmpsql == null)
325:                        continue;
326:                    _form.tp.appendInputArea(tmpsql);
327:                    if (promptuser) {
328:                        tmpsql = (String) JOptionPane.showInputDialog(null,
329:                                "Edit and execute the following command:",
330:                                "Format Pattern", JOptionPane.QUESTION_MESSAGE,
331:                                null, null, tmpsql);
332:                        // if user cancels it will be null
333:                    }
334:                    if (tmpsql != null)
335:                        _form.Run(tmpsql);
336:                } // for
337:            }
338:
339:            /** should return a String[] so you can do what you want.
340:             * selects should not be prmpted. Others should be.
341:             */
342:            public static String[] formatCommand(SQLForm _form,
343:                    String sqlpattern) {
344:                if (sqlpattern == null) {
345:                    _form
346:                            .popup("Need a string with $tn and $clist or $cn to do magic with.");
347:                    return null;
348:                }
349:                String tname = getSelectedTableName(_form);
350:                String cname[] = getSelectedColumnNames(_form);
351:                if (tname == null || cname == null) {
352:                    _form
353:                            .popup("Please select a table and one or more columns.");
354:                    return null;
355:                }
356:                String clist = ArrayUtil.join(cname, ',');
357:                ArrayList /*<String>*/commands = new ArrayList(12);
358:
359:                /* if specific column then we want to iterate through each
360:                 * column */
361:                if (sqlpattern.indexOf("$cn") != -1) {
362:                    for (int i = 0; i < cname.length; i++) {
363:                        String tmpsql = PerlWrapper.perlSubstitute("s/\\$tn/"
364:                                + tname + "/g", sqlpattern);
365:                        tmpsql = PerlWrapper.perlSubstitute("s/\\$cn/"
366:                                + cname[i] + "/g", tmpsql);
367:                        tmpsql = PerlWrapper.perlSubstitute("s/\\$clist/"
368:                                + clist + "/g", tmpsql);
369:                        if (tmpsql.indexOf("$dt") > -1) {
370:                            String dt = _form.myjdbc.getFormattedDatatypeFor(
371:                                    tname, cname[i]);
372:                            if (dt != null)
373:                                tmpsql = PerlWrapper.perlSubstitute("s/\\$dt/"
374:                                        + dt + "/g", tmpsql);
375:
376:                        }
377:
378:                        commands.add(tmpsql);
379:                        // append to input are instead 
380:                        //        _form.tp.appendInputArea("\n");
381:                        //        _form.tp.appendInputArea(tmpsql);
382:                    }
383:                    //    _form.setErrorArea('\n'+ "See Input Area for generated statements.");
384:                    //_form.tp.makeInputAreaVisible();
385:                    if (commands == null)
386:                        return null;
387:                    return ArrayUtil.toStringArray(commands);
388:                }
389:                if (sqlpattern.indexOf("$tn") != -1) {
390:                    //_form.tp.makeOutputAreaVisible();
391:                    String tmpsql = PerlWrapper.perlSubstitute("s/\\$tn/"
392:                            + tname + "/g", sqlpattern);
393:                    tmpsql = PerlWrapper.perlSubstitute("s/\\$clist/" + clist
394:                            + "/g", tmpsql);
395:
396:                    _form.setErrorArea('\n' + tmpsql);
397:                    //_form.tp.appendInputArea(tmpsql);
398:                    commands.add(tmpsql);
399:                    return ArrayUtil.toStringArray(commands);
400:                } else {
401:                    System.out
402:                            .println("SQLPATTERN doesnt contain $tn or $clist :"
403:                                    + sqlpattern);
404:                    _form.setErrorArea('\n' + sqlpattern);
405:                }
406:                return null;
407:            }
408:
409:            public static String[] getSelectedColumnNames(SQLForm _form) {
410:                Object[] o = _form.columnPanel.getSelectedValues();
411:                String ret[] = new String[o.length];
412:                for (int i = 0; i < ret.length; i++) {
413:                    ret[i] = o[i].toString();
414:                }
415:                return ret;
416:            }
417:
418:        } //class
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.