KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > loskutov > bco > views > EditorListener


1 /*******************************************************************************
2  * Copyright (c) 2004 Andrei Loskutov.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the BSD License
5  * which accompanies this distribution, and is available at
6  * http://www.opensource.org/licenses/bsd-license.php
7  * Contributor: Andrei Loskutov - initial API and implementation
8  *******************************************************************************/

9 package de.loskutov.bco.views;
10
11 import org.eclipse.core.filebuffers.IFileBuffer;
12 import org.eclipse.core.filebuffers.IFileBufferListener;
13 import org.eclipse.core.runtime.IPath;
14 import org.eclipse.jdt.core.IJavaElement;
15 import org.eclipse.jface.text.ITextSelection;
16 import org.eclipse.jface.viewers.ISelection;
17 import org.eclipse.jface.viewers.IStructuredSelection;
18 import org.eclipse.swt.widgets.Display;
19 import org.eclipse.ui.IPartListener2;
20 import org.eclipse.ui.ISelectionListener;
21 import org.eclipse.ui.IWorkbenchPart;
22 import org.eclipse.ui.IWorkbenchPartReference;
23
24 /**
25  * @author Andrei
26  */

27 public class EditorListener implements ISelectionListener, IFileBufferListener,
28     IPartListener2 {
29     protected BytecodeOutlineView view;
30
31     EditorListener(BytecodeOutlineView view){
32         this.view = view;
33     }
34
35     /**
36      * clean view reference
37      */

38     public void dispose(){
39         this.view = null;
40     }
41
42     /**
43      * @param part
44      * @param selection
45      *
46      */

47     public void selectionChanged(IWorkbenchPart part, ISelection selection) {
48         if(!(selection instanceof ITextSelection)){
49             if(selection instanceof IStructuredSelection){
50                 IStructuredSelection ssel = (IStructuredSelection) selection;
51                 if(ssel.isEmpty()){
52                     return;
53                 }
54                 if(ssel.getFirstElement() instanceof IJavaElement){
55                     /*
56                      * this may be selection in outline view. If so, the editor selection
57                      * would be changed but no event would be sent :(
58                      * So we just delay the call and wait for new selection in editor
59                      */

60                     Display display = Display.getDefault();
61                     // fork
62
display.asyncExec(new Runnable JavaDoc() {
63                         public void run() {
64                             view.checkOpenEditors(true);
65                         }
66                     });
67                 }
68             }
69             return;
70         }
71         view.handleSelectionChanged(part, selection);
72     }
73
74     /**
75      * @see org.eclipse.core.filebuffers.IFileBufferListener#dirtyStateChanged(org.eclipse.core.filebuffers.IFileBuffer, boolean)
76      */

77     public void dirtyStateChanged(IFileBuffer buffer, final boolean isDirty) {
78         if(!view.isLinkedWithEditor()){
79             return;
80         }
81         if(isSupportedBuffer(buffer)){ //$NON-NLS-1$
82
// first call set only view flag - cause
83
view.handleBufferIsDirty(isDirty);
84
85             // second call will really refresh view
86
if(!isDirty){
87                 // this one will be called in UI thread after some delay, because we need
88
// to wait until the bytecode will be written on disk
89
final Runnable JavaDoc runnable2 = new Runnable JavaDoc() {
90                     public void run() {
91                         view.handleBufferIsDirty(isDirty);
92                     }
93                 };
94                 // this one will be called in UI thread ASAP and allow us to leave
95
// current (probably non-UI) thread
96
Runnable JavaDoc runnable1 = new Runnable JavaDoc() {
97                     public void run() {
98                         Display display = Display.getCurrent();
99                         display.timerExec(1000, runnable2);
100                     }
101                 };
102                 Display display = Display.getDefault();
103                 // fork
104
display.asyncExec(runnable1);
105             }
106         }
107     }
108
109     private boolean isSupportedBuffer(IFileBuffer buffer) {
110         String JavaDoc fileExtension = buffer.getLocation().getFileExtension();
111         // TODO export to properties
112
return "java".equals(fileExtension);// || "groovy".equals(fileExtension); //$NON-NLS-1$//$NON-NLS-2$
113
}
114
115     /**
116      * @see org.eclipse.ui.IPartListener2#partClosed(org.eclipse.ui.IWorkbenchPartReference)
117      */

118     public void partClosed(IWorkbenchPartReference partRef) {
119         view.handlePartHidden(partRef.getPart(false));
120     }
121
122     /**
123      * @see org.eclipse.ui.IPartListener2#partHidden(org.eclipse.ui.IWorkbenchPartReference)
124      */

125     public void partHidden(IWorkbenchPartReference partRef) {
126         view.handlePartHidden(partRef.getPart(false));
127     }
128
129     /**
130      * @see org.eclipse.ui.IPartListener2#partOpened(org.eclipse.ui.IWorkbenchPartReference)
131      */

132     public void partOpened(IWorkbenchPartReference partRef) {
133         view.handlePartVisible(partRef.getPart(false));
134     }
135
136     /**
137      * @see org.eclipse.ui.IPartListener2#partVisible(org.eclipse.ui.IWorkbenchPartReference)
138      */

139     public void partVisible(IWorkbenchPartReference partRef) {
140         view.handlePartVisible(partRef.getPart(false));
141     }
142
143
144     /**
145      * @see org.eclipse.core.filebuffers.IFileBufferListener#bufferDisposed(org.eclipse.core.filebuffers.IFileBuffer)
146      */

147     public void bufferDisposed(IFileBuffer buffer) {
148         // is not used here
149
}
150
151     /**
152      * @see org.eclipse.core.filebuffers.IFileBufferListener#bufferCreated(org.eclipse.core.filebuffers.IFileBuffer)
153      */

154     public void bufferCreated(IFileBuffer buffer) {
155         // is not used here
156
}
157
158     /**
159      * @see org.eclipse.core.filebuffers.IFileBufferListener#bufferContentAboutToBeReplaced(org.eclipse.core.filebuffers.IFileBuffer)
160      */

161     public void bufferContentAboutToBeReplaced(IFileBuffer buffer) {
162         // is not used here
163
}
164
165     /**
166      * @see org.eclipse.core.filebuffers.IFileBufferListener#bufferContentReplaced(org.eclipse.core.filebuffers.IFileBuffer)
167      */

168     public void bufferContentReplaced(IFileBuffer buffer) {
169         // is not used here
170
}
171
172     /**
173      * @see org.eclipse.core.filebuffers.IFileBufferListener#stateChanging(org.eclipse.core.filebuffers.IFileBuffer)
174      */

175     public void stateChanging(IFileBuffer buffer) {
176         // is not used here
177
}
178
179     /**
180      * @see org.eclipse.core.filebuffers.IFileBufferListener#stateValidationChanged(org.eclipse.core.filebuffers.IFileBuffer, boolean)
181      */

182     public void stateValidationChanged(IFileBuffer buffer, boolean isStateValidated) {
183         // is not used here
184
}
185
186     /**
187      * @see org.eclipse.core.filebuffers.IFileBufferListener#underlyingFileMoved(org.eclipse.core.filebuffers.IFileBuffer, org.eclipse.core.runtime.IPath)
188      */

189     public void underlyingFileMoved(IFileBuffer buffer, IPath path) {
190         //is not used here
191
}
192
193     /**
194      * @see org.eclipse.core.filebuffers.IFileBufferListener#underlyingFileDeleted(org.eclipse.core.filebuffers.IFileBuffer)
195      */

196     public void underlyingFileDeleted(IFileBuffer buffer) {
197         //is not used here
198
}
199
200     /**
201      * @see org.eclipse.core.filebuffers.IFileBufferListener#stateChangeFailed(org.eclipse.core.filebuffers.IFileBuffer)
202      */

203     public void stateChangeFailed(IFileBuffer buffer) {
204         //is not used here
205
}
206
207     /**
208      * @see org.eclipse.ui.IPartListener2#partInputChanged(org.eclipse.ui.IWorkbenchPartReference)
209      */

210     public void partInputChanged(IWorkbenchPartReference partRef) {
211         // is not used here
212
}
213
214     /**
215      * @see org.eclipse.ui.IPartListener2#partActivated(org.eclipse.ui.IWorkbenchPartReference)
216      */

217     public void partActivated(IWorkbenchPartReference partRef) {
218         // is not used here
219
}
220
221     /**
222      * @see org.eclipse.ui.IPartListener2#partBroughtToTop(org.eclipse.ui.IWorkbenchPartReference)
223      */

224     public void partBroughtToTop(IWorkbenchPartReference partRef) {
225         // is not used here
226
}
227
228     /**
229      * @see org.eclipse.ui.IPartListener2#partDeactivated(org.eclipse.ui.IWorkbenchPartReference)
230      */

231     public void partDeactivated(IWorkbenchPartReference partRef) {
232         // is not used here
233
}
234
235
236
237
238
239 }
240
Popular Tags