KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > texteditor > GotoLastEditPositionAction


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.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 /**
33  * Goes to last edit position.
34  *
35  * @see org.eclipse.ui.internal.texteditor.EditPosition
36  * @since 2.1
37  */

38 public class GotoLastEditPositionAction extends Action implements IWorkbenchWindowActionDelegate {
39
40     /** The worbench window */
41     private IWorkbenchWindow fWindow;
42     /** The action */
43     private IAction fAction;
44
45     /**
46      * Creates a goto last edit action.
47      */

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     /*
56      * @see IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
57      */

58     public void init(IWorkbenchWindow window) {
59         fWindow= window;
60     }
61
62     /*
63      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
64      */

65     public void run(IAction action) {
66         run();
67     }
68
69     /*
70      * @see IAction#run()
71      */

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         // Optimization - could also use else branch
95
if (editor instanceof ITextEditor) {
96             ITextEditor textEditor= (ITextEditor)editor;
97             textEditor.selectAndReveal(pos.offset, pos.length);
98             return;
99         }
100
101         /*
102          * Workaround: send out a text selection
103          * XXX: Needs to be improved, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=32214
104          */

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     /*
119      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
120      */

121     public void selectionChanged(IAction action, ISelection selection) {
122         boolean enabled= TextEditorPlugin.getDefault().getLastEditPosition() != null;
123         setEnabled(enabled);
124         action.setEnabled(enabled);
125
126         // This is no longer needed once the action is enabled.
127
if (!enabled) {
128              // adding the same action twice has no effect.
129
TextEditorPlugin.getDefault().addLastEditPositionDependentAction(action);
130             // this is always the same action for this instance
131
fAction= action;
132         }
133     }
134
135     /**
136      * Returns the workbench window.
137      *
138      * @return the workbench window
139      */

140     private IWorkbenchWindow getWindow() {
141         if (fWindow == null)
142             fWindow= PlatformUI.getWorkbench().getActiveWorkbenchWindow();
143         return fWindow;
144     }
145
146     /*
147      * @see IWorkbenchWindowActionDelegate#dispose()
148      */

149     public void dispose() {
150         fWindow= null;
151         TextEditorPlugin.getDefault().removeLastEditPositionDependentAction(fAction);
152     }
153 }
154
Popular Tags