blob: ee4947e411c6786bbb437aff5d5c4434efd55909 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
/*******************************************************************************
* Copyright (c) 2007, 2011 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.compare.internal;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.ui.editors.text.EditorsUI;
public class TextEditorPropertyAction extends Action implements IPropertyChangeListener {
private final MergeSourceViewer[] viewers;
private final String preferenceKey;
private IPreferenceStore store;
public TextEditorPropertyAction(String label, MergeSourceViewer[] viewers, String preferenceKey) {
super(label, IAction.AS_CHECK_BOX);
this.viewers = viewers;
this.preferenceKey = preferenceKey;
this.store = EditorsUI.getPreferenceStore();
if (store != null)
store.addPropertyChangeListener(this);
synchronizeWithPreference();
addActionToViewers();
}
private void addActionToViewers() {
for (int i = 0; i < viewers.length; i++) {
MergeSourceViewer viewer = viewers[i];
viewer.addTextAction(this);
}
}
public MergeSourceViewer[] getViewers() {
return viewers;
}
@Override
public void propertyChange(PropertyChangeEvent event) {
if (event.getProperty().equals(getPreferenceKey())) {
synchronizeWithPreference();
}
}
protected void synchronizeWithPreference() {
boolean checked = false;
if (store != null) {
checked = store.getBoolean(getPreferenceKey());
}
if (checked != isChecked()) {
if (toggleState(checked))
setChecked(checked);
}
}
public String getPreferenceKey() {
return preferenceKey;
}
@Override
public void run() {
toggleState(isChecked());
if (store != null)
store.setValue(getPreferenceKey(), isChecked());
}
public void dispose() {
if (store != null)
store.removePropertyChangeListener(this);
}
/**
* @param checked
* new state
* @return <code>true</code> if state has been changed, toggle has been
* successful
*/
protected boolean toggleState(boolean checked) {
// No-op by default
return false;
}
}
|