931ea8b513e4993d428eeef54ea275a66e81c476
[phpeclipse.git] /
1 /*
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
7  *
8  * Contributors:
9  *     Igor Malinin - initial contribution
10  *     Christopher Lenz - integrated outline page
11  *
12  * $Id: XMLEditor.java,v 1.1 2004-09-02 18:28:04 jsurfer Exp $
13  */
14
15 package net.sourceforge.phpeclipse.xml.ui.internal.editor;
16
17 import java.util.ArrayList;
18 import java.util.List;
19
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;
26
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;
41
42 /**
43  * XML Editor.
44  * 
45  * @author Igor Malinin
46  */
47 public class XMLEditor extends TextEditor implements IReconcilingParticipant {
48
49     /**
50      * Listens to changes to the selection in the outline page, and changes the
51      * selection and highlight range in the editor accordingly.
52      */
53     private class OutlineSelectionChangedListener
54         implements ISelectionChangedListener {
55
56         /*
57          * @see ISelectionChangedListener#selectionChanged(SelectionChangedEvent)
58          */
59         public void selectionChanged(SelectionChangedEvent event) {
60             IStructuredSelection selection =
61                 (IStructuredSelection) event.getSelection();
62             if (selection.isEmpty()) {
63                 resetHighlightRange();
64             } else {
65                 ISourceReference element = (ISourceReference)
66                                         selection.getFirstElement();
67                 highlightElement(element, true);
68             }
69         }
70
71     }
72
73     /**
74      * The associated outline page.
75      */
76         XMLOutlinePage outlinePage;
77
78     /**
79      * Listens to changes in the outline page's selection to update the editor
80      * selection and highlight range.
81      */
82     private ISelectionChangedListener outlineSelectionChangedListener;
83
84     /**
85      * Constructor.
86      */
87         public XMLEditor() {
88                 List stores = new ArrayList(3);
89
90                 stores.add(XMLPlugin.getDefault().getPreferenceStore());
91                 stores.add(EditorsUI.getPreferenceStore());
92
93                 setPreferenceStore(
94                         new ChainedPreferenceStore(
95                                 (IPreferenceStore[]) stores
96                                         .toArray(new IPreferenceStore[stores.size()])));
97         }
98
99         /*
100          * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
101          */
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);
110                         }
111
112                         return outlinePage;
113                 }
114
115                 return super.getAdapter(adapter);
116         }
117
118         /* 
119          * @see IReconcilingParticipant#reconciled()
120          */
121         public void reconciled() {
122                 Shell shell = getSite().getShell();
123                 if ((shell != null) && !shell.isDisposed()) {
124                         shell.getDisplay().asyncExec(new Runnable() {
125                                 public void run() {
126                                         if (outlinePage != null) {
127                                                 outlinePage.update();
128                                         }
129                                 }
130                         });
131                 }
132         }
133
134         /*
135          * @see org.eclipse.ui.editors.text.TextEditor#initializeEditor()
136          */
137         protected void initializeEditor() {
138                 super.initializeEditor();
139
140                 XMLTextTools xmlTextTools = XMLPlugin.getDefault().getXMLTextTools();
141                 setSourceViewerConfiguration(new XMLConfiguration(xmlTextTools, this));
142                 setDocumentProvider(new XMLDocumentProvider());
143         }
144
145         /*
146          * @see org.eclipse.ui.texteditor.AbstractTextEditor#affectsTextPresentation(PropertyChangeEvent)
147          */
148         protected boolean affectsTextPresentation(PropertyChangeEvent event) {
149                 return XMLPlugin.getDefault().getXMLTextTools().affectsBehavior(event);
150         }
151
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);
158         } else {
159             resetHighlightRange();
160         }
161     }
162     
163     protected void createActions() {
164                 super.createActions();
165
166                 IAction action = new ContentAssistAction(XMLEditorMessages.getResourceBundle(),
167                 "ContentAssistProposal.", this); //$NON-NLS-1$
168             action
169                 .setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
170             setAction("ContentAssistProposal", action); //$NON-NLS-1$
171             markAsStateDependentAction("ContentAssistProposal", true); //$NON-NLS-1$
172             
173 //              IAction action= new TextOperationAction(
174 //                              TemplateMessages.getResourceBundle(),
175 //                              "Editor." + TEMPLATE_PROPOSALS + ".", //$NON-NLS-1$ //$NON-NLS-2$
176 //                              this,
177 //                              ISourceViewer.CONTENTASSIST_PROPOSALS);
178 //              action.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
179 //              setAction(TEMPLATE_PROPOSALS, action);
180 //              markAsStateDependentAction(TEMPLATE_PROPOSALS, true);
181         }
182 }