Bug 9884 fixed: Add in prefs the possibility to add a default header on new files...
[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 static org.scilab.modules.commons.xml.XConfiguration.XConfAttribute;
27 import org.scilab.modules.commons.xml.XConfiguration;
28
29 /**
30  * The class ScilabContext provides a context to render a Scilab's document.
31  * @author Calixte DENIZET
32  */
33 public class SciNotesOptions {
34
35     public static final String PREFERENCESPATH = "//scinotes/body/scinotes-preferences";
36     public static final String DISPLAYPATH = "//scinotes/display/body/scinotes-display";
37     public static final String AUTOSAVEPATH = "//scinotes/autosave/body/scinotes-autosave";
38     public static final String HEADERPATH = "//scinotes/header/body/scinotes-header";
39     public static final String KEYMAPPATH = "//general/shortcuts/body/actions/action-folder[@xconf-uid=\"scinotes\"]/action";
40
41     private static SciNotesOptions.Preferences prefs;
42     private static SciNotesOptions.Display display;
43     private static SciNotesOptions.Autosave autosave;
44     private static SciNotesOptions.Header header;
45
46     private static Document doc;
47
48     /* scinotes-preferences */
49     @XConfAttribute
50     public static class Preferences {
51
52         public boolean restartOpen;
53         public boolean addLineTermination;
54         public int numberOfRecentlyOpen;
55         public String encoding;
56         public String eol;
57         public boolean useScinotes;
58         public boolean externalCmd;
59         public String cmd;
60
61         private Preferences() { }
62
63         @XConfAttribute(tag = "scinotes-preferences", attributes = {"restart-reopen", "add-line-termination", "number-of-recently-open", "encoding", "eol", "scinotes", "cmd", "external-cmd"})
64         private void set(boolean restartOpen, boolean addLineTermination, int numberOfRecentlyOpen, String encoding, String eol, boolean useScinotes, boolean externalCmd, String cmd) {
65             this.restartOpen = restartOpen;
66             this.addLineTermination = addLineTermination;
67             this.numberOfRecentlyOpen = numberOfRecentlyOpen;
68             this.encoding = encoding.toLowerCase();
69
70             if (eol.startsWith("Windows")) {
71                 this.eol = ScilabDocument.EOLWIN;
72             } else if (eol.startsWith("Mac")) {
73                 this.eol = ScilabDocument.EOLMAC;
74             } else {
75                 this.eol = ScilabDocument.EOLUNIX;
76             }
77
78             this.useScinotes = useScinotes;
79             this.externalCmd = externalCmd;
80             this.cmd = cmd;
81         }
82     }
83
84     /* scinotes-display */
85     @XConfAttribute
86     public static class Display {
87
88         public boolean highlightCurrentLine;
89         public Color currentLineColor;
90         public boolean showLineNumbers;
91         public boolean wrapLines;
92         public boolean keywordsColorization;
93         public boolean highlightBrackets;
94         public Color bracketsColor;
95         public int bracketsHighlightment;
96         public boolean bracketsOnmouseover;
97         public boolean highlightKeywords;
98         public Color keywordsColor;
99         public int keywordsHighlightment;
100         public boolean keywordsOnmouseover;
101         public boolean whereami;
102         public int tabSize;
103         public int tabRepresentation;
104         public boolean useSpaces;
105         public int indentSize;
106         public boolean automaticIndent;
107         public boolean autoCompleteOpeners;
108         public boolean autoCompleteKeywords;
109
110         private Display() { }
111
112         @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"})
113         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) {
114             this.highlightCurrentLine = highlightCurrentLine;
115             this.currentLineColor = currentLineColor;
116             this.showLineNumbers = showLineNumbers;
117             this.wrapLines = wrapLines;
118             this.keywordsColorization = keywordsColorization;
119
120             this.highlightBrackets = highlightBrackets;
121             this.bracketsColor = bracketsColor;
122             this.bracketsOnmouseover = bracketsOnmouseover;
123             this.highlightKeywords = highlightKeywords;
124             this.keywordsColor = keywordsColor;
125             this.keywordsOnmouseover = keywordsOnmouseover;
126             this.whereami = whereami;
127             this.tabSize = tabSize;
128             if (tabRepresentation.equalsIgnoreCase("chevrons")) {
129                 this.tabRepresentation = ScilabView.TABDOUBLECHEVRONS;
130             } else if (tabRepresentation.equalsIgnoreCase("hrule")) {
131                 this.tabRepresentation = ScilabView.TABHORIZONTAL;
132             } else if (tabRepresentation.equalsIgnoreCase("vrule")) {
133                 this.tabRepresentation = ScilabView.TABVERTICAL;
134             } else {
135                 this.tabRepresentation = ScilabView.TABNOTHING;
136             }
137             this.useSpaces = useSpaces;
138             this.indentSize = indentSize;
139             this.automaticIndent = automaticIndent;
140
141             if (bracketsHighlightment.equalsIgnoreCase("filled")) {
142                 this.bracketsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.FILLED;
143             } else if (bracketsHighlightment.equalsIgnoreCase("framed")) {
144                 this.bracketsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.FRAMED;
145             } else {
146                 this.bracketsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.UNDERLINED;
147             }
148
149             if (keywordsHighlightment.equalsIgnoreCase("filled")) {
150                 this.keywordsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.FILLED;
151             } else if (keywordsHighlightment.equalsIgnoreCase("framed")) {
152                 this.keywordsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.FRAMED;
153             } else {
154                 this.keywordsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.UNDERLINED;
155             }
156
157             this.autoCompleteOpeners = autoCompleteOpeners;
158             this.autoCompleteKeywords = autoCompleteKeywords;
159         }
160     }
161
162     /* scinotes-autosave */
163     @XConfAttribute
164     public static class Autosave {
165
166         public boolean autoSave;
167         public int saveEvery;
168         public boolean automaticDelete;
169         public boolean appendFilename;
170         public String appendWith;
171         public String replaceWith;
172         public boolean sourceFlag;
173         public String singleDirectory;
174
175         private Autosave() { }
176
177         @XConfAttribute(tag = "scinotes-autosave", attributes = {"enable", "save-every", "automatic-delete", "append-filename", "append-with", "replace-with", "source-flag", "single-directory"})
178         private void set(boolean autoSave, int saveEvery, boolean automaticDelete, boolean appendFilename, String appendWith, String replaceWith, boolean sourceFlag, String singleDirectory) {
179             this.autoSave = autoSave;
180             this.saveEvery = saveEvery;
181             this.automaticDelete = automaticDelete;
182             this.appendFilename = appendFilename;
183             this.appendWith = appendWith;
184             this.replaceWith = replaceWith;
185             this.sourceFlag = sourceFlag;
186             this.singleDirectory = singleDirectory;
187         }
188     }
189
190     /* scinotes-header */
191     @XConfAttribute
192     public static class Header {
193
194         public String header;
195         public boolean enable;
196
197         @XConfAttribute(tag = "scinotes-header", attributes = {"enable"})
198         private void set(boolean enable) {
199             this.enable = enable;
200             if (enable) {
201                 XPathFactory xpathFactory = XPathFactory.newInstance();
202                 XPath xp = xpathFactory.newXPath();
203                 try {
204                     header = (String) xp.compile("string(" + HEADERPATH + ")").evaluate(doc, XPathConstants.STRING);
205                 } catch (XPathExpressionException e) {
206                     System.err.println(e);
207                 }
208
209                 if (header != null) {
210                     Calendar cal = Calendar.getInstance();
211                     DateFormat dateFormat = DateFormat.getDateInstance();
212                     header = header.replaceAll("\\{\\$current-year\\}", Integer.toString(cal.get(Calendar.YEAR)));
213                     header = header.replaceAll("\\{\\$current-date\\}", dateFormat.format(cal.getTime()));
214                 }
215             }
216         }
217     }
218
219     public static void invalidate(SciNotesConfiguration.Conf conf) {
220         if (conf.preferences) {
221             prefs = null;
222             doc = null;
223         }
224         if (conf.display) {
225             display = null;
226             doc = null;
227         }
228         if (conf.autosave) {
229             autosave = null;
230             doc = null;
231         }
232         if (conf.header) {
233             header = null;
234             doc = null;
235         }
236     }
237
238     public static final SciNotesOptions.Preferences getSciNotesPreferences() {
239         if (prefs == null) {
240             if (doc == null) {
241                 doc = XConfiguration.getXConfigurationDocument();
242             }
243             prefs = XConfiguration.get(SciNotesOptions.Preferences.class, doc, PREFERENCESPATH)[0];
244         }
245
246         return prefs;
247     }
248
249     public static final SciNotesOptions.Display getSciNotesDisplay() {
250         if (display == null) {
251             if (doc == null) {
252                 doc = XConfiguration.getXConfigurationDocument();
253             }
254             display = XConfiguration.get(SciNotesOptions.Display.class, doc, DISPLAYPATH)[0];
255         }
256
257         return display;
258     }
259
260     public static final SciNotesOptions.Autosave getSciNotesAutosave() {
261         if (autosave == null) {
262             if (doc == null) {
263                 doc = XConfiguration.getXConfigurationDocument();
264             }
265             autosave = XConfiguration.get(SciNotesOptions.Autosave.class, doc, AUTOSAVEPATH)[0];
266         }
267
268         return autosave;
269     }
270
271     public static final SciNotesOptions.Header getSciNotesHeader() {
272         if (header == null) {
273             if (doc == null) {
274                 doc = XConfiguration.getXConfigurationDocument();
275             }
276
277             header = XConfiguration.get(SciNotesOptions.Header.class, doc, HEADERPATH)[0];
278         }
279
280
281         return header;
282     }
283 }