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: package org.netbeans.modules.css.visual.ui.preview;
042:
043: import java.beans.PropertyChangeEvent;
044: import java.beans.PropertyChangeListener;
045: import java.lang.ref.WeakReference;
046: import javax.swing.text.Document;
047: import org.netbeans.modules.css.editor.CssEditorSupport;
048: import org.netbeans.modules.css.visual.ui.StyleBuilderTopComponent;
049: import org.openide.cookies.EditorCookie;
050: import org.openide.util.Lookup;
051: import org.openide.util.WeakListeners;
052: import org.openide.windows.TopComponent;
053: import org.openide.windows.TopComponent.Registry;
054: import org.openide.windows.WindowManager;
055:
056: /**
057: * Listens on the WindowManager.Registry for TCs and updates registered TCs accordingly.
058: *
059: * @author Marek Fukala
060: */
061: public class CSSTCController implements PropertyChangeListener {
062:
063: //allow GCize the shared class instance if noone needs it anymore
064: public static WeakReference<CSSTCController> instance;
065: private TopComponent lastCSSTC = null;
066:
067: /** Clients (CSSPreviewable TopComponent-s) should hold a strong reference to the
068: * instance obtained by this method call during its livecycle.
069: */
070: public static synchronized CSSTCController getDefault() {
071: if (instance == null) {
072: instance = new WeakReference<CSSTCController>(
073: new CSSTCController());
074: }
075: CSSTCController controllerInstance = instance.get();
076: if (controllerInstance == null) {
077: controllerInstance = new CSSTCController();
078: instance = new WeakReference<CSSTCController>(
079: controllerInstance);
080: return controllerInstance;
081: }
082: return instance.get();
083: }
084:
085: public CSSTCController() {
086: //register a weak property change listener to the window manager registry
087: Registry reg = WindowManager.getDefault().getRegistry();
088: reg.addPropertyChangeListener(WeakListeners.propertyChange(
089: this , reg));
090: }
091:
092: public void propertyChange(PropertyChangeEvent evt) {
093: if (TopComponent.Registry.PROP_ACTIVATED.equals(evt
094: .getPropertyName())) {
095: //a TC activated -
096: //check if the TC is editor TC and if so close the CSS preview and style builder
097: TopComponent activated = (TopComponent) evt.getNewValue();
098:
099: if (isCSSTC(activated)) {
100: previewableActivated(activated);
101: } else {
102: //issue 104603 workaround
103: if (activated instanceof CssPreviewTopComponent
104: || activated instanceof StyleBuilderTopComponent) {
105: return; //do not close the windows if user click on them
106: }
107:
108: //A non - CSS previewable activated in editor - close the CSS windows
109: if (WindowManager.getDefault()
110: .isOpenedEditorTopComponent(activated)
111: && lastCSSTC != null) {
112: notPreviewableActivated();
113: }
114: }
115: } else if (lastCSSTC != null
116: && TopComponent.Registry.PROP_TC_CLOSED.equals(evt
117: .getPropertyName())) {
118: //a TC closed - check if the TC is CSSpreviewable
119: //check if the activated nodes
120: TopComponent closedTC = (TopComponent) evt.getNewValue();
121: // if (isCSSTC(closedTC)) {
122: if (closedTC == lastCSSTC) {
123: //close the CSS windows
124: //FIXME side effect is that the windows are close
125: //and reopened again if another css previewable gets active
126: notPreviewableActivated();
127: }
128: }
129: }
130:
131: private boolean isCSSTC(TopComponent tc) {
132: if (tc == null) {
133: return false;
134: }
135: Document doc = getDocument(tc);
136: if (doc != null) {
137: String mimeType = (String) doc.getProperty("mimeType");
138: if (mimeType != null && "text/x-css".equals(mimeType)) {
139: return true;
140: }
141: }
142: return false;
143: }
144:
145: private Document getDocument(TopComponent tc) {
146: Lookup lookup = tc.getLookup(); //should be always non null unless someone overrides the TC
147: if (lookup == null) {
148: return null;
149: }
150: EditorCookie ec = lookup.lookup(EditorCookie.class);
151: if (ec != null) {
152: return ec.getDocument();
153: } else {
154: return null;
155: }
156: }
157:
158: private void previewableActivated(TopComponent tc) {
159: this .lastCSSTC = tc;
160: WindowManager.getDefault().findTopComponentGroup("Csswsgrp")
161: .open();
162: CssEditorSupport.getDefault().cssTCActivated(tc);
163: }
164:
165: private void notPreviewableActivated() {
166: this .lastCSSTC = null;
167: WindowManager.getDefault().findTopComponentGroup("Csswsgrp")
168: .close();
169: CssEditorSupport.getDefault().cssTCDeactivated();
170: }
171: }
|