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.awt.Dialog;
045: import java.io.File;
046: import java.io.FileOutputStream;
047: import java.io.ObjectOutputStream;
048: import java.text.MessageFormat;
049: import java.util.Enumeration;
050:
051: import javax.swing.JButton;
052: import javax.swing.JDialog;
053: import javax.swing.JFileChooser;
054: import javax.swing.SwingUtilities;
055: import org.netbeans.api.db.explorer.DatabaseException;
056: import org.netbeans.api.progress.ProgressHandle;
057: import org.netbeans.api.progress.ProgressHandleFactory;
058: import org.netbeans.modules.db.explorer.DbUtilities;
059: import org.netbeans.modules.db.explorer.dlg.GrabTableProgressPanel;
060: import org.openide.DialogDescriptor;
061:
062: import org.openide.DialogDisplayer;
063: import org.openide.NotifyDescriptor;
064: import org.openide.filesystems.FileUtil;
065: import org.openide.nodes.Node;
066:
067: import org.netbeans.lib.ddl.impl.CreateTable;
068: import org.netbeans.lib.ddl.impl.Specification;
069: import org.netbeans.modules.db.explorer.nodes.DatabaseNode;
070: import org.netbeans.modules.db.explorer.infos.ColumnNodeInfo;
071: import org.netbeans.modules.db.explorer.infos.DatabaseNodeInfo;
072: import org.openide.util.NbBundle;
073: import org.openide.util.RequestProcessor;
074: import org.openide.util.Task;
075: import org.openide.util.TaskListener;
076: import org.openide.windows.WindowManager;
077:
078: public class GrabTableAction extends DatabaseAction {
079: static final long serialVersionUID = -7685449970256732671L;
080:
081: protected boolean enable(Node[] activatedNodes) {
082: if (activatedNodes != null && activatedNodes.length == 1)
083: return true;
084: else
085: return false;
086: }
087:
088: public void performAction(Node[] activatedNodes) {
089: Node node;
090: if (activatedNodes != null && activatedNodes.length == 1)
091: node = activatedNodes[0];
092: else
093: return;
094:
095: try {
096: DatabaseNodeInfo info = (DatabaseNodeInfo) node
097: .getCookie(DatabaseNodeInfo.class);
098: DatabaseNodeInfo nfo = info.getParent(nodename);
099: Specification spec = (Specification) nfo.getSpecification();
100: String tablename = (String) nfo.get(DatabaseNode.TABLE);
101:
102: // Get command
103:
104: CreateTable cmd = (CreateTable) spec
105: .createCommandCreateTable(tablename);
106:
107: GrabTableWorker run = new GrabTableWorker(nfo);
108: Enumeration enu = run.execute();
109:
110: // Get filename
111:
112: JFileChooser chooser = new JFileChooser();
113: FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
114: chooser.setDialogType(JFileChooser.SAVE_DIALOG);
115: chooser.setDialogTitle(bundle().getString(
116: "GrabTableFileSaveDialogTitle")); //NOI18N
117: chooser.setSelectedFile(new File(tablename + ".grab")); //NOI18N
118: chooser
119: .setFileFilter(new javax.swing.filechooser.FileFilter() {
120: public boolean accept(File f) {
121: return (f.isDirectory() || f.getName()
122: .endsWith(".grab")); //NOI18N
123: }
124:
125: public String getDescription() {
126: return bundle().getString(
127: "GrabTableFileTypeDescription"); //NOI18N
128: }
129: });
130:
131: java.awt.Component par = WindowManager.getDefault()
132: .getMainWindow();
133: boolean noResult = true;
134: File file = null;
135: while (noResult) {
136: if (chooser.showSaveDialog(par) == JFileChooser.APPROVE_OPTION) {
137: file = chooser.getSelectedFile();
138: if (file != null) {
139: if (file.exists()) {
140: Object yesOption = new JButton(bundle()
141: .getString("Yes"));
142: Object noOption = new JButton(bundle()
143: .getString("No"));
144: Object result = DialogDisplayer
145: .getDefault()
146: .notify(
147: new NotifyDescriptor(
148: MessageFormat
149: .format(
150: bundle()
151: .getString(
152: "MSG_ReplaceFileOrNot"), // NOI18N
153: new String[] { file
154: .getName() }), //question
155: bundle()
156: .getString(
157: "GrabTableFileSaveDialogTitle"), // title
158: NotifyDescriptor.YES_NO_OPTION, // optionType
159: NotifyDescriptor.QUESTION_MESSAGE, // messageType
160:
161: new Object[] {
162: yesOption,
163: noOption }, // options
164: yesOption // initialValue
165: ));
166: if (result.equals(yesOption)) {
167: // the file can be replaced
168: noResult = false;
169: }
170: } else
171: noResult = false;
172: }
173: } else
174: return;
175: }
176:
177: new GrabTableHelper().execute(spec, tablename, enu, file);
178:
179: } catch (Exception exc) {
180: DbUtilities.reportError(bundle().getString(
181: "ERR_UnableToGrabTable"), exc.getMessage()); // NOI18N
182: }
183: }
184:
185: private static final class GrabTableWorker {
186:
187: private DatabaseNodeInfo nfo;
188: private Task task;
189: private Dialog dialog;
190: private ProgressHandle progressHandle;
191: //private boolean finished;
192:
193: private Enumeration enumeration;
194: private DatabaseException exception;
195:
196: public GrabTableWorker(DatabaseNodeInfo nfo) {
197: this .nfo = nfo;
198: }
199:
200: public Enumeration execute() throws DatabaseException {
201: progressHandle = ProgressHandleFactory.createHandle(null);
202: GrabTableProgressPanel progressPanel = new GrabTableProgressPanel();
203: progressPanel.setProgressComponent(ProgressHandleFactory
204: .createProgressComponent(progressHandle));
205: String dialogTitle = NbBundle.getBundle(
206: "org.netbeans.modules.db.resources.Bundle")
207: .getString("GrabTableProgressDialogTitle"); // NOI18N
208: DialogDescriptor desc = new DialogDescriptor(progressPanel,
209: dialogTitle, true, new Object[0],
210: DialogDescriptor.NO_OPTION,
211: DialogDescriptor.DEFAULT_ALIGN, null, null);
212: dialog = DialogDisplayer.getDefault().createDialog(desc);
213: dialog.setResizable(false);
214: if (dialog instanceof JDialog) {
215: ((JDialog) dialog)
216: .setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
217: }
218: progressHandle.start();
219:
220: task = RequestProcessor.getDefault().post(new Runnable() {
221: public void run() {
222: try {
223: enumeration = nfo.getChildren().elements();
224: } catch (DatabaseException e) {
225: exception = e;
226: }
227: }
228: });
229:
230: task.addTaskListener(new TaskListener() {
231: public void taskFinished(Task t) {
232: SwingUtilities.invokeLater(new Runnable() {
233: public void run() {
234: dialog.setVisible(false);
235: }
236: });
237: }
238: });
239:
240: if (!task.isFinished()) {
241: dialog.setVisible(true);
242: }
243: dialog.dispose();
244: progressHandle.finish();
245: if (exception != null) {
246: throw exception;
247: }
248: return enumeration;
249: }
250: }
251: }
|