summaryrefslogtreecommitdiffstats
path: root/logo/src/xlogo/StyledDocument/DocumentLogo.java
blob: 83e2c81b02a223f2df0eb25277aafb83ee3bb78e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/* XLogo4Schools - A Logo Interpreter specialized for use in schools, based on XLogo by Loic Le Coq
 * Copyright (C) 2013 Marko Zivkovic
 * 
 * Contact Information: marko88zivkovic at gmail dot com
 * 
 * This program is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the Free 
 * Software Foundation; either version 2 of the License, or (at your option) 
 * any later version.  This program is distributed in the hope that it will be 
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
 * Public License for more details.  You should have received a copy of the 
 * GNU General Public License along with this program; if not, write to the Free 
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA 02110-1301, USA.
 * 
 * 
 * This Java source code belongs to XLogo4Schools, written by Marko Zivkovic
 * during his Bachelor thesis at the computer science department of ETH Zurich,
 * in the year 2013 and/or during future work.
 * 
 * It is a reengineered version of XLogo written by Loic Le Coq, published
 * under the GPL License at http://xlogo.tuxfamily.org/
 * 
 * Contents of this file were initially written by Loic Le Coq,
 * modifications, extensions, refactorings might have been applied by Marko Zivkovic 
 */package xlogo.StyledDocument;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;

import java.awt.*;

import javax.swing.text.*;

import xlogo.kernel.Primitive;
import xlogo.storage.WSManager;
import xlogo.storage.workspace.SyntaxHighlightConfig;
import xlogo.storage.workspace.WorkspaceConfig;

/**
 * Title : XLogo Description : XLogo is an interpreter for the Logo programming
 * language
 * 
 * @author Loïc Le Coq
 */

/*
 * Cette classe permet de définir la coloration syntaxique dans l'éditeur
 * Coloration des primitives Coloration des nombres, variables ou mot Coloration
 * des commentaires Correspondance entre parenthèses ou crochets
 */
public class DocumentLogo extends DefaultStyledDocument {
	private static final long serialVersionUID = 1L;
	private DefaultStyledDocument doc;
	private Element rootElement;

	private MutableAttributeSet parenthese;
	private MutableAttributeSet normal;
	private MutableAttributeSet keyword;
	private MutableAttributeSet comment;
	private MutableAttributeSet quote;
	private boolean coloration_activee = true;
	private boolean colore_parenthese = false;

	public void setColoration(boolean b) {
		coloration_activee = b;
	}

	public DocumentLogo() {
		WorkspaceConfig wc = WSManager.getWorkspaceConfig();

		doc = this;
		rootElement = doc.getDefaultRootElement();
		putProperty(DefaultEditorKit.EndOfLineStringProperty, "\n");
		
		SyntaxHighlightConfig syntaxHighlight = new SyntaxHighlightConfig();
		if (wc != null){
			coloration_activee = wc.isSyntaxHighlightingEnabled();
			syntaxHighlight = wc.getSyntaxHighlightStyles();
		}
		
		initStyles(syntaxHighlight.getCommentColor(), syntaxHighlight.getCommentStyle(),
				syntaxHighlight.getPrimitiveColor(), syntaxHighlight.getPrimitiveStyle(),
				syntaxHighlight.getBraceColor(), syntaxHighlight.getBraceStyle(),
				syntaxHighlight.getOperandColor(), syntaxHighlight.getOperandStyle());
	}

	public void initStyles(int c_comment, int sty_comment, int c_primitive,
			int sty_primitive, int c_parenthese, int sty_parenthese,
			int c_operande, int sty_operande) {
		WorkspaceConfig wc = WSManager.getWorkspaceConfig();
		Font font;
		if (wc != null){
			font = WSManager.getWorkspaceConfig().getFont();
		} else {
			font = WorkspaceConfig.DEFAULT_FONT;
		}
		 

		normal = new SimpleAttributeSet();
		StyleConstants.setFontFamily(normal, font.getFamily());
		StyleConstants.setForeground(normal, Color.black);
		StyleConstants.setFontSize(normal, font.getSize());

		comment = new SimpleAttributeSet();
		StyleConstants.setForeground(comment, new Color(c_comment));
		setBoldItalic(sty_comment, comment);
		StyleConstants.setFontSize(comment, font.getSize());

		keyword = new SimpleAttributeSet();
		StyleConstants.setForeground(keyword, new Color(c_primitive));
		setBoldItalic(sty_primitive, keyword);
		StyleConstants.setFontSize(keyword, font.getSize());

		quote = new SimpleAttributeSet();
		StyleConstants.setForeground(quote, new Color(c_operande));
		setBoldItalic(sty_operande, quote);
		StyleConstants.setFontSize(quote, font.getSize());

		parenthese = new SimpleAttributeSet();
		StyleConstants.setForeground(parenthese, new Color(c_parenthese));
		setBoldItalic(sty_parenthese, parenthese);
		StyleConstants.setFontSize(parenthese, font.getSize());
	}

