1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sourceforge.jivalo.editor;
17
18 import java.io.InvalidClassException;
19 import java.io.InvalidObjectException;
20 import java.nio.charset.Charset;
21 import java.util.Properties;
22
23 import net.sourceforge.jivalo.editor.key.KeyBindings;
24 import net.sourceforge.jivalo.editor.syntax.SyntaxHighlightings;
25
26 /**
27 * jIvaloEditor preferences properties used to configure editor behavior.
28 * @author <a href="mailto:ivalo@iki.fi">Markku Saarela</a>
29 *
30 */
31 public final class JIvaloEditorProperties
32 {
33
34 /**
35 * Current file version of <code>jivalo.editor.properties</code> file.
36 */
37 private static final int CURRENT_FILE_VERSION = 1;
38
39 /**
40 * Minimum file version.
41 */
42 private static final int MIN_FILE_VERSION = 0;
43
44 private static final String PROPERTIES_FILE_VERSION =
45 "jivalo.preferences.version";
46
47 private static final String CHARACTER_ENCODING_TYPE =
48 "character.encoding.type";
49
50 private static final String SYNTAX_HIGHLIGHTING =
51 "syntax.highlighting";
52
53 private static final String SYNTAX_HIGHLIGHTING_DEFAULT =
54 "syntax.highlighting.default";
55
56 private static final String TAB_STYLE =
57 "tab.style";
58
59 private static final String TAB_STYLE_SPACES_INDENTATION_SIZE =
60 "tab.style.spaces.indentation.size";
61
62 private static final String KEY_BINDING_FILE_LIST =
63 "key.binding.file.list";
64
65 private static final String CURRENT_KEY_BINDING_FILE_NAME =
66 "key.binding.file.current.name";
67
68
69 private static final String DEFAULT_CHARACTER_ENCODING_TYPE = "";
70
71 private static final String DEFAULT_SYNTAX_HIGHLIGHTING =
72 "java=/java.highlight";
73
74 private static final String DEFAULT_SYNTAX_HIGHLIGHTING_DEFAULT = "java";
75
76 private static final String DEFAULT_TAB_STYLE = "spaces";
77
78 private static final int DEFAULT_TAB_STYLE_SPACES_INDENTATION_SIZE = 4;
79
80 private int fileVersion;
81
82 private Charset encodingCharset;
83
84 private SyntaxHighlightings syntaxHighlightings;
85
86 private String syntaxHighlightingDefault;
87
88 private TabStyle tabStyle;
89
90 private int spacesIndentation;
91
92 private KeyBindings keyBindings;
93
94 private String currenKeyBindingName;
95
96 /**
97 *
98 */
99 private boolean upgradePropertiesFile = false;
100
101 private String spacesIndentationString;
102
103 /**
104 * Constructor for JIvaloEditorProperties.
105 * @param props jIvalo Editor properties
106 * @throws NullPointerException If props is null.
107 * @throws InvalidClassException
108 * if properties.file.version property is missing or invalid.
109 * @throws InvalidObjectException If properties.file.version value is invalid.
110 * @throws NumberFormatException If properties.file.version is not integer.
111 */
112 public JIvaloEditorProperties( Properties props )
113 throws InvalidClassException, InvalidObjectException
114 {
115 super();
116
117 if ( props == null )
118 {
119 throw new NullPointerException( "props" );
120 }
121
122 if ( props.containsKey( PROPERTIES_FILE_VERSION ) )
123 {
124 this.fileVersion = Integer.parseInt(
125 props.getProperty( PROPERTIES_FILE_VERSION ).trim() );
126 }
127 else
128 {
129 throw new InvalidClassException( "Property "
130 + PROPERTIES_FILE_VERSION + " is missing in properties: "
131 + props );
132 }
133
134 if ( this.fileVersion < MIN_FILE_VERSION
135 && this.fileVersion > CURRENT_FILE_VERSION )
136 {
137 throw new InvalidObjectException( "Editor Properties file version "
138 + this.fileVersion
139 + " is invalid. Should be in range of 1 to "
140 + CURRENT_FILE_VERSION
141 + "." );
142 }
143 else if ( this.fileVersion < CURRENT_FILE_VERSION )
144 {
145 this.upgradePropertiesFile = true;
146 }
147
148 if ( props.containsKey( CHARACTER_ENCODING_TYPE ) )
149 {
150 String encoding = props.getProperty(
151 CHARACTER_ENCODING_TYPE ).trim();
152
153 if ( encoding != null && encoding.length() > 1)
154 {
155 this.encodingCharset = Charset.forName( encoding );
156 }
157 }
158
159 if ( props.containsKey( SYNTAX_HIGHLIGHTING ) )
160 {
161 String highLightning =
162 props.getProperty( SYNTAX_HIGHLIGHTING ).trim();
163
164 if ( highLightning != null && highLightning.length() > 1)
165 {
166 this.syntaxHighlightings =
167 new SyntaxHighlightings( highLightning );
168 }
169 }
170
171 if ( props.containsKey( SYNTAX_HIGHLIGHTING_DEFAULT ) )
172 {
173 String highLightning =
174 props.getProperty( SYNTAX_HIGHLIGHTING_DEFAULT ).trim();
175
176 if ( highLightning != null && highLightning.length() > 1)
177 {
178 this.syntaxHighlightingDefault = highLightning;
179 }
180 }
181
182 if ( props.containsKey( TAB_STYLE ) )
183 {
184 String tbStyle =
185 props.getProperty( TAB_STYLE ).trim();
186
187 if ( tbStyle != null && tbStyle.length() > 1)
188 {
189 this.tabStyle = TabStyle.valueOf( tbStyle );
190 }
191 }
192 else
193 {
194 this.tabStyle = TabStyle.TABS;
195 }
196
197 if ( props.containsKey( TAB_STYLE_SPACES_INDENTATION_SIZE ) )
198 {
199 this.spacesIndentation = Integer.parseInt(
200 props.getProperty( TAB_STYLE_SPACES_INDENTATION_SIZE ).trim() );
201 }
202 else
203 {
204 this.spacesIndentation = DEFAULT_TAB_STYLE_SPACES_INDENTATION_SIZE;
205 }
206
207 if ( props.containsKey( KEY_BINDING_FILE_LIST ) )
208 {
209 String keys = props.getProperty( KEY_BINDING_FILE_LIST ).trim();
210
211 if ( keys != null && keys.length() > 1)
212 {
213 this.keyBindings = new KeyBindings( keys );
214 }
215 }
216
217 if ( props.containsKey( CURRENT_KEY_BINDING_FILE_NAME ) )
218 {
219 String currenKeyBindingName =
220 props.getProperty( CURRENT_KEY_BINDING_FILE_NAME ).trim();
221
222 if ( currenKeyBindingName != null && currenKeyBindingName.length() > 1)
223 {
224 this.currenKeyBindingName = currenKeyBindingName;
225 }
226 }
227
228 }
229
230 /**
231 * Get's Charset defined.
232 * @return Charset defined otherwise null.
233 */
234 public Charset getEncodingCharset()
235 {
236 return this.encodingCharset;
237 }
238
239 public SyntaxHighlightings getSyntaxHighlightings()
240 {
241 return this.syntaxHighlightings;
242 }
243
244 public String getSyntaxHighlightingDefault()
245 {
246 return this.syntaxHighlightingDefault;
247 }
248
249 public String getSpacesIndentation()
250 {
251 if ( this.spacesIndentationString == null )
252 {
253 char[] spaces = new char[this.spacesIndentation];
254
255 for ( int i = 0; i < spaces.length; i++ )
256 {
257 spaces[i] = ' ';
258 }
259
260 this.spacesIndentationString = new String( spaces );
261
262 }
263 return this.spacesIndentationString;
264 }
265
266 public TabStyle getTabStyle()
267 {
268 return tabStyle;
269 }
270
271 public KeyBindings getKeyBindings()
272 {
273 return this.keyBindings;
274 }
275
276 /**
277 * @return the currenKeyBindingName
278 */
279 public String getCurrenKeyBindingName()
280 {
281 return this.currenKeyBindingName;
282 }
283
284 public Properties asProperties()
285 {
286 Properties properties = new Properties();
287
288 properties.put( PROPERTIES_FILE_VERSION
289 , Integer.toString( CURRENT_FILE_VERSION ) );
290
291 properties.put(CHARACTER_ENCODING_TYPE,
292 ( this.encodingCharset == null
293 ? DEFAULT_CHARACTER_ENCODING_TYPE
294 : this.encodingCharset.name() ) );
295
296 properties.put( SYNTAX_HIGHLIGHTING,
297 ( this.syntaxHighlightings == null
298 ? DEFAULT_SYNTAX_HIGHLIGHTING
299 : this.syntaxHighlightings.toString() ) );
300
301 properties.put( SYNTAX_HIGHLIGHTING_DEFAULT,
302 ( this.syntaxHighlightingDefault == null
303 ? DEFAULT_SYNTAX_HIGHLIGHTING_DEFAULT
304 : this.syntaxHighlightingDefault ) );
305
306 properties.put( TAB_STYLE,
307 ( this.tabStyle == null
308 ? DEFAULT_TAB_STYLE.toString()
309 : this.tabStyle.toString() ) );
310
311 properties.put( TAB_STYLE_SPACES_INDENTATION_SIZE,
312 Integer.toString( this.spacesIndentation ) );
313
314 properties.put( KEY_BINDING_FILE_LIST,
315 ( this.keyBindings == null
316 ? ""
317 : this.keyBindings.toString() ) );
318
319 properties.put( CURRENT_KEY_BINDING_FILE_NAME,
320 ( this.currenKeyBindingName == null
321 ? ""
322 : this.currenKeyBindingName ) );
323
324 return properties;
325 }
326
327 /**
328 * Get's comment string for properties file.
329 * @return comment string for properties file.
330 */
331 public String getPropertiesFileComment()
332 {
333
334 StringBuffer comments = new StringBuffer("\n");
335
336 comments.append( "# jIvalo Editor Preferences File structure:\n" );
337 comments.append( "#\n" );
338 comments.append( "# int value of the version of file format. Do not edit!\n" );
339 comments.append( "# jivalo.preferences.version=1\n" );
340 comments.append( "#\n" );
341 comments.append( "# Character encoding for saving files. ISO-8859-1 UTF-8 UTF-16 etc.\n" );
342 comments.append( "# If empty no encoding occurs.\n" );
343 comments.append( "# character.encoding.type=UTF-8\n" );
344 comments.append( "#\n" );
345 comments.append( "# Comma separated list for syntax highlighting for file types.\n" );
346 comments.append( "# syntax.highlighting=java\\=/java.highlight,jsp\\=C:\\etc\\jsp.highlight\n" );
347 comments.append( "# Format: [fileSuffix\\=Fully qualified path to highlight file]\n" );
348 comments.append( "#\n" );
349 comments.append( "# Default syntax highlighting used by files if not specified.\n" );
350 comments.append( "# syntax.highlighting.default=java\n" );
351 comments.append( "#\n" );
352 comments.append( "# Tab style either spaces or tabs.\n" );
353 comments.append( "# tab.style=spaces\n" );
354 comments.append( "#\n" );
355 comments.append( "# Size of tab spaces indentation.\n" );
356 comments.append( "# tab.style.spaces.indentation.size=4\n" );
357 comments.append( "#\n" );
358 comments.append( "# Key Binding files containing specified key bindings. Comma separated list. Especially for emacs users ;-).\n" );
359 comments.append( "# Format: [name\\=Fully qualified path to key binding file]\n" );
360 comments.append( "# key.binding.file.list=emacs\\=C:\\etc\\emacs.keybindings\n" );
361 comments.append( "# key.binding.file.current.name=emacs\n" );
362 comments.append( "#" );
363
364 return comments.toString();
365 }
366
367 /**
368 * @return the upgradePropertiesFile
369 */
370 public boolean isUpgradePropertiesFile()
371 {
372 return this.upgradePropertiesFile;
373 }
374
375 /**
376 * Get's current default properties.
377 * @return default jIvaloEditor properties.
378 * @throws InvalidClassException
379 * @throws InvalidObjectException
380 */
381 public static JIvaloEditorProperties defaultProperties()
382 throws InvalidClassException, InvalidObjectException
383 {
384 Properties properties = new Properties();
385
386 properties.put( PROPERTIES_FILE_VERSION
387 , Integer.toString( CURRENT_FILE_VERSION ) );
388
389 properties.put(CHARACTER_ENCODING_TYPE
390 , DEFAULT_CHARACTER_ENCODING_TYPE );
391
392 properties.put( SYNTAX_HIGHLIGHTING
393 , DEFAULT_SYNTAX_HIGHLIGHTING );
394
395 properties.put( SYNTAX_HIGHLIGHTING_DEFAULT
396 , DEFAULT_SYNTAX_HIGHLIGHTING_DEFAULT );
397
398 properties.put( TAB_STYLE
399 , DEFAULT_TAB_STYLE );
400
401 properties.put( TAB_STYLE_SPACES_INDENTATION_SIZE
402 , Integer.toString( DEFAULT_TAB_STYLE_SPACES_INDENTATION_SIZE ) );
403
404 properties.put( KEY_BINDING_FILE_LIST
405 , "" );
406
407 return new JIvaloEditorProperties( properties );
408 }
409
410 }