KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > refactoring > UsageSet


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 package org.netbeans.modules.xml.refactoring;
20
21 import java.beans.PropertyChangeEvent JavaDoc;
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.beans.PropertyChangeSupport JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30 import org.netbeans.api.project.SourceGroup;
31 import org.netbeans.modules.xml.xam.Component;
32 import org.netbeans.modules.xml.xam.ComponentEvent;
33 import org.netbeans.modules.xml.xam.ComponentListener;
34 import org.netbeans.modules.xml.xam.Model;
35 import org.netbeans.modules.xml.xam.Referenceable;
36 import org.openide.filesystems.FileObject;
37 import org.openide.util.WeakListeners;
38
39 /**
40  * This class represents an atomic workset of usages for a usage search or
41  * a refactoring request. Any change to sources within the scope of request
42  * (currently project) would invalidate the usage set and undo/redo state of
43  * the refactoring change..
44  *
45  * @author Nam Nguyen
46  */

47
48 public class UsageSet implements PropertyChangeListener JavaDoc, ComponentListener {
49     public static final String JavaDoc VALID_PROPERTY = "valid";
50     private Model targetModel;
51     private Referenceable target;
52     private Set JavaDoc<UsageGroup> usageGroups;
53     private boolean valid;
54     private PropertyChangeSupport JavaDoc pcs;
55     
56     /** Creates a new instance of RefactoringPreviewSet */
57     public UsageSet(Referenceable target) {
58         assert target != null;
59         this.target = target;
60         targetModel = RefactorRequest.getModel(target);
61         assert targetModel != null;
62         pcs = new PropertyChangeSupport JavaDoc(this);
63         valid = true;
64         usageGroups = new HashSet JavaDoc<UsageGroup>();
65         addModelListeners(targetModel);
66     }
67     
68     private void addModelListeners(Model model) {
69         model.addPropertyChangeListener(WeakListeners.propertyChange(this, model));
70         model.addComponentListener((ComponentListener)
71             WeakListeners.create(ComponentListener.class, this, model));
72     }
73     
74     public void addPropertyChangeListener(PropertyChangeListener JavaDoc pcl) {
75         pcs.addPropertyChangeListener(pcl);
76     }
77
78     public void removePropertyChangeListener(PropertyChangeListener JavaDoc pcl) {
79         pcs.removePropertyChangeListener(pcl);
80     }
81
82     public Collection JavaDoc<UsageGroup> getUsages() {
83         return usageGroups;
84     }
85     
86     public Set JavaDoc<Model> getModels() {
87         Set JavaDoc<Model> models = new HashSet JavaDoc<Model>();
88         for (UsageGroup ug : usageGroups ) {
89             if (ug.getModel() != null) {
90                 models.add(ug.getModel());
91             }
92         }
93         return models;
94     }
95     
96     public Map JavaDoc<SourceGroup, Map JavaDoc<FileObject, Set JavaDoc<UsageGroup>>> getSortedUsages() {
97         Map JavaDoc<SourceGroup,Map JavaDoc<FileObject,Set JavaDoc<UsageGroup>>> result = new HashMap JavaDoc<SourceGroup,Map JavaDoc<FileObject,Set JavaDoc<UsageGroup>>>();
98         for (UsageGroup u : getUsages()) {
99             Map JavaDoc<FileObject,Set JavaDoc<UsageGroup>> usagesInGroup = result.get(u.getSourceGroup());
100             if (usagesInGroup == null) {
101                 usagesInGroup = new HashMap JavaDoc<FileObject,Set JavaDoc<UsageGroup>>();
102                 result.put(u.getSourceGroup(), usagesInGroup);
103             }
104             
105             Set JavaDoc<UsageGroup> usagesInFolder = usagesInGroup.get(u.getFolder());
106             if (usagesInFolder == null) {
107                 usagesInFolder = new HashSet JavaDoc<UsageGroup>();
108                 usagesInGroup.put(u.getFolder(), usagesInFolder);
109             }
110             usagesInFolder.add(u);
111         }
112         return result;
113     }
114     
115     public void addUsages(List JavaDoc<UsageGroup> usages) {
116         if (usages == null) return;
117         for (UsageGroup u : usages) {
118             addUsage(u);
119         }
120     }
121     
122     public void addUsage(UsageGroup p) {
123         if (p.getErrors().isEmpty()) {
124             assert p.getModel() != null : "Usage group does not have model"; //NOI18N
125
}
126         if (! p.getItems().isEmpty() || ! p.getErrors().isEmpty()) {
127             addModelListeners(p.getModel());
128             usageGroups.add(p);
129         }
130     }
131     
132     public void removeUsage(UsageGroup p) {
133         usageGroups.remove(p);
134     }
135     
136     public boolean isEmpty() {
137         return isEmpty(true);
138     }
139     public boolean isEmpty(boolean countErrors) {
140         for (UsageGroup ug : usageGroups) {
141             if (! ug.isEmpty(countErrors)) {
142                 return false;
143             }
144         }
145         return true;
146     }
147     
148     /**
149      * Any change to components in usages set invalidate the usage set.
150      */

151     public boolean isValid() {
152         return valid;
153     }
154     
155     protected void setValid(boolean valid) {
156         boolean old = isValid();
157         this.valid = valid;
158         pcs.firePropertyChange(VALID_PROPERTY, old, valid);
159     }
160     
161     private boolean isMySource(Object JavaDoc source) {
162         if (source == target) {
163             return true;
164         }
165         Model sourceModel = null;
166         if (source instanceof Model) {
167             sourceModel = (Model) source;
168         } else if (source instanceof Component) {
169             sourceModel = ((Component)source).getModel();
170         }
171         if (sourceModel == null) {
172             return false;
173         }
174         if (targetModel == sourceModel) {
175             return true;
176         }
177         for (UsageGroup u : getUsages()) {
178             if (u.getModel() == sourceModel) {
179                 return true;
180             }
181         }
182         return false;
183     }
184     
185     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
186         handleChange(evt.getSource());
187     }
188     
189     private void handleChange(Object JavaDoc source) {
190         // ignore changes from the refactoring itself
191
if (RefactoringManager.getInstance().isRunning(this) || //
192
! isValid() || ! isMySource(source)) {
193             return;
194         }
195         setValid(false);
196     }
197
198     public void valueChanged(ComponentEvent evt) {
199         handleChange(evt.getSource());
200     }
201
202     public void childrenDeleted(ComponentEvent evt) {
203         handleChange(evt.getSource());
204     }
205
206     public void childrenAdded(ComponentEvent evt) {
207         handleChange(evt.getSource());
208     }
209 }
210
Popular Tags