82e91cc5339d042fbafefbdfb59304bd38a2523b
[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.1-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, String cmd, boolean externalCmd) {
66             this.restartOpen = restartOpen;
67             this.addLineTermination = addLineTermination;
68             this.numberOfRecentlyOpen = numberOfRecentlyOpen > 0 ? numberOfRecentlyOpen : 1;
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                     default:
77                         this.eol = ScilabDocument.EOLUNIX;
78                         break;
79                 }
80             } else if (eol.startsWith("Windows")) {
81                 this.eol = ScilabDocument.EOLWIN;
82             } else {
83                 this.eol = ScilabDocument.EOLUNIX;
84             }
85
86             this.useScinotes = useScinotes;
87             this.externalCmd = externalCmd;
88             this.cmd = cmd;
89         }
90     }
91
92     /* scinotes-display */
93     @XConfAttribute
94     public static class Display {
95
96         public boolean highlightCurrentLine;
97         public Color currentLineColor;
98         public boolean showLineNumbers;
99         public boolean wrapLines;
100         public boolean keywordsColorization;
101         public boolean highlightBrackets;
102         public Color bracketsColor;
103         public int bracketsHighlightment;
104         public boolean bracketsOnmouseover;
105         public boolean highlightKeywords;
106         public Color keywordsColor;
107         public int keywordsHighlightment;
108         public boolean keywordsOnmouseover;
109         public boolean whereami;
110         public int tabSize;
111         public int tabRepresentation;
112         public boolean useSpaces;
113         public int indentSize;
114         public boolean automaticIndent;
115         public boolean autoCompleteOpeners;
116         public boolean autoCompleteKeywords;
117         public Color backgroundColor;
118         public Color caretColor;
119
120         private Display() { }
121
122         @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", "background-color", "caret-color"})
123         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, Color backgroundColor, Color caretColor) {
124             this.highlightCurrentLine = highlightCurrentLine;
125             this.currentLineColor = currentLineColor;
126             this.showLineNumbers = showLineNumbers;
127             this.wrapLines = wrapLines;
128             this.keywordsColorization = keywordsColorization;
129
130             this.highlightBrackets = highlightBrackets;
131             this.bracketsColor = bracketsColor;
132             this.bracketsOnmouseover = bracketsOnmouseover;
133             this.highlightKeywords = highlightKeywords;
134             this.keywordsColor = keywordsColor;
135             this.keywordsOnmouseover = keywordsOnmouseover;
136             this.whereami = whereami;
137             this.tabSize = tabSize;
138             if (tabRepresentation.equalsIgnoreCase("chevrons")) {
139                 this.tabRepresentation = ScilabView.TABDOUBLECHEVRONS;
140             } else if (tabRepresentation.equalsIgnoreCase("hrule")) {
141                 this.tabRepresentation = ScilabView.TABHORIZONTAL;
142             } else if (tabRepresentation.equalsIgnoreCase("vrule")) {
143                 this.tabRepresentation = ScilabView.TABVERTICAL;
144             } else {
145                 this.tabRepresentation = ScilabView.TABNOTHING;
146             }
147             this.useSpaces = useSpaces;
148             this.indentSize = indentSize;
149             this.automaticIndent = automaticIndent;
150
151             if (bracketsHighlightment.equalsIgnoreCase("filled")) {
152                 this.bracketsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.FILLED;
153             } else if (bracketsHighlightment.equalsIgnoreCase("framed")) {
154                 this.bracketsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.FRAMED;
155             } else {
156                 this.bracketsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.UNDERLINED;
157             }
158
159             if (keywordsHighlightment.equalsIgnoreCase("filled")) {
160                 this.keywordsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.FILLED;
161             } else if (keywordsHighlightment.equalsIgnoreCase("framed")) {
162                 this.keywordsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.FRAMED;
163             } else {
164                 this.keywordsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.UNDERLINED;
165             }
166
167             this.autoCompleteOpeners = autoCompleteOpeners;
168             this.autoCompleteKeywords = autoCompleteKeywords;
169             this.backgroundColor = backgroundColor;
170             this.caretColor = caretColor;
171         }
172     }
173
174     /* scinotes-autosave */
175     @XConfAttribute
176     public static class Autosave {
177
178         public boolean autoSave;
179         public int saveEvery;
180         public boolean automaticDelete;
181         public boolean appendFilename;
182         public String appendWith;
183         public String replaceWith;
184         public boolean sourceFlag;
185         public String singleDirectory;
186
187         private Autosave() { }
188
189         @XConfAttribute(tag = "scinotes-autosave", attributes = {"enable", "save-every", "automatic-delete", "append-filename", "append-with", "replace-with", "source-flag", "single-directory"})
190         private void set(boolean autoSave, int saveEvery, boolean automaticDelete, boolean appendFilename, String appendWith, String replaceWith, boolean sourceFlag, String singleDirectory) {
191             this.autoSave = autoSave;
192             this.saveEvery = saveEvery;
193             this.automaticDelete = automaticDelete;
194             this.appendFilename = appendFilename;
195             this.appendWith = appendWith;
196             this.replaceWith = replaceWith;
197             this.sourceFlag = sourceFlag;
198             this.singleDirectory = singleDirectory;
199         }
200     }
201
202     /* scinotes-header */
203     @XConfAttribute
204     public static class Header {
205
206         public String header;
207         public boolean enable;
208
209         @XConfAttribute(tag = "scinotes-header", attributes = {"enable"})
210         private void set(boolean enable) {
211             this.enable = enable;
212             if (enable) {
213                 XPathFactory xpathFactory = XPathFactory.newInstance();
214                 XPath xp = xpathFactory.newXPath();
215                 try {
216                     header = (String) xp.compile("string(" + HEADERPATH + ")").evaluate(doc, XPathConstants.STRING);
217                 } catch (XPathExpressionException e) {
218                     System.err.println(e);
219                 }
220
221                 if (header != null) {
222                     Calendar cal = Calendar.getInstance();
223                     DateFormat dateFormat = DateFormat.getDateInstance();
224                     header = header.replaceAll("\\{\\$current-year\\}", Integer.toString(cal.get(Calendar.YEAR)));
225                     header = header.replaceAll("\\{\\$current-date\\}", dateFormat.format(cal.getTime()));
226                 }
227             }
228         }
229     }
230
231     public static void invalidate(SciNotesConfiguration.Conf conf) {
232         if (conf.preferences) {
233             prefs = null;
234             doc = null;
235         }
236         if (conf.display) {
237             display = null;
238             doc = null;
239         }
240         if (conf.autosave) {
241             autosave = null;
242             doc = null;
243         }
244         if (conf.header) {
245             header = null;
246             doc = null;
247         }
248     }
249
250     public static final SciNotesOptions.Preferences getSciNotesPreferences() {
251         if (prefs == null) {
252             if (doc == null) {
253                 doc = XConfiguration.getXConfigurationDocument();
254             }
255             prefs = XConfiguration.get(SciNotesOptions.Preferences.class, doc, PREFERENCESPATH)[0];
256         }
257
258         return prefs;
259     }
260
261     public static final SciNotesOptions.Display getSciNotesDisplay() {
262         if (display == null) {
263             if (doc == null) {
264                 doc = XConfiguration.getXConfigurationDocument();
265             }
266             display = XConfiguration.get(SciNotesOptions.Display.class, doc, DISPLAYPATH)[0];
267         }
268
269         return display;
270     }
271
272     public static final SciNotesOptions.Autosave getSciNotesAutosave() {
273         if (autosave == null) {
274             if (doc == null) {
275                 doc = XConfiguration.getXConfigurationDocument();
276             }
277             autosave = XConfiguration.get(SciNotesOptions.Autosave.class, doc, AUTOSAVEPATH)[0];
278         }
279
280         return autosave;
281     }
282
283     public static final SciNotesOptions.Header getSciNotesHeader() {
284         if (header == null) {
285             if (doc == null) {
286                 doc = XConfiguration.getXConfigurationDocument();
287             }
288
289             header = XConfiguration.get(SciNotesOptions.Header.class, doc, HEADERPATH)[0];
290         }
291
292
293         return header;
294     }
295 }