summaryrefslogtreecommitdiffstats
path: root/logo/src/xlogo/gui/components/ProcedureSearch.java
blob: 4c01accb84c2f4b0ed25bc0ad4604ce6f6a14f1e (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
/* 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 entirely written by Marko Zivkovic
 */

package xlogo.gui.components;

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import xlogo.interfaces.ProcedureMapper;
import xlogo.interfaces.ProcedureMapper.ProcedureMapListener;
import xlogo.utils.Utils;

public class ProcedureSearch extends JPanel
{
	private static final long	serialVersionUID	= 2275479142269992452L;

	private ProcedureMapper	procedureMapper		= null;

	private JComboBox	procedureSelection		= new JComboBox();
	private JButton		procedureSearchButton	= new JButton();
	
	public ProcedureSearch(ProcedureMapper procedureMapper)
	{
		this.procedureMapper = procedureMapper;
		setExecutables(procedureMapper.getAllProcedureNames());
		 initComponents();
		 //applyColorTheme();
		 initListeners();
	}

	private void initListeners()
	{
		procedureMapper.addProcedureMapListener(new ProcedureMapListener(){

			@Override
			public void ownerRenamed(String oldName, String newName)
			{
				if (SwingUtilities.isEventDispatchThread())
				{
					setExecutables(procedureMapper.getAllProcedureNames());
					return;
				}
				try
				{
					SwingUtilities.invokeAndWait(new Runnable(){
						
						@Override
						public void run()
						{
							setExecutables(procedureMapper.getAllProcedureNames());
						}
					});
				}
				catch (InterruptedException e)
				{
					ownerRenamed(oldName, newName);
				}
				catch (InvocationTargetException e)
				{
					e.printStackTrace();
				}
			}
		
			@Override
			public void defined(String fileName, Collection<String> procedures)
			{
				if (SwingUtilities.isEventDispatchThread())
				{
					setExecutables(procedureMapper.getAllProcedureNames());
					return;
				}
				try
				{
					SwingUtilities.invokeAndWait(new Runnable(){
						
						@Override
						public void run()
						{
							setExecutables(procedureMapper.getAllProcedureNames());
						}
					});
				}
				catch (InterruptedException e)
				{
					defined(fileName, procedures);
				}
				catch (InvocationTargetException e)
				{
					e.printStackTrace();
				}
			}

			@Override
			public void defined(String fileName, String procedure)
			{
				if (SwingUtilities.isEventDispatchThread())
				{
					setExecutables(procedureMapper.getAllProcedureNames());
					return;
				}
				try
				{
					SwingUtilities.invokeAndWait(new Runnable(){
						
						@Override
						public void run()
						{
							setExecutables(procedureMapper.getAllProcedureNames());
						}
					});
				}
				catch (InterruptedException e)
				{
					defined(fileName, procedure);
				}
				catch (InvocationTargetException e)
				{
					e.printStackTrace();
				}
			}

			@Override
			public void undefined(String fileName, Collection<String> procedures)
			{
				if (SwingUtilities.isEventDispatchThread())
				{
					setExecutables(procedureMapper.getAllProcedureNames());
					return;
				}
				try
				{
					SwingUtilities.invokeAndWait(new Runnable(){
						
						@Override
						public void run()
						{
							setExecutables(procedureMapper.getAllProcedureNames());
						}
					});
				}
				catch (InterruptedException e)
				{
					undefined(fileName, procedures);
				}
				catch (InvocationTargetException e)
				{
					e.printStackTrace();
				}
			}

			@Override
			public void undefined(String fileName, String procedure)
			{
				if (SwingUtilities.isEventDispatchThread())
				{
					setExecutables(procedureMapper.getAllProcedureNames());
					return;
				}
				try
				{
					SwingUtilities.invokeAndWait(new Runnable(){
						
						@Override
						public void run()
						{
							setExecutables(procedureMapper.getAllProcedureNames());
						}
					});
				}
				catch (InterruptedException e)
				{
					undefined(fileName, procedure);
				}
				catch (InvocationTargetException e)
				{
					e.printStackTrace();
				}
			}
		});
		
		procedureSearchButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0)
			{
				String selected = (String) procedureSelection.getSelectedItem();
				if (selected == null || selected.length() == 0)
					return;
				notifySearchListeners(selected);
			}
		});
		
		procedureSelection.getEditor().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            	String selected = (String) procedureSelection.getSelectedItem();
            	if (selected == null || selected.length() == 0)
					return;
            	notifySearchListeners(selected);
            }               
        });
	}
	
	private void setExecutables(Collection<String> procedures)
	{
		String[] sorted = new String[procedures.size()];
		sorted = procedures.toArray(sorted);
		Arrays.sort(sorted);
		
		procedureSelection.setModel(new DefaultComboBoxModel(sorted));
	}
	
	private void initComponents()
	{
		procedureSelection.setEditable(true);
		
		Image img = Toolkit.getDefaultToolkit().getImage(Utils.class.getResource("chercher.png"));
		procedureSearchButton.setIcon(new ImageIcon(img.getScaledInstance(20, 20, Image.SCALE_SMOOTH)));
	
		setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
	
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1;
		add(procedureSelection, c);
		
		c.gridx = 1;
		c.weightx = 0;
		add(procedureSearchButton, c);
	}
	
	public JComponent getComponent()
	{
		return this;
	}
	
	@Override
	public void setFont(Font font)
	{
		super.setFont(font);
		if (procedureSelection != null) // Apparently this became called before object construction...
			procedureSelection.setFont(font);
	}
	
	/*
	 * PROCEDURE SEARCH REQUEST LISTENER
	 */
	
	public interface ProcedureSearchRequestListener
	{
		public void requestProcedureSearch(String procedureName);
	}
	
	private ArrayList<ProcedureSearchRequestListener> searchListeners = new ArrayList<ProcedureSearchRequestListener>();
	
	public void addSearchRequestListener(ProcedureSearchRequestListener listener)
	{
		searchListeners.add(listener);
	}
	
	public void removeSearchRequestListener(ProcedureSearchRequestListener listener)
	{
		searchListeners.remove(listener);
	}

	private void notifySearchListeners(String procedureName)
	{
		for (ProcedureSearchRequestListener listener : searchListeners)
			listener.requestProcedureSearch(procedureName);
	}
}