KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > commands > ToggleState


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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
12 package org.eclipse.jface.commands;
13
14 import org.eclipse.jface.menus.IMenuStateIds;
15 import org.eclipse.jface.preference.IPreferenceStore;
16
17 /**
18  * <p>
19  * A piece of state storing a {@link Boolean}.
20  * </p>
21  * <p>
22  * If this state is registered using {@link IMenuStateIds#STYLE}, then it will
23  * control the presentation of the command if displayed in the menus, tool bars
24  * or status line.
25  * </p>
26  * <p>
27  * Clients may instantiate this class, but must not extend.
28  * </p>
29  *
30  * @since 3.2
31  */

32 public class ToggleState extends PersistentState {
33
34     /**
35      * Constructs a new <code>ToggleState</code>. By default, the toggle is
36      * off (e.g., <code>false</code>).
37      */

38     public ToggleState() {
39         setValue(Boolean.FALSE);
40     }
41
42     public final void load(final IPreferenceStore store,
43             final String JavaDoc preferenceKey) {
44         final boolean currentValue = ((Boolean JavaDoc) getValue()).booleanValue();
45         store.setDefault(preferenceKey, currentValue);
46         if (shouldPersist() && (store.contains(preferenceKey))) {
47             final boolean value = store.getBoolean(preferenceKey);
48             setValue(value ? Boolean.TRUE : Boolean.FALSE);
49         }
50     }
51
52     public final void save(final IPreferenceStore store,
53             final String JavaDoc preferenceKey) {
54         if (shouldPersist()) {
55             final Object JavaDoc value = getValue();
56             if (value instanceof Boolean JavaDoc) {
57                 store.setValue(preferenceKey, ((Boolean JavaDoc) value).booleanValue());
58             }
59         }
60     }
61
62     public void setValue(final Object JavaDoc value) {
63         if (!(value instanceof Boolean JavaDoc)) {
64             throw new IllegalArgumentException JavaDoc(
65                     "ToggleState takes a Boolean as a value"); //$NON-NLS-1$
66
}
67
68         super.setValue(value);
69     }
70 }
71
Popular Tags