summaryrefslogtreecommitdiffstats
path: root/logo/src/xlogo/kernel/Affichage.java
blob: 7da212044afea5ea3d749f5627fa5ec3903e195c (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
/* 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 
 */

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

import java.util.Stack;
import java.util.HashMap;
import java.awt.event.*;

import javax.swing.SwingUtilities;

import xlogo.Application;
import xlogo.messages.async.history.HistoryMessenger;
import xlogo.utils.Utils;
import xlogo.MemoryChecker;
import xlogo.Logo;

// Ce thread gère l'animation de la tortue pendant l'exécution
/**
 * This Thread is responsible of the turtle animation.
 * This animation has to be executed in a separated thread, else it will block
 * the event dispatcher thread
 */
public class Affichage extends Thread
{
	public static boolean	execution_lancee	= false;
	private boolean			pause				= false;
	private Application		cadre;
	private StringBuffer	instruction;
	private Souris			souris				= new Souris();
	private MemoryChecker	memoryChecker					= null;
	
	public Affichage()
	{
	}
	
	public Affichage(Application cadre, StringBuffer instruction)
	{
		this.cadre = cadre;
		this.instruction = instruction;
	}
	
	class Souris extends MouseAdapter
	{ // Si on déplace les Scrollbar pendant
		// le dessin
		public Souris()
		{
		} // Ceci permet d'interrompre momentanément l'exécution
		
		public void mousePressed(MouseEvent e)
		{
			pause = true;
		}
		
		public void mouseReleased(MouseEvent e)
		{
			pause = false;
		}
	}
	
	public void run()
	{
		// currentThread().setPriority(Thread.MIN_PRIORITY);
		SwingUtilities.invokeLater(new Runnable(){
			public void run()
			{
				cadre.setCommandLine(false);// la ligne de commandes
											// n'est plus active
			}
		});
		execution_lancee = true;
		cadre.getDrawPanel().active_souris(); // On active les événements souris
											// sur
		// la zone de dessin
		cadre.scrollArea.getVerticalScrollBar().addMouseListener(souris);
		cadre.scrollArea.getHorizontalScrollBar().addMouseListener(souris);
		try
		{
			cadre.setCar(-1);
			cadre.error = false;
			Interprete.operande = Interprete.operateur = Interprete.drapeau_ouvrante = false;
			cadre.getKernel().getInstructionBuffer().clear();
			Interprete.calcul = new Stack<String>();
			Interprete.nom = new Stack<String>();
			Interprete.locale = new HashMap<String, String>();
			Interprete.en_cours = new Stack<String>();
			memoryChecker = new MemoryChecker(cadre);
			memoryChecker.start();
			boolean b = true;
			while (b)
			{
				String st = cadre.getKernel().execute(instruction);
				if (!st.equals(""))
					throw new LogoError(Logo.messages.getString("error.whattodo") + " " + st + " ?");
				if (Interprete.actionInstruction.length() == 0)
					b = false;
				else
				{
					instruction = Interprete.actionInstruction;
					Interprete.actionInstruction = new StringBuffer();
				}
			}
		}
		catch (LogoError e)
		{
			// if (st.equals("siwhile")) st=Logo.messages.getString("tantque");
			while (!Interprete.en_cours.isEmpty() && Interprete.en_cours.peek().equals("("))
				Interprete.en_cours.pop();
			if (!cadre.error & !Interprete.en_cours.isEmpty())
			{
				HistoryMessenger.getInstance().dispatchError(
						Logo.messages.getString("dans") + " " + Interprete.en_cours.pop() + ", "
								+ Logo.messages.getString("line") + " " + getLineNumber() + ":\n");
			}
			if (!cadre.error)
				HistoryMessenger.getInstance().dispatchError(Utils.SortieTexte(e.getMessage()) + "\n");
			abortExecution();
		}
		cadre.setCommandLine(true);
		if (!cadre.viewer3DVisible())
			cadre.focus_Commande();
		execution_lancee = false;
		memoryChecker.kill();
		cadre.error = false;
		cadre.scrollArea.getVerticalScrollBar().removeMouseListener(souris);
		cadre.scrollArea.getHorizontalScrollBar().removeMouseListener(souris);
	}
	
	private void abortExecution()
	{
		cadre.focus_Commande();
		cadre.error = true;
		Interprete.calcul = new Stack<String>();
		cadre.getKernel().getInstructionBuffer().clear();
		Primitive.stackLoop = new Stack<LoopProperties>();
	}
	
	private int getLineNumber()
	{
		String string = Interprete.lineNumber;
		// System.out.println("bb"+string+"bb");
		if (string.equals(""))
			string = cadre.getKernel().getInstructionBuffer().toString();
		// System.out.println("cc"+string+"cc");
		int id = string.indexOf("\\l");
		if (id != -1)
		{
			String lineNumber = "";
			int i = id + 2;
			char c = string.charAt(i);
			while (c != ' ')
			{
				lineNumber += c;
				i++;
				c = string.charAt(i);
			}
			// System.out.println(lineNumber);
			return Integer.parseInt(lineNumber);
		}
		return 1;
	}
	
	protected boolean isOnPause()
	{
		return pause;
	}
	
	/**
	 * @param b
	 * @uml.property name="pause"
	 */
	public void setPause(boolean b)
	{
		pause = b;
	}
}