KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.Dialog JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.ObjectOutputStream JavaDoc;
26 import java.lang.reflect.InvocationTargetException JavaDoc;
27 import java.text.MessageFormat JavaDoc;
28 import java.util.Enumeration JavaDoc;
29
30 import javax.swing.JButton JavaDoc;
31 import javax.swing.JDialog JavaDoc;
32 import javax.swing.JFileChooser JavaDoc;
33 import javax.swing.SwingUtilities JavaDoc;
34 import org.netbeans.api.db.explorer.DatabaseException;
35 import org.netbeans.api.progress.ProgressHandle;
36 import org.netbeans.api.progress.ProgressHandleFactory;
37 import org.netbeans.modules.db.explorer.dlg.GrabTableProgressPanel;
38 import org.openide.DialogDescriptor;
39
40 import org.openide.DialogDisplayer;
41 import org.openide.ErrorManager;
42 import org.openide.NotifyDescriptor;
43 import org.openide.filesystems.FileUtil;
44 import org.openide.nodes.Node;
45
46 import org.netbeans.lib.ddl.impl.CreateTable;
47 import org.netbeans.lib.ddl.impl.Specification;
48 import org.netbeans.modules.db.explorer.nodes.DatabaseNode;
49 import org.netbeans.modules.db.explorer.infos.ColumnNodeInfo;
50 import org.netbeans.modules.db.explorer.infos.DatabaseNodeInfo;
51 import org.openide.util.NbBundle;
52 import org.openide.util.RequestProcessor;
53 import org.openide.util.Task;
54 import org.openide.util.TaskListener;
55 import org.openide.windows.WindowManager;
56
57 public class GrabTableAction extends DatabaseAction {
58     static final long serialVersionUID =-7685449970256732671L;
59     
60     protected boolean enable(Node[] activatedNodes) {
61         if (activatedNodes != null && activatedNodes.length == 1)
62             return true;
63         else
64             return false;
65     }
66
67     public void performAction (Node[] activatedNodes)
68     {
69         Node node;
70         if (activatedNodes != null && activatedNodes.length == 1)
71             node = activatedNodes[0];
72         else
73             return;
74         
75         try {
76             DatabaseNodeInfo info = (DatabaseNodeInfo)node.getCookie(DatabaseNodeInfo.class);
77             DatabaseNodeInfo nfo = info.getParent(nodename);
78             Specification spec = (Specification)nfo.getSpecification();
79             String JavaDoc tablename = (String JavaDoc)nfo.get(DatabaseNode.TABLE);
80
81             // Get command
82

83             CreateTable cmd = (CreateTable)spec.createCommandCreateTable(tablename);
84             
85             GrabTableWorker run = new GrabTableWorker(nfo);
86             Enumeration JavaDoc enu = run.execute();
87             
88             while (enu.hasMoreElements()) {
89                 Object JavaDoc element = enu.nextElement();
90                 if (element instanceof ColumnNodeInfo) {
91                     cmd.getColumns().add(((ColumnNodeInfo)element).getColumnSpecification());
92                 }
93             }
94
95             // System.out.println(cmd.getCommand());
96

97             // Get filename
98

99             JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
100             FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
101             chooser.setDialogType(JFileChooser.SAVE_DIALOG);
102             chooser.setDialogTitle(bundle().getString("GrabTableFileSaveDialogTitle")); //NOI18N
103
chooser.setSelectedFile(new File JavaDoc(tablename+".grab")); //NOI18N
104
chooser.setFileFilter(new javax.swing.filechooser.FileFilter JavaDoc() {
105                                       public boolean accept(File JavaDoc f) {
106                                           return (f.isDirectory() || f.getName().endsWith(".grab")); //NOI18N
107
}
108                                       public String JavaDoc getDescription() {
109                                           return bundle().getString("GrabTableFileTypeDescription"); //NOI18N
110
}
111                                   });
112
113             java.awt.Component JavaDoc par = WindowManager.getDefault().getMainWindow();
114             boolean noResult = true;
115             File JavaDoc file = null;
116             while(noResult) {
117                 if (chooser.showSaveDialog(par) == JFileChooser.APPROVE_OPTION) {
118                     file = chooser.getSelectedFile();
119                     if (file != null) {
120                         if(file.exists()) {
121                             Object JavaDoc yesOption = new JButton JavaDoc(bundle().getString("Yes"));
122                             Object JavaDoc noOption = new JButton JavaDoc (bundle().getString("No"));
123                             Object JavaDoc result = DialogDisplayer.getDefault ().notify (new NotifyDescriptor
124                                             (MessageFormat.format(bundle().getString("MSG_ReplaceFileOrNot"), // NOI18N
125
new String JavaDoc[] {file.getName()}), //question
126
bundle().getString("GrabTableFileSaveDialogTitle"), // title
127
NotifyDescriptor.YES_NO_OPTION, // optionType
128
NotifyDescriptor.QUESTION_MESSAGE, // messageType
129

130                                              new Object JavaDoc[] { yesOption, noOption }, // options
131
yesOption // initialValue
132
));
133                             if (result.equals(yesOption)) {
134                                 // the file can be replaced
135
noResult = false;
136                             }
137                         } else noResult = false;
138                     }
139                 } else return;
140             }
141             FileOutputStream JavaDoc fstream = new FileOutputStream JavaDoc(file);
142             ObjectOutputStream JavaDoc ostream = new ObjectOutputStream JavaDoc(fstream);
143             cmd.setSpecification(null);
144             ostream.writeObject(cmd);
145             ostream.flush();
146             ostream.close();
147
148         } catch(Exception JavaDoc exc) {
149             String JavaDoc message = MessageFormat.format(bundle().getString("ERR_UnableToGrabTable"), new String JavaDoc[] {exc.getMessage()}); // NOI18N
150
DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE));
151         }
152     }
153     
154     private static final class GrabTableWorker {
155
156         private DatabaseNodeInfo nfo;
157         private Task task;
158         private Dialog JavaDoc dialog;
159         private ProgressHandle progressHandle;
160         //private boolean finished;
161

162         private Enumeration JavaDoc enumeration;
163         private DatabaseException exception;
164         
165         public GrabTableWorker(DatabaseNodeInfo nfo) {
166             this.nfo = nfo;
167         }
168         
169         public Enumeration JavaDoc execute() throws DatabaseException {
170             progressHandle = ProgressHandleFactory.createHandle(null);
171             GrabTableProgressPanel progressPanel = new GrabTableProgressPanel();
172             progressPanel.setProgressComponent(ProgressHandleFactory.createProgressComponent(progressHandle));
173             String JavaDoc dialogTitle = NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("GrabTableProgressDialogTitle"); // NOI18N
174
DialogDescriptor desc = new DialogDescriptor(progressPanel, dialogTitle, true, new Object JavaDoc[0], DialogDescriptor.NO_OPTION, DialogDescriptor.DEFAULT_ALIGN, null, null);
175             dialog = DialogDisplayer.getDefault().createDialog(desc);
176             dialog.setResizable(false);
177             if (dialog instanceof JDialog JavaDoc) {
178                 ((JDialog JavaDoc)dialog).setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
179             }
180             progressHandle.start();
181             
182             task = RequestProcessor.getDefault().post(new Runnable JavaDoc() {
183                 public void run() {
184                     try {
185                         enumeration = nfo.getChildren().elements();
186                     } catch (DatabaseException e) {
187                         exception = e;
188                     }
189                 }
190             });
191             
192             task.addTaskListener(new TaskListener() {
193                 public void taskFinished(Task t) {
194                     SwingUtilities.invokeLater(new Runnable JavaDoc() {
195                         public void run() {
196                             dialog.setVisible(false);
197                         }
198                     });
199                 }
200             });
201             
202             if (!task.isFinished()) {
203                 dialog.setVisible(true);
204             }
205             dialog.dispose();
206             progressHandle.finish();
207             if (exception != null) {
208                 throw exception;
209             }
210             return enumeration;
211         }
212     }
213 }
214
Popular Tags