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:
042: package org.netbeans.modules.cnd.modelimpl.csm.core;
043:
044: import java.util.Collection;
045: import java.util.Collections;
046: import java.util.HashMap;
047: import java.util.HashSet;
048: import java.util.Map;
049: import java.util.Set;
050: import org.netbeans.modules.cnd.api.model.CsmChangeEvent;
051: import org.netbeans.modules.cnd.api.model.CsmFile;
052: import org.netbeans.modules.cnd.api.model.CsmNamespace;
053: import org.netbeans.modules.cnd.api.model.CsmOffsetableDeclaration;
054: import org.netbeans.modules.cnd.api.model.CsmProject;
055:
056: /**
057: * CsmChangeEvent implementation
058: * @author vk155633
059: */
060: public class ChangeEventImpl extends CsmChangeEvent {
061:
062: protected Set<CsmFile> newFiles;
063: protected Set<CsmFile> removedFiles;
064: protected Set<CsmFile> changedFiles;
065:
066: protected Set<CsmOffsetableDeclaration> newDeclarations;
067: protected Set<CsmOffsetableDeclaration> removedDeclarations;
068: protected Map<CsmOffsetableDeclaration, CsmOffsetableDeclaration> changedDeclarations;
069:
070: protected Set<CsmProject> changedProjects;
071:
072: protected Map<CharSequence, CsmNamespace> newNamespaces;
073: protected Map<CharSequence, CsmNamespace> removedNamespaces;
074:
075: public ChangeEventImpl(Object source) {
076: super (source);
077: }
078:
079: public Collection<CsmFile> getNewFiles() {
080: if (newFiles == null) {
081: newFiles = new HashSet<CsmFile>();
082: }
083: return newFiles;
084: }
085:
086: public Collection<CsmFile> getRemovedFiles() {
087: if (removedFiles == null) {
088: removedFiles = new HashSet<CsmFile>();
089: }
090: return removedFiles;
091: }
092:
093: public Collection<CsmFile> getChangedFiles() {
094: if (changedFiles == null) {
095: changedFiles = new HashSet<CsmFile>();
096: }
097: return changedFiles;
098: }
099:
100: public Collection<CsmOffsetableDeclaration> getNewDeclarations() {
101: if (newDeclarations == null) {
102: newDeclarations = new HashSet<CsmOffsetableDeclaration>();
103: }
104: return newDeclarations;
105: }
106:
107: public Collection<CsmOffsetableDeclaration> getRemovedDeclarations() {
108: if (removedDeclarations == null) {
109: removedDeclarations = new HashSet<CsmOffsetableDeclaration>();
110: }
111: return removedDeclarations;
112: }
113:
114: public Map<CsmOffsetableDeclaration, CsmOffsetableDeclaration> getChangedDeclarations() {
115: if (changedDeclarations == null) {
116: changedDeclarations = new HashMap<CsmOffsetableDeclaration, CsmOffsetableDeclaration>();
117: }
118: return changedDeclarations;
119: }
120:
121: public Collection<CsmProject> getChangedProjects() {
122: if (changedProjects == null) {
123: changedProjects = new HashSet<CsmProject>();
124: }
125: return changedProjects;
126: }
127:
128: public Collection<CsmNamespace> getNewNamespaces() {
129: if (newNamespaces != null) {
130: return newNamespaces.values();
131: }
132: return Collections.EMPTY_LIST;
133: }
134:
135: public Collection<CsmNamespace> getRemovedNamespaces() {
136: if (removedNamespaces != null) {
137: return removedNamespaces.values();
138: }
139: return Collections.EMPTY_LIST;
140: }
141:
142: public boolean isEmpty() {
143: return (changedProjects == null || changedProjects.isEmpty())
144: && (newFiles == null || newFiles.isEmpty())
145: && (changedFiles == null || changedFiles.isEmpty())
146: && (removedFiles == null || removedFiles.isEmpty())
147: && (newDeclarations == null || newDeclarations
148: .isEmpty())
149: && (removedDeclarations == null || removedDeclarations
150: .isEmpty())
151: && (changedDeclarations == null || changedDeclarations
152: .isEmpty())
153: && (newNamespaces == null || newNamespaces.isEmpty())
154: && (removedNamespaces == null || removedNamespaces
155: .isEmpty());
156: }
157:
158: public void addChangedFile(CsmFile file) {
159: getChangedFiles().add(file);
160: getChangedProjects().add(file.getProject());
161: }
162:
163: public void addNewFile(CsmFile file) {
164: getNewFiles().add(file);
165: getChangedProjects().add(file.getProject());
166: }
167:
168: public void addRemovedFile(CsmFile file) {
169: getRemovedFiles().add(file);
170: getChangedProjects().add(file.getProject());
171: }
172:
173: public void addChangedDeclaration(CsmOffsetableDeclaration oldDecl,
174: CsmOffsetableDeclaration newDecl) {
175: getChangedDeclarations().put(oldDecl, newDecl);
176: addChangedFile(oldDecl.getContainingFile());
177: }
178:
179: public void addNewDeclaration(CsmOffsetableDeclaration declaration) {
180: getNewDeclarations().add(declaration);
181: CsmFile file = declaration.getContainingFile();
182: if (!getNewFiles().contains(file)) {
183: addChangedFile(file);
184: }
185: }
186:
187: public void addRemovedDeclaration(
188: CsmOffsetableDeclaration declaration) {
189: getRemovedDeclarations().add(declaration);
190: CsmFile file = declaration.getContainingFile();
191: if (!getRemovedFiles().contains(file)) {
192: addChangedFile(file);
193: }
194: }
195:
196: public void addNewNamespace(CsmNamespace ns) {
197: // remove from removed
198: if (_getRemovedNamespaces().remove(ns.getQualifiedName()) == null) {
199: _getNewNamespaces().put(ns.getQualifiedName(), ns);
200: getChangedProjects().add(ns.getProject());
201: }
202: }
203:
204: public void addRemovedNamespace(CsmNamespace ns) {
205: // put in removed only if not added as "new"
206: if (_getNewNamespaces().get(ns.getQualifiedName()) == null) {
207: _getRemovedNamespaces().put(ns.getQualifiedName(), ns);
208: getChangedProjects().add(ns.getProject());
209: }
210: }
211:
212: private Map<CharSequence, CsmNamespace> _getRemovedNamespaces() {
213: if (removedNamespaces == null) {
214: removedNamespaces = new HashMap<CharSequence, CsmNamespace>();
215: }
216: return removedNamespaces;
217: }
218:
219: private Map<CharSequence, CsmNamespace> _getNewNamespaces() {
220: if (newNamespaces == null) {
221: newNamespaces = new HashMap<CharSequence, CsmNamespace>();
222: }
223: return newNamespaces;
224: }
225: }
|