| 1 |
/** |
| 2 |
* Copyright (c) 2002-2006 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 v2.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v20.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM - Initial API and implementation |
| 10 |
*/ |
| 11 |
package org.eclipse.emf.edit.ui.action; |
| 12 |
|
| 13 |
|
| 14 |
import org.eclipse.jface.action.Action; |
| 15 |
import org.eclipse.ui.IEditorPart; |
| 16 |
import org.eclipse.ui.IWorkbenchPart; |
| 17 |
|
| 18 |
import org.eclipse.emf.common.command.Command; |
| 19 |
import org.eclipse.emf.edit.domain.EditingDomain; |
| 20 |
import org.eclipse.emf.edit.domain.IEditingDomainProvider; |
| 21 |
import org.eclipse.emf.edit.ui.EMFEditUIPlugin; |
| 22 |
|
| 23 |
|
| 24 |
/** |
| 25 |
* An undo action is implemented by using the {@link org.eclipse.emf.common.command.CommandStack}. |
| 26 |
*/ |
| 27 |
public class UndoAction extends Action |
| 28 |
{ |
| 29 |
protected EditingDomain domain; |
| 30 |
|
| 31 |
public UndoAction(EditingDomain domain) |
| 32 |
{ |
| 33 |
super(EMFEditUIPlugin.INSTANCE.getString("_UI_Undo_menu_item", new Object [] { "" })); |
| 34 |
this.domain = domain; |
| 35 |
update(); |
| 36 |
} |
| 37 |
|
| 38 |
public UndoAction() |
| 39 |
{ |
| 40 |
super(EMFEditUIPlugin.INSTANCE.getString("_UI_Undo_menu_item", new Object [] { "" })); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* This returns the action's domain. |
| 45 |
*/ |
| 46 |
public EditingDomain getEditingDomain() |
| 47 |
{ |
| 48 |
return domain; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* This sets the action's domain. |
| 53 |
*/ |
| 54 |
public void setEditingDomain(EditingDomain domain) |
| 55 |
{ |
| 56 |
this.domain = domain; |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
public void run() |
| 61 |
{ |
| 62 |
domain.getCommandStack().undo(); |
| 63 |
} |
| 64 |
|
| 65 |
public void update() |
| 66 |
{ |
| 67 |
setEnabled(domain.getCommandStack().canUndo()); |
| 68 |
|
| 69 |
Command undoCommand = domain.getCommandStack().getUndoCommand(); |
| 70 |
if (undoCommand != null && undoCommand.getLabel() != null) |
| 71 |
{ |
| 72 |
setText(EMFEditUIPlugin.INSTANCE.getString("_UI_Undo_menu_item", new Object [] { undoCommand.getLabel() })); |
| 73 |
} |
| 74 |
else |
| 75 |
{ |
| 76 |
setText(EMFEditUIPlugin.INSTANCE.getString("_UI_Undo_menu_item", new Object [] { "" })); |
| 77 |
} |
| 78 |
|
| 79 |
if (undoCommand != null && undoCommand.getDescription() != null) |
| 80 |
{ |
| 81 |
setDescription(EMFEditUIPlugin.INSTANCE.getString("_UI_Undo_menu_item_description", new Object [] { undoCommand.getDescription() })); |
| 82 |
} |
| 83 |
else |
| 84 |
{ |
| 85 |
setDescription(EMFEditUIPlugin.INSTANCE.getString("_UI_Undo_menu_item_simple_description")); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* @deprecated As of EMF 2.1.0, replaced by {@link #setActiveWorkbenchPart}. |
| 91 |
*/ |
| 92 |
@Deprecated |
| 93 |
public void setActiveEditor(IEditorPart editorPart) |
| 94 |
{ |
| 95 |
setActiveWorkbenchPart(editorPart); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* @since 2.1.0 |
| 100 |
*/ |
| 101 |
public void setActiveWorkbenchPart(IWorkbenchPart workbenchPart) |
| 102 |
{ |
| 103 |
if (workbenchPart instanceof IEditingDomainProvider) |
| 104 |
{ |
| 105 |
domain = ((IEditingDomainProvider)workbenchPart).getEditingDomain(); |
| 106 |
} |
| 107 |
} |
| 108 |
} |