1 package net.sourceforge.phpeclipse.phpeditor;
3 /**********************************************************************
4 Copyright (c) 2000, 2002 IBM Corp. and others.
5 All rights reserved. This program and the accompanying materials
6 are made available under the terms of the Common Public License v1.0
7 which accompanies this distribution, and is available at
8 http://www.eclipse.org/legal/cpl-v10.html
11 IBM Corporation - Initial implementation
12 Klaus Hartlage - www.eclipseproject.de
13 **********************************************************************/
14 import net.sourceforge.phpeclipse.IPreferenceConstants;
15 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
16 import net.sourceforge.phpeclipse.phpeditor.php.PHPCodeScanner;
17 import net.sourceforge.phpeclipse.phpeditor.php.PHPWordExtractor;
18 import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.help.IHelp;
22 import org.eclipse.help.IHelpResource;
23 import org.eclipse.jface.action.Action;
24 import org.eclipse.jface.action.IAction;
25 import org.eclipse.jface.action.MenuManager;
26 import org.eclipse.jface.preference.IPreferenceStore;
27 import org.eclipse.jface.preference.PreferenceConverter;
28 import org.eclipse.jface.text.BadLocationException;
29 import org.eclipse.jface.text.IDocument;
30 import org.eclipse.jface.text.ITextOperationTarget;
31 import org.eclipse.jface.text.ITextSelection;
32 import org.eclipse.jface.text.source.AnnotationRulerColumn;
33 import org.eclipse.jface.text.source.CompositeRuler;
34 import org.eclipse.jface.text.source.ISourceViewer;
35 import org.eclipse.jface.text.source.IVerticalRuler;
36 import org.eclipse.jface.text.source.IVerticalRulerColumn;
37 import org.eclipse.jface.text.source.LineNumberRulerColumn;
38 import org.eclipse.jface.util.IPropertyChangeListener;
39 import org.eclipse.jface.util.PropertyChangeEvent;
40 import org.eclipse.swt.graphics.Point;
41 import org.eclipse.swt.graphics.RGB;
42 import org.eclipse.ui.IEditorInput;
43 import org.eclipse.ui.editors.text.TextEditor;
44 import org.eclipse.ui.help.WorkbenchHelp;
45 import org.eclipse.ui.texteditor.DefaultRangeIndicator;
46 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
47 import org.eclipse.ui.texteditor.TextOperationAction;
48 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
50 * PHP specific text editor.
52 public class PHPEditor extends TextEditor {
54 protected PHPActionGroup actionGroup;
55 /** The outline page */
56 private PHPContentOutlinePage fOutlinePage;
57 private IPreferenceStore phpPrefStore;
59 /** The line number ruler column */
60 private LineNumberRulerColumn fLineNumberRulerColumn;
63 * Default constructor.
69 public PHPContentOutlinePage getfOutlinePage() {
72 /** The <code>JavaEditor</code> implementation of this
73 * <code>AbstractTextEditor</code> method extend the
74 * actions to add those specific to the receiver
76 protected void createActions() {
77 super.createActions();
79 "ContentAssistProposal",
80 new TextOperationAction(
81 PHPEditorMessages.getResourceBundle(),
82 "ContentAssistProposal.",
84 ISourceViewer.CONTENTASSIST_PROPOSALS));
87 new TextOperationAction(
88 PHPEditorMessages.getResourceBundle(),
91 ISourceViewer.CONTENTASSIST_CONTEXT_INFORMATION));
93 Action action = new TextOperationAction(PHPEditorMessages.getResourceBundle(), "Comment.", this, ITextOperationTarget.PREFIX);
94 action.setActionDefinitionId(PHPEditorActionDefinitionIds.COMMENT);
95 setAction("Comment", action);
97 action = new TextOperationAction(PHPEditorMessages.getResourceBundle(), "Uncomment.", this, ITextOperationTarget.STRIP_PREFIX);
98 action.setActionDefinitionId(PHPEditorActionDefinitionIds.UNCOMMENT);
99 setAction("Uncomment", action);
101 actionGroup = new PHPActionGroup(this, ITextEditorActionConstants.GROUP_EDIT);
105 /** The <code>JavaEditor</code> implementation of this
106 * <code>AbstractTextEditor</code> method performs any extra
107 * disposal actions required by the java editor.
109 public void dispose() {
110 PHPEditorEnvironment.disconnect(this);
111 if (fOutlinePage != null)
112 fOutlinePage.setInput(null);
116 /** The <code>PHPEditor</code> implementation of this
117 * <code>AbstractTextEditor</code> method performs any extra
118 * revert behavior required by the php editor.
120 public void doRevertToSaved() {
121 super.doRevertToSaved();
122 if (fOutlinePage != null)
123 fOutlinePage.update();
126 /** The <code>PHPEditor</code> implementation of this
127 * <code>AbstractTextEditor</code> method performs any extra
128 * save behavior required by the php editor.
130 public void doSave(IProgressMonitor monitor) {
131 super.doSave(monitor);
132 // compile or not, according to the user preferences
133 IPreferenceStore store = phpPrefStore;
134 if (store.getBoolean(PHPeclipsePlugin.PHP_PARSE_ON_SAVE)) {
135 IAction a = PHPParserAction.getInstance();
139 if (fOutlinePage != null)
140 fOutlinePage.update();
143 /** The <code>PHPEditor</code> implementation of this
144 * <code>AbstractTextEditor</code> method performs any extra
145 * save as behavior required by the php editor.
147 public void doSaveAs() {
149 if (fOutlinePage != null)
150 fOutlinePage.update();
153 /** The <code>PHPEditor</code> implementation of this
154 * <code>AbstractTextEditor</code> method performs sets the
155 * input of the outline page after AbstractTextEditor has set input.
157 public void doSetInput(IEditorInput input) throws CoreException {
158 super.doSetInput(input);
159 if (fOutlinePage != null)
160 fOutlinePage.setInput(input);
163 /** The <code>JavaEditor</code> implementation of this
164 * <code>AbstractTextEditor</code> method adds any
165 * JavaEditor specific entries.
167 public void editorContextMenuAboutToShow(MenuManager menu) {
168 super.editorContextMenuAboutToShow(menu);
169 // addAction(menu, "ContentAssistProposal"); //$NON-NLS-1$
170 // addAction(menu, "ContentAssistTip"); //$NON-NLS-1$
171 actionGroup.fillContextMenu(menu);
174 /** The <code>JavaEditor</code> implementation of this
175 * <code>AbstractTextEditor</code> method performs gets
176 * the java content outline page if request is for a an
179 public Object getAdapter(Class required) {
180 if (IContentOutlinePage.class.equals(required)) {
181 if (fOutlinePage == null) {
182 fOutlinePage = new PHPContentOutlinePage(getDocumentProvider(), this);
183 if (getEditorInput() != null)
184 fOutlinePage.setInput(getEditorInput());
188 return super.getAdapter(required);
191 public void openContextHelp() {
192 IDocument doc = this.getDocumentProvider().getDocument(this.getEditorInput());
193 ITextSelection selection = (ITextSelection) this.getSelectionProvider().getSelection();
194 int pos = selection.getOffset();
195 String word = getFunctionName(doc, pos);
196 openContextHelp(word);
199 private void openContextHelp(String word) {
203 public static void open(String word) {
204 IHelp help = WorkbenchHelp.getHelpSupport();
206 IHelpResource helpResource = new PHPFunctionHelpResource(word);
207 WorkbenchHelp.getHelpSupport().displayHelpResource(helpResource);
209 // showMessage(shell, dialogTitle, ActionMessages.getString("Open help not available"), false); //$NON-NLS-1$
213 private String getFunctionName(IDocument doc, int pos) {
214 Point word = PHPWordExtractor.findWord(doc, pos);
217 return doc.get(word.x, word.y).replace('_', '-');
218 } catch (BadLocationException e) {
225 * @see AbstractTextEditor#handlePreferenceStoreChanged(PropertyChangeEvent)
227 protected void handlePreferenceStoreChanged(PropertyChangeEvent event) {
231 ISourceViewer sourceViewer = getSourceViewer();
232 if (sourceViewer == null)
235 String property = event.getProperty();
237 // if (JavaSourceViewerConfiguration.PREFERENCE_TAB_WIDTH.equals(property)) {
238 // Object value= event.getNewValue();
239 // if (value instanceof Integer) {
240 // sourceViewer.getTextWidget().setTabs(((Integer) value).intValue());
241 // } else if (value instanceof String) {
242 // sourceViewer.getTextWidget().setTabs(Integer.parseInt((String) value));
247 if (IPreferenceConstants.LINE_NUMBER_RULER.equals(property)) {
248 if (isLineNumberRulerVisible())
249 showLineNumberRuler();
251 hideLineNumberRuler();
255 if (fLineNumberRulerColumn != null
256 && (IPreferenceConstants.LINE_NUMBER_COLOR.equals(property)
257 || PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT.equals(property)
258 || PREFERENCE_COLOR_BACKGROUND.equals(property))) {
260 initializeLineNumberRulerColumn(fLineNumberRulerColumn);
264 super.handlePreferenceStoreChanged(event);
268 * Shows the line number ruler column.
270 private void showLineNumberRuler() {
271 IVerticalRuler v = getVerticalRuler();
272 if (v instanceof CompositeRuler) {
273 CompositeRuler c = (CompositeRuler) v;
274 c.addDecorator(1, createLineNumberRulerColumn());
279 * Return whether the line number ruler column should be
280 * visible according to the preference store settings.
281 * @return <code>true</code> if the line numbers should be visible
283 private boolean isLineNumberRulerVisible() {
284 // IPreferenceStore store= getPreferenceStore();
285 return phpPrefStore.getBoolean(IPreferenceConstants.LINE_NUMBER_RULER);
288 * Hides the line number ruler column.
290 private void hideLineNumberRuler() {
291 IVerticalRuler v = getVerticalRuler();
292 if (v instanceof CompositeRuler) {
293 CompositeRuler c = (CompositeRuler) v;
294 c.removeDecorator(1);
299 * Initializes the given line number ruler column from the preference store.
300 * @param rulerColumn the ruler column to be initialized
302 protected void initializeLineNumberRulerColumn(LineNumberRulerColumn rulerColumn) {
303 // JavaTextTools textTools= JavaPlugin.getDefault().getJavaTextTools();
304 PHPColorProvider manager = PHPEditorEnvironment.getPHPColorProvider();
306 if (phpPrefStore != null) {
310 if (phpPrefStore.contains(IPreferenceConstants.LINE_NUMBER_COLOR)) {
311 if (phpPrefStore.isDefault(IPreferenceConstants.LINE_NUMBER_COLOR))
312 rgb = PreferenceConverter.getDefaultColor(phpPrefStore, IPreferenceConstants.LINE_NUMBER_COLOR);
314 rgb = PreferenceConverter.getColor(phpPrefStore, IPreferenceConstants.LINE_NUMBER_COLOR);
316 rulerColumn.setForeground(manager.getColor(rgb));
320 if (!phpPrefStore.getBoolean(IPreferenceConstants.PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT)) {
321 if (phpPrefStore.contains(IPreferenceConstants.PREFERENCE_COLOR_BACKGROUND)) {
322 if (phpPrefStore.isDefault(IPreferenceConstants.PREFERENCE_COLOR_BACKGROUND))
323 rgb = PreferenceConverter.getDefaultColor(phpPrefStore, IPreferenceConstants.PREFERENCE_COLOR_BACKGROUND);
325 rgb = PreferenceConverter.getColor(phpPrefStore, IPreferenceConstants.PREFERENCE_COLOR_BACKGROUND);
327 rulerColumn.setBackground(manager.getColor(rgb));
334 * Creates a new line number ruler column that is appropriately initialized.
336 protected IVerticalRulerColumn createLineNumberRulerColumn() {
337 fLineNumberRulerColumn = new LineNumberRulerColumn();
338 initializeLineNumberRulerColumn(fLineNumberRulerColumn);
339 return fLineNumberRulerColumn;
343 * @see AbstractTextEditor#createVerticalRuler()
345 protected IVerticalRuler createVerticalRuler() {
346 CompositeRuler ruler = new CompositeRuler();
347 ruler.addDecorator(0, new AnnotationRulerColumn(VERTICAL_RULER_WIDTH));
348 if (isLineNumberRulerVisible())
349 ruler.addDecorator(1, createLineNumberRulerColumn());
354 * Method declared on AbstractTextEditor
356 protected void initializeEditor() {
357 PHPEditorEnvironment.connect(this);
359 setSourceViewerConfiguration(new PHPSourceViewerConfiguration());
360 setRangeIndicator(new DefaultRangeIndicator());
361 setEditorContextMenuId("#PHPEditorContext"); //$NON-NLS-1$
362 setRulerContextMenuId("#PHPRulerContext"); //$NON-NLS-1$
363 // setDocumentProvider(PHPeclipsePlugin.getCompilationUnitDocumentProvider());
364 phpPrefStore = PHPeclipsePlugin.getDefault().getPreferenceStore();
366 phpPrefStore.addPropertyChangeListener(new IPropertyChangeListener() {
367 public void propertyChange(PropertyChangeEvent event) {
368 PHPCodeScanner scanner = PHPEditorEnvironment.getPHPCodeScanner();
369 if (scanner != null) {
370 scanner.updateToken(PHPEditorEnvironment.getPHPColorProvider());
372 if (getSourceViewer() != null) {
373 getSourceViewer().invalidateTextPresentation();
376 String property = event.getProperty();
377 if (IPreferenceConstants.LINE_NUMBER_RULER.equals(property)) {
378 if (isLineNumberRulerVisible())
379 showLineNumberRuler();
381 hideLineNumberRuler();