1 package net.sourceforge.phpeclipse.phpeditor;
3 import java.lang.reflect.InvocationTargetException;
4 import java.text.MessageFormat;
5 import java.util.ArrayList;
6 import java.util.Iterator;
9 import net.sourceforge.phpdt.core.ICompilationUnit;
10 import net.sourceforge.phpdt.core.IJavaElement;
11 import net.sourceforge.phpdt.core.ISourceRange;
12 import net.sourceforge.phpdt.core.ISourceReference;
13 import net.sourceforge.phpdt.core.JavaModelException;
14 import net.sourceforge.phpdt.core.JavaCore;
15 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
16 import net.sourceforge.phpdt.internal.ui.text.ContentAssistPreference;
17 import net.sourceforge.phpdt.internal.ui.text.PHPPairMatcher;
18 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
19 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
20 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI.ExitFlags;
21 import net.sourceforge.phpdt.ui.IWorkingCopyManager;
22 import net.sourceforge.phpdt.ui.PreferenceConstants;
23 import net.sourceforge.phpdt.ui.text.JavaTextTools;
24 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
25 import net.sourceforge.phpeclipse.phpeditor.php.IPHPPartitionScannerConstants;
27 import org.eclipse.core.resources.IFile;
28 import org.eclipse.core.resources.IWorkspace;
29 import org.eclipse.core.resources.ResourcesPlugin;
30 import org.eclipse.core.runtime.CoreException;
31 import org.eclipse.core.runtime.IPath;
32 import org.eclipse.core.runtime.IProgressMonitor;
33 import org.eclipse.core.runtime.Preferences;
34 import org.eclipse.jface.action.IMenuManager;
35 import org.eclipse.jface.dialogs.Dialog;
36 import org.eclipse.jface.dialogs.ErrorDialog;
37 import org.eclipse.jface.dialogs.IMessageProvider;
38 import org.eclipse.jface.dialogs.MessageDialog;
39 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
40 import org.eclipse.jface.preference.IPreferenceStore;
41 import org.eclipse.jface.preference.PreferenceConverter;
42 import org.eclipse.jface.text.BadLocationException;
43 import org.eclipse.jface.text.DocumentCommand;
44 import org.eclipse.jface.text.IDocument;
45 import org.eclipse.jface.text.ILineTracker;
46 import org.eclipse.jface.text.IRegion;
47 import org.eclipse.jface.text.ITextSelection;
48 import org.eclipse.jface.text.ITextViewerExtension;
49 import org.eclipse.jface.text.ITypedRegion;
50 import org.eclipse.jface.text.IWidgetTokenKeeper;
51 import org.eclipse.jface.text.contentassist.ContentAssistant;
52 import org.eclipse.jface.text.contentassist.IContentAssistant;
53 import org.eclipse.jface.text.source.IOverviewRuler;
54 import org.eclipse.jface.text.source.ISourceViewer;
55 import org.eclipse.jface.text.source.IVerticalRuler;
56 import org.eclipse.jface.text.source.SourceViewerConfiguration;
57 import org.eclipse.jface.util.PropertyChangeEvent;
58 import org.eclipse.jface.viewers.ISelectionProvider;
59 import org.eclipse.swt.custom.VerifyKeyListener;
60 import org.eclipse.swt.events.VerifyEvent;
61 import org.eclipse.swt.graphics.Color;
62 import org.eclipse.swt.graphics.Point;
63 import org.eclipse.swt.graphics.RGB;
64 import org.eclipse.swt.widgets.Composite;
65 import org.eclipse.swt.widgets.Display;
66 import org.eclipse.swt.widgets.Shell;
67 import org.eclipse.ui.IEditorInput;
68 import org.eclipse.ui.IFileEditorInput;
69 import org.eclipse.ui.actions.ActionContext;
70 import org.eclipse.ui.actions.WorkspaceModifyOperation;
71 import org.eclipse.ui.dialogs.SaveAsDialog;
72 import org.eclipse.ui.editors.text.IStorageDocumentProvider;
73 import org.eclipse.ui.help.WorkbenchHelp;
74 import org.eclipse.ui.part.FileEditorInput;
75 import org.eclipse.ui.texteditor.IDocumentProvider;
77 /**********************************************************************
78 Copyright (c) 2000, 2002 IBM Corp. and others.
79 All rights reserved. This program and the accompanying materials
80 are made available under the terms of the Common Public License v1.0
81 which accompanies this distribution, and is available at
82 http://www.eclipse.org/legal/cpl-v10.html
85 IBM Corporation - Initial implementation
86 Klaus Hartlage - www.eclipseproject.de
87 **********************************************************************/
89 * PHP specific text editor.
91 public class PHPUnitEditor extends PHPEditor {
93 interface ITextConverter {
94 void customizeDocumentCommand(IDocument document, DocumentCommand command);
97 // class AdaptedRulerLayout extends Layout {
99 // protected int fGap;
100 // protected AdaptedSourceViewer fAdaptedSourceViewer;
102 // protected AdaptedRulerLayout(int gap, AdaptedSourceViewer asv) {
104 // fAdaptedSourceViewer = asv;
107 // protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) {
108 // Control[] children = composite.getChildren();
109 // Point s = children[children.length - 1].computeSize(SWT.DEFAULT, SWT.DEFAULT, flushCache);
110 // if (fAdaptedSourceViewer.isVerticalRulerVisible())
111 // s.x += fAdaptedSourceViewer.getVerticalRuler().getWidth() + fGap;
115 // protected void layout(Composite composite, boolean flushCache) {
116 // Rectangle clArea = composite.getClientArea();
117 // if (fAdaptedSourceViewer.isVerticalRulerVisible()) {
119 // StyledText textWidget = fAdaptedSourceViewer.getTextWidget();
120 // Rectangle trim = textWidget.computeTrim(0, 0, 0, 0);
121 // int scrollbarHeight = trim.height;
123 // IVerticalRuler vr = fAdaptedSourceViewer.getVerticalRuler();
124 // int vrWidth = vr.getWidth();
127 // if (fAdaptedSourceViewer.isOverviewRulerVisible()) {
128 // OverviewRuler or = fAdaptedSourceViewer.getOverviewRuler();
129 // orWidth = or.getWidth();
130 // or.getControl().setBounds(clArea.width - orWidth, scrollbarHeight, orWidth, clArea.height - 3 * scrollbarHeight);
133 // textWidget.setBounds(vrWidth + fGap, 0, clArea.width - vrWidth - orWidth - 2 * fGap, clArea.height);
134 // vr.getControl().setBounds(0, 0, vrWidth, clArea.height - scrollbarHeight);
137 // StyledText textWidget = fAdaptedSourceViewer.getTextWidget();
138 // textWidget.setBounds(0, 0, clArea.width, clArea.height);
143 // class AdaptedSourceViewer extends SourceViewer { // extends JavaCorrectionSourceViewer {
145 // private List fTextConverters;
147 // private OverviewRuler fOverviewRuler;
148 // private boolean fIsOverviewRulerVisible;
149 // /** The viewer's overview ruler hovering controller */
150 // private AbstractHoverInformationControlManager fOverviewRulerHoveringController;
152 // private boolean fIgnoreTextConverters = false;
154 // private IVerticalRuler fCachedVerticalRuler;
155 // private boolean fCachedIsVerticalRulerVisible;
157 // public AdaptedSourceViewer(Composite parent, IVerticalRuler ruler, int styles) {
158 // super(parent, ruler, styles); //, CompilationUnitEditor.this);
160 // fCachedVerticalRuler = ruler;
161 // fCachedIsVerticalRulerVisible = (ruler != null);
162 // fOverviewRuler = new OverviewRuler(VERTICAL_RULER_WIDTH);
164 // delayedCreateControl(parent, styles);
168 // * @see ISourceViewer#showAnnotations(boolean)
170 // public void showAnnotations(boolean show) {
171 // fCachedIsVerticalRulerVisible = (show && fCachedVerticalRuler != null);
172 // // super.showAnnotations(show);
175 // public IContentAssistant getContentAssistant() {
176 // return fContentAssistant;
180 // * @see ITextOperationTarget#doOperation(int)
182 // public void doOperation(int operation) {
184 // if (getTextWidget() == null)
187 // switch (operation) {
188 // case CONTENTASSIST_PROPOSALS :
189 // String msg = fContentAssistant.showPossibleCompletions();
190 // setStatusLineErrorMessage(msg);
193 // fIgnoreTextConverters = true;
196 // fIgnoreTextConverters = true;
200 // super.doOperation(operation);
203 // public void insertTextConverter(ITextConverter textConverter, int index) {
204 // throw new UnsupportedOperationException();
207 // public void addTextConverter(ITextConverter textConverter) {
208 // if (fTextConverters == null) {
209 // fTextConverters = new ArrayList(1);
210 // fTextConverters.add(textConverter);
211 // } else if (!fTextConverters.contains(textConverter))
212 // fTextConverters.add(textConverter);
215 // public void removeTextConverter(ITextConverter textConverter) {
216 // if (fTextConverters != null) {
217 // fTextConverters.remove(textConverter);
218 // if (fTextConverters.size() == 0)
219 // fTextConverters = null;
224 // * @see TextViewer#customizeDocumentCommand(DocumentCommand)
226 // protected void customizeDocumentCommand(DocumentCommand command) {
227 // super.customizeDocumentCommand(command);
228 // if (!fIgnoreTextConverters && fTextConverters != null) {
229 // for (Iterator e = fTextConverters.iterator(); e.hasNext();)
230 // ((ITextConverter) e.next()).customizeDocumentCommand(getDocument(), command);
232 // fIgnoreTextConverters = false;
235 // public IVerticalRuler getVerticalRuler() {
236 // return fCachedVerticalRuler;
239 // public boolean isVerticalRulerVisible() {
240 // return fCachedIsVerticalRulerVisible;
243 // public OverviewRuler getOverviewRuler() {
244 // return fOverviewRuler;
248 // * @see TextViewer#createControl(Composite, int)
250 // protected void createControl(Composite parent, int styles) {
251 // // do nothing here
254 // protected void delayedCreateControl(Composite parent, int styles) {
255 // //create the viewer
256 // super.createControl(parent, styles);
258 // Control control = getControl();
259 // if (control instanceof Composite) {
260 // Composite composite = (Composite) control;
261 // composite.setLayout(new AdaptedRulerLayout(GAP_SIZE, this));
262 // fOverviewRuler.createControl(composite, this);
266 // protected void ensureOverviewHoverManagerInstalled() {
267 // if (fOverviewRulerHoveringController == null && fAnnotationHover != null && fHoverControlCreator != null) {
268 // fOverviewRulerHoveringController =
269 // new OverviewRulerHoverManager(fOverviewRuler, this, fAnnotationHover, fHoverControlCreator);
270 // fOverviewRulerHoveringController.install(fOverviewRuler.getControl());
274 // public void hideOverviewRuler() {
275 // fIsOverviewRulerVisible = false;
276 // Control control = getControl();
277 // if (control instanceof Composite) {
278 // Composite composite = (Composite) control;
279 // composite.layout();
281 // if (fOverviewRulerHoveringController != null) {
282 // fOverviewRulerHoveringController.dispose();
283 // fOverviewRulerHoveringController = null;
287 // public void showOverviewRuler() {
288 // fIsOverviewRulerVisible = true;
289 // Control control = getControl();
290 // if (control instanceof Composite) {
291 // Composite composite = (Composite) control;
292 // composite.layout();
294 // ensureOverviewHoverManagerInstalled();
297 // public boolean isOverviewRulerVisible() {
298 // return fIsOverviewRulerVisible;
302 // * @see ISourceViewer#setDocument(IDocument, IAnnotationModel, int, int)
304 // public void setDocument(
305 // IDocument document,
306 // IAnnotationModel annotationModel,
307 // int visibleRegionOffset,
308 // int visibleRegionLength) {
309 // super.setDocument(document, annotationModel, visibleRegionOffset, visibleRegionLength);
310 // fOverviewRuler.setModel(annotationModel);
313 // // http://dev.eclipse.org/bugs/show_bug.cgi?id=19270
314 // public void updateIndentationPrefixes() {
315 // SourceViewerConfiguration configuration = getSourceViewerConfiguration();
316 // String[] types = configuration.getConfiguredContentTypes(this);
317 // for (int i = 0; i < types.length; i++) {
318 // String[] prefixes = configuration.getIndentPrefixes(this, types[i]);
319 // if (prefixes != null && prefixes.length > 0)
320 // setIndentPrefixes(prefixes, types[i]);
325 // * @see IWidgetTokenOwner#requestWidgetToken(IWidgetTokenKeeper)
327 // public boolean requestWidgetToken(IWidgetTokenKeeper requester) {
328 // if (WorkbenchHelp.isContextHelpDisplayed())
330 // return super.requestWidgetToken(requester);
334 // * @see org.eclipse.jface.text.source.ISourceViewer#configure(org.eclipse.jface.text.source.SourceViewerConfiguration)
336 // public void configure(SourceViewerConfiguration configuration) {
337 // super.configure(configuration);
338 // // prependAutoEditStrategy(new SmartBracesAutoEditStrategy(this), IDocument.DEFAULT_CONTENT_TYPE);
341 // protected void handleDispose() {
342 // fOverviewRuler = null;
344 // if (fOverviewRulerHoveringController != null) {
345 // fOverviewRulerHoveringController.dispose();
346 // fOverviewRulerHoveringController = null;
349 // super.handleDispose();
354 class AdaptedSourceViewer extends JavaSourceViewer {
356 private List fTextConverters;
357 private boolean fIgnoreTextConverters = false;
358 // private JavaCorrectionAssistant fCorrectionAssistant;
360 public AdaptedSourceViewer(
362 IVerticalRuler verticalRuler,
363 IOverviewRuler overviewRuler,
364 boolean showAnnotationsOverview,
366 super(parent, verticalRuler, overviewRuler, showAnnotationsOverview, styles);
369 public IContentAssistant getContentAssistant() {
370 return fContentAssistant;
374 * @see ITextOperationTarget#doOperation(int)
376 public void doOperation(int operation) {
378 if (getTextWidget() == null)
382 case CONTENTASSIST_PROPOSALS :
383 String msg = fContentAssistant.showPossibleCompletions();
384 setStatusLineErrorMessage(msg);
386 // case CORRECTIONASSIST_PROPOSALS:
387 // fCorrectionAssistant.showPossibleCompletions();
390 fIgnoreTextConverters = true;
393 fIgnoreTextConverters = true;
397 super.doOperation(operation);
401 * @see ITextOperationTarget#canDoOperation(int)
403 public boolean canDoOperation(int operation) {
404 // if (operation == CORRECTIONASSIST_PROPOSALS)
405 // return isEditable();
406 return super.canDoOperation(operation);
410 * @see TextViewer#handleDispose()
412 protected void handleDispose() {
413 // if (fCorrectionAssistant != null) {
414 // fCorrectionAssistant.uninstall();
415 // fCorrectionAssistant= null;
417 super.handleDispose();
420 public void insertTextConverter(ITextConverter textConverter, int index) {
421 throw new UnsupportedOperationException();
424 public void addTextConverter(ITextConverter textConverter) {
425 if (fTextConverters == null) {
426 fTextConverters = new ArrayList(1);
427 fTextConverters.add(textConverter);
428 } else if (!fTextConverters.contains(textConverter))
429 fTextConverters.add(textConverter);
432 public void removeTextConverter(ITextConverter textConverter) {
433 if (fTextConverters != null) {
434 fTextConverters.remove(textConverter);
435 if (fTextConverters.size() == 0)
436 fTextConverters = null;
441 * @see TextViewer#customizeDocumentCommand(DocumentCommand)
443 protected void customizeDocumentCommand(DocumentCommand command) {
444 super.customizeDocumentCommand(command);
445 if (!fIgnoreTextConverters && fTextConverters != null) {
446 for (Iterator e = fTextConverters.iterator(); e.hasNext();)
447 ((ITextConverter) e.next()).customizeDocumentCommand(getDocument(), command);
449 fIgnoreTextConverters = false;
452 // http://dev.eclipse.org/bugs/show_bug.cgi?id=19270
453 public void updateIndentationPrefixes() {
454 SourceViewerConfiguration configuration = getSourceViewerConfiguration();
455 String[] types = configuration.getConfiguredContentTypes(this);
456 for (int i = 0; i < types.length; i++) {
457 String[] prefixes = configuration.getIndentPrefixes(this, types[i]);
458 if (prefixes != null && prefixes.length > 0)
459 setIndentPrefixes(prefixes, types[i]);
464 * @see IWidgetTokenOwner#requestWidgetToken(IWidgetTokenKeeper)
466 public boolean requestWidgetToken(IWidgetTokenKeeper requester) {
467 if (WorkbenchHelp.isContextHelpDisplayed())
469 return super.requestWidgetToken(requester);
473 * @see org.eclipse.jface.text.source.ISourceViewer#configure(org.eclipse.jface.text.source.SourceViewerConfiguration)
475 public void configure(SourceViewerConfiguration configuration) {
476 super.configure(configuration);
477 // fCorrectionAssistant= new JavaCorrectionAssistant(CompilationUnitEditor.this);
478 // fCorrectionAssistant.install(this);
479 //TODO install SmartBracesAutoEditStrategy
480 // prependAutoEditStrategy(new SmartBracesAutoEditStrategy(this), IDocument.DEFAULT_CONTENT_TYPE);
483 static class TabConverter implements ITextConverter {
485 private int fTabRatio;
486 private ILineTracker fLineTracker;
488 public TabConverter() {
491 public void setNumberOfSpacesPerTab(int ratio) {
495 public void setLineTracker(ILineTracker lineTracker) {
496 fLineTracker = lineTracker;
499 private int insertTabString(StringBuffer buffer, int offsetInLine) {
504 int remainder = offsetInLine % fTabRatio;
505 remainder = fTabRatio - remainder;
506 for (int i = 0; i < remainder; i++)
511 public void customizeDocumentCommand(IDocument document, DocumentCommand command) {
512 String text = command.text;
516 int index = text.indexOf('\t');
519 StringBuffer buffer = new StringBuffer();
521 fLineTracker.set(command.text);
522 int lines = fLineTracker.getNumberOfLines();
526 for (int i = 0; i < lines; i++) {
528 int offset = fLineTracker.getLineOffset(i);
529 int endOffset = offset + fLineTracker.getLineLength(i);
530 String line = text.substring(offset, endOffset);
534 IRegion firstLine = document.getLineInformationOfOffset(command.offset);
535 position = command.offset - firstLine.getOffset();
538 int length = line.length();
539 for (int j = 0; j < length; j++) {
540 char c = line.charAt(j);
542 position += insertTabString(buffer, position);
551 command.text = buffer.toString();
553 } catch (BadLocationException x) {
559 private static class ExitPolicy implements LinkedPositionUI.ExitPolicy {
561 final char fExitCharacter;
563 public ExitPolicy(char exitCharacter) {
564 fExitCharacter = exitCharacter;
568 * @see org.phpeclipse.phpdt.internal.ui.text.link.LinkedPositionUI.ExitPolicy#doExit(org.phpeclipse.phpdt.internal.ui.text.link.LinkedPositionManager, org.eclipse.swt.events.VerifyEvent, int, int)
570 public ExitFlags doExit(LinkedPositionManager manager, VerifyEvent event, int offset, int length) {
572 if (event.character == fExitCharacter) {
573 if (manager.anyPositionIncludes(offset, length))
574 return new ExitFlags(LinkedPositionUI.COMMIT | LinkedPositionUI.UPDATE_CARET, false);
576 return new ExitFlags(LinkedPositionUI.COMMIT, true);
579 switch (event.character) {
581 if (manager.getFirstPosition().length == 0)
582 return new ExitFlags(0, false);
588 return new ExitFlags(LinkedPositionUI.COMMIT, true);
597 private static class BracketLevel {
600 LinkedPositionManager fManager;
601 LinkedPositionUI fEditor;
604 private class BracketInserter implements VerifyKeyListener, LinkedPositionUI.ExitListener {
606 private boolean fCloseBracketsPHP = true;
607 private boolean fCloseStringsPHP = true;
608 private boolean fCloseBracketsHTML = true;
609 private boolean fCloseStringsHTML = true;
614 public void setCloseBracketsPHPEnabled(boolean enabled) {
615 fCloseBracketsPHP = enabled;
618 public void setCloseStringsPHPEnabled(boolean enabled) {
619 fCloseStringsPHP = enabled;
622 public void setCloseBracketsHTMLEnabled(boolean enabled) {
623 fCloseBracketsHTML = enabled;
626 public void setCloseStringsHTMLEnabled(boolean enabled) {
627 fCloseStringsHTML = enabled;
630 private boolean hasIdentifierToTheRight(IDocument document, int offset) {
633 IRegion endLine = document.getLineInformationOfOffset(end);
634 int maxEnd = endLine.getOffset() + endLine.getLength();
635 while (end != maxEnd && Character.isWhitespace(document.getChar(end)))
638 return end != maxEnd && Scanner.isPHPIdentifierPart(document.getChar(end));
640 } catch (BadLocationException e) {
646 private boolean hasIdentifierToTheLeft(IDocument document, int offset) {
649 IRegion startLine = document.getLineInformationOfOffset(start);
650 int minStart = startLine.getOffset();
651 while (start != minStart && Character.isWhitespace(document.getChar(start - 1)))
654 return start != minStart && Scanner.isPHPIdentifierPart(document.getChar(start - 1));
656 } catch (BadLocationException e) {
661 private boolean hasCharacterToTheRight(IDocument document, int offset, char character) {
664 IRegion endLine = document.getLineInformationOfOffset(end);
665 int maxEnd = endLine.getOffset() + endLine.getLength();
666 while (end != maxEnd && Character.isWhitespace(document.getChar(end)))
669 return end != maxEnd && document.getChar(end) == character;
671 } catch (BadLocationException e) {
678 * @see org.eclipse.swt.custom.VerifyKeyListener#verifyKey(org.eclipse.swt.events.VerifyEvent)
680 public void verifyKey(VerifyEvent event) {
685 final ISourceViewer sourceViewer = getSourceViewer();
686 IDocument document = sourceViewer.getDocument();
688 final Point selection = sourceViewer.getSelectedRange();
689 final int offset = selection.x;
690 final int length = selection.y;
693 ITypedRegion partition = document.getPartition(offset);
694 String type = partition.getType();
695 if (type.equals(IPHPPartitionScannerConstants.PHP)) {
696 switch (event.character) {
698 if (hasCharacterToTheRight(document, offset + length, '('))
704 if (!fCloseBracketsPHP)
706 if (hasIdentifierToTheRight(document, offset + length))
712 if (event.character == '"') {
713 if (!fCloseStringsPHP)
715 // changed for statements like echo "" print ""
716 // if (hasIdentifierToTheLeft(document, offset) || hasIdentifierToTheRight(document, offset + length))
717 if (hasIdentifierToTheRight(document, offset + length))
721 // ITypedRegion partition= document.getPartition(offset);
722 // if (! IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType()) && (partition.getOffset() != offset))
725 final char character = event.character;
726 final char closingCharacter = getPeerCharacter(character);
727 final StringBuffer buffer = new StringBuffer();
728 buffer.append(character);
729 buffer.append(closingCharacter);
731 document.replace(offset, length, buffer.toString());
733 LinkedPositionManager manager = new LinkedPositionManager(document);
734 manager.addPosition(offset + 1, 0);
739 LinkedPositionUI editor = new LinkedPositionUI(sourceViewer, manager);
740 editor.setCancelListener(this);
741 editor.setExitPolicy(new ExitPolicy(closingCharacter));
742 editor.setFinalCaretOffset(offset + 2);
745 IRegion newSelection = editor.getSelectedRegion();
746 sourceViewer.setSelectedRange(newSelection.getOffset(), newSelection.getLength());
750 } else if (type.equals(IPHPPartitionScannerConstants.HTML) || type.equals(IDocument.DEFAULT_CONTENT_TYPE)) {
751 switch (event.character) {
753 if (hasCharacterToTheRight(document, offset + length, '('))
759 if (!fCloseBracketsHTML)
761 if (hasIdentifierToTheRight(document, offset + length))
767 if (event.character == '"') {
768 if (!fCloseStringsHTML)
771 if (hasIdentifierToTheLeft(document, offset) || hasIdentifierToTheRight(document, offset + length))
775 // ITypedRegion partition= document.getPartition(offset);
776 // if (! IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType()) && (partition.getOffset() != offset))
779 final char character = event.character;
780 final char closingCharacter = getPeerCharacter(character);
781 final StringBuffer buffer = new StringBuffer();
782 buffer.append(character);
783 buffer.append(closingCharacter);
785 document.replace(offset, length, buffer.toString());
787 LinkedPositionManager manager = new LinkedPositionManager(document);
788 manager.addPosition(offset + 1, 0);
793 LinkedPositionUI editor = new LinkedPositionUI(sourceViewer, manager);
794 editor.setCancelListener(this);
795 editor.setExitPolicy(new ExitPolicy(closingCharacter));
796 editor.setFinalCaretOffset(offset + 2);
799 IRegion newSelection = editor.getSelectedRegion();
800 sourceViewer.setSelectedRange(newSelection.getOffset(), newSelection.getLength());
805 } catch (BadLocationException e) {
811 * @see org.phpeclipse.phpdt.internal.ui.text.link.LinkedPositionUI.ExitListener#exit(boolean)
813 public void exit(boolean accept) {
819 final ISourceViewer sourceViewer = getSourceViewer();
820 IDocument document = sourceViewer.getDocument();
821 document.replace(fOffset, fLength, null);
822 } catch (BadLocationException e) {
828 /** The editor's save policy */
829 protected ISavePolicy fSavePolicy;
830 /** Listener to annotation model changes that updates the error tick in the tab image */
831 private JavaEditorErrorTickUpdater fJavaEditorErrorTickUpdater;
833 /** The editor's paint manager */
834 // private PaintManager fPaintManager;
835 /** The editor's bracket painter */
836 private BracketPainter fBracketPainter;
837 /** The editor's bracket matcher */
838 private PHPPairMatcher fBracketMatcher;
839 /** The editor's line painter */
840 private LinePainter fLinePainter;
841 /** The editor's print margin ruler painter */
842 private PrintMarginPainter fPrintMarginPainter;
843 /** The editor's problem painter */
844 // private ProblemPainter fProblemPainter;
845 /** The editor's tab converter */
846 private TabConverter fTabConverter;
847 /** History for structure select action */
848 //private SelectionHistory fSelectionHistory;
850 /** The preference property change listener for php core. */
851 // private IPropertyChangeListener fPropertyChangeListener = new PropertyChangeListener();
852 /** The remembered java element */
853 private IJavaElement fRememberedElement;
854 /** The remembered selection */
855 private ITextSelection fRememberedSelection;
856 /** The remembered php element offset */
857 private int fRememberedElementOffset;
858 /** The bracket inserter. */
859 private BracketInserter fBracketInserter = new BracketInserter();
861 // private class PropertyChangeListener implements IPropertyChangeListener {
863 // * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
865 // public void propertyChange(org.eclipse.core.runtime.Preferences.PropertyChangeEvent event) {
866 // handlePreferencePropertyChanged(event);
869 /* Preference key for code formatter tab size */
870 private final static String CODE_FORMATTER_TAB_SIZE = JavaCore.FORMATTER_TAB_SIZE;
871 /** Preference key for matching brackets */
872 private final static String MATCHING_BRACKETS = PreferenceConstants.EDITOR_MATCHING_BRACKETS;
873 /** Preference key for matching brackets color */
874 private final static String MATCHING_BRACKETS_COLOR = PreferenceConstants.EDITOR_MATCHING_BRACKETS_COLOR;
875 /** Preference key for highlighting current line */
876 private final static String CURRENT_LINE = PreferenceConstants.EDITOR_CURRENT_LINE;
877 /** Preference key for highlight color of current line */
878 private final static String CURRENT_LINE_COLOR = PreferenceConstants.EDITOR_CURRENT_LINE_COLOR;
879 /** Preference key for showing print marging ruler */
880 private final static String PRINT_MARGIN = PreferenceConstants.EDITOR_PRINT_MARGIN;
881 /** Preference key for print margin ruler color */
882 private final static String PRINT_MARGIN_COLOR = PreferenceConstants.EDITOR_PRINT_MARGIN_COLOR;
883 /** Preference key for print margin ruler column */
884 private final static String PRINT_MARGIN_COLUMN = PreferenceConstants.EDITOR_PRINT_MARGIN_COLUMN;
885 /** Preference key for inserting spaces rather than tabs */
886 private final static String SPACES_FOR_TABS = PreferenceConstants.EDITOR_SPACES_FOR_TABS;
887 /** Preference key for error indication */
888 // private final static String ERROR_INDICATION = PreferenceConstants.EDITOR_PROBLEM_INDICATION;
889 /** Preference key for error color */
890 // private final static String ERROR_INDICATION_COLOR = PreferenceConstants.EDITOR_PROBLEM_INDICATION_COLOR;
891 /** Preference key for warning indication */
892 // private final static String WARNING_INDICATION = PreferenceConstants.EDITOR_WARNING_INDICATION;
893 /** Preference key for warning color */
894 // private final static String WARNING_INDICATION_COLOR = PreferenceConstants.EDITOR_WARNING_INDICATION_COLOR;
895 /** Preference key for task indication */
896 private final static String TASK_INDICATION = PreferenceConstants.EDITOR_TASK_INDICATION;
897 /** Preference key for task color */
898 private final static String TASK_INDICATION_COLOR = PreferenceConstants.EDITOR_TASK_INDICATION_COLOR;
899 /** Preference key for bookmark indication */
900 private final static String BOOKMARK_INDICATION = PreferenceConstants.EDITOR_BOOKMARK_INDICATION;
901 /** Preference key for bookmark color */
902 private final static String BOOKMARK_INDICATION_COLOR = PreferenceConstants.EDITOR_BOOKMARK_INDICATION_COLOR;
903 /** Preference key for search result indication */
904 private final static String SEARCH_RESULT_INDICATION = PreferenceConstants.EDITOR_SEARCH_RESULT_INDICATION;
905 /** Preference key for search result color */
906 private final static String SEARCH_RESULT_INDICATION_COLOR = PreferenceConstants.EDITOR_SEARCH_RESULT_INDICATION_COLOR;
907 /** Preference key for unknown annotation indication */
908 private final static String UNKNOWN_INDICATION = PreferenceConstants.EDITOR_UNKNOWN_INDICATION;
909 /** Preference key for unknown annotation color */
910 private final static String UNKNOWN_INDICATION_COLOR = PreferenceConstants.EDITOR_UNKNOWN_INDICATION_COLOR;
911 /** Preference key for linked position color */
912 private final static String LINKED_POSITION_COLOR = PreferenceConstants.EDITOR_LINKED_POSITION_COLOR;
913 /** Preference key for shwoing the overview ruler */
914 private final static String OVERVIEW_RULER = PreferenceConstants.EDITOR_OVERVIEW_RULER;
916 /** Preference key for error indication in overview ruler */
917 private final static String ERROR_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_ERROR_INDICATION_IN_OVERVIEW_RULER;
918 /** Preference key for warning indication in overview ruler */
919 private final static String WARNING_INDICATION_IN_OVERVIEW_RULER =
920 PreferenceConstants.EDITOR_WARNING_INDICATION_IN_OVERVIEW_RULER;
921 /** Preference key for task indication in overview ruler */
922 private final static String TASK_INDICATION_IN_OVERVIEW_RULER = PreferenceConstants.EDITOR_TASK_INDICATION_IN_OVERVIEW_RULER;
923 /** Preference key for bookmark indication in overview ruler */
924 private final static String BOOKMARK_INDICATION_IN_OVERVIEW_RULER =
925 PreferenceConstants.EDITOR_BOOKMARK_INDICATION_IN_OVERVIEW_RULER;
926 /** Preference key for search result indication in overview ruler */
927 private final static String SEARCH_RESULT_INDICATION_IN_OVERVIEW_RULER =
928 PreferenceConstants.EDITOR_SEARCH_RESULT_INDICATION_IN_OVERVIEW_RULER;
929 /** Preference key for unknown annotation indication in overview ruler */
930 private final static String UNKNOWN_INDICATION_IN_OVERVIEW_RULER =
931 PreferenceConstants.EDITOR_UNKNOWN_INDICATION_IN_OVERVIEW_RULER;
932 /** Preference key for automatically closing strings */
933 private final static String CLOSE_STRINGS_PHP = PreferenceConstants.EDITOR_CLOSE_STRINGS_PHP;
934 /** Preference key for automatically wrapping Java strings */
935 private final static String WRAP_STRINGS = PreferenceConstants.EDITOR_WRAP_STRINGS;
936 /** Preference key for automatically closing brackets and parenthesis */
937 private final static String CLOSE_BRACKETS_PHP = PreferenceConstants.EDITOR_CLOSE_BRACKETS_PHP;
938 /** Preference key for automatically closing phpdocs and comments */
939 private final static String CLOSE_JAVADOCS = PreferenceConstants.EDITOR_CLOSE_JAVADOCS;
940 /** Preference key for automatically adding phpdoc tags */
941 private final static String ADD_JAVADOC_TAGS = PreferenceConstants.EDITOR_ADD_JAVADOC_TAGS;
942 /** Preference key for automatically formatting phpdocs */
943 private final static String FORMAT_JAVADOCS = PreferenceConstants.EDITOR_FORMAT_JAVADOCS;
944 /** Preference key for automatically closing strings */
945 private final static String CLOSE_STRINGS_HTML = PreferenceConstants.EDITOR_CLOSE_STRINGS_HTML;
946 /** Preference key for automatically closing brackets and parenthesis */
947 private final static String CLOSE_BRACKETS_HTML = PreferenceConstants.EDITOR_CLOSE_BRACKETS_HTML;
949 /** Preference key for smart paste */
950 private final static String SMART_PASTE = PreferenceConstants.EDITOR_SMART_PASTE;
951 // private final static class AnnotationInfo {
952 // public String fColorPreference;
953 // public String fOverviewRulerPreference;
954 // public String fEditorPreference;
957 // private final static Map ANNOTATION_MAP;
960 // AnnotationInfo info;
961 // ANNOTATION_MAP = new HashMap();
963 // info = new AnnotationInfo();
964 // info.fColorPreference = TASK_INDICATION_COLOR;
965 // info.fOverviewRulerPreference = TASK_INDICATION_IN_OVERVIEW_RULER;
966 // info.fEditorPreference = TASK_INDICATION;
967 // ANNOTATION_MAP.put(AnnotationType.TASK, info);
969 // info = new AnnotationInfo();
970 // info.fColorPreference = ERROR_INDICATION_COLOR;
971 // info.fOverviewRulerPreference = ERROR_INDICATION_IN_OVERVIEW_RULER;
972 // info.fEditorPreference = ERROR_INDICATION;
973 // ANNOTATION_MAP.put(AnnotationType.ERROR, info);
975 // info = new AnnotationInfo();
976 // info.fColorPreference = WARNING_INDICATION_COLOR;
977 // info.fOverviewRulerPreference = WARNING_INDICATION_IN_OVERVIEW_RULER;
978 // info.fEditorPreference = WARNING_INDICATION;
979 // ANNOTATION_MAP.put(AnnotationType.WARNING, info);
981 // info = new AnnotationInfo();
982 // info.fColorPreference = BOOKMARK_INDICATION_COLOR;
983 // info.fOverviewRulerPreference = BOOKMARK_INDICATION_IN_OVERVIEW_RULER;
984 // info.fEditorPreference = BOOKMARK_INDICATION;
985 // ANNOTATION_MAP.put(AnnotationType.BOOKMARK, info);
987 // info = new AnnotationInfo();
988 // info.fColorPreference = SEARCH_RESULT_INDICATION_COLOR;
989 // info.fOverviewRulerPreference = SEARCH_RESULT_INDICATION_IN_OVERVIEW_RULER;
990 // info.fEditorPreference = SEARCH_RESULT_INDICATION;
991 // ANNOTATION_MAP.put(AnnotationType.SEARCH, info);
993 // info = new AnnotationInfo();
994 // info.fColorPreference = UNKNOWN_INDICATION_COLOR;
995 // info.fOverviewRulerPreference = UNKNOWN_INDICATION_IN_OVERVIEW_RULER;
996 // info.fEditorPreference = UNKNOWN_INDICATION;
997 // ANNOTATION_MAP.put(AnnotationType.UNKNOWN, info);
1000 // private final static AnnotationType[] ANNOTATION_LAYERS =
1001 // new AnnotationType[] {
1002 // AnnotationType.UNKNOWN,
1003 // AnnotationType.BOOKMARK,
1004 // AnnotationType.TASK,
1005 // AnnotationType.SEARCH,
1006 // AnnotationType.WARNING,
1007 // AnnotationType.ERROR };
1009 * Creates a new php unit editor.
1011 public PHPUnitEditor() {
1013 setDocumentProvider(PHPeclipsePlugin.getDefault().getCompilationUnitDocumentProvider());
1014 setEditorContextMenuId("#PHPEditorContext"); //$NON-NLS-1$
1015 setRulerContextMenuId("#PHPRulerContext"); //$NON-NLS-1$
1016 setOutlinerContextMenuId("#PHPOutlinerContext"); //$NON-NLS-1$
1017 // don't set help contextId, we install our own help context
1020 fJavaEditorErrorTickUpdater = new JavaEditorErrorTickUpdater(this);
1024 * @see JavaEditor#getElementAt(int)
1026 protected IJavaElement getElementAt(int offset) {
1027 return getElementAt(offset, true);
1031 * Returns the most narrow element including the given offset. If <code>reconcile</code>
1032 * is <code>true</code> the editor's input element is reconciled in advance. If it is
1033 * <code>false</code> this method only returns a result if the editor's input element
1034 * does not need to be reconciled.
1036 * @param offset the offset included by the retrieved element
1037 * @param reconcile <code>true</code> if working copy should be reconciled
1039 protected IJavaElement getElementAt(int offset, boolean reconcile) {
1040 IWorkingCopyManager manager = PHPeclipsePlugin.getDefault().getWorkingCopyManager();
1041 ICompilationUnit unit = manager.getWorkingCopy(getEditorInput());
1046 synchronized (unit) {
1049 return unit.getElementAt(offset);
1050 } else if (unit.isConsistent())
1051 return unit.getElementAt(offset);
1053 } catch (JavaModelException x) {
1054 PHPeclipsePlugin.log(x.getStatus());
1055 // nothing found, be tolerant and go on
1063 * @see JavaEditor#getCorrespondingElement(IJavaElement)
1065 protected IJavaElement getCorrespondingElement(IJavaElement element) {
1067 return EditorUtility.getWorkingCopy(element, true);
1068 } catch (JavaModelException x) {
1069 PHPeclipsePlugin.log(x.getStatus());
1070 // nothing found, be tolerant and go on
1075 public void createPartControl(Composite parent) {
1076 super.createPartControl(parent);
1078 // fPaintManager = new PaintManager(getSourceViewer());
1080 LinePainter linePainter;
1081 linePainter = new LinePainter(getSourceViewer());
1083 linePainter.setHighlightColor(new Color(Display.getCurrent(), 225, 235, 224));
1085 // fPaintManager.addPainter(linePainter);
1087 if (isBracketHighlightingEnabled())
1088 startBracketHighlighting();
1089 if (isLineHighlightingEnabled())
1090 startLineHighlighting();
1091 if (isPrintMarginVisible())
1094 // Iterator e = ANNOTATION_MAP.keySet().iterator();
1095 // while (e.hasNext()) {
1096 // AnnotationType type = (AnnotationType) e.next();
1097 // if (isAnnotationIndicationEnabled(type))
1098 // startAnnotationIndication(type);
1101 if (isTabConversionEnabled())
1102 startTabConversion();
1104 // if (isOverviewRulerVisible())
1105 // showOverviewRuler();
1107 // Preferences preferences = PHPeclipsePlugin.getDefault().getPluginPreferences();
1108 // preferences.addPropertyChangeListener(fPropertyChangeListener);
1110 IPreferenceStore preferenceStore = getPreferenceStore();
1111 boolean closeBracketsPHP = preferenceStore.getBoolean(CLOSE_BRACKETS_PHP);
1112 boolean closeStringsPHP = preferenceStore.getBoolean(CLOSE_STRINGS_PHP);
1113 boolean closeBracketsHTML = preferenceStore.getBoolean(CLOSE_BRACKETS_HTML);
1114 boolean closeStringsHTML = preferenceStore.getBoolean(CLOSE_STRINGS_HTML);
1116 fBracketInserter.setCloseBracketsPHPEnabled(closeBracketsPHP);
1117 fBracketInserter.setCloseStringsPHPEnabled(closeStringsPHP);
1118 fBracketInserter.setCloseBracketsHTMLEnabled(closeBracketsHTML);
1119 fBracketInserter.setCloseStringsHTMLEnabled(closeStringsHTML);
1121 ISourceViewer sourceViewer = getSourceViewer();
1122 if (sourceViewer instanceof ITextViewerExtension)
1123 ((ITextViewerExtension) sourceViewer).prependVerifyKeyListener(fBracketInserter);
1127 private static char getPeerCharacter(char character) {
1128 switch (character) {
1145 throw new IllegalArgumentException();
1149 * The compilation unit editor implementation of this <code>AbstractTextEditor</code>
1150 * method asks the user for the workspace path of a file resource and saves the document
1151 * there. See http://dev.eclipse.org/bugs/show_bug.cgi?id=6295
1153 protected void performSaveAs(IProgressMonitor progressMonitor) {
1155 Shell shell = getSite().getShell();
1156 IEditorInput input = getEditorInput();
1158 SaveAsDialog dialog = new SaveAsDialog(shell);
1160 IFile original = (input instanceof IFileEditorInput) ? ((IFileEditorInput) input).getFile() : null;
1161 if (original != null)
1162 dialog.setOriginalFile(original);
1166 IDocumentProvider provider = getDocumentProvider();
1167 if (provider == null) {
1168 // editor has been programmatically closed while the dialog was open
1172 if (provider.isDeleted(input) && original != null) {
1173 String message = PHPEditorMessages.getFormattedString("CompilationUnitEditor.warning.save.delete", new Object[] { original.getName()}); //$NON-NLS-1$
1174 dialog.setErrorMessage(null);
1175 dialog.setMessage(message, IMessageProvider.WARNING);
1178 if (dialog.open() == Dialog.CANCEL) {
1179 if (progressMonitor != null)
1180 progressMonitor.setCanceled(true);
1184 IPath filePath = dialog.getResult();
1185 if (filePath == null) {
1186 if (progressMonitor != null)
1187 progressMonitor.setCanceled(true);
1191 IWorkspace workspace = ResourcesPlugin.getWorkspace();
1192 IFile file = workspace.getRoot().getFile(filePath);
1193 final IEditorInput newInput = new FileEditorInput(file);
1195 WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
1196 public void execute(final IProgressMonitor monitor) throws CoreException {
1197 getDocumentProvider().saveDocument(monitor, newInput, getDocumentProvider().getDocument(getEditorInput()), true);
1201 boolean success = false;
1204 provider.aboutToChange(newInput);
1205 new ProgressMonitorDialog(shell).run(false, true, op);
1208 } catch (InterruptedException x) {
1209 } catch (InvocationTargetException x) {
1211 Throwable t = x.getTargetException();
1212 if (t instanceof CoreException) {
1213 CoreException cx = (CoreException) t;
1214 ErrorDialog.openError(shell, PHPEditorMessages.getString("CompilationUnitEditor.error.saving.title2"), PHPEditorMessages.getString("CompilationUnitEditor.error.saving.message2"), cx.getStatus()); //$NON-NLS-1$ //$NON-NLS-2$
1216 MessageDialog.openError(shell, PHPEditorMessages.getString("CompilationUnitEditor.error.saving.title3"), PHPEditorMessages.getString("CompilationUnitEditor.error.saving.message3") + t.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
1220 provider.changed(newInput);
1225 if (progressMonitor != null)
1226 progressMonitor.setCanceled(!success);
1229 * @see AbstractTextEditor#doSetInput(IEditorInput)
1231 protected void doSetInput(IEditorInput input) throws CoreException {
1232 super.doSetInput(input);
1233 configureTabConverter();
1236 private void startBracketHighlighting() {
1237 if (fBracketPainter == null) {
1238 ISourceViewer sourceViewer = getSourceViewer();
1239 fBracketPainter = new BracketPainter(sourceViewer);
1240 fBracketPainter.setHighlightColor(getColor(MATCHING_BRACKETS_COLOR));
1241 // fPaintManager.addPainter(fBracketPainter);
1245 private void stopBracketHighlighting() {
1246 if (fBracketPainter != null) {
1247 // fPaintManager.removePainter(fBracketPainter);
1248 fBracketPainter.deactivate(true);
1249 fBracketPainter.dispose();
1250 fBracketPainter = null;
1254 private boolean isBracketHighlightingEnabled() {
1255 IPreferenceStore store = getPreferenceStore();
1256 return store.getBoolean(MATCHING_BRACKETS);
1259 private void startLineHighlighting() {
1260 if (fLinePainter == null) {
1261 ISourceViewer sourceViewer = getSourceViewer();
1262 fLinePainter = new LinePainter(sourceViewer);
1263 fLinePainter.setHighlightColor(getColor(CURRENT_LINE_COLOR));
1264 // fPaintManager.addPainter(fLinePainter);
1268 private void stopLineHighlighting() {
1269 if (fLinePainter != null) {
1270 // fPaintManager.removePainter(fLinePainter);
1271 fLinePainter.deactivate(true);
1272 fLinePainter.dispose();
1273 fLinePainter = null;
1277 private boolean isLineHighlightingEnabled() {
1278 IPreferenceStore store = getPreferenceStore();
1279 return store.getBoolean(CURRENT_LINE);
1282 private void showPrintMargin() {
1283 if (fPrintMarginPainter == null) {
1284 fPrintMarginPainter = new PrintMarginPainter(getSourceViewer());
1285 fPrintMarginPainter.setMarginRulerColor(getColor(PRINT_MARGIN_COLOR));
1286 fPrintMarginPainter.setMarginRulerColumn(getPreferenceStore().getInt(PRINT_MARGIN_COLUMN));
1287 // fPaintManager.addPainter(fPrintMarginPainter);
1291 private void hidePrintMargin() {
1292 if (fPrintMarginPainter != null) {
1293 // fPaintManager.removePainter(fPrintMarginPainter);
1294 fPrintMarginPainter.deactivate(true);
1295 fPrintMarginPainter.dispose();
1296 fPrintMarginPainter = null;
1300 private boolean isPrintMarginVisible() {
1301 IPreferenceStore store = getPreferenceStore();
1302 return store.getBoolean(PRINT_MARGIN);
1305 // private void startAnnotationIndication(AnnotationType annotationType) {
1306 // if (fProblemPainter == null) {
1307 // fProblemPainter = new ProblemPainter(this, getSourceViewer());
1308 //// fPaintManager.addPainter(fProblemPainter);
1310 // fProblemPainter.setColor(annotationType, getColor(annotationType));
1311 // fProblemPainter.paintAnnotations(annotationType, true);
1312 // fProblemPainter.paint(IPainter.CONFIGURATION);
1315 // private void shutdownAnnotationIndication() {
1316 // if (fProblemPainter != null) {
1318 // if (!fProblemPainter.isPaintingAnnotations()) {
1319 //// fPaintManager.removePainter(fProblemPainter);
1320 // fProblemPainter.deactivate(true);
1321 // fProblemPainter.dispose();
1322 // fProblemPainter = null;
1324 // fProblemPainter.paint(IPainter.CONFIGURATION);
1329 // private void stopAnnotationIndication(AnnotationType annotationType) {
1330 // if (fProblemPainter != null) {
1331 // fProblemPainter.paintAnnotations(annotationType, false);
1332 // shutdownAnnotationIndication();
1336 // private boolean isAnnotationIndicationEnabled(AnnotationType annotationType) {
1337 // IPreferenceStore store = getPreferenceStore();
1338 // AnnotationInfo info = (AnnotationInfo) ANNOTATION_MAP.get(annotationType);
1339 // if (info != null)
1340 // return store.getBoolean(info.fEditorPreference);
1344 // private boolean isAnnotationIndicationInOverviewRulerEnabled(AnnotationType annotationType) {
1345 // IPreferenceStore store = getPreferenceStore();
1346 // AnnotationInfo info = (AnnotationInfo) ANNOTATION_MAP.get(annotationType);
1347 // if (info != null)
1348 // return store.getBoolean(info.fOverviewRulerPreference);
1352 // private void showAnnotationIndicationInOverviewRuler(AnnotationType annotationType, boolean show) {
1353 // AdaptedSourceViewer asv = (AdaptedSourceViewer) getSourceViewer();
1354 // OverviewRuler ruler = asv.getOverviewRuler();
1355 // if (ruler != null) {
1356 // ruler.setColor(annotationType, getColor(annotationType));
1357 // ruler.showAnnotation(annotationType, show);
1362 // private void setColorInOverviewRuler(AnnotationType annotationType, Color color) {
1363 // AdaptedSourceViewer asv = (AdaptedSourceViewer) getSourceViewer();
1364 // OverviewRuler ruler = asv.getOverviewRuler();
1365 // if (ruler != null) {
1366 // ruler.setColor(annotationType, color);
1371 private void configureTabConverter() {
1372 if (fTabConverter != null) {
1373 IDocumentProvider provider = getDocumentProvider();
1374 if (provider instanceof PHPDocumentProvider) {
1375 PHPDocumentProvider cup = (PHPDocumentProvider) provider;
1376 fTabConverter.setLineTracker(cup.createLineTracker(getEditorInput()));
1381 private int getTabSize() {
1382 Preferences preferences = PHPeclipsePlugin.getDefault().getPluginPreferences();
1383 return preferences.getInt(CODE_FORMATTER_TAB_SIZE);
1386 private void startTabConversion() {
1387 if (fTabConverter == null) {
1388 fTabConverter = new TabConverter();
1389 configureTabConverter();
1390 fTabConverter.setNumberOfSpacesPerTab(getTabSize());
1391 AdaptedSourceViewer asv = (AdaptedSourceViewer) getSourceViewer();
1392 asv.addTextConverter(fTabConverter);
1393 // http://dev.eclipse.org/bugs/show_bug.cgi?id=19270
1394 asv.updateIndentationPrefixes();
1398 private void stopTabConversion() {
1399 if (fTabConverter != null) {
1400 AdaptedSourceViewer asv = (AdaptedSourceViewer) getSourceViewer();
1401 asv.removeTextConverter(fTabConverter);
1402 // http://dev.eclipse.org/bugs/show_bug.cgi?id=19270
1403 asv.updateIndentationPrefixes();
1404 fTabConverter = null;
1408 private boolean isTabConversionEnabled() {
1409 IPreferenceStore store = getPreferenceStore();
1410 return store.getBoolean(SPACES_FOR_TABS);
1413 // private void showOverviewRuler() {
1414 // AdaptedSourceViewer asv = (AdaptedSourceViewer) getSourceViewer();
1415 // asv.showOverviewRuler();
1417 // OverviewRuler overviewRuler = asv.getOverviewRuler();
1418 // if (overviewRuler != null) {
1419 // for (int i = 0; i < ANNOTATION_LAYERS.length; i++) {
1420 // AnnotationType type = ANNOTATION_LAYERS[i];
1421 // overviewRuler.setLayer(type, i);
1422 // if (isAnnotationIndicationInOverviewRulerEnabled(type))
1423 // showAnnotationIndicationInOverviewRuler(type, true);
1428 // private void hideOverviewRuler() {
1429 // AdaptedSourceViewer asv = (AdaptedSourceViewer) getSourceViewer();
1430 // asv.hideOverviewRuler();
1433 // private boolean isOverviewRulerVisible() {
1434 // IPreferenceStore store = getPreferenceStore();
1435 // return store.getBoolean(OVERVIEW_RULER);
1438 private Color getColor(String key) {
1439 RGB rgb = PreferenceConverter.getColor(getPreferenceStore(), key);
1440 return getColor(rgb);
1443 private Color getColor(RGB rgb) {
1444 JavaTextTools textTools = PHPeclipsePlugin.getDefault().getJavaTextTools();
1445 return textTools.getColorManager().getColor(rgb);
1448 // private Color getColor(AnnotationType annotationType) {
1449 // AnnotationInfo info = (AnnotationInfo) ANNOTATION_MAP.get(annotationType);
1450 // if (info != null)
1451 // return getColor(info.fColorPreference);
1455 public void dispose() {
1456 ISourceViewer sourceViewer = getSourceViewer();
1457 if (sourceViewer instanceof ITextViewerExtension)
1458 ((ITextViewerExtension) sourceViewer).removeVerifyKeyListener(fBracketInserter);
1460 // if (fPropertyChangeListener != null) {
1461 // Preferences preferences = PHPeclipsePlugin.getDefault().getPluginPreferences();
1462 // preferences.removePropertyChangeListener(fPropertyChangeListener);
1463 // fPropertyChangeListener = null;
1466 if (fJavaEditorErrorTickUpdater != null) {
1467 fJavaEditorErrorTickUpdater.dispose();
1468 fJavaEditorErrorTickUpdater = null;
1471 // if (fSelectionHistory != null)
1472 // fSelectionHistory.dispose();
1474 // if (fPaintManager != null) {
1475 // fPaintManager.dispose();
1476 // fPaintManager = null;
1479 if (fActionGroups != null) {
1480 fActionGroups.dispose();
1481 fActionGroups = null;
1487 // protected AnnotationType getAnnotationType(String preferenceKey) {
1488 // Iterator e = ANNOTATION_MAP.keySet().iterator();
1489 // while (e.hasNext()) {
1490 // AnnotationType type = (AnnotationType) e.next();
1491 // AnnotationInfo info = (AnnotationInfo) ANNOTATION_MAP.get(type);
1492 // if (info != null) {
1493 // if (preferenceKey.equals(info.fColorPreference)
1494 // || preferenceKey.equals(info.fEditorPreference)
1495 // || preferenceKey.equals(info.fOverviewRulerPreference))
1503 * @see AbstractTextEditor#handlePreferenceStoreChanged(PropertyChangeEvent)
1505 protected void handlePreferenceStoreChanged(PropertyChangeEvent event) {
1509 AdaptedSourceViewer asv = (AdaptedSourceViewer) getSourceViewer();
1512 String p = event.getProperty();
1514 if (CLOSE_BRACKETS_PHP.equals(p)) {
1515 fBracketInserter.setCloseBracketsPHPEnabled(getPreferenceStore().getBoolean(p));
1519 if (CLOSE_STRINGS_PHP.equals(p)) {
1520 fBracketInserter.setCloseStringsPHPEnabled(getPreferenceStore().getBoolean(p));
1524 if (CLOSE_BRACKETS_HTML.equals(p)) {
1525 fBracketInserter.setCloseBracketsHTMLEnabled(getPreferenceStore().getBoolean(p));
1529 if (CLOSE_STRINGS_HTML.equals(p)) {
1530 fBracketInserter.setCloseStringsHTMLEnabled(getPreferenceStore().getBoolean(p));
1534 if (SPACES_FOR_TABS.equals(p)) {
1535 if (isTabConversionEnabled())
1536 startTabConversion();
1538 stopTabConversion();
1542 if (MATCHING_BRACKETS.equals(p)) {
1543 if (isBracketHighlightingEnabled())
1544 startBracketHighlighting();
1546 stopBracketHighlighting();
1550 if (MATCHING_BRACKETS_COLOR.equals(p)) {
1551 if (fBracketPainter != null)
1552 fBracketPainter.setHighlightColor(getColor(MATCHING_BRACKETS_COLOR));
1556 if (CURRENT_LINE.equals(p)) {
1557 if (isLineHighlightingEnabled())
1558 startLineHighlighting();
1560 stopLineHighlighting();
1564 if (CURRENT_LINE_COLOR.equals(p)) {
1565 if (fLinePainter != null) {
1566 stopLineHighlighting();
1567 startLineHighlighting();
1572 if (PRINT_MARGIN.equals(p)) {
1573 if (isPrintMarginVisible())
1580 if (PRINT_MARGIN_COLOR.equals(p)) {
1581 if (fPrintMarginPainter != null)
1582 fPrintMarginPainter.setMarginRulerColor(getColor(PRINT_MARGIN_COLOR));
1586 if (PRINT_MARGIN_COLUMN.equals(p)) {
1587 if (fPrintMarginPainter != null)
1588 fPrintMarginPainter.setMarginRulerColumn(getPreferenceStore().getInt(PRINT_MARGIN_COLUMN));
1592 // if (OVERVIEW_RULER.equals(p)) {
1593 // if (isOverviewRulerVisible())
1594 // showOverviewRuler();
1596 // hideOverviewRuler();
1600 // AnnotationType type = getAnnotationType(p);
1601 // if (type != null) {
1603 // AnnotationInfo info = (AnnotationInfo) ANNOTATION_MAP.get(type);
1604 // if (info.fColorPreference.equals(p)) {
1605 // Color color = getColor(type);
1606 // if (fProblemPainter != null) {
1607 // fProblemPainter.setColor(type, color);
1608 // fProblemPainter.paint(IPainter.CONFIGURATION);
1610 // setColorInOverviewRuler(type, color);
1614 // if (info.fEditorPreference.equals(p)) {
1615 // if (isAnnotationIndicationEnabled(type))
1616 // startAnnotationIndication(type);
1618 // stopAnnotationIndication(type);
1622 // if (info.fOverviewRulerPreference.equals(p)) {
1623 // if (isAnnotationIndicationInOverviewRulerEnabled(type))
1624 // showAnnotationIndicationInOverviewRuler(type, true);
1626 // showAnnotationIndicationInOverviewRuler(type, false);
1631 IContentAssistant c = asv.getContentAssistant();
1632 if (c instanceof ContentAssistant)
1633 ContentAssistPreference.changeConfiguration((ContentAssistant) c, getPreferenceStore(), event);
1637 super.handlePreferenceStoreChanged(event);
1642 * @see org.eclipse.jdt.internal.ui.javaeditor.JavaEditor#handlePreferencePropertyChanged(org.eclipse.core.runtime.Preferences.PropertyChangeEvent)
1644 protected void handlePreferencePropertyChanged(org.eclipse.core.runtime.Preferences.PropertyChangeEvent event) {
1645 AdaptedSourceViewer asv = (AdaptedSourceViewer) getSourceViewer();
1647 String p = event.getProperty();
1648 if (CODE_FORMATTER_TAB_SIZE.equals(p)) {
1649 asv.updateIndentationPrefixes();
1650 if (fTabConverter != null)
1651 fTabConverter.setNumberOfSpacesPerTab(getTabSize());
1654 super.handlePreferencePropertyChanged(event);
1658 * Handles a property change event describing a change
1659 * of the php core's preferences and updates the preference
1660 * related editor properties.
1662 * @param event the property change event
1664 // protected void handlePreferencePropertyChanged(org.eclipse.core.runtime.Preferences.PropertyChangeEvent event) {
1665 // AdaptedSourceViewer asv = (AdaptedSourceViewer) getSourceViewer();
1666 // if (asv != null) {
1667 // String p = event.getProperty();
1668 // if (CODE_FORMATTER_TAB_SIZE.equals(p)) {
1669 // asv.updateIndentationPrefixes();
1670 // if (fTabConverter != null)
1671 // fTabConverter.setNumberOfSpacesPerTab(getTabSize());
1677 * @see PHPEditor#createJavaSourceViewer(Composite, IVerticalRuler, int)
1679 protected ISourceViewer createJavaSourceViewer(
1681 IVerticalRuler verticalRuler,
1682 IOverviewRuler overviewRuler,
1683 boolean isOverviewRulerVisible,
1685 return new AdaptedSourceViewer(parent, verticalRuler, overviewRuler, isOverviewRulerVisible, styles);
1687 // protected ISourceViewer createJavaSourceViewer(Composite parent, IVerticalRuler ruler, int styles) {
1688 // return new AdaptedSourceViewer(parent, ruler, styles);
1691 private boolean isValidSelection(int offset, int length) {
1692 IDocumentProvider provider = getDocumentProvider();
1693 if (provider != null) {
1694 IDocument document = provider.getDocument(getEditorInput());
1695 if (document != null) {
1696 int end = offset + length;
1697 int documentLength = document.getLength();
1698 return 0 <= offset && offset <= documentLength && 0 <= end && end <= documentLength;
1705 * @see AbstractTextEditor#canHandleMove(IEditorInput, IEditorInput)
1707 protected boolean canHandleMove(IEditorInput originalElement, IEditorInput movedElement) {
1709 String oldExtension = ""; //$NON-NLS-1$
1710 if (originalElement instanceof IFileEditorInput) {
1711 IFile file = ((IFileEditorInput) originalElement).getFile();
1713 String ext = file.getFileExtension();
1719 String newExtension = ""; //$NON-NLS-1$
1720 if (movedElement instanceof IFileEditorInput) {
1721 IFile file = ((IFileEditorInput) movedElement).getFile();
1723 newExtension = file.getFileExtension();
1726 return oldExtension.equals(newExtension);
1730 * @see AbstractTextEditor#editorContextMenuAboutToShow(IMenuManager)
1732 public void editorContextMenuAboutToShow(IMenuManager menu) {
1733 super.editorContextMenuAboutToShow(menu);
1735 ActionContext context = new ActionContext(getSelectionProvider().getSelection());
1736 fContextMenuGroup.setContext(context);
1737 fContextMenuGroup.fillContextMenu(menu);
1738 fContextMenuGroup.setContext(null);
1742 * @see JavaEditor#setOutlinePageInput(JavaOutlinePage, IEditorInput)
1744 protected void setOutlinePageInput(JavaOutlinePage page, IEditorInput input) {
1746 IWorkingCopyManager manager = PHPeclipsePlugin.getDefault().getWorkingCopyManager();
1747 page.setInput(manager.getWorkingCopy(input));
1752 * @see AbstractTextEditor#performSaveOperation(WorkspaceModifyOperation, IProgressMonitor)
1754 // protected void performSaveOperation(WorkspaceModifyOperation operation, IProgressMonitor progressMonitor) {
1755 // IDocumentProvider p = getDocumentProvider();
1756 // if (p instanceof PHPDocumentProvider) {
1757 // PHPDocumentProvider cp = (PHPDocumentProvider) p;
1758 // cp.setSavePolicy(fSavePolicy);
1762 // super.performSaveOperation(operation, progressMonitor);
1764 // if (p instanceof PHPDocumentProvider) {
1765 // PHPDocumentProvider cp = (PHPDocumentProvider) p;
1766 // cp.setSavePolicy(null);
1772 * @see org.eclipse.ui.texteditor.AbstractTextEditor#performSave(boolean, org.eclipse.core.runtime.IProgressMonitor)
1774 protected void performSave(boolean overwrite, IProgressMonitor progressMonitor) {
1775 IDocumentProvider p= getDocumentProvider();
1776 if (p instanceof PHPDocumentProvider) {
1777 PHPDocumentProvider cp= (PHPDocumentProvider) p;
1778 cp.setSavePolicy(fSavePolicy);
1781 super.performSave(overwrite, progressMonitor);
1783 if (p instanceof PHPDocumentProvider) {
1784 PHPDocumentProvider cp= (PHPDocumentProvider) p;
1785 cp.setSavePolicy(null);
1790 * @see AbstractTextEditor#doSaveAs
1792 public void doSaveAs() {
1793 if (askIfNonWorkbenchEncodingIsOk()) {
1799 * @see AbstractTextEditor#doSave(IProgressMonitor)
1801 public void doSave(IProgressMonitor progressMonitor) {
1803 IDocumentProvider p = getDocumentProvider();
1805 // editor has been closed
1809 if (!askIfNonWorkbenchEncodingIsOk()) {
1810 progressMonitor.setCanceled(true);
1814 if (p.isDeleted(getEditorInput())) {
1816 if (isSaveAsAllowed()) {
1819 * 1GEUSSR: ITPUI:ALL - User should never loose changes made in the editors.
1820 * Changed Behavior to make sure that if called inside a regular save (because
1821 * of deletion of input element) there is a way to report back to the caller.
1823 // performSaveAs(progressMonitor);
1824 super.doSave(progressMonitor);
1828 * 1GF5YOX: ITPJUI:ALL - Save of delete file claims it's still there
1829 * Missing resources.
1831 Shell shell = getSite().getShell();
1832 MessageDialog.openError(shell, PHPEditorMessages.getString("PHPUnitEditor.error.saving.title1"), PHPEditorMessages.getString("PHPUnitEditor.error.saving.message1")); //$NON-NLS-1$ //$NON-NLS-2$
1837 setStatusLineErrorMessage(null);
1838 super.doSave(progressMonitor);
1840 // IWorkingCopyManager manager= JavaPlugin.getDefault().getWorkingCopyManager();
1841 // ICompilationUnit unit= manager.getWorkingCopy(getEditorInput());
1843 // if (unit != null) {
1844 // synchronized (unit) {
1845 // performSaveOperation(createSaveOperation(false), progressMonitor);
1848 // performSaveOperation(createSaveOperation(false), progressMonitor);
1852 * Asks the user if it is ok to store in non-workbench encoding.
1853 * @return <true> if the user wants to continue
1855 private boolean askIfNonWorkbenchEncodingIsOk() {
1856 IDocumentProvider provider = getDocumentProvider();
1857 if (provider instanceof IStorageDocumentProvider) {
1858 IEditorInput input = getEditorInput();
1859 IStorageDocumentProvider storageProvider = (IStorageDocumentProvider) provider;
1860 String encoding = storageProvider.getEncoding(input);
1861 String defaultEncoding = storageProvider.getDefaultEncoding();
1862 if (encoding != null && !encoding.equals(defaultEncoding)) {
1863 Shell shell = getSite().getShell();
1864 String title = PHPEditorMessages.getString("PHPUnitEditor.warning.save.nonWorkbenchEncoding.title"); //$NON-NLS-1$
1867 msg = MessageFormat.format(PHPEditorMessages.getString("PHPUnitEditor.warning.save.nonWorkbenchEncoding.message1"), new String[] { input.getName(), encoding }); //$NON-NLS-1$
1869 msg = MessageFormat.format(PHPEditorMessages.getString("PHPUnitEditor.warning.save.nonWorkbenchEncoding.message2"), new String[] { encoding }); //$NON-NLS-1$
1870 return MessageDialog.openQuestion(shell, title, msg);
1876 * @see IReconcilingParticipant#reconciled()
1878 public void reconciled() {
1879 if (synchronizeOutlineOnCursorMove()) {
1880 Shell shell = getSite().getShell();
1881 if (shell != null && !shell.isDisposed()) {
1882 shell.getDisplay().asyncExec(new Runnable() {
1884 synchronizeOutlinePageSelection();
1891 private boolean synchronizeOutlineOnCursorMove() {
1892 return PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SYNC_OUTLINE_ON_CURSOR_MOVE);
1894 protected void updateStateDependentActions() {
1895 super.updateStateDependentActions();
1896 fGenerateActionGroup.editorStateChanged();
1900 * Returns the updated java element for the old java element.
1902 private IJavaElement findElement(IJavaElement element) {
1904 if (element == null)
1907 IWorkingCopyManager manager = PHPeclipsePlugin.getDefault().getWorkingCopyManager();
1908 ICompilationUnit unit = manager.getWorkingCopy(getEditorInput());
1913 synchronized (unit) {
1916 IJavaElement[] findings = unit.findElements(element);
1917 if (findings != null && findings.length > 0)
1920 } catch (JavaModelException x) {
1921 PHPeclipsePlugin.log(x.getStatus());
1922 // nothing found, be tolerant and go on
1930 * Returns the offset of the given Java element.
1932 private int getOffset(IJavaElement element) {
1933 if (element instanceof ISourceReference) {
1934 ISourceReference sr = (ISourceReference) element;
1936 ISourceRange srcRange = sr.getSourceRange();
1937 if (srcRange != null)
1938 return srcRange.getOffset();
1939 } catch (JavaModelException e) {
1946 * @see AbstractTextEditor#rememberSelection()
1948 protected void rememberSelection() {
1949 ISelectionProvider sp = getSelectionProvider();
1950 fRememberedSelection = (sp == null ? null : (ITextSelection) sp.getSelection());
1951 if (fRememberedSelection != null) {
1952 fRememberedElement = getElementAt(fRememberedSelection.getOffset(), true);
1953 fRememberedElementOffset = getOffset(fRememberedElement);
1958 * @see AbstractTextEditor#restoreSelection()
1960 protected void restoreSelection() {
1964 if (getSourceViewer() == null || fRememberedSelection == null)
1967 IJavaElement newElement = findElement(fRememberedElement);
1968 int newOffset = getOffset(newElement);
1969 int delta = (newOffset > -1 && fRememberedElementOffset > -1) ? newOffset - fRememberedElementOffset : 0;
1970 if (isValidSelection(delta + fRememberedSelection.getOffset(), fRememberedSelection.getLength()))
1971 selectAndReveal(delta + fRememberedSelection.getOffset(), fRememberedSelection.getLength());
1974 fRememberedSelection = null;
1975 fRememberedElement = null;
1976 fRememberedElementOffset = -1;