KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > actions > ActionDelegateHelper


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.debug.ui.actions;
12
13
14 import org.eclipse.jdt.core.IClassFile;
15 import org.eclipse.jdt.core.ICompilationUnit;
16 import org.eclipse.jdt.core.IJavaElement;
17 import org.eclipse.jdt.core.IMember;
18 import org.eclipse.jdt.core.JavaModelException;
19 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
20 import org.eclipse.jdt.internal.debug.ui.snippeteditor.JavaSnippetEditor;
21 import org.eclipse.jdt.ui.IWorkingCopyManager;
22 import org.eclipse.jdt.ui.JavaUI;
23 import org.eclipse.jface.text.ITextSelection;
24 import org.eclipse.jface.viewers.ISelection;
25 import org.eclipse.ui.IEditorInput;
26 import org.eclipse.ui.IEditorPart;
27 import org.eclipse.ui.IPartListener;
28 import org.eclipse.ui.IWindowListener;
29 import org.eclipse.ui.IWorkbenchPage;
30 import org.eclipse.ui.IWorkbenchPart;
31 import org.eclipse.ui.IWorkbenchWindow;
32 import org.eclipse.ui.texteditor.ITextEditor;
33
34 public class ActionDelegateHelper implements IPartListener, IWindowListener {
35
36     private static ActionDelegateHelper fgDefault;
37     private IMember fMember= null;
38     private ITextEditor fTextEditor= null;
39     private ISelection fCurrentSelection= null;
40     private IWorkbenchWindow fCurrentWindow= null;
41     
42     public static ActionDelegateHelper getDefault() {
43         if (fgDefault == null) {
44             fgDefault= new ActionDelegateHelper();
45         }
46         return fgDefault;
47     }
48     
49     private ActionDelegateHelper() {
50         fCurrentWindow= JDIDebugUIPlugin.getActiveWorkbenchWindow();
51         if (fCurrentWindow != null) {
52             fCurrentWindow.getWorkbench().addWindowListener(this);
53             fCurrentWindow.getPartService().addPartListener(this);
54             IWorkbenchPage page= fCurrentWindow.getActivePage();
55             if (page != null) {
56                 IEditorPart part= page.getActiveEditor();
57                 checkToSetTextEditor(part);
58             }
59         }
60     }
61     
62     /**
63      * @see IPartListener#partActivated(IWorkbenchPart)
64      */

65     public void partActivated(IWorkbenchPart part) {
66         checkToSetTextEditor(part);
67     }
68
69     /**
70      * @see IPartListener#partBroughtToTop(IWorkbenchPart)
71      */

72     public void partBroughtToTop(IWorkbenchPart part) {
73     }
74
75     /**
76      * @see IPartListener#partClosed(IWorkbenchPart)
77      */

78     public void partClosed(IWorkbenchPart part) {
79         if (part == getTextEditor()) {
80             cleanup();
81         }
82     }
83
84     /**
85      * @see IPartListener#partDeactivated(IWorkbenchPart)
86      */

87     public void partDeactivated(IWorkbenchPart part) {
88     }
89
90     /**
91      * @see IPartListener#partOpened(IWorkbenchPart)
92      */

93     public void partOpened(IWorkbenchPart part) {
94     }
95
96     protected IMember getMember() {
97         return fMember;
98     }
99
100     protected void setMember(IMember member) {
101         fMember = member;
102     }
103     
104     protected void checkToSetTextEditor(IWorkbenchPart part) {
105         if (part instanceof ITextEditor) {
106             if (part instanceof JavaSnippetEditor) {
107                 cleanup();
108             } else {
109                 setTextEditor((ITextEditor)part);
110             }
111         }
112     }
113     
114     public IMember getCurrentMember(ITextSelection currentSelection) {
115         if (currentSelection == getCurrentSelection()) {
116             return getMember();
117         }
118         setCurrentSelection(currentSelection);
119         ITextEditor editor= getTextEditor();
120         if (editor == null) {
121             return null;
122         }
123         IEditorInput editorInput= editor.getEditorInput();
124         IMember m= null;
125         try {
126             IClassFile classFile= (IClassFile)editorInput.getAdapter(IClassFile.class);
127             if (classFile != null) {
128                 IJavaElement e= classFile.getElementAt(currentSelection.getOffset());
129                 if (e instanceof IMember) {
130                     m= (IMember)e;
131                 }
132             } else {
133                 IWorkingCopyManager manager= JavaUI.getWorkingCopyManager();
134                 ICompilationUnit unit= manager.getWorkingCopy(editorInput);
135                 if (unit != null) {
136                     synchronized (unit) {
137                         unit.reconcile(ICompilationUnit.NO_AST /*don't create ast*/, false/*don't force problem detection*/, null/*use primary owner*/, null/*no progress monitor*/);
138                     }
139                     IJavaElement e = unit.getElementAt(currentSelection.getOffset());
140                     if (e instanceof IMember) {
141                         m= (IMember)e;
142                     }
143                 }
144             }
145
146         } catch (JavaModelException jme) {
147             JDIDebugUIPlugin.log(jme);
148         }
149         
150         setMember(m);
151         return m;
152     }
153     
154     protected ITextEditor getTextEditor() {
155         return fTextEditor;
156     }
157
158     protected void setTextEditor(ITextEditor textEditor) {
159         fTextEditor = textEditor;
160     }
161     
162     protected ISelection getCurrentSelection() {
163         return fCurrentSelection;
164     }
165
166     protected void setCurrentSelection(ISelection currentSelection) {
167         fCurrentSelection = currentSelection;
168     }
169     
170     /**
171      * @see IWindowListener#windowActivated(IWorkbenchWindow)
172      */

173     public void windowActivated(IWorkbenchWindow window) {
174         if (fCurrentWindow != null) {
175             fCurrentWindow.getPartService().removePartListener(this);
176             cleanup();
177         }
178         fCurrentWindow= window;
179         fCurrentWindow.getPartService().addPartListener(this);
180         IWorkbenchPage page= window.getActivePage();
181         if (page != null) {
182             checkToSetTextEditor(page.getActiveEditor());
183         }
184     }
185
186     /**
187      * @see IWindowListener#windowClosed(IWorkbenchWindow)
188      */

189     public void windowClosed(IWorkbenchWindow window) {
190         if (fCurrentWindow == window) {
191             fCurrentWindow= null;
192             cleanup();
193         }
194     }
195
196     /**
197      * @see IWindowListener#windowDeactivated(IWorkbenchWindow)
198      */

199     public void windowDeactivated(IWorkbenchWindow window) {
200     }
201
202     /**
203      * @see IWindowListener#windowOpened(IWorkbenchWindow)
204      */

205     public void windowOpened(IWorkbenchWindow window) {
206     }
207     
208     protected void cleanup() {
209         setTextEditor(null);
210         setCurrentSelection(null);
211         setMember(null);
212     }
213 }
214
Popular Tags