How to enable and disable items in the menu contribution? - eclipse

I am trying to use a property tester on a menu contribution that I have done for my eclipse plugin.
Basically I added a new menu in the main menu bar (by extending menu:org.eclipse.ui.main.menu and adding a menu). I then added my commands in there with the correct handlers.
Everything works as expected.
My only problem is that I am not able to decide when to have them active.
I am trying to use the activeWhen for my handlers. i want them to be active when there is certain data on a server.
I tried using a property tester but it does not get called everytime. It only gets called when you select a different view.
What is the correct way of doing this?
EDIT: here is the code I am using
http://pastebin.com/TGtZaBtM
My property tester runs because I print out stuff when it does.
The only problem is that it does not run every time the menu is opened.
I would like it to run every time so that I can check if a user is logged in or not.

I'm probably answering late...
Anyway to do that, I always define a sourceProvider and some variable using the org.eclipse.ui.services extension point:
As example for a pause action somewhere in a toolbar, here is the piece of code of the source provider and its definition in the plugins.xml:
<extension
point="org.eclipse.ui.services">
<sourceProvider
provider="DataCollectionSourceProvider">
<variable
name="Pause"
priorityLevel="workbench">
</variable>
</sourceProvider>
</extension>
source provider:
public class DataCollectionSourceProvider extends AbstractSourceProvider {
public final static String ID = "DataCollectionSourceProvider";
public final static String ID_PAUSED = "Pause";
public final static String VAL_TRUE = "TRUE";
public final static String VAL_FALSE = "FALSE";
/**
* #return the instance of this source provider in this workbench
*/
public static DataCollectionSourceProvider getInstance() {
ISourceProviderService sourceProviderService = ISourceProviderService)PlatformUI.getWorkbench().getService(ISourceProviderService.class);
DataCollectionSourceProvider dcProvider = (DataCollectionSourceProvider)sourceProviderService.getSourceProvider(ID);
return dcProvider;
}
private boolean paused = false;
public DataCollectionSourceProvider() {
// do nothing
}
#Override
public Map<?, ?> getCurrentState() {
String value = null;
Map<String, String> map = new HashMap<String, String>(2);
// fake variable (my id)
map.put(ID, VAL_TRUE);
// paused state
value = paused ? VAL_TRUE : VAL_FALSE;
map.put(ID_PAUSED, value);
return map;
}
#Override
public String[] getProvidedSourceNames() {
return new String[] { ID, ID_PAUSED };
}
public void setPaused(boolean paused) {
this.paused = paused;
String value = paused ? VAL_TRUE : VAL_FALSE;
fireSourceChanged(ISources.WORKBENCH, ID_PAUSED, value);
}
}
Then on your org.eclipse.ui.handlers contribution, add the enableWhen by using the variable from its defined id:
<extension
point="org.eclipse.ui.handlers">
<handler
commandId="__your_command_id__">
<class
class="__your_handler_class__">
</class>
<enabledWhen>
<with
variable="Pause">
<equals
value="FALSE">
</equals>
</with>
</enabledWhen>
</handler>
</extension>
At last, if you want to update the handler/action state, you just have to call the following piece of code somewhere in your code
DataCollectionSourceProvider.getInstance().setPause(...)

At a quick glance: You are using 'activeWhen' in your handler. You can probably try using 'enabledWhen' in the XML
You can also look into overriding isEnabled() in your Handler. This will work, when your plugin is activated. Look into the docs for more information.

Related

How can I observe the changed state of model items in an ObservableList?

