Disable/enable toolbar menu item depending on toggle sate - java

I am working with enable/disable state of toolbar items.
I need to disable second menu until the first one is in toggle on state. For this I try to get toggle state of the first menu item. But the state is always NULL.
ICommandService service = (ICommandService) PlatformUI.getWorkbench().
getService(ICommandService.class);
Command command = service.getCommand("com.commandID");
State state = command.getState("org.eclipse.ui.commands.toggleState");
Somebody knows what is the problem? Maybe there is a better way to solve it?

Have you registered ToggleState for your command?
Since it is possible to have multiple menu contributions for the same
command, we have to keep track of the state in a central place.
Imagine a toggle button triggerable from the main menu and a views
toolbar. The state of these buttons are keept in sync by storing the
state directly in the command. The key to this is the
org.eclipse.jface.commands.ToggleState.
<command id="org.eclipse.example.command.toggle"
name="Toggle Me">
<state class="org.eclipse.jface.commands.ToggleState"
id="org.eclipse.example.command.toggleState" />
</command>
Please read this blog post for more details.

Related

Prevent chip from double click

I have a chipgroup and some chips inside. I loop through this chipgroup, get every chip and add a checkedChangeListener for it.
for (int i = 0; i < chipGroup.getChildCount(); i++) {
final Chip chip = (Chip) chipGroup.getChildAt(i);
chip.setOnCheckedChangeListener((buttonView, isChecked) -> {
//My code
});
}
So when I open my app and click on chip, e.g. chip1, it selects it. Then when I click on that chip1 second time, it also fires the checkedChangeListener and runs my code inside it. The isChecked status is not helping, because it just changes it value like true->false->true->false on every click. But I just don't want to run my code inside onCheckedChangeListener, if the chip is already checked.
One solution I think is to save my selected chip inside fragment or ViewModel. What's your solution?
I believe you can just set a setOnCheckedChangeListener on the chipGroup instead of each individual chip. That listener will pass in an ID and isChecked as parameters. I believe using this callback on the group will be help resolve your issue. Hopefully that helps with the issue you're having.
More info on https://material.io/components/chips/android#using-chips

RCP visiblewhen programmatically (Java)

I have a command into the plugin.xml which will add a new menu button. This button should not be visible all the time, hence I would like to check a complex condition from Java code to decide when it have to be visible.
I know that there is a visiblewhen and a hidewhen possibility, but I don't know how can let a Java class/method to make the decision.
For this check the enabled state of the command is used, which is determined by the return value of IHandler.isEnabled().
In the plugin.xml the contribution of the command to the menu has to have the visibleWhen element and checkEnabled="true". In Eclipse you can right click the command contribution and add visible when, in the plugin.xml it looks like this:
<command
commandId="...">
<visibleWhen
checkEnabled="true">
</visibleWhen>
</command>
To enable/disable the command you have to implement the isEnabled() method from org.eclipse.core.commands.IHandler (or override from AbstractHandler) in your command handler and return false, if the menu entry should be hidden.

NavigationView: Mark menu as dirty

I am using the android.support.design.widget.NavigationView. During the lifetime of this view the menu items get changed (removed, renamed and added) dynamically. However these changes do not get displayed directly, but only after the view has been destroyed and created again (e.g. on orientation change).
Is there a way force it to update the menu?
Adding a dynamic menu to NavigationView is currently bug on Design Support library. And I see that the someone has been report it to android bug source tracking. So wait till the bug will fixed. But if you want the temporary solution you can do it. First add your dynamic menu ..
navView = (NavigationView) findViewById(R.id.navView);
Menu m = navView.getMenu();
SubMenu topChannelMenu = m.addSubMenu("Top Channels");
topChannelMenu.add("Foo");
topChannelMenu.add("Bar");
topChannelMenu.add("Baz");
After adding your menu just write below code ..
MenuItem mi = m.getItem(m.size()-1);
mi.setTitle(mi.getTitle());
https://code.google.com/p/android/issues/detail?id=176300

How to remove unwanted menu contributions in eclipse rcp application?

I have made a eclipse RCP application, everything is working fine but i recently noticed the Refractor option in menu. I would like to get rid of it. I have the following in ActionBarAdvisor.java:
#Override
protected void fillMenuBar(IMenuManager menu) {
menu.add(createFile());
menu.add(createEdit());
menu.add(createNavigate());
menu.add(createProject());
menu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
menu.add(createWindow());
menu.add(createHelp());
}
The above functions add actions to menu as:
edit.add(undoAct);
and also undoAct is defined as:
private IWorkbenchAction undoAction
makeActions function has contents as:
#Override
protected void makeActions(IWorkbenchWindow window) {
undoAction = ActionFactory.UNDO.create(window);
undoAction.setText("Undo Menu");
register(undoAction);
}
I found a suggestion which said to use hideActionSets to hide the menu. But I could not hide the entire menu but just its actions!
Remove "File, edit,...etc" menus from Eclipse RCP application
How to remove Refractor option now?
Thank you.
You can use activities, as described here.
First, you will need to find the ID of the menu:
Use the Plug-In Spy
The first way is to use the Plug-In Spy. Press alt-shift-F2 and click on a
menu item or toolbar button that you want to be hidden. If there is an ID
string under the heading "active action definition identifier" then you are
in luck. This item has been added using the Command Extension and you can
use this ID as the pattern argument for the Activities Extension. But not
all items that have been added using the Command Extension present their ID
string to the plug-in spy.
As a side note, the ID strings are period separated. For instance the ID for
a button might be "org.eclipse.ui.navigate.backwardHistory". Regular
expressions use the period to stand for any character. Luckily the period
used as a wild card matches with actual period characters so you don't need
to escape them if you don't want to. I find it makes it a bit easier to read
if they are not escaped and it is highly unlikely it will cause any
ambiguous matches.
Use the Plug-In Registry and plugin.xml files
The second way is to use the Plug-In Registry. You can open this view by
going to:
Window/Show View.../Other/Plug-in Development/Plug-In Registry
What you would like to do is to try to get a couple pieces of information:
a) the plugin that is contributing the UI element
b) information about what kind of extension the plugin is using to create
the UI element
If there is a very unique word associated with the UI element or its tool
tip then you can use this in the Plug-In Registry's filter field to try to
nail down which plug-in is contributing the UI element. The filter field is
not a very powerful tool so it can be a bit frustrating to use. It does not
allow wildcards and does not match space characters.
When you track down which plug-in is contributing the UI element then you
open the the plug-in in question from the Plug-Ins view which is found
grouped with the Package Explorer in the Plug-in Development perspective.
Then go to the Extensions tab and search for the ID string which can usually
be found in either a usage of the Command or ActionSet extension. If the UI
element is added using an ActionSet then you prefix the plug-in ID to UI ID
in the pattern argument given to the Activities Extension. For example
org.eclipse.ui.actionsets.foo becomes the pattern
org.eclipse.ui/org.eclipse.ui.actionsets.foo.
Then create a new Activity which will never be activated and a corresponding activityPatternBinding with the id you found in the last step. It will look like this in your plugin.xml:
<extension point="org.eclipse.ui.activities">
<activity id="myActivity" name="MenuHidingActivity">
<enabledWhen>
<with variable="activePartId">
<equals value="nonExistentPartId"></equals>
</with>
</enabledWhen>
</activity>
<activityPatternBinding activityId="myActivity" pattern="menuItemID">
</activityPatternBinding>
</extension>

setVolumeControlStream not working when a popup window is displayed

I have this.setVolumeControlStream(AudioManager.STREAM_MUSIC); at the start of all activities in my application so when the user presses the volume up or down buttons, he controls the media volume.
I have a popup window in my program and when that appears the user can no longer control the volume.
Looking at similar questions it seems that setting up onKeyup/down listeners can interfere with the process - but I have not set any up - the only listeners I have for the popup window are setOnClickListeners for the buttons and a setOnDismissListener for the window.
How can I fix this?
Looks like you have to call setOwnerActivity on the Dialog object.
Documentation from the method:
Sets the Activity that owns this dialog. An example use: This Dialog will use the suggested volume control stream of the Activity.
While not tested, this should do the trick. There is also the possibility to use setVolumeControlStream.
I had been creating the popup window with
my_popup_window = new PopupWindow(layout, x, y, true);
I then change it to this...
my_popup_window = new PopupWindow(layout);
my_popup_window.setWidth(x);
my_popup_window.setHeight(y);
and the volume control started to work again. I don't understand why - but it worked.
I just do this pop.setFocusable(false). and it worked.
though the Mick's answer didn't work for me, this is for posterity.
//Declaration
PopupWindow mWindow;
...
//Constructor
mWindow = new PopupWindow(context);
...
//Prepare to Show
mWindow.setContentView();
mWindow.setBackgroundDrawable();
mWindow.setFocusable(false);
...
setting setFocusable to false helped my activity capture onKeyDown() again.

Categories