Preferences: continue to connect SciNotes
[scilab.git] / scilab / modules / scinotes / src / java / org / scilab / modules / scinotes / HelpOnTypingManager.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2010 - 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.event.KeyListener;
16 import java.awt.event.KeyEvent;
17
18 import javax.swing.text.BadLocationException;
19
20 import org.scilab.modules.commons.xml.XConfiguration;
21
22 /**
23  * This class handles the help on typing
24  * @author Calixte DENIZET
25  */
26 public final class HelpOnTypingManager implements KeyListener {
27
28     private static HelpOnTypingManager instance = new HelpOnTypingManager();
29     private static boolean openers;
30     private static boolean keywords;
31
32     /**
33      * Constructor
34      */
35     private HelpOnTypingManager() {
36         super();
37         instance = this;
38         openers = SciNotesOptions.getSciNotesDisplay().autoCompleteOpeners;
39         keywords = SciNotesOptions.getSciNotesDisplay().autoCompleteKeywords;
40     }
41
42     /**
43      * @return the singleton instance of HelpOnTypingManager
44      */
45     public static HelpOnTypingManager getInstance() {
46         return instance;
47     }
48
49     /**
50      * @param b true if autocompletion on '(', '{', ...
51      */
52     public static void enableOpeners(boolean b) {
53         openers = b;
54         SciNotesOptions.getSciNotesDisplay().autoCompleteOpeners = b;
55         XConfiguration.set(XConfiguration.getXConfigurationDocument(), SciNotesOptions.DISPLAYPATH + "/@auto-complete-openers", Boolean.toString(b));
56     }
57
58     /**
59      * @param b true if autocompletion on 'if', 'function', ...
60      */
61     public static void enableKeywords(boolean b) {
62         keywords = b;
63         SciNotesOptions.getSciNotesDisplay().autoCompleteKeywords = b;
64         XConfiguration.set(XConfiguration.getXConfigurationDocument(), SciNotesOptions.DISPLAYPATH + "/@auto-complete-keywords", Boolean.toString(b));
65     }
66
67     /**
68      * @return true if help on typing is active
69      */
70     public static boolean isActive() {
71         return openers || keywords;
72     }
73
74     /**
75      * @return true if help on typing for the openers is active
76      */
77     public static boolean isOpenersActive() {
78         return openers;
79     }
80
81     /**
82      * @return true if help on typing for the openers is active
83      */
84     public static boolean isKeywordsActive() {
85         return keywords;
86     }
87
88     /**
89      * Nothing !
90      * @param e the event
91      */
92     public void keyPressed(KeyEvent e) { }
93
94     /**
95      * Nothing !
96      * @param e the event
97      */
98     public void keyReleased(KeyEvent e) { }
99
100     /**
101      * Called when a key is typed
102      * @param e the event
103      */
104     public void keyTyped(KeyEvent e) {
105         char c = e.getKeyChar();
106         ScilabEditorPane textPane = ScilabEditorPane.getFocusedPane();
107         ScilabDocument doc = (ScilabDocument) textPane.getDocument();
108         if (isActive() && c != KeyEvent.CHAR_UNDEFINED && textPane.getSelectionStart() == textPane.getSelectionEnd()) {
109             int pos = textPane.getCaretPosition();
110             if (c == ' ' && e.getModifiers() == 0) {
111                 int end = doc.getDefaultRootElement().getElement(doc.getDefaultRootElement().getElementIndex(pos)).getEndOffset() - 1;
112                 /* the following test is used to know if an insertion in the line is done */
113                 if (pos == end) {
114                     try {
115                         doc.insertString(pos, " ", null);
116                         e.consume();
117                         KeywordEvent kwe = textPane.getKeywordEvent(pos);
118                         int[] ret;
119                         String kw;
120                         switch (kwe.getType()) {
121                             case ScilabLexerConstants.OSKEYWORD :
122                                 kw = doc.getText(kwe.getStart(), kwe.getLength());
123                                 if ("if".equals(kw)) {
124                                     doc.insertString(pos + 1, " then\nend", null);
125                                     ret = textPane.getIndentManager().indentDoc(pos + 1, pos + 9);
126                                     textPane.setCaretPosition(ret[0]);
127                                 } else if (!"end".equals(kw)) {
128                                     doc.insertString(pos + 1, "\nend", null);
129                                     ret = textPane.getIndentManager().indentDoc(pos + 1, pos + 4);
130                                     textPane.setCaretPosition(ret[0]);
131                                 }
132                                 break;
133                             case ScilabLexerConstants.SKEYWORD :
134                                 kw = doc.getText(kwe.getStart(), kwe.getLength());
135                                 if ("elseif".equals(kw)) {
136                                     doc.insertString(pos + 1, " then", null);
137                                     textPane.setCaretPosition(pos + 1);
138                                 }
139                                 break;
140                             case ScilabLexerConstants.FKEYWORD :
141                                 /* We have 'function' or 'endfunction' */
142                                 if ("f".equals(doc.getText(kwe.getStart(), 1))) {
143                                     doc.insertString(pos + 1, "()\nendfunction", null);
144                                     textPane.getIndentManager().indentDoc(pos + 3, pos + 14);
145                                     textPane.setCaretPosition(pos + 1);
146                                 }
147                                 break;
148                             default :
149                         }
150                     } catch (BadLocationException exc) {
151                         System.err.println(exc);
152                     }
153                 }
154             } else if (openers) {
155                 try {
156                     char ch = doc.getText(pos, 1).charAt(0);
157                     if (c == ch && (c == ')' || c == ']' || c == '}' || c == '\"')) {
158                         e.consume();
159                         textPane.setCaretPosition(pos + 1);
160                         return;
161                     }
162                 } catch (BadLocationException exc) {
163                     System.err.println(exc);
164                 }
165
166                 String str = null;
167                 switch (c) {
168                     case '(' :
169                         str = "()";
170                         break;
171                     case '[' :
172                         str = "[]";
173                         break;
174                     case '{' :
175                         str = "{}";
176                         break;
177                     case '\"' :
178                         str = "\"\"";
179                         break;
180                     default :
181                 }
182
183                 if (str != null) {
184                     try {
185                         doc.insertString(pos, str, null);
186                         e.consume();
187                         textPane.setCaretPosition(pos + 1);
188                     } catch (BadLocationException exc) {
189                         System.err.println(exc);
190                     }
191                 }
192             }
193         }
194     }
195 }