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-2007 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: package org.openide.actions;
042:
043: import java.awt.event.ActionEvent;
044: import java.beans.PropertyChangeEvent;
045: import java.beans.PropertyChangeListener;
046: import java.io.File;
047: import java.io.IOException;
048: import java.util.Collection;
049: import java.util.logging.Level;
050: import java.util.logging.Logger;
051: import javax.swing.AbstractAction;
052: import javax.swing.Action;
053: import javax.swing.JFileChooser;
054: import javax.swing.SwingUtilities;
055: import org.openide.DialogDisplayer;
056: import org.openide.NotifyDescriptor;
057: import org.openide.filesystems.FileObject;
058: import org.openide.filesystems.FileUtil;
059: import org.openide.loaders.SaveAsCapable;
060: import org.openide.loaders.DataObject;
061: import org.openide.util.ContextAwareAction;
062: import org.openide.util.Exceptions;
063: import org.openide.util.Lookup;
064: import org.openide.util.LookupEvent;
065: import org.openide.util.LookupListener;
066: import org.openide.util.NbBundle;
067: import org.openide.util.Utilities;
068: import org.openide.windows.TopComponent;
069: import org.openide.windows.WindowManager;
070:
071: /**
072: * Action to save document under a different file name and/or extension.
073: * The action is enabled for editor windows only.
074: *
075: * @since 6.3
076: * @author S. Aubrecht
077: */
078: final class SaveAsAction extends AbstractAction implements
079: ContextAwareAction, LookupListener, PropertyChangeListener {
080:
081: private Lookup context;
082: private Lookup.Result<SaveAsCapable> lkpInfo;
083: private boolean isEditorWindowActivated;
084:
085: private SaveAsAction() {
086: this (Utilities.actionsGlobalContext());
087: TopComponent.getRegistry().addPropertyChangeListener(this );
088: }
089:
090: private SaveAsAction(Lookup context) {
091: super (NbBundle.getMessage(DataObject.class, "CTL_SaveAsAction")); //NOI18N
092: this .context = context;
093: putValue("noIconInMenu", Boolean.TRUE); //NOI18N
094: }
095:
096: /**
097: * Method is called from XML layers to create action instance for the main menu/toolbar.
098: * @return Global instance for menu/toolbar
099: */
100: public static ContextAwareAction create() {
101: return new SaveAsAction();
102: }
103:
104: void init() {
105: assert SwingUtilities.isEventDispatchThread() : "this shall be called just from AWT thread";
106:
107: if (lkpInfo != null) {
108: return;
109: }
110:
111: //The thing we want to listen for the presence or absence of
112: //on the global selection
113: Lookup.Template<SaveAsCapable> tpl = new Lookup.Template<SaveAsCapable>(
114: SaveAsCapable.class);
115: lkpInfo = context.lookup(tpl);
116: lkpInfo.addLookupListener(this );
117: propertyChange(null);
118: }
119:
120: public boolean isEnabled() {
121: init();
122: return super .isEnabled();
123: }
124:
125: public void actionPerformed(ActionEvent e) {
126: init();
127: Collection<? extends SaveAsCapable> inst = lkpInfo
128: .allInstances();
129: if (inst.size() > 0) {
130: SaveAsCapable saveAs = inst.iterator().next();
131: File newFile = getNewFileName();
132: if (null != newFile) {
133: //create target folder if necessary
134: FileObject newFolder = null;
135: try {
136: File targetFolder = newFile.getParentFile();
137: if (null == targetFolder)
138: throw new IOException(newFile.getAbsolutePath());
139: newFolder = FileUtil.createFolder(targetFolder);
140: } catch (IOException ioE) {
141: NotifyDescriptor error = new NotifyDescriptor(
142: NbBundle.getMessage(DataObject.class,
143: "MSG_CannotCreateTargetFolder"), //NOI18N
144: NbBundle.getMessage(DataObject.class,
145: "LBL_SaveAsTitle"), //NOI18N
146: NotifyDescriptor.DEFAULT_OPTION,
147: NotifyDescriptor.ERROR_MESSAGE,
148: new Object[] { NotifyDescriptor.OK_OPTION },
149: NotifyDescriptor.OK_OPTION);
150: DialogDisplayer.getDefault().notify(error);
151: return;
152: }
153:
154: try {
155: saveAs.saveAs(newFolder, newFile.getName());
156: } catch (IOException ioE) {
157: Exceptions.attachLocalizedMessage(ioE, NbBundle
158: .getMessage(DataObject.class,
159: "MSG_SaveAsFailed")); //NOI18N
160: Logger.getLogger(getClass().getName()).log(
161: Level.WARNING, null, ioE);
162: }
163: }
164: }
165: }
166:
167: public void resultChanged(LookupEvent ev) {
168: setEnabled(null != lkpInfo && lkpInfo.allItems().size() != 0
169: && isEditorWindowActivated);
170: }
171:
172: /**
173: * Show file 'save as' dialog window to ask user for a new file name.
174: * @return File selected by the user or null if no file was selected.
175: */
176: private File getNewFileName() {
177: File newFile = null;
178: FileObject currentFileObject = getCurrentFileObject();
179: if (null != currentFileObject)
180: newFile = FileUtil.toFile(currentFileObject);
181:
182: JFileChooser chooser = new JFileChooser();
183: chooser.setDialogTitle(NbBundle.getMessage(DataObject.class,
184: "LBL_SaveAsTitle")); //NOI18N
185: chooser.setMultiSelectionEnabled(false);
186: if (null != newFile) {
187: chooser.setSelectedFile(newFile);
188: FileUtil.preventFileChooserSymlinkTraversal(chooser,
189: newFile.getParentFile());
190: }
191: File origFile = newFile;
192: if (JFileChooser.APPROVE_OPTION != chooser
193: .showSaveDialog(WindowManager.getDefault()
194: .getMainWindow())) {
195: return null;
196: }
197: newFile = chooser.getSelectedFile();
198: if (null == newFile || newFile.equals(origFile))
199: return null;
200:
201: return newFile;
202: }
203:
204: private FileObject getCurrentFileObject() {
205: TopComponent tc = TopComponent.getRegistry().getActivated();
206: if (null != tc) {
207: DataObject dob = tc.getLookup().lookup(DataObject.class);
208: if (null != dob)
209: return dob.getPrimaryFile();
210: }
211: return null;
212: }
213:
214: public Action createContextAwareInstance(Lookup actionContext) {
215: return new SaveAsAction(actionContext);
216: }
217:
218: public void propertyChange(PropertyChangeEvent arg0) {
219: TopComponent tc = TopComponent.getRegistry().getActivated();
220:
221: isEditorWindowActivated = null != tc
222: && WindowManager.getDefault().isEditorTopComponent(tc);
223:
224: resultChanged(null);
225: }
226: }
|