a69406262da8863845c6d575f454266fea4b15d0
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / phpeditor / JavaEditorErrorTickUpdater.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 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 net.sourceforge.phpeclipse.phpeditor;
12
13 import net.sourceforge.phpdt.core.IJavaElement;
14 import net.sourceforge.phpdt.internal.ui.viewsupport.IProblemChangedListener;
15 import net.sourceforge.phpdt.internal.ui.viewsupport.JavaElementImageProvider;
16 import net.sourceforge.phpdt.internal.ui.viewsupport.JavaUILabelProvider;
17 import net.sourceforge.phpdt.ui.ProblemsLabelDecorator;
18 //import net.sourceforge.phpeclipse.PHPeclipsePlugin;
19 import net.sourceforge.phpeclipse.ui.WebUI;
20
21 import org.eclipse.core.resources.IResource;
22 //incastrix
23 //import org.eclipse.jface.text.Assert;
24 import org.eclipse.core.runtime.Assert;
25 import org.eclipse.swt.graphics.Image;
26 import org.eclipse.swt.widgets.Shell;
27 import org.eclipse.ui.IEditorInput;
28
29 /**
30  * The <code>JavaEditorErrorTickUpdater</code> will register as a
31  * IProblemChangedListener to listen on problem changes of the editor's input.
32  * It updates the title images when the annotation model changed.
33  */
34 public class JavaEditorErrorTickUpdater implements IProblemChangedListener {
35
36         private PHPEditor fJavaEditor;
37
38         private JavaUILabelProvider fLabelProvider;
39
40         public JavaEditorErrorTickUpdater(PHPEditor editor) {
41                 Assert.isNotNull(editor);
42                 fJavaEditor = editor;
43                 fLabelProvider = new JavaUILabelProvider(0,
44                                 JavaElementImageProvider.SMALL_ICONS);
45                 fLabelProvider.addLabelDecorator(new ProblemsLabelDecorator(null));
46                 WebUI.getDefault().getProblemMarkerManager().addListener(
47                                 this);
48         }
49
50         /*
51          * (non-Javadoc)
52          * 
53          * @see IProblemChangedListener#problemsChanged(IResource[], boolean)
54          */
55         public void problemsChanged(IResource[] changedResources,
56                         boolean isMarkerChange) {
57                 if (isMarkerChange) {
58                         return;
59                 }
60                 IEditorInput input = fJavaEditor.getEditorInput();
61                 if (input != null) { // might run async, tests needed
62                         IJavaElement jelement = (IJavaElement) input
63                                         .getAdapter(IJavaElement.class);
64                         if (jelement != null) {
65                                 IResource resource = jelement.getResource();
66                                 for (int i = 0; i < changedResources.length; i++) {
67                                         if (changedResources[i].equals(resource)) {
68                                                 updateEditorImage(jelement);
69                                         }
70                                 }
71                         }
72                 }
73         }
74
75         public void updateEditorImage(IJavaElement jelement) {
76                 Image titleImage = fJavaEditor.getTitleImage();
77                 if (titleImage == null) {
78                         return;
79                 }
80                 Image newImage = fLabelProvider.getImage(jelement);
81                 if (titleImage != newImage) {
82                         postImageChange(newImage);
83                 }
84         }
85
86         private void postImageChange(final Image newImage) {
87                 Shell shell = fJavaEditor.getEditorSite().getShell();
88                 if (shell != null && !shell.isDisposed()) {
89                         shell.getDisplay().syncExec(new Runnable() {
90                                 public void run() {
91                                         fJavaEditor.updatedTitleImage(newImage);
92                                 }
93                         });
94                 }
95         }
96
97         public void dispose() {
98                 fLabelProvider.dispose();
99                 WebUI.getDefault().getProblemMarkerManager().removeListener(
100                                 this);
101         }
102
103 }