SciNotes Prefs: choose the eol according to the OS
[scilab.git] / scilab / modules / scinotes / src / java / org / scilab / modules / scinotes / SciNotesOptions.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2012 - Scilab Enterprises -Calixte DENIZET
4  *
5  * This file must be used under the terms of the CeCILL.
6  * This source file is licensed as described in the file COPYING, which
7  * you should have received as part of this distribution.  The terms
8  * are also available at
9  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10  *
11  */
12
13 package org.scilab.modules.scinotes;
14
15 import java.awt.Color;
16 import java.text.DateFormat;
17 import java.util.Calendar;
18
19 import javax.xml.xpath.XPath;
20 import javax.xml.xpath.XPathConstants;
21 import javax.xml.xpath.XPathExpressionException;
22 import javax.xml.xpath.XPathFactory;
23
24 import org.w3c.dom.Document;
25
26 import org.scilab.modules.commons.OS;
27 import static org.scilab.modules.commons.xml.XConfiguration.XConfAttribute;
28 import org.scilab.modules.commons.xml.XConfiguration;
29
30 /**
31  * The class ScilabContext provides a context to render a Scilab's document.
32  * @author Calixte DENIZET
33  */
34 public class SciNotesOptions {
35
36     public static final String PREFERENCESPATH = "//scinotes/body/scinotes-preferences";
37     public static final String DISPLAYPATH = "//scinotes/display/body/scinotes-display";
38     public static final String AUTOSAVEPATH = "//scinotes/autosave/body/scinotes-autosave";
39     public static final String HEADERPATH = "//scinotes/header/body/scinotes-header";
40     public static final String KEYMAPPATH = "//general/shortcuts/body/actions/action-folder[@xconf-uid=\"scinotes\"]/action";
41
42     private static SciNotesOptions.Preferences prefs;
43     private static SciNotesOptions.Display display;
44     private static SciNotesOptions.Autosave autosave;
45     private static SciNotesOptions.Header header;
46
47     private static Document doc;
48
49     /* scinotes-preferences */
50     @XConfAttribute
51     public static class Preferences {
52
53         public boolean restartOpen;
54         public boolean addLineTermination;
55         public int numberOfRecentlyOpen;
56         public String encoding;
57         public String eol;
58         public boolean useScinotes;
59         public boolean externalCmd;
60         public String cmd;
61
62         private Preferences() { }
63
64         @XConfAttribute(tag = "scinotes-preferences", attributes = {"restart-reopen", "add-line-termination", "number-of-recently-open", "encoding", "eol", "scinotes", "cmd", "external-cmd"})
65         private void set(boolean restartOpen, boolean addLineTermination, int numberOfRecentlyOpen, String encoding, String eol, boolean useScinotes, boolean externalCmd, String cmd) {
66             this.restartOpen = restartOpen;
67             this.addLineTermination = addLineTermination;
68             this.numberOfRecentlyOpen = numberOfRecentlyOpen;
69             this.encoding = encoding.toLowerCase();
70
71             if (eol.equals("")) {
72                 switch (OS.get()) {
73                 case WINDOWS:
74                     this.eol = ScilabDocument.EOLWIN;
75                     break;
76                 case MAC:
77                     this.eol = ScilabDocument.EOLMAC;
78                     break;
79                 default:
80                     this.eol = ScilabDocument.EOLUNIX;
81                     break;
82                 }
83             } else if (eol.startsWith("Windows")) {
84                 this.eol = ScilabDocument.EOLWIN;
85             } else if (eol.startsWith("Mac")) {
86                 this.eol = ScilabDocument.EOLMAC;
87             } else {
88                 this.eol = ScilabDocument.EOLUNIX;
89             }
90
91             this.useScinotes = useScinotes;
92             this.externalCmd = externalCmd;
93             this.cmd = cmd;
94         }
95     }
96
97     /* scinotes-display */
98     @XConfAttribute
99     public static class Display {
100
101         public boolean highlightCurrentLine;
102         public Color currentLineColor;
103         public boolean showLineNumbers;
104         public boolean wrapLines;
105         public boolean keywordsColorization;
106         public boolean highlightBrackets;
107         public Color bracketsColor;
108         public int bracketsHighlightment;
109         public boolean bracketsOnmouseover;
110         public boolean highlightKeywords;
111         public Color keywordsColor;
112         public int keywordsHighlightment;
113         public boolean keywordsOnmouseover;
114         public boolean whereami;
115         public int tabSize;
116         public int tabRepresentation;
117         public boolean useSpaces;
118         public int indentSize;
119         public boolean automaticIndent;
120         public boolean autoCompleteOpeners;
121         public boolean autoCompleteKeywords;
122
123         private Display() { }
124
125         @XConfAttribute(tag = "scinotes-display", attributes = {"highlight-current-line", "current-line-color", "show-line-numbers", "wrap-lines", "keywords-colorization", "highlight-brackets", "brackets-color", "brackets-highlightment", "brackets-onmouseover", "highlight-keywords", "keywords-color", "keywords-highlightment", "keywords-onmouseover", "whereami", "tab-size", "tab-representation", "use-spaces", "indent-size", "automatic-indent", "auto-complete-openers", "auto-complete-keywords"})
126         private void set(boolean highlightCurrentLine, Color currentLineColor, boolean showLineNumbers, boolean wrapLines, boolean keywordsColorization, boolean highlightBrackets, Color bracketsColor, String bracketsHighlightment, boolean bracketsOnmouseover, boolean highlightKeywords, Color keywordsColor, String keywordsHighlightment, boolean keywordsOnmouseover, boolean whereami, int tabSize, String tabRepresentation, boolean useSpaces, int indentSize, boolean automaticIndent, boolean autoCompleteOpeners, boolean autoCompleteKeywords) {
127             this.highlightCurrentLine = highlightCurrentLine;
128             this.currentLineColor = currentLineColor;
129             this.showLineNumbers = showLineNumbers;
130             this.wrapLines = wrapLines;
131             this.keywordsColorization = keywordsColorization;
132
133             this.highlightBrackets = highlightBrackets;
134             this.bracketsColor = bracketsColor;
135             this.bracketsOnmouseover = bracketsOnmouseover;
136             this.highlightKeywords = highlightKeywords;
137             this.keywordsColor = keywordsColor;
138             this.keywordsOnmouseover = keywordsOnmouseover;
139             this.whereami = whereami;
140             this.tabSize = tabSize;
141             if (tabRepresentation.equalsIgnoreCase("chevrons")) {
142                 this.tabRepresentation = ScilabView.TABDOUBLECHEVRONS;
143             } else if (tabRepresentation.equalsIgnoreCase("hrule")) {
144                 this.tabRepresentation = ScilabView.TABHORIZONTAL;
145             } else if (tabRepresentation.equalsIgnoreCase("vrule")) {
146                 this.tabRepresentation = ScilabView.TABVERTICAL;
147             } else {
148                 this.tabRepresentation = ScilabView.TABNOTHING;
149             }
150             this.useSpaces = useSpaces;
151             this.indentSize = indentSize;
152             this.automaticIndent = automaticIndent;
153
154             if (bracketsHighlightment.equalsIgnoreCase("filled")) {
155                 this.bracketsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.FILLED;
156             } else if (bracketsHighlightment.equalsIgnoreCase("framed")) {
157                 this.bracketsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.FRAMED;
158             } else {
159                 this.bracketsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.UNDERLINED;
160             }
161
162             if (keywordsHighlightment.equalsIgnoreCase("filled")) {
163                 this.keywordsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.FILLED;
164             } else if (keywordsHighlightment.equalsIgnoreCase("framed")) {
165                 this.keywordsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.FRAMED;
166             } else {
167                 this.keywordsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.UNDERLINED;
168             }
169
170             this.autoCompleteOpeners = autoCompleteOpeners;
171             this.autoCompleteKeywords = autoCompleteKeywords;
172         }
173     }
174
175     /* scinotes-autosave */
176     @XConfAttribute
177     public static class Autosave {
178
179         public boolean autoSave;
180         public int saveEvery;
181         public boolean automaticDelete;
182         public boolean appendFilename;
183         public String appendWith;
184         public String replaceWith;
185         public boolean sourceFlag;
186         public String singleDirectory;
187
188         private Autosave() { }
189
190         @XConfAttribute(tag = "scinotes-autosave", attributes = {"enable", "save-every", "automatic-delete", "append-filename", "append-with", "replace-with", "source-flag", "single-directory"})
191         private void set(boolean autoSave, int saveEvery, boolean automaticDelete, boolean appendFilename, String appendWith, String replaceWith, boolean sourceFlag, String singleDirectory) {
192             this.autoSave = autoSave;
193             this.saveEvery = saveEvery;
194             this.automaticDelete = automaticDelete;
195             this.appendFilename = appendFilename;
196             this.appendWith = appendWith;
197             this.replaceWith = replaceWith;
198             this.sourceFlag = sourceFlag;
199             this.singleDirectory = singleDirectory;
200         }
201     }
202
203     /* scinotes-header */
204     @XConfAttribute
205     public static class Header {
206
207         public String header;
208         public boolean enable;
209
210         @XConfAttribute(tag = "scinotes-header", attributes = {"enable"})
211         private void set(boolean enable) {
212             this.enable = enable;
213             if (enable) {
214                 XPathFactory xpathFactory = XPathFactory.newInstance();
215                 XPath xp = xpathFactory.newXPath();
216                 try {
217                     header = (String) xp.compile("string(" + HEADERPATH + ")").evaluate(doc, XPathConstants.STRING);
218                 } catch (XPathExpressionException e) {
219                     System.err.println(e);
220                 }
221
222                 if (header != null) {
223                     Calendar cal = Calendar.getInstance();
224                     DateFormat dateFormat = DateFormat.getDateInstance();
225                     header = header.replaceAll("\\{\\$current-year\\}", Integer.toString(cal.get(Calendar.YEAR)));
226                     header = header.replaceAll("\\{\\$current-date\\}", dateFormat.format(cal.getTime()));
227                 }
228             }
229         }
230     }
231
232     public static void invalidate(SciNotesConfiguration.Conf conf) {
233         if (conf.preferences) {
234             prefs = null;
235             doc = null;
236         }
237         if (conf.display) {
238             display = null;
239             doc = null;
240         }
241         if (conf.autosave) {
242             autosave = null;
243             doc = null;
244         }
245         if (conf.header) {
246             header = null;
247             doc = null;
248         }
249     }
250
251     public static final SciNotesOptions.Preferences getSciNotesPreferences() {
252         if (prefs == null) {
253             if (doc == null) {
254                 doc = XConfiguration.getXConfigurationDocument();
255             }
256             prefs = XConfiguration.get(SciNotesOptions.Preferences.class, doc, PREFERENCESPATH)[0];
257         }
258
259         return prefs;
260     }
261
262     public static final SciNotesOptions.Display getSciNotesDisplay() {
263         if (display == null) {
264             if (doc == null) {
265                 doc = XConfiguration.getXConfigurationDocument();
266             }
267             display = XConfiguration.get(SciNotesOptions.Display.class, doc, DISPLAYPATH)[0];
268         }
269
270         return display;
271     }
272
273     public static final SciNotesOptions.Autosave getSciNotesAutosave() {
274         if (autosave == null) {
275             if (doc == null) {
276                 doc = XConfiguration.getXConfigurationDocument();
277             }
278             autosave = XConfiguration.get(SciNotesOptions.Autosave.class, doc, AUTOSAVEPATH)[0];
279         }
280
281         return autosave;
282     }
283
284     public static final SciNotesOptions.Header getSciNotesHeader() {
285         if (header == null) {
286             if (doc == null) {
287                 doc = XConfiguration.getXConfigurationDocument();
288             }
289
290             header = XConfiguration.get(SciNotesOptions.Header.class, doc, HEADERPATH)[0];
291         }
292
293
294         return header;
295     }
296 }