0fd345df5a37058bfe99f955b706ae329fbe61f4
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / text / java / JavaFormattingStrategy.java
1 /*
2  * (c) Copyright IBM Corp. 2000, 2001. All Rights Reserved.
3  */
4 package net.sourceforge.phpdt.internal.ui.text.java;
5
6 import net.sourceforge.phpdt.core.ICodeFormatter;
7 import net.sourceforge.phpdt.core.ToolFactory;
8 import net.sourceforge.phpdt.internal.corext.codemanipulation.StubUtility;
9 import net.sourceforge.phpdt.internal.corext.util.Strings;
10 import net.sourceforge.phpdt.internal.ui.preferences.CodeFormatterPreferencePage;
11
12 import org.eclipse.jface.text.IDocument;
13 import org.eclipse.jface.text.formatter.IFormattingStrategy;
14 import org.eclipse.jface.text.source.ISourceViewer;
15
16 public class JavaFormattingStrategy implements IFormattingStrategy {
17
18         private String fInitialIndentation;
19
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,
43                         String indentation, int[] positions) {
44                 ICodeFormatter formatter = ToolFactory.createCodeFormatter();
45
46                 IDocument doc = fViewer.getDocument();
47                 String lineDelimiter = StubUtility.getLineDelimiterFor(doc);
48
49                 int indent = 0;
50                 if (fInitialIndentation != null) {
51                         indent = Strings.computeIndent(fInitialIndentation,
52                                         CodeFormatterPreferencePage.getTabSize());
53                 }
54
55                 return formatter.format(content, indent, positions, lineDelimiter);
56         }
57 }