KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > internal > TextEditorPropertyAction


1 /*******************************************************************************
2  * Copyright (c) 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.compare.internal;
12
13 import org.eclipse.jface.action.Action;
14 import org.eclipse.jface.action.IAction;
15 import org.eclipse.jface.preference.IPreferenceStore;
16 import org.eclipse.jface.util.IPropertyChangeListener;
17 import org.eclipse.jface.util.PropertyChangeEvent;
18 import org.eclipse.ui.editors.text.EditorsUI;
19
20 public class TextEditorPropertyAction extends Action implements IPropertyChangeListener {
21
22     private final MergeSourceViewer[] viewers;
23     private final String JavaDoc preferenceKey;
24     private IPreferenceStore store;
25     
26     public TextEditorPropertyAction(String JavaDoc label, MergeSourceViewer[] viewers, String JavaDoc preferenceKey) {
27         super(label, IAction.AS_CHECK_BOX);
28         this.viewers = viewers;
29         this.preferenceKey = preferenceKey;
30         this.store = EditorsUI.getPreferenceStore();
31         if (store != null)
32             store.addPropertyChangeListener(this);
33         synchronizeWithPreference();
34         addActionToViewers();
35     }
36
37     private void addActionToViewers() {
38         for (int i = 0; i < viewers.length; i++) {
39             MergeSourceViewer viewer = viewers[i];
40             viewer.addTextAction(this);
41         }
42     }
43
44     public MergeSourceViewer[] getViewers() {
45         return viewers;
46     }
47     
48     public void propertyChange(PropertyChangeEvent event) {
49         if (event.getProperty().equals(getPreferenceKey())) {
50             synchronizeWithPreference();
51         }
52     }
53     
54     private void synchronizeWithPreference() {
55         boolean checked = false;
56         if (store != null) {
57             checked = store.getBoolean(getPreferenceKey());
58         }
59         if (checked != isChecked()) {
60             toggleState(checked);
61             setChecked(checked);
62         }
63     }
64
65     public String JavaDoc getPreferenceKey() {
66         return preferenceKey;
67     }
68     
69     public void run() {
70         toggleState(isChecked());
71         if (store != null)
72             store.setValue(getPreferenceKey(), isChecked());
73     }
74     
75     public void dispose() {
76         if (store != null)
77             store.removePropertyChangeListener(this);
78     }
79     
80     protected void toggleState(boolean checked) {
81         // No-op by default
82
}
83
84 }
85
Popular Tags