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.phpdt.internal.ui.actions.CompositeActionGroup;
15 import net.sourceforge.phpdt.ui.actions.GenerateActionGroup;
16 import net.sourceforge.phpeclipse.IPreferenceConstants;
17 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
18 import net.sourceforge.phpeclipse.phpeditor.php.PHPCodeScanner;
19 import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IProgressMonitor;
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.IDocument;
29 import org.eclipse.jface.text.ITextOperationTarget;
30 import org.eclipse.jface.text.source.AnnotationRulerColumn;
31 import org.eclipse.jface.text.source.CompositeRuler;
32 import org.eclipse.jface.text.source.ISourceViewer;
33 import org.eclipse.jface.text.source.IVerticalRuler;
34 import org.eclipse.jface.text.source.IVerticalRulerColumn;
35 import org.eclipse.jface.text.source.LineNumberRulerColumn;
36 import org.eclipse.jface.util.IPropertyChangeListener;
37 import org.eclipse.jface.util.PropertyChangeEvent;
38 import org.eclipse.swt.graphics.RGB;
39 import org.eclipse.ui.IEditorInput;
40 import org.eclipse.ui.actions.ActionContext;
41 import org.eclipse.ui.actions.ActionGroup;
42 import org.eclipse.ui.editors.text.TextEditor;
43 import org.eclipse.ui.texteditor.ContentAssistAction;
44 import org.eclipse.ui.texteditor.DefaultRangeIndicator;
45 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
46 import org.eclipse.ui.texteditor.TextOperationAction;
47 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
49 * PHP specific text editor.
51 public class PHPEditor extends TextEditor {
53 // protected PHPActionGroup fActionGroups;
54 /** The outline page */
55 private PHPContentOutlinePage fOutlinePage;
56 private IPreferenceStore phpPrefStore;
58 /** The line number ruler column */
59 private LineNumberRulerColumn fLineNumberRulerColumn;
61 protected CompositeActionGroup fActionGroups;
62 /** The standard action groups added to the menu */
63 private GenerateActionGroup fGenerateActionGroup;
64 private CompositeActionGroup fContextMenuGroup;
66 * Default constructor.
70 setRulerContextMenuId("#PHPRulerContext"); //$NON-NLS-1$
74 // * @see IMember#getCompilationUnit()
76 // public ICompilationUnit getCompilationUnit() {
80 // * @see org.phpeclipse.phpdt.internal.compiler.env.ICompilationUnit#getContents()
82 // public char[] getContents() {
83 // IDocument doc = this.getDocumentProvider().getDocument(this.getEditorInput());
85 // return doc.get().toCharArray();
89 * Returns this document's complete text.
91 * @return the document's complete text
94 IDocument doc = this.getDocumentProvider().getDocument(this.getEditorInput());
99 * Returns the standard action group of this editor.
101 protected ActionGroup getActionGroup() {
102 return fActionGroups;
105 public PHPContentOutlinePage getfOutlinePage() {
109 /** The <code>PHPEditor</code> implementation of this
110 * <code>AbstractTextEditor</code> method extend the
111 * actions to add those specific to the receiver
113 protected void createActions() {
114 super.createActions();
118 // "ContentAssistProposal",
119 // new TextOperationAction(
120 // PHPEditorMessages.getResourceBundle(),
121 // "ContentAssistProposal.",
123 // ISourceViewer.CONTENTASSIST_PROPOSALS));
124 action = new ContentAssistAction(PHPEditorMessages.getResourceBundle(), "ContentAssistProposal.", this); //$NON-NLS-1$
125 action.setActionDefinitionId(
126 PHPEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
127 setAction("ContentAssistProposal", action); //$NON-NLS-1$
129 // System.out.println(action.getId());
130 // System.out.println(action.getActionDefinitionId());
134 new TextOperationAction(
135 PHPEditorMessages.getResourceBundle(),
138 ISourceViewer.CONTENTASSIST_CONTEXT_INFORMATION));
141 new TextOperationAction(
142 PHPEditorMessages.getResourceBundle(),
145 ITextOperationTarget.PREFIX);
146 action.setActionDefinitionId(PHPEditorActionDefinitionIds.COMMENT);
147 setAction("Comment", action);
149 // System.out.println(action.getId());
150 // System.out.println(action.getActionDefinitionId());
153 new TextOperationAction(
154 PHPEditorMessages.getResourceBundle(),
157 ITextOperationTarget.STRIP_PREFIX);
158 action.setActionDefinitionId(PHPEditorActionDefinitionIds.UNCOMMENT);
159 setAction("Uncomment", action);
161 // System.out.println(action.getId());
162 // System.out.println(action.getActionDefinitionId());
164 // fActionGroups = new PHPActionGroup(this, ITextEditorActionConstants.GROUP_EDIT);
166 markAsStateDependentAction("Comment", true); //$NON-NLS-1$
167 markAsStateDependentAction("Uncomment", true); //$NON-NLS-1$
169 fGenerateActionGroup =
170 new GenerateActionGroup(
172 ITextEditorActionConstants.GROUP_EDIT);
175 new CompositeActionGroup(
176 new ActionGroup[] { fGenerateActionGroup });
178 // We have to keep the context menu group separate to have better control over positioning
180 new CompositeActionGroup(
181 new ActionGroup[] { fGenerateActionGroup });
183 // new LocalHistoryActionGroup(this, ITextEditorActionConstants.GROUP_EDIT)});
186 /** The <code>PHPEditor</code> implementation of this
187 * <code>AbstractTextEditor</code> method performs any extra
188 * disposal actions required by the php editor.
190 public void dispose() {
191 PHPEditorEnvironment.disconnect(this);
192 if (fOutlinePage != null)
193 fOutlinePage.setInput(null);
195 if (fActionGroups != null)
196 fActionGroups.dispose();
201 /** The <code>PHPEditor</code> implementation of this
202 * <code>AbstractTextEditor</code> method performs any extra
203 * revert behavior required by the php editor.
205 public void doRevertToSaved() {
206 super.doRevertToSaved();
207 if (fOutlinePage != null)
208 fOutlinePage.update();
211 /** The <code>PHPEditor</code> implementation of this
212 * <code>AbstractTextEditor</code> method performs any extra
213 * save behavior required by the php editor.
215 public void doSave(IProgressMonitor monitor) {
216 super.doSave(monitor);
217 // compile or not, according to the user preferences
218 IPreferenceStore store = phpPrefStore;
219 if (store.getBoolean(PHPeclipsePlugin.PHP_PARSE_ON_SAVE)) {
220 IAction a = PHPParserAction.getInstance();
224 if (fOutlinePage != null)
225 fOutlinePage.update();
228 /** The <code>PHPEditor</code> implementation of this
229 * <code>AbstractTextEditor</code> method performs any extra
230 * save as behavior required by the php editor.
232 public void doSaveAs() {
234 if (fOutlinePage != null)
235 fOutlinePage.update();
238 /** The <code>PHPEditor</code> implementation of this
239 * <code>AbstractTextEditor</code> method performs sets the
240 * input of the outline page after AbstractTextEditor has set input.
242 public void doSetInput(IEditorInput input) throws CoreException {
243 super.doSetInput(input);
244 if (fOutlinePage != null)
245 fOutlinePage.setInput(input);
248 /** The <code>PHPEditor</code> implementation of this
249 * <code>AbstractTextEditor</code> method adds any
250 * PHPEditor specific entries.
252 public void editorContextMenuAboutToShow(MenuManager menu) {
253 super.editorContextMenuAboutToShow(menu);
254 // addAction(menu, "ContentAssistProposal"); //$NON-NLS-1$
255 // addAction(menu, "ContentAssistTip"); //$NON-NLS-1$
257 // fActionGroups.fillContextMenu(menu);
259 ActionContext context =
260 new ActionContext(getSelectionProvider().getSelection());
261 fContextMenuGroup.setContext(context);
262 fContextMenuGroup.fillContextMenu(menu);
263 fContextMenuGroup.setContext(null);
266 protected void updateStateDependentActions() {
267 super.updateStateDependentActions();
268 fGenerateActionGroup.editorStateChanged();
271 /** The <code>PHPEditor</code> implementation of this
272 * <code>AbstractTextEditor</code> method performs gets
273 * the java content outline page if request is for a an
276 public Object getAdapter(Class required) {
277 if (IContentOutlinePage.class.equals(required)) {
278 if (fOutlinePage == null) {
280 new PHPContentOutlinePage(getDocumentProvider(), this);
281 if (getEditorInput() != null)
282 fOutlinePage.setInput(getEditorInput());
286 return super.getAdapter(required);
289 // public void openContextHelp() {
290 // IDocument doc = this.getDocumentProvider().getDocument(this.getEditorInput());
291 // ITextSelection selection = (ITextSelection) this.getSelectionProvider().getSelection();
292 // int pos = selection.getOffset();
293 // String word = getFunctionName(doc, pos);
294 // openContextHelp(word);
297 // private void openContextHelp(String word) {
301 // public static void open(String word) {
302 // IHelp help = WorkbenchHelp.getHelpSupport();
303 // if (help != null) {
304 // IHelpResource helpResource = new PHPFunctionHelpResource(word);
305 // WorkbenchHelp.getHelpSupport().displayHelpResource(helpResource);
307 // // showMessage(shell, dialogTitle, ActionMessages.getString("Open help not available"), false); //$NON-NLS-1$
311 // private String getFunctionName(IDocument doc, int pos) {
312 // Point word = PHPWordExtractor.findWord(doc, pos);
313 // if (word != null) {
315 // return doc.get(word.x, word.y).replace('_', '-');
316 // } catch (BadLocationException e) {
323 * @see AbstractTextEditor#handlePreferenceStoreChanged(PropertyChangeEvent)
325 protected void handlePreferenceStoreChanged(PropertyChangeEvent event) {
329 ISourceViewer sourceViewer = getSourceViewer();
330 if (sourceViewer == null)
333 String property = event.getProperty();
335 // if (JavaSourceViewerConfiguration.PREFERENCE_TAB_WIDTH.equals(property)) {
336 // Object value= event.getNewValue();
337 // if (value instanceof Integer) {
338 // sourceViewer.getTextWidget().setTabs(((Integer) value).intValue());
339 // } else if (value instanceof String) {
340 // sourceViewer.getTextWidget().setTabs(Integer.parseInt((String) value));
345 if (IPreferenceConstants.LINE_NUMBER_RULER.equals(property)) {
346 if (isLineNumberRulerVisible())
347 showLineNumberRuler();
349 hideLineNumberRuler();
353 if (fLineNumberRulerColumn != null
354 && (IPreferenceConstants.LINE_NUMBER_COLOR.equals(property)
355 || PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT.equals(property)
356 || PREFERENCE_COLOR_BACKGROUND.equals(property))) {
358 initializeLineNumberRulerColumn(fLineNumberRulerColumn);
362 super.handlePreferenceStoreChanged(event);
366 * Shows the line number ruler column.
368 private void showLineNumberRuler() {
369 IVerticalRuler v = getVerticalRuler();
370 if (v instanceof CompositeRuler) {
371 CompositeRuler c = (CompositeRuler) v;
372 c.addDecorator(1, createLineNumberRulerColumn());
377 * Return whether the line number ruler column should be
378 * visible according to the preference store settings.
379 * @return <code>true</code> if the line numbers should be visible
381 private boolean isLineNumberRulerVisible() {
382 // IPreferenceStore store= getPreferenceStore();
383 return phpPrefStore.getBoolean(IPreferenceConstants.LINE_NUMBER_RULER);
386 * Hides the line number ruler column.
388 private void hideLineNumberRuler() {
389 IVerticalRuler v = getVerticalRuler();
390 if (v instanceof CompositeRuler) {
391 CompositeRuler c = (CompositeRuler) v;
393 c.removeDecorator(1);
394 } catch (Throwable e) {
400 * Initializes the given line number ruler column from the preference store.
401 * @param rulerColumn the ruler column to be initialized
403 protected void initializeLineNumberRulerColumn(LineNumberRulerColumn rulerColumn) {
404 // JavaTextTools textTools= JavaPlugin.getDefault().getJavaTextTools();
405 PHPColorProvider manager = PHPEditorEnvironment.getPHPColorProvider();
407 if (phpPrefStore != null) {
412 .contains(IPreferenceConstants.LINE_NUMBER_COLOR)) {
414 .isDefault(IPreferenceConstants.LINE_NUMBER_COLOR))
416 PreferenceConverter.getDefaultColor(
418 IPreferenceConstants.LINE_NUMBER_COLOR);
421 PreferenceConverter.getColor(
423 IPreferenceConstants.LINE_NUMBER_COLOR);
425 rulerColumn.setForeground(manager.getColor(rgb));
432 .PREFERENCE_COLOR_BACKGROUND_SYSTEM_DEFAULT)) {
435 IPreferenceConstants.PREFERENCE_COLOR_BACKGROUND)) {
438 IPreferenceConstants.PREFERENCE_COLOR_BACKGROUND))
440 PreferenceConverter.getDefaultColor(
443 .PREFERENCE_COLOR_BACKGROUND);
446 PreferenceConverter.getColor(
449 .PREFERENCE_COLOR_BACKGROUND);
451 rulerColumn.setBackground(manager.getColor(rgb));
458 * Creates a new line number ruler column that is appropriately initialized.
460 protected IVerticalRulerColumn createLineNumberRulerColumn() {
461 fLineNumberRulerColumn = new LineNumberRulerColumn();
462 initializeLineNumberRulerColumn(fLineNumberRulerColumn);
463 return fLineNumberRulerColumn;
467 * @see AbstractTextEditor#createVerticalRuler()
469 protected IVerticalRuler createVerticalRuler() {
470 CompositeRuler ruler = new CompositeRuler();
471 ruler.addDecorator(0, new AnnotationRulerColumn(VERTICAL_RULER_WIDTH));
472 if (isLineNumberRulerVisible())
473 ruler.addDecorator(1, createLineNumberRulerColumn());
478 * Method declared on AbstractTextEditor
480 protected void initializeEditor() {
481 PHPEditorEnvironment.connect(this);
483 setSourceViewerConfiguration(new PHPSourceViewerConfiguration());
484 setRangeIndicator(new DefaultRangeIndicator());
485 setEditorContextMenuId("#PHPEditorContext"); //$NON-NLS-1$
486 setRulerContextMenuId("#PHPRulerContext"); //$NON-NLS-1$
487 // setDocumentProvider(PHPeclipsePlugin.getCompilationUnitDocumentProvider());
488 phpPrefStore = PHPeclipsePlugin.getDefault().getPreferenceStore();
490 phpPrefStore.addPropertyChangeListener(new IPropertyChangeListener() {
491 public void propertyChange(PropertyChangeEvent event) {
492 PHPCodeScanner scanner =
493 PHPEditorEnvironment.getPHPCodeScanner();
494 if (scanner != null) {
496 PHPEditorEnvironment.getPHPColorProvider());
498 if (getSourceViewer() != null) {
499 getSourceViewer().invalidateTextPresentation();
502 String property = event.getProperty();
503 if (IPreferenceConstants.LINE_NUMBER_RULER.equals(property)) {
504 if (isLineNumberRulerVisible())
505 showLineNumberRuler();
507 hideLineNumberRuler();