001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal;
011:
012: import org.eclipse.ui.IEditorPart;
013: import org.eclipse.ui.IPropertyListener;
014: import org.eclipse.ui.IWorkbenchPartConstants;
015: import org.eclipse.ui.IWorkbenchWindow;
016: import org.eclipse.ui.internal.tweaklets.TabBehaviour;
017: import org.eclipse.ui.internal.tweaklets.Tweaklets;
018:
019: /**
020: * Action to toggle the pin state of an editor. If an editor is
021: * pinned, then it is not reused.
022: */
023: public class PinEditorAction extends ActiveEditorAction {
024: private IPropertyListener propListener = new IPropertyListener() {
025: public void propertyChanged(Object source, int propId) {
026: if (propId == WorkbenchPartReference.INTERNAL_PROPERTY_PINNED) {
027: WorkbenchPartReference ref = (WorkbenchPartReference) source;
028: setChecked(ref.isPinned());
029: } else if (propId == IWorkbenchPartConstants.PROP_DIRTY) {
030: if (((TabBehaviour) Tweaklets.get(TabBehaviour.KEY))
031: .autoPinOnDirty()) {
032: WorkbenchPartReference ref = (WorkbenchPartReference) source;
033: if (ref.isDirty()) {
034: ref.setPinned(true);
035: }
036: }
037: }
038: }
039: };
040:
041: /**
042: * Creates a PinEditorAction.
043: */
044: public PinEditorAction(IWorkbenchWindow window) {
045: super (WorkbenchMessages.PinEditorAction_text, window);
046: setActionDefinitionId("org.eclipse.ui.window.pinEditor"); //$NON-NLS-1$
047: setToolTipText(WorkbenchMessages.PinEditorAction_toolTip);
048: setId("org.eclipse.ui.internal.PinEditorAction"); //$NON-NLS-1$
049: // @issue need help constant for this?
050: // WorkbenchHelp.setHelp(this, new Object[] {IHelpContextIds.SAVE_ACTION});
051: setImageDescriptor(WorkbenchImages
052: .getImageDescriptor(IWorkbenchGraphicConstants.IMG_ETOOL_PIN_EDITOR));
053: setDisabledImageDescriptor(WorkbenchImages
054: .getImageDescriptor(IWorkbenchGraphicConstants.IMG_ETOOL_PIN_EDITOR_DISABLED));
055: }
056:
057: /* (non-Javadoc)
058: * Method declared on IAction.
059: */
060: public void run() {
061: if (getWorkbenchWindow() == null) {
062: // action has been dispose
063: return;
064: }
065: IEditorPart editor = getActiveEditor();
066: if (editor != null) {
067: WorkbenchPartReference ref = getReference(editor);
068: ref.setPinned(isChecked());
069: }
070: }
071:
072: private WorkbenchPartReference getReference(IEditorPart editor) {
073: return (WorkbenchPartReference) ((EditorSite) editor.getSite())
074: .getPartReference();
075: }
076:
077: /* (non-Javadoc)
078: * Method declared on ActiveEditorAction.
079: */
080: protected void updateState() {
081: if (getWorkbenchWindow() == null || getActivePage() == null) {
082: setChecked(false);
083: setEnabled(false);
084: return;
085: }
086:
087: IEditorPart editor = getActiveEditor();
088: boolean enabled = (editor != null);
089: setEnabled(enabled);
090: if (enabled) {
091: setChecked(getReference(editor).isPinned());
092: } else {
093: setChecked(false);
094: }
095: }
096:
097: /* (non-Javadoc)
098: * @see org.eclipse.ui.internal.ActiveEditorAction#editorActivated(org.eclipse.ui.IEditorPart)
099: */
100: protected void editorActivated(IEditorPart part) {
101: super .editorActivated(part);
102: if (part != null) {
103: getReference(part)
104: .addInternalPropertyListener(propListener);
105: }
106: }
107:
108: /* (non-Javadoc)
109: * @see org.eclipse.ui.internal.ActiveEditorAction#editorDeactivated(org.eclipse.ui.IEditorPart)
110: */
111: protected void editorDeactivated(IEditorPart part) {
112: super .editorDeactivated(part);
113: if (part != null) {
114: getReference(part).removeInternalPropertyListener(
115: propListener);
116: }
117: }
118:
119: /* (non-Javadoc)
120: * @see org.eclipse.ui.actions.ActionFactory.IWorkbenchAction#dispose()
121: */
122: public void dispose() {
123: // deactivate current editor now before super dispose because active editor will be null after call
124: editorDeactivated(getActiveEditor());
125: super.dispose();
126: }
127: }
|