EclipseRCP - Update menu items programmcatically - java

Ik have used this tutorial to define a menu that is built up during runtime. But the next step I want to take is when some event occurs I want to re-build this menu programmatically, for instance by saying to the menu manager, refresh or something like that? Any idea how I can do this?

You can tell the menu manager to remove all items each time the menu is shown, giving you the opportunity to rebuild your menu:
MenuManager mm = new MenuManager();
mm.setRemoveAllWhenShown(true);
mm.addMenuListener(new IMenuListener() {
public void menuAboutToShow(IMenuManager manager) {
if(giraffes) {
Action giraffeAction = new Action("Giraffe") {
public void run() {
// do giraffe-y stuff
}
};
mgr.add(giraffeAction);
}
}
});
Control myControl = myViewer.getControl();
myControl().setMenu(mm.createContextMenu(myControl));

Instead of using an ExtensionContributionFactory, use org.eclipse.ui.menus to add a dynamic element to the menu you want. The implementation class you provide subclasses org.eclipse.ui.actions.CompoundContributionItem and you will have the opportunity to rebuild that part of the menu on every menu open.
EDIT: add pointer to example.
See http://git.eclipse.org/c/platform/eclipse.platform.ui.git/tree/examples/org.eclipse.ui.examples.contributions/plugin.xml#n666 for the plugin.xml difference. The implementing class is also contained in that plugin.

Related

Toggling Menu Items in JavaFX

I have created a class which extends JavaFX's MenuBar that creates a menu bar for my application.
By default I won't specialized operations, like opening/saving a file and running a simulation, to be disabled (and they are). When a user runs the app they can select an item in the menu File>New>, and based on which component they select it will toggle on the appropriate menu options.
I was planning on doing this by having each component give a list of which items it toggles on, and then activating the appropriate items when the component is created.
However, I cannot access the list of menus from within in a function (am trying to do it with this.getMenus() but from within the function the only function that is recognized it this.getClass()).
Does anyone know why I cannot call getMenus() and how I could get access to it?
Alternatively, if you have a better idea for how I can toggle these menu items, I'd love to hear. I don't think this is a good way to do it, but it is the best idea we have come up with.
private void fileNew()
{
Menu fileNew = new Menu("New");
menuFile.getItems().add(fileNew);
for(String k: CLHM.keySet())
{
CComponent comp = CLHM.get(k);
if(comp.supportedFeatures().contains((new SupportsNew())))
{
MenuItem i = new MenuItem(comp.getName());
fileNew.getItems().add(i);
i.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent event)
{
CComponent ctemp = CLHM.get(i.getText());
ArrayList<String> menuItems = (ArrayList) ctemp.getMenuItems();
for (String s : menuItems)
{
Scanner scanner = new Scanner(s).useDelimiter("\\s>\\s");
String menu = scanner.next();
//Menu temp = this.getMenus();
/*
Here the program will parse the string of the
Menu path (e.g. File>Open) and activate the
relevant item, if it exists.
*/
}
borderPane.setCenter((Node) ctemp);
}
});
}
}
}
When you use this inside an anonymous class, it actually refers to the anonymous class instance. So in your case, this is an instance of EventHandler, which is why there are so little methods that you can call (because it is an interface type).
What you are looking for is YourExtendedMenuBar.this.getMenus(). This will tell the compiler that you are looking for the enclosing instance. Alternatively, you can simply drop the this keyword (i.e. getMenus()). Doing so will allow you to use/call any accessible members of the anonymous class and its enclosing class.
On the side note, if you replaced that anonymous class with a lambda expression, then this would have meant YourExtendedMenuBar. It is not possible to access any members of the class that the lambda expression represents, at least not directly.
P.S. I have no idea what your toggling is all about, so I can't answer until I figured out what you mean.

Add listener jDateChooser

i haved create this metod for add listeners in my dataChooser.
public final void añadirEsccuhaDataChoser() {
jDateChooser1.getDateEditor().addPropertyChangeListener((PropertyChangeEvent e) -> {
if ("date".equals(e.getPropertyName())) {
listarviajes1();
}
});
this.add(jDateChooser1);
}
but the result who i have obtened after run the project is this.
datachooser moved
but the my original desing is this.
data chooser original position
the method i haved situate into the public vReservas().
situation method
What can I do to prevent the dataChooser from moving?
this is a solution.
solution
The problem is that I created a method, instead of adding the listener in the JdateChooser's internal code.
The code to add the listener is the same, just place it in the custom code option of the jDateChooser.
the ubication of listener

Eclipse plugin: Properties information not shown correctly when treeview is part of a tabfolder/tabitem

