29ffb69bc01700f6ec8990341f4665ed670ec868
[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
17 import org.w3c.dom.Document;
18
19 import static org.scilab.modules.commons.xml.XConfiguration.XConfAttribute;
20 import org.scilab.modules.commons.xml.XConfiguration;
21
22 /**
23  * The class ScilabContext provides a context to render a Scilab's document.
24  * @author Calixte DENIZET
25  */
26 public class SciNotesOptions {
27
28     public static final String PREFERENCESPATH = "//scinotes/body/scinotes-preferences";
29     public static final String DISPLAYPATH = "//scinotes/display/body/scinotes-display";
30     public static final String AUTOSAVEPATH = "//scinotes/autosave/body/scinotes-autosave";
31     public static final String KEYMAPPATH = "//general/shortcuts/body/actions/action-folder[@xconf-uid=\"scinotes\"]/action";
32
33     private static SciNotesOptions.Preferences prefs;
34     private static SciNotesOptions.Display display;
35     private static SciNotesOptions.Autosave autosave;
36
37     private static Document doc;
38
39     /* scinotes-preferences */
40     @XConfAttribute
41     public static class Preferences {
42
43         public boolean restartOpen;
44         public boolean addLineTermination;
45         public int numberOfRecentlyOpen;
46         public String encoding;
47         public String eol;
48         public boolean useScinotes;
49         public boolean externalCmd;
50         public String cmd;
51
52         private Preferences() { }
53
54         @XConfAttribute(tag = "scinotes-preferences", attributes = {"restart-reopen", "add-line-termination", "number-of-recently-open", "encoding", "eol", "scinotes", "cmd", "external-cmd"})
55         private void set(boolean restartOpen, boolean addLineTermination, int numberOfRecentlyOpen, String encoding, String eol, boolean useScinotes, boolean externalCmd, String cmd) {
56             this.restartOpen = restartOpen;
57             this.addLineTermination = addLineTermination;
58             this.numberOfRecentlyOpen = numberOfRecentlyOpen;
59             this.encoding = encoding.toLowerCase();
60
61             if (eol.startsWith("Windows")) {
62                 this.eol = ScilabDocument.EOLWIN;
63             } else if (eol.startsWith("Mac")) {
64                 this.eol = ScilabDocument.EOLMAC;
65             } else {
66                 this.eol = ScilabDocument.EOLUNIX;
67             }
68
69             this.useScinotes = useScinotes;
70             this.externalCmd = externalCmd;
71             this.cmd = cmd;
72         }
73     }
74
75     /* scinotes-display */
76     @XConfAttribute
77     public static class Display {
78
79         public boolean highlightCurrentLine;
80         public Color currentLineColor;
81         public boolean showLineNumbers;
82         public boolean wrapLines;
83         public boolean keywordsColorization;
84         public boolean highlightBrackets;
85         public Color bracketsColor;
86         public int bracketsHighlightment;
87         public boolean bracketsOnmouseover;
88         public boolean highlightKeywords;
89         public Color keywordsColor;
90         public int keywordsHighlightment;
91         public boolean keywordsOnmouseover;
92         public boolean whereami;
93         public int tabSize;
94         public int tabRepresentation;
95         public boolean useSpaces;
96         public int indentSize;
97         public boolean automaticIndent;
98         public boolean autoCompleteOpeners;
99         public boolean autoCompleteKeywords;
100
101         private Display() { }
102
103         @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"})
104         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) {
105             this.highlightCurrentLine = highlightCurrentLine;
106             this.currentLineColor = currentLineColor;
107             this.showLineNumbers = showLineNumbers;
108             this.wrapLines = wrapLines;
109             this.keywordsColorization = keywordsColorization;
110
111             this.highlightBrackets = highlightBrackets;
112             this.bracketsColor = bracketsColor;
113             this.bracketsOnmouseover = bracketsOnmouseover;
114             this.highlightKeywords = highlightKeywords;
115             this.keywordsColor = keywordsColor;
116             this.keywordsOnmouseover = keywordsOnmouseover;
117             this.whereami = whereami;
118             this.tabSize = tabSize;
119             if (tabRepresentation.equalsIgnoreCase("chevrons")) {
120                 this.tabRepresentation = ScilabView.TABDOUBLECHEVRONS;
121             } else if (tabRepresentation.equalsIgnoreCase("hrule")) {
122                 this.tabRepresentation = ScilabView.TABHORIZONTAL;
123             } else if (tabRepresentation.equalsIgnoreCase("vrule")) {
124                 this.tabRepresentation = ScilabView.TABVERTICAL;
125             } else {
126                 this.tabRepresentation = ScilabView.TABNOTHING;
127             }
128             this.useSpaces = useSpaces;
129             this.indentSize = indentSize;
130             this.automaticIndent = automaticIndent;
131
132             if (bracketsHighlightment.equalsIgnoreCase("filled")) {
133                 this.bracketsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.FILLED;
134             } else if (bracketsHighlightment.equalsIgnoreCase("framed")) {
135                 this.bracketsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.FRAMED;
136             } else {
137                 this.bracketsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.UNDERLINED;
138             }
139
140             if (keywordsHighlightment.equalsIgnoreCase("filled")) {
141                 this.keywordsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.FILLED;
142             } else if (keywordsHighlightment.equalsIgnoreCase("framed")) {
143                 this.keywordsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.FRAMED;
144             } else {
145                 this.keywordsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.UNDERLINED;
146             }
147
148             this.autoCompleteOpeners = autoCompleteOpeners;
149             this.autoCompleteKeywords = autoCompleteKeywords;
150         }
151     }
152
153     /* scinotes-autosave */
154     @XConfAttribute
155     public static class Autosave {
156
157         public boolean autoSave;
158         public int saveEvery;
159         public boolean automaticDelete;
160         public boolean appendFilename;
161         public String appendWith;
162         public String replaceWith;
163         public boolean sourceFlag;
164         public String singleDirectory;
165
166         private Autosave() { }
167
168         @XConfAttribute(tag = "scinotes-autosave", attributes = {"enable", "save-every", "automatic-delete", "append-filename", "append-with", "replace-with", "source-flag", "single-directory"})
169         private void set(boolean autoSave, int saveEvery, boolean automaticDelete, boolean appendFilename, String appendWith, String replaceWith, boolean sourceFlag, String singleDirectory) {
170             this.autoSave = autoSave;
171             this.saveEvery = saveEvery;
172             this.automaticDelete = automaticDelete;
173             this.appendFilename = appendFilename;
174             this.appendWith = appendWith;
175             this.replaceWith = replaceWith;
176             this.sourceFlag = sourceFlag;
177             this.singleDirectory = singleDirectory;
178         }
179     }
180
181     public static void invalidate(SciNotesConfiguration.Conf conf) {
182         if (conf.preferences) {
183             prefs = null;
184             doc = null;
185         }
186         if (conf.display) {
187             display = null;
188             doc = null;
189         }
190         if (conf.autosave) {
191             autosave = null;
192             doc = null;
193         }
194     }
195
196     public static final SciNotesOptions.Preferences getSciNotesPreferences() {
197         if (prefs == null) {
198             if (doc == null) {
199                 doc = XConfiguration.getXConfigurationDocument();
200             }
201             prefs = XConfiguration.get(SciNotesOptions.Preferences.class, doc, PREFERENCESPATH)[0];
202         }
203
204         return prefs;
205     }
206
207     public static final SciNotesOptions.Display getSciNotesDisplay() {
208         if (display == null) {
209             if (doc == null) {
210                 doc = XConfiguration.getXConfigurationDocument();
211             }
212             display = XConfiguration.get(SciNotesOptions.Display.class, doc, DISPLAYPATH)[0];
213         }
214
215         return display;
216     }
217
218     public static final SciNotesOptions.Autosave getSciNotesAutosave() {
219         if (autosave == null) {
220             if (doc == null) {
221                 doc = XConfiguration.getXConfigurationDocument();
222             }
223             autosave = XConfiguration.get(SciNotesOptions.Autosave.class, doc, AUTOSAVEPATH)[0];
224         }
225
226         return autosave;
227     }
228 }