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.visualweb.outline;
043:
044: import com.sun.rave.designtime.DesignBean;
045: import java.util.Collection;
046: import java.util.logging.Level;
047: import java.util.logging.Logger;
048: import javax.swing.JComponent;
049: import org.netbeans.spi.navigator.NavigatorPanelWithUndo;
050: import org.openide.awt.UndoRedo;
051: import org.openide.nodes.Node;
052: import org.openide.util.Lookup;
053: import org.openide.util.LookupEvent;
054: import org.openide.util.LookupListener;
055: import org.openide.util.NbBundle;
056: import org.openide.util.WeakListeners;
057:
058: /**
059: * Implementation of <code>NavigatorPanel</code>, which provides the
060: * outline component.
061: *
062: * @author Peter Zavadsky
063: */
064: public class OutlinePanelProvider implements NavigatorPanelWithUndo {
065:
066: // #123003 Removed memory leak.
067: // /** Current context to work on. */
068: // private Lookup.Result<DesignBean> currentContextResult;
069:
070: /** Listens on the retrieved <code>Lookup.Result</code>. */
071: private/*final*/LookupListener outlineLookupListener;// = new OutlineLookupListener();
072:
073: /** Creates a new instance of OutlinePanel */
074: public OutlinePanelProvider() {
075: }
076:
077: public String getDisplayName() {
078: return NbBundle.getMessage(OutlinePanel.class,
079: "LBL_OutlinePanel");
080: }
081:
082: public String getDisplayHint() {
083: return NbBundle.getMessage(OutlinePanel.class,
084: "HINT_OutlinePanel");
085: }
086:
087: public JComponent getComponent() {
088: return OutlinePanel.getDefault();
089: }
090:
091: public void panelActivated(Lookup lookup) {
092: Lookup.Result<DesignBean> currentContextResult = lookup
093: .lookup(new Lookup.Template<DesignBean>(
094: DesignBean.class));
095:
096: if (isFine()) {
097: fine("panelActivated lookup=" + lookup); // NOI18N
098: if (lookup != null) {
099: java.util.Collection col = lookup.lookup(
100: new Lookup.Template<Object>(Object.class))
101: .allInstances();
102: for (java.util.Iterator it = col.iterator(); it
103: .hasNext();) {
104: fine("item=" + it.next()); // NOI18N
105: }
106: }
107: }
108:
109: Collection<? extends DesignBean> designBeans = currentContextResult
110: .allInstances();
111: OutlinePanel.getDefault()
112: .setActiveBeans(
113: designBeans.toArray(new DesignBean[designBeans
114: .size()]));
115:
116: // XXX Get new listener (in case there is lingering the old one).
117: outlineLookupListener = new OutlineLookupListener();
118: LookupListener weakLookupListener = WeakListeners.create(
119: LookupListener.class, outlineLookupListener,
120: currentContextResult);
121: currentContextResult.addLookupListener(weakLookupListener);
122: }
123:
124: public void panelDeactivated() {
125: // if (currentContextResult == null) {
126: // // #105327 There seems to be panelDeactivated called while there was missing panelActivated call before.
127: // info(new IllegalStateException(
128: // "Called panelDeactivated method without previous correspondent panelActiavated method call on NavigatorPanel impl, "
129: // + "navigatorPanel=" + this)); // NOI18N
130: // } else {
131: // currentContextResult.removeLookupListener(outlineLookupListener);
132: // currentContextResult = null;
133: // }
134: // XXX Removing weak listener.
135: outlineLookupListener = null;
136:
137: // XXX #99299 Memory leak. Removing the tree when the panel is deactivated.
138: OutlinePanel.getDefault().setActiveBeans(new DesignBean[0]);
139: }
140:
141: public Lookup getLookup() {
142: return OutlinePanel.getDefault().getLookup();
143: }
144:
145: /** Listens on retrieved <code>Lookup.Result</code>. */
146: private static class OutlineLookupListener implements
147: LookupListener {
148: public void resultChanged(LookupEvent evt) {
149: Collection<? extends DesignBean> designBeans = ((Lookup.Result<DesignBean>) evt
150: .getSource()).allInstances();
151: if (isFine()) {
152: fine("designBeans=" + designBeans); // NOI18N
153: }
154:
155: OutlinePanel.getDefault().setActiveBeans(
156: designBeans.toArray(new DesignBean[designBeans
157: .size()]));
158: }
159: } // End of OultlineLookupListener.
160:
161: public UndoRedo getUndoRedo() {
162: return getUndoRedoFromNodes(OutlinePanel.getDefault()
163: .getExplorerManager().getSelectedNodes());
164: }
165:
166: private static UndoRedo getUndoRedoFromNodes(Node[] nodes) {
167: if (nodes == null) {
168: return UndoRedo.NONE;
169: }
170:
171: for (Node node : nodes) {
172: UndoRedo undoRedo = node.getLookup().lookup(UndoRedo.class);
173: if (undoRedo != null) {
174: return undoRedo;
175: }
176: }
177:
178: return UndoRedo.NONE;
179: }
180:
181: private static Logger getLogger() {
182: return Logger.getLogger(OutlinePanelProvider.class.getName());
183: }
184:
185: private static boolean isFine() {
186: return getLogger().isLoggable(Level.FINE);
187: }
188:
189: private static void fine(String message) {
190: getLogger().fine(message);
191: }
192:
193: private static void info(Exception ex) {
194: getLogger().log(Level.INFO, null, ex);
195: }
196: }
|