1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package net.sourceforge.phpeclipse.phpeditor;
15 import org.eclipse.jface.text.IDocument;
16 import org.eclipse.jface.text.IRegion;
17 import org.eclipse.jface.text.IRewriteTarget;
18 import org.eclipse.jface.text.Region;
19 import org.eclipse.jface.text.formatter.FormattingContextProperties;
20 import org.eclipse.jface.text.formatter.IContentFormatterExtension2;
21 import org.eclipse.jface.text.formatter.IFormattingContext;
22 import org.eclipse.jface.text.information.IInformationPresenter;
23 import org.eclipse.jface.text.source.IOverviewRuler;
24 import org.eclipse.jface.text.source.IVerticalRuler;
25 import org.eclipse.jface.text.source.SourceViewer;
26 import org.eclipse.jface.text.source.SourceViewerConfiguration;
27 import org.eclipse.swt.graphics.Point;
28 import org.eclipse.swt.widgets.Composite;
30 //import net.sourceforge.phpdt.ui.text.JavaSourceViewerConfiguration;
34 public class JavaSourceViewer extends SourceViewer {
37 * Text operation code for requesting the outline for the current input.
39 public static final int SHOW_OUTLINE= 51;
42 * Text operation code for requesting the outline for the element at the current position.
44 public static final int OPEN_STRUCTURE= 52;
47 private IInformationPresenter fOutlinePresenter;
48 private IInformationPresenter fStructurePresenter;
50 public JavaSourceViewer(Composite parent, IVerticalRuler verticalRuler, IOverviewRuler overviewRuler, boolean showAnnotationsOverview, int styles) {
51 super(parent, verticalRuler, overviewRuler, showAnnotationsOverview, styles);
55 * @see ITextOperationTarget#doOperation(int)
57 public void doOperation(int operation) {
58 if (getTextWidget() == null)
63 fOutlinePresenter.showInformation();
66 fStructurePresenter.showInformation();
70 final Point selection= rememberSelection();
72 final IDocument document= getDocument();
73 IRegion region= new Region(selection.x, selection.y);
74 if (region.getLength()==0) {
75 region = new Region(0, document.getLength());
77 final IRewriteTarget target= getRewriteTarget();
78 final IFormattingContext context= createFormattingContext();
80 if (selection.y == 0) {
81 context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.TRUE);
83 context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.FALSE);
84 context.setProperty(FormattingContextProperties.CONTEXT_REGION, region);
88 startSequentialRewriteMode(false);
89 target.beginCompoundChange();
91 final String rememberedContents= document.get();
95 if (fContentFormatter instanceof IContentFormatterExtension2) {
97 final IContentFormatterExtension2 extension= (IContentFormatterExtension2)fContentFormatter;
98 extension.format(document, context);
101 fContentFormatter.format(document, region);
103 } catch (RuntimeException x) {
104 // firewall for https://bugs.eclipse.org/bugs/show_bug.cgi?id=47472
105 // if something went wrong we undo the changes we just did
106 // TODO to be removed
107 document.set(rememberedContents);
113 target.endCompoundChange();
114 stopSequentialRewriteMode();
124 super.doOperation(operation);
128 * @see ITextOperationTarget#canDoOperation(int)
130 public boolean canDoOperation(int operation) {
131 if (operation == SHOW_OUTLINE)
132 return fOutlinePresenter != null;
133 if (operation == OPEN_STRUCTURE)
134 return fStructurePresenter != null;
135 return super.canDoOperation(operation);
139 * @see ISourceViewer#configure(SourceViewerConfiguration)
141 public void configure(SourceViewerConfiguration configuration) {
142 super.configure(configuration);
143 if (configuration instanceof PHPSourceViewerConfiguration) {
144 fOutlinePresenter= ((PHPSourceViewerConfiguration)configuration).getOutlinePresenter(this, false);
145 fOutlinePresenter.install(this);
147 if (configuration instanceof PHPSourceViewerConfiguration) {
148 fStructurePresenter= ((PHPSourceViewerConfiguration)configuration).getOutlinePresenter(this, true);
149 fStructurePresenter.install(this);
154 * @see TextViewer#handleDispose()
156 protected void handleDispose() {
157 if (fOutlinePresenter != null) {
158 fOutlinePresenter.uninstall();
159 fOutlinePresenter= null;
161 if (fStructurePresenter != null) {
162 fStructurePresenter.uninstall();
163 fStructurePresenter= null;
165 super.handleDispose();