How to prevent empty context menu from showing in GXT - java

I have a TreePanel which shows different kind of objects hierarchically. Region, City, Location...
I want to be able to show different context menu items in different levels. For example: miR for Region, miC for City, miL for Location...
I used this snipped to achieve that dynamic structure:
contextMenu.addListener(Events.BeforeShow, new Listener<MenuEvent>() {
#Override
public void handleEvent(MenuEvent be) {
//First make all menu items invisible
List<Component> menuItems = contextMenu.getItems();
for (Component c : menuItems) {
c.setVisible(false);
}
//And make apprepriate menu items visible
TopologyTreeElement s = tree.getSelectionModel().getSelectedItem();
if (s instanceof TopologyTreeElement.Region) {
miR.setVisible(true);
}
if (s instanceof TopologyTreeElement.City) {
miC.setVisible(true);
}
}
});
But, in any level if all of the items are invisible, it shows an empty box. I want it not to show the menu totally. I tried adding this code snippet to the method, but it gave no help.
//Do not show menu if no menu item is invisible
boolean isMenuShouldBeVisible = miC.isVisible() || miR.isVisible();
if (!isMenuShouldBeVisible) {
be.preventDefault();
be.stopEvent();
}
Anyone can suggest a different approach?

Since you are listening to the BeforeShow event, you are allowed to cancel the event and stop the actual Show event from happening. Check to see if all items are invisible, and if so, call be.setCancelled(true).
Any event that starts in Before can be used to cancel the later event - this is why these before- events exist at all.

Related

Disable dragging of selected object in Piccolo2d?

I know Piccolo2d is an old project, but I have a couple of questions.
1) Is it possible to disable dragging a selected object? For this particular use case, I only want to select an object, not move or delete it. I know I can disable deletion using:
this.selector.setDeleteKeyActive(false);
But I don't see an option to disable dragging. Is the only option to override the drag functionality in the event handler?
2) Is there no way to have the selection handler active at the same time as the pan/zoom handlers? It seems a bit archaic to disable pan/zoom when you want to support object picking. Or do I have to create my own handlers?
My current code is:
...
this.pluginContext.getCanvas().setPanEventHandler(null);
this.selector = new PSelectionEventHandler(this.mapLayer.getNode(), this.mapLayer.getNode()) {
};
this.selector.setDeleteKeyActive(false);
this.pluginContext.getCanvas().addInputEventListener(this.selector);
PNotificationCenter.defaultCenter().addListener(this, "nodeSelected",
PSelectionEventHandler.SELECTION_CHANGED_NOTIFICATION, this.selector);
...
public void nodeSelected(final PNotification notification) {
logger.debug("Selection - " + this.selector.getSelection().toString());
}
In true developer tradition, I found an answer.
My pickable nodes are all grouped under individual grouping nodes. This lets me add an event listener on the grouping node, like this:
groupNode.addInputEventListener(new PBasicInputEventHandler() {
public void mouseReleased(final PInputEvent event) {
PNode node = event.getPickedNode();
if (node != null) {
onNodeSelected(node); // your logic here
}
}
});
The beauty of this is that the event contains the picked node that is grouped under that group node. Perfect! Also, you don't have to disable the pan/zoom handlers.
The only downside is that this doesn't give you the selection decorator around the node. Can't win 'em all!

How to delete a CheckBox from a TableView in JavaFX?

I'm writing a seating chart program using JavaFX. I have a table that keeps a list of students together that holds their name, grade, and whether they are present or absent (using a checkbox). I have a delete button that allows me to delete the students from the list. This works fine, however, whenever I delete the student object, the checkbox does not go along with it. I'm not sure what I would need to add to get that to work. Here is a snippet of the delete code. There are also two images below that show my problem. This is my first post so please let me know if I missed something. Please help! Thanks!
ObservableList<Student> items, sel;
items = currentTable.getItems();
sel = currentTable.getSelectionModel().getSelectedItems();
Student s = new Student("", "", 0, "");
for (Student p : sel) {
items.remove(p);
s = p;
}
Before Delete
After Delete
This has nothing to do with the delete or remove method. It has to do with what you did in TableColumn.setCellFactory().
To get the checkbox you shown, you should have used (in general) one of the two methods:
Overriding updateItem() in TableCell while setting Cell Factory
There is this empty parameter in updateItem() which indicates whether the row is empty or not. You need to use that to determine when not to show the checkbox.
column.setCellFactory(col -> {
return new TableCell<Foo, Boolean>() {
final CheckBox checkBox = new CheckBox();
#Override
public void updateItem(final Boolean selected, final boolean empty) {
super.updateItem(selected, empty);
if (!this.isEmpty()) {
setGraphic(checkBox);
setText("");
}
else {
setGraphic(null); // Remove checkbox if row is empty
setText("");
}
}
}
}
Using CheckBoxTableCell
JavaFX API has this convenient class CheckBoxTableCell that would do all these for you. Most people find this class hard to use because there are 2 things that you need to ensure to use it correctly:
The TableView that the column belongs to must be editable.
The TableColumn itself must be editable.
Example:
tableView.setEditable(true);
tableColumnSelected.setCellFactory(CheckBoxTableCell.forTableColumn(tableColumnSelected));
tableColumnSelected.setEditable(true);
As for whether which entry you want to be removed with the delete button, you just need to remove the correct items from the TableView.

Osmdroid: show and hide markers description on click

I am programming an app in Java using Android Studio.
I already displayed a map using osmdroid, added some overlays to display markers on special locations and added a title & a description to the markers.
Now I display the title & description of the marker on click using the setFocusItemsOnTap method.
My problem is that I am not able to hide the title & description of the marker on a second click (so if its already shown). Is there any way to do this?
Or if thats not possible is there a way to only display the title & description of one marker at once using the setFocusItemsOnTab methode?
public static List<OverlayItem> items = new ArrayList<OverlayItem>();
//[...]
items.add(new OverlayItem("uid1","Title", "Description", new GeoPoint(51.398,6.875)));
//[...]
List<OverlayItem> currentList;
currentList = new ArrayList<OverlayItem>();
currentList.add(items.get(i));
//[...]
final ItemizedOverlayWithFocus<OverlayItem> mOverlay = new ItemizedOverlayWithFocus<OverlayItem>(this, currentList, new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
#Override
public boolean onItemSingleTapUp(final int index, final OverlayItem item) {
//here it should decide if the title & description is already shown or not. (true => hide it, false => display it)
return true;
}
#Override
public boolean onItemLongPress(final int index, final OverlayItem item) {
return false;
}
});
mOverlay.setFocusItemsOnTap(true);
I have to use these parts of the code, because i wanted to add different markers and i wanted to be able to focus all of them. Also i need to be able to add them to a dynamic list during runtime.
Thanks for your help!
If you're sticking with ItemizedIconOverlay, I believe you can only have one popup at a time. You would have to subclass ItemizedIconOverlay and override the draw method to support rendering popups for multiple items at a time.
I think you'll have better luck with the Marker class. It already supports the click to close function. Again, only a single marker popup can be displayed at a time. This can be overridden by supplying your own InfoWindow instances for each marker.

