KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > preference > BooleanPropertyAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.preference;
13
14 import org.eclipse.jface.action.Action;
15 import org.eclipse.jface.util.IPropertyChangeListener;
16 import org.eclipse.jface.util.PropertyChangeEvent;
17
18 /**
19  * The BooleanPropertyAction is an action that set the values of a
20  * boolean property in the preference store.
21  */

22
23 public class BooleanPropertyAction extends Action {
24
25     private IPreferenceStore preferenceStore;
26
27     private String JavaDoc property;
28
29     /**
30      * Create a new instance of the receiver.
31      * @param title The displayable name of the action.
32      * @param preferenceStore The preference store to propogate changes to
33      * @param property The property that is being updated
34      * @throws IllegalArgumentException Thrown if preferenceStore or
35      * property are <code>null</code>.
36      */

37     public BooleanPropertyAction(String JavaDoc title,
38             IPreferenceStore preferenceStore, String JavaDoc property)
39             throws IllegalArgumentException JavaDoc {
40         super(title, AS_CHECK_BOX);
41
42         if (preferenceStore == null || property == null) {
43             throw new IllegalArgumentException JavaDoc();
44         }
45
46         this.preferenceStore = preferenceStore;
47         this.property = property;
48         final String JavaDoc finalProprety = property;
49
50         preferenceStore
51                 .addPropertyChangeListener(new IPropertyChangeListener() {
52                     public void propertyChange(PropertyChangeEvent event) {
53                         if (finalProprety.equals(event.getProperty())) {
54                             setChecked(Boolean.TRUE.equals(event.getNewValue()));
55                         }
56                     }
57                 });
58
59         setChecked(preferenceStore.getBoolean(property));
60     }
61
62     /*
63      * (non-Javadoc)
64      * @see org.eclipse.jface.action.IAction#run()
65      */

66     public void run() {
67         preferenceStore.setValue(property, isChecked());
68     }
69 }
70
Popular Tags