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-2006 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: package org.netbeans.modules.vmd.api.io;
042:
043: import org.openide.util.*;
044: import org.openide.windows.TopComponent;
045: import org.openide.windows.WindowManager;
046: import org.openide.windows.Mode;
047: import org.netbeans.modules.vmd.api.model.*;
048: import org.netbeans.modules.vmd.api.model.common.ActiveDocumentSupport;
049:
050: import java.beans.PropertyChangeEvent;
051: import java.beans.PropertyChangeListener;
052: import java.util.Collection;
053: import java.util.EventListener;
054: import java.util.Set;
055:
056: /**
057: * This class allows to register a Listener.
058: *
059: * @author David Kaspar
060: */
061: public final class ActiveViewSupport {
062:
063: private static final String MODE_EDITOR = "editor"; // NOI18N
064:
065: private static final ActiveViewSupport support = new ActiveViewSupport();
066:
067: private final WeakSet<Listener> listeners = new WeakSet<Listener>();
068: private ActiveViewObserver topComponentListener = null;
069:
070: private DataObjectContext context = null;
071: private final ActiveDocumentObserver awereness = new ActiveDocumentObserver();
072:
073: private ActiveViewSupport() {
074: if (topComponentListener == null)
075: TopComponent.getRegistry().addPropertyChangeListener(
076: topComponentListener = new ActiveViewObserver());
077: }
078:
079: /**
080: * Returns a default instance of the ActiveViewSupport.
081: * @return the default ActiveViewSupport
082: */
083: public static ActiveViewSupport getDefault() {
084: return support;
085: }
086:
087: /**
088: * Registers an listener.
089: * @param listener the listener
090: */
091: public void addActiveViewListener(Listener listener) {
092: synchronized (listeners) {
093: listeners.add(listener);
094: }
095: }
096:
097: /**
098: * Unregisters an listener.
099: * @param listener the listener
100: */
101: public void removeActiveViewListener(Listener listener) {
102: synchronized (listeners) {
103: listeners.remove(listener);
104: }
105: }
106:
107: /**
108: * Returns an active view in whole environment.
109: * @return the active view
110: */
111: public DataEditorView getActiveView() {
112: synchronized (listeners) {
113: return topComponentListener != null ? topComponentListener.activeView
114: : null;
115: }
116: }
117:
118: private void fireActiveViewChanged(DataEditorView deactivatedView,
119: DataEditorView activatedView) {
120: Listener[] activeViewListeners;
121: synchronized (listeners) {
122: activeViewListeners = listeners
123: .toArray(new Listener[listeners.size()]);
124: }
125: for (Listener listener : activeViewListeners) {
126: if (listener != null)
127: listener.activeViewChanged(deactivatedView,
128: activatedView);
129: }
130:
131: DataObjectContext newContext = activatedView != null ? activatedView
132: .getContext()
133: : null;
134: if (context != newContext) {
135: if (context != null)
136: context.removeDesignDocumentAwareness(awereness);
137: context = newContext;
138: if (context != null)
139: context.addDesignDocumentAwareness(awereness);
140: else
141: awereness.setDesignDocument(null);
142: }
143: }
144:
145: private static Mode findEditorMode() {
146: Set modes = WindowManager.getDefault().getModes();
147: for (Object o : modes) {
148: Mode m = (Mode) o;
149: if (m != null && MODE_EDITOR.equals(m.getName()))
150: return m;
151: }
152: return null;
153: }
154:
155: private class ActiveViewObserver implements PropertyChangeListener,
156: LookupListener {
157:
158: private Lookup.Result<DataEditorView> result;
159: private DataEditorView activeView;
160:
161: public void propertyChange(PropertyChangeEvent evt) {
162: if (!TopComponent.Registry.PROP_ACTIVATED.equals(evt
163: .getPropertyName()))
164: return;
165: // System.out.println ("!! Global Context Lookup Result = " + Utilities.actionsGlobalContext ().lookupAll (Object.class));
166: Mode m = findEditorMode();
167: TopComponent component = m != null ? m
168: .getSelectedTopComponent() : null;
169: if (result != null)
170: result.removeLookupListener(this );
171: result = component != null ? component.getLookup()
172: .lookupResult(DataEditorView.class) : null;
173: if (result != null)
174: result.addLookupListener(this );
175: update();
176: }
177:
178: public void resultChanged(LookupEvent ev) {
179: update();
180: }
181:
182: private void update() {
183: DataEditorView lastView = activeView;
184: if (result != null) {
185: Collection<? extends DataEditorView> dataEditorViews = result
186: .allInstances();
187: activeView = dataEditorViews.isEmpty() ? null
188: : dataEditorViews.iterator().next();
189: } else
190: activeView = null;
191: if (lastView != activeView)
192: fireActiveViewChanged(lastView, activeView);
193:
194: }
195:
196: }
197:
198: private class ActiveDocumentObserver implements
199: DesignDocumentAwareness {
200:
201: public void setDesignDocument(DesignDocument designDocument) {
202: ActiveDocumentSupport.getDefault().setActiveDocument(
203: designDocument);
204: }
205:
206: }
207:
208: /**
209: * Used for notification of an active view in the whole environment.
210: * Usually used by active-view-aware components like inspector, palette, ...
211: * The listener could be registered using ActiveViewSupport class.
212: * @author David Kaspar
213: */
214: public interface Listener extends EventListener {
215:
216: /**
217: * Called when a reference to an active view is changed.
218: * Called before activeDocumentChanged and activeComponentsChanged methods.
219: * @param deactivatedView the deactivated view; null if there was no active view before.
220: * @param activatedView the activated view; null if there is no active view now.
221: */
222: void activeViewChanged(DataEditorView deactivatedView,
223: DataEditorView activatedView);
224:
225: }
226:
227: }
|