1 11 package org.eclipse.ui.internal.texteditor; 12 13 import org.eclipse.jface.action.Action; 14 import org.eclipse.jface.action.IAction; 15 import org.eclipse.jface.viewers.ISelection; 16 import org.eclipse.jface.viewers.ISelectionProvider; 17 18 import org.eclipse.jface.text.Position; 19 import org.eclipse.jface.text.TextSelection; 20 21 import org.eclipse.ui.IEditorPart; 22 import org.eclipse.ui.IEditorSite; 23 import org.eclipse.ui.IWorkbenchPage; 24 import org.eclipse.ui.IWorkbenchWindow; 25 import org.eclipse.ui.IWorkbenchWindowActionDelegate; 26 import org.eclipse.ui.PartInitException; 27 import org.eclipse.ui.PlatformUI; 28 import org.eclipse.ui.texteditor.IAbstractTextEditorHelpContextIds; 29 import org.eclipse.ui.texteditor.ITextEditor; 30 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds; 31 32 38 public class GotoLastEditPositionAction extends Action implements IWorkbenchWindowActionDelegate { 39 40 41 private IWorkbenchWindow fWindow; 42 43 private IAction fAction; 44 45 48 public GotoLastEditPositionAction() { 49 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IAbstractTextEditorHelpContextIds.GOTO_LAST_EDIT_POSITION_ACTION); 50 setId(ITextEditorActionDefinitionIds.GOTO_LAST_EDIT_POSITION); 51 setActionDefinitionId(ITextEditorActionDefinitionIds.GOTO_LAST_EDIT_POSITION); 52 setEnabled(false); 53 } 54 55 58 public void init(IWorkbenchWindow window) { 59 fWindow= window; 60 } 61 62 65 public void run(IAction action) { 66 run(); 67 } 68 69 72 public void run() { 73 EditPosition editPosition= TextEditorPlugin.getDefault().getLastEditPosition(); 74 if (editPosition == null) 75 return; 76 77 final Position pos= editPosition.getPosition(); 78 if (pos == null || pos.isDeleted) 79 return; 80 81 IWorkbenchWindow window= getWindow(); 82 if (window == null) 83 return; 84 85 IWorkbenchPage page= window.getActivePage(); 86 87 IEditorPart editor; 88 try { 89 editor= page.openEditor(editPosition.getEditorInput(), editPosition.getEditorId()); 90 } catch (PartInitException ex) { 91 return; 92 } 93 94 if (editor instanceof ITextEditor) { 96 ITextEditor textEditor= (ITextEditor)editor; 97 textEditor.selectAndReveal(pos.offset, pos.length); 98 return; 99 } 100 101 105 if (editor != null) { 106 IEditorSite site= editor.getEditorSite(); 107 if (site == null) 108 return; 109 110 ISelectionProvider provider= editor.getEditorSite().getSelectionProvider(); 111 if (provider == null) 112 return; 113 114 provider.setSelection(new TextSelection(pos.offset, pos.length)); 115 } 116 } 117 118 121 public void selectionChanged(IAction action, ISelection selection) { 122 boolean enabled= TextEditorPlugin.getDefault().getLastEditPosition() != null; 123 setEnabled(enabled); 124 action.setEnabled(enabled); 125 126 if (!enabled) { 128 TextEditorPlugin.getDefault().addLastEditPositionDependentAction(action); 130 fAction= action; 132 } 133 } 134 135 140 private IWorkbenchWindow getWindow() { 141 if (fWindow == null) 142 fWindow= PlatformUI.getWorkbench().getActiveWorkbenchWindow(); 143 return fWindow; 144 } 145 146 149 public void dispose() { 150 fWindow= null; 151 TextEditorPlugin.getDefault().removeLastEditPositionDependentAction(fAction); 152 } 153 } 154 | Popular Tags |