	void setBoldItalic(int id, MutableAttributeSet sty) {
		switch (id) {
		case 0: // aucun style
			StyleConstants.setBold(sty, false);
			StyleConstants.setItalic(sty, false);
			StyleConstants.setUnderline(sty, false);
			break;
		case 1: // Gras
			StyleConstants.setItalic(sty, false);
			StyleConstants.setBold(sty, true);
			StyleConstants.setUnderline(sty, false);
			break;
		case 2: // italique
			StyleConstants.setBold(sty, false);
			StyleConstants.setItalic(sty, true);
			StyleConstants.setUnderline(sty, false);
			break;
		case 3: // Souligné
			StyleConstants.setBold(sty, false);
			StyleConstants.setItalic(sty, false);
			StyleConstants.setUnderline(sty, true);
			break;
		}

	}

	/*
	 * Override to apply syntax highlighting after the document has been updated
	 */
	public void insertString(int offset, String str, AttributeSet a)
			throws BadLocationException {
		if (str.equals("\t"))
			str = "  ";
		else if (str.equals("[")) {
			if (offset > 0) {
				String element = doc.getText(offset - 1, 1);
				if (!element.equals(" ") && !element.equals("\\"))
					str = " [";
			}
		} else if (str.equals("(")) {
			if (offset > 0) {
				String element = doc.getText(offset - 1, 1);
				if ("\\ *-+/&|><=(".indexOf(element) == -1)
					str = " (";
			}
		}
		super.insertString(offset, str, a);
		if (coloration_activee)
			processChangedLines(offset, str.length());
	}

	/*
	 * Override to apply syntax highlighting after the document has been updated
	 */
	public void remove(int offset, int length) throws BadLocationException {
		/*
		 * if (getText(offset+length-1,1).equals(" ")){
		 * if(getLength()>offset+length) { String
		 * element=getText(offset+length,1); if (element.equals("[")) length--;
		 * } }
		 */
		super.remove(offset, length);

		if (coloration_activee)
			processChangedLines(offset, 0);
	}

	/*
	 * Determine how many lines have been changed, then apply highlighting to
	 * each line
	 */
	public void processChangedLines(int offset, int length)
			throws BadLocationException {
		String content = doc.getText(0, doc.getLength());

		// The lines affected by the latest document update

		int startLine = rootElement.getElementIndex(offset);
		int endLine = rootElement.getElementIndex(offset + length);

		// Do the actual highlighting

		for (int i = startLine; i <= endLine; i++) {
			applyHighlighting(content, i);
		}
	}

	/*
	 * Parse the line to determine the appropriate highlighting
	 */
	private void applyHighlighting(String content, int line)
			throws BadLocationException {
		int startOffset = rootElement.getElement(line).getStartOffset();
		int endOffset = rootElement.getElement(line).getEndOffset() - 1;
		int lineLength = endOffset - startOffset;
		int contentLength = content.length();

		if (endOffset > contentLength)
			endOffset = contentLength - 1;

		// set normal attributes for the line

		doc.setCharacterAttributes(startOffset, lineLength, normal, true);

		// check for single line comment
		// On enlève les éventuels commentaires
		int index = content.indexOf(getSingleLineDelimiter(), startOffset);
		while (index != -1) {
			if (index == 0) {
				break;
			} else if (!content.substring(index - 1, index).equals("\\")) {
				break;
			}
			index = content.indexOf(getSingleLineDelimiter(), index + 1);
		}

		if ((index > -1) && (index < endOffset)) {
			doc.setCharacterAttributes(index, endOffset - index + 1, comment,
					false);
			endOffset = index - 1;
		}

		// check for tokens
		colore(content, startOffset, endOffset);
	}

