2  * Copyright (c) 2002-2004 Widespace, OU 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  *     Igor Malinin - initial contribution
 
  10  *     Christopher Lenz - integrated outline page
 
  12  * $Id: XMLEditor.java,v 1.4 2006-10-21 23:14:14 pombredanne Exp $
 
  15 package net.sourceforge.phpeclipse.xml.ui.internal.editor;
 
  17 import java.util.ArrayList;
 
  18 import java.util.List;
 
  20 import net.sourceforge.phpeclipse.core.model.ISourceReference;
 
  21 import net.sourceforge.phpeclipse.ui.editor.ShowExternalPreviewAction;
 
  22 import net.sourceforge.phpeclipse.ui.text.IReconcilingParticipant;
 
  23 import net.sourceforge.phpeclipse.xml.ui.XMLPlugin;
 
  24 import net.sourceforge.phpeclipse.xml.ui.internal.outline.XMLOutlinePage;
 
  25 import net.sourceforge.phpeclipse.xml.ui.internal.text.XMLConfiguration;
 
  26 import net.sourceforge.phpeclipse.xml.ui.text.XMLTextTools;
 
  28 import org.eclipse.jface.action.IAction;
 
  29 import org.eclipse.jface.preference.IPreferenceStore;
 
  30 import org.eclipse.jface.text.IRegion;
 
  31 import org.eclipse.jface.util.PropertyChangeEvent;
 
  32 import org.eclipse.jface.viewers.ISelectionChangedListener;
 
  33 import org.eclipse.jface.viewers.IStructuredSelection;
 
  34 import org.eclipse.jface.viewers.SelectionChangedEvent;
 
  35 import org.eclipse.swt.widgets.Shell;
 
  36 import org.eclipse.ui.editors.text.EditorsUI;
 
  37 import org.eclipse.ui.editors.text.TextEditor;
 
  38 import org.eclipse.ui.texteditor.ChainedPreferenceStore;
 
  39 import org.eclipse.ui.texteditor.ContentAssistAction;
 
  40 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
 
  41 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
 
  46  * @author Igor Malinin
 
  48 public class XMLEditor extends TextEditor implements IReconcilingParticipant {
 
  51          * Listens to changes to the selection in the outline page, and changes the
 
  52          * selection and highlight range in the editor accordingly.
 
  54         private class OutlineSelectionChangedListener implements
 
  55                         ISelectionChangedListener {
 
  58                  * @see ISelectionChangedListener#selectionChanged(SelectionChangedEvent)
 
  60                 public void selectionChanged(SelectionChangedEvent event) {
 
  61                         IStructuredSelection selection = (IStructuredSelection) event
 
  63                         if (selection.isEmpty()) {
 
  64                                 resetHighlightRange();
 
  66                                 ISourceReference element = (ISourceReference) selection
 
  68                                 highlightElement(element, true);
 
  75          * The associated outline page.
 
  77         XMLOutlinePage outlinePage;
 
  82          * Listens to changes in the outline page's selection to update the editor
 
  83          * selection and highlight range.
 
  85         private ISelectionChangedListener outlineSelectionChangedListener;
 
  88                 this(ShowExternalPreviewAction.XML_TYPE);
 
  94         public XMLEditor(int type) {
 
  96                 List stores = new ArrayList(3);
 
  98                 stores.add(XMLPlugin.getDefault().getPreferenceStore());
 
  99                 stores.add(EditorsUI.getPreferenceStore());
 
 101                 setPreferenceStore(new ChainedPreferenceStore(
 
 102                                 (IPreferenceStore[]) stores.toArray(new IPreferenceStore[stores
 
 107          * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
 
 109         public Object getAdapter(Class adapter) {
 
 110                 if (adapter.equals(IContentOutlinePage.class)) {
 
 111                         if (outlinePage == null) {
 
 112                                 outlinePage = new XMLOutlinePage(this);
 
 113                                 outlineSelectionChangedListener = new OutlineSelectionChangedListener();
 
 115                                                 .addSelectionChangedListener(outlineSelectionChangedListener);
 
 121                 return super.getAdapter(adapter);
 
 125          * @see IReconcilingParticipant#reconciled()
 
 127         public void reconciled() {
 
 128                 Shell shell = getSite().getShell();
 
 129                 if ((shell != null) && !shell.isDisposed()) {
 
 130                         shell.getDisplay().asyncExec(new Runnable() {
 
 132                                         if (outlinePage != null) {
 
 133                                                 outlinePage.update();
 
 141          * @see org.eclipse.ui.editors.text.TextEditor#initializeEditor()
 
 143         protected void initializeEditor() {
 
 144                 super.initializeEditor();
 
 146                 XMLTextTools xmlTextTools = XMLPlugin.getDefault().getXMLTextTools();
 
 147                 setSourceViewerConfiguration(new XMLConfiguration(xmlTextTools, this));
 
 148                 setDocumentProvider(new XMLDocumentProvider());
 
 150                 ShowExternalPreviewAction fShowExternalPreviewAction = ShowExternalPreviewAction
 
 152                 fShowExternalPreviewAction.setEditor(this);
 
 153                 fShowExternalPreviewAction.update();
 
 154                 if (fShowExternalPreviewAction != null)
 
 155                         fShowExternalPreviewAction.doRun(fType);
 
 159          * @see org.eclipse.ui.texteditor.AbstractTextEditor#affectsTextPresentation(PropertyChangeEvent)
 
 161         protected boolean affectsTextPresentation(PropertyChangeEvent event) {
 
 162                 return XMLPlugin.getDefault().getXMLTextTools().affectsBehavior(event);
 
 165         void highlightElement(ISourceReference element, boolean moveCursor) {
 
 166                 if (element != null) {
 
 167                         IRegion highlightRegion = element.getSourceRegion();
 
 168                         setHighlightRange(highlightRegion.getOffset(), highlightRegion
 
 169                                         .getLength(), moveCursor);
 
 171                         resetHighlightRange();
 
 175         protected void createActions() {
 
 176                 super.createActions();
 
 178                 IAction action = new ContentAssistAction(XMLEditorMessages
 
 179                                 .getResourceBundle(), "ContentAssistProposal.", this); //$NON-NLS-1$
 
 181                                 .setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
 
 182                 setAction("ContentAssistProposal", action); //$NON-NLS-1$
 
 183                 markAsStateDependentAction("ContentAssistProposal", true); //$NON-NLS-1$
 
 185                 // IAction action= new TextOperationAction(
 
 186                 // TemplateMessages.getResourceBundle(),
 
 187                 // "Editor." + TEMPLATE_PROPOSALS + ".", //$NON-NLS-1$ //$NON-NLS-2$
 
 189                 // ISourceViewer.CONTENTASSIST_PROPOSALS);
 
 190                 // action.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
 
 191                 // setAction(TEMPLATE_PROPOSALS, action);
 
 192                 // markAsStateDependentAction(TEMPLATE_PROPOSALS, true);
 
 198          * @see org.eclipse.ui.texteditor.AbstractTextEditor#editorSaved()
 
 200         protected void editorSaved() {
 
 202                 ShowExternalPreviewAction a = ShowExternalPreviewAction.getInstance();