e1eae821ba9612d42a878bef0b49ba6f80cc1763
[phpeclipse.git] / net.sourceforge.phpeclipse.xml.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.2 2004-09-03 17:29:08 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.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.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;
43
44 /**
45  * XML Editor.
46  * 
47  * @author Igor Malinin
48  */
49 public class XMLEditor extends TextEditor implements IReconcilingParticipant {
50   
51   
52   /**
53    * Listens to changes to the selection in the outline page, and changes the selection and highlight range in the editor
54    * accordingly.
55    */
56   private class OutlineSelectionChangedListener implements ISelectionChangedListener {
57
58     /*
59      * @see ISelectionChangedListener#selectionChanged(SelectionChangedEvent)
60      */
61     public void selectionChanged(SelectionChangedEvent event) {
62       IStructuredSelection selection = (IStructuredSelection) event.getSelection();
63       if (selection.isEmpty()) {
64         resetHighlightRange();
65       } else {
66         ISourceReference element = (ISourceReference) selection.getFirstElement();
67         highlightElement(element, true);
68       }
69     }
70
71   }
72
73   /**
74    * The associated outline page.
75    */
76   XMLOutlinePage outlinePage;
77   int fType;
78   /**
79    * Listens to changes in the outline page's selection to update the editor selection and highlight range.
80    */
81   private ISelectionChangedListener outlineSelectionChangedListener;
82
83   public XMLEditor() {
84     this(ShowExternalPreviewAction.XML_TYPE);
85   }
86   /**
87    * Constructor.
88    */
89   public XMLEditor(int type) {
90     fType = type;
91     List stores = new ArrayList(3);
92
93     stores.add(XMLPlugin.getDefault().getPreferenceStore());
94     stores.add(EditorsUI.getPreferenceStore());
95
96     setPreferenceStore(new ChainedPreferenceStore((IPreferenceStore[]) stores.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 = new OutlineSelectionChangedListener();
107         outlinePage.addSelectionChangedListener(outlineSelectionChangedListener);
108       }
109
110       return outlinePage;
111     }
112
113     return super.getAdapter(adapter);
114   }
115
116   /*
117    * @see IReconcilingParticipant#reconciled()
118    */
119   public void reconciled() {
120     Shell shell = getSite().getShell();
121     if ((shell != null) && !shell.isDisposed()) {
122       shell.getDisplay().asyncExec(new Runnable() {
123         public void run() {
124           if (outlinePage != null) {
125             outlinePage.update();
126           }
127         }
128       });
129     }
130   }
131
132   /*
133    * @see org.eclipse.ui.editors.text.TextEditor#initializeEditor()
134    */
135   protected void initializeEditor() {
136     super.initializeEditor();
137
138     XMLTextTools xmlTextTools = XMLPlugin.getDefault().getXMLTextTools();
139     setSourceViewerConfiguration(new XMLConfiguration(xmlTextTools, this));
140     setDocumentProvider(new XMLDocumentProvider());
141     
142     ShowExternalPreviewAction fShowExternalPreviewAction = ShowExternalPreviewAction.getInstance();
143     fShowExternalPreviewAction.setEditor(this);
144         fShowExternalPreviewAction.update();
145         if (fShowExternalPreviewAction != null)
146           fShowExternalPreviewAction.doRun(fType);
147   }
148
149   /*
150    * @see org.eclipse.ui.texteditor.AbstractTextEditor#affectsTextPresentation(PropertyChangeEvent)
151    */
152   protected boolean affectsTextPresentation(PropertyChangeEvent event) {
153     return XMLPlugin.getDefault().getXMLTextTools().affectsBehavior(event);
154   }
155
156   void highlightElement(ISourceReference element, boolean moveCursor) {
157     if (element != null) {
158       IRegion highlightRegion = element.getSourceRegion();
159       setHighlightRange(highlightRegion.getOffset(), highlightRegion.getLength(), moveCursor);
160     } else {
161       resetHighlightRange();
162     }
163   }
164
165   protected void createActions() {
166     super.createActions();
167
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$
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
183   /* (non-Javadoc)
184    * @see org.eclipse.ui.texteditor.AbstractTextEditor#editorSaved()
185    */
186   protected void editorSaved() {
187     super.editorSaved();
188     ShowExternalPreviewAction a = ShowExternalPreviewAction.getInstance();
189     if (a != null) {
190       a.refresh(fType);
191     }
192   }
193 }