1 /*******************************************************************************
 
   2  * Copyright (c) 2000, 2003 IBM Corporation and others.
 
   3  * All rights reserved. This program and the accompanying materials 
 
   4  * are made available under the terms of the Common Public License v1.0
 
   5  * which accompanies this distribution, and is available at
 
   6  * http://www.eclipse.org/legal/cpl-v10.html
 
   9  *     IBM Corporation - initial API and implementation
 
  10  *******************************************************************************/
 
  11 package net.sourceforge.phpeclipse.phpeditor;
 
  13 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
 
  14 import net.sourceforge.phpdt.ui.PreferenceConstants;
 
  15 import net.sourceforge.phpdt.ui.actions.PHPEditorActionDefinitionIds;
 
  16 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 
  17 import net.sourceforge.phpeclipse.ui.WebUI;
 
  19 import org.eclipse.jface.preference.IPreferenceStore;
 
  20 import org.eclipse.jface.text.IRegion;
 
  21 import org.eclipse.jface.util.IPropertyChangeListener;
 
  22 import org.eclipse.jface.util.PropertyChangeEvent;
 
  23 import org.eclipse.ui.texteditor.ITextEditor;
 
  24 import org.eclipse.ui.texteditor.TextEditorAction;
 
  27  * A toolbar action which toggles the presentation model of the connected text
 
  28  * editor. The editor shows either the highlight range only or always the whole
 
  31 public class TogglePresentationAction extends TextEditorAction implements
 
  32                 IPropertyChangeListener {
 
  34         private IPreferenceStore fStore;
 
  37          * Constructs and updates the action.
 
  39         public TogglePresentationAction() {
 
  40                 super(PHPEditorMessages.getResourceBundle(),
 
  41                                 "TogglePresentation.", null); //$NON-NLS-1$
 
  42                 PHPUiImages.setToolImageDescriptors(this, "segment_edit.gif"); //$NON-NLS-1$
 
  43                 setToolTipText(PHPEditorMessages
 
  44                                 .getString("TogglePresentation.tooltip")); //$NON-NLS-1$
 
  45                 setActionDefinitionId(PHPEditorActionDefinitionIds.TOGGLE_PRESENTATION);
 
  46                 // WorkbenchHelp.setHelp(this,
 
  47                 // IJavaHelpContextIds.TOGGLE_PRESENTATION_ACTION);
 
  52          * @see IAction#actionPerformed
 
  56                 ITextEditor editor = getTextEditor();
 
  60                 IRegion remembered = editor.getHighlightRange();
 
  61                 editor.resetHighlightRange();
 
  63                 boolean showAll = !editor.showsHighlightRangeOnly();
 
  66                 editor.showHighlightRangeOnly(showAll);
 
  67                 if (remembered != null)
 
  68                         editor.setHighlightRange(remembered.getOffset(), remembered
 
  71                 fStore.removePropertyChangeListener(this);
 
  72                 fStore.setValue(PreferenceConstants.EDITOR_SHOW_SEGMENTS, showAll);
 
  73                 fStore.addPropertyChangeListener(this);
 
  77          * @see TextEditorAction#update
 
  79         public void update() {
 
  80                 ITextEditor editor = getTextEditor();
 
  81                 boolean checked = (editor != null && editor.showsHighlightRangeOnly());
 
  83                 setEnabled(editor != null);
 
  87          * @see TextEditorAction#setEditor(ITextEditor)
 
  89         public void setEditor(ITextEditor editor) {
 
  91                 super.setEditor(editor);
 
  96                                 fStore = WebUI.getDefault().getPreferenceStore();
 
  97                                 fStore.addPropertyChangeListener(this);
 
  99                         synchronizeWithPreference(editor);
 
 101                 } else if (fStore != null) {
 
 102                         fStore.removePropertyChangeListener(this);
 
 110          * Synchronizes the appearance of the editor with what the preference store
 
 113         private void synchronizeWithPreference(ITextEditor editor) {
 
 118                 boolean showSegments = fStore
 
 119                                 .getBoolean(PreferenceConstants.EDITOR_SHOW_SEGMENTS);
 
 120                 setChecked(showSegments);
 
 122                 if (editor.showsHighlightRangeOnly() != showSegments) {
 
 123                         IRegion remembered = editor.getHighlightRange();
 
 124                         editor.resetHighlightRange();
 
 125                         editor.showHighlightRangeOnly(showSegments);
 
 126                         if (remembered != null)
 
 127                                 editor.setHighlightRange(remembered.getOffset(), remembered
 
 133          * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
 
 135         public void propertyChange(PropertyChangeEvent event) {
 
 136                 if (event.getProperty()
 
 137                                 .equals(PreferenceConstants.EDITOR_SHOW_SEGMENTS))
 
 138                         synchronizeWithPreference(getTextEditor());