KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.ui.IEditorPart;
14 import org.eclipse.ui.IPropertyListener;
15 import org.eclipse.ui.IWorkbenchPartConstants;
16 import org.eclipse.ui.IWorkbenchWindow;
17 import org.eclipse.ui.internal.tweaklets.TabBehaviour;
18 import org.eclipse.ui.internal.tweaklets.Tweaklets;
19
20 /**
21  * Action to toggle the pin state of an editor. If an editor is
22  * pinned, then it is not reused.
23  */

24 public class PinEditorAction extends ActiveEditorAction {
25     private IPropertyListener propListener = new IPropertyListener() {
26         public void propertyChanged(Object JavaDoc source, int propId) {
27             if (propId == WorkbenchPartReference.INTERNAL_PROPERTY_PINNED) {
28                 WorkbenchPartReference ref = (WorkbenchPartReference)source;
29                 setChecked(ref.isPinned());
30             } else if (propId == IWorkbenchPartConstants.PROP_DIRTY) {
31                 if (((TabBehaviour)Tweaklets.get(TabBehaviour.KEY)).autoPinOnDirty()) {
32                     WorkbenchPartReference ref = (WorkbenchPartReference) source;
33                     if (ref.isDirty()) {
34                         ref.setPinned(true);
35                     }
36                 }
37             }
38         }
39     };
40
41     /**
42      * Creates a PinEditorAction.
43      */

44     public PinEditorAction(IWorkbenchWindow window) {
45         super(WorkbenchMessages.PinEditorAction_text, window);
46         setActionDefinitionId("org.eclipse.ui.window.pinEditor"); //$NON-NLS-1$
47
setToolTipText(WorkbenchMessages.PinEditorAction_toolTip);
48         setId("org.eclipse.ui.internal.PinEditorAction"); //$NON-NLS-1$
49
// @issue need help constant for this?
50
// WorkbenchHelp.setHelp(this, new Object[] {IHelpContextIds.SAVE_ACTION});
51
setImageDescriptor(WorkbenchImages
52                 .getImageDescriptor(IWorkbenchGraphicConstants.IMG_ETOOL_PIN_EDITOR));
53         setDisabledImageDescriptor(WorkbenchImages
54                 .getImageDescriptor(IWorkbenchGraphicConstants.IMG_ETOOL_PIN_EDITOR_DISABLED));
55     }
56
57     /* (non-Javadoc)
58      * Method declared on IAction.
59      */

60     public void run() {
61         if (getWorkbenchWindow() == null) {
62             // action has been dispose
63
return;
64         }
65         IEditorPart editor = getActiveEditor();
66         if (editor != null) {
67             WorkbenchPartReference ref = getReference(editor);
68             ref.setPinned(isChecked());
69         }
70     }
71
72     private WorkbenchPartReference getReference(IEditorPart editor) {
73         return (WorkbenchPartReference)((EditorSite)editor.getSite()).getPartReference();
74     }
75     
76     /* (non-Javadoc)
77      * Method declared on ActiveEditorAction.
78      */

79     protected void updateState() {
80         if (getWorkbenchWindow() == null || getActivePage() == null) {
81             setChecked(false);
82             setEnabled(false);
83             return;
84         }
85
86         IEditorPart editor = getActiveEditor();
87         boolean enabled = (editor != null);
88         setEnabled(enabled);
89         if (enabled) {
90             setChecked(getReference(editor).isPinned());
91         } else {
92             setChecked(false);
93         }
94     }
95
96     /* (non-Javadoc)
97      * @see org.eclipse.ui.internal.ActiveEditorAction#editorActivated(org.eclipse.ui.IEditorPart)
98      */

99     protected void editorActivated(IEditorPart part) {
100         super.editorActivated(part);
101         if (part != null) {
102             getReference(part).addInternalPropertyListener(propListener);
103         }
104     }
105
106     /* (non-Javadoc)
107      * @see org.eclipse.ui.internal.ActiveEditorAction#editorDeactivated(org.eclipse.ui.IEditorPart)
108      */

109     protected void editorDeactivated(IEditorPart part) {
110         super.editorDeactivated(part);
111         if (part != null) {
112             getReference(part).removeInternalPropertyListener(propListener);
113         }
114     }
115
116     /* (non-Javadoc)
117      * @see org.eclipse.ui.actions.ActionFactory.IWorkbenchAction#dispose()
118      */

119     public void dispose() {
120         // deactivate current editor now before super dispose because active editor will be null after call
121
editorDeactivated(getActiveEditor());
122         super.dispose();
123     }
124 }
125
Popular Tags