I am developing an eclipse plug in with a tree viewer. Initially I had a single treeview of which I have displayed information of some elements in the standard eclipse properties tab. That worked without problems.
I have followed an example where I implement the IPropertySource and IAdapterFactory. In the method createPartControl() of the view I call
getSite().setSelectionProvider(searchViewer);
which registers the properties.
Now I have added an swt tabfolder item to the plug in. Now In every new tabitem a treeview is displayed. That works fine, but the information in the properties tab are not shown correctly anymore. There's a strange behaviour though. On the tree elements which are of interest I have also added a doubleclick listener to do other things. After I double click an entry and right after single click on another element, the properties are shown for the doubleclicked element?!
I guess the problem is with the SelectionProvider. But I was not able to figure out how to implement it correctly now
The properties view always shows the properties for the object that the selection provider says is the current selection.
If you have multiple tree views on several tabs you will have to write a custom selection provider (ISelectionProvider) that knows which tree is currently active and provides the appropriate selection.
For example, the following is a selection provider used by the JDT code for some things which you could use as a base:
public class SimpleSelectionProvider implements ISelectionProvider {
private final ListenerList<ISelectionChangedListener> fSelectionChangedListeners;
private ISelection fSelection;
public SimpleSelectionProvider() {
fSelectionChangedListeners = new ListenerList<>();
}
#Override
public ISelection getSelection() {
return fSelection;
}
#Override
public void setSelection(ISelection selection) {
fSelection= selection;
for (ISelectionChangedListener listener : fSelectionChangedListeners) {
listener.selectionChanged(new SelectionChangedEvent(this, selection));
}
}
#Override
public void removeSelectionChangedListener(ISelectionChangedListener listener) {
fSelectionChangedListeners.remove(listener);
}
#Override
public void addSelectionChangedListener(ISelectionChangedListener listener) {
fSelectionChangedListeners.add(listener);
}
}
(org.eclipse.jdt.internal.ui.actions.SimpleSelectionProvider)
You would have to make all your trees call the setSelection method when selections change.

how to reload or refresh a tab's content base on actions in vaadin

The content of the tab is formed and displayed when the application is loaded. Later the content of the tab may be changed by other actions. I want to show the newer content after each action. And each time when I click the tab sheet, the content should be refresh/updated. But I failed.
//the content of the tab from the "reprintsTab" class
//in the "reprintsTab" it query data from database and print out
//later I update the data in the database from somewhere else, and I want the tab shows the new content
//I want to click the tab sheet to reload the "reprintTab" class and print out the new content
//here is what I did:
public TabSheet sheet;
//add tab and add the content from "reprintTab" into this tab
sheet.addTab(new reprintsTab());
//add the listener
sheet.addListener(new TabSheet.SelectedTabChangeListener() {
#Override
public void selectedTabChange(SelectedTabChangeEvent event) {
//I know it does not work, because it only reload the class. but not put the content under the tab I want
new reprintsTab();
}
});
What should I do? please help me, thanks.
You can use TabSheet.replaceComponent method to do this:
//Field to store current component
private reprintsTab currentComponent;
//during initialization
currentComponent = new reprintsTab();
sheet.addTab(currentComponent);
sheet.addListener(new TabSheet.SelectedTabChangeListener() {
#Override
public void selectedTabChange(SelectedTabChangeEvent event) {
reprintsTab newComponent = new reprintsTab();
sheet.replaceComponent(currentComponent, newComponent);
currentComponent = newComponent;
}
});
Also, you might want to reload this tab only when it's shown:
sheet.addListener(new TabSheet.SelectedTabChangeListener() {
#Override
public void selectedTabChange(SelectedTabChangeEvent event) {
if (event.getTabSheet().getSelectedTab() == currentComponent) {
//here goes the code
}
}
});
This should work for you, but I would suggest a cleaner approach: implement reprintsTab as a container for components, create method reload or buildInterface method to refresh its' state, so you can just call:
currentComponent.reload();
when you need to update interface.
Also, I hope reprintsTab is just an example name, java class names starting with lowercase letter look ugly.

Java Gui Menubars

I'm practicing my gui skills with Java and I have been doing menus and menu bars.
They make sense to me just fine but I have a question about where I can learn more about them.
The basic menus I have done, the ActionListener function actionPerformed has to be in the same class as the menu, and the item that the menu is changing also has to be in the class as the menu.
What If I want to have a menu that affects a JPanel that is created by a constructor from another class and placed in my frame.. I'm not sure how the menu can change components of it.
Any tips, hints or sites you guys have found helpful would be great, thanks in advance.
I find it useful to wrap menubar actions in an Action object. This encapsulates:
The name and icon of the action
If it is enabled or disabled
(for a checkbox action) if it is selected
The keyboard shortcut for the action
The implementation of the action listener
You would define a subclass of AbstractAction in the class whose data that action acts on. Then define a getter for that action so that your menu building code can get it. You can add the Action directly to a menu - a MenuItem is constructed automatically for it.
The other advantage of actions is that that same action can be used in a button, toolbar, etc.
class ModelClass {
int value;
Action incAction = new AbstractAction("Increment") {
public void actionPerformed() {
value++;
setEnabled(value < 10);
}
};
public Action getIncAction() {
return incAction;
}
}
class UIClass {
void buildMenu() {
JMenu menu = new JMenu("Model");
menu.add(model.getIncAction());
}
}

Categories