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