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 org.netbeans.modules.cnd.api.model.CsmChangeEvent;
046: import org.netbeans.modules.cnd.api.model.CsmListeners;
047: import org.netbeans.modules.cnd.api.model.CsmModelListener;
048: import org.netbeans.modules.cnd.api.model.CsmModelState;
049: import org.netbeans.modules.cnd.api.model.CsmModelStateListener;
050: import org.netbeans.modules.cnd.api.model.CsmProgressListener;
051: import org.netbeans.modules.cnd.api.model.CsmProject;
052: import org.netbeans.modules.cnd.modelimpl.debug.DiagnosticExceptoins;
053: import org.netbeans.modules.cnd.modelimpl.util.WeakList;
054: import org.openide.util.Lookup;
055:
056: /**
057: * CsmListeners implementation
058: * @author Vladimir Kvashin
059: */
060: public class ListenersImpl extends CsmListeners {
061:
062: private WeakList<CsmModelListener> modelListeners = new WeakList<CsmModelListener>();
063: private WeakList<CsmModelStateListener> modelStateListeners = new WeakList<CsmModelStateListener>();
064:
065: @Override
066: public void addModelListener(CsmModelListener listener) {
067: modelListeners.add(listener);
068: }
069:
070: @Override
071: public void removeModelListener(CsmModelListener listener) {
072: modelListeners.remove(listener);
073: }
074:
075: @Override
076: public void addProgressListener(CsmProgressListener listener) {
077: ProgressSupport.instance().addProgressListener(listener);
078: }
079:
080: @Override
081: public void removeProgressListener(CsmProgressListener listener) {
082: ProgressSupport.instance().removeProgressListener(listener);
083: }
084:
085: @Override
086: public void addModelStateListener(CsmModelStateListener listener) {
087: modelStateListeners.add(listener);
088: }
089:
090: @Override
091: public void removeModelStateListener(CsmModelStateListener listener) {
092: modelStateListeners.remove(listener);
093: }
094:
095: //package-local
096: static ListenersImpl getImpl() {
097: return (ListenersImpl) CsmListeners.getDefault();
098: }
099:
100: private Iterable<? extends CsmModelListener> getModelListeners() {
101: Collection<? extends CsmModelListener> services = Lookup
102: .getDefault().lookupAll(CsmModelListener.class);
103: return (services.isEmpty()) ? modelListeners : modelListeners
104: .join(services);
105: }
106:
107: private Iterable<? extends CsmModelStateListener> getModelStateListeners() {
108: Collection<? extends CsmModelStateListener> services = Lookup
109: .getDefault().lookupAll(CsmModelStateListener.class);
110: return (services.isEmpty()) ? modelStateListeners
111: : modelStateListeners.join(services);
112: }
113:
114: //package-local
115: void fireProjectOpened(final ProjectBase csmProject) {
116: csmProject.onAddedToModel();
117: for (CsmModelListener listener : getModelListeners()) {
118: try {
119: listener.projectOpened(csmProject);
120: } catch (AssertionError ex) {
121: DiagnosticExceptoins.register(ex);
122: } catch (Exception ex) {
123: DiagnosticExceptoins.register(ex);
124: }
125: }
126: }
127:
128: //package-local
129: void fireProjectClosed(CsmProject csmProject) {
130: for (CsmModelListener listener : getModelListeners()) {
131: try {
132: listener.projectClosed(csmProject);
133: } catch (AssertionError ex) {
134: DiagnosticExceptoins.register(ex);
135: } catch (Exception ex) {
136: DiagnosticExceptoins.register(ex);
137: }
138: }
139: }
140:
141: //package-local
142: void fireModelChanged(CsmChangeEvent e) {
143: for (CsmModelListener listener : getModelListeners()) {
144: try {
145: listener.modelChanged(e);
146: } catch (AssertionError ex) {
147: DiagnosticExceptoins.register(ex);
148: } catch (Exception ex) {
149: DiagnosticExceptoins.register(ex);
150: }
151: }
152: }
153:
154: //package-local
155: void fireModelStateChanged(CsmModelState newState,
156: CsmModelState oldState) {
157: for (CsmModelStateListener listener : getModelStateListeners()) {
158: try {
159: listener.modelStateChanged(newState, oldState);
160: } catch (AssertionError ex) {
161: DiagnosticExceptoins.register(ex);
162: } catch (Exception ex) {
163: DiagnosticExceptoins.register(ex);
164: }
165: }
166: }
167:
168: }
|