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: package org.netbeans.modules.refactoring.plugins;
042:
043: import java.io.IOException;
044: import java.net.URL;
045: import org.netbeans.modules.refactoring.api.SingleCopyRefactoring;
046: import org.netbeans.modules.refactoring.api.Problem;
047: import org.netbeans.modules.refactoring.api.RefactoringSession;
048: import org.netbeans.modules.refactoring.spi.RefactoringElementsBag;
049: import org.netbeans.modules.refactoring.spi.RefactoringPlugin;
050: import org.netbeans.modules.refactoring.spi.SimpleRefactoringElementImplementation;
051: import org.openide.ErrorManager;
052: import org.openide.filesystems.FileObject;
053: import org.openide.loaders.DataFolder;
054: import org.openide.loaders.DataObject;
055: import org.openide.text.PositionBounds;
056: import org.openide.util.Lookup;
057: import org.openide.util.NbBundle;
058:
059: /**
060: *
061: * @author Jan Becicka
062: */
063: public class FileCopyPlugin implements RefactoringPlugin {
064: private SingleCopyRefactoring refactoring;
065:
066: /** Creates a new instance of WhereUsedQuery */
067: public FileCopyPlugin(SingleCopyRefactoring refactoring) {
068: this .refactoring = refactoring;
069: }
070:
071: public Problem preCheck() {
072: return null;
073: }
074:
075: public Problem prepare(RefactoringElementsBag elements) {
076: elements.add(refactoring, new CopyFile(refactoring
077: .getRefactoringSource().lookup(FileObject.class),
078: elements.getSession()));
079: return null;
080: }
081:
082: public Problem fastCheckParameters() {
083: return null;
084: }
085:
086: public Problem checkParameters() {
087: return null;
088: }
089:
090: public void cancelRequest() {
091: }
092:
093: private class CopyFile extends
094: SimpleRefactoringElementImplementation {
095:
096: private FileObject fo;
097: private RefactoringSession session;
098: private DataObject newOne;
099:
100: public CopyFile(FileObject fo, RefactoringSession session) {
101: this .fo = fo;
102: this .session = session;
103: }
104:
105: public String getText() {
106: return NbBundle.getMessage(FileCopyPlugin.class,
107: "TXT_CopyFile", fo.getNameExt());
108: }
109:
110: public String getDisplayText() {
111: return getText();
112: }
113:
114: public void performChange() {
115: try {
116: FileObject fo = FileHandlingFactory
117: .getOrCreateFolder(refactoring.getTarget()
118: .lookup(URL.class));
119: FileObject source = refactoring.getRefactoringSource()
120: .lookup(FileObject.class);
121: DataObject dob = DataObject.find(source);
122: newOne = dob.copy(DataFolder.findFolder(fo));
123: newOne.rename(refactoring.getNewName());
124: refactoring.getContext().add(newOne.getPrimaryFile());
125: } catch (Exception ioe) {
126: ErrorManager.getDefault().notify(ioe);
127: }
128: }
129:
130: @Override
131: public void undoChange() {
132: try {
133: newOne.delete();
134: } catch (IOException ex) {
135: ErrorManager.getDefault().notify(ex);
136: }
137: }
138:
139: public Lookup getLookup() {
140: return Lookup.EMPTY;
141: }
142:
143: public FileObject getParentFile() {
144: return fo;
145: }
146:
147: public PositionBounds getPosition() {
148: return null;
149: }
150: }
151: }
|