KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > editor > text > XMLMarkerAnnotation


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

11 package org.eclipse.ant.internal.ui.editor.text;
12
13
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.core.resources.IMarker;
17 import org.eclipse.jface.resource.ImageRegistry;
18 import org.eclipse.jface.text.Assert;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.graphics.Image;
21 import org.eclipse.swt.widgets.Display;
22 import org.eclipse.ui.texteditor.MarkerAnnotation;
23
24 public class XMLMarkerAnnotation extends MarkerAnnotation implements IXMLAnnotation {
25     private static final int NO_IMAGE= 0;
26     private static final int ORIGINAL_MARKER_IMAGE= 1;
27     private static final int OVERLAY_IMAGE= 4;
28     private static final int GRAY_IMAGE= 5;
29
30     private static ImageRegistry fgGrayMarkersImageRegistry;
31     
32     private IXMLAnnotation fOverlay;
33     private boolean fNotRelevant= false;
34     private int fImageType;
35     
36     public XMLMarkerAnnotation(String JavaDoc annotationType, IMarker marker) {
37         super(annotationType, marker);
38     }
39     
40     /* (non-Javadoc)
41      * @see org.eclipse.ui.texteditor.MarkerAnnotation#initialize()
42      */

43     protected void initialize() {
44         fImageType= NO_IMAGE;
45         
46         super.initialize();
47     }
48         
49     /* (non-Javadoc)
50      * @see org.eclipse.ui.texteditor.IAnnotationExtension#getMessage()
51      */

52     public String JavaDoc getMessage() {
53         IMarker marker= getMarker();
54         if (marker == null || !marker.exists()) {
55             return ""; //$NON-NLS-1$
56
}
57         
58         return marker.getAttribute(IMarker.MESSAGE, ""); //$NON-NLS-1$
59
}
60
61     /* (non-Javadoc)
62      * @see org.eclipse.ui.texteditor.IAnnotationExtension#isTemporary()
63      */

64     public boolean isTemporary() {
65         return false;
66     }
67     
68     /* (non-Javadoc)
69      * @see org.eclipse.ant.internal.ui.editor.text.IXMLAnnotation#isProblem()
70      */

71     public boolean isProblem() {
72         return WARNING_ANNOTATION_TYPE.equals(getType()) || ERROR_ANNOTATION_TYPE.equals(getType());
73     }
74     
75     /* (non-Javadoc)
76      * @see org.eclipse.ant.internal.ui.editor.text.IXMLAnnotation#isRelevant()
77      */

78     public boolean isRelevant() {
79         return !fNotRelevant;
80     }
81
82     /**
83      * Overlays this annotation with the given xmlAnnotation.
84      *
85      * @param xmlAnnotation annotation that is overlaid by this annotation
86      */

87     public void setOverlay(IXMLAnnotation xmlAnnotation) {
88         if (fOverlay != null) {
89             fOverlay.removeOverlaid(this);
90         }
91             
92         fOverlay= xmlAnnotation;
93         fNotRelevant= (fNotRelevant || fOverlay != null);
94         
95         if (xmlAnnotation != null) {
96             xmlAnnotation.addOverlaid(this);
97         }
98     }
99     
100     /* (non-Javadoc)
101      * @see org.eclipse.ant.internal.ui.editor.text.IXMLAnnotation#hasOverlay()
102      */

103     public boolean hasOverlay() {
104         return fOverlay != null;
105     }
106     
107     /* (non-Javadoc)
108      * @see org.eclipse.ui.texteditor.MarkerAnnotation#getImage(org.eclipse.swt.widgets.Display)
109      */

110     public Image getImage(Display display) {
111         int newImageType= NO_IMAGE;
112
113         if (hasOverlay()) {
114             newImageType= OVERLAY_IMAGE;
115         } else if (isRelevant()) {
116             newImageType= ORIGINAL_MARKER_IMAGE;
117         } else {
118             newImageType= GRAY_IMAGE;
119         }
120
121         if (fImageType == newImageType && newImageType != OVERLAY_IMAGE) {
122             // Nothing changed - simply return the current image
123
return super.getImage(display);
124         }
125
126         Image newImage= null;
127         switch (newImageType) {
128             case ORIGINAL_MARKER_IMAGE:
129                 newImage= null;
130                 break;
131             case OVERLAY_IMAGE:
132                 newImage= fOverlay.getImage(display);
133                 break;
134             case GRAY_IMAGE:
135                 if (fImageType != ORIGINAL_MARKER_IMAGE) {
136                     setImage(null);
137                 }
138                 Image originalImage= super.getImage(display);
139                 if (originalImage != null) {
140                     ImageRegistry imageRegistry= getGrayMarkerImageRegistry(display);
141                     if (imageRegistry != null) {
142                         String JavaDoc key= Integer.toString(originalImage.hashCode());
143                         Image grayImage= imageRegistry.get(key);
144                         if (grayImage == null) {
145                             grayImage= new Image(display, originalImage, SWT.IMAGE_GRAY);
146                             imageRegistry.put(key, grayImage);
147                         }
148                         newImage= grayImage;
149                     }
150                 }
151                 break;
152             default:
153                 Assert.isLegal(false);
154         }
155
156         fImageType= newImageType;
157         setImage(newImage);
158         return super.getImage(display);
159     }
160     
161     private ImageRegistry getGrayMarkerImageRegistry(Display display) {
162         if (fgGrayMarkersImageRegistry == null) {
163             fgGrayMarkersImageRegistry= new ImageRegistry(display);
164         }
165         return fgGrayMarkersImageRegistry;
166     }
167     
168     /* (non-Javadoc)
169      * @see org.eclipse.ant.internal.ui.editor.text.IXMLAnnotation#addOverlaid(org.eclipse.ant.internal.ui.editor.text.IXMLAnnotation)
170      */

171     public void addOverlaid(IXMLAnnotation annotation) {
172         // not supported
173
}
174
175     /* (non-Javadoc)
176      * @see org.eclipse.ant.internal.ui.editor.text.IXMLAnnotation#removeOverlaid(org.eclipse.ant.internal.ui.editor.text.IXMLAnnotation)
177      */

178     public void removeOverlaid(IXMLAnnotation annotation) {
179         // not supported
180
}
181     
182     /* (non-Javadoc)
183      * @see org.eclipse.ant.internal.ui.editor.text.IXMLAnnotation#getOverlaidIterator()
184      */

185     public Iterator JavaDoc getOverlaidIterator() {
186         // not supported
187
return null;
188     }
189 }
Popular Tags