1 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 preferenceKey; 24 private IPreferenceStore store; 25 26 public TextEditorPropertyAction(String label, MergeSourceViewer[] viewers, String 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 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 } 83 84 } 85 | Popular Tags |