david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
amywu | ecebb04 | 2007-04-10 20:07:35 +0000 | [diff] [blame] | 2 | * Copyright (c) 2001, 2005 IBM Corporation and others. |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 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 |
amywu | ecebb04 | 2007-04-10 20:07:35 +0000 | [diff] [blame] | 7 | * |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | * Jens Lukowski/Innoopract - initial renaming/restructuring |
| 11 | * |
| 12 | *******************************************************************************/ |
nitind | bca2663 | 2005-03-30 18:13:32 +0000 | [diff] [blame] | 13 | package org.eclipse.wst.sse.ui.internal.contentoutline; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 14 | |
| 15 | import org.eclipse.jface.action.Action; |
| 16 | import org.eclipse.jface.action.IAction; |
| 17 | import org.eclipse.jface.preference.IPreferenceStore; |
| 18 | import org.eclipse.ui.texteditor.IUpdate; |
| 19 | |
| 20 | /** |
| 21 | * An IAction.AS_CHECK_BOX action that sets and gets its checked state along |
| 22 | * with a value from a preference store. Should be used with |
| 23 | * PropertyChangeUpdateActionContributionItem to listen to changes in the |
nitind | 56dbcb7 | 2004-11-30 04:07:19 +0000 | [diff] [blame] | 24 | * store and update the checked state from PropertyChangeEvents. |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 25 | */ |
| 26 | public class PropertyChangeUpdateAction extends Action implements IUpdate { |
| 27 | private String fPreferenceKey; |
| 28 | private IPreferenceStore fStore; |
| 29 | private boolean fUpdateFromPropertyChange = true; |
| 30 | |
| 31 | public PropertyChangeUpdateAction(String text, IPreferenceStore store, String preferenceKey, boolean defaultValue) { |
| 32 | super(text, IAction.AS_CHECK_BOX); |
| 33 | fPreferenceKey = preferenceKey; |
| 34 | fStore = store; |
| 35 | fStore.setDefault(getPreferenceKey(), defaultValue); |
nitind | 22def7a | 2005-01-28 03:09:21 +0000 | [diff] [blame] | 36 | setId(getPreferenceKey()); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 37 | setChecked(getPreferenceStore().getBoolean(getPreferenceKey())); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @return Returns the orderPreferenceKey. |
| 42 | */ |
| 43 | public String getPreferenceKey() { |
| 44 | return fPreferenceKey; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @return Returns the store. |
| 49 | */ |
| 50 | public IPreferenceStore getPreferenceStore() { |
| 51 | return fStore; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @return Returns the updateFromPropertyChange. |
| 56 | */ |
| 57 | public boolean isUpdateFromPropertyChange() { |
| 58 | return fUpdateFromPropertyChange; |
| 59 | } |
| 60 | |
| 61 | public final void run() { |
| 62 | super.run(); |
| 63 | fStore.setValue(getPreferenceKey(), isChecked()); |
| 64 | if (!isUpdateFromPropertyChange()) |
| 65 | update(); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @param updateFromPropertyChange |
| 70 | * The updateFromPropertyChange to set. |
| 71 | */ |
| 72 | public void setUpdateFromPropertyChange(boolean updateFromPropertyChange) { |
| 73 | fUpdateFromPropertyChange = updateFromPropertyChange; |
| 74 | } |
| 75 | |
| 76 | public void update() { |
| 77 | setChecked(fStore.getBoolean(getPreferenceKey())); |
| 78 | } |
| 79 | |
| 80 | } |