Prefs: add a function preferences()
[scilab.git] / scilab / modules / preferences / src / java / org / scilab / modules / preferences / Component / PreviewCode.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2011 - Pierre GRADIT
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.preferences.Component;
14
15 import java.awt.Color;
16 import java.awt.Font;
17 import java.awt.event.ActionEvent;
18 import java.awt.event.ActionListener;
19 import java.awt.event.MouseEvent;
20
21 import javax.swing.JScrollPane;
22 import javax.swing.text.DefaultCaret;
23
24 import org.w3c.dom.Node;
25 import org.w3c.dom.NodeList;
26
27 import org.scilab.modules.commons.ScilabCommonsUtils;
28 import org.scilab.modules.preferences.XChooser;
29 import org.scilab.modules.preferences.XComponent;
30 import org.scilab.modules.preferences.XConfigManager;
31 import org.scilab.modules.scinotes.KeywordAdaptater;
32 import org.scilab.modules.scinotes.KeywordEvent;
33 import org.scilab.modules.scinotes.ScilabEditorPane;
34 import org.scilab.modules.scinotes.ScilabEditorKit;
35 import org.scilab.modules.scinotes.ScilabLexerConstants;
36
37 /** Implementation of Panel compliant with extended management.
38  *
39  * @author Pierre GRADIT
40  *
41  */
42 public class PreviewCode extends Panel implements XComponent, XChooser {
43
44     /** Universal identifier for serialization.
45      *
46      */
47     private static final long serialVersionUID = 3462302313959678932L;
48     private static final int GAP = 5;
49
50     private final ScilabEditorPane previewEditorPane;
51
52     /** Constructor.
53      *
54      * @param peer : associated view DOM node.
55      */
56     public PreviewCode(final Node peer) {
57         super(peer);
58         fixedHeight = false;
59         previewEditorPane = new ScilabEditorPane(null);
60         ScilabEditorKit kit = new ScilabEditorKit();
61         previewEditorPane.setEditorKit(kit);
62         previewEditorPane.setFont(kit.getStylePreferences().tokenFonts[0]);
63         String codeSample = "// A comment with whites    and tabulations \t\t\n"
64                             + "// Email: <scilab.support@scilab.org>\n"
65                             + "// Scilab editor: http://www.scilab.org/\n"
66                             + "//\n"
67                             + "// LaTeX $\\sum_{n=1}^{+\\infty}\\frac1{n^2}=\\frac{\\pi^2}6$\n"
68                             + "function [a, b] = myfunction(d, e, f)\n"
69                             + "\ta = 2.71828 + %pi + f($, :);\n"
70                             + "\tb = cos(a) + cosh(a);\n"
71                             + "\tif d == e then\n"
72                             + "\t\tb = 10 - e.field;\n"
73                             + "\telse\n"
74                             + "\t\tb = \"\t\ttest     \" + home\n"
75                             + "\t\treturn\n"
76                             + "\tend\n"
77                             + "\tmyvar = 1.23e-45;\n"
78                             + "endfunction";
79         previewEditorPane.setText(codeSample);
80         previewEditorPane.setCaret(new DefaultCaret() {
81             public void mouseDragged(MouseEvent e) {
82                 e.consume();
83             }
84         });
85         previewEditorPane.setBackground(Color.WHITE);
86         previewEditorPane.getCaret().setBlinkRate(500);
87         previewEditorPane.getCaret().setVisible(true);
88         previewEditorPane.setEditable(false);
89         previewEditorPane.addKeywordListener(new KeywordAdaptater.MouseOverAdaptater() {
90             public void caughtKeyword(KeywordEvent e) {
91                 previewEditorPane.setToolTipText(ScilabLexerConstants.getStringRep(e.getType()));
92             }
93         });
94         previewEditorPane.addKeywordListener(new KeywordAdaptater.MouseClickedAdaptater() {
95             public void caughtKeyword(KeywordEvent e) {
96                 PreviewCode.this.keywordClicked(e);
97             }
98         });
99         JScrollPane scrollPane = new JScrollPane(previewEditorPane);
100         previewEditorPane.setRequestFocusEnabled(true);
101         previewEditorPane.setFocusable(true);
102
103         add(scrollPane);
104     }
105
106     /** Define the set of actuators.
107      *
108      * @return array of actuator names.
109      */
110     public final String [] actuators() {
111         String [] actuators = {};
112         return actuators;
113     }
114
115     /** Refresh the component by the use of actuators.
116      *
117      * @param peer the corresponding view DOM node
118      */
119     public void refresh(final Node peer) {
120         NodeList nodelist = peer.getChildNodes();
121         for (int i = 0; i < nodelist.getLength(); i++) {
122             Node node = nodelist.item(i);
123             if (node.getNodeName().equals("tableRow")) {
124                 String name = XConfigManager.getAttribute(node , "name");
125                 String xColor = XConfigManager.getAttribute(node , "color");
126                 Color color = XConfigManager.getColor(xColor);
127                 previewEditorPane.resetColor(name, color);
128                 int style = 0;
129                 if (XConfigManager.getAttribute(node , "underline").equals("true")) {
130                     style = 1;
131                 }
132                 if (XConfigManager.getAttribute(node , "strike-through").equals("true")) {
133                     style += 2;
134                 }
135                 previewEditorPane.resetAttribute(name, style);
136                 int bold = -1;
137                 if (XConfigManager.getAttribute(node , "bold").equals("true")) {
138                     bold = 1;
139                 }
140                 int italic = -2;
141                 if (XConfigManager.getAttribute(node , "italic").equals("true")) {
142                     italic = 2;
143                 }
144                 previewEditorPane.resetFont(name, bold);
145                 previewEditorPane.resetFont(name, italic);
146             }
147         }
148         previewEditorPane.repaint();
149     }
150
151     /** Row selection management.
152      *
153      */
154     private ActionListener actionListener = null;
155     String chosenItem;
156
157     /** Actual response read by the listener.
158      *
159      * @return index of the selected line.
160      */
161     public final Object choose() {
162         return chosenItem;
163     }
164
165     public void keywordClicked(KeywordEvent e) {
166         if (actionListener != null) {
167             int choosedIndex = e.getType();
168             chosenItem = ScilabLexerConstants.getStringRep(choosedIndex);
169             ActionEvent transmit  = new ActionEvent(this, 0, "Keyword changed", System.currentTimeMillis(), 0);
170             actionListener.actionPerformed(transmit);
171         }
172     }
173
174     /** Registration of a single listener.
175      * @param listener for the unique actionListener.
176      */
177     public final void addActionListener(final ActionListener listener) {
178         actionListener = listener;
179     }
180
181     /** Developer serialization method.
182      *
183      * @return equivalent signature.
184      */
185     public final String toString() {
186         return "PreviewCode";
187     }
188 }