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.2 2004-09-03 17:29:08 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.core.runtime.IProgressMonitor;
29 import org.eclipse.jface.action.IAction;
30 import org.eclipse.jface.preference.IPreferenceStore;
31 import org.eclipse.jface.text.IRegion;
32 import org.eclipse.jface.util.PropertyChangeEvent;
33 import org.eclipse.jface.viewers.ISelectionChangedListener;
34 import org.eclipse.jface.viewers.IStructuredSelection;
35 import org.eclipse.jface.viewers.SelectionChangedEvent;
36 import org.eclipse.swt.widgets.Shell;
37 import org.eclipse.ui.editors.text.EditorsUI;
38 import org.eclipse.ui.editors.text.TextEditor;
39 import org.eclipse.ui.texteditor.ChainedPreferenceStore;
40 import org.eclipse.ui.texteditor.ContentAssistAction;
41 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
42 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
47 * @author Igor Malinin
49 public class XMLEditor extends TextEditor implements IReconcilingParticipant {
53 * Listens to changes to the selection in the outline page, and changes the selection and highlight range in the editor
56 private class OutlineSelectionChangedListener implements ISelectionChangedListener {
59 * @see ISelectionChangedListener#selectionChanged(SelectionChangedEvent)
61 public void selectionChanged(SelectionChangedEvent event) {
62 IStructuredSelection selection = (IStructuredSelection) event.getSelection();
63 if (selection.isEmpty()) {
64 resetHighlightRange();
66 ISourceReference element = (ISourceReference) selection.getFirstElement();
67 highlightElement(element, true);
74 * The associated outline page.
76 XMLOutlinePage outlinePage;
79 * Listens to changes in the outline page's selection to update the editor selection and highlight range.
81 private ISelectionChangedListener outlineSelectionChangedListener;
84 this(ShowExternalPreviewAction.XML_TYPE);
89 public XMLEditor(int type) {
91 List stores = new ArrayList(3);
93 stores.add(XMLPlugin.getDefault().getPreferenceStore());
94 stores.add(EditorsUI.getPreferenceStore());
96 setPreferenceStore(new ChainedPreferenceStore((IPreferenceStore[]) stores.toArray(new IPreferenceStore[stores.size()])));
100 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
102 public Object getAdapter(Class adapter) {
103 if (adapter.equals(IContentOutlinePage.class)) {
104 if (outlinePage == null) {
105 outlinePage = new XMLOutlinePage(this);
106 outlineSelectionChangedListener = new OutlineSelectionChangedListener();
107 outlinePage.addSelectionChangedListener(outlineSelectionChangedListener);
113 return super.getAdapter(adapter);
117 * @see IReconcilingParticipant#reconciled()
119 public void reconciled() {
120 Shell shell = getSite().getShell();
121 if ((shell != null) && !shell.isDisposed()) {
122 shell.getDisplay().asyncExec(new Runnable() {
124 if (outlinePage != null) {
125 outlinePage.update();
133 * @see org.eclipse.ui.editors.text.TextEditor#initializeEditor()
135 protected void initializeEditor() {
136 super.initializeEditor();
138 XMLTextTools xmlTextTools = XMLPlugin.getDefault().getXMLTextTools();
139 setSourceViewerConfiguration(new XMLConfiguration(xmlTextTools, this));
140 setDocumentProvider(new XMLDocumentProvider());
142 ShowExternalPreviewAction fShowExternalPreviewAction = ShowExternalPreviewAction.getInstance();
143 fShowExternalPreviewAction.setEditor(this);
144 fShowExternalPreviewAction.update();
145 if (fShowExternalPreviewAction != null)
146 fShowExternalPreviewAction.doRun(fType);
150 * @see org.eclipse.ui.texteditor.AbstractTextEditor#affectsTextPresentation(PropertyChangeEvent)
152 protected boolean affectsTextPresentation(PropertyChangeEvent event) {
153 return XMLPlugin.getDefault().getXMLTextTools().affectsBehavior(event);
156 void highlightElement(ISourceReference element, boolean moveCursor) {
157 if (element != null) {
158 IRegion highlightRegion = element.getSourceRegion();
159 setHighlightRange(highlightRegion.getOffset(), highlightRegion.getLength(), moveCursor);
161 resetHighlightRange();
165 protected void createActions() {
166 super.createActions();
168 IAction action = new ContentAssistAction(XMLEditorMessages.getResourceBundle(), "ContentAssistProposal.", this); //$NON-NLS-1$
169 action.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
170 setAction("ContentAssistProposal", action); //$NON-NLS-1$
171 markAsStateDependentAction("ContentAssistProposal", true); //$NON-NLS-1$
173 // IAction action= new TextOperationAction(
174 // TemplateMessages.getResourceBundle(),
175 // "Editor." + TEMPLATE_PROPOSALS + ".", //$NON-NLS-1$ //$NON-NLS-2$
177 // ISourceViewer.CONTENTASSIST_PROPOSALS);
178 // action.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
179 // setAction(TEMPLATE_PROPOSALS, action);
180 // markAsStateDependentAction(TEMPLATE_PROPOSALS, true);
184 * @see org.eclipse.ui.texteditor.AbstractTextEditor#editorSaved()
186 protected void editorSaved() {
188 ShowExternalPreviewAction a = ShowExternalPreviewAction.getInstance();