264938e91fae6e20c30ad589d26cf7bb36f000a8
[phpeclipse.git] /
1 /*
2  * (c) Copyright IBM Corp. 2000, 2001.
3  * All Rights Reserved.
4  */
5 package net.sourceforge.phpdt.internal.ui.text.java;
6
7 import net.sourceforge.phpdt.core.ICodeFormatter;
8 import net.sourceforge.phpdt.core.ToolFactory;
9 import net.sourceforge.phpdt.internal.corext.codemanipulation.StubUtility;
10 import net.sourceforge.phpdt.internal.corext.util.Strings;
11 import net.sourceforge.phpdt.internal.ui.preferences.CodeFormatterPreferencePage;
12
13 import org.eclipse.jface.text.IDocument;
14 import org.eclipse.jface.text.formatter.IFormattingStrategy;
15 import org.eclipse.jface.text.source.ISourceViewer;
16
17 public class JavaFormattingStrategy implements IFormattingStrategy {
18         
19         private String fInitialIndentation;
20         private ISourceViewer fViewer;  
21
22         public JavaFormattingStrategy(ISourceViewer viewer) {
23                 fViewer = viewer;
24         }
25         
26         /**
27          * @see IFormattingStrategy#formatterStarts(String)
28          */
29         public void formatterStarts(String initialIndentation) {
30                 fInitialIndentation= initialIndentation;
31         }
32         
33         /**
34          * @see IFormattingStrategy#formatterStops()
35          */
36         public void formatterStops() {
37         }
38         
39         /**
40          * @see IFormattingStrategy#format(String, boolean, String, int[])
41          */
42         public String format(String content, boolean isLineStart, String indentation, int[] positions) {
43                 ICodeFormatter formatter= ToolFactory.createCodeFormatter();
44                 
45                 IDocument doc= fViewer.getDocument();
46                 String lineDelimiter= StubUtility.getLineDelimiterFor(doc);
47
48                 int indent= 0;
49                 if (fInitialIndentation != null) {
50                         indent= Strings.computeIndent(fInitialIndentation, CodeFormatterPreferencePage.getTabSize());
51                 }
52                 return formatter.format(content, indent, positions, lineDelimiter);
53         }       
54 }