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