summaryrefslogtreecommitdiffstats
path: root/logo/src/xlogo/gui/ZoneEdition.java
blob: afbda11b59cfcd462557c17ba7823f1951d289d5 (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
/* XLogo4Schools - A Logo Interpreter specialized for use in schools, based on XLogo by Lo�c 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 Z�rich,
 * in the year 2013 and/or during future work.
 * 
 * It is a reengineered version of XLogo written by Lo�c Le Coq, published
 * under the GPL License at http://xlogo.tuxfamily.org/
 * 
 * Contents of this file were initially written by Lo�c Le Coq,
 * modifications, extensions, refactorings might have been applied by Marko Zivkovic 
 */

package xlogo.gui;

import javax.swing.event.*;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.SwingUtilities;

import xlogo.StyledDocument.DocumentLogo;
import xlogo.storage.WSManager;

/**
 * Title :        XLogo
 * Description :  XLogo is an interpreter for the Logo 
 * 						programming language
 * @author Loïc Le Coq
 */
class ZoneEdition extends JTextPane implements CaretListener{
	private static final long serialVersionUID = 1L;
	private DocumentLogo dsd=null;
	// Si la correspondance entre parenthese ou crochets est activée
	private boolean active=false;
	// Dernière position allumée
	private int[] position=new int[2];
	

//	public DocumentLogo getDsd(){
//		return dsd;
//	}
	public void setActive(boolean b){
		active=b;
	}
	ZoneEdition(EditorTextPane etp){
	dsd=etp.getDsd();
	addCaretListener(this);
}

// Teste si le caractère précédent est un backslash
private boolean TesteBackslash(String content,int pos){
	String caractere="";
	if (pos>0) caractere=content.substring(pos-1,pos);
	//System.out.println(caractere);
	if (caractere.equals("\\")) return true;
	return false;
}

class verif_parenthese implements Runnable{
	int pos;
	verif_parenthese(int pos){
	this.pos=pos;	
	}
	public void run(){
		if (active){
			active=false;
			int debut=position[0];
			int fin=position[0];
			if (debut!=-1){
				if (debut>0) debut--;
				if (fin<dsd.getLength()) fin++;
				try{			
					String content=dsd.getText(0,dsd.getLength());
					dsd.colore(content,debut,fin);	
				}
				catch(BadLocationException e){}
			}
			debut=position[1];
			fin=position[1];
			if (debut!=-1){
				if (debut>0) debut--;
				if (fin<dsd.getLength()) debut++;
				if (fin<dsd.getLength()) fin++;
				try{			
					String content=dsd.getText(0,dsd.getLength());
					dsd.colore(content,debut,fin);	
				}
				catch(BadLocationException e){}
			}
		}
		int length=dsd.getLength();
		try{
			String content=dsd.getText(pos,length-pos);
			int id=-1;
			if (length>pos)	{
				id="[]()".indexOf(content.substring(0,1));
			}
			if (id>-1&&!TesteBackslash(dsd.getText(0,pos),pos)){
				active=true;
				switch(id){
				case 0: 
					chercheApres(content,pos,"[","]");
					break;
				case 1:
					content=getText(0,pos);
					chercheAvant(content,pos,"[","]");
				break;
				case 2:
					chercheApres(content,pos,"(",")");
				break;
				case 3:
					content=getText(0,pos);
					chercheAvant(content,pos,"(",")");
				break;
				}
			}
		}
		catch(BadLocationException e1){}

	}
}
public void caretUpdate(CaretEvent e){
	int pos=e.getDot();
	if (WSManager.getWorkspaceConfig().isSyntaxHighlightingEnabled()) SwingUtilities.invokeLater(new verif_parenthese(pos));
}
	void chercheApres(String content,int pos,String ouv,String fer){
		boolean continuer=true;
		int of_ouvrant;
		int of_fermant=0;
		int from_index_ouvrant=1;
		int from_index_fermant=1;
		while(continuer){
			of_ouvrant=content.indexOf(ouv,from_index_ouvrant);
			while (of_ouvrant!=-1&&TesteBackslash(content,of_ouvrant)) of_ouvrant=content.indexOf(ouv,of_ouvrant+1);
			of_fermant=content.indexOf(fer,from_index_fermant);
			while (of_fermant!=-1&&TesteBackslash(content,of_fermant)) of_fermant=content.indexOf(fer,of_fermant+1);
			if (of_fermant==-1) break;
			if (of_ouvrant!=-1 && of_ouvrant<of_fermant) {
				from_index_ouvrant=of_ouvrant+1;
				from_index_fermant=of_fermant+1;
			}
			else continuer=false;;
		}
		if (of_fermant!=-1) {
			dsd.Montre_Parenthese(of_fermant+pos);
			position[1]=of_fermant+pos;
		}
		else position[1]=-1;
		dsd.Montre_Parenthese(pos);
		position[0]=pos;
	}
	void chercheAvant(String content,int pos,String ouv,String fer){
		boolean continuer=true;
		int of_fermant=0;
		int of_ouvrant=0;
		int from_index_ouvrant=pos;
		int from_index_fermant=pos;
		while(continuer){
			of_ouvrant=content.lastIndexOf(ouv,from_index_ouvrant);
			while (of_ouvrant!=-1&&TesteBackslash(content,of_ouvrant)) of_ouvrant=content.lastIndexOf(ouv,of_ouvrant-1);			
			of_fermant=content.lastIndexOf(fer,from_index_fermant);
			while (of_fermant!=-1&&TesteBackslash(content,of_fermant)) of_fermant=content.lastIndexOf(fer,of_fermant-1);
			if (of_ouvrant==-1) break;
			if (of_ouvrant<of_fermant) {
				from_index_ouvrant=of_ouvrant-1;
				from_index_fermant=of_fermant-1;
			}
			else continuer=false;;
		}
		if (of_ouvrant!=-1) {
			dsd.Montre_Parenthese(of_ouvrant);
			position[0]=of_ouvrant;
		}
		else position[0]=-1;
		dsd.Montre_Parenthese(pos);
		position[1]=pos;
	
	
	}

}