I have an Eclipse RCP app I'm working on. It has some view-specific menus and one of the menu items is an item which I would like to display a tick next to when the corresponding functionality is enabled. Similarly, the next time the item is selected, the item should become unticked to reflect that the corresponding functionality is disabled.
My question is this: how do I set the toggle state of these menu items? I have an IHandler to deal with the event when the menu item is selected but I'm unsure how to update the GUI element itself.
Does the StackOverflow community have any thoughts on how I might solve this?
The solution involves having the command handler implement the IElementUpdater interface. The UI element can then be updated as so:
public void updateElement(UIElement element, Map parameters)
{
element.setChecked(isSelected);
}
updateElement is called as part of a UI refresh which can be invoked from the handler's execute command as so:
ICommandService service = (ICommandService) HandlerUtil
.getActiveWorkbenchWindowChecked(event).getService(
ICommandService.class);
service.refreshElements(event.getCommand().getId(), null);
Lots more info here (see Radio Button Command and Update checked state entries)
Maybe things have changed, maybe there's a difference between RCP and an Eclipse plug-in, but I just set the style of my action to toggle, and it handled the toggling automatically. You can see the source code on github.com.
<action
class="live_py.EnableAction"
id="live-py.enable.action"
label="Li&ve Coding"
menubarPath="pyGeneralMenu/pyNavigateGroup"
state="false"
style="toggle">
To see whether the feature is currently enabled, I just look up the action by its id, and then call isChecked().
Related
We are making a reading book application with React Native, but we have a problem about context menu. As you know many reading applications have a customized menu when user long press on text. There is a different menu instead of standart Android context menu (Copy-Paste-Select All)
For example: https://prnt.sc/w14pap (App XODO - https://play.google.com/store/apps/details?id=com.xodo.pdf.reader&hl=tr)
We want to show a menu like that.
We tried both in javascript side and react native plugin (https://github.com/Astrocoders/react-native-selectable-text), but we couldn't succeed.
Plugin supports only text, but we want to show icons only.
Could you help me please how can I make a context menu like this?
What we need?
When user long press on text, a customized menu must be shown
When user extend selection, the customized menu mustn't be lost or should be shown again.
When user long press or extend selection, we must get start and end indexes of selection.
We are using Text component because of we can colorize/underline some words of text. (TextInput doesn't support partial styling)
If it is not possible to do it without native bridge, is there any ready plugins in native side to bridge?
I'm developing an Eclipse plugin that has a toolbar menu item that will be enabling/disabling a feature. I know how to set the icon and tooltip text for it using the plugin.xml file, but I want to be able to change the icon and especially the tooltip text for it depending on the state of the project being worked on. I.e., if the feature is not enabled, I want an icon that shows turning the feature on along with tooltip text that says "enable feature", but if the feature is enabled, I want an icon that shows turning the feature off along with tooltip text that says "disable feature". Right now, the only option I can see is a generic icon and tooltip text that says "enable/disable feature", but that feels clumsy to me.
Edit to add: In response to indigomonkey's answer, which is a good one based on what I originally wrote, I would like to clarify that the behavior that is being enabled by the toolbar button is one that we really discourage ever disabling once it has been enabled (and a dialog pops up both to when enabling it to verify that they want to enable it because it should not be started lightly as well as to discourage them from disabling it once it is enabled). Because of this, I would like the icon to change to one that suggests "don't click on me".
If your command Handler implements IElementUpdater the updateElement method will be called whenever the command is run.
The UIElement parameter of updateElement has setIcon and setTooltip methods.
I would suggest that you choose to create your toolbar contribution as a 'checkbox' type, rather than the standard 'push'. This will mean that the button will toggle between a selected and unselected state, allowing you to equate this behaviour to the enabled and disabled state of your feature.
If you're using org.eclipse.ui.commands and org.eclipse.ui.handlers along with the org.eclipse.ui.menus extension point to contribute your button with a <command> element, you'll need to set its style attribute to check. You can then read and toggle the command's selection from inside the handler.
For more information, see http://blog.eclipse-tips.com/2009/03/commands-part-6-toggle-radio-menu.html.
In rcp E4 the solution seems to be a bit different. Since I first stumbled accross this question, I want to summerize/link the solution for future developers.
The relevant questions, where I found the answer are Switch Image of a Handler in a e4 rcp and RCP 4 Toggle a button in the toolbar.
Basically you need to add the argument MToolItem to your Execute-method like this:
#Execute
public void execute(final MToolItem item)
{
item.setIconURI("platform:/plugin/......");
//additional changes e.g. to the tooltip are also possible.
}
If you additionally give your toolbarbutton type checked
<elements xsi:type="menu:HandledToolItem" type="Check" ...
the item will automatically toggle between selected/unselected with each click. You can get the current state with
item.isSelected(). As a side effect, the toolbar icon will be highlighed in the selected-state. If you don't want that, you need to keep track of the state yourself.
I'm doing an app with lots of buttons and menus and I want enable and disable the buttons and menu items when the actions attached to them can or can not be performed. ie the save button and the save menu item only will be active when there are unsaved changes.
Question: How can I do this efficiently/Which is the correct way to do this?
One solution can be have a private variable for each button and menu entry and enable/disable it as needed.
Other solution can be get all the components of the JToolBar and the JMenu in an array and iterate over all them and enable/disable as needed.
But I think there are better solutions. Any help or guideline will be apreciated.
edit: The question is not how to enable/disable a single button or menu item, I will know how can I manage the state of all the buttons and menu items of the app. Which is the best way to achive this?. I have explained some solutions in which I have been thinkin, but none of them convince me.
The Action class already supports this. See the Action#isEnabled and Action#setEnabled methods. Calling the setter will fire an event, on which the UI on which this Action is set will enable/disable itself.
The Action can be seen as the model for your UI buttons/menu items/... . All state is stored and updated in the model, and the view needs to reflect this (MVC pattern).
1) to disable from clicking use:
JButton#setEnabled(boolean)
and
JMenuItem#setEnabled(boolean)
2) to disable ActionListener for some time use a private boolean variable inside
public void actionPerformed (ActionEvent ae) {
if (!enabled) return;
// rest of code
}
I've encountered next problem. I've got list of products and each product has button to open its QuickLook PopUp. I've created special event for this button, registered it in EventBus and trigger them. Everything seems good, but when I click one button, popup is shown not just for this item, but for all items, that are in list (I mean numbers of that "show"). This happens cause I have one Event-class for all these buttons, but can I somehow separate them one from another?
I just want to set some ID or something like that to every button and check this condition while triggering or (this would be even better) trigger only event, that I really need.
You don't need an EventBus for this. You can create a simple ClickHandler and attach it to your buttons. When you create a ClickHandler, you pass a product ID (or whatever you use to differentiate your products) to a method that shows your popup.
You can add productId to your Custom Event as a property. Event handler will check it and show only required product info.
I have a menu with a few JCheckBoxMnuItems. How do I ensure that the Menu stays open until I have done all my selections (i.e. checked the menuitems) and does not close on just clicking one of them?
I'd rather not try to change the normal menu behavior for an application or for a part of the menu tree. A User expects that the menu closes automatically after a menu item is clicked. And, if you kept the menu expanded, what kind of action would you invent to close it manually after you've done your last selection?
If there's a requirement to change more then one setting within one use case, then you should consider to provide a small dialog where the use can apply the changes and confirm them at once. I believe, that's more consistent with typical behaviors of UIs.
And it declutters the menu bar, you'll have just one 'setup' menu item instead of a dozen (?) check box actions :)
I guess menu's aren't supposed to allow multi-selection.
But you may offer keyboard shortcuts to set the menuitems without using the menu at all.
If the set-operation of your flags is a central aspect in your application, I would tend to use a dialog here. These are all suggestions which do not require to change the internal implementation of the existing controls, even though I know, that it would be possible in swing.
I agree that it is better to do this with standard UI. However, if do you want to add checkboxes that do not close the menu it is surprisingly easy:
JCheckBox checkBox = new JCheckBox("Text");
checkBox.setOpaque(false);
checkBox.setRequestFocusEnabled(false);
menu.add(checkBox);
This may not work on every look and feel and the check boxes will not line up with menu items in the same manner as JMenuItems but it seems to be a reasonable place to start.