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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
|
/* *************************************************************************
IT Mill Toolkit
Development of Browser User Interfaces Made Easy
Copyright (C) 2000-2006 IT Mill Ltd
*************************************************************************
This product is distributed under commercial license that can be found
from the product package on license.pdf. Use of this product might
require purchasing a commercial license from IT Mill Ltd. For guidelines
on usage, see licensing-guidelines.html
*************************************************************************
For more information, contact:
IT Mill Ltd phone: +358 2 4802 7180
Ruukinkatu 2-4 fax: +358 2 4802 7181
20540, Turku email: info@itmill.com
Finland company www: www.itmill.com
Primary source for information and releases: www.itmill.com
********************************************************************** */
package com.itmill.toolkit.ui;
import com.itmill.toolkit.Application;
import com.itmill.toolkit.terminal.DownloadStream;
import com.itmill.toolkit.terminal.PaintException;
import com.itmill.toolkit.terminal.PaintTarget;
import com.itmill.toolkit.terminal.ParameterHandler;
import com.itmill.toolkit.terminal.Resource;
import com.itmill.toolkit.terminal.Sizeable;
import com.itmill.toolkit.terminal.Terminal;
import com.itmill.toolkit.terminal.URIHandler;
import java.lang.ref.WeakReference;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Iterator;
/**
* Application window component.
*
* @author IT Mill Ltd.
* @version
* @VERSION@
* @since 3.0
*/
public class Window extends Panel implements URIHandler, ParameterHandler {
/**
* Window with no border.
*/
public static final int BORDER_NONE = 0;
/**
* Window with only minimal border.
*/
public static final int BORDER_MINIMAL = 1;
/**
* Window with default borders.
*/
public static final int BORDER_DEFAULT = 2;
/**
* The terminal this window is attached to.
*/
private Terminal terminal = null;
/**
* The applicaiton this window is attached to.
*/
private Application application = null;
/**
* List of URI handlers for this window.
*/
private LinkedList uriHandlerList = null;
/**
* List of parameter handlers for this window.
*/
private LinkedList parameterHandlerList = null;
/**
* Explicitly specified theme of this window. If null, application theme is
* used.
*/
private String theme = null;
/**
* Resources to be opened automatically on next repaint.
*/
private LinkedList openList = new LinkedList();
/**
* The name of the window.
*/
private String name = null;
/**
* Window border mode.
*/
private int border = BORDER_DEFAULT;
/**
* Focused component.
*/
private Focusable focusedComponent;
/**
* Distance of Window top border in pixels from top border of the
* containing (main window) or -1 if unspecified.
*/
private int positionY = -1;
/**
* Distance of Window left border in pixels from left border of the
* containing (main window) or -1 if unspecified .
*/
private int positionX = -1;
/* ********************************************************************* */
/**
* Creates a new empty unnamed window with default layout.
*
* <p>
* To show the window in application, it must be added to application with
* <code>Application.addWindow</code> method.
* </p>
*
* <p>
* The windows are scrollable by default.
* </p>
*
* @param caption
* the Title of the window.
*/
public Window() {
this("", null);
}
/**
* Creates a new empty window with default layout.
*
* <p>
* To show the window in application, it must be added to application with
* <code>Application.addWindow</code> method.
* </p>
*
* <p>
* The windows are scrollable by default.
* </p>
*
* @param caption
* the Title of the window.
*/
public Window(String caption) {
this(caption, null);
}
/**
* Creates a new window.
*
* <p>
* To show the window in application, it must be added to application with
* <code>Application.addWindow</code> method.
* </p>
*
* <p>
* The windows are scrollable by default.
* </p>
*
* @param caption
* the Title of the window.
* @param layout
* the Layout of the window.
*/
public Window(String caption, Layout layout) {
super(caption, layout);
setScrollable(true);
}
/**
* Gets the terminal type.
*
* @return the Value of property terminal.
*/
public Terminal getTerminal() {
return this.terminal;
}
/* ********************************************************************* */
/**
* Gets the window of the component. Returns the window where this component
* belongs to. If the component does not yet belong to a window the returns
* null.
*
* @return the parent window of the component.
*/
public final Window getWindow() {
return this;
}
/**
* Gets the application instance of the component. Returns the application where
* this component belongs to. If the component does not yet belong to a
* application the returns null.
*
* @return the parent application of the component.
*/
public final Application getApplication() {
return this.application;
}
/**
* Getter for property parent. Parent is the visual parent of a component.
* Each component can belong to only one ComponentContainer at time.
*
* @return the Value of property parent.
*/
public final Component getParent() {
return null;
}
/**
* Setter for property parent. Parent is the visual parent of a component.
* This is mostly called by containers add method. Setting parent is not
* allowed for the window, and thus this call should newer be called.
*
* @param parent
* the New value of property parent.
*/
public void setParent(Component parent) {
throw new RuntimeException("Setting parent for Window is not allowed");
}
/**
* Gets the component UIDL tag.
*
* @return the Component UIDL tag as string.
*/
public String getTag() {
return "window";
}
/* ********************************************************************* */
/**
* Adds the new URI handler to this window.
* @param handler the URI handler to add.
*/
public void addURIHandler(URIHandler handler) {
if (uriHandlerList == null)
uriHandlerList = new LinkedList();
synchronized (uriHandlerList) {
if (!uriHandlerList.contains(handler))
uriHandlerList.addLast(handler);
}
}
/**
* Removes the given URI handler from this window.
* @param handler the URI handler to remove.
*/
public void removeURIHandler(URIHandler handler) {
if (handler == null || uriHandlerList == null)
return;
synchronized (uriHandlerList) {
uriHandlerList.remove(handler);
if (uriHandlerList.isEmpty())
uriHandlerList = null;
}
}
/**
* Handles uri recursively.
* @param context
* @param relativeUri
*/
public DownloadStream handleURI(URL context, String relativeUri) {
DownloadStream result = null;
if (uriHandlerList != null) {
Object[] handlers;
synchronized (uriHandlerList) {
handlers = uriHandlerList.toArray();
}
for (int i = 0; i < handlers.length; i++) {
DownloadStream ds = ((URIHandler) handlers[i]).handleURI(
context, relativeUri);
if (ds != null) {
if (result != null)
throw new RuntimeException("handleURI for " + context
+ " uri: '" + relativeUri
+ "' returns ambigious result.");
result = ds;
}
}
}
return result;
}
/* ********************************************************************* */
/**
* Adds the new parameter handler to this window.
* @param handler the parameter handler to add.
*/
public void addParameterHandler(ParameterHandler handler) {
if (parameterHandlerList == null)
parameterHandlerList = new LinkedList();
synchronized (parameterHandlerList) {
if (!parameterHandlerList.contains(handler))
parameterHandlerList.addLast(handler);
}
}
/**
* Removes the given URI handler from this window.
* @param handler the parameter handler to remove.
*/
public void removeParameterHandler(ParameterHandler handler) {
if (handler == null || parameterHandlerList == null)
return;
synchronized (parameterHandlerList) {
parameterHandlerList.remove(handler);
if (parameterHandlerList.isEmpty())
parameterHandlerList = null;
}
}
/* Documented by the interface */
public void handleParameters(Map parameters) {
if (parameterHandlerList != null) {
Object[] handlers;
synchronized (parameterHandlerList) {
handlers = parameterHandlerList.toArray();
}
for (int i = 0; i < handlers.length; i++)
((ParameterHandler) handlers[i]).handleParameters(parameters);
}
}
/* ********************************************************************* */
/**
* Gets the theme for this window.
*
* @return the Name of the theme used in window. If the theme for this
* individual window is not explicitly set, the application theme is
* used instead. If application is not assigned the
* terminal.getDefaultTheme is used. If terminal is not set, null is
* returned
*/
public String getTheme() {
if (theme != null)
return theme;
if ((application != null) && (application.getTheme() != null))
return application.getTheme();
if (terminal != null)
return terminal.getDefaultTheme();
return null;
}
/**
* Sets the theme for this window.
*
* @param theme
* the New theme for this window. Null implies the default theme.
*/
public void setTheme(String theme) {
this.theme = theme;
requestRepaint();
}
/**
* Paints the content of this component.
*
* @param event
* the Paint Event.
* @throws PaintException
* if the paint operation failed.
*/
public synchronized void paintContent(PaintTarget target)
throws PaintException {
// Sets the window name
target.addAttribute("name", getName());
// Sets the window theme
target.addAttribute("theme", getTheme());
// Marks the main window
if (getApplication() != null
&& this == getApplication().getMainWindow())
target.addAttribute("main", true);
// Open requested resource
synchronized (openList) {
if (!openList.isEmpty()) {
for (Iterator i = openList.iterator(); i.hasNext();)
((OpenResource) i.next()).paintContent(target);
openList.clear();
}
}
// Contents of the window panel is painted
super.paintContent(target);
// Window position
target.addVariable(this, "positionx", getPositionX());
target.addVariable(this, "positiony", getPositionY());
// Window closing
target.addVariable(this, "close", false);
// Sets the focused component
if (this.focusedComponent != null)
target.addVariable(this, "focused", ""
+ this.focusedComponent.getFocusableId());
else
target.addVariable(this, "focused", "");
}
/* ********************************************************************* */
/**
* Opens the given resource in this window.
* @param resource
*/
public void open(Resource resource) {
synchronized (openList) {
if (!openList.contains(resource))
openList.add(new OpenResource(resource, null, -1, -1,
BORDER_DEFAULT));
}
requestRepaint();
}
/* ********************************************************************* */
/**
* Opens the given resource in named terminal window. Empty or
* <code>null</code> window name results the resource to be opened in this
* window.
* @param resource the resource.
* @param windowName the name of the window.
*/
public void open(Resource resource, String windowName) {
synchronized (openList) {
if (!openList.contains(resource))
openList.add(new OpenResource(resource, windowName, -1, -1,
BORDER_DEFAULT));
}
requestRepaint();
}
/* ********************************************************************* */
/**
* Opens the given resource in named terminal window with given size and
* border properties. Empty or <code>null</code> window name results the
* resource to be opened in this window.
* @param resource
* @param windowName
* @param width
* @param height
* @param border
*/
public void open(Resource resource, String windowName, int width,
int height, int border) {
synchronized (openList) {
if (!openList.contains(resource))
openList.add(new OpenResource(resource, windowName, width,
height, border));
}
requestRepaint();
}
/* ********************************************************************* */
/**
* Returns the full url of the window, this returns window specific url even
* for the main window.
*
* @return the URL of the window.
*/
public URL getURL() {
if (application == null)
return null;
try {
return new URL(application.getURL(), getName() + "/");
} catch (MalformedURLException e) {
throw new RuntimeException("Internal problem, please report");
}
}
/**
* Gets the unique name of the window that indentifies it on the terminal.
*
* @return the Name of the Window.
*/
public String getName() {
return name;
}
/**
* Returns the border.
*
* @return the border.
*/
public int getBorder() {
return border;
}
/**
* Sets the border.
*
* @param border
* the border to set.
*/
public void setBorder(int border) {
this.border = border;
}
/**
* Sets the application this window is connected to.
*
* <p>
* This method should not be invoked directly. Instead the
* {@link com.itmill.toolkit.Application#addWindow(Window)} method should be
* used to add the window to an application and
* {@link com.itmill.toolkit.Application#removeWindow(Window)} method for
* removing the window from the applicion. These methods call this method
* implicitly.
* </p>
*
* <p>
* The method invokes {@link Component#attach()} and
* {@link Component#detach()} methods when necessary.
* <p>
*
* @param application
* the application to set.
*/
public void setApplication(Application application) {
// If the application is not changed, dont do nothing
if (application == this.application)
return;
// Sends detach event if the window is connected to application
if (this.application != null) {
detach();
}
// Connects to new parent
this.application = application;
// Sends the attach event if connected to a window
if (application != null)
attach();
}
/**
* Sets the name.
* <p>
* The name of the window must be unique inside the application. Also the
* name may only contain the following characters: a-z, A-Z and 0-9.
* </p>
*
* <p>
* If the name is null, the the window is given name automatically when it
* is added to an application.
* </p>
*
* @param name
* the name to set.
*/
public void setName(String name) {
// The name can not be changed in application
if (getApplication() != null)
throw new IllegalStateException(
"Window name can not be changed while "
+ "the window is in application");
// Checks the name format
if (name != null)
for (int i = 0; i < name.length(); i++) {
char c = name.charAt(i);
if (!(('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9')))
throw new IllegalArgumentException(
"Window name can contain "
+ "only a-z, A-Z and 0-9 characters: '"
+ name + "' given.");
}
this.name = name;
}
/**
* Sets the terminal type. The terminal type is set by the the terminal adapter
* and may change from time to time.
*
* @param type
* the terminal type to set.
*/
public void setTerminal(Terminal type) {
this.terminal = type;
}
/**
* Window only supports pixels as unit.
*
* @see com.itmill.toolkit.terminal.Sizeable#getHeightUnits()
*/
public void setHeightUnits(int units) {
if (units != Sizeable.UNITS_PIXELS)
throw new IllegalArgumentException("Only pixels are supported");
}
/**
* Window only supports pixels as unit.
*
* @see com.itmill.toolkit.terminal.Sizeable#getWidthUnits()
*/
public void setWidthUnits(int units) {
if (units != Sizeable.UNITS_PIXELS)
throw new IllegalArgumentException("Only pixels are supported");
}
/**
* Private data structure for storing opening window properties.
*/
private class OpenResource {
private Resource resource;
private String name;
private int width;
private int height;
private int border;
/**
* Creates a new open resource.
* @param resource
* @param name
* @param width
* @param height
* @param border
*/
private OpenResource(Resource resource, String name, int width,
int height, int border) {
this.resource = resource;
this.name = name;
this.width = width;
this.height = height;
this.border = border;
}
/**
* Paints the open-tag inside the window.
* @param target the Paint Event.
* @throws PaintException if the Paint Operation fails.
*/
private void paintContent(PaintTarget target) throws PaintException {
target.startTag("open");
target.addAttribute("src", resource);
if (name != null && name.length() > 0)
target.addAttribute("name", name);
if (width >= 0)
target.addAttribute("width", width);
if (height >= 0)
target.addAttribute("height", height);
switch (border) {
case Window.BORDER_MINIMAL:
target.addAttribute("border", "minimal");
break;
case Window.BORDER_NONE:
target.addAttribute("border", "none");
break;
}
target.endTag("open");
}
}
/**
* Called when one or more variables handled by the implementing class
* are changed.
* @see com.itmill.toolkit.terminal.VariableOwner#changeVariables(java.lang.Object,
* java.util.Map)
*/
public void changeVariables(Object source, Map variables) {
super.changeVariables(source, variables);
// Gets the focused component
String focusedId = (String) variables.get("focused");
if (focusedId != null) {
try {
long id = Long.parseLong(focusedId);
this.focusedComponent = Window.getFocusableById(id);
} catch (NumberFormatException ignored) {
// We ignore invalid focusable ids
}
}
// Positioning
Integer positionx = (Integer) variables.get("positionx");
if (positionx != null) {
int x = positionx.intValue();
setPositionX(x<0?-1:x);
}
Integer positiony = (Integer) variables.get("positiony");
if (positiony != null) {
int y = positiony.intValue();
setPositionY(y<0?-1:y);
}
// Closing
Boolean close = (Boolean) variables.get("close");
if (close != null && close.booleanValue()) {
this.setVisible(false);
fireClose();
}
}
/**
* Gets the currently focused component in this window.
*
* @return the Focused component or null if none is focused.
*/
public Component.Focusable getFocusedComponent() {
return this.focusedComponent;
}
/**
* Sets the currently focused component in this window.
*
* @param focusable
* the Focused component or null if none is focused.
*/
public void setFocusedComponent(Component.Focusable focusable) {
this.application.setFocusedComponent(focusable);
this.focusedComponent = focusable;
}
/* Focusable id generator ****************************************** */
private static long lastUsedFocusableId = 0;
private static Map focusableComponents = new HashMap();
/**
* Gets an id for focusable component.
* @param focusable the focused component.
*/
public static long getNewFocusableId(Component.Focusable focusable) {
long newId = ++lastUsedFocusableId;
WeakReference ref = new WeakReference(focusable);
focusableComponents.put(new Long(newId), ref);
return newId;
}
/**
* Maps the focusable id back to focusable component.
* @param focusableId the Focused Id.
* @return the focusable Id.
*/
public static Component.Focusable getFocusableById(long focusableId) {
WeakReference ref = (WeakReference) focusableComponents.get(new Long(
focusableId));
if (ref != null) {
Object o = ref.get();
if (o != null) {
return (Component.Focusable) o;
}
}
return null;
}
/**
* Releases the focusable component id when not used anymore.
* @param focusableId the focusable Id to remove.
*/
public static void removeFocusableId(long focusableId) {
Long id = new Long(focusableId);
WeakReference ref = (WeakReference) focusableComponents.get(id);
ref.clear();
focusableComponents.remove(id);
}
/**
* Gets the distance of Window left border in pixels from left
* border of the containing (main window).
* @return the Distance of Window left border in pixels from left
* border of the containing (main window). or -1 if unspecified.
* @since 4.0.0
*/
public int getPositionX() {
return positionX;
}
/**
* Sets the distance of Window left border in pixels from left border
* of the containing (main window).
* @param positionX the Distance of Window left border in pixels from left
* border of the containing (main window). or -1 if unspecified.
* @since 4.0.0
*/
public void setPositionX(int positionX) {
this.positionX = positionX;
}
/**
* Gets the distance of Window top border in pixels from top border of
* the containing (main window).
* @return Distance of Window top border in pixels from top border of the
* containing (main window). or -1 if unspecified .
*
* @since 4.0.0
*/
public int getPositionY() {
return positionY;
}
/**
* Sets the distance of Window top border in pixels from top border of the containing (main window).
* @param positionY the Distance of Window top border in pixels from top border of the containing (main window). or -1 if unspecified
*
* @since 4.0.0
*/
public void setPositionY(int positionY) {
this.positionY = positionY;
}
private static final Method WINDOW_CLOSE_METHOD;
static {
try {
WINDOW_CLOSE_METHOD =
CloseListener.class.getDeclaredMethod(
"windowClose",
new Class[] { CloseEvent.class });
} catch (java.lang.NoSuchMethodException e) {
// This should never happen
throw new java.lang.RuntimeException();
}
}
public class CloseEvent extends Component.Event {
/**
* Serial generated by eclipse.
*/
private static final long serialVersionUID = -7235770057344367327L;
/**
*
* @param source
*/
public CloseEvent(Component source) {
super(source);
}
/**
* Gets the Window.
* @return the window.
*/
public Window getWindow() {
return (Window) getSource();
}
}
public interface CloseListener {
public void windowClose(CloseEvent e);
}
/**
* Adds the listener.
* @param listener the listener to add.
*/
public void addListener(CloseListener listener) {
addListener(CloseEvent.class, listener, WINDOW_CLOSE_METHOD);
}
/**
* Removes the listener.
* @param listener the listener to remove.
*/
public void removeListener(CloseListener listener) {
addListener(CloseEvent.class, listener, WINDOW_CLOSE_METHOD);
}
protected void fireClose() {
fireEvent(new Window.CloseEvent(this));
}
}
|