1 19 20 package org.netbeans.modules.db.explorer.actions; 21 22 import java.awt.Dialog ; 23 import java.io.File ; 24 import java.io.FileOutputStream ; 25 import java.io.ObjectOutputStream ; 26 import java.lang.reflect.InvocationTargetException ; 27 import java.text.MessageFormat ; 28 import java.util.Enumeration ; 29 30 import javax.swing.JButton ; 31 import javax.swing.JDialog ; 32 import javax.swing.JFileChooser ; 33 import javax.swing.SwingUtilities ; 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 tablename = (String )nfo.get(DatabaseNode.TABLE); 80 81 83 CreateTable cmd = (CreateTable)spec.createCommandCreateTable(tablename); 84 85 GrabTableWorker run = new GrabTableWorker(nfo); 86 Enumeration enu = run.execute(); 87 88 while (enu.hasMoreElements()) { 89 Object element = enu.nextElement(); 90 if (element instanceof ColumnNodeInfo) { 91 cmd.getColumns().add(((ColumnNodeInfo)element).getColumnSpecification()); 92 } 93 } 94 95 97 99 JFileChooser chooser = new JFileChooser (); 100 FileUtil.preventFileChooserSymlinkTraversal(chooser, null); 101 chooser.setDialogType(JFileChooser.SAVE_DIALOG); 102 chooser.setDialogTitle(bundle().getString("GrabTableFileSaveDialogTitle")); chooser.setSelectedFile(new File (tablename+".grab")); chooser.setFileFilter(new javax.swing.filechooser.FileFilter () { 105 public boolean accept(File f) { 106 return (f.isDirectory() || f.getName().endsWith(".grab")); } 108 public String getDescription() { 109 return bundle().getString("GrabTableFileTypeDescription"); } 111 }); 112 113 java.awt.Component par = WindowManager.getDefault().getMainWindow(); 114 boolean noResult = true; 115 File 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 yesOption = new JButton (bundle().getString("Yes")); 122 Object noOption = new JButton (bundle().getString("No")); 123 Object result = DialogDisplayer.getDefault ().notify (new NotifyDescriptor 124 (MessageFormat.format(bundle().getString("MSG_ReplaceFileOrNot"), new String [] {file.getName()}), bundle().getString("GrabTableFileSaveDialogTitle"), NotifyDescriptor.YES_NO_OPTION, NotifyDescriptor.QUESTION_MESSAGE, 130 new Object [] { yesOption, noOption }, yesOption )); 133 if (result.equals(yesOption)) { 134 noResult = false; 136 } 137 } else noResult = false; 138 } 139 } else return; 140 } 141 FileOutputStream fstream = new FileOutputStream (file); 142 ObjectOutputStream ostream = new ObjectOutputStream (fstream); 143 cmd.setSpecification(null); 144 ostream.writeObject(cmd); 145 ostream.flush(); 146 ostream.close(); 147 148 } catch(Exception exc) { 149 String message = MessageFormat.format(bundle().getString("ERR_UnableToGrabTable"), new String [] {exc.getMessage()}); 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 dialog; 159 private ProgressHandle progressHandle; 160 162 private Enumeration enumeration; 163 private DatabaseException exception; 164 165 public GrabTableWorker(DatabaseNodeInfo nfo) { 166 this.nfo = nfo; 167 } 168 169 public Enumeration execute() throws DatabaseException { 170 progressHandle = ProgressHandleFactory.createHandle(null); 171 GrabTableProgressPanel progressPanel = new GrabTableProgressPanel(); 172 progressPanel.setProgressComponent(ProgressHandleFactory.createProgressComponent(progressHandle)); 173 String dialogTitle = NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("GrabTableProgressDialogTitle"); DialogDescriptor desc = new DialogDescriptor(progressPanel, dialogTitle, true, new Object [0], DialogDescriptor.NO_OPTION, DialogDescriptor.DEFAULT_ALIGN, null, null); 175 dialog = DialogDisplayer.getDefault().createDialog(desc); 176 dialog.setResizable(false); 177 if (dialog instanceof JDialog ) { 178 ((JDialog )dialog).setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); 179 } 180 progressHandle.start(); 181 182 task = RequestProcessor.getDefault().post(new Runnable () { 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 () { 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 |