001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.db.explorer.actions;
043:
044: import java.io.File;
045: import java.io.FileInputStream;
046: import java.io.ObjectInputStream;
047: import java.text.MessageFormat;
048: import java.util.logging.Level;
049: import java.util.logging.Logger;
050:
051: import javax.swing.JFileChooser;
052: import org.netbeans.modules.db.explorer.DbUtilities;
053:
054: import org.openide.DialogDisplayer;
055: import org.openide.NotifyDescriptor;
056: import org.openide.filesystems.FileUtil;
057: import org.openide.nodes.Node;
058: import org.openide.util.RequestProcessor;
059: import org.openide.windows.WindowManager;
060:
061: import org.netbeans.lib.ddl.impl.AbstractCommand;
062: import org.netbeans.lib.ddl.impl.Specification;
063:
064: import org.netbeans.modules.db.explorer.infos.DatabaseNodeInfo;
065: import org.netbeans.modules.db.explorer.infos.TableListNodeInfo;
066: import org.netbeans.modules.db.explorer.dlg.LabeledTextFieldDialog;
067: import org.netbeans.modules.db.explorer.dataview.DataViewWindow;
068:
069: public class RecreateTableAction extends DatabaseAction {
070: static final long serialVersionUID = 6992569917995229492L;
071:
072: protected boolean enable(Node[] activatedNodes) {
073: return (activatedNodes != null && activatedNodes.length == 1);
074: }
075:
076: public void performAction(Node[] activatedNodes) {
077: Node node;
078: if (activatedNodes != null && activatedNodes.length == 1)
079: node = activatedNodes[0];
080: else
081: return;
082:
083: final DatabaseNodeInfo info = (DatabaseNodeInfo) node
084: .getCookie(DatabaseNodeInfo.class);
085: final java.awt.Component par = WindowManager.getDefault()
086: .getMainWindow();
087: RequestProcessor.getDefault().post(new Runnable() {
088: public void run() {
089: try {
090: TableListNodeInfo nfo = (TableListNodeInfo) info
091: .getParent(nodename);
092: Specification spec = (Specification) nfo
093: .getSpecification();
094: AbstractCommand cmd;
095:
096: // Get filename
097: JFileChooser chooser = new JFileChooser();
098: FileUtil.preventFileChooserSymlinkTraversal(
099: chooser, null);
100: chooser.setDialogType(JFileChooser.OPEN_DIALOG);
101: chooser.setDialogTitle(bundle().getString(
102: "RecreateTableFileOpenDialogTitle")); //NOI18N
103: chooser
104: .setFileFilter(new javax.swing.filechooser.FileFilter() {
105: public boolean accept(File f) {
106: return (f.isDirectory() || f
107: .getName()
108: .endsWith(".grab")); //NOI18N
109: }
110:
111: public String getDescription() {
112: return bundle()
113: .getString(
114: "GrabTableFileTypeDescription"); //NOI18N
115: }
116: });
117:
118: if (chooser.showOpenDialog(par) == JFileChooser.APPROVE_OPTION) {
119: File file = chooser.getSelectedFile();
120: if (file != null && file.isFile()) {
121: FileInputStream fstream = new FileInputStream(
122: file);
123: ObjectInputStream istream = new ObjectInputStream(
124: fstream);
125: cmd = (AbstractCommand) istream
126: .readObject();
127: istream.close();
128: cmd.setSpecification(spec);
129: } else
130: return;
131: } else
132: return;
133:
134: cmd.setObjectOwner((String) info
135: .get(DatabaseNodeInfo.SCHEMA));
136: String newtab = cmd.getObjectName();
137: String msg = cmd.getCommand();
138: LabeledTextFieldDialog dlg = new LabeledTextFieldDialog(
139: msg); //NOI18N
140: dlg.setStringValue(newtab);
141: boolean noResult = true;
142: while (noResult) {
143: if (dlg.run()) { // OK option
144: try {
145: if (!dlg.isEditable()) { // from file
146: newtab = dlg.getStringValue();
147: cmd.setObjectName(newtab);
148: try {
149: cmd.execute();
150: nfo.addTable(newtab);
151: } catch (org.netbeans.lib.ddl.DDLException exc) {
152: Logger.getLogger("global").log(
153: Level.INFO, null, exc);
154: DialogDisplayer
155: .getDefault()
156: .notify(
157: new NotifyDescriptor.Message(
158: exc
159: .getMessage(),
160: NotifyDescriptor.ERROR_MESSAGE));
161: continue;
162: } catch (org.netbeans.api.db.explorer.DatabaseException exc) {
163: Logger.getLogger("global").log(
164: Level.INFO, null, exc);
165: continue;
166: }
167: noResult = false;
168: } else { // from editable text area
169: DataViewWindow win = new DataViewWindow(
170: info, dlg
171: .getEditedCommand());
172: if (win.executeCommand())
173: noResult = false;
174: }
175: } catch (Exception exc) {
176: Logger.getLogger("global").log(
177: Level.INFO, null, exc);
178: DbUtilities
179: .reportError(
180: bundle()
181: .getString(
182: "ERR_UnableToRecreateTable"),
183: exc.getMessage()); //NOI18N
184: }
185: } else { // CANCEL option
186: noResult = false;
187: }
188: }
189: } catch (Exception exc) {
190: Logger.getLogger("global").log(Level.INFO, null,
191: exc);
192: DbUtilities.reportError(bundle().getString(
193: "ERR_UnableToRecreateTable"), exc
194: .getMessage()); //NOI18N
195: }
196: }
197: }, 0);
198: }
199: }
|