de.ovgu.cide.editor.inlineprojection.InlineProjectionAnnotation.java Source code

Java tutorial

Introduction

Here is the source code for de.ovgu.cide.editor.inlineprojection.InlineProjectionAnnotation.java

Source

/**
Copyright 2010 Christian Kstner
    
This file is part of CIDE.
    
CIDE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.
    
CIDE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with CIDE.  If not, see <http://www.gnu.org/licenses/>.
    
See http://www.fosd.de/cide/ for further information.
*/

/**
 * 
 */
package de.ovgu.cide.editor.inlineprojection;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.IAnnotationPresentation;
import org.eclipse.jface.text.source.ImageUtilities;
import org.eclipse.jface.text.source.projection.ProjectionAnnotation;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;

public class InlineProjectionAnnotation extends Annotation implements IAnnotationPresentation {

    private static class DisplayDisposeRunnable implements Runnable {

        public void run() {
            if (fgCollapsedImage != null) {
                fgCollapsedImage.dispose();
                fgCollapsedImage = null;
            }
            if (fgExpandedImage != null) {
                fgExpandedImage.dispose();
                fgExpandedImage = null;
            }
        }
    }

    /**
     * The type of projection annotations.
     */
    public static final String TYPE = "de.ovgu.cide.editor.inlineprojection"; //$NON-NLS-1$

    private static Image fgCollapsedImage;

    private static Image fgExpandedImage;

    /** The state of this annotation */
    private boolean fIsCollapsed = false;

    /**
     * Creates a new expanded projection annotation.
     */
    public InlineProjectionAnnotation() {
        this(false);
    }

    /**
     * Creates a new projection annotation. When <code>isCollapsed</code>
     * is <code>true</code> the annotation is initially collapsed.
     * 
     * @param isCollapsed
     *            <code>true</code> if the annotation should initially be
     *            collapsed, <code>false</code> otherwise
     */
    public InlineProjectionAnnotation(boolean isCollapsed) {
        super(TYPE, false, null);
        fIsCollapsed = isCollapsed;
    }

    /*
     * @see org.eclipse.jface.text.source.IAnnotationPresentation#paint(org.eclipse.swt.graphics.GC,
     *      org.eclipse.swt.widgets.Canvas,
     *      org.eclipse.swt.graphics.Rectangle)
     */
    public void paint(GC gc, Canvas canvas, Rectangle rectangle) {
        Image image = getImage(canvas.getDisplay());
        if (image != null) {
            ImageUtilities.drawImage(image, gc, canvas, rectangle, SWT.CENTER, SWT.TOP);

        }
    }

    /*
     * @see org.eclipse.jface.text.source.IAnnotationPresentation#getLayer()
     */
    public int getLayer() {
        return IAnnotationPresentation.DEFAULT_LAYER;
    }

    private Image getImage(Display display) {
        initializeImages(display);
        return isCollapsed() ? fgCollapsedImage : fgExpandedImage;
    }

    private void initializeImages(Display display) {
        if (fgCollapsedImage == null) {

            ImageDescriptor descriptor = ImageDescriptor.createFromFile(ProjectionAnnotation.class,
                    "images/collapsed.gif"); //$NON-NLS-1$
            fgCollapsedImage = descriptor.createImage(display);
            descriptor = ImageDescriptor.createFromFile(ProjectionAnnotation.class, "images/expanded.gif"); //$NON-NLS-1$
            fgExpandedImage = descriptor.createImage(display);

            display.disposeExec(new DisplayDisposeRunnable());
        }
    }

    /**
     * Returns the state of this annotation.
     * 
     * @return <code>true</code> if collapsed
     */
    public boolean isCollapsed() {
        return fIsCollapsed;
    }

    /**
     * Marks this annotation as being collapsed.
     */
    public void markCollapsed() {
        fIsCollapsed = true;
    }

    /**
     * Marks this annotation as being unfolded.
     */
    public void markExpanded() {
        fIsCollapsed = false;
    }
}