	protected boolean isOperator(char character) {
		String operands = "+-/*<=>&|";

		if (operands.indexOf(character) != -1)
			return true;
		else
			return false;
	}

	/*
	 * Override for other languages
	 */
	protected boolean isKeyword(String token) {
		token = token.toLowerCase();
		return Primitive.primitives.containsKey(token);
	}

	protected String getSingleLineDelimiter() {
		return "#";
	}

	public void Montre_Parenthese(int offset) {
		doc.setCharacterAttributes(offset, 1, parenthese, false);
	}

	public void setColore_Parenthese(boolean b) {
		colore_parenthese = b;
	}

	public void colore(String content, int startOffset, int endOffset) {
		int debut = startOffset;
		boolean nouveau_mot = true;
		boolean backslash = false;
		boolean mot = false;
		boolean variable = false;
		for (int i = startOffset; i < endOffset; i++) {
			try { // Sometimes, Exception happens on next line
				char c = content.charAt(i);
				if (c == ' ' || c == '(' || c == ')' || c == '[' || c == ']') {
					if (!backslash) {
						String element = content.substring(debut, i);
						if (mot) {
							doc.setCharacterAttributes(debut, i - debut, quote,
									false);
						} else if (variable) {
							doc.setCharacterAttributes(debut, i - debut, quote,
									false);
						} else if (isKeyword(element)) {
							doc.setCharacterAttributes(debut, i - debut,
									keyword, false);
						} else
							try {
								Double.parseDouble(element);
								doc.setCharacterAttributes(debut, i - debut,
										quote, false);
							} catch (NumberFormatException e) {
							}
						mot = false;
						variable = false;
						debut = i + 1;
						nouveau_mot = true;
						if (c != ' ') {
							if (colore_parenthese)
								doc.setCharacterAttributes(i, 1, parenthese,
										false);
							else {
								doc.setCharacterAttributes(i, 1, normal, false);
								// System.out.println(i+" normal "+StyleConstants.getFontFamily(normal)+" "+StyleConstants.isBold(normal));
							}
						}
					}
					backslash = false;
				} else if (nouveau_mot) {
					if (c == '\"') {
						mot = true;
						backslash = false;
						nouveau_mot = false;
					} else if (c == ':') {
						variable = true;
						backslash = false;
						nouveau_mot = false;
					} else if (isOperator(c)) {
						backslash = false;
						mot = false;
						variable = false;
						nouveau_mot = true;
						debut = i + 1;
						doc.setCharacterAttributes(i, 1, keyword, false);
					} else
						nouveau_mot = false;
				} else if (c == '\\') {
					if (!backslash) {
						backslash = true;
					} else
						backslash = false;
					nouveau_mot = false;
				} else if (isOperator(c)) {
					if (!mot) {
						String element = content.substring(debut, i);
						if (variable)
							doc.setCharacterAttributes(debut, i - debut, quote,
									false);
						else if (isKeyword(element))
							doc.setCharacterAttributes(debut, i - debut,
									keyword, false);
						else
							try {
								Double.parseDouble(element);
								doc.setCharacterAttributes(debut, i - debut,
										quote, false);
							} catch (NumberFormatException e) {
							}
						backslash = false;
						mot = false;
						variable = false;
						nouveau_mot = true;
						debut = i + 1;
						doc.setCharacterAttributes(i, 1, keyword, false);
					}
				}
			} catch (StringIndexOutOfBoundsException e22) {
			}
		}
		if (!nouveau_mot) {
			String element = content.substring(debut, endOffset);
			if (mot) {
				doc.setCharacterAttributes(debut, endOffset - debut, quote,
						false);
			} else if (variable) {
				doc.setCharacterAttributes(debut, endOffset - debut, quote,
						false);
			} else if (isKeyword(element)) {
				doc.setCharacterAttributes(debut, endOffset - debut, keyword,
						false);
			} else
				try {
					Double.parseDouble(element);
					doc.setCharacterAttributes(debut, endOffset - debut, quote,
							false);
				} catch (NumberFormatException e) {
				}
		}

	}

	public void insertStyleNormal(int offset, String str, AttributeSet a) {
		try {
			super.insertString(offset, str, a);
		} catch (BadLocationException e) {
		}
	}
}