How to add toolbar to java text hover eclipse - java

I am tring to create my own text hover plugin for eclipse.
I success to write my own code in my hover, but I try to add a toolbar to the hover (inside the new tooltip opened).
I read that I need to use the getHoverControlCreator function, and I managed to add the toolbar manager that I see when the text hover is opened while running the plugin,in the debbuger I can see that the ToolBarManger has the ToolBar that has the ToolItems, but I can't see them in the real text hover when I opened it.
this is my code:
public IInformationControlCreator getHoverControlCreator() {
return new IInformationControlCreator() {
public IInformationControl createInformationControl(Shell parent) {
ToolBar tb = new ToolBar(parent, SWT.HORIZONTAL);
ToolBarManager tbm = new ToolBarManager(tb);
DefaultInformationControl dic = new DefaultInformationControl(parent, tbm);
ToolItem ti = new ToolItem(tb, SWT.PUSH);
ti.setText("hello");
tb.update();
tb.redraw();
tbm.update(true);
parent.update();
parent.redraw();
parent.layout();
return dic;
}

This is what one of the Eclipse hover controls does:
#Override
public IInformationControl doCreateInformationControl(Shell parent) {
ToolBarManager tbm = new ToolBarManager(SWT.FLAT);
DefaultInformationControl iControl = new DefaultInformationControl(parent, tbm);
IAction action = new MyAction();
tbm.add(action);
tbm.update(true);
return iControl;
}
So it does not create the ToolBar - leave that up to DefaultInformationControl. It uses an Action in the tool bar and adds it after creating the DefaultInformationControl. It just calls update(true) at the end.
(This is a modified version of parts of org.eclipse.jdt.internal.ui.text.java.hover.NLSStringHover)
MyAction would be something like:
private class MyAction extends Action
{
MyAction()
{
super("Title", .. image descriptor ..);
setToolTipText("Tooltip");
}
#Override
public void run()
{
// TODO your code for the action
}
}

Related

Change NavBar content in AppLayout dynamically when navigation occurs

I would like to dynamically add buttons in the AppLayout NavBar section when navigating to a view. I'm using RouterLink links in the drawer (wrapped in tabs) i.e. the view object is not instantiated prior to the navigation event. Is there a standard way to achieving this in Vaadin 14+?
Ideally the view navigated to would be able to inspect its parent (the layout) and access the navBar to add/remove components from it.
Here is how the AppLayout looks:
MainLayout.java
public class MainLayout extends AppLayout implements BeforeEnterObserver, TrackerConfigurator {
private Map<Tab, Component> tabSet = new HashMap<>();
public MainLayout() {
FlexLayout navBarLayout = new FlexLayout(leftNavBarLayout, navBarContentContainer, rightNavBarLayout);
navBarLayout.setWidthFull();
navBarLayout.setJustifyContentMode(FlexComponent.JustifyContentMode.BETWEEN);
navBarLayout.setAlignItems(FlexComponent.Alignment.CENTER);
addToNavbar(navBarLayout);
// Tabs
TabWithIdentifier page0 = tabForPageWithRouter("Dashboard", new IronIcon("icomoon", "icons"), DashboardView.class);
TabWithIdentifier page1 = tabForPageWithRouter("Users", new IronIcon("icomoon", "users2"), UserView.class);
...
final Tabs tabs = new Tabs(page0, page1, ...);
tabs.setOrientation(Tabs.Orientation.VERTICAL);
addToDrawer(tabs);
}
private TabWithIdentifier tabForPageWithRouter(String title, IronIcon icon, Class classType) {
icon.setSize("1.3em");
icon.getStyle().set("margin-right", "10px");
RouterLink routerLink = new RouterLink(null, classType);
routerLink.add(icon);
routerLink.add(title);
TabWithIdentifier tab = new TabWithIdentifier(routerLink);
tab.setId(title);
tab.setIdentifier(title);
tab.addClassName("drawer-tab");
return tab;
}
Resolved this case by overriding showRouterLayoutContent(HasElement content) and creating a base view class which provides a Div container for any components
I want to add to the navbar.
#Override
public void showRouterLayoutContent(HasElement content) {
super.showRouterLayoutContent(content);
BaseView baseView = null;
if (content instanceof BaseView) {
baseView = (BaseView) content;
pageTitle = new Label(baseView.getViewTitle());
controlContainer.removeAll();
controlContainer.add(baseView.getViewIconTray());
}
fireEvent(new RouterNavigated(this));
}

Accessing to navbar in vaadin 14

I'm using vaadin 14 for my application.
My MainView class extends Applayout class. This allows me to use addToNavBar(true, some Components) function which adds navigation bar to your application.
Now, In my main view, inside the navigation bar, I have register and login buttons populated. If click on these buttons, using addonclick listener I delegate to other views like Login and Registration. During these view changes, the navbar on top still stays there. However, if the user logged in or registered, I want to remove these login and register buttons in navigation bar and replace them with profile picture icon located inside the navbar. However, from child views(register,login) I couldn't find a way to access to navbar with vaadin 14. Accordingly, how can I access and change the content of the navbar from child views?
public class MainView extends AppLayout {
private static final long serialVersionUID = 1L;
private final Tabs menu;
private HorizontalLayout headerLayout;
public MainView() {
setPrimarySection(Section.NAVBAR);
headerLayout = createHeaderContent();
addToNavbar(true, headerLayout);
setDrawerOpened(false);
menu = createMenu();
addToDrawer(createDrawerContent(menu));
}
private HorizontalLayout createHeaderContent() {
headerLayout = new HorizontalLayout();
headerLayout.setId("header");
headerLayout.getThemeList().set("dark", true);
headerLayout.setWidthFull();
headerLayout.setSpacing(false);
headerLayout.setAlignItems(FlexComponent.Alignment.CENTER);
headerLayout.add(new DrawerToggle());
headerLayout.add(createWebsiteName());
headerLayout.add(createMiddleSpacingInHeader());
headerLayout.add(createLoginAndRegisterButtons());
return headerLayout;
}
private Component createLoginAndRegisterButtons() {
HorizontalLayout layout = new HorizontalLayout();
layout.setPadding(true);
layout.setSpacing(true);
layout.setAlignItems(Alignment.STRETCH);
Button register = createRegisterButton();
Button login = createLoginButton();
Image loggedInUserPicture = createLoggedInUserImage();
layout.add(register, login, loggedInUserPicture);
return layout;
}
There is currently no good API for this. One reason is that framework needs to be agnostic to how you build your menu. I have solved this by creating small interface of this kind
public interface HasTabsAccessor {
public default Tabs getTabs(Component component) {
Optional<Component> parent = component.getParent();
Tabs menu = null;
while (parent.isPresent()) {
Component p = parent.get();
if (p instanceof MainLayout) {
MainLayout main = (MainLayout) p;
menu = main.getMenu();
}
parent = p.getParent();
}
return menu;
}
}
Which I can then add to views where I need to access the menu.
#Route(value = FormLayoutView.ROUTE, layout = MainLayout.class)
#PageTitle(FormLayoutView.TITLE)
public class FormLayoutView extends VerticalLayout implements BeforeLeaveObserver, HasTabsAccessor {
...
And then just using getTabs() in the view.

Toolbar dissapears from custom hover in eclipse editor

I am working on an editor plugin for Eclipse that handles my own script language. In the editor, I have a hover that shows short information about element under the mouse cursor.
Now, I am trying to create a toolbar on the bottom of the hover and place a button there that will open a more detailed description online.
I have written my code based on answer to that question. The button is visible and it works when it is clicked.
However, it disappears a short time after I move my mouse over the hover. Why is this happening and how can I prevent that?
Here is the relevant part of my code:
#Override
public IInformationControlCreator getHoverControlCreator() {
return new IInformationControlCreator() {
#Override
public IInformationControl createInformationControl(final Shell parent) {
ToolBarManager tbm = new ToolBarManager(SWT.FLAT);
DefaultInformationControl defaultInformationControl = new DefaultInformationControl(parent, tbm);
Action action = new Action() {
#Override
public void run() {
MessageDialog.openInformation(parent, "omg", "It works.");
}
};
action.setText("123 test 321");
Bundle bundle = FrameworkUtil.getBundle(this.getClass());
URL url = FileLocator.find(bundle, new Path("icons/test.gif"), null);
action.setImageDescriptor(ImageDescriptor.createFromURL(url));
tbm.add(action);
tbm.update(true);
return defaultInformationControl;
}
};
}
When hover is created with DefaultInformationControl(parent, tbm) then toolbar is visible. However when you move mouse over the hover, then it gains focus. Then method getInformationPresenterControlCreator() from DefaultInformationControl is called.
It looks like (from source code):
public IInformationControlCreator getInformationPresenterControlCreator() {
return new IInformationControlCreator() {
/*
* #see org.eclipse.jface.text.IInformationControlCreator#createInformationControl(org.eclipse.swt.widgets.Shell)
*/
public IInformationControl createInformationControl(Shell parent) {
return new DefaultInformationControl(parent,
(ToolBarManager) null, fPresenter);
}
};
}
Look at return line. It nulls your Toolbar manager. That is the reason is gone.
Quick solution might be to create a new class which extends DefaultInformationControl and then in overrides
#Override
public IInformationControlCreator getInformationPresenterControlCreator() {
return new YourOwnInformationControlCreator();
}
This way you can pass correct ToolbarManager.

How do I dynamically set the visibility of the SideMenuBar in Codename one?

Using the Toolbar class within codenameone, how do I dynamically set the visibility of the SideMenuBar?
I'm using a WebBrowser component, and I only want the SideMenu to be accessible after login.
I achieved the behavior I wanted when I was simply placing commands on a SideMenuBar (METHOD 1), but now that I've switched over to use the Toolbar class for the LnF advantages (METHOD 2), the hideLeftSideMenuBool theme constant does not seem to be observed.
//METHOD 1
//CHANGING THE THEME DYNAMICALLY HIDES THE SIDEMENUBAR WHEN I'VE SIMPLY
//ADDED COMMANDS LIKE THIS
current.addCommand(new Command("Home") {
{
putClientProperty("place", "side");
}
});
//METHOD 2
//CHANGING THE THEME DYNAMICALLY DOES NOT HIDE THE SIDEMENUBAR WHEN I'VE
//USED toolbar.addComponentToSideMenu TO ADD BUTTONS WITH COMMANDS
toolbar = new Toolbar();
current.setToolbar(toolbar);
Button home = new Button("Home");
toolbar.addComponentToSideMenu(home, new Command("Home"){
#Override
public void actionPerformed(ActionEvent evt) {
wb.setURL(startURL);
}
});
...
//I USED THE FOLLOWING CODE TO DYNAMICALLY SET THE THEME AFTER EVALUATING A
//WebBrowser URI REGARDLESS OF WHICH METHOD WAS USED TO ADD COMMANDS
wb.setBrowserNavigationCallback(new BrowserNavigationCallback() {
public boolean shouldNavigate(String url) {
if ((url.indexOf("users/login") != -1)) {
try {
//theme_noside.res has hideLeftSideMenuBool set to true
theme = Resources.openLayered("/theme_noside");
UIManager.getInstance().setThemeProps(theme.getTheme(theme.getThemeResourceNames()[0]));
UIManager.getInstance().getLookAndFeel().setMenuBarClass(SideMenuBar.class);
Display.getInstance().setCommandBehavior(Display.COMMAND_BEHAVIOR_SIDE_NAVIGATION);
current.refreshTheme();
}catch(IOException e){
Log.p(e.toString());
}
}
else {
try {
//theme.res has hideLeftSideMenuBool set to false
theme = Resources.openLayered("/theme");
UIManager.getInstance().setThemeProps(theme.getTheme(theme.getThemeResourceNames()[0]));
UIManager.getInstance().getLookAndFeel().setMenuBarClass(SideMenuBar.class);
Display.getInstance().setCommandBehavior(Display.COMMAND_BEHAVIOR_SIDE_NAVIGATION);
current.refreshTheme();
}catch(IOException e){
Log.p(e.toString());
}
}
return true;
}
});
Use Toolbar api only and you don't have to call or change any theme constant.
Make your toolbar final or declare it outside the beforeShow() method, so you can access it within inner method shouldNavigate(String url).
All you need to do is call removeAll() and then reset the title and add components you want. If toolbar has no command or title, it would be hidden by default.
wb.setBrowserNavigationCallback(new BrowserNavigationCallback() {
public boolean shouldNavigate(String url) {
if ((url.indexOf("users/login") != -1)) {
toolbar.removeAll();
toolbar.setTitleComponent(new Label("My Form", "Title"));
toolbar.getComponentForm().revalidate();
} else {
//Do nothing, since I've already add the commands I want earlier
}
return true;
}
});

How can we get the text of the submenu context created when it is pressed

I have created a context menu bar with submenus.i have generated the submenus by using for loop
from the list.so when i press any one the submenu item generated i should get the text of the submenu item pressed.so how can we do it.I have only one run method inside the submenu but each submenu items perform different functionality.so how different functionality can be handled by one run method.
for ex
main menu->submenu1
->submenu2
->submenu3
so if i press submenu 2 i should get the string of the submenu item pressed and respective method should be called as it has only one run method
the code for generating context menu is as follows
MenuManager contextMenu = new MenuManager("#ViewerMenu"); //$NON-NLS-1$
contextMenu.setRemoveAllWhenShown(true);
contextMenu.addMenuListener(new IMenuListener() {
#Override
public void menuAboutToShow(IMenuManager mgr) {
fillContextMenu(mgr);
// IMenuManager submenu = new MenuManager("Sub menu title");
// submenu.add(someaction);
}
});
protected void fillContextMenu(final IMenuManager contextMenu) {
IMenuManager submenu = new MenuManager("Add Case");
List<String> testCases = new ArrayList<>();
testCases.addAll(TestCases.testCaseNames);
for (String item : testCases)
{
System.out.println("item is"+item);
submenu.add(new Action(item) {
#Override
public void run()
{
// implement this
}
});
}
contextMenu.add(submenu);
}
So how can this be done
The getText() method of Action returns the menu text.
So:
submenu.add(new Action(item) {
#Override
public void run()
{
String itemText = getText();
...
}

Categories

Resources