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.1 2004-09-02 18:28:04 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.text.IReconcilingParticipant;
22 import net.sourceforge.phpeclipse.xml.ui.XMLPlugin;
23 import net.sourceforge.phpeclipse.xml.ui.internal.outline.XMLOutlinePage;
24 import net.sourceforge.phpeclipse.xml.ui.internal.text.XMLConfiguration;
25 import net.sourceforge.phpeclipse.xml.ui.text.XMLTextTools;
27 import org.eclipse.jface.action.IAction;
28 import org.eclipse.jface.preference.IPreferenceStore;
29 import org.eclipse.jface.text.IRegion;
30 import org.eclipse.jface.util.PropertyChangeEvent;
31 import org.eclipse.jface.viewers.ISelectionChangedListener;
32 import org.eclipse.jface.viewers.IStructuredSelection;
33 import org.eclipse.jface.viewers.SelectionChangedEvent;
34 import org.eclipse.swt.widgets.Shell;
35 import org.eclipse.ui.editors.text.EditorsUI;
36 import org.eclipse.ui.editors.text.TextEditor;
37 import org.eclipse.ui.texteditor.ChainedPreferenceStore;
38 import org.eclipse.ui.texteditor.ContentAssistAction;
39 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
40 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
45 * @author Igor Malinin
47 public class XMLEditor extends TextEditor implements IReconcilingParticipant {
50 * Listens to changes to the selection in the outline page, and changes the
51 * selection and highlight range in the editor accordingly.
53 private class OutlineSelectionChangedListener
54 implements ISelectionChangedListener {
57 * @see ISelectionChangedListener#selectionChanged(SelectionChangedEvent)
59 public void selectionChanged(SelectionChangedEvent event) {
60 IStructuredSelection selection =
61 (IStructuredSelection) event.getSelection();
62 if (selection.isEmpty()) {
63 resetHighlightRange();
65 ISourceReference element = (ISourceReference)
66 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
80 * selection and highlight range.
82 private ISelectionChangedListener outlineSelectionChangedListener;
88 List stores = new ArrayList(3);
90 stores.add(XMLPlugin.getDefault().getPreferenceStore());
91 stores.add(EditorsUI.getPreferenceStore());
94 new ChainedPreferenceStore(
95 (IPreferenceStore[]) stores
96 .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 =
107 new OutlineSelectionChangedListener();
108 outlinePage.addSelectionChangedListener(
109 outlineSelectionChangedListener);
115 return super.getAdapter(adapter);
119 * @see IReconcilingParticipant#reconciled()
121 public void reconciled() {
122 Shell shell = getSite().getShell();
123 if ((shell != null) && !shell.isDisposed()) {
124 shell.getDisplay().asyncExec(new Runnable() {
126 if (outlinePage != null) {
127 outlinePage.update();
135 * @see org.eclipse.ui.editors.text.TextEditor#initializeEditor()
137 protected void initializeEditor() {
138 super.initializeEditor();
140 XMLTextTools xmlTextTools = XMLPlugin.getDefault().getXMLTextTools();
141 setSourceViewerConfiguration(new XMLConfiguration(xmlTextTools, this));
142 setDocumentProvider(new XMLDocumentProvider());
146 * @see org.eclipse.ui.texteditor.AbstractTextEditor#affectsTextPresentation(PropertyChangeEvent)
148 protected boolean affectsTextPresentation(PropertyChangeEvent event) {
149 return XMLPlugin.getDefault().getXMLTextTools().affectsBehavior(event);
152 void highlightElement(ISourceReference element,
153 boolean moveCursor) {
154 if (element != null) {
155 IRegion highlightRegion = element.getSourceRegion();
156 setHighlightRange(highlightRegion.getOffset(),
157 highlightRegion.getLength(), moveCursor);
159 resetHighlightRange();
163 protected void createActions() {
164 super.createActions();
166 IAction action = new ContentAssistAction(XMLEditorMessages.getResourceBundle(),
167 "ContentAssistProposal.", this); //$NON-NLS-1$
169 .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);