Search in sources :

Example 1 with IInformationPresenter

use of org.eclipse.jface.text.DefaultInformationControl.IInformationPresenter in project mylyn.docs by eclipse.

the class InformationPresenterUtil method getHtmlInformationPresenter.

/**
 * Get an information presenter to present the provided HTML content. The returned presenter is ready for displaying
 * the information, all that is left to do is call {@link InformationPresenter#showInformation()}.
 *
 * @param viewer
 *            the viewer for which the information control should be created
 * @param constraint
 *            the size constraint
 * @param toolBarManager
 *            the tool bar manager, or null if there should be none
 * @param htmlContent
 *            the HTML content to be displayed by the information presenter.
 * @return the presenter
 */
public static InformationPresenter getHtmlInformationPresenter(ISourceViewer viewer, SizeConstraint constraint, final ToolBarManager toolBarManager, String htmlContent) {
    Control control = viewer.getTextWidget();
    InformationPresenter presenter = (InformationPresenter) control.getData(DATA_INFORMATION_PRESENTER);
    // bug 270059: ensure that the size/positioning math works by specifying the offet of the
    // current selection.
    int offset = viewer.getSelectedRange().x;
    IInformationControlCreator informationControlCreator;
    if (presenter == null) {
        informationControlCreator = new IInformationControlCreator() {

            @SuppressWarnings("deprecation")
            public IInformationControl createInformationControl(Shell shell) {
                try {
                    // DefaultInformationControl(Shell parent, ToolBarManager toolBarManager, IInformationPresenter presenter);
                    return DefaultInformationControl.class.getConstructor(Shell.class, ToolBarManager.class, IInformationPresenter.class).newInstance(shell, toolBarManager, new HtmlTextPresenter());
                } catch (NoSuchMethodException e) {
                    // no way with 3.3 to get V_SCROLL and a ToolBarManager
                    return new DefaultInformationControl(shell, SWT.RESIZE, SWT.V_SCROLL | SWT.H_SCROLL, new HtmlTextPresenter());
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }
        };
        presenter = new InformationPresenter(informationControlCreator) {

            @Override
            public IInformationProvider getInformationProvider(String contentType) {
                IInformationProvider informationProvider = super.getInformationProvider(contentType);
                if (informationProvider == null) {
                    informationProvider = super.getInformationProvider(IDocument.DEFAULT_CONTENT_TYPE);
                }
                return informationProvider;
            }
        };
        presenter.install(viewer);
        presenter.setAnchor(AbstractInformationControlManager.ANCHOR_BOTTOM);
        // default values from AbstractInformationControlManager
        presenter.setMargins(6, 6);
        presenter.setOffset(offset);
        presenter.install(viewer);
        final InformationPresenter informationPresenter = presenter;
        viewer.getTextWidget().addDisposeListener(new DisposeListener() {

            public void widgetDisposed(DisposeEvent e) {
                try {
                    informationPresenter.uninstall();
                } catch (Exception e2) {
                }
                informationPresenter.dispose();
            }
        });
        control.setData(DATA_INFORMATION_PRESENTER, presenter);
        control.setData(DATA_INFORMATION_CONTROL_CREATOR, informationControlCreator);
    } else {
        informationControlCreator = (IInformationControlCreator) control.getData(DATA_INFORMATION_CONTROL_CREATOR);
        presenter.disposeInformationControl();
    }
    presenter.setSizeConstraints(constraint.horizontalWidthInChars, constraint.verticalWidthInChars, constraint.enforceAsMinimumSize, constraint.enforceAsMaximumSize);
    InformationProvider informationProvider = new InformationProvider(new org.eclipse.jface.text.Region(offset, 0), htmlContent, informationControlCreator);
    for (String contentType : FastMarkupPartitioner.ALL_CONTENT_TYPES) {
        presenter.setInformationProvider(informationProvider, contentType);
    }
    presenter.setInformationProvider(informationProvider, IDocument.DEFAULT_CONTENT_TYPE);
    return presenter;
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) DefaultInformationControl(org.eclipse.jface.text.DefaultInformationControl) DisposeEvent(org.eclipse.swt.events.DisposeEvent) IInformationControlCreator(org.eclipse.jface.text.IInformationControlCreator) IInformationControl(org.eclipse.jface.text.IInformationControl) DefaultInformationControl(org.eclipse.jface.text.DefaultInformationControl) Control(org.eclipse.swt.widgets.Control) Shell(org.eclipse.swt.widgets.Shell) IInformationControl(org.eclipse.jface.text.IInformationControl) IInformationProvider(org.eclipse.jface.text.information.IInformationProvider) HtmlTextPresenter(org.eclipse.mylyn.wikitext.ui.viewer.HtmlTextPresenter) IInformationProvider(org.eclipse.jface.text.information.IInformationProvider) InformationPresenter(org.eclipse.jface.text.information.InformationPresenter) IInformationPresenter(org.eclipse.jface.text.DefaultInformationControl.IInformationPresenter)

Aggregations

DefaultInformationControl (org.eclipse.jface.text.DefaultInformationControl)1 IInformationPresenter (org.eclipse.jface.text.DefaultInformationControl.IInformationPresenter)1 IInformationControl (org.eclipse.jface.text.IInformationControl)1 IInformationControlCreator (org.eclipse.jface.text.IInformationControlCreator)1 IInformationProvider (org.eclipse.jface.text.information.IInformationProvider)1 InformationPresenter (org.eclipse.jface.text.information.InformationPresenter)1 HtmlTextPresenter (org.eclipse.mylyn.wikitext.ui.viewer.HtmlTextPresenter)1 DisposeEvent (org.eclipse.swt.events.DisposeEvent)1 DisposeListener (org.eclipse.swt.events.DisposeListener)1 Control (org.eclipse.swt.widgets.Control)1 Shell (org.eclipse.swt.widgets.Shell)1