KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > javaeditor > JavaEditorErrorTickUpdater


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.jdt.internal.ui.javaeditor;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.core.resources.IResource;
16
17 import org.eclipse.swt.graphics.Image;
18 import org.eclipse.swt.widgets.Shell;
19
20
21 import org.eclipse.ui.IEditorInput;
22
23 import org.eclipse.jdt.core.ICompilationUnit;
24 import org.eclipse.jdt.core.IJavaElement;
25
26 import org.eclipse.jdt.ui.ProblemsLabelDecorator;
27
28 import org.eclipse.jdt.internal.ui.JavaPlugin;
29 import org.eclipse.jdt.internal.ui.viewsupport.IProblemChangedListener;
30 import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider;
31 import org.eclipse.jdt.internal.ui.viewsupport.JavaUILabelProvider;
32
33 /**
34  * The <code>JavaEditorErrorTickUpdater</code> will register as a IProblemChangedListener
35  * to listen on problem changes of the editor's input. It updates the title images when the annotation
36  * model changed.
37  */

38 public class JavaEditorErrorTickUpdater implements IProblemChangedListener {
39
40     private JavaEditor fJavaEditor;
41     private JavaUILabelProvider fLabelProvider;
42
43     public JavaEditorErrorTickUpdater(JavaEditor editor) {
44         Assert.isNotNull(editor);
45         fJavaEditor= editor;
46         fLabelProvider= new JavaUILabelProvider(0, JavaElementImageProvider.SMALL_ICONS);
47         fLabelProvider.addLabelDecorator(new ProblemsLabelDecorator(null));
48         JavaPlugin.getDefault().getProblemMarkerManager().addListener(this);
49     }
50
51     /* (non-Javadoc)
52      * @see IProblemChangedListener#problemsChanged(IResource[], boolean)
53      */

54     public void problemsChanged(IResource[] changedResources, boolean isMarkerChange) {
55         if (!isMarkerChange)
56             return;
57
58         IEditorInput input= fJavaEditor.getEditorInput();
59         if (input != null) { // might run async, tests needed
60
IJavaElement jelement= (IJavaElement) input.getAdapter(IJavaElement.class);
61             if (jelement != null) {
62                 IResource resource= jelement.getResource();
63                 for (int i = 0; i < changedResources.length; i++) {
64                     if (changedResources[i].equals(resource)) {
65                         updateEditorImage(jelement);
66                     }
67                 }
68             }
69         }
70     }
71
72     public void updateEditorImage(IJavaElement jelement) {
73         Image titleImage= fJavaEditor.getTitleImage();
74         if (titleImage == null) {
75             return;
76         }
77         Image newImage;
78         if (jelement instanceof ICompilationUnit && !jelement.getJavaProject().isOnClasspath(jelement))
79             newImage= fLabelProvider.getImage(jelement.getResource());
80         else
81             newImage= fLabelProvider.getImage(jelement);
82         if (titleImage != newImage) {
83             postImageChange(newImage);
84         }
85     }
86
87     private void postImageChange(final Image newImage) {
88         Shell shell= fJavaEditor.getEditorSite().getShell();
89         if (shell != null && !shell.isDisposed()) {
90             shell.getDisplay().syncExec(new Runnable JavaDoc() {
91                 public void run() {
92                     fJavaEditor.updatedTitleImage(newImage);
93                 }
94             });
95         }
96     }
97
98     public void dispose() {
99         fLabelProvider.dispose();
100         JavaPlugin.getDefault().getProblemMarkerManager().removeListener(this);
101     }
102
103
104 }
105
106
107
Popular Tags