简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM