1 package paolomind.multitalk.plugin;
2
3 import java.awt.event.KeyEvent;
4 import java.awt.event.MouseEvent;
5 import java.util.Iterator;
6 import java.util.Properties;
7
8 import paolomind.commons.NamedObject;
9 import paolomind.multitalk.netmessage.IMessage;
10 import paolomind.multitalk.netmessage.MessageException;
11 import paolomind.multitalk.netmessage.Sender;
12
13
14
15
16
17
18 public class ToolManager implements paolomind.multitalk.plugin.ToolInterface,
19 paolomind.commons.ObjectContainer, paolomind.multitalk.netmessage.Sender {
20
21
22 private String name;
23
24 private ToolInterface current;
25
26 private java.util.Map toolmap;
27
28
29 private Sender psender;
30
31
32
33
34
35
36
37 public ToolManager(final String pname) {
38 this(pname, null, new java.util.HashMap());
39 }
40
41
42
43
44
45
46
47
48
49 public ToolManager(final String pname, final Sender s) {
50 this(pname, s, new java.util.HashMap());
51 }
52
53
54
55
56
57
58
59 public ToolManager(final Sender s) {
60 this(null, s);
61 }
62
63
64
65
66
67
68
69
70
71
72
73 public ToolManager(final String pname, final Sender s, final java.util.Map m) {
74 toolmap = m;
75 psender = s;
76 this.name = pname;
77 }
78
79
80
81
82
83
84 public final void select() {
85 if (current != null) {
86 current.select();
87 }
88 }
89
90
91
92
93
94
95 public final String getSelfId() {
96 return name;
97 }
98
99
100
101
102
103
104
105
106 public final void mouseClicked(final MouseEvent e) {
107 if (current != null) {
108 current.mouseClicked(e);
109 }
110 }
111
112
113
114
115
116
117
118
119 public final void mouseEntered(final MouseEvent e) {
120 if (current != null) {
121 current.mouseEntered(e);
122 }
123 }
124
125
126
127
128
129
130
131
132 public final void mouseExited(final MouseEvent e) {
133 if (current != null) {
134 current.mouseExited(e);
135 }
136 }
137
138
139
140
141
142
143
144
145 public final void mousePressed(final MouseEvent e) {
146 if (current != null) {
147 current.mousePressed(e);
148 }
149 }
150
151
152
153
154
155
156
157
158 public final void mouseReleased(final MouseEvent e) {
159 if (current != null) {
160 current.mouseReleased(e);
161 }
162 }
163
164
165
166
167
168
169
170
171 public final void mouseDragged(final MouseEvent e) {
172 if (current != null) {
173 current.mouseDragged(e);
174 }
175 }
176
177
178
179
180
181
182
183
184 public final void mouseMoved(final MouseEvent e) {
185 if (current != null) {
186 current.mouseMoved(e);
187 }
188 }
189
190
191
192
193
194
195
196
197 public final void keyPressed(final KeyEvent e) {
198 if (current != null) {
199 current.keyPressed(e);
200 }
201 }
202
203
204
205
206
207
208
209
210 public final void keyReleased(final KeyEvent e) {
211 if (current != null) {
212 current.keyReleased(e);
213 }
214 }
215
216
217
218
219
220
221
222
223 public final void keyTyped(final KeyEvent e) {
224 if (current != null) {
225 current.keyTyped(e);
226 }
227 }
228
229
230
231
232
233
234
235
236
237
238 public final void receive(final IMessage m) throws MessageException {
239 Object info = m.getInfo();
240 if (info != null) {
241 ToolInterface t = (ToolInterface) toolmap.get(info);
242 if (t != null) {
243 t.receive(m);
244 } else {
245 this.receive(m);
246 }
247 } else {
248
249 throw new MessageException();
250 }
251 }
252
253
254
255
256
257
258
259
260 public final void send(final IMessage m) {
261 String s = this.getSelfId();
262 if (s != null) {
263 m.addInfo(s);
264 }
265 if (psender != null) {
266 psender.send(m);
267 }
268 }
269
270
271
272
273
274
275
276
277 public final void register(final NamedObject element) {
278 try {
279 this.register((ToolInterface) element);
280 } catch (ClassCastException e) {
281 throw new UnsupportedOperationException(
282 "non è possibile inserire oggetti che non siano ToolInterface", e);
283 }
284 }
285
286
287
288
289
290
291
292
293
294 public final void register(final String n, final ToolInterface t) {
295 toolmap.put(n, t);
296 t.setSelfId(n);
297 }
298
299
300
301
302
303
304
305 public final void register(final ToolInterface t) {
306 String s = t.getSelfId();
307 if (s != null) {
308 toolmap.put(s, t);
309 } else {
310 throw new NullPointerException("nome nullo per il NamedObject");
311 }
312 }
313
314
315
316
317
318
319
320
321
322
323 public final boolean select(final String pname) {
324 current = (ToolInterface) toolmap.get(pname);
325 return (current != null);
326 }
327
328
329
330
331
332
333
334 public final Properties getPropertes() {
335 if (current != null) {
336 return current.getPropertes();
337 }
338 return null;
339 }
340
341
342
343
344
345
346
347
348 public final void setSelfId(final String pname) {
349 this.name = pname;
350 }
351
352
353
354
355
356
357
358
359
360 public final Object get(final String pname) {
361 return toolmap.get(name);
362 }
363
364
365
366
367
368
369
370 public final Iterator getAll() {
371 return toolmap.values().iterator();
372 }
373 }