I have an ObservableList of model items. The model item is enabled for property binding (the setter fires a property changed event). The list is the content provider to a TableViewer which allows cell editing. I also intend to add a way of adding new rows (model items) via the TableViewer so the number of items in the list may vary with time.
So far, so good.
As this is all within an eclipse editor, I would like to know when the model gets changed. I just need one changed event from any changed model item in order to set the editor 'dirty'. I guess I could attach some kind of listener to each individual list item object but I wonder if there is a clever way to do it.
I think that I might have a solution. The following class is an inline Text editor. Changes to the model bean (all instances) are picked up using the listener added in doCreateElementObservable. My eclipse editor just needs to add its' own change listener to be kept informed.
public class InlineEditingSupport extends ObservableValueEditingSupport
{
private CellEditor cellEditor;
private String property;
private DataBindingContext dbc;
IChangeListener changeListener = new IChangeListener()
{
#Override
public void handleChange(ChangeEvent event)
{
for (ITableEditorChangeListener listener : listenersChange)
{
listener.changed();
}
}
};
public InlineEditingSupport(ColumnViewer viewer, DataBindingContext dbc, String property)
{
super(viewer, dbc);
cellEditor = new TextCellEditor((Composite) viewer.getControl());
this.property = property;
this.dbc = dbc;
}
protected CellEditor getCellEditor(Object element)
{
return cellEditor;
}
#Override
protected IObservableValue doCreateCellEditorObservable(CellEditor cellEditor)
{
return SWTObservables.observeText(cellEditor.getControl(), SWT.Modify);
}
#Override
protected IObservableValue doCreateElementObservable(Object element, ViewerCell cell)
{
IObservableValue value = BeansObservables.observeValue(element, property);
value.addChangeListener(changeListener); // ADD THIS LINE TO GET CHANGE EVENTS
return value;
}
private List<ITableEditorChangeListener> listenersChange = new ArrayList<ITableEditorChangeListener>();
public void addChangeListener(ITableEditorChangeListener listener)
{
listenersChange.remove(listener);
listenersChange.add(listener);
}
public void removeChangeListener(ITableEditorChangeListener listener)
{
listenersChange.remove(listener);
}
}

checkboxlist with prechecked in struts2

I am trying to display checkboxlist with prechecked items.
The values are saved in a list in my database and for "editing" the user should be able to select new options as well as "uncheck" some of the earlier selected one.
Thats why I need to translate my List back to the checkboxlist...
Any idea how that could possibly work?
Thanks a lot!
I'll give you an example of how you can do it.
[1]edit.jsp page:
It is the page where you want to display your checked checkboxlist:
<s:checkboxlist name="type" list="typeList" />
here "type" is name of checkbox and "typeList" is a list which is loaded from actionclass in my case.
[2]loadEditData method in action class:
public class your_action_class_name extends ActionSupport {
private List<String> type;
private List<String> typeList;
public List<String> getType() {
return type;}
public void setType(List<String> type) {
this.type = type;}
public List<String> getTypeList() {
return typeList;
}
public void setTypeList(List<String> typeList) {
this.typeList = typeList;
}
public String loadEditData(){
tpyeList=\\add whole checkboxlist here;
type.add("value that you want to prechecked");
return SUCCESS;
}
}
[3]struts.xml:
<action name="edit" method="loadEditData" class="your_action_class_Name" >
<result name="success">/edit.jsp</result>
</action>
Now your flow is like follow:
1st call the edit Action that will implement loadEditData method and on returning success display edit.jsp page with checkboxlist which have prechecked value.
Is this answer helpful?

ZK Fire Event from Java API

I have defined a custom Command in ZK and want to call it by clicking on a Menu Item.
I see that we can define a AuRequest object, but can't find a way to send this AuRequest like we do in JavaScript using zkau.send function.
is something possible at all? If not, is it possible to define the zkau.send in a JavaScript function and call it in MeunItem Click Event?
public class MyCustomCommand extends Command
{
protected MyCustomCommand(final String id, final int flags)
{
super(id, flags);
}
#Override
protected void process(final AuRequest request)
{
System.out.println("Menu Item Click");
}
}
register the command:
<bean id="myCustomCommand" class="com.test.commands.MyCustomCommand">
<constructor-arg value="onMenuEdit" />
<constructor-arg><util:constant static-field="org.zkoss.zk.au.Command.IGNORE_OLD_EQUIV"/></constructor-arg>
</bean>
and MenuItem Event
menuItem.addEventListener(Events.ON_CLICK, new EventListener()
{
#Override
public void onEvent(final Event event) throws Exception
{
final Tree tree = (Tree) parent;
final Treeitem treeitem = tree.getSelectedItem();
final AuRequest auRequest = new AuRequest(treeitem.getDesktop(), treeitem.getUuid(), "onMenuEdit", new String[]{});
//how to send the auRequest??
}
});
I can't comment on the use of the Command or AuRequest objects as you're suggesting here. I've never seen them used and have never used them myself. If there is a way to use them to solve this problem, hopefully you will get an answer. That said, there are other ways to achieve what you are looking to do.
As detailed in the Event Firing section of the Developer Reference, you can fire an event from the static Events object.
Events.postEvent("onMenuEdit", myTree, myDataEgTheTreeItem);
or..
Events.sendEvent("onMenuEdit", myTree, myDataEgTheTreeItem);
or..
Events.echoEvent("onMenuEdit", myTree, myDataEgTheTreeItem);
Any of these can be handled in a Composer using..
#Listen("onMenuItem = #myTree")
public void onTreeMenuItemEvent(Event event) {
// Handle event
}
Hope that helps.

Eclipse: Howto bind an activity to a view

For my Eclipse rcp application I want to use activities to show and hide some views. I read the Eclipse documentation about activities and tried to get a working example based on the 'Using expression-based activities' snippets from the documentation.
In the first step i created a new view and add a placeholder for it in my perspective class:
layout.addPlaceholder(View1.ID, IPageLayout.RIGHT, 0.5f, layout.getEditorArea());
Then i added my activity with a 'enabled when' expression and a binding:
<extension point="org.eclipse.ui.activities">
<activity id="org.project.activities.activity1" name="myActivity">
<enabledWhen>
<with variable="org.project.activities.sessionState">
<equals value="loggedIn"></equals>
</with>
</enabledWhen>
</activity>
</extension>
<activityPatternBinding
activityId="org.project.activities.activity1"
pattern="org.project.activities/org.project.activities.View1">
</activityPatternBinding>
In the last step i added my source-provider:
public class ActivitiySourceProvider extends AbstractSourceProvider {
public static final String SESSION_STATE = "org.project.activities.sessionState";
private static final String LOGGED_OUT = "loggedOut";
private static final String LOGGED_IN = "loggedIn";
private static final String[] SOURCE_NAMES = new String[] { SESSION_STATE };
private boolean loggedIn = false;
#Override
public Map<String, String> getCurrentState() {
Map<String, String> map = new HashMap<String, String>(1);
String value = loggedIn ? LOGGED_IN : LOGGED_OUT;
map.put(SESSION_STATE, value);
return map;
}
#Override
public String[] getProvidedSourceNames() {
return SOURCE_NAMES;
}
public void setLoggedIn() {
loggedIn = !loggedIn;
String value = loggedIn ? LOGGED_IN : LOGGED_OUT;
fireSourceChanged(ISources.WORKBENCH, SESSION_STATE, value);
}
}
When I start the test application my view 'View1' is hidden and when I toggle my variable the view is still hidden. To toggle my variable i used a handle and i don't receive any exceptions. I also tried to set my variable to explicit to 'loggedOut' at the application start, but i didn't worked either.
Did I missed something from the documentation?
Did you register your ActivitySourceProvider as source provider in an extension for extension point org.eclipse.ui.services? Otherwise it won't be used for expression evaluation.

DragSourceListener never called while dragging and dropping file inside eclipse project explorer

I am working on eclipse plugin that will allow a java bean to be dragged onto jsp file then on the drop event some code generators will be called.
I'm attempting to use the extension point "org.eclipse.ui.dropActions" but drag and drop listeners never get called .Is there any way to attach drag and drop listener to IFile object.
Am I on the right track with the DropActionDelegate?
Code:
DragListener
class DragListener implements DragSourceListener {
#Override
public void dragFinished(DragSourceEvent event) {
System.out.println("Finish");
}
#Override
public void dragSetData(DragSourceEvent event) {
PluginTransferData p;
p = new PluginTransferData (
"dream_action", // must be id of registered drop action
"some_data".getBytes() // may be of arbitrary type
);
event.data = p;
}
#Override
public void dragStart(DragSourceEvent event) {
// TODO Auto-generated method stub
System.out.println("Start");
}
}
DropActionDelegate
class DropActionDelegate implements IDropActionDelegate {
#Override
public boolean run(Object source, Object target) {
String Data= (String) target;
return true;
}
}
Plugin.xml
<extension point="org.eclipse.ui.dropActions">
<action
id="dream_action"
class="newdreamfileplugin.wizards.DropActionDelegate">
</action>
</extension>
Thanks.
Solved it.Finally i created my own navigator using org.eclipse.ui.navigator.navigatorContent extension which have a property dropAssistant.