KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > refactoring > plugins > FileDeletePlugin


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.refactoring.plugins;
20
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import org.netbeans.modules.refactoring.spi.Transaction;
24 import org.netbeans.modules.refactoring.api.Problem;
25 import org.netbeans.modules.refactoring.api.SafeDeleteRefactoring;
26 import org.netbeans.modules.refactoring.spi.BackupFacility;
27 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag;
28 import org.netbeans.modules.refactoring.spi.RefactoringPlugin;
29 import org.netbeans.modules.refactoring.spi.SimpleRefactoringElementImpl;
30 import org.openide.filesystems.FileObject;
31 import org.openide.filesystems.FileUtil;
32 import org.openide.loaders.DataObject;
33 import org.openide.loaders.DataObjectNotFoundException;
34 import org.openide.text.PositionBounds;
35
36
37 /**
38  *
39  * @author Jan Becicka
40  */

41 public class FileDeletePlugin implements RefactoringPlugin {
42     private SafeDeleteRefactoring refactoring;
43     
44     /** Creates a new instance of WhereUsedQuery */
45     public FileDeletePlugin(SafeDeleteRefactoring refactoring) {
46         this.refactoring = refactoring;
47     }
48     
49     public Problem preCheck() {
50         return null;
51     }
52     
53     public Problem prepare(RefactoringElementsBag elements) {
54         for (FileObject fo: refactoring.getRefactoringSource().lookupAll(FileObject.class)) {
55             elements.add(refactoring, new DeleteFile(fo, elements));
56         }
57         return null;
58     }
59     
60     public Problem fastCheckParameters() {
61         return null;
62     }
63         
64     public Problem checkParameters() {
65         return null;
66     }
67
68     public void cancelRequest() {
69     }
70     
71     private class DeleteFile extends SimpleRefactoringElementImpl {
72         
73         private FileObject fo;
74         private RefactoringElementsBag session;
75         public DeleteFile(FileObject fo, RefactoringElementsBag session) {
76             this.fo = fo;
77             this.session = session;
78         }
79         public String JavaDoc getText() {
80             return "Delete file " + fo.getNameExt();
81         }
82
83         public String JavaDoc getDisplayText() {
84             return getText();
85         }
86
87         public void performChange() {
88             session.registerFileChange(new Transaction() {
89                BackupFacility.Handle id;
90                 public void commit () {
91                     try {
92                         if (!fo.isValid()) {
93                             fo = FileUtil.toFileObject(new File JavaDoc(fo.getPath()));
94                         }
95                             
96                         id = BackupFacility.getDefault().backup(fo);
97                         DataObject.find(fo).delete();
98                     } catch (DataObjectNotFoundException ex) {
99                         ex.printStackTrace();
100                     } catch (IOException JavaDoc ex) {
101                         ex.printStackTrace();
102                     }
103                 }
104                 
105                 public void rollback() {
106                     try {
107                         id.restore();
108                     } catch (IOException JavaDoc ex) {
109                         ex.printStackTrace();
110                     }
111                 }
112             });
113         }
114
115         public Object JavaDoc getComposite() {
116             return fo;
117         }
118
119         public FileObject getParentFile() {
120             return fo;
121         }
122
123         public PositionBounds getPosition() {
124             return null;
125         }
126     }
127
128 }
129
Popular Tags