| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2003, 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.actions; |
| 12 | |
| 13 | import org.eclipse.jface.action.ActionContributionItem; |
| 14 | import org.eclipse.jface.preference.IPreferenceStore; |
| 15 | import org.eclipse.jface.util.IPropertyChangeListener; |
| 16 | import org.eclipse.jface.util.PropertyChangeEvent; |
| 17 | import org.eclipse.ui.IWorkbenchWindow; |
| 18 | import org.eclipse.ui.internal.IPreferenceConstants; |
| 19 | import org.eclipse.ui.internal.PinEditorAction; |
| 20 | import org.eclipse.ui.internal.WorkbenchPlugin; |
| 21 | import org.eclipse.ui.internal.tweaklets.TabBehaviour; |
| 22 | import org.eclipse.ui.internal.tweaklets.Tweaklets; |
| 23 | |
| 24 | /** |
| 25 | * This contribution item controls the visibility of the pin editor |
| 26 | * action based on the current preference value for reusing editors. |
| 27 | * |
| 28 | * @since 3.0 |
| 29 | */ |
| 30 | public class PinEditorContributionItem extends ActionContributionItem { |
| 31 | private IWorkbenchWindow window = null; |
| 32 | |
| 33 | private boolean reuseEditors = false; |
| 34 | |
| 35 | private IPropertyChangeListener prefListener = new IPropertyChangeListener() { |
| 36 | public void propertyChange(PropertyChangeEvent event) { |
| 37 | if (event.getProperty().equals( |
| 38 | IPreferenceConstants.REUSE_EDITORS_BOOLEAN)) { |
| 39 | if (getParent() != null) { |
| 40 | IPreferenceStore store = WorkbenchPlugin.getDefault() |
| 41 | .getPreferenceStore(); |
| 42 | reuseEditors = store |
| 43 | .getBoolean(IPreferenceConstants.REUSE_EDITORS_BOOLEAN) |
| 44 | || ((TabBehaviour)Tweaklets.get(TabBehaviour.KEY)).alwaysShowPinAction(); |
| 45 | setVisible(reuseEditors); |
| 46 | getParent().markDirty(); |
| 47 | if (window.getShell() != null |
| 48 | && !window.getShell().isDisposed()) { |
| 49 | // this property change notification could be from a non-ui thread |
| 50 | window.getShell().getDisplay().syncExec(new Runnable() { |
| 51 | public void run() { |
| 52 | getParent().update(false); |
| 53 | } |
| 54 | }); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | /** |
| 62 | * @param action |
| 63 | */ |
| 64 | public PinEditorContributionItem(PinEditorAction action, |
| 65 | IWorkbenchWindow window) { |
| 66 | super(action); |
| 67 | |
| 68 | if (window == null) { |
| 69 | throw new IllegalArgumentException(); |
| 70 | } |
| 71 | this.window = window; |
| 72 | |
| 73 | IPreferenceStore store = WorkbenchPlugin.getDefault().getPreferenceStore(); |
| 74 | reuseEditors = store |
| 75 | .getBoolean(IPreferenceConstants.REUSE_EDITORS_BOOLEAN) |
| 76 | || ((TabBehaviour)Tweaklets.get(TabBehaviour.KEY)).alwaysShowPinAction(); |
| 77 | setVisible(reuseEditors); |
| 78 | WorkbenchPlugin.getDefault().getPreferenceStore() |
| 79 | .addPropertyChangeListener(prefListener); |
| 80 | } |
| 81 | |
| 82 | /* (non-Javadoc) |
| 83 | * @see org.eclipse.jface.action.IContributionItem#isVisible() |
| 84 | */ |
| 85 | public boolean isVisible() { |
| 86 | // @issue the ActionContributionItem implementation of this method ignores the "visible" value set |
| 87 | return super.isVisible() && reuseEditors; |
| 88 | } |
| 89 | |
| 90 | /* (non-Javadoc) |
| 91 | * @see org.eclipse.jface.action.IContributionItem#dispose() |
| 92 | */ |
| 93 | public void dispose() { |
| 94 | super.dispose(); |
| 95 | WorkbenchPlugin.getDefault().getPreferenceStore() |
| 96 | .removePropertyChangeListener(prefListener); |
| 97 | } |
| 98 | } |