blob: 55a1e665e0fbe58169ea7767097b030c1fa77989 [file] [log] [blame]
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
= DevFaqTrackingExplorerSelections
:jbake-type: wiki
:jbake-tags: wiki, devfaq, needsreview
:jbake-status: published
:keywords: Apache NetBeans wiki DevFaqTrackingExplorerSelections
:description: Apache NetBeans wiki DevFaqTrackingExplorerSelections
:toc: left
:toc-title:
:syntax: true
=== How should I keep track of what the current nodeselection in the Explorer window is? Should I use the `ExplorerManager` ?
You can use the `ExplorerManager` if you created the explorer window, or you can programmatically get a reference to it. If you can somehow find a class implementing `ExplorerManager.Provider` then you can get the Explorer manager. This provider might in fact be a `TopComponent` in the `TopComponent.Registry`, if for example it was actually a `ExplorerPanel`.
But this is bad style - for example, if someone wrote a `TopComponent` that included a component implementing `ExplorerManager.Provider`, but as a subcomponent, and manually managed the node selection, this trick would fail.
Rather, if you know which top component you care about, you can just call `TopComponent.getActivatedNodes()` and this will work correctly even for non-Explorer components with a node selection, such as Editor panes open on Java sources.
Better still is to be agnostic about which top component should be providing the activated nodes, and just listen to changes in the `TopComponent.Registry.PROP''ACTIVATED''NODES` (or `TopComponent.Registry.PROP_CURRENT_NODES` as appropriate).
But best of all is not to have to ever directly pay attention to
the node selection. If you only need to know the node selection in
order to make some user action enabled or not, you should simply
extend `NodeAction`;
this class does all the dirty work for you of listening to changes
in the node selection and updating its state automatically.
If you just want to write some code that is sensitive to the global selection (not an action), you probably want to use link:DevFaqTrackGlobalSelection.asciidoc[Utilities.actionsGlobalContext()].
==== Multiple nodes selection - gotcha
If you allow multiple nodes to be selected you also have to keep in mind that certain other Netbeans components may only operate on single nodes.
One example is the link:http://www.netbeans.org/download/dev/javadoc/org-netbeans-spi-navigator/overview-summary.html[Navigator]. Let's suppose you have a navigator window associated with your selected node. What you will notice is that while your multiple selection is in focus, your link:DevFaqTrackGlobalSelection.asciidoc[code for acquiring] the selected nodes returns all the selected nodes. If the focus is then switched to the Navigator window, only one node is retrieved, all that while the multiple selection is still there, in the un-focused window.
The reason is because along with the focus change, the (single) node represented by the Navigator and stored in its lookup becomes the global selection which your retrieval code will then grab.
=== Apache Migration Information
The content in this page was kindly donated by Oracle Corp. to the
Apache Software Foundation.
This page was exported from link:http://wiki.netbeans.org/DevFaqTrackingExplorerSelections[http://wiki.netbeans.org/DevFaqTrackingExplorerSelections] ,
that was last modified by NetBeans user Admin
on 2009-11-06T16:02:52Z.
*NOTE:* This document was automatically converted to the AsciiDoc format on 2018-02-07, and needs to be reviewed.