KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23 import org.netbeans.api.project.SourceGroup;
24 import org.netbeans.modules.xml.refactoring.spi.RefactoringEngine;
25 import org.netbeans.modules.xml.xam.Component;
26 import org.netbeans.modules.xml.xam.Model;
27 import org.netbeans.modules.xml.xam.Referenceable;
28 import org.openide.filesystems.FileObject;
29
30 /**
31  * This class represent a usage search result within a model or a model source.
32  *
33  * @author Nam Nguyen
34  */

35
36 // TODO soft references to target instead ?
37

38 public class UsageGroup {
39     private RefactoringEngine engine;
40     private Model model;
41     private FileObject source; //TODO maybe remove dependency on NB ?
42
private Referenceable target;
43     private SourceGroup sourceGroup;
44     
45     private List JavaDoc<Usage> items = new ArrayList JavaDoc<Usage>();
46     private List JavaDoc<ErrorItem> errors = new ArrayList JavaDoc<ErrorItem>();
47     
48     public UsageGroup(RefactoringEngine engine, Model model, Referenceable target) {
49         this(engine, target);
50         this.model = model;
51     }
52     
53     public UsageGroup(RefactoringEngine engine, FileObject src, Referenceable target) {
54         this(engine, target);
55         this.source = src;
56     }
57     
58     private UsageGroup(RefactoringEngine engine, Referenceable target) {
59         if (engine == null || target == null) {
60             throw new IllegalArgumentException JavaDoc(
61                "Usage constructor needs non-null engine and target arguments"); //NOI18N
62
}
63         this.engine = engine;
64         this.target = target;
65     }
66
67     /**
68      * Returns the engine that generated this usage search result.
69      */

70     public RefactoringEngine getEngine() {
71         return engine;
72     }
73     
74     /**
75      * Returns the model in which this usage search is done.
76      */

77     public Model getModel() {
78         return model;
79     }
80     
81     /**
82      * Returns target of the usage search.
83      */

84     public Referenceable getTarget() {
85         return target;
86     }
87     
88     /**
89      * Returns target component of the usage search.
90      */

91     public Component getTargetComponent() {
92         if (target instanceof Component) {
93             return (Component) target;
94         }
95         return null;
96     }
97     
98     /**
99      * Returns target component of the usage search.
100      */

101     public Model getTargetModel() {
102         if (target instanceof Component) {
103             return ((Component) target).getModel();
104         } else if (target instanceof Model) {
105             return (Model) target;
106         }
107         return null;
108     }
109     
110     /**
111      * Returns the usage search result items.
112      */

113     public List JavaDoc<Usage> getItems() {
114         assert model != null : "Try to retrieve items from an Usage created with null model"; //NOI18N
115
return items;
116     }
117
118     /**
119      * Returns the usage search result items.
120      */

121     public List JavaDoc<Component> getUsageComponents() {
122         assert model != null : "Try to retrieve items from an Usage created with null model"; //NOI18N
123
List JavaDoc<Component> ret = new ArrayList JavaDoc<Component>();
124         for(Usage i : items) {
125             ret.add(i.getComponent());
126         }
127         return ret;
128     }
129     
130     /**
131      * Returns the usage items selected for refactoring.
132      */

133     public List JavaDoc<Component> getRefactorComponents() {
134         assert model != null : "Try to retrieve items from an Usage created with null model"; //NOI18N
135
List JavaDoc<Component> ret = new ArrayList JavaDoc<Component>();
136         for(Usage i : items) {
137             if (i.isIncludedInRefactoring()) {
138                 ret.add(i.getComponent());
139             }
140         }
141         return ret;
142     }
143     
144     public void addItem(Component usageComponent) {
145         assert model != null : "Cannot add item to Usage created with null model"; //NOI18N
146
items.add(new Usage(usageComponent, this));
147     }
148     
149     /**
150      * Returns the list of errors seen during the search.
151      */

152     public List JavaDoc<ErrorItem> getErrors() {
153         return errors;
154     }
155     
156     public void addError(Component errorComponent, String JavaDoc message) {
157         errors.add(new ErrorItem(errorComponent, message));
158     }
159     
160     public void addError(ErrorItem error) {
161         errors.add(error);
162         for (Usage u : items) {
163             if (u.getComponent().equals(error.getComponent())) {
164                 u.setIncludedInRefactoring(false);
165                 break;
166             }
167         }
168     }
169
170     public boolean isEmpty() {
171         return isEmpty(true);
172     }
173     
174     public boolean isEmpty(boolean countErrors) {
175         if (countErrors) {
176             return getItems().isEmpty() && getErrors().isEmpty();
177         } else {
178             return getItems().isEmpty();
179         }
180     }
181     
182     protected void setSourceGroup(SourceGroup group) {
183         sourceGroup = group;
184     }
185     
186     public SourceGroup getSourceGroup() {
187         return sourceGroup;
188     }
189     
190     public FileObject getFolder() {
191         return getFileObject().getParent();
192     }
193     
194     public FileObject getFileObject() {
195         if (source == null) {
196             assert model != null : "Invalid Usage object, expecting non-null model."; //NOI18N
197
source = (FileObject) getModel().getModelSource().getLookup().lookup(FileObject.class);
198             assert source != null : "ModelSource should have FileObject in lookup"; //NOI18N
199
}
200         return source;
201     }
202     
203 }
204
Popular Tags