KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > refactoring > spi > RefactoringElementsBag


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.refactoring.spi;
21
22 import java.io.IOException JavaDoc;
23 import java.util.*;
24 import javax.swing.text.Position JavaDoc;
25 import org.netbeans.api.editor.guards.GuardedSection;
26 import org.netbeans.api.editor.guards.GuardedSectionManager;
27 import org.netbeans.modules.refactoring.api.impl.APIAccessor;
28 import org.netbeans.modules.refactoring.api.impl.SPIAccessor;
29 import org.netbeans.modules.refactoring.api.*;
30 import org.openide.cookies.EditorCookie;
31 import org.openide.filesystems.FileObject;
32 import org.openide.loaders.DataObject;
33 import org.openide.loaders.DataObjectNotFoundException;
34
35 /**
36  * Container holding RefactoringElements
37  * @author Jan Becicka
38  */

39 public final class RefactoringElementsBag {
40     ArrayList<Transaction> commits;
41     ArrayList<Transaction> fileChanges;
42
43     static {
44         SPIAccessor.DEFAULT = new AccessorImpl();
45     }
46     
47     private final List<RefactoringElementImplementation> delegate;
48     private final RefactoringSession session;
49     private Collection<FileObject> readOnlyFiles = new HashSet();
50     
51     /**
52      * Creates an instance of RefactoringElementsBag
53      */

54     RefactoringElementsBag(RefactoringSession session, List<RefactoringElementImplementation> delegate) {
55         this.session = session;
56         this.delegate = delegate;
57         this.commits = new ArrayList();
58         this.fileChanges = new ArrayList();
59     }
60     
61     /**
62      * Adds RefactoringElementImplementation to this bag.
63      * If RefactoringElementImplementation is in read-only file - status of this element is
64      * changes to RefactoringElement.READ_ONLY
65      * If RefactoringElementImplementation is in guarded block, all registered GuardedBlockHandler
66      * are asked, if they can replace given RefactoringElementImplementation by it's own
67      * RefactoringElementImplementation. If there is no suitable replacement found,
68      * given element is added and it's status is set to RefactringElement.GUARDED
69      *
70      * @param refactoring refactoring, which adds this RefactoringElementImplementation
71      * @param el element to add
72      * @return instance of Problem or null
73      */

74     public Problem add(AbstractRefactoring refactoring, RefactoringElementImplementation el) {
75         Problem p = null;
76         if (isReadOnly(el)) {
77             FileObject file = el.getParentFile();
78             readOnlyFiles.add(file);
79             el.setEnabled(false);
80             el.setStatus(el.READ_ONLY);
81             delegate.add(el);
82         } else if (isGuarded(el)) {
83             ArrayList<RefactoringElementImplementation> proposedChanges = new ArrayList();
84             ArrayList<Transaction> transactions = new ArrayList();
85             for (GuardedBlockHandler gbHandler: APIAccessor.DEFAULT.getGBHandlers(refactoring)) {
86                 el.setEnabled(false);
87                 p = APIAccessor.DEFAULT.chainProblems(gbHandler.handleChange(el, proposedChanges, transactions), p);
88                 
89                 if (p != null && p.isFatal())
90                     return p;
91                 
92                 delegate.addAll(proposedChanges);
93                 
94                 for (Transaction transaction:transactions) {
95                     registerTransaction(transaction);
96                 }
97                 
98                 if (!proposedChanges.isEmpty() || !transactions.isEmpty())
99                     return p;
100                 
101             }
102             el.setEnabled(false);
103             el.setStatus(el.GUARDED);
104             delegate.add(el);
105         } else {
106             delegate.add(el);
107         }
108         return p;
109     }
110     
111     /**
112      * Adds all RefactringElements from given Collection using #add method
113      * @param refactoring refactoring, which adds this RefactoringElement
114      * @param elements Collection of RefactoringElements
115      * @return instance of Problem or null
116      */

117     public Problem addAll(AbstractRefactoring refactoring, Collection<RefactoringElementImplementation> elements) {
118     Problem p = null;
119     for (RefactoringElementImplementation rei:elements) {
120         p = APIAccessor.DEFAULT.chainProblems(p, add(refactoring, rei));
121             if (p!=null && p.isFatal())
122                 return p;
123     }
124         return p;
125     }
126     
127     
128     /**
129      *
130      * @return RefactoringSession associated with this RefactoringElementsBag
131      */

132     public RefactoringSession getSession() {
133         return session;
134     }
135     
136     Collection<FileObject> getReadOnlyFiles() {
137         return readOnlyFiles;
138     }
139     
140     /**
141      * commits are called after all changes are performed
142      * @param commit Transaction to commit
143      * @see Transaction
144      * @see BackupFacilty
145      */

146     public void registerTransaction(Transaction commit) {
147         if (APIAccessor.DEFAULT.isCommit(session))
148             if (!commits.contains(commit))
149                 commits.add(commit);
150     }
151     
152     
153     /**
154      * fileChanges are performed after all element changes
155      * @param changes changes to be performed
156      * @see Transaction
157      * @see BackupFacilty
158      */

159     public void registerFileChange(Transaction changes) {
160         if (APIAccessor.DEFAULT.isCommit(session))
161             fileChanges.add(changes);
162     }
163     
164     private boolean isReadOnly(RefactoringElementImplementation rei) {
165         return !rei.getParentFile().canWrite();
166     }
167     
168     /**
169      * TODO: GuardedQuery is still missing
170      * this solution has performance issues.
171      */

172     private boolean isGuarded(RefactoringElementImplementation el) {
173         if (el.getPosition()==null)
174             return false;
175         try {
176             DataObject dob = DataObject.find(el.getParentFile());
177             EditorCookie e = dob.getCookie(EditorCookie.class);
178             if (e!=null) {
179                 GuardedSectionManager manager = GuardedSectionManager.getInstance(e.openDocument());
180                 if (manager != null) {
181                     Position JavaDoc elementStart = el.getPosition().getBegin().getPosition();
182                     Position JavaDoc elementEnd = el.getPosition().getEnd().getPosition();
183                     for(GuardedSection section:manager.getGuardedSections()) {
184                         if (section.contains(elementStart, false) ||
185                                 section.contains(elementEnd, false)) {
186                             return true;
187                         }
188                     }
189                 }
190             }
191         } catch (DataObjectNotFoundException ex) {
192             java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE,
193                     ex.getMessage(), ex);
194         } catch (IOException JavaDoc ex) {
195             java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE,
196                     ex.getMessage(), ex);
197         }
198         return false;
199     }
200 }
201
Popular Tags