001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.javaeditor;
011:
012: import org.eclipse.jface.action.IAction;
013: import org.eclipse.jface.preference.IPreferenceStore;
014: import org.eclipse.jface.util.IPropertyChangeListener;
015: import org.eclipse.jface.util.PropertyChangeEvent;
016:
017: import org.eclipse.ui.PlatformUI;
018: import org.eclipse.ui.texteditor.ITextEditor;
019: import org.eclipse.ui.texteditor.TextEditorAction;
020:
021: import org.eclipse.jdt.ui.PreferenceConstants;
022:
023: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
024: import org.eclipse.jdt.internal.ui.JavaPlugin;
025: import org.eclipse.jdt.internal.ui.JavaPluginImages;
026:
027: /**
028: * A toolbar action which toggles the {@linkplain org.eclipse.jdt.ui.PreferenceConstants#EDITOR_MARK_OCCURRENCES mark occurrences preference}.
029: *
030: * @since 3.0
031: */
032: public class ToggleMarkOccurrencesAction extends TextEditorAction
033: implements IPropertyChangeListener {
034:
035: private IPreferenceStore fStore;
036:
037: /**
038: * Constructs and updates the action.
039: */
040: public ToggleMarkOccurrencesAction() {
041: super (
042: JavaEditorMessages.getBundleForConstructedKeys(),
043: "ToggleMarkOccurrencesAction.", null, IAction.AS_CHECK_BOX); //$NON-NLS-1$
044: JavaPluginImages.setToolImageDescriptors(this ,
045: "mark_occurrences.gif"); //$NON-NLS-1$
046: PlatformUI.getWorkbench().getHelpSystem().setHelp(this ,
047: IJavaHelpContextIds.TOGGLE_MARK_OCCURRENCES_ACTION);
048: update();
049: }
050:
051: /*
052: * @see IAction#actionPerformed
053: */
054: public void run() {
055: fStore.setValue(PreferenceConstants.EDITOR_MARK_OCCURRENCES,
056: isChecked());
057: }
058:
059: /*
060: * @see TextEditorAction#update
061: */
062: public void update() {
063: ITextEditor editor = getTextEditor();
064:
065: boolean checked = false;
066: if (editor instanceof JavaEditor)
067: checked = ((JavaEditor) editor).isMarkingOccurrences();
068:
069: setChecked(checked);
070: setEnabled(editor != null);
071: }
072:
073: /*
074: * @see TextEditorAction#setEditor(ITextEditor)
075: */
076: public void setEditor(ITextEditor editor) {
077:
078: super .setEditor(editor);
079:
080: if (editor != null) {
081:
082: if (fStore == null) {
083: fStore = JavaPlugin.getDefault().getPreferenceStore();
084: fStore.addPropertyChangeListener(this );
085: }
086:
087: } else if (fStore != null) {
088: fStore.removePropertyChangeListener(this );
089: fStore = null;
090: }
091:
092: update();
093: }
094:
095: /*
096: * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
097: */
098: public void propertyChange(PropertyChangeEvent event) {
099: if (event.getProperty().equals(
100: PreferenceConstants.EDITOR_MARK_OCCURRENCES))
101: setChecked(Boolean.valueOf(event.getNewValue().toString())
102: .booleanValue());
103: }
104: }
|