improved code formatter
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / JavaSourceViewer.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11
12 package net.sourceforge.phpeclipse.phpeditor;
13
14
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;
29
30 //import net.sourceforge.phpdt.ui.text.JavaSourceViewerConfiguration;
31
32
33
34 public class JavaSourceViewer extends SourceViewer {
35
36         /**
37          * Text operation code for requesting the outline for the current input.
38          */
39         public static final int SHOW_OUTLINE= 51;
40
41         /**
42          * Text operation code for requesting the outline for the element at the current position.
43          */
44         public static final int OPEN_STRUCTURE= 52;
45
46
47         private IInformationPresenter fOutlinePresenter;
48         private IInformationPresenter fStructurePresenter;
49
50         public JavaSourceViewer(Composite parent, IVerticalRuler verticalRuler, IOverviewRuler overviewRuler, boolean showAnnotationsOverview, int styles) {
51                 super(parent, verticalRuler, overviewRuler, showAnnotationsOverview, styles);
52         }
53
54         /*
55          * @see ITextOperationTarget#doOperation(int)
56          */
57         public void doOperation(int operation) {
58                 if (getTextWidget() == null)
59                         return;
60
61                 switch (operation) {
62                         case SHOW_OUTLINE:
63                                 fOutlinePresenter.showInformation();
64                                 return;
65                         case OPEN_STRUCTURE:
66                                 fStructurePresenter.showInformation();
67                                 return;
68                         case FORMAT :
69                         {
70                                 final Point selection= rememberSelection();
71
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());
76                                 }
77                                 final IRewriteTarget target= getRewriteTarget();
78                                 final IFormattingContext context= createFormattingContext();
79
80                                 if (selection.y == 0) {
81                                         context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.TRUE);
82                                 } else {
83                                         context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.FALSE);
84                                         context.setProperty(FormattingContextProperties.CONTEXT_REGION, region);
85                                 }
86                                 try {
87                                         setRedraw(false);
88                                         startSequentialRewriteMode(false);
89                                         target.beginCompoundChange();
90
91                                         final String rememberedContents= document.get();
92                                         
93                                         try {
94                                                 
95                                                 if (fContentFormatter instanceof IContentFormatterExtension2) {
96                                                         
97                                                         final IContentFormatterExtension2 extension= (IContentFormatterExtension2)fContentFormatter;
98                                                         extension.format(document, context);
99                                                         
100                                                 } else
101                                                         fContentFormatter.format(document, region);
102                                                 
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);
108                                                 throw x;
109                                         }
110                                         
111                                 } finally {
112
113                                         target.endCompoundChange();
114                                         stopSequentialRewriteMode();
115                                         setRedraw(true);
116
117                                         restoreSelection();
118                                         context.dispose();
119                                 }
120                                 return;
121                         }
122                 }
123                 
124                 super.doOperation(operation);
125         }
126
127         /*
128          * @see ITextOperationTarget#canDoOperation(int)
129          */
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);
136         }
137
138         /*
139          * @see ISourceViewer#configure(SourceViewerConfiguration)
140          */
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);
146                 }
147                 if (configuration instanceof PHPSourceViewerConfiguration) {
148                         fStructurePresenter= ((PHPSourceViewerConfiguration)configuration).getOutlinePresenter(this, true);
149                         fStructurePresenter.install(this);
150                 }
151         }
152
153         /*
154          * @see TextViewer#handleDispose()
155          */
156         protected void handleDispose() {
157                 if (fOutlinePresenter != null) {
158                         fOutlinePresenter.uninstall();  
159                         fOutlinePresenter= null;
160                 }
161                 if (fStructurePresenter != null) {
162                         fStructurePresenter.uninstall();
163                         fStructurePresenter= null;
164                 }
165                 super.handleDispose();
166         }
167 }