KickJava   Java API By Example, From Geeks To Geeks.

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


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.IEditorActionDelegate;
16 import org.eclipse.ui.IEditorPart;
17 import org.eclipse.ui.WorkbenchException;
18
19 /**
20  * Extends PartPluginAction for usage in editor parts. Objects
21  * of this class are created by reading the registry (extension point "editorActions").
22  */

23 public final class EditorPluginAction extends PartPluginAction {
24     private IEditorPart currentEditor;
25
26     /**
27      * This class adds the requirement that action delegates
28      * loaded on demand implement IViewActionDelegate
29      */

30     public EditorPluginAction(IConfigurationElement actionElement,
31             IEditorPart part, String JavaDoc id, int style) {
32         super(actionElement, id, style);
33         if (part != null) {
34             editorChanged(part);
35         }
36     }
37
38     /* (non-Javadoc)
39      * Method declared on PluginAction.
40      */

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

54     protected void initDelegate() {
55         super.initDelegate();
56         ((IEditorActionDelegate) getDelegate()).setActiveEditor(this,
57                 currentEditor);
58     }
59
60     /**
61      * Handles editor change by re-registering for selection
62      * changes and updating IEditorActionDelegate.
63      */

64     public void editorChanged(IEditorPart part) {
65         if (currentEditor != null) {
66             unregisterSelectionListener(currentEditor);
67         }
68
69         currentEditor = part;
70
71         if (getDelegate() == null && isOkToCreateDelegate()) {
72             createDelegate();
73         }
74         if (getDelegate() != null) {
75             ((IEditorActionDelegate) getDelegate()).setActiveEditor(this, part);
76         }
77
78         if (part != null) {
79             registerSelectionListener(part);
80         }
81     }
82     
83     /* (non-Javadoc)
84      * @see org.eclipse.ui.internal.PluginAction#dispose()
85      */

86     public void dispose() {
87         if (currentEditor != null) {
88             unregisterSelectionListener(currentEditor);
89             currentEditor = null;
90         }
91         super.dispose();
92     }
93 }
94
Popular Tags