e2a9d52386e4b8a37e1ccce156df60eafe037ded
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / java / JavaFormattingStrategy.java
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 org.eclipse.jface.text.IDocument;
8 import org.eclipse.jface.text.formatter.IFormattingStrategy;
9 import org.eclipse.jface.text.source.ISourceViewer;
10
11 import net.sourceforge.phpdt.core.ICodeFormatter;
12 import net.sourceforge.phpdt.core.ToolFactory;
13
14 import net.sourceforge.phpdt.internal.corext.codemanipulation.StubUtility;
15 import net.sourceforge.phpdt.internal.corext.util.Strings;
16 import net.sourceforge.phpdt.internal.ui.preferences.CodeFormatterPreferencePage; 
17
18 public class JavaFormattingStrategy implements IFormattingStrategy {
19         
20         private String fInitialIndentation;
21         private ISourceViewer fViewer;  
22
23         public JavaFormattingStrategy(ISourceViewer viewer) {
24                 fViewer = viewer;
25         }
26         
27         /**
28          * @see IFormattingStrategy#formatterStarts(String)
29          */
30         public void formatterStarts(String initialIndentation) {
31                 fInitialIndentation= initialIndentation;
32         }
33         
34         /**
35          * @see IFormattingStrategy#formatterStops()
36          */
37         public void formatterStops() {
38         }
39         
40         /**
41          * @see IFormattingStrategy#format(String, boolean, String, int[])
42          */
43         public String format(String content, boolean isLineStart, 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, CodeFormatterPreferencePage.getTabSize());
52                 }
53                 return formatter.format(content, indent, positions, lineDelimiter);
54         }       
55 }