KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > edit > ui > action > UndoAction


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2005 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: UndoAction.java,v 1.3 2005/06/08 06:20:52 nickb Exp $
16  */

17 package org.eclipse.emf.edit.ui.action;
18
19
20 import org.eclipse.jface.action.Action;
21 import org.eclipse.ui.IEditorPart;
22 import org.eclipse.ui.IWorkbenchPart;
23
24 import org.eclipse.emf.common.command.Command;
25 import org.eclipse.emf.edit.domain.EditingDomain;
26 import org.eclipse.emf.edit.domain.IEditingDomainProvider;
27 import org.eclipse.emf.edit.ui.EMFEditUIPlugin;
28
29
30 /**
31  * An undo action is implemented by using the {@link org.eclipse.emf.common.command.CommandStack}.
32  */

33 public class UndoAction extends Action
34 {
35   protected EditingDomain domain;
36
37   public UndoAction(EditingDomain domain)
38   {
39     super(EMFEditUIPlugin.INSTANCE.getString("_UI_Undo_menu_item", new Object JavaDoc [] { "" }));
40     this.domain = domain;
41     update();
42   }
43
44   public UndoAction()
45   {
46     super(EMFEditUIPlugin.INSTANCE.getString("_UI_Undo_menu_item", new Object JavaDoc [] { "" }));
47   }
48
49   /**
50    * This returns the action's domain.
51    */

52   public EditingDomain getEditingDomain()
53   {
54     return domain;
55   }
56
57   /**
58    * This sets the action's domain.
59    */

60   public void setEditingDomain(EditingDomain domain)
61   {
62     this.domain = domain;
63   }
64
65   public void run()
66   {
67     domain.getCommandStack().undo();
68   }
69
70   public void update()
71   {
72     setEnabled(domain.getCommandStack().canUndo());
73
74     Command undoCommand = domain.getCommandStack().getUndoCommand();
75     if (undoCommand != null && undoCommand.getLabel() != null)
76     {
77       setText(EMFEditUIPlugin.INSTANCE.getString("_UI_Undo_menu_item", new Object JavaDoc [] { undoCommand.getLabel() }));
78     }
79     else
80     {
81       setText(EMFEditUIPlugin.INSTANCE.getString("_UI_Undo_menu_item", new Object JavaDoc [] { "" }));
82     }
83
84     if (undoCommand != null && undoCommand.getDescription() != null)
85     {
86       setDescription(EMFEditUIPlugin.INSTANCE.getString("_UI_Undo_menu_item_description", new Object JavaDoc [] { undoCommand.getDescription() }));
87     }
88     else
89     {
90       setDescription(EMFEditUIPlugin.INSTANCE.getString("_UI_Undo_menu_item_simple_description"));
91     }
92   }
93
94   /**
95    * @deprecated As of EMF 2.1.0, replaced by {@link #setActiveWorkbenchPart}.
96    */

97   public void setActiveEditor(IEditorPart editorPart)
98   {
99     setActiveWorkbenchPart(editorPart);
100   }
101
102   /**
103    * @since 2.1.0
104    */

105   public void setActiveWorkbenchPart(IWorkbenchPart workbenchPart)
106   {
107     if (workbenchPart instanceof IEditingDomainProvider)
108     {
109       domain = ((IEditingDomainProvider)workbenchPart).getEditingDomain();
110     }
111   }
112 }
113
Popular Tags