1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
package tsukuba_bunko.peko.canvas.select; |
20 |
|
|
21 |
|
import java.awt.Dimension; |
22 |
|
|
23 |
|
import java.awt.event.KeyEvent; |
24 |
|
import java.awt.event.KeyListener; |
25 |
|
|
26 |
|
import java.util.List; |
27 |
|
import java.util.Map; |
28 |
|
|
29 |
|
import javax.swing.JComponent; |
30 |
|
|
31 |
|
import tsukuba_bunko.peko.ActionControler; |
32 |
|
import tsukuba_bunko.peko.Logger; |
33 |
|
import tsukuba_bunko.peko.PekoSystem; |
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
public class SelectCanvas extends JComponent implements KeyListener { |
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
private static final long serialVersionUID = 103943516971774006L; |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
public static final int ALIGN_LEFT = 0; |
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
public static final int ALIGN_CENTER = 1; |
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
public static final int ALIGN_RIGHT = 2; |
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
public static final int VALIGN_TOP = 0; |
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
public static final int VALIGN_MIDDLE = 1; |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
public static final int VALIGN_BOTTOM = 2; |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
0 |
private boolean _active = false; |
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
0 |
private List _buttons = null; |
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
0 |
private String _id = null; |
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
0 |
private SelectItemButton _selected = null; |
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
0 |
private int _selectedIndex = -1; |
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
0 |
private int _lastKeyCode = -1; |
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
0 |
private int _align = SelectCanvas.ALIGN_CENTER; |
115 |
|
|
116 |
|
|
117 |
|
|
118 |
0 |
private int _verticalAlign = SelectCanvas.VALIGN_TOP; |
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
0 |
private int _columns = 1; |
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
|
128 |
0 |
private int _columnSpan = 20; |
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
133 |
0 |
private int _rowSpan = 20; |
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
0 |
private int _boundSpan = 20; |
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
|
143 |
0 |
private Map _buttonStyle = null; |
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
|
148 |
0 |
private Dimension _dimensionCache = null; |
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
public SelectCanvas() |
155 |
|
{ |
156 |
0 |
super(); |
157 |
0 |
} |
158 |
|
|
159 |
|
|
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
public void setAlignment( int align ) |
166 |
|
{ |
167 |
0 |
if( (align == SelectCanvas.ALIGN_CENTER) || (align == SelectCanvas.ALIGN_LEFT) || (align == SelectCanvas.ALIGN_RIGHT) ) { |
168 |
0 |
_align = align; |
169 |
0 |
} |
170 |
|
else { |
171 |
0 |
throw new IllegalArgumentException( "invalid alignment type is specified." ); |
172 |
|
} |
173 |
0 |
} |
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
|
179 |
|
public int getAlignment() |
180 |
|
{ |
181 |
0 |
return _align; |
182 |
|
} |
183 |
|
|
184 |
|
|
185 |
|
|
186 |
|
|
187 |
|
|
188 |
|
|
189 |
|
public void setVerticalAlignment( int valign ) |
190 |
|
{ |
191 |
0 |
if( (valign == SelectCanvas.ALIGN_CENTER) || (valign == SelectCanvas.ALIGN_LEFT) || (valign == SelectCanvas.ALIGN_RIGHT) ) { |
192 |
0 |
_verticalAlign = valign; |
193 |
0 |
} |
194 |
|
else { |
195 |
0 |
throw new IllegalArgumentException( "invalid vertical alignment type is specified." ); |
196 |
|
} |
197 |
0 |
} |
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
|
203 |
|
public int getVericalAlignment() |
204 |
|
{ |
205 |
0 |
return _verticalAlign; |
206 |
|
} |
207 |
|
|
208 |
|
|
209 |
|
|
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
public void setColumns( int columns ) |
214 |
|
{ |
215 |
0 |
_columns = columns; |
216 |
0 |
} |
217 |
|
|
218 |
|
|
219 |
|
|
220 |
|
|
221 |
|
|
222 |
|
public int getColumns() |
223 |
|
{ |
224 |
0 |
return _columns; |
225 |
|
} |
226 |
|
|
227 |
|
|
228 |
|
|
229 |
|
|
230 |
|
|
231 |
|
public void setColumnSpan( int span ) |
232 |
|
{ |
233 |
0 |
_columnSpan = span; |
234 |
0 |
} |
235 |
|
|
236 |
|
|
237 |
|
|
238 |
|
|
239 |
|
|
240 |
|
public int getColumnSpan() |
241 |
|
{ |
242 |
0 |
return _columnSpan; |
243 |
|
} |
244 |
|
|
245 |
|
|
246 |
|
|
247 |
|
|
248 |
|
|
249 |
|
public void setRowSpan( int span ) |
250 |
|
{ |
251 |
0 |
_rowSpan = span; |
252 |
0 |
} |
253 |
|
|
254 |
|
|
255 |
|
|
256 |
|
|
257 |
|
|
258 |
|
public int getRowSpan() |
259 |
|
{ |
260 |
0 |
return _rowSpan; |
261 |
|
} |
262 |
|
|
263 |
|
|
264 |
|
|
265 |
|
|
266 |
|
|
267 |
|
public void setBoundSpan( int span ) |
268 |
|
{ |
269 |
0 |
_boundSpan = span; |
270 |
0 |
} |
271 |
|
|
272 |
|
|
273 |
|
|
274 |
|
|
275 |
|
|
276 |
|
public int getBoundSpan() |
277 |
|
{ |
278 |
0 |
return _boundSpan; |
279 |
|
} |
280 |
|
|
281 |
|
|
282 |
|
|
283 |
|
|
284 |
|
|
285 |
|
public void setButtonStyle( Map styles ) |
286 |
|
{ |
287 |
0 |
_buttonStyle = styles; |
288 |
0 |
} |
289 |
|
|
290 |
|
|
291 |
|
|
292 |
|
|
293 |
|
|
294 |
|
public Map getButtonStyle() |
295 |
|
{ |
296 |
0 |
return _buttonStyle; |
297 |
|
} |
298 |
|
|
299 |
|
|
300 |
|
|
301 |
|
|
302 |
|
|
303 |
|
|
304 |
|
public String select( List selectItems ) |
305 |
|
{ |
306 |
0 |
return select( selectItems, -1 ); |
307 |
|
} |
308 |
|
|
309 |
|
|
310 |
|
|
311 |
|
|
312 |
|
|
313 |
|
|
314 |
|
|
315 |
|
public String select( List selectItems, long timeout ) |
316 |
|
{ |
317 |
0 |
if( _buttonStyle == null ) { |
318 |
0 |
throw new IllegalStateException( "button styles is not specified." ); |
319 |
|
} |
320 |
|
|
321 |
0 |
if( _active ) { |
322 |
0 |
Logger.debug( "[canvas.select] already show selection." ); |
323 |
0 |
return null; |
324 |
|
} |
325 |
|
|
326 |
0 |
synchronized( this ) { |
327 |
0 |
_id = null; |
328 |
|
|
329 |
0 |
Dimension canvasSize = getSize( _dimensionCache ); |
330 |
|
|
331 |
0 |
int size = selectItems.size(); |
332 |
0 |
int buttonWidth = 320; |
333 |
0 |
Integer intValue = (Integer)_buttonStyle.get( SelectItemButton.STYLE_WIDTH ); |
334 |
0 |
if( intValue != null ) { |
335 |
0 |
buttonWidth = intValue.intValue(); |
336 |
|
} |
337 |
|
|
338 |
0 |
setVisible( false ); |
339 |
|
|
340 |
0 |
SelectItem item = null; |
341 |
0 |
SelectItemButton button = null; |
342 |
0 |
List buttons = new java.util.ArrayList( size ); |
343 |
0 |
int x = 0; |
344 |
0 |
int y = 0; |
345 |
0 |
int rowHeight = 0; |
346 |
0 |
Dimension buttonSize = new Dimension( 0, 0 ); |
347 |
0 |
for( int i = 0; i < size; ++i ) { |
348 |
0 |
if( (i % _columns) == 0 ) { |
349 |
0 |
y += rowHeight; |
350 |
0 |
if( item != null ) { |
351 |
0 |
y += _rowSpan; |
352 |
|
} |
353 |
0 |
x = 0; |
354 |
0 |
rowHeight = 0; |
355 |
0 |
} |
356 |
|
else { |
357 |
0 |
x += (buttonSize.width + _columnSpan); |
358 |
|
} |
359 |
|
|
360 |
0 |
item = (SelectItem)selectItems.get( i ); |
361 |
0 |
button = new SelectItemButton( this ); |
362 |
0 |
button.setSelectItem( item ); |
363 |
0 |
button.prepare( _buttonStyle ); |
364 |
0 |
buttons.add( button ); |
365 |
|
|
366 |
0 |
add( button ); |
367 |
0 |
button.setLocation( x, y ); |
368 |
0 |
buttonSize = button.getSize( buttonSize ); |
369 |
0 |
if( buttonSize.height > rowHeight ) { |
370 |
0 |
rowHeight = buttonSize.height; |
371 |
|
} |
372 |
|
} |
373 |
|
|
374 |
0 |
int areaWidth = (buttonWidth * _columns) + (_columnSpan * (_columns - 1)); |
375 |
0 |
int leftPosition = 0; |
376 |
0 |
if( areaWidth > canvasSize.width ) { |
377 |
0 |
leftPosition = 0; |
378 |
0 |
} |
379 |
0 |
else if( (areaWidth + _boundSpan) > canvasSize.width ) { |
380 |
0 |
leftPosition = (canvasSize.width - areaWidth) / 2; |
381 |
0 |
} |
382 |
0 |
else if( _align == SelectCanvas.ALIGN_CENTER ) { |
383 |
0 |
leftPosition = (canvasSize.width - areaWidth) / 2; |
384 |
0 |
} |
385 |
0 |
else if( _align == SelectCanvas.ALIGN_LEFT ) { |
386 |
0 |
leftPosition = _boundSpan; |
387 |
0 |
} |
388 |
0 |
else if( _align == SelectCanvas.ALIGN_RIGHT ) { |
389 |
0 |
leftPosition = canvasSize.width - areaWidth - _boundSpan; |
390 |
|
} |
391 |
|
|
392 |
0 |
int areaHeight = y + rowHeight; |
393 |
0 |
int topPosition = 0; |
394 |
0 |
if( areaHeight > canvasSize.height ) { |
395 |
0 |
topPosition = 0; |
396 |
0 |
} |
397 |
0 |
else if( (areaHeight + _boundSpan) > canvasSize.height ) { |
398 |
0 |
topPosition = (canvasSize.height - areaHeight) / 2; |
399 |
0 |
} |
400 |
0 |
else if( _verticalAlign == SelectCanvas.VALIGN_TOP ) { |
401 |
0 |
topPosition = _boundSpan; |
402 |
0 |
} |
403 |
0 |
else if( _verticalAlign == SelectCanvas.VALIGN_MIDDLE ) { |
404 |
0 |
topPosition = (canvasSize.height - areaHeight) / 2; |
405 |
0 |
} |
406 |
0 |
else if( _verticalAlign == SelectCanvas.VALIGN_BOTTOM ) { |
407 |
0 |
topPosition = canvasSize.height - areaHeight - _boundSpan; |
408 |
|
} |
409 |
0 |
setLocation( leftPosition, topPosition ); |
410 |
|
|
411 |
0 |
_buttons = buttons; |
412 |
0 |
_active = true; |
413 |
|
|
414 |
0 |
setVisible( true ); |
415 |
0 |
} |
416 |
|
|
417 |
0 |
Logger.debug( "[canvas.select] wait for selecting." ); |
418 |
0 |
ActionControler controler = PekoSystem.getInstance().getActionControler(); |
419 |
0 |
if( timeout > 0 ) { |
420 |
0 |
controler.stop( timeout ); |
421 |
0 |
} |
422 |
|
else { |
423 |
0 |
controler.stop(); |
424 |
|
} |
425 |
|
|
426 |
0 |
synchronized( this ) { |
427 |
0 |
int size = _buttons.size(); |
428 |
0 |
for( int i = 0; i < size; ++i ) { |
429 |
0 |
remove( (SelectItemButton)_buttons.get(i) ); |
430 |
|
} |
431 |
0 |
_buttons.clear(); |
432 |
0 |
_buttons = null; |
433 |
0 |
} |
434 |
|
|
435 |
0 |
Logger.debug( "[canvas.select] id: " + _id ); |
436 |
0 |
return _id; |
437 |
|
} |
438 |
|
|
439 |
|
|
440 |
|
|
441 |
|
|
442 |
|
public void cancel() |
443 |
|
{ |
444 |
0 |
_active = false; |
445 |
0 |
synchronized( this ) { |
446 |
0 |
if( _buttons != null ) { |
447 |
0 |
_id = null; |
448 |
0 |
ActionControler controler = PekoSystem.getInstance().getActionControler(); |
449 |
0 |
controler.start(); |
450 |
0 |
Logger.debug( "[cenvas.select] canceled." ); |
451 |
|
} |
452 |
0 |
} |
453 |
0 |
} |
454 |
|
|
455 |
|
|
456 |
|
|
457 |
|
|
458 |
|
|
459 |
|
|
460 |
|
|
461 |
|
|
462 |
|
|
463 |
|
public void itemSelecting( SelectItemButton button ) |
464 |
|
{ |
465 |
0 |
SelectItemButton old = _selected; |
466 |
0 |
_selected = button; |
467 |
0 |
if( (old != null) && (old != button) ) { |
468 |
0 |
old.setSelected( false ); |
469 |
|
} |
470 |
0 |
_selectedIndex = _buttons.indexOf( button ); |
471 |
0 |
} |
472 |
|
|
473 |
|
|
474 |
|
|
475 |
|
|
476 |
|
|
477 |
|
public void itemDeselected( SelectItemButton button ) |
478 |
|
{ |
479 |
0 |
if( _selected == button ) { |
480 |
0 |
_selected = null; |
481 |
0 |
_selectedIndex = -1; |
482 |
|
} |
483 |
0 |
} |
484 |
|
|
485 |
|
|
486 |
|
|
487 |
|
|
488 |
|
|
489 |
|
public void itemSelected( SelectItemButton button ) |
490 |
|
{ |
491 |
0 |
if( _active ) { |
492 |
0 |
synchronized( this ) { |
493 |
0 |
if( _active ) { |
494 |
0 |
_selected = null; |
495 |
0 |
_selectedIndex = -1; |
496 |
0 |
_id = button.getSelectItem().getID(); |
497 |
0 |
_active = false; |
498 |
0 |
Logger.debug( "[canvas.select] selected: " + _id ); |
499 |
0 |
ActionControler controler = PekoSystem.getInstance().getActionControler(); |
500 |
0 |
controler.start(); |
501 |
|
} |
502 |
0 |
} |
503 |
|
} |
504 |
0 |
} |
505 |
|
|
506 |
|
|
507 |
|
|
508 |
|
|
509 |
|
|
510 |
|
public void keyPressed( KeyEvent ev ) |
511 |
|
{ |
512 |
0 |
if( _active ) { |
513 |
0 |
int code = ev.getKeyCode(); |
514 |
0 |
if( _lastKeyCode == -1 ) { |
515 |
0 |
_lastKeyCode = code; |
516 |
0 |
} |
517 |
0 |
else if( _lastKeyCode != code ) { |
518 |
0 |
_lastKeyCode = -1; |
519 |
|
} |
520 |
|
|
521 |
0 |
int index = _selectedIndex; |
522 |
0 |
int size = _buttons.size(); |
523 |
|
|
524 |
0 |
if( code == KeyEvent.VK_DOWN ) { |
525 |
0 |
index = (index + 1) % size; |
526 |
0 |
} |
527 |
0 |
else if( (code == KeyEvent.VK_RIGHT) && (_columns > 1) ) { |
528 |
0 |
index = (index + 1) % size; |
529 |
0 |
} |
530 |
0 |
else if( code == KeyEvent.VK_UP ) { |
531 |
0 |
index--; |
532 |
0 |
if( index < 0 ) { |
533 |
0 |
index = _buttons.size() -1 ; |
534 |
0 |
} |
535 |
|
} |
536 |
0 |
else if( (code == KeyEvent.VK_LEFT) && (_columns > 1) ) { |
537 |
0 |
index--; |
538 |
0 |
if( index < 0 ) { |
539 |
0 |
index = _buttons.size() -1 ; |
540 |
0 |
} |
541 |
|
} |
542 |
|
else { |
543 |
0 |
return; |
544 |
|
} |
545 |
0 |
SelectItemButton button = (SelectItemButton)_buttons.get( index ); |
546 |
0 |
button.setSelected( true ); |
547 |
|
} |
548 |
0 |
} |
549 |
|
|
550 |
|
public void keyReleased( KeyEvent ev ) |
551 |
|
{ |
552 |
0 |
if( !_active || (_buttons == null) ) { |
553 |
0 |
return; |
554 |
|
} |
555 |
|
|
556 |
0 |
int code = ev.getKeyCode(); |
557 |
0 |
if( code != _lastKeyCode ) { |
558 |
0 |
_lastKeyCode = -1; |
559 |
0 |
return; |
560 |
|
} |
561 |
|
|
562 |
0 |
if( (code == KeyEvent.VK_ENTER) && (_selected != null) ) { |
563 |
0 |
itemSelected( _selected ); |
564 |
|
} |
565 |
0 |
_lastKeyCode = -1; |
566 |
0 |
} |
567 |
|
|
568 |
|
public void keyTyped( KeyEvent ev ) |
569 |
|
{ |
570 |
0 |
} |
571 |
|
} |