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.netbeans.modules.refactoring.plugins;
042:
043: import java.io.IOException;
044: import java.net.URL;
045: import org.netbeans.modules.refactoring.api.Problem;
046: import org.netbeans.modules.refactoring.api.SafeDeleteRefactoring;
047: import org.netbeans.modules.refactoring.spi.BackupFacility;
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.filesystems.FileObject;
052: import org.openide.filesystems.FileStateInvalidException;
053: import org.openide.filesystems.URLMapper;
054: import org.openide.loaders.DataObject;
055: import org.openide.loaders.DataObjectNotFoundException;
056: import org.openide.text.PositionBounds;
057: import org.openide.util.Exceptions;
058: import org.openide.util.Lookup;
059: import org.openide.util.NbBundle;
060:
061: /**
062: *
063: * @author Jan Becicka
064: */
065: public class FileDeletePlugin implements RefactoringPlugin {
066: private SafeDeleteRefactoring refactoring;
067:
068: /** Creates a new instance of WhereUsedQuery */
069: public FileDeletePlugin(SafeDeleteRefactoring refactoring) {
070: this .refactoring = refactoring;
071: }
072:
073: public Problem preCheck() {
074: return null;
075: }
076:
077: public Problem prepare(RefactoringElementsBag elements) {
078: for (FileObject fo : refactoring.getRefactoringSource()
079: .lookupAll(FileObject.class)) {
080: elements.addFileChange(refactoring, new DeleteFile(fo,
081: elements));
082: }
083: return null;
084: }
085:
086: public Problem fastCheckParameters() {
087: return null;
088: }
089:
090: public Problem checkParameters() {
091: return null;
092: }
093:
094: public void cancelRequest() {
095: }
096:
097: private class DeleteFile extends
098: SimpleRefactoringElementImplementation {
099:
100: private final URL res;
101: private String filename;
102: private RefactoringElementsBag session;
103:
104: public DeleteFile(FileObject fo, RefactoringElementsBag session) {
105: try {
106: this .res = fo.getURL();
107: } catch (FileStateInvalidException ex) {
108: throw new IllegalStateException(ex);
109: }
110: this .filename = fo.getNameExt();
111: this .session = session;
112: }
113:
114: public String getText() {
115: return NbBundle.getMessage(FileDeletePlugin.class,
116: "TXT_DeleteFile", filename);
117: }
118:
119: public String getDisplayText() {
120: return getText();
121: }
122:
123: BackupFacility.Handle id;
124:
125: public void performChange() {
126: try {
127: FileObject fo = URLMapper.findFileObject(res);
128: if (fo == null) {
129: throw new IOException(res.toString());
130: }
131: id = BackupFacility.getDefault().backup(fo);
132: DataObject.find(fo).delete();
133: } catch (DataObjectNotFoundException ex) {
134: Exceptions.printStackTrace(ex);
135: } catch (IOException ex) {
136: Exceptions.printStackTrace(ex);
137: }
138: }
139:
140: public void undoChange() {
141: try {
142: id.restore();
143: } catch (IOException ex) {
144: ex.printStackTrace();
145: }
146: }
147:
148: public Lookup getLookup() {
149: return Lookup.EMPTY;
150: }
151:
152: public FileObject getParentFile() {
153: return URLMapper.findFileObject(res);
154: }
155:
156: public PositionBounds getPosition() {
157: return null;
158: }
159: }
160:
161: }
|