KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > source > projection > ProjectionAnnotation


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.text.source.projection;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.graphics.FontMetrics;
15 import org.eclipse.swt.graphics.GC;
16 import org.eclipse.swt.graphics.Image;
17 import org.eclipse.swt.graphics.Rectangle;
18 import org.eclipse.swt.widgets.Canvas;
19 import org.eclipse.swt.widgets.Display;
20
21 import org.eclipse.jface.resource.ImageDescriptor;
22
23 import org.eclipse.jface.text.source.Annotation;
24 import org.eclipse.jface.text.source.IAnnotationPresentation;
25 import org.eclipse.jface.text.source.ImageUtilities;
26
27 /**
28  * Annotation used to represent the projection of a master document onto a
29  * {@link org.eclipse.jface.text.projection.ProjectionDocument}. A projection
30  * annotation can be either expanded or collapsed. If expanded it corresponds to
31  * a segment of the projection document. If collapsed, it represents a region of
32  * the master document that does not have a corresponding segment in the
33  * projection document.
34  * <p>
35  * Clients may subclass or use as is.
36  * </p>
37  *
38  * @since 3.0
39  */

40 public class ProjectionAnnotation extends Annotation implements IAnnotationPresentation {
41
42     private static class DisplayDisposeRunnable implements Runnable JavaDoc {
43
44         public void run() {
45             if (fgCollapsedImage != null) {
46                 fgCollapsedImage.dispose();
47                 fgCollapsedImage= null;
48             }
49             if (fgExpandedImage != null) {
50                 fgExpandedImage.dispose();
51                 fgExpandedImage= null;
52             }
53         }
54     }
55
56     /**
57      * The type of projection annotations.
58      */

59     public static final String JavaDoc TYPE= "org.eclipse.projection"; //$NON-NLS-1$
60

61
62     private static final int COLOR= SWT.COLOR_GRAY;
63     private static Image fgCollapsedImage;
64     private static Image fgExpandedImage;
65
66
67     /** The state of this annotation */
68     private boolean fIsCollapsed= false;
69     /** Indicates whether this annotation should be painted as range */
70     private boolean fIsRangeIndication= false;
71
72     /**
73      * Creates a new expanded projection annotation.
74      */

75     public ProjectionAnnotation() {
76         this(false);
77     }
78
79     /**
80      * Creates a new projection annotation. When <code>isCollapsed</code>
81      * is <code>true</code> the annotation is initially collapsed.
82      *
83      * @param isCollapsed <code>true</code> if the annotation should initially be collapsed, <code>false</code> otherwise
84      */

85     public ProjectionAnnotation(boolean isCollapsed) {
86         super(TYPE, false, null);
87         fIsCollapsed= isCollapsed;
88     }
89
90     /**
91      * Enables and disables the range indication for this annotation.
92      *
93      * @param rangeIndication the enable state for the range indication
94      */

95     public void setRangeIndication(boolean rangeIndication) {
96         fIsRangeIndication= rangeIndication;
97     }
98
99     private void drawRangeIndication(GC gc, Canvas canvas, Rectangle r) {
100         final int MARGIN= 3;
101         
102         /* cap the height - at least on GTK, large numbers are converted to
103          * negatives at some point */

104         int height= Math.min(r.y + r.height - MARGIN, canvas.getSize().y);
105         
106         gc.setForeground(canvas.getDisplay().getSystemColor(COLOR));
107         gc.setLineWidth(0); // NOTE: 0 means width is 1 but with optimized performance
108
gc.drawLine(r.x + 4, r.y + 12, r.x + 4, height);
109         gc.drawLine(r.x + 4, height, r.x + r.width - MARGIN, height);
110     }
111
112     /*
113      * @see org.eclipse.jface.text.source.IAnnotationPresentation#paint(org.eclipse.swt.graphics.GC, org.eclipse.swt.widgets.Canvas, org.eclipse.swt.graphics.Rectangle)
114      */

115     public void paint(GC gc, Canvas canvas, Rectangle rectangle) {
116         Image image= getImage(canvas.getDisplay());
117         if (image != null) {
118             ImageUtilities.drawImage(image, gc, canvas, rectangle, SWT.CENTER, SWT.TOP);
119             if (fIsRangeIndication) {
120                 FontMetrics fontMetrics= gc.getFontMetrics();
121                 int delta= (fontMetrics.getHeight() - image.getBounds().height)/2;
122                 rectangle.y += delta;
123                 rectangle.height -= delta;
124                 drawRangeIndication(gc, canvas, rectangle);
125             }
126         }
127     }
128
129     /*
130      * @see org.eclipse.jface.text.source.IAnnotationPresentation#getLayer()
131      */

132     public int getLayer() {
133         return IAnnotationPresentation.DEFAULT_LAYER;
134     }
135
136     private Image getImage(Display display) {
137         initializeImages(display);
138         return isCollapsed() ? fgCollapsedImage : fgExpandedImage;
139     }
140
141     private void initializeImages(Display display) {
142         if (fgCollapsedImage == null) {
143
144             ImageDescriptor descriptor= ImageDescriptor.createFromFile(ProjectionAnnotation.class, "images/collapsed.gif"); //$NON-NLS-1$
145
fgCollapsedImage= descriptor.createImage(display);
146             descriptor= ImageDescriptor.createFromFile(ProjectionAnnotation.class, "images/expanded.gif"); //$NON-NLS-1$
147
fgExpandedImage= descriptor.createImage(display);
148
149             display.disposeExec(new DisplayDisposeRunnable());
150         }
151     }
152
153     /**
154      * Returns the state of this annotation.
155      *
156      * @return <code>true</code> if collapsed
157      */

158     public boolean isCollapsed() {
159         return fIsCollapsed;
160     }
161
162     /**
163      * Marks this annotation as being collapsed.
164      */

165     public void markCollapsed() {
166         fIsCollapsed= true;
167     }
168
169     /**
170      * Marks this annotation as being unfolded.
171      */

172     public void markExpanded() {
173         fIsCollapsed= false;
174     }
175 }
176
Popular Tags