Preferences: continue to connect SciNotes
[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 String appendWith;
161         public String replaceWith;
162         public boolean sourceFlag;
163         public String singleDirectory;
164
165         private Autosave() { }
166
167         @XConfAttribute(tag = "scinotes-autosave", attributes = {"enable", "save-every", "automatic-delete", "append-with", "replace-with", "source-flag", "single-directory"})
168         private void set(boolean autoSave, int saveEvery, boolean automaticDelete, String appendWith, String replaceWith, boolean sourceFlag, String singleDirectory) {
169             this.autoSave = autoSave;
170             this.saveEvery = saveEvery;
171             this.automaticDelete = automaticDelete;
172             this.appendWith = appendWith;
173             this.replaceWith = replaceWith;
174             this.sourceFlag = sourceFlag;
175             this.singleDirectory = singleDirectory;
176         }
177     }
178
179     public static void invalidate(SciNotesConfiguration.Conf conf) {
180         if (conf.preferences) {
181             prefs = null;
182             doc = null;
183         }
184         if (conf.display) {
185             display = null;
186             doc = null;
187         }
188         if (conf.autosave) {
189             autosave = null;
190             doc = null;
191         }
192     }
193
194     public static final SciNotesOptions.Preferences getSciNotesPreferences() {
195         if (prefs == null) {
196             if (doc == null) {
197                 doc = XConfiguration.getXConfigurationDocument();
198             }
199             prefs = XConfiguration.get(SciNotesOptions.Preferences.class, doc, PREFERENCESPATH)[0];
200         }
201
202         return prefs;
203     }
204
205     public static final SciNotesOptions.Display getSciNotesDisplay() {
206         if (display == null) {
207             if (doc == null) {
208                 doc = XConfiguration.getXConfigurationDocument();
209             }
210             display = XConfiguration.get(SciNotesOptions.Display.class, doc, DISPLAYPATH)[0];
211         }
212
213         return display;
214     }
215
216     public static final SciNotesOptions.Autosave getSciNotesAutosave() {
217         if (autosave == null) {
218             if (doc == null) {
219                 doc = XConfiguration.getXConfigurationDocument();
220             }
221             autosave = XConfiguration.get(SciNotesOptions.Autosave.class, doc, AUTOSAVEPATH)[0];
222         }
223
224         return autosave;
225     }
226 }