Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » How to enable / disable button on toolbar?
How to enable / disable button on toolbar? [message #331715] Thu, 18 September 2008 07:54 Go to next message
Prasanna K is currently offline Prasanna KFriend
Messages: 78
Registered: July 2009
Member
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.
Re: How to enable / disable button on toolbar? [message #331730 is a reply to message #331715] Thu, 18 September 2008 14:33 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

In theory you can use action/selection or action/enablement for some
minimal declarative enabled state. It is the older platform expression
language.

I believe we discussed this on IRC and in commands+handlers you can do
something like:

<with variable="selection">
<and>
<count value="1"/>
<iterate> <!-- you might want to set the ifEmpty attribute -->
<adapt type="org.eclipse.core.resources.IResource">
<instanceof value="org.eclipse.core.resources.IProject"/>
</adapt>
</iterate>
</and>
</with>
[/xml]

Later,
PW


--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/ganymede/index.jsp?topic=/org.eclips e.platform.doc.isv/guide/workbench.htm


Re: How to enable / disable button on toolbar? [message #331743 is a reply to message #331730] Fri, 19 September 2008 09:38 Go to previous message
Prasanna K is currently offline Prasanna KFriend
Messages: 78
Registered: July 2009
Member
Paul Webster wrote:
> I believe we discussed this on IRC and in commands+handlers you can do
> something like:

Yes indeed.
Thanks a lot.
Previous Topic:Is it possible to override VariableLabelProvider for varibles view
Next Topic:Custom Eclipse Distribution
Goto Forum:
  


Current Time: Fri Feb 07 09:11:51 GMT 2025

Powered by FUDForum. Page generated in 0.03936 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top