KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > explorer > actions > RecreateTableAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.db.explorer.actions;
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.ObjectInputStream JavaDoc;
25 import java.text.MessageFormat JavaDoc;
26
27 import javax.swing.JFileChooser JavaDoc;
28
29 import org.openide.DialogDisplayer;
30 import org.openide.ErrorManager;
31 import org.openide.NotifyDescriptor;
32 import org.openide.filesystems.FileUtil;
33 import org.openide.nodes.Node;
34 import org.openide.util.RequestProcessor;
35 import org.openide.windows.WindowManager;
36
37 import org.netbeans.lib.ddl.impl.AbstractCommand;
38 import org.netbeans.lib.ddl.impl.Specification;
39
40 import org.netbeans.modules.db.explorer.infos.DatabaseNodeInfo;
41 import org.netbeans.modules.db.explorer.infos.TableListNodeInfo;
42 import org.netbeans.modules.db.explorer.dlg.LabeledTextFieldDialog;
43 import org.netbeans.modules.db.explorer.dataview.DataViewWindow;
44
45 public class RecreateTableAction extends DatabaseAction {
46     static final long serialVersionUID =6992569917995229492L;
47     
48     protected boolean enable(Node[] activatedNodes) {
49         return (activatedNodes != null && activatedNodes.length == 1);
50     }
51
52     public void performAction (Node[] activatedNodes) {
53         Node node;
54         if (activatedNodes != null && activatedNodes.length == 1)
55             node = activatedNodes[0];
56         else
57             return;
58         
59         final DatabaseNodeInfo info = (DatabaseNodeInfo) node.getCookie(DatabaseNodeInfo.class);
60         final java.awt.Component JavaDoc par = WindowManager.getDefault().getMainWindow();
61         RequestProcessor.getDefault().post(new Runnable JavaDoc() {
62             public void run () {
63                 try {
64                     TableListNodeInfo nfo = (TableListNodeInfo) info.getParent(nodename);
65                     Specification spec = (Specification) nfo.getSpecification();
66                     AbstractCommand cmd;
67
68                     // Get filename
69
JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
70                     FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
71                     chooser.setDialogType(JFileChooser.OPEN_DIALOG);
72                     chooser.setDialogTitle(bundle().getString("RecreateTableFileOpenDialogTitle")); //NOI18N
73
chooser.setFileFilter(new javax.swing.filechooser.FileFilter JavaDoc() {
74                         public boolean accept(File JavaDoc f) {
75                             return (f.isDirectory() || f.getName().endsWith(".grab")); //NOI18N
76
}
77
78                         public String JavaDoc getDescription() {
79                             return bundle().getString("GrabTableFileTypeDescription"); //NOI18N
80
}
81                     });
82
83                     if (chooser.showOpenDialog(par) == JFileChooser.APPROVE_OPTION) {
84                         File JavaDoc file = chooser.getSelectedFile();
85                         if (file != null && file.isFile()) {
86                             FileInputStream JavaDoc fstream = new FileInputStream JavaDoc(file);
87                             ObjectInputStream JavaDoc istream = new ObjectInputStream JavaDoc(fstream);
88                             cmd = (AbstractCommand)istream.readObject();
89                             istream.close();
90                             cmd.setSpecification(spec);
91                         } else
92                             return;
93                     } else
94                         return;
95
96                     cmd.setObjectOwner((String JavaDoc) info.get(DatabaseNodeInfo.SCHEMA));
97                     String JavaDoc newtab = cmd.getObjectName();
98                     String JavaDoc msg = cmd.getCommand();
99                     LabeledTextFieldDialog dlg = new LabeledTextFieldDialog(msg); //NOI18N
100
dlg.setStringValue(newtab);
101                     boolean noResult = true;
102                     while(noResult) {
103                         if (dlg.run()) { // OK option
104
try {
105                                 if(!dlg.isEditable()) { // from file
106
newtab = dlg.getStringValue();
107                                     cmd.setObjectName(newtab);
108                                     try {
109                                         cmd.execute();
110                                         nfo.addTable(newtab);
111                                     } catch (org.netbeans.lib.ddl.DDLException exc) {
112                                         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc);
113                                         DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(exc.getMessage(), NotifyDescriptor.ERROR_MESSAGE));
114                                         continue;
115                                     } catch (org.netbeans.api.db.explorer.DatabaseException exc) {
116                                         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc);
117                                         continue;
118                                     }
119                                     noResult = false;
120                                 } else { // from editable text area
121
DataViewWindow win = new DataViewWindow(info, dlg.getEditedCommand());
122                                     if(win.executeCommand())
123                                         noResult = false;
124                                 }
125                             } catch(Exception JavaDoc exc) {
126                                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc);
127                                 String JavaDoc message = MessageFormat.format(bundle().getString("ERR_UnableToRecreateTable"), new String JavaDoc[] {exc.getMessage()}); // NOI18N
128
DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE));
129                             }
130                         } else { // CANCEL option
131
noResult = false;
132                         }
133                     }
134                 } catch(Exception JavaDoc exc) {
135                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc);
136                     String JavaDoc message = MessageFormat.format(bundle().getString("ERR_UnableToRecreateTable"), new String JavaDoc[] {exc.getMessage()}); // NOI18N
137
DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE));
138                 }
139             }
140         }, 0);
141     }
142 }
143
Popular Tags