Codeformatter preferences should work now
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / formatter / impl / FormatterOptions.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v0.5 
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v05.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  ******************************************************************************/
11 package net.sourceforge.phpdt.internal.formatter.impl;
12
13 import java.util.Map;
14
15 public class FormatterOptions {
16
17   /**
18    * Option IDs
19    */
20   public static final String OPTION_InsertNewlineBeforeOpeningBrace = "net.sourceforge.phpeclipse.core.formatter.newline.openingBrace"; //$NON-NLS-1$
21   public static final String OPTION_InsertNewlineInControlStatement = "net.sourceforge.phpeclipse.core.formatter.newline.controlStatement"; //$NON-NLS-1$
22  // public static final String OPTION_InsertNewLineBetweenElseAndIf = "net.sourceforge.phpeclipse.core.formatter.newline.elseIf"; //$NON-NLS-1$
23   public static final String OPTION_InsertNewLineInEmptyBlock = "net.sourceforge.phpeclipse.core.formatter.newline.emptyBlock"; //$NON-NLS-1$
24   public static final String OPTION_ClearAllBlankLines = "net.sourceforge.phpeclipse.core.formatter.newline.clearAll"; //$NON-NLS-1$
25   public static final String OPTION_SplitLineExceedingLength = "net.sourceforge.phpeclipse.core.formatter.lineSplit"; //$NON-NLS-1$
26   public static final String OPTION_CompactAssignment = "net.sourceforge.phpeclipse.core.formatter.style.assignment"; //$NON-NLS-1$
27   public static final String OPTION_TabulationChar = "net.sourceforge.phpeclipse.core.formatter.tabulation.char"; //$NON-NLS-1$
28   public static final String OPTION_TabulationSize = "net.sourceforge.phpeclipse.core.formatter.tabulation.size"; //$NON-NLS-1$
29
30   public static final String INSERT = "insert"; //$NON-NLS-1$
31   public static final String DO_NOT_INSERT = "do not insert"; //$NON-NLS-1$
32   public static final String PRESERVE_ONE = "preserve one"; //$NON-NLS-1$
33   public static final String CLEAR_ALL = "clear all"; //$NON-NLS-1$
34   public static final String NORMAL = "normal"; //$NON-NLS-1$
35   public static final String COMPACT = "compact"; //$NON-NLS-1$
36   public static final String TAB = "tab"; //$NON-NLS-1$
37   public static final String SPACE = "space"; //$NON-NLS-1$
38
39   // by default, do not insert blank line before opening brace
40   public boolean newLineBeforeOpeningBraceMode = false;
41
42   // by default, do not insert blank line behind keywords (ELSE, CATCH, FINALLY,...) in control statements
43   public boolean newlineInControlStatementMode = false;
44
45   // by default, preserve one blank line per sequence of blank lines
46   public boolean clearAllBlankLinesMode = false;
47
48   // line splitting will occur when line exceeds this length
49   public int maxLineLength = 80;
50
51   public boolean compactAssignmentMode = false;
52   // if isTrue, assignments look like x= 12 (not like x = 12);
53
54   //number of consecutive spaces used to replace the tab char
55   public int tabSize = 4; // n spaces for one tab
56   public boolean indentWithTab = true;
57
58   //public boolean compactElseIfMode = true;
59   // if true, else and if are kept on the same line.
60   public boolean newLineInEmptyBlockMode = true;
61   // if false, no new line in {} if it's empty.
62
63   public char[] lineSeparatorSequence = System.getProperty("line.separator").toCharArray(); //$NON-NLS-1$
64   /** 
65    * Initializing the formatter options with default settings
66    */
67   public FormatterOptions() {
68   }
69   /** 
70    * Initializing the formatter options with external settings
71    */
72   public FormatterOptions(Map settings) {
73     if (settings == null)
74       return;
75
76     // filter options which are related to the assist component
77     Object[] entries = settings.entrySet().toArray();
78     for (int i = 0, max = entries.length; i < max; i++) {
79       Map.Entry entry = (Map.Entry) entries[i];
80       if (!(entry.getKey() instanceof String))
81         continue;
82       if (!(entry.getValue() instanceof String))
83         continue;
84       String optionID = (String) entry.getKey();
85       String optionValue = (String) entry.getValue();
86
87       if (optionID.equals(OPTION_InsertNewlineBeforeOpeningBrace)) {
88         if (optionValue.equals(INSERT)) {
89           this.newLineBeforeOpeningBraceMode = true;
90         } else if (optionValue.equals(DO_NOT_INSERT)) {
91           this.newLineBeforeOpeningBraceMode = false;
92         }
93         continue;
94       }
95       if (optionID.equals(OPTION_InsertNewlineInControlStatement)) {
96         if (optionValue.equals(INSERT)) {
97           this.newlineInControlStatementMode = true;
98         } else if (optionValue.equals(DO_NOT_INSERT)) {
99           this.newlineInControlStatementMode = false;
100         }
101         continue;
102       }
103       if (optionID.equals(OPTION_ClearAllBlankLines)) {
104         if (optionValue.equals(CLEAR_ALL)) {
105           this.clearAllBlankLinesMode = true;
106         } else if (optionValue.equals(PRESERVE_ONE)) {
107           this.clearAllBlankLinesMode = false;
108         }
109         continue;
110       }
111 //      if (optionID.equals(OPTION_InsertNewLineBetweenElseAndIf)) {
112 //        if (optionValue.equals(INSERT)) {
113 //          this.compactElseIfMode = false;
114 //        } else if (optionValue.equals(DO_NOT_INSERT)) {
115 //          this.compactElseIfMode = true;
116 //        }
117 //        continue;
118 //      }
119       if (optionID.equals(OPTION_InsertNewLineInEmptyBlock)) {
120         if (optionValue.equals(INSERT)) {
121           this.newLineInEmptyBlockMode = true;
122         } else if (optionValue.equals(DO_NOT_INSERT)) {
123           this.newLineInEmptyBlockMode = false;
124         }
125         continue;
126       }
127       if (optionID.equals(OPTION_SplitLineExceedingLength)) {
128         try {
129           int val = Integer.parseInt(optionValue);
130           if (val >= 0)
131             this.maxLineLength = val;
132         } catch (NumberFormatException e) {
133         }
134       }
135       if (optionID.equals(OPTION_CompactAssignment)) {
136         if (optionValue.equals(COMPACT)) {
137           this.compactAssignmentMode = true;
138         } else if (optionValue.equals(NORMAL)) {
139           this.compactAssignmentMode = false;
140         }
141         continue;
142       }
143       if (optionID.equals(OPTION_TabulationChar)) {
144         if (optionValue.equals(TAB)) {
145           this.indentWithTab = true;
146         } else if (optionValue.equals(SPACE)) {
147           this.indentWithTab = false;
148         }
149         continue;
150       }
151       if (optionID.equals(OPTION_TabulationSize)) {
152         try {
153           int val = Integer.parseInt(optionValue);
154           if (val > 0)
155             this.tabSize = val;
156         } catch (NumberFormatException e) {
157         }
158       }
159     }
160   }
161
162   /**
163    * 
164    * @return int
165    */
166   public int getMaxLineLength() {
167     return maxLineLength;
168   }
169   public int getTabSize() {
170     return tabSize;
171   }
172   public boolean isAddingNewLineBeforeOpeningBrace() {
173     return newLineBeforeOpeningBraceMode;
174   }
175   public boolean isAddingNewLineInControlStatement() {
176     return newlineInControlStatementMode;
177   }
178   public boolean isAddingNewLineInEmptyBlock() {
179     return newLineInEmptyBlockMode;
180   }
181   public boolean isClearingAllBlankLines() {
182     return clearAllBlankLinesMode;
183   }
184   public boolean isCompactingAssignment() {
185     return compactAssignmentMode;
186   }
187 //  public boolean isCompactingElseIf() {
188 //    return compactElseIfMode;
189 //  }
190   public boolean isUsingTabForIndenting() {
191     return indentWithTab;
192   }
193   public void setLineSeparator(String lineSeparator) {
194     lineSeparatorSequence = lineSeparator.toCharArray();
195   }
196   /**
197    * @deprecated - should use a Map when creating the options.
198    */
199   public void setMaxLineLength(int maxLineLength) {
200     this.maxLineLength = maxLineLength;
201   }
202   /**
203    * @deprecated - should use a Map when creating the options.
204    */
205 //  public void setCompactElseIfMode(boolean flag) {
206 //    compactElseIfMode = flag;
207 //  }
208
209 }