001: /*******************************************************************************
002: * Copyright (c) 2003, 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.actions;
011:
012: import org.eclipse.jface.action.ActionContributionItem;
013: import org.eclipse.jface.preference.IPreferenceStore;
014: import org.eclipse.jface.util.IPropertyChangeListener;
015: import org.eclipse.jface.util.PropertyChangeEvent;
016: import org.eclipse.ui.IWorkbenchWindow;
017: import org.eclipse.ui.internal.IPreferenceConstants;
018: import org.eclipse.ui.internal.PinEditorAction;
019: import org.eclipse.ui.internal.WorkbenchPlugin;
020: import org.eclipse.ui.internal.tweaklets.TabBehaviour;
021: import org.eclipse.ui.internal.tweaklets.Tweaklets;
022:
023: /**
024: * This contribution item controls the visibility of the pin editor
025: * action based on the current preference value for reusing editors.
026: *
027: * @since 3.0
028: */
029: public class PinEditorContributionItem extends ActionContributionItem {
030: private IWorkbenchWindow window = null;
031:
032: private boolean reuseEditors = false;
033:
034: private IPropertyChangeListener prefListener = new IPropertyChangeListener() {
035: public void propertyChange(PropertyChangeEvent event) {
036: if (event.getProperty().equals(
037: IPreferenceConstants.REUSE_EDITORS_BOOLEAN)) {
038: if (getParent() != null) {
039: IPreferenceStore store = WorkbenchPlugin
040: .getDefault().getPreferenceStore();
041: reuseEditors = store
042: .getBoolean(IPreferenceConstants.REUSE_EDITORS_BOOLEAN)
043: || ((TabBehaviour) Tweaklets
044: .get(TabBehaviour.KEY))
045: .alwaysShowPinAction();
046: setVisible(reuseEditors);
047: getParent().markDirty();
048: if (window.getShell() != null
049: && !window.getShell().isDisposed()) {
050: // this property change notification could be from a non-ui thread
051: window.getShell().getDisplay().syncExec(
052: new Runnable() {
053: public void run() {
054: getParent().update(false);
055: }
056: });
057: }
058: }
059: }
060: }
061: };
062:
063: /**
064: * @param action
065: */
066: public PinEditorContributionItem(PinEditorAction action,
067: IWorkbenchWindow window) {
068: super (action);
069:
070: if (window == null) {
071: throw new IllegalArgumentException();
072: }
073: this .window = window;
074:
075: IPreferenceStore store = WorkbenchPlugin.getDefault()
076: .getPreferenceStore();
077: reuseEditors = store
078: .getBoolean(IPreferenceConstants.REUSE_EDITORS_BOOLEAN)
079: || ((TabBehaviour) Tweaklets.get(TabBehaviour.KEY))
080: .alwaysShowPinAction();
081: setVisible(reuseEditors);
082: WorkbenchPlugin.getDefault().getPreferenceStore()
083: .addPropertyChangeListener(prefListener);
084: }
085:
086: /* (non-Javadoc)
087: * @see org.eclipse.jface.action.IContributionItem#isVisible()
088: */
089: public boolean isVisible() {
090: // @issue the ActionContributionItem implementation of this method ignores the "visible" value set
091: return super .isVisible() && reuseEditors;
092: }
093:
094: /* (non-Javadoc)
095: * @see org.eclipse.jface.action.IContributionItem#dispose()
096: */
097: public void dispose() {
098: super.dispose();
099: WorkbenchPlugin.getDefault().getPreferenceStore()
100: .removePropertyChangeListener(prefListener);
101: }
102: }
|