2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2012 - Scilab Enterprises -Calixte DENIZET
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
13 package org.scilab.modules.scinotes;
15 import java.awt.Color;
16 import java.text.DateFormat;
17 import java.util.Calendar;
19 import javax.xml.xpath.XPath;
20 import javax.xml.xpath.XPathConstants;
21 import javax.xml.xpath.XPathExpressionException;
22 import javax.xml.xpath.XPathFactory;
24 import org.w3c.dom.Document;
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;
31 * The class ScilabContext provides a context to render a Scilab's document.
32 * @author Calixte DENIZET
34 public class SciNotesOptions {
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";
42 private static SciNotesOptions.Preferences prefs;
43 private static SciNotesOptions.Display display;
44 private static SciNotesOptions.Autosave autosave;
45 private static SciNotesOptions.Header header;
47 private static Document doc;
49 /* scinotes-preferences */
51 public static class Preferences {
53 public boolean restartOpen;
54 public boolean addLineTermination;
55 public int numberOfRecentlyOpen;
56 public String encoding;
58 public boolean useScinotes;
59 public boolean externalCmd;
62 private Preferences() { }
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, boolean externalCmd, String cmd) {
66 this.restartOpen = restartOpen;
67 this.addLineTermination = addLineTermination;
68 this.numberOfRecentlyOpen = numberOfRecentlyOpen;
69 this.encoding = encoding.toLowerCase();
74 this.eol = ScilabDocument.EOLWIN;
77 this.eol = ScilabDocument.EOLMAC;
80 this.eol = ScilabDocument.EOLUNIX;
83 } else if (eol.startsWith("Windows")) {
84 this.eol = ScilabDocument.EOLWIN;
85 } else if (eol.startsWith("Mac")) {
86 this.eol = ScilabDocument.EOLMAC;
88 this.eol = ScilabDocument.EOLUNIX;
91 this.useScinotes = useScinotes;
92 this.externalCmd = externalCmd;
97 /* scinotes-display */
99 public static class Display {
101 public boolean highlightCurrentLine;
102 public Color currentLineColor;
103 public boolean showLineNumbers;
104 public boolean wrapLines;
105 public boolean keywordsColorization;
106 public boolean highlightBrackets;
107 public Color bracketsColor;
108 public int bracketsHighlightment;
109 public boolean bracketsOnmouseover;
110 public boolean highlightKeywords;
111 public Color keywordsColor;
112 public int keywordsHighlightment;
113 public boolean keywordsOnmouseover;
114 public boolean whereami;
116 public int tabRepresentation;
117 public boolean useSpaces;
118 public int indentSize;
119 public boolean automaticIndent;
120 public boolean autoCompleteOpeners;
121 public boolean autoCompleteKeywords;
123 private Display() { }
125 @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"})
126 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) {
127 this.highlightCurrentLine = highlightCurrentLine;
128 this.currentLineColor = currentLineColor;
129 this.showLineNumbers = showLineNumbers;
130 this.wrapLines = wrapLines;
131 this.keywordsColorization = keywordsColorization;
133 this.highlightBrackets = highlightBrackets;
134 this.bracketsColor = bracketsColor;
135 this.bracketsOnmouseover = bracketsOnmouseover;
136 this.highlightKeywords = highlightKeywords;
137 this.keywordsColor = keywordsColor;
138 this.keywordsOnmouseover = keywordsOnmouseover;
139 this.whereami = whereami;
140 this.tabSize = tabSize;
141 if (tabRepresentation.equalsIgnoreCase("chevrons")) {
142 this.tabRepresentation = ScilabView.TABDOUBLECHEVRONS;
143 } else if (tabRepresentation.equalsIgnoreCase("hrule")) {
144 this.tabRepresentation = ScilabView.TABHORIZONTAL;
145 } else if (tabRepresentation.equalsIgnoreCase("vrule")) {
146 this.tabRepresentation = ScilabView.TABVERTICAL;
148 this.tabRepresentation = ScilabView.TABNOTHING;
150 this.useSpaces = useSpaces;
151 this.indentSize = indentSize;
152 this.automaticIndent = automaticIndent;
154 if (bracketsHighlightment.equalsIgnoreCase("filled")) {
155 this.bracketsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.FILLED;
156 } else if (bracketsHighlightment.equalsIgnoreCase("framed")) {
157 this.bracketsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.FRAMED;
159 this.bracketsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.UNDERLINED;
162 if (keywordsHighlightment.equalsIgnoreCase("filled")) {
163 this.keywordsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.FILLED;
164 } else if (keywordsHighlightment.equalsIgnoreCase("framed")) {
165 this.keywordsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.FRAMED;
167 this.keywordsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.UNDERLINED;
170 this.autoCompleteOpeners = autoCompleteOpeners;
171 this.autoCompleteKeywords = autoCompleteKeywords;
175 /* scinotes-autosave */
177 public static class Autosave {
179 public boolean autoSave;
180 public int saveEvery;
181 public boolean automaticDelete;
182 public boolean appendFilename;
183 public String appendWith;
184 public String replaceWith;
185 public boolean sourceFlag;
186 public String singleDirectory;
188 private Autosave() { }
190 @XConfAttribute(tag = "scinotes-autosave", attributes = {"enable", "save-every", "automatic-delete", "append-filename", "append-with", "replace-with", "source-flag", "single-directory"})
191 private void set(boolean autoSave, int saveEvery, boolean automaticDelete, boolean appendFilename, String appendWith, String replaceWith, boolean sourceFlag, String singleDirectory) {
192 this.autoSave = autoSave;
193 this.saveEvery = saveEvery;
194 this.automaticDelete = automaticDelete;
195 this.appendFilename = appendFilename;
196 this.appendWith = appendWith;
197 this.replaceWith = replaceWith;
198 this.sourceFlag = sourceFlag;
199 this.singleDirectory = singleDirectory;
203 /* scinotes-header */
205 public static class Header {
207 public String header;
208 public boolean enable;
210 @XConfAttribute(tag = "scinotes-header", attributes = {"enable"})
211 private void set(boolean enable) {
212 this.enable = enable;
214 XPathFactory xpathFactory = XPathFactory.newInstance();
215 XPath xp = xpathFactory.newXPath();
217 header = (String) xp.compile("string(" + HEADERPATH + ")").evaluate(doc, XPathConstants.STRING);
218 } catch (XPathExpressionException e) {
219 System.err.println(e);
222 if (header != null) {
223 Calendar cal = Calendar.getInstance();
224 DateFormat dateFormat = DateFormat.getDateInstance();
225 header = header.replaceAll("\\{\\$current-year\\}", Integer.toString(cal.get(Calendar.YEAR)));
226 header = header.replaceAll("\\{\\$current-date\\}", dateFormat.format(cal.getTime()));
232 public static void invalidate(SciNotesConfiguration.Conf conf) {
233 if (conf.preferences) {
251 public static final SciNotesOptions.Preferences getSciNotesPreferences() {
254 doc = XConfiguration.getXConfigurationDocument();
256 prefs = XConfiguration.get(SciNotesOptions.Preferences.class, doc, PREFERENCESPATH)[0];
262 public static final SciNotesOptions.Display getSciNotesDisplay() {
263 if (display == null) {
265 doc = XConfiguration.getXConfigurationDocument();
267 display = XConfiguration.get(SciNotesOptions.Display.class, doc, DISPLAYPATH)[0];
273 public static final SciNotesOptions.Autosave getSciNotesAutosave() {
274 if (autosave == null) {
276 doc = XConfiguration.getXConfigurationDocument();
278 autosave = XConfiguration.get(SciNotesOptions.Autosave.class, doc, AUTOSAVEPATH)[0];
284 public static final SciNotesOptions.Header getSciNotesHeader() {
285 if (header == null) {
287 doc = XConfiguration.getXConfigurationDocument();
290 header = XConfiguration.get(SciNotesOptions.Header.class, doc, HEADERPATH)[0];