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 static org.scilab.modules.commons.xml.XConfiguration.XConfAttribute;
27 import org.scilab.modules.commons.xml.XConfiguration;
30 * The class ScilabContext provides a context to render a Scilab's document.
31 * @author Calixte DENIZET
33 public class SciNotesOptions {
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";
41 private static SciNotesOptions.Preferences prefs;
42 private static SciNotesOptions.Display display;
43 private static SciNotesOptions.Autosave autosave;
44 private static SciNotesOptions.Header header;
46 private static Document doc;
48 /* scinotes-preferences */
50 public static class Preferences {
52 public boolean restartOpen;
53 public boolean addLineTermination;
54 public int numberOfRecentlyOpen;
55 public String encoding;
57 public boolean useScinotes;
58 public boolean externalCmd;
61 private Preferences() { }
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();
70 if (eol.startsWith("Windows")) {
71 this.eol = ScilabDocument.EOLWIN;
72 } else if (eol.startsWith("Mac")) {
73 this.eol = ScilabDocument.EOLMAC;
75 this.eol = ScilabDocument.EOLUNIX;
78 this.useScinotes = useScinotes;
79 this.externalCmd = externalCmd;
84 /* scinotes-display */
86 public static class Display {
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;
103 public int tabRepresentation;
104 public boolean useSpaces;
105 public int indentSize;
106 public boolean automaticIndent;
107 public boolean autoCompleteOpeners;
108 public boolean autoCompleteKeywords;
110 private Display() { }
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;
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;
135 this.tabRepresentation = ScilabView.TABNOTHING;
137 this.useSpaces = useSpaces;
138 this.indentSize = indentSize;
139 this.automaticIndent = automaticIndent;
141 if (bracketsHighlightment.equalsIgnoreCase("filled")) {
142 this.bracketsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.FILLED;
143 } else if (bracketsHighlightment.equalsIgnoreCase("framed")) {
144 this.bracketsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.FRAMED;
146 this.bracketsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.UNDERLINED;
149 if (keywordsHighlightment.equalsIgnoreCase("filled")) {
150 this.keywordsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.FILLED;
151 } else if (keywordsHighlightment.equalsIgnoreCase("framed")) {
152 this.keywordsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.FRAMED;
154 this.keywordsHighlightment = MatchingBlockManager.ScilabKeywordsPainter.UNDERLINED;
157 this.autoCompleteOpeners = autoCompleteOpeners;
158 this.autoCompleteKeywords = autoCompleteKeywords;
162 /* scinotes-autosave */
164 public static class Autosave {
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;
175 private Autosave() { }
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;
190 /* scinotes-header */
192 public static class Header {
194 public String header;
195 public boolean enable;
197 @XConfAttribute(tag = "scinotes-header", attributes = {"enable"})
198 private void set(boolean enable) {
199 this.enable = enable;
201 XPathFactory xpathFactory = XPathFactory.newInstance();
202 XPath xp = xpathFactory.newXPath();
204 header = (String) xp.compile("string(" + HEADERPATH + ")").evaluate(doc, XPathConstants.STRING);
205 } catch (XPathExpressionException e) {
206 System.err.println(e);
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()));
219 public static void invalidate(SciNotesConfiguration.Conf conf) {
220 if (conf.preferences) {
238 public static final SciNotesOptions.Preferences getSciNotesPreferences() {
241 doc = XConfiguration.getXConfigurationDocument();
243 prefs = XConfiguration.get(SciNotesOptions.Preferences.class, doc, PREFERENCESPATH)[0];
249 public static final SciNotesOptions.Display getSciNotesDisplay() {
250 if (display == null) {
252 doc = XConfiguration.getXConfigurationDocument();
254 display = XConfiguration.get(SciNotesOptions.Display.class, doc, DISPLAYPATH)[0];
260 public static final SciNotesOptions.Autosave getSciNotesAutosave() {
261 if (autosave == null) {
263 doc = XConfiguration.getXConfigurationDocument();
265 autosave = XConfiguration.get(SciNotesOptions.Autosave.class, doc, AUTOSAVEPATH)[0];
271 public static final SciNotesOptions.Header getSciNotesHeader() {
272 if (header == null) {
274 doc = XConfiguration.getXConfigurationDocument();
277 header = XConfiguration.get(SciNotesOptions.Header.class, doc, HEADERPATH)[0];