The submenu is present in the UI (I'm able to see it). But, when I try to access it programmatically, I'm not getting that submenu

We are having a view, with pulldownmenu items (Group by, Grouped by).
'Group by' has sub-menu which are the columns in the views. When any of the submenu is selected, THe view entries will be grouped with the column and also there will be a entry added to 'Group by' Menu.
We are having a util method of signature:
public static void clickPullDownMenuItem(String viewID, String menu){
display.asyncExec(new Runnable() {
#Override
public void run() {
IViewPart viewPart = getView(viewID);
MenuManager mainMenuManager = (MenuManager) viewPart.getViewSite().getActionBars().getMenuManager();
IContributionItem[] items = mainMenuManager.getItems();
//iterate to through the items and invoke the action of the item.
}
Thread.sleep(2000);
}
I use the below calls to simulate the same.
1. clickPullDownMenuItem(viewID, "Group by/Resource")
2. clickPullDownMenuItem(viewID, "Grouped by/Resource")
After the first statement is executed I am able to see the 'Resource' entry in the 'Grouped by' menu.
But, when the second statement is being executed, I'm getting no items under 'Grouped by' in the above method.
The thing I don't understand is the submenu is present in the UI (I'm able to see it). But, when I try to access it programmatically, I'm not getting that submenu.
Use the findUsingPath to find a contribution item matching a path - particularly one containing a '/':
IMenuManager manager = viewPart.getViewSite().getActionBars().getMenuManager();
IContributionItem item = manager.findUsingPath(menu);

Which radio button is selected in a button group

What can I do to get which radiobutton is selected on a buttongroup without doing this:
if (jRadioButton1.isSelected()) {
//...
}
if (jRadioButton2.isSelected()) {
//...
}
if (jRadioButton3.isSelected()) {
//...
}
if (jRadioButton4.isSelected()) {
//...
}
You can get the ButtonModel for the selected button via the getSelection() method of ButtonGroup. I don't know how you can avoid conditionally branching on the selected button though, unless you have some sort of ancillary data structure mapping from ButtonModel to actions to perform, for instance. If you had that, then you could just fire the action based on the returned ButtonModel.
Darryl's Select Button Group has a getSelectedButton() method.
I know the question was posted long back. Anyway, we can use the setActioncommand function. while creating the radio button, setActionCommand could be invoked to set the action command value, which could be used to refer to the radio button that was selected.
jRadioButton1.setActionCommand("jRadioButton1");
jRadioButton2.setActionCommand("jRadioButton2")
.
.
String button_name = ((JToggleButton.ToggleButtonModel)button_group.getSelection()).getActionCommand();
ButtonGroup class does not provide a method to identify the currently selected button (inherited from AbstractButton) in the group if that is your intention. It only has clearSelection() method to clear the selected state of all buttons in the group (with exception for JButton and JMenuItem which don't have select/deselect button state).
One solution I can think of is to use a special variable or field (AbstractButton, JRadioButton or JRadioButtonMenuItem if it is in a menu item) to identify which one is currently selected by updating it inside each AbstractButton's action listener method (make sure to validate user clicks since it can be triggered more than once!). Use the variable (by typecasting it - for AbstractButton only) in other method(s).
Other than that, nope...you will need to do conditional branching.
For dealing with a button group bg, you can get the buttons by calling the button group's getElements() method, and using that as the parameter for the Collections.list() method, just save the result in an arraylist. From there it is relatively simple to retrieve the correct button.
ArrayList<AbstractButton> arl = Collections.list(bg.getElements());
for (AbstractButton ab : arl) {
JRadioButton jrb = (JRadioButton) ab;
if (jrb.isSelected()) {
return jrb;
}
}

Categories

Resources