1 19 20 package org.netbeans.modules.db.explorer.actions; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.ObjectInputStream ; 25 import java.text.MessageFormat ; 26 27 import javax.swing.JFileChooser ; 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 par = WindowManager.getDefault().getMainWindow(); 61 RequestProcessor.getDefault().post(new Runnable () { 62 public void run () { 63 try { 64 TableListNodeInfo nfo = (TableListNodeInfo) info.getParent(nodename); 65 Specification spec = (Specification) nfo.getSpecification(); 66 AbstractCommand cmd; 67 68 JFileChooser chooser = new JFileChooser (); 70 FileUtil.preventFileChooserSymlinkTraversal(chooser, null); 71 chooser.setDialogType(JFileChooser.OPEN_DIALOG); 72 chooser.setDialogTitle(bundle().getString("RecreateTableFileOpenDialogTitle")); chooser.setFileFilter(new javax.swing.filechooser.FileFilter () { 74 public boolean accept(File f) { 75 return (f.isDirectory() || f.getName().endsWith(".grab")); } 77 78 public String getDescription() { 79 return bundle().getString("GrabTableFileTypeDescription"); } 81 }); 82 83 if (chooser.showOpenDialog(par) == JFileChooser.APPROVE_OPTION) { 84 File file = chooser.getSelectedFile(); 85 if (file != null && file.isFile()) { 86 FileInputStream fstream = new FileInputStream (file); 87 ObjectInputStream istream = new ObjectInputStream (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 ) info.get(DatabaseNodeInfo.SCHEMA)); 97 String newtab = cmd.getObjectName(); 98 String msg = cmd.getCommand(); 99 LabeledTextFieldDialog dlg = new LabeledTextFieldDialog(msg); dlg.setStringValue(newtab); 101 boolean noResult = true; 102 while(noResult) { 103 if (dlg.run()) { try { 105 if(!dlg.isEditable()) { 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 { DataViewWindow win = new DataViewWindow(info, dlg.getEditedCommand()); 122 if(win.executeCommand()) 123 noResult = false; 124 } 125 } catch(Exception exc) { 126 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc); 127 String message = MessageFormat.format(bundle().getString("ERR_UnableToRecreateTable"), new String [] {exc.getMessage()}); DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE)); 129 } 130 } else { noResult = false; 132 } 133 } 134 } catch(Exception exc) { 135 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc); 136 String message = MessageFormat.format(bundle().getString("ERR_UnableToRecreateTable"), new String [] {exc.getMessage()}); DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE)); 138 } 139 } 140 }, 0); 141 } 142 } 143 | Popular Tags |