KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ViewPluginAction


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.ui.internal;
12
13 import org.eclipse.core.runtime.IConfigurationElement;
14 import org.eclipse.ui.IActionDelegate;
15 import org.eclipse.ui.IViewActionDelegate;
16 import org.eclipse.ui.IViewPart;
17 import org.eclipse.ui.WorkbenchException;
18
19 /**
20  * This class extends regular plugin action with the
21  * additional requirement that the delegate has
22  * to implement interface IViewActionDeelgate.
23  * This interface has one additional method (init)
24  * whose purpose is to initialize the delegate with
25  * the view part in which the action is intended to run.
26  */

27 public final class ViewPluginAction extends PartPluginAction {
28     private IViewPart viewPart;
29
30     /**
31      * This class adds the requirement that action delegates
32      * loaded on demand implement IViewActionDelegate
33      */

34     public ViewPluginAction(IConfigurationElement actionElement,
35             IViewPart viewPart, String JavaDoc id, int style) {
36         super(actionElement, id, style);
37         this.viewPart = viewPart;
38         registerSelectionListener(viewPart);
39     }
40
41     /* (non-Javadoc)
42      * Method declared on PluginAction.
43      */

44     protected IActionDelegate validateDelegate(Object JavaDoc obj)
45             throws WorkbenchException {
46         if (obj instanceof IViewActionDelegate) {
47             return (IViewActionDelegate) obj;
48         } else {
49             throw new WorkbenchException(
50                     "Action must implement IViewActionDelegate"); //$NON-NLS-1$
51
}
52     }
53
54     /* (non-Javadoc)
55      * Method declared on PluginAction.
56      */

57     protected void initDelegate() {
58         super.initDelegate();
59         ((IViewActionDelegate) getDelegate()).init(viewPart);
60     }
61
62     /**
63      * Returns true if the view has been set
64      * The view may be null after the constructor is called and
65      * before the view is stored. We cannot create the delegate
66      * at that time.
67      */

68     public boolean isOkToCreateDelegate() {
69         return super.isOkToCreateDelegate() && viewPart != null;
70     }
71     
72     /* (non-Javadoc)
73      * @see org.eclipse.ui.internal.PluginAction#dispose()
74      */

75     public void dispose() {
76         unregisterSelectionListener(viewPart);
77         super.dispose();
78     }
79 }
80
Popular Tags