refactory: added UI removed from core plugin.
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / xml / ui / internal / editor / XMLEditor.java
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.4 2006-10-21 23:14:14 pombredanne 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.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;
27
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;
42
43 /**
44  * XML Editor.
45  * 
46  * @author Igor Malinin
47  */
48 public class XMLEditor extends TextEditor implements IReconcilingParticipant {
49
50         /**
51          * Listens to changes to the selection in the outline page, and changes the
52          * selection and highlight range in the editor accordingly.
53          */
54         private class OutlineSelectionChangedListener implements
55                         ISelectionChangedListener {
56
57                 /*
58                  * @see ISelectionChangedListener#selectionChanged(SelectionChangedEvent)
59                  */
60                 public void selectionChanged(SelectionChangedEvent event) {
61                         IStructuredSelection selection = (IStructuredSelection) event
62                                         .getSelection();
63                         if (selection.isEmpty()) {
64                                 resetHighlightRange();
65                         } else {
66                                 ISourceReference element = (ISourceReference) selection
67                                                 .getFirstElement();
68                                 highlightElement(element, true);
69                         }
70                 }
71
72         }
73
74         /**
75          * The associated outline page.
76          */
77         XMLOutlinePage outlinePage;
78
79         int fType;
80
81         /**
82          * Listens to changes in the outline page's selection to update the editor
83          * selection and highlight range.
84          */
85         private ISelectionChangedListener outlineSelectionChangedListener;
86
87         public XMLEditor() {
88                 this(ShowExternalPreviewAction.XML_TYPE);
89         }
90
91         /**
92          * Constructor.
93          */
94         public XMLEditor(int type) {
95                 fType = type;
96                 List stores = new ArrayList(3);
97
98                 stores.add(XMLPlugin.getDefault().getPreferenceStore());
99                 stores.add(EditorsUI.getPreferenceStore());
100
101                 setPreferenceStore(new ChainedPreferenceStore(
102                                 (IPreferenceStore[]) stores.toArray(new IPreferenceStore[stores
103                                                 .size()])));
104         }
105
106         /*
107          * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
108          */
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();
114                                 outlinePage
115                                                 .addSelectionChangedListener(outlineSelectionChangedListener);
116                         }
117
118                         return outlinePage;
119                 }
120
121                 return super.getAdapter(adapter);
122         }
123
124         /*
125          * @see IReconcilingParticipant#reconciled()
126          */
127         public void reconciled() {
128                 Shell shell = getSite().getShell();
129                 if ((shell != null) && !shell.isDisposed()) {
130                         shell.getDisplay().asyncExec(new Runnable() {
131                                 public void run() {
132                                         if (outlinePage != null) {
133                                                 outlinePage.update();
134                                         }
135                                 }
136                         });
137                 }
138         }
139
140         /*
141          * @see org.eclipse.ui.editors.text.TextEditor#initializeEditor()
142          */
143         protected void initializeEditor() {
144                 super.initializeEditor();
145
146                 XMLTextTools xmlTextTools = XMLPlugin.getDefault().getXMLTextTools();
147                 setSourceViewerConfiguration(new XMLConfiguration(xmlTextTools, this));
148                 setDocumentProvider(new XMLDocumentProvider());
149
150                 ShowExternalPreviewAction fShowExternalPreviewAction = ShowExternalPreviewAction
151                                 .getInstance();
152                 fShowExternalPreviewAction.setEditor(this);
153                 fShowExternalPreviewAction.update();
154                 if (fShowExternalPreviewAction != null)
155                         fShowExternalPreviewAction.doRun(fType);
156         }
157
158         /*
159          * @see org.eclipse.ui.texteditor.AbstractTextEditor#affectsTextPresentation(PropertyChangeEvent)
160          */
161         protected boolean affectsTextPresentation(PropertyChangeEvent event) {
162                 return XMLPlugin.getDefault().getXMLTextTools().affectsBehavior(event);
163         }
164
165         void highlightElement(ISourceReference element, boolean moveCursor) {
166                 if (element != null) {
167                         IRegion highlightRegion = element.getSourceRegion();
168                         setHighlightRange(highlightRegion.getOffset(), highlightRegion
169                                         .getLength(), moveCursor);
170                 } else {
171                         resetHighlightRange();
172                 }
173         }
174
175         protected void createActions() {
176                 super.createActions();
177
178                 IAction action = new ContentAssistAction(XMLEditorMessages
179                                 .getResourceBundle(), "ContentAssistProposal.", this); //$NON-NLS-1$
180                 action
181                                 .setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
182                 setAction("ContentAssistProposal", action); //$NON-NLS-1$
183                 markAsStateDependentAction("ContentAssistProposal", true); //$NON-NLS-1$
184
185                 // IAction action= new TextOperationAction(
186                 // TemplateMessages.getResourceBundle(),
187                 // "Editor." + TEMPLATE_PROPOSALS + ".", //$NON-NLS-1$ //$NON-NLS-2$
188                 // this,
189                 // ISourceViewer.CONTENTASSIST_PROPOSALS);
190                 // action.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
191                 // setAction(TEMPLATE_PROPOSALS, action);
192                 // markAsStateDependentAction(TEMPLATE_PROPOSALS, true);
193         }
194
195         /*
196          * (non-Javadoc)
197          * 
198          * @see org.eclipse.ui.texteditor.AbstractTextEditor#editorSaved()
199          */
200         protected void editorSaved() {
201                 super.editorSaved();
202                 ShowExternalPreviewAction a = ShowExternalPreviewAction.getInstance();
203                 if (a != null) {
204                         a.refresh(fType);
205                 }
206         }
207 }