org.eclipse.ui.actions.SelectionProviderAction Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of workbench Show documentation
Show all versions of workbench Show documentation
This plug-in contains the bulk of the Workbench implementation, and depends on JFace, SWT, and Core Runtime. It cannot be used independently from org.eclipse.ui. Workbench client plug-ins should not depend directly on this plug-in.
The newest version!
/*******************************************************************************
* Copyright (c) 2000, 2006 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.ui.actions;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
/**
* The abstract superclass for actions that listen to selection changes
* from a particular selection provider. This implementation splits the current
* selection along structured/unstructured lines, providing a convenient place
* to monitor selection changes that require adjusting action state.
*
* Subclasses must implement the following IAction
method:
*
* run
- to do the action's work
*
*
*
* Subclasses may reimplement either of the following methods:
*
* selectionChanged(IStructuredSelection)
* selectionChanged(ISelection)
*
*
*/
public abstract class SelectionProviderAction extends Action implements
ISelectionChangedListener {
/**
* The selection provider that is the target of this action.
*/
private ISelectionProvider provider;
/**
* Creates a new action with the given text that monitors selection changes
* within the given selection provider.
* The resulting action is added as a listener on the selection provider.
*
* @param provider the selection provider that will provide selection notification
* @param text the string used as the text for the action,
* or null
if there is no text
*/
protected SelectionProviderAction(ISelectionProvider provider, String text) {
super(text);
this.provider = provider;
provider.addSelectionChangedListener(this);
}
/**
* Disposes this action by removing it as a listener from the selection provider.
* This must be called by the creator of the action when the action is no longer needed.
*/
public void dispose() {
provider.removeSelectionChangedListener(this);
}
/**
* Returns the current selection in the selection provider.
*
* @return the current selection in the selection provider
*/
public ISelection getSelection() {
return provider.getSelection();
}
/**
* Returns the selection provider that is the target of this action.
*
* @return the target selection provider of this action
*/
public ISelectionProvider getSelectionProvider() {
return provider;
}
/**
* Returns the current structured selection in the selection provider, or an
* empty selection if nothing is selected or if selection does not include
* objects (for example, raw text).
*
* @return the current structured selection in the selection provider
*/
public IStructuredSelection getStructuredSelection() {
ISelection selection = provider.getSelection();
if (selection instanceof IStructuredSelection) {
return (IStructuredSelection) selection;
} else {
return new StructuredSelection();
}
}
/**
* Notifies this action that the given (non-structured) selection has changed
* in the selection provider.
*
* The SelectionProviderAction
implementation of this method
* does nothing. Subclasses may reimplement to react to this selection change.
*
*
* @param selection the new selection
*/
public void selectionChanged(ISelection selection) {
}
/**
* Notifies this action that the given structured selection has changed
* in the selection provider.
*
* The SelectionProviderAction
implementation of this method
* does nothing. Subclasses may reimplement to react to this selection change.
*
*
* @param selection the new selection
*/
public void selectionChanged(IStructuredSelection selection) {
// Hook in subclass.
}
/**
* The SelectionProviderAction
implementation of this
* ISelectionChangedListener
method calls
* selectionChanged(IStructuredSelection)
if the selection is
* a structured selection but selectionChanged(ISelection)
if it is
* not. Subclasses should override either of those methods method to react to
* selection changes.
*/
public final void selectionChanged(SelectionChangedEvent event) {
ISelection selection = event.getSelection();
if (selection instanceof IStructuredSelection) {
selectionChanged((IStructuredSelection) selection);
} else {
selectionChanged(selection);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy