How to enable / disable button on toolbar? [message #331715] |
Thu, 18 September 2008 03:54  |
Eclipse User |
|
|
|
Hello,
I have added a button on toolbar using the org.eclipse.ui.actionSets
extension.
I want it to be enabled only if a project
(org.eclipse.core.resources.IProject) is selected inside the workbench.
How do I do that?
I googled for this problem and came across this thread :
http://dev.eclipse.org/newslists/news.eclipse.platform/msg77 563.html
If I follow the solution in this thread, my button on the toolbar remains
disabled by default but gets enabled if user selects some resource
(project / folder / package / file).
Only when the user clicks on the button for the first time, the enable /
disable feature starts working. That's not what I want.
The button should be disabled by default and I should be able to enable it
only when user selects a project and disable it otherwise.
Here are details :
Plugin.xml :
<extension
point="org.eclipse.ui.actionSets">
<actionSet
id="com.abc.actionSet1"
visible="true"
label="Test button">
<action
class="com.abc.ActionClass"
icon="icons/logo.gif"
id="com.abc.actionId1"
label="Test action"
enablesFor="1"
toolbarPath="org.eclipse.ui.workbench.file/new.ext"
tooltip="Test the button's action">
</action>
</actionSet>
</extension>
com.abc.ActionClass :
public class ActionClass implements
IWorkbenchWindowActionDelegate {
ISelection selection;
IProject selectedProject;
public void dispose() {
}
public void init(IWorkbenchWindow window) {
}
public void selectionChanged(IAction action, ISelection selection) {
this.selection = selection;
IStructuredSelection sSelection = (IStructuredSelection) selection;
Object[] selectedObjects = sSelection.toArray();
for(Object selectedObject : selectedObjects) {
if(selectedObject instanceof IAdaptable) {
Object adaptableObject =
((IAdaptable)selectedObject).getAdapter(IResource.class);
if(adaptableObject instanceof IProject) {
selectedProject = (IProject)adaptableObject;
break;
}
else {
action.setEnabled(false);
}
}
}
}
public void run(IAction action) {
// use selectedProject;
}
Is there any other way of adding a button to the toolbar that'll give me
this flexibility?
Thanks.
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03707 seconds