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