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.inspector;
042:
043: import java.awt.BorderLayout;
044: import java.beans.PropertyVetoException;
045: import org.netbeans.modules.vmd.api.inspector.InspectorRegistry;
046: import org.netbeans.modules.vmd.api.model.DesignComponent;
047: import org.netbeans.modules.vmd.api.model.DesignDocument;
048: import org.netbeans.modules.vmd.api.model.DesignEvent;
049: import java.util.Collection;
050: import java.util.WeakHashMap;
051: import javax.swing.JComponent;
052: import javax.swing.JLabel;
053: import org.netbeans.modules.vmd.api.io.DataObjectContext;
054: import org.netbeans.modules.vmd.api.io.DesignDocumentAwareness;
055: import org.netbeans.modules.vmd.api.io.IOUtils;
056: import org.netbeans.modules.vmd.api.model.DesignEventFilter;
057: import org.netbeans.modules.vmd.api.model.DesignListener;
058: import org.netbeans.modules.vmd.api.model.common.ActiveDocumentSupport;
059: import org.openide.nodes.Node;
060: import org.openide.util.NbBundle;
061:
062: /**
063: * @author Karol Harezlak
064: */
065: public final class InspectorManagerView implements
066: DesignDocumentAwareness, ActiveDocumentSupport.Listener,
067: DesignListener {
068:
069: private static WeakHashMap<DataObjectContext, InspectorManagerView> INSTANCES = new WeakHashMap<DataObjectContext, InspectorManagerView>();
070: private static final JLabel emptyPanel = new JLabel(NbBundle
071: .getMessage(InspectorManagerView.class, "LBL_emptyPanel"),
072: JLabel.CENTER); //NOI18N
073: private DesignDocument document;
074: private InspectorWrapperTree folderWrapperTree;
075: private InspectorUI ui;
076: private DataObjectContext context;
077:
078: public static void register(DataObjectContext context) {
079: assert context != null;
080: synchronized (InspectorManagerView.class) {
081: if (INSTANCES.get(context) == null) {
082: INSTANCES.put(context,
083: new InspectorManagerView(context));
084: }
085: }
086: }
087:
088: private InspectorManagerView(DataObjectContext context) {
089: this .context = context;
090: context.addDesignDocumentAwareness(this );
091: }
092:
093: public void setDesignDocument(DesignDocument document) {
094: if (document != null) {
095: this .document = document;
096: ui = new InspectorUI(document);
097: folderWrapperTree = new InspectorWrapperTree(document, ui);
098: ActiveDocumentSupport.getDefault()
099: .addActiveDocumentListener(this );
100: document.getListenerManager().addDesignListener(this ,
101: new DesignEventFilter().setGlobal(true));
102: IOUtils.runInAWTNoBlocking(new Runnable() {
103: public void run() {
104: if (folderWrapperTree == null)
105: return;
106: folderWrapperTree.buildTree(null);
107: ui.getExplorerManager().setRootContext(
108: folderWrapperTree.getRootWrapperFolder()
109: .getNode());
110: }
111: });
112: } else if (this .document != null && document == null) {
113: ActiveDocumentSupport.getDefault()
114: .removeActiveDocumentListener(this );
115: this .document.getListenerManager().removeDesignListener(
116: this );
117: folderWrapperTree.terminate();
118: ui = null;
119: folderWrapperTree = null;
120: this .document = null;
121: INSTANCES.remove(context);
122: context = null;
123: }
124: }
125:
126: private void notifyUIContentChanged(final DesignEvent event) {
127: IOUtils.runInAWTNoBlocking(new Runnable() {
128: public void run() {
129: if (folderWrapperTree.isLocked()) {
130: throw new IllegalStateException(
131: "Access to the Navigator is locked"); //NOI18N
132: }
133: folderWrapperTree.buildTree(event);
134: ui.setRootNode(folderWrapperTree.getRootWrapperFolder()
135: .getNode());
136: }
137: });
138: }
139:
140: private void notifyUISelectionChanged() {
141: IOUtils.runInAWTNoBlocking(new Runnable() {
142: public void run() {
143: if (folderWrapperTree.isLocked()) {
144: throw new IllegalStateException(
145: "Access to the Navigator is locked"); //NOI18N
146: }
147: if (document == null
148: || document.getListenerManager() == null) {
149: return;
150: }
151: if (folderWrapperTree.getRootWrapperFolder().getNode() != null) {
152: final Collection<Node> selectedNodes = folderWrapperTree
153: .getSelectedNodes();
154: if (selectedNodes != null) {
155: try {
156: if (!selectedNodes.isEmpty()
157: && !folderWrapperTree.isLocked()) {
158: ui
159: .getExplorerManager()
160: .setSelectedNodes(
161: selectedNodes
162: .toArray(new Node[selectedNodes
163: .size()]));
164: }
165: } catch (PropertyVetoException e) {
166: e.printStackTrace();
167: }
168: }
169: }
170: if (folderWrapperTree.getRootWrapperFolder().getNode() == null) {
171: ui.setRootNode(Node.EMPTY);
172: }
173: }
174: });
175: }
176:
177: public synchronized void activeDocumentChanged(
178: DesignDocument deactivatedDocument,
179: final DesignDocument activatedDocument) {
180: if (deactivatedDocument == document
181: && activatedDocument == null) {
182: IOUtils.runInAWTNoBlocking(new Runnable() {
183: public void run() {
184: JComponent panel = InspectorPanel.getInstance()
185: .getComponent();
186: panel.removeAll();
187: panel.add(emptyPanel, BorderLayout.CENTER);
188: panel.revalidate();
189: panel.repaint();
190: }
191: });
192: return;
193: } else if (activatedDocument == document) {
194: IOUtils.runInAWTNoBlocking(new Runnable() {
195: public void run() {
196: if (InspectorPanel.getInstance() == null)
197: return;
198: JComponent panel = InspectorPanel.getInstance()
199: .getComponent();
200: panel.removeAll();
201: panel.add(ui, BorderLayout.CENTER);
202: panel.revalidate();
203: panel.repaint();
204: }
205: });
206: }
207: }
208:
209: public void activeComponentsChanged(
210: Collection<DesignComponent> activeComponents) {
211: }
212:
213: public void designChanged(DesignEvent event) {
214: if (document.getRootComponent() == null) {
215: return;
216: }
217: if (event.isStructureChanged()) {
218: notifyUIContentChanged(event);
219: }
220: if (event.isSelectionChanged()) {
221: notifyUISelectionChanged();
222: }
223: InspectorRegistry.getInstance(document).cleanUpRegistry();
224: }
225: }
|