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.3 2004-09-22 18:57:42 jsurfer 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 {
52 * Listens to changes to the selection in the outline page, and changes the selection and highlight range in the editor
55 private class OutlineSelectionChangedListener implements ISelectionChangedListener {
58 * @see ISelectionChangedListener#selectionChanged(SelectionChangedEvent)
60 public void selectionChanged(SelectionChangedEvent event) {
61 IStructuredSelection selection = (IStructuredSelection) event.getSelection();
62 if (selection.isEmpty()) {
63 resetHighlightRange();
65 ISourceReference element = (ISourceReference) selection.getFirstElement();
66 highlightElement(element, true);
73 * The associated outline page.
75 XMLOutlinePage outlinePage;
78 * Listens to changes in the outline page's selection to update the editor selection and highlight range.
80 private ISelectionChangedListener outlineSelectionChangedListener;
83 this(ShowExternalPreviewAction.XML_TYPE);
88 public XMLEditor(int type) {
90 List stores = new ArrayList(3);
92 stores.add(XMLPlugin.getDefault().getPreferenceStore());
93 stores.add(EditorsUI.getPreferenceStore());
95 setPreferenceStore(new ChainedPreferenceStore((IPreferenceStore[]) stores.toArray(new IPreferenceStore[stores.size()])));
99 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
101 public Object getAdapter(Class adapter) {
102 if (adapter.equals(IContentOutlinePage.class)) {
103 if (outlinePage == null) {
104 outlinePage = new XMLOutlinePage(this);
105 outlineSelectionChangedListener = new OutlineSelectionChangedListener();
106 outlinePage.addSelectionChangedListener(outlineSelectionChangedListener);
112 return super.getAdapter(adapter);
116 * @see IReconcilingParticipant#reconciled()
118 public void reconciled() {
119 Shell shell = getSite().getShell();
120 if ((shell != null) && !shell.isDisposed()) {
121 shell.getDisplay().asyncExec(new Runnable() {
123 if (outlinePage != null) {
124 outlinePage.update();
132 * @see org.eclipse.ui.editors.text.TextEditor#initializeEditor()
134 protected void initializeEditor() {
135 super.initializeEditor();
137 XMLTextTools xmlTextTools = XMLPlugin.getDefault().getXMLTextTools();
138 setSourceViewerConfiguration(new XMLConfiguration(xmlTextTools, this));
139 setDocumentProvider(new XMLDocumentProvider());
141 ShowExternalPreviewAction fShowExternalPreviewAction = ShowExternalPreviewAction.getInstance();
142 fShowExternalPreviewAction.setEditor(this);
143 fShowExternalPreviewAction.update();
144 if (fShowExternalPreviewAction != null)
145 fShowExternalPreviewAction.doRun(fType);
149 * @see org.eclipse.ui.texteditor.AbstractTextEditor#affectsTextPresentation(PropertyChangeEvent)
151 protected boolean affectsTextPresentation(PropertyChangeEvent event) {
152 return XMLPlugin.getDefault().getXMLTextTools().affectsBehavior(event);
155 void highlightElement(ISourceReference element, boolean moveCursor) {
156 if (element != null) {
157 IRegion highlightRegion = element.getSourceRegion();
158 setHighlightRange(highlightRegion.getOffset(), highlightRegion.getLength(), moveCursor);
160 resetHighlightRange();
164 protected void createActions() {
165 super.createActions();
167 IAction action = new ContentAssistAction(XMLEditorMessages.getResourceBundle(), "ContentAssistProposal.", this); //$NON-NLS-1$
168 action.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
169 setAction("ContentAssistProposal", action); //$NON-NLS-1$
170 markAsStateDependentAction("ContentAssistProposal", true); //$NON-NLS-1$
172 // IAction action= new TextOperationAction(
173 // TemplateMessages.getResourceBundle(),
174 // "Editor." + TEMPLATE_PROPOSALS + ".", //$NON-NLS-1$ //$NON-NLS-2$
176 // ISourceViewer.CONTENTASSIST_PROPOSALS);
177 // action.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
178 // setAction(TEMPLATE_PROPOSALS, action);
179 // markAsStateDependentAction(TEMPLATE_PROPOSALS, true);
183 * @see org.eclipse.ui.texteditor.AbstractTextEditor#editorSaved()
185 protected void editorSaved() {
187 ShowExternalPreviewAction a = ShowExternalPreviewAction.getInstance();