summaryrefslogtreecommitdiffstats
path: root/logo/src/xlogo/gui/Editor.java
blob: e6f0e3b554a02e1d9dab67573d59aa5184f950df (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
/* 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.gui;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import xlogo.storage.WSManager;
import xlogo.storage.user.UserConfig;
import xlogo.storage.workspace.WorkspaceConfig;
import xlogo.utils.Utils;
import xlogo.Application;
import xlogo.kernel.userspace.UserSpace;
import xlogo.messages.MessageKeys;
import xlogo.messages.async.dialog.DialogMessenger;
import xlogo.Logo;

/**
 * Title : XLogo Description : XLogo is an interpreter for the Logo programming
 * language
 * <p>
 * <p>
 * Changes made in July 2013 by Marko Zivkovic: All the procedure analyzing is
 * moved to class 'Workspace' which was then renamed to {@link UserSpace}.
 * Reason: This is a GUI component and it should not do much model manipulation
 * things. As often in XLogo this was an issue of weak separation of concerns,
 * leading to bad modifiability, extensibility, changeability, and
 * maintainability. Better have the Workspace deal with all the workspace
 * management. For the editor, it's enough to edit text, do syntax highlighting,
 * and provide the input text to some other specialized class.
 * 
 * @author Loïc Le Coq, Marko Zivkovic
 * 
 */

/*
 * The main class for the Editor windows
 */
public class Editor implements ActionListener
{
	private JPanel				mainPanel	= new JPanel();
	private JPanel				menu		= new JPanel();
	private JButton				chercher, undo, redo;
	private JScrollPane			scroll;
	private EditorTextZone		textZone;
	// private ZoneEdition zonedition;
	
	private Application			app;
	private ReplaceFrame		sf;
	UserConfig					uc;
	
	private KeyListener logoTextAnalyzerTrigger;
	//private ArrayList<ProcedureErrorMessage> errors = new ArrayList<ProcedureErrorMessage>();

	
	public Editor(Application app)
	{
		super();
		mainPanel.setOpaque(true);
		this.app = app;
		uc = WSManager.getUserConfig();
				
		try
		{
			initGui();
		}
		catch (Exception e)
		{
			DialogMessenger.getInstance().dispatchError(
					Logo.messages.getString(MessageKeys.GENERAL_ERROR_TITLE),
					Logo.messages.getString(MessageKeys.ERROR_WHILE_CREATING_EDITOR));
		}
		
	}
	
	public String getText()
	{
		return textZone.getText();
	}
	
	public JComponent getComponent()
	{
		return mainPanel;
	}
	
	/**
	 * Set the text of this component and displays it. <br>
	 * If the Editor was already open, it will store the old file and then
	 * display the new file.
	 * 
	 * @param file
	 */
	public void setText(String text)
	{
		textZone.clearText();
		setEditorStyledText(text);
		textZone.requestFocus();
	}
		
	/**
	 * @author Marko Zivkovic
	 */
	public void displayProcedure(String procedureName)
	{
		String to = Logo.messages.getString("pour");
		if (!textZone.find(to + " " + procedureName, false))
			textZone.find(to + " " + procedureName, true);
		
	}
		
	private void initGui() throws Exception
	{	
		WorkspaceConfig wc = WSManager.getWorkspaceConfig();
		
		// Init All other components
		scroll = new JScrollPane();
		if (wc.isSyntaxHighlightingEnabled())
		{
			textZone = new EditorTextPane(this);
			textZone.getTextComponent().addKeyListener(logoTextAnalyzerTrigger);
		}
		else
			textZone = new EditorTextArea(this);
		
		sf = new ReplaceFrame(app.getFrame(), textZone);
		
		scroll.setPreferredSize(new Dimension(500, 500));
		scroll.getViewport().add(textZone.getTextComponent(), null);
		sf = new ReplaceFrame(app.getFrame(), textZone);
		
		applyLayout();
		initToolbar();
	}
	
	private void applyLayout()
	{
		GroupLayout groupLayout = new GroupLayout(mainPanel);
		mainPanel.setLayout(groupLayout);
		
		groupLayout.setAutoCreateContainerGaps(true);
		groupLayout.setAutoCreateGaps(true);
		
		groupLayout.setVerticalGroup(
				groupLayout.createSequentialGroup()
					.addComponent(menu)
					.addComponent(scroll));
		
		groupLayout.setHorizontalGroup(
				groupLayout.createParallelGroup()
					.addComponent(menu)
					.addComponent(scroll));
		
		FlowLayout flowLayout = new FlowLayout();
		menu.setLayout(flowLayout);
		mainPanel.add(menu);
		mainPanel.add(scroll);
	}
	
	/*
	 * Below everything is inherited from XLogo, except for minor changes due to refactoring
	 */
	
	
	private void initToolbar()
	{
		menu.setMaximumSize(new Dimension(Integer.MAX_VALUE, 40));
		
		chercher = getIconButton("chercher.png","find");
		undo = getIconButton("undo.png","editor.undo");
		redo = getIconButton("redo.png","editor.redo");
		
		undo.setEnabled(false);
		redo.setEnabled(false);
		
		menu.add(chercher);
		menu.add(undo);
		menu.add(redo);
		
	}
	
	private JButton getIconButton(String iconName, String description)
	{
		JButton btn = new JButton();
		if (iconName != null)
			btn.setIcon(Utils.dimensionne_image(iconName, mainPanel));
		
		btn.setToolTipText(Logo.messages.getString(description));
		btn.setActionCommand(Logo.messages.getString(description));
		btn.addActionListener(this);
		return btn;
	}
	
	public void actionPerformed(ActionEvent e)
	{		
		String cmd = e.getActionCommand();
		if (cmd.equals(Logo.messages.getString("find")))
		{
			if (!sf.isVisible())
			{
				sf.setSize(350, 350);
				sf.setVisible(true);
			}
		}
		// Undo Action
		else if (cmd.equals(Logo.messages.getString("editor.undo")))
		{
			textZone.getUndoManager().undo();
			updateUndoRedoButtons();
		}
		// Redo Action
		else if (cmd.equals(Logo.messages.getString("editor.redo")))
		{
			textZone.getUndoManager().redo();
			updateUndoRedoButtons();
		}
	}
	
	// Change Syntax Highlighting for the editor
	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();
		
		if (textZone.supportHighlighting())
		{
			((EditorTextPane) textZone).getDsd().initStyles(wc.getCommentColor(), wc.getCommentStyle(),
					wc.getPrimitiveColor(), wc.getPrimitiveStyle(), wc.getBraceColor(),
					wc.getBraceStyle(), wc.getOperatorColor(), wc.getOperatorStyle());
		}
	}
	
	// Enable or disable Syntax Highlighting
	/*
	 * public void setColoration(boolean b){ if (textZone.supportHighlighting())
	 * ((EditorTextPane)textZone).getDsd().setColoration(b); }
	 */
	public void setEditorFont(Font f)
	{
		textZone.setFont(f);
	}
	
	/**
	 * Erase all text
	 */
	public void clearText()
	{
		textZone.clearText();
	}
	
	/**
	 * Convert the textZone from a JTextArea to a JTextPane To allow Syntax
	 * Highlighting
	 */
	public void toTextPane()
	{
		textZone.getTextComponent().removeKeyListener(logoTextAnalyzerTrigger);
		scroll.getViewport().removeAll();// .remove(textZone.getTextComponent());
		String s = textZone.getText();
		textZone = new EditorTextPane(this);
		sf = new ReplaceFrame(app.getFrame(), textZone);
		textZone.ecris(s);
		textZone.getTextComponent().addKeyListener(logoTextAnalyzerTrigger);
		scroll.getViewport().add(textZone.getTextComponent());
		scroll.revalidate();
	}
	
	/**
	 * Convert the textZone from a JTextPane to a JTextArea Cause could be that:
	 * - Syntax Highlighting is disabled - Large text to display in the editor
	 */
	public void toTextArea()
	{
		/*
		 * Marko : for large text or disabled highlighting, we also don't want error messages to be produced while typing.
		 * 
		 */
		textZone.getTextComponent().removeKeyListener(logoTextAnalyzerTrigger); 
		String s = textZone.getText();
		scroll.getViewport().removeAll();// .remove(textZone.getTextComponent());
		textZone = new EditorTextArea(this);
		sf = new ReplaceFrame(app.getFrame(), textZone);
		textZone.ecris(s);
		scroll.getViewport().add(textZone.getTextComponent());
		scroll.revalidate();
	}
	
	/**
	 * Inserts the text at the current caret position.
	 * 
	 * @param txt
	 */
	public void setEditorStyledText(String txt)
	{
		if (txt.length() < 100000)
		{
			textZone.ecris(txt);
		}
		else
		{
			if (textZone instanceof EditorTextPane)
			{
				WSManager.getWorkspaceConfig().setSyntaxHighlightingEnabled(false);
				toTextArea();
				textZone.ecris(txt);
			}
			else
				textZone.ecris(txt);
		}
	}
	
	/**
	 * append a procedure to the end of the document.
	 * 
	 * @param program
	 * @author Marko Zivkovic
	 */
	public void append(String procedure)
	{
		if (procedure.length() < 100000)
		{
			textZone.append(procedure);
		}
		else
		{
			if (textZone instanceof EditorTextPane)
			{
				WSManager.getWorkspaceConfig().setSyntaxHighlightingEnabled(false);
				toTextArea();
				textZone.append(procedure);
			}
			else
				textZone.append(procedure);
		}
	}
	
	public void requestFocus()
	{
		textZone.requestFocus();
	}
		
	public void discardAllEdits()
	{
		textZone.getUndoManager().discardAllEdits();
		updateUndoRedoButtons();
	}
	
	protected void updateUndoRedoButtons()
	{
		if (textZone.getUndoManager().canRedo())
			redo.setEnabled(true);
		else
			redo.setEnabled(false);
		if (textZone.getUndoManager().canUndo())
			undo.setEnabled(true);
		else
			undo.setEnabled(false);
	}

		
}