mbtarranger.editors.GenericInformationControl.java Source code

Java tutorial

Introduction

Here is the source code for mbtarranger.editors.GenericInformationControl.java

Source

/*******************************************************************************
 * Copyright (c) 2013, 2014 ETAS GmbH.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * IBM Corporation - initial API and implementation under classname 'org.eclipse.jface.text.DefaultInformationControl'
 * Dennis Eder (ETAS GmbH) - modifications for integration into MBT Arranger
 *******************************************************************************/
package mbtarranger.editors;

import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.text.AbstractInformationControl;
import org.eclipse.jface.text.DefaultInformationControl;
import org.eclipse.jface.text.IInformationControl;
import org.eclipse.jface.text.IInformationControlCreator;
import org.eclipse.jface.text.TextPresentation;
import org.eclipse.jface.util.Geometry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Drawable;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class GenericInformationControl extends AbstractInformationControl implements DisposeListener {
    /**
     * An information presenter determines the style presentation
     * of information displayed in the default information control.
     * The interface can be implemented by clients.
     */
    public interface IInformationPresenter {

        /**
         * Updates the given presentation of the given information and
         * thereby may manipulate the information to be displayed. The manipulation
         * could be the extraction of textual encoded style information etc. Returns the
         * manipulated information.
         * <p>
         * <strong>Note:</strong> The given display must only be used for measuring.</p>
         *
         * @param display the display of the information control
         * @param hoverInfo the information to be presented
         * @param presentation the presentation to be updated
         * @param maxWidth the maximal width in pixels
         * @param maxHeight the maximal height in pixels
         *
         * @return the manipulated information
         * @deprecated As of 3.2, replaced by {@link DefaultInformationControl.IInformationPresenterExtension#updatePresentation(Drawable, String, TextPresentation, int, int)}
         */
        String updatePresentation(Display display, String hoverInfo, GenericInformationPresentation presentation,
                int maxWidth, int maxHeight);
    }

    /**
     * An information presenter determines the style presentation
     * of information displayed in the default information control.
     * The interface can be implemented by clients.
     *
     * @since 3.2
     */
    public interface IInformationPresenterExtension {

        /**
         * Updates the given presentation of the given information and
         * thereby may manipulate the information to be displayed. The manipulation
         * could be the extraction of textual encoded style information etc. Returns the
         * manipulated information.
         * <p>
         * Replaces {@link DefaultInformationControl.IInformationPresenter#updatePresentation(Display, String, TextPresentation, int, int)}
         * Implementations should use the font of the given <code>drawable</code> to calculate
         * the size of the text to be presented.
         * </p>
         *
         * @param drawable the drawable of the information control
         * @param hoverInfo the information to be presented
         * @param presentation the presentation to be updated
         * @param maxWidth the maximal width in pixels
         * @param maxHeight the maximal height in pixels
         *
         * @return the manipulated information
         */
        String updatePresentation(Drawable drawable, String hoverInfo, GenericInformationPresentation presentation,
                int maxWidth, int maxHeight);
    }

    public GenericInformationControl(Shell parent) {
        this(parent, (String) null, null);
    }

    public GenericInformationControl(Shell parent, boolean isResizeable) {
        super(parent, isResizeable);
        fPresenter = new GenericInformationPresenter(!isResizeable);
        create();
    }

    public GenericInformationControl(Shell parent, IInformationPresenter presenter) {
        this(parent, (String) null, presenter);
    }

    public GenericInformationControl(Shell parent, String statusFieldText, IInformationPresenter presenter) {
        super(parent, statusFieldText);
        fPresenter = presenter;
        create();
    }

    public GenericInformationControl(Shell parent, String statusFieldText) {
        this(parent, statusFieldText, new GenericInformationPresenter(true));
    }

    public GenericInformationControl(Shell parent, ToolBarManager toolBarManager, IInformationPresenter presenter) {
        super(parent, toolBarManager);
        fPresenter = presenter;
        create();
    }

    public GenericInformationControl(Shell parent, ToolBarManager toolBarManager) {
        this(parent, toolBarManager, new GenericInformationPresenter(false));

    }

    /**
     * Inner border thickness in pixels.
     * 
     * @since 3.1
     */
    private static final int INNER_BORDER = 1;

    /**
     * Additional styles to use for the text control.
     * 
     * @since 3.4, previously called <code>fTextStyle</code>
     */

    //   /** The control's text widget */
    //   private StyledText fText;
    /** The control's html widget */
    protected Browser fBrowser;
    /** The information presenter, or <code>null</code> if none. */
    private final IInformationPresenter fPresenter;
    /** A cached text presentation */
    private final GenericInformationPresentation fPresentation = new GenericInformationPresentation();

    private boolean fHasContents = false;

    /*
     * @see
     * org.eclipse.jface.text.AbstractInformationControl#createContent(org.eclipse
     * .swt.widgets.Composite)
     */
    @Override
    protected void createContent(Composite parent) {
        //      fText= new StyledText(parent, SWT.MULTI | SWT.READ_ONLY | fAdditionalTextStyles);
        //      fText.setForeground(parent.getForeground());
        //      fText.setBackground(parent.getBackground());
        //      fText.setFont(JFaceResources.getDialogFont());
        fBrowser = new Browser(parent, SWT.NONE);
        fBrowser.setSize(parent.getSize());
        fBrowser.setForeground(parent.getForeground());
        fBrowser.setBackground(parent.getBackground());
        fBrowser.setFont(JFaceResources.getDialogFont());
        //      FillLayout layout= (FillLayout)parent.getLayout();
        //      if (fText.getWordWrap()) {
        //         // indent does not work for wrapping StyledText, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=56342 and https://bugs.eclipse.org/bugs/show_bug.cgi?id=115432
        //         layout.marginHeight= INNER_BORDER;
        //         layout.marginWidth= INNER_BORDER;
        //      } else {
        //         fText.setIndent(INNER_BORDER);
        //      }

    }

    /*
     * @see IInformationControlExtension#hasContents()
     */
    public boolean hasContents() {
        //      return fText.getCharCount() > 0;
        /* WARN: not working, because setText() does not immediately update result of getText()
         *    return fBrowser.getText().length()  > 0;
         * Therefore: info stored in extra field fHasContents
         */
        return fHasContents;
    }

    /*
     * @see IInformationControl#setInformation(String)
     */
    @Override
    public void setInformation(String content) {
        if (fPresenter == null) {
            //         fText.setText(content);
            fBrowser.setText(content);
        } else {
            //         fPresentation.clear();

            int maxWidth = -1;
            int maxHeight = -1;
            Point constraints = getSizeConstraints();
            if (constraints != null) {
                maxWidth = constraints.x;
                maxHeight = constraints.y;
                //            if (fText.getWordWrap()) {
                //               maxWidth -= INNER_BORDER * 2;
                //               maxHeight -= INNER_BORDER * 2;
                //            } else {
                maxWidth -= INNER_BORDER; // indent
                //            }
                Rectangle trim = computeTrim();
                maxWidth -= trim.width;
                maxHeight -= trim.height;
                //            maxWidth -= fText.getCaret().getSize().x; // StyledText adds a
                // border at the end
                // of the line for
                // the caret.
            }
            if (isResizable())
                maxHeight = Integer.MAX_VALUE;

            if (fPresenter instanceof IInformationPresenterExtension)
                content = ((IInformationPresenterExtension) fPresenter).updatePresentation(
                        //fText, content, fPresentation, maxWidth, maxHeight);
                        fBrowser, content, fPresentation, maxWidth, maxHeight);
            else
                content = fPresenter.updatePresentation(getShell().getDisplay(), content, fPresentation, maxWidth,
                        maxHeight);

            if (content != null) {
                //            fText.setText(content);
                //            TextPresentation.applyTextPresentation(fPresentation, fText);
                fBrowser.setText(content);
                fHasContents = true;
            } else {
                //fText.setText(""); //$NON-NLS-1$
                fBrowser.setText(""); //$NON-NLS-1$
                fHasContents = false;
            }
        }
    }

    /*
     * @see IInformationControl#setForegroundColor(Color)
     */
    public void setForegroundColor(Color foreground) {
        super.setForegroundColor(foreground);
        //      fText.setForeground(foreground);
        fBrowser.setForeground(foreground);
    }

    /*
     * @see IInformationControl#setBackgroundColor(Color)
     */
    public void setBackgroundColor(Color background) {
        super.setBackgroundColor(background);
        //fText.setBackground(background);
        fBrowser.setBackground(background);
    }

    /*
     * @see IInformationControl#setVisible(boolean)
     */
    public void setVisible(boolean visible) {
        if (visible) {
            //         if (fText.getWordWrap()) {
            Point currentSize = getShell().getSize();
            getShell().pack(true);
            Point newSize = getShell().getSize();
            if (newSize.x > currentSize.x || newSize.y > currentSize.y)
                setSize(currentSize.x, currentSize.y); // restore previous size
            //         }
        }

        super.setVisible(visible);
    }

    /*
     * @see org.eclipse.jface.text.IInformationControlExtension5#getInformationPresenterControlCreator()
     * @since 3.4
     */
    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 GenericInformationControl(parent, (ToolBarManager) null, fPresenter);
            }
        };
    }

    /*
     * @see IInformationControl#computeSizeHint()
     */
    public Point computeSizeHint() {
        // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=117602
        int widthHint = SWT.DEFAULT;
        Point constraints = getSizeConstraints();
        //      if (constraints != null && fText.getWordWrap())
        if (constraints != null)
            widthHint = constraints.x;

        return getShell().computeSize(widthHint, SWT.DEFAULT, true);
    }

    /*
     * @see org.eclipse.jface.text.AbstractInformationControl#computeTrim()
     */
    public Rectangle computeTrim() {
        return Geometry.add(super.computeTrim()
        //, fText.computeTrim(0, 0, 0, 0));
                , fBrowser.computeTrim(0, 0, 0, 0));
    }

    /**
     * @see org.eclipse.swt.events.DisposeListener#widgetDisposed(org.eclipse.swt.events.DisposeEvent)
     * @since 3.0
     * @deprecated As of 3.2, no longer used and called
     */
    public void widgetDisposed(DisposeEvent event) {
    }

}