KickJava   Java API By Example, From Geeks To Geeks.

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


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: DelegatingCommandAction.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.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.ISelectionChangedListener;
23 import org.eclipse.jface.viewers.ISelectionProvider;
24 import org.eclipse.jface.viewers.SelectionChangedEvent;
25 import org.eclipse.ui.IActionDelegate;
26 import org.eclipse.ui.IEditorActionDelegate;
27 import org.eclipse.ui.IEditorPart;
28 import org.eclipse.ui.ISelectionListener;
29 import org.eclipse.ui.IViewActionDelegate;
30 import org.eclipse.ui.IViewPart;
31 import org.eclipse.ui.IWorkbenchPart;
32
33
34 /**
35  * This class wraps an {@link IActionDelegate}, e.g., a {@link CommandAction}, to make it into an {@link Action}.
36  * Even if the action delegate implements {@link IActionDelegate2}, this class will still ony use the older interface
37  * (i.e. it will not call {@link IActionDelegate2#init(IAction) init}, {@link IActoinDelegate2#runWithEvent
38  * runWithEvent}, or {@link IActionDelegate2#dispose dispose}, since it does not have the information required
39  * to do so).
40  */

41 public class DelegatingCommandAction extends Action implements ISelectionListener, ISelectionChangedListener
42 {
43   /**
44    * This is the action delegate we're wrapping.
45    * @since 2.1.0
46    */

47   protected IActionDelegate actionDelegate;
48
49   /**
50    * If the action delegate is associated with an editor, is also recorded here.
51    * This field was retained for backwards compatibility.
52    * @deprecated As of EMF 2.1.0, replaced by {@link #actionDelegate}.
53    */

54   protected IEditorActionDelegate editorActionDelegate;
55
56   /**
57    * This is the current workbench part.
58    */

59   protected IWorkbenchPart workbenchPart;
60
61   /**
62    * If the workbench part is an editor, it is also recorded here.
63    * This field was retained for backwards compatibility.
64    * @deprecated As of EMF 2.1.0, replaced by {@link #workbenchPart}.
65    */

66   protected IEditorPart editorPart;
67
68   /**
69    * This constructs an instance.
70    * @since 2.1.0
71    */

72   public DelegatingCommandAction(IActionDelegate actionDelegate)
73   {
74     this.actionDelegate = actionDelegate;
75     if (actionDelegate instanceof IEditorActionDelegate)
76     {
77       editorActionDelegate = (IEditorActionDelegate)actionDelegate;
78     }
79   }
80
81   /**
82    * This constructor is simply retained for binary compatibility.
83    * It just calls the {@link #DelegatingCommandAction(IActionDelegate) new form}.
84    */

85   public DelegatingCommandAction(IEditorActionDelegate editorActionDelegate)
86   {
87     this((IActionDelegate)editorActionDelegate);
88   }
89
90   public void selectionChanged(SelectionChangedEvent event)
91   {
92     handleSelection(event.getSelection());
93   }
94
95   public void selectionChanged(IWorkbenchPart part, ISelection selection)
96   {
97     handleSelection(selection);
98   }
99
100   protected void selectionChanged(ISelection selection)
101   {
102     if (actionDelegate != null)
103     {
104       // This is for backwards compatibility, since the constructor may have been overridden before it was expected
105
// to set actionDelegate.
106
//
107
editorActionDelegate.selectionChanged(this, selection);
108     }
109     else
110     {
111       actionDelegate.selectionChanged(this, selection);
112     }
113   }
114
115   protected void handleSelection(ISelection selection)
116   {
117     selectionChanged(selection);
118   }
119
120   /**
121    * @since 2.1.0
122    */

123   protected void registerSelectionListener(IWorkbenchPart workbenchPart)
124   {
125     ISelectionProvider selectionProvider = workbenchPart.getSite().getSelectionProvider();
126     if (selectionProvider != null)
127     {
128       selectionProvider.addSelectionChangedListener(this);
129       handleSelection(selectionProvider.getSelection());
130     }
131   }
132
133   /**
134    * @deprecated As of EMF 2.1.0, replaced by {@link #registerSelectionListener(IWorkbenchPart) registerSelectionListener}.
135    */

136   protected void registerSelectionListener(IEditorPart editorPart)
137   {
138     registerSelectionListener((IWorkbenchPart)editorPart);
139   }
140
141   /**
142    * @since 2.1.0
143    */

144   protected void unregisterSelectionListener(IWorkbenchPart workbenchPart)
145   {
146     ISelectionProvider selectionProvider = workbenchPart.getSite().getSelectionProvider();
147     if (selectionProvider != null)
148     {
149       selectionProvider.removeSelectionChangedListener(this);
150     }
151   }
152
153   /**
154    * @deprecated As of EMF 2.1.0, replaced by {@link #unregisterSelectionListener(IWorkbenchPart) unregisterSelectionListener}.
155    */

156   protected void unregisterSelectionListener(IEditorPart editorPart)
157   {
158     unregisterSelectionListener((IWorkbenchPart)editorPart);
159   }
160
161   /**
162    * @deprecated As of EMF 2.1.0, replaced by {@link #setActiveWorkbenchPart}.
163    */

164   public void setActiveEditor(IEditorPart editorPart)
165   {
166     setActiveWorkbenchPart(editorPart);
167     this.editorPart = editorPart;
168   }
169
170   public void setActiveWorkbenchPart(IWorkbenchPart workbenchPart)
171   {
172     if (this.workbenchPart != workbenchPart)
173     {
174       if (this.workbenchPart != null)
175       {
176         unregisterSelectionListener(this.workbenchPart);
177       }
178       this.workbenchPart = workbenchPart;
179
180       if (actionDelegate == null)
181       {
182         // This is for backwards compatibility, since the constructor may have been overridden before it was expected
183
// to set actionDelegate.
184
//
185
editorActionDelegate.setActiveEditor(this, (IEditorPart)workbenchPart);
186       }
187       else if (actionDelegate instanceof IEditorActionDelegate)
188       {
189         ((IEditorActionDelegate)actionDelegate).setActiveEditor(this, (IEditorPart)workbenchPart);
190       }
191       else
192       {
193         ((IViewActionDelegate)actionDelegate).init((IViewPart)workbenchPart);
194       }
195
196       if (workbenchPart != null)
197       {
198         registerSelectionListener(workbenchPart);
199       }
200     }
201   }
202
203   public void run()
204   {
205     actionDelegate.run(this);
206   }
207 }
208
Popular Tags