001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.texteditor;
011:
012: import org.eclipse.jface.action.Action;
013: import org.eclipse.jface.action.IAction;
014: import org.eclipse.jface.viewers.ISelection;
015: import org.eclipse.jface.viewers.ISelectionProvider;
016:
017: import org.eclipse.jface.text.Position;
018: import org.eclipse.jface.text.TextSelection;
019:
020: import org.eclipse.ui.IEditorPart;
021: import org.eclipse.ui.IEditorSite;
022: import org.eclipse.ui.IWorkbenchPage;
023: import org.eclipse.ui.IWorkbenchWindow;
024: import org.eclipse.ui.IWorkbenchWindowActionDelegate;
025: import org.eclipse.ui.PartInitException;
026: import org.eclipse.ui.PlatformUI;
027: import org.eclipse.ui.texteditor.IAbstractTextEditorHelpContextIds;
028: import org.eclipse.ui.texteditor.ITextEditor;
029: import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
030:
031: /**
032: * Goes to last edit position.
033: *
034: * @see org.eclipse.ui.internal.texteditor.EditPosition
035: * @since 2.1
036: */
037: public class GotoLastEditPositionAction extends Action implements
038: IWorkbenchWindowActionDelegate {
039:
040: /** The worbench window */
041: private IWorkbenchWindow fWindow;
042: /** The action */
043: private IAction fAction;
044:
045: /**
046: * Creates a goto last edit action.
047: */
048: public GotoLastEditPositionAction() {
049: PlatformUI
050: .getWorkbench()
051: .getHelpSystem()
052: .setHelp(
053: this ,
054: IAbstractTextEditorHelpContextIds.GOTO_LAST_EDIT_POSITION_ACTION);
055: setId(ITextEditorActionDefinitionIds.GOTO_LAST_EDIT_POSITION);
056: setActionDefinitionId(ITextEditorActionDefinitionIds.GOTO_LAST_EDIT_POSITION);
057: setEnabled(false);
058: }
059:
060: /*
061: * @see IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
062: */
063: public void init(IWorkbenchWindow window) {
064: fWindow = window;
065: }
066:
067: /*
068: * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
069: */
070: public void run(IAction action) {
071: run();
072: }
073:
074: /*
075: * @see IAction#run()
076: */
077: public void run() {
078: EditPosition editPosition = TextEditorPlugin.getDefault()
079: .getLastEditPosition();
080: if (editPosition == null)
081: return;
082:
083: final Position pos = editPosition.getPosition();
084: if (pos == null || pos.isDeleted)
085: return;
086:
087: IWorkbenchWindow window = getWindow();
088: if (window == null)
089: return;
090:
091: IWorkbenchPage page = window.getActivePage();
092:
093: IEditorPart editor;
094: try {
095: editor = page.openEditor(editPosition.getEditorInput(),
096: editPosition.getEditorId());
097: } catch (PartInitException ex) {
098: return;
099: }
100:
101: // Optimization - could also use else branch
102: if (editor instanceof ITextEditor) {
103: ITextEditor textEditor = (ITextEditor) editor;
104: textEditor.selectAndReveal(pos.offset, pos.length);
105: return;
106: }
107:
108: /*
109: * Workaround: send out a text selection
110: * XXX: Needs to be improved, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=32214
111: */
112: if (editor != null) {
113: IEditorSite site = editor.getEditorSite();
114: if (site == null)
115: return;
116:
117: ISelectionProvider provider = editor.getEditorSite()
118: .getSelectionProvider();
119: if (provider == null)
120: return;
121:
122: provider.setSelection(new TextSelection(pos.offset,
123: pos.length));
124: }
125: }
126:
127: /*
128: * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
129: */
130: public void selectionChanged(IAction action, ISelection selection) {
131: boolean enabled = TextEditorPlugin.getDefault()
132: .getLastEditPosition() != null;
133: setEnabled(enabled);
134: action.setEnabled(enabled);
135:
136: // This is no longer needed once the action is enabled.
137: if (!enabled) {
138: // adding the same action twice has no effect.
139: TextEditorPlugin.getDefault()
140: .addLastEditPositionDependentAction(action);
141: // this is always the same action for this instance
142: fAction = action;
143: }
144: }
145:
146: /**
147: * Returns the workbench window.
148: *
149: * @return the workbench window
150: */
151: private IWorkbenchWindow getWindow() {
152: if (fWindow == null)
153: fWindow = PlatformUI.getWorkbench()
154: .getActiveWorkbenchWindow();
155: return fWindow;
156: }
157:
158: /*
159: * @see IWorkbenchWindowActionDelegate#dispose()
160: */
161: public void dispose() {
162: fWindow = null;
163: TextEditorPlugin.getDefault()
164: .removeLastEditPositionDependentAction(fAction);
165: }
166: }
|