A massive organize imports and formatting of the sources using default Eclipse code...
[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.formatter.newline.openingBrace"; //$NON-NLS-1$
21
22         public static final String OPTION_InsertNewlineInControlStatement = "net.sourceforge.phpeclipse.formatter.newline.controlStatement"; //$NON-NLS-1$
23
24         // public static final String OPTION_InsertNewLineBetweenElseAndIf =
25         // "net.sourceforge.phpeclipse.formatter.newline.elseIf"; //$NON-NLS-1$
26         public static final String OPTION_InsertNewLineInEmptyBlock = "net.sourceforge.phpeclipse.formatter.newline.emptyBlock"; //$NON-NLS-1$
27
28         public static final String OPTION_ClearAllBlankLines = "net.sourceforge.phpeclipse.formatter.newline.clearAll"; //$NON-NLS-1$
29
30         public static final String OPTION_SplitLineExceedingLength = "net.sourceforge.phpeclipse.formatter.lineSplit"; //$NON-NLS-1$
31
32         public static final String OPTION_CompactAssignment = "net.sourceforge.phpeclipse.formatter.style.assignment"; //$NON-NLS-1$
33
34         public static final String OPTION_TabulationChar = "net.sourceforge.phpeclipse.formatter.tabulation.char"; //$NON-NLS-1$
35
36         public static final String OPTION_TabulationSize = "net.sourceforge.phpeclipse.formatter.tabulation.size"; //$NON-NLS-1$
37
38         public static final String OPTION_CompactDereferencing = "net.sourceforge.phpeclipse.formatter.style.assignment";
39
40         // TODO: add the checkbox in the preferences panel ; load/save
41
42         public static final String INSERT = "insert"; //$NON-NLS-1$
43
44         public static final String DO_NOT_INSERT = "do not insert"; //$NON-NLS-1$
45
46         public static final String PRESERVE_ONE = "preserve one"; //$NON-NLS-1$
47
48         public static final String CLEAR_ALL = "clear all"; //$NON-NLS-1$
49
50         public static final String NORMAL = "normal"; //$NON-NLS-1$
51
52         public static final String COMPACT = "compact"; //$NON-NLS-1$
53
54         public static final String TAB = "tab"; //$NON-NLS-1$
55
56         public static final String SPACE = "space"; //$NON-NLS-1$
57
58         // by default, do not insert blank line before opening brace
59         public boolean newLineBeforeOpeningBraceMode = false;
60
61         // by default, do not insert blank line behind keywords (ELSE, CATCH,
62         // FINALLY,...) in control statements
63         public boolean newlineInControlStatementMode = false;
64
65         // by default, preserve one blank line per sequence of blank lines
66         public boolean clearAllBlankLinesMode = false;
67
68         // line splitting will occur when line exceeds this length
69         public int maxLineLength = 80;
70
71         public boolean compactAssignmentMode = false;
72
73         // if isTrue, assignments look like x= 12 (not like x = 12);
74         public boolean compactDereferencingMode = true;
75
76         // if isTrue, dereferencing look like $obj->method (not like $obj ->
77         // method);
78
79         // number of consecutive spaces used to replace the tab char
80         public int tabSize = 4; // n spaces for one tab
81
82         public boolean indentWithTab = true;
83
84         // public boolean compactElseIfMode = true;
85         // if true, else and if are kept on the same line.
86         public boolean newLineInEmptyBlockMode = true;
87
88         // if false, no new line in {} if it's empty.
89
90         public char[] lineSeparatorSequence = System
91                         .getProperty("line.separator").toCharArray(); //$NON-NLS-1$
92
93         /**
94          * Initializing the formatter options with default settings
95          */
96         public FormatterOptions() {
97         }
98
99         /**
100          * Initializing the formatter options with external settings
101          */
102         public FormatterOptions(Map settings) {
103                 if (settings == null)
104                         return;
105
106                 // filter options which are related to the assist component
107                 Object[] entries = settings.entrySet().toArray();
108                 for (int i = 0, max = entries.length; i < max; i++) {
109                         Map.Entry entry = (Map.Entry) entries[i];
110                         if (!(entry.getKey() instanceof String))
111                                 continue;
112                         if (!(entry.getValue() instanceof String))
113                                 continue;
114                         String optionID = (String) entry.getKey();
115                         String optionValue = (String) entry.getValue();
116
117                         if (optionID.equals(OPTION_InsertNewlineBeforeOpeningBrace)) {
118                                 if (optionValue.equals(INSERT)) {
119                                         this.newLineBeforeOpeningBraceMode = true;
120                                 } else if (optionValue.equals(DO_NOT_INSERT)) {
121                                         this.newLineBeforeOpeningBraceMode = false;
122                                 }
123                                 continue;
124                         }
125                         if (optionID.equals(OPTION_InsertNewlineInControlStatement)) {
126                                 if (optionValue.equals(INSERT)) {
127                                         this.newlineInControlStatementMode = true;
128                                 } else if (optionValue.equals(DO_NOT_INSERT)) {
129                                         this.newlineInControlStatementMode = false;
130                                 }
131                                 continue;
132                         }
133                         if (optionID.equals(OPTION_ClearAllBlankLines)) {
134                                 if (optionValue.equals(CLEAR_ALL)) {
135                                         this.clearAllBlankLinesMode = true;
136                                 } else if (optionValue.equals(PRESERVE_ONE)) {
137                                         this.clearAllBlankLinesMode = false;
138                                 }
139                                 continue;
140                         }
141                         // if (optionID.equals(OPTION_InsertNewLineBetweenElseAndIf)) {
142                         // if (optionValue.equals(INSERT)) {
143                         // this.compactElseIfMode = false;
144                         // } else if (optionValue.equals(DO_NOT_INSERT)) {
145                         // this.compactElseIfMode = true;
146                         // }
147                         // continue;
148                         // }
149                         if (optionID.equals(OPTION_InsertNewLineInEmptyBlock)) {
150                                 if (optionValue.equals(INSERT)) {
151                                         this.newLineInEmptyBlockMode = true;
152                                 } else if (optionValue.equals(DO_NOT_INSERT)) {
153                                         this.newLineInEmptyBlockMode = false;
154                                 }
155                                 continue;
156                         }
157                         if (optionID.equals(OPTION_SplitLineExceedingLength)) {
158                                 try {
159                                         int val = Integer.parseInt(optionValue);
160                                         if (val >= 0)
161                                                 this.maxLineLength = val;
162                                 } catch (NumberFormatException e) {
163                                 }
164                         }
165                         if (optionID.equals(OPTION_CompactAssignment)) {
166                                 if (optionValue.equals(COMPACT)) {
167                                         this.compactAssignmentMode = true;
168                                 } else if (optionValue.equals(NORMAL)) {
169                                         this.compactAssignmentMode = false;
170                                 }
171                                 continue;
172                         }
173                         if (optionID.equals(OPTION_TabulationChar)) {
174                                 if (optionValue.equals(TAB)) {
175                                         this.indentWithTab = true;
176                                 } else if (optionValue.equals(SPACE)) {
177                                         this.indentWithTab = false;
178                                 }
179                                 continue;
180                         }
181                         if (optionID.equals(OPTION_TabulationSize)) {
182                                 try {
183                                         int val = Integer.parseInt(optionValue);
184                                         if (val > 0)
185                                                 this.tabSize = val;
186                                 } catch (NumberFormatException e) {
187                                 }
188                         }
189                 }
190         }
191
192         /**
193          * 
194          * @return int
195          */
196         public int getMaxLineLength() {
197                 return maxLineLength;
198         }
199
200         public int getTabSize() {
201                 return tabSize;
202         }
203
204         public boolean isAddingNewLineBeforeOpeningBrace() {
205                 return newLineBeforeOpeningBraceMode;
206         }
207
208         public boolean isAddingNewLineInControlStatement() {
209                 return newlineInControlStatementMode;
210         }
211
212         public boolean isAddingNewLineInEmptyBlock() {
213                 return newLineInEmptyBlockMode;
214         }
215
216         public boolean isClearingAllBlankLines() {
217                 return clearAllBlankLinesMode;
218         }
219
220         public boolean isCompactingAssignment() {
221                 return compactAssignmentMode;
222         }
223
224         public boolean isCompactingDereferencing() {
225                 return compactDereferencingMode;
226         }
227
228         // public boolean isCompactingElseIf() {
229         // return compactElseIfMode;
230         // }
231         public boolean isUsingTabForIndenting() {
232                 return indentWithTab;
233         }
234
235         public void setLineSeparator(String lineSeparator) {
236                 lineSeparatorSequence = lineSeparator.toCharArray();
237         }
238
239         /**
240          * @deprecated - should use a Map when creating the options.
241          */
242         public void setMaxLineLength(int maxLineLength) {
243                 this.maxLineLength = maxLineLength;
244         }
245         /**
246          * @deprecated - should use a Map when creating the options.
247          */
248         // public void setCompactElseIfMode(boolean flag) {
249         // compactElseIfMode = flag;
250         // }
251 }