KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > synchronize > ActionDelegateWrapper


1 /*******************************************************************************
2  * Copyright (c) 2000, 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 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.team.internal.ui.synchronize;
12
13 import org.eclipse.jface.action.Action;
14 import org.eclipse.jface.viewers.ISelectionChangedListener;
15 import org.eclipse.jface.viewers.SelectionChangedEvent;
16 import org.eclipse.swt.events.DisposeEvent;
17 import org.eclipse.swt.events.DisposeListener;
18 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
19 import org.eclipse.ui.*;
20
21 /**
22  * An Action that wraps IActionDelegates so they can be used programatically
23  * in toolbars, etc.
24  */

25 public class ActionDelegateWrapper extends Action implements ISelectionChangedListener {
26     
27     private IActionDelegate delegate;
28
29     public ActionDelegateWrapper(IActionDelegate delegate, ISynchronizePageConfiguration configuration) {
30         this.delegate = delegate;
31         IWorkbenchPart part = configuration.getSite().getPart();
32         if(part != null) {
33             if (delegate instanceof IObjectActionDelegate) {
34                 ((IObjectActionDelegate)delegate).setActivePart(this, part);
35             }
36             if (part instanceof IViewPart
37                     && delegate instanceof IViewActionDelegate) {
38                 ((IViewActionDelegate)delegate).init((IViewPart)part);
39             }
40             if (part instanceof IEditorPart
41                     && delegate instanceof IViewActionDelegate) {
42                 ((IEditorActionDelegate)delegate).setActiveEditor(this, (IEditorPart)part);
43             }
44         }
45         initialize(configuration);
46     }
47     
48     public ActionDelegateWrapper(IActionDelegate delegate, ISynchronizePageConfiguration configuration, String JavaDoc id) {
49         this(delegate, configuration);
50         setId(id);
51         setActionDefinitionId(id);
52     }
53
54     /**
55      * Method invoked from the constructor when a configuration is provided.
56      * The default implementation registers the action as a selection change
57      * listener. Subclass may override.
58      * @param configuration the synchronize page configuration
59      */

60     protected void initialize(final ISynchronizePageConfiguration configuration) {
61         configuration.getSite().getSelectionProvider().addSelectionChangedListener(this);
62         configuration.getPage().getViewer().getControl().addDisposeListener(new DisposeListener() {
63             public void widgetDisposed(DisposeEvent e) {
64                 configuration.getSite().getSelectionProvider().removeSelectionChangedListener(ActionDelegateWrapper.this);
65             }
66         });
67     }
68     
69     /* (non-Javadoc)
70      * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
71      */

72     public void selectionChanged(SelectionChangedEvent event) {
73         getDelegate().selectionChanged(this, event.getSelection());
74     }
75     
76     /* (non-Javadoc)
77      * @see org.eclipse.jface.action.IAction#run()
78      */

79     public void run() {
80         getDelegate().run(this);
81     }
82     
83     /**
84      * Return the delegate associated with this action.
85      * @return the delegate associated with this action
86      */

87     public IActionDelegate getDelegate() {
88         return delegate;
89     }
90
91 }
92
Popular Tags