1 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 65 public void partActivated(IWorkbenchPart part) { 66 checkToSetTextEditor(part); 67 } 68 69 72 public void partBroughtToTop(IWorkbenchPart part) { 73 } 74 75 78 public void partClosed(IWorkbenchPart part) { 79 if (part == getTextEditor()) { 80 cleanup(); 81 } 82 } 83 84 87 public void partDeactivated(IWorkbenchPart part) { 88 } 89 90 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 , false, null, null); 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 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 189 public void windowClosed(IWorkbenchWindow window) { 190 if (fCurrentWindow == window) { 191 fCurrentWindow= null; 192 cleanup(); 193 } 194 } 195 196 199 public void windowDeactivated(IWorkbenchWindow window) { 200 } 201 202 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 |