001: /*
002: * Sun Public License Notice
003: *
004: * The contents of this file are subject to the Sun Public License
005: * Version 1.0 (the "License"). You may not use this file except in
006: * compliance with the License. A copy of the License is available at
007: * http://www.sun.com/
008: *
009: * The Original Code is NetBeans. The Initial Developer of the Original
010: * Code is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
011: * Microsystems, Inc. All Rights Reserved.
012:
013: If you wish your version of this file to be governed by only the CDDL
014: or only the GPL Version 2, indicate your decision by adding
015: "[Contributor] elects to include this software in this distribution
016: under the [CDDL or GPL Version 2] license." If you do not indicate a
017: single choice of license, a recipient has the option to distribute
018: your version of this file under either the CDDL, the GPL Version 2 or
019: to extend the choice of license to its licensees as provided above.
020: However, if you add GPL Version 2 code and therefore, elected the GPL
021: Version 2 license, then the option applies only if the new code is
022: made subject to such option by the copyright holder.
023:
024: If you wish your version of this file to be governed by only the CDDL
025: or only the GPL Version 2, indicate your decision by adding
026: "[Contributor] elects to include this software in this distribution
027: under the [CDDL or GPL Version 2] license." If you do not indicate a
028: single choice of license, a recipient has the option to distribute
029: your version of this file under either the CDDL, the GPL Version 2 or
030: to extend the choice of license to its licensees as provided above.
031: However, if you add GPL Version 2 code and therefore, elected the GPL
032: Version 2 license, then the option applies only if the new code is
033: made subject to such option by the copyright holder.
034: */
035:
036: package org.netbeans.modules.vmd.inspector;
037:
038: import java.awt.BorderLayout;
039: import java.awt.Color;
040: import java.beans.PropertyChangeEvent;
041: import java.beans.PropertyChangeListener;
042: import java.lang.ref.WeakReference;
043: import java.util.Collection;
044: import javax.swing.Action;
045: import javax.swing.JMenu;
046: import javax.swing.JMenuItem;
047: import org.netbeans.modules.vmd.api.model.DesignComponent;
048: import org.netbeans.modules.vmd.api.model.DesignDocument;
049: import org.openide.util.actions.Presenter;
050: import org.openide.explorer.ExplorerManager;
051: import org.openide.explorer.ExplorerUtils;
052: import org.openide.explorer.view.BeanTreeView;
053: import org.openide.nodes.Node;
054: import org.openide.util.WeakSet;
055: import org.openide.windows.TopComponent;
056:
057: /**
058: *
059: * @author Karol Harezlak
060: */
061:
062: final class InspectorUI extends TopComponent implements
063: ExplorerManager.Provider, PropertyChangeListener,
064: Presenter.Popup {
065:
066: static final String INSPECTOR_UI_ID = "InspectorUI"; //NOI18N
067: private transient ExplorerManager explorerManager;
068: private volatile transient boolean lockSelectionSetting;
069: private transient BeanTreeView inspectorBeanTreeView;
070: private transient WeakReference<DesignDocument> document;
071:
072: InspectorUI(DesignDocument document) {
073: lockSelectionSetting = false;
074: explorerManager = new ExplorerManager();
075: explorerManager.addPropertyChangeListener(this );
076: initComponents();
077: lockSelectionSetting = false;
078: associateLookup(ExplorerUtils.createLookup(explorerManager,
079: getActionMap()));
080: this .document = new WeakReference<DesignDocument>(document);
081: }
082:
083: private void initComponents() {
084: inspectorBeanTreeView = new InspectorBeanTreeView(
085: explorerManager);
086: inspectorBeanTreeView.setRootVisible(false);
087: setLayout(new BorderLayout());
088: add(inspectorBeanTreeView, BorderLayout.CENTER);
089: setBackground(Color.WHITE);
090: }
091:
092: public ExplorerManager getExplorerManager() {
093: return explorerManager;
094: }
095:
096: public synchronized void propertyChange(PropertyChangeEvent evt) {
097: if (!ExplorerManager.PROP_SELECTED_NODES.equals(evt
098: .getPropertyName())) {
099: return;
100: }
101: if (explorerManager.getSelectedNodes().length < 1) {
102: return;
103: }
104: if (document == null || document.get() == null) {
105: return;
106: }
107: final DesignDocument d = document.get();
108: if (d.getTransactionManager().isAccess())
109: return;
110: d.getTransactionManager().writeAccess(new Runnable() {
111:
112: public void run() {
113: if (lockSelectionSetting) {
114: return;
115: }
116: try {
117: lockSelectionSetting = true;
118: Node[] selectedNodes = explorerManager
119: .getSelectedNodes();
120: WeakSet<DesignComponent> selectedComponents = new WeakSet<DesignComponent>();
121: for (Node node : selectedNodes) {
122: if (node instanceof InspectorFolderNode) {
123: Long componentID = ((InspectorFolderNode) node)
124: .getComponentID();
125: DesignComponent component = componentID == null ? null
126: : d.getComponentByUID(componentID);
127: if (component != null) {
128: selectedComponents.add(component);
129: }
130: }
131: }
132: d.setSelectedComponents(
133: InspectorUI.INSPECTOR_UI_ID,
134: selectedComponents);
135: } finally {
136: lockSelectionSetting = false;
137: }
138: }
139: });
140: }
141:
142: void expandNodes(
143: final Collection<InspectorFolderWrapper> foldersToUpdate) {
144: if (foldersToUpdate == null) {
145: return;
146: }
147: for (InspectorFolderWrapper wrapper : foldersToUpdate) {
148: InspectorFolderNode node = wrapper.getNode();
149: if (node != null) {
150: inspectorBeanTreeView.expandNode(node);
151: }
152: }
153: }
154:
155: synchronized void setRootNode(final Node rootNode) {
156: getExplorerManager().setRootContext(rootNode);
157: revalidate();
158: repaint();
159: }
160:
161: public void activeComponentsChanged(
162: Collection<DesignComponent> activeComponents) {
163: }
164:
165: @Override
166: public Action[] getActions() {
167: return new Action[0];
168: }
169:
170: public JMenuItem getPopupPresenter() {
171: return new JMenu("menu"); //NOI18N
172: }
173:
174: @Override
175: public int getPersistenceType() {
176: return TopComponent.PERSISTENCE_NEVER;
177: }
178:
179: @Override
180: protected void componentActivated() {
181: super .componentActivated();
182: ExplorerUtils.activateActions(explorerManager, true);
183: }
184:
185: @Override
186: protected void componentDeactivated() {
187: super .componentDeactivated();
188: ExplorerUtils.activateActions(explorerManager, false);
189: }
190: }
|