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