|
@@ -1,123 +1,123 @@ |
1 |
|
- |
// This Source Code is in the Public Domain per: http://unlicense.org |
2 |
|
- |
package org.litesoft.GWT.client; |
3 |
|
- |
|
4 |
|
- |
import org.litesoft.commonfoundation.typeutils.*; |
5 |
|
- |
|
6 |
|
- |
/** |
7 |
|
- |
* Window Name is the name of the window. |
8 |
|
- |
* <p/> |
9 |
|
- |
* This name cannot contain spaces or special characters, but can be quite long (on the order of 2MB). |
10 |
|
- |
* <p/> |
11 |
|
- |
* Known good characters are US Letters (both upper & lower case), digits, and underscore ('_'). |
12 |
|
- |
* <p/> |
13 |
|
- |
* Special / reserved Names when used as a 'target' in an 'href' (will load the document into...) are: |
14 |
|
- |
* . . . _blank . . a new blank window. This window is not named. |
15 |
|
- |
* . . . _parent . . the immediate parent of the document the link is in. |
16 |
|
- |
* . . . _self . . . the same window the link was clicked in (the active window). |
17 |
|
- |
* . . . _top . . . the topmost window. |
18 |
|
- |
* . . . _search . . the browser's search pane. (Available in IE 5+) |
19 |
|
- |
*/ |
20 |
|
- |
public class WindowName { |
21 |
|
- |
public interface Listener { |
22 |
|
- |
public void windowNameChanged(); |
23 |
|
- |
} |
24 |
|
- |
|
25 |
|
- |
public static final String[] RESERVED_NAMES = // |
26 |
|
- |
{ // |
27 |
|
- |
"_blank", //. . new blank window. This window is not named. |
28 |
|
- |
"_parent", // . immediate parent of the document the link is in. |
29 |
|
- |
"_self", // . . same window the link was clicked in (the active window). |
30 |
|
- |
"_top", //. . . topmost window. |
31 |
|
- |
"_search", // . browser's search pane. (Available in Internet Explorer 5 or later) |
32 |
|
- |
}; |
33 |
|
- |
|
34 |
|
- |
public static void add( Listener pListener ) { |
35 |
|
- |
if ( (pListener != null) && (-1 == indexOf( pListener )) ) { |
36 |
|
- |
sListeners = append( sListeners, pListener ); |
37 |
|
- |
} |
38 |
|
- |
} |
39 |
|
- |
|
40 |
|
- |
public static void remove( Listener pListener ) { |
41 |
|
- |
if ( pListener != null ) { |
42 |
|
- |
int zAt = indexOf( pListener ); |
43 |
|
- |
if ( zAt != -1 ) { |
44 |
|
- |
sListeners = removeAt( sListeners, zAt ); |
45 |
|
- |
} |
46 |
|
- |
} |
47 |
|
- |
} |
48 |
|
- |
|
49 |
|
- |
public static void set( String pWinName ) |
50 |
|
- |
throws IllegalArgumentException { |
51 |
|
- |
if ( (pWinName == null) || (pWinName.length() == 0) ) { |
52 |
|
- |
throw new IllegalArgumentException( "WinName null or empty" ); |
53 |
|
- |
} |
54 |
|
- |
if ( -1 != pWinName.indexOf( ' ' ) ) { |
55 |
|
- |
throw new IllegalArgumentException( "WinName '" + pWinName + "' contains a space" ); |
56 |
|
- |
} |
57 |
|
- |
if ( pWinName.charAt( 0 ) == '_' ) { |
58 |
|
- |
for ( String zReservedName : RESERVED_NAMES ) { |
59 |
|
- |
if ( zReservedName.equals( pWinName ) ) { |
60 |
|
- |
throw new IllegalArgumentException( "WinName '" + pWinName + "' is a reserved name" ); |
61 |
|
- |
} |
62 |
|
- |
} |
63 |
|
- |
} |
64 |
|
- |
if ( !pWinName.equals( get() ) ) { |
65 |
|
- |
LLset( pWinName ); |
66 |
|
- |
Listener[] zListeners = sListeners; // Snag the List |
67 |
|
- |
for ( Listener zListener : zListeners ) { |
68 |
|
- |
zListener.windowNameChanged(); |
69 |
|
- |
} |
70 |
|
- |
} |
71 |
|
- |
} |
72 |
|
- |
|
73 |
|
- |
/** |
74 |
|
- |
* @return null or a non-Empty WindowName |
75 |
|
- |
*/ |
76 |
|
- |
public static String get() { |
77 |
|
- |
return Strings.noEmpty( LLget() ); |
78 |
|
- |
} |
79 |
|
- |
|
80 |
|
- |
private static native String LLget()/*-{ |
81 |
|
- |
return $wnd.name; |
82 |
|
- |
}-*/; |
83 |
|
- |
|
84 |
|
- |
private static native void LLset( String pWinName )/*-{ |
85 |
|
- |
$wnd.name = pWinName; |
86 |
|
- |
}-*/; |
87 |
|
- |
|
88 |
|
- |
private static Listener[] sListeners = new Listener[0]; |
89 |
|
- |
|
90 |
|
- |
private static int indexOf( Listener pListener ) { |
91 |
|
- |
for ( int i = 0; i < sListeners.length; i++ ) { |
92 |
|
- |
Listener zListener = sListeners[i]; |
93 |
|
- |
if ( zListener == pListener ) // Identity |
94 |
|
- |
{ |
95 |
|
- |
return i; |
96 |
|
- |
} |
97 |
|
- |
} |
98 |
|
- |
return -1; |
99 |
|
- |
} |
100 |
|
- |
|
101 |
|
- |
private static Listener[] append( Listener[] pArray, Listener pListener ) { |
102 |
|
- |
int zLength = pArray.length; |
103 |
|
- |
if ( zLength == 0 ) { |
104 |
|
- |
return new Listener[]{pListener}; |
105 |
|
- |
} |
106 |
|
- |
Listener[] zNew = new Listener[zLength + 1]; |
107 |
|
- |
System.arraycopy( pArray, 0, zNew, 1, zLength ); |
108 |
|
- |
zNew[0] = pListener; |
109 |
|
- |
return zNew; |
110 |
|
- |
} |
111 |
|
- |
|
112 |
|
- |
private static Listener[] removeAt( Listener[] pArray, int pIndexToRemove ) { |
113 |
|
- |
int zNewLength = pArray.length - 1; |
114 |
|
- |
Listener[] zNew = new Listener[zNewLength]; |
115 |
|
- |
if ( pIndexToRemove != 0 ) { |
116 |
|
- |
System.arraycopy( pArray, 0, zNew, 0, pIndexToRemove ); |
117 |
|
- |
} |
118 |
|
- |
if ( pIndexToRemove != zNewLength ) { |
119 |
|
- |
System.arraycopy( pArray, pIndexToRemove + 1, zNew, pIndexToRemove, zNewLength - pIndexToRemove ); |
120 |
|
- |
} |
121 |
|
- |
return zNew; |
122 |
|
- |
} |
123 |
|
- |
} |
|
1 |
+ |
// This Source Code is in the Public Domain per: http://unlicense.org |
|
2 |
+ |
package org.litesoft.GWT.client; |
|
3 |
+ |
|
|
4 |
+ |
import org.litesoft.commonfoundation.base.*; |
|
5 |
+ |
|
|
6 |
+ |
/** |
|
7 |
+ |
* Window Name is the name of the window. |
|
8 |
+ |
* <p/> |
|
9 |
+ |
* This name cannot contain spaces or special characters, but can be quite long (on the order of 2MB). |
|
10 |
+ |
* <p/> |
|
11 |
+ |
* Known good characters are US Letters (both upper & lower case), digits, and underscore ('_'). |
|
12 |
+ |
* <p/> |
|
13 |
+ |
* Special / reserved Names when used as a 'target' in an 'href' (will load the document into...) are: |
|
14 |
+ |
* . . . _blank . . a new blank window. This window is not named. |
|
15 |
+ |
* . . . _parent . . the immediate parent of the document the link is in. |
|
16 |
+ |
* . . . _self . . . the same window the link was clicked in (the active window). |
|
17 |
+ |
* . . . _top . . . the topmost window. |
|
18 |
+ |
* . . . _search . . the browser's search pane. (Available in IE 5+) |
|
19 |
+ |
*/ |
|
20 |
+ |
public class WindowName { |
|
21 |
+ |
public interface Listener { |
|
22 |
+ |
public void windowNameChanged(); |
|
23 |
+ |
} |
|
24 |
+ |
|
|
25 |
+ |
public static final String[] RESERVED_NAMES = // |
|
26 |
+ |
{ // |
|
27 |
+ |
"_blank", //. . new blank window. This window is not named. |
|
28 |
+ |
"_parent", // . immediate parent of the document the link is in. |
|
29 |
+ |
"_self", // . . same window the link was clicked in (the active window). |
|
30 |
+ |
"_top", //. . . topmost window. |
|
31 |
+ |
"_search", // . browser's search pane. (Available in Internet Explorer 5 or later) |
|
32 |
+ |
}; |
|
33 |
+ |
|
|
34 |
+ |
public static void add( Listener pListener ) { |
|
35 |
+ |
if ( (pListener != null) && (-1 == indexOf( pListener )) ) { |
|
36 |
+ |
sListeners = append( sListeners, pListener ); |
|
37 |
+ |
} |
|
38 |
+ |
} |
|
39 |
+ |
|
|
40 |
+ |
public static void remove( Listener pListener ) { |
|
41 |
+ |
if ( pListener != null ) { |
|
42 |
+ |
int zAt = indexOf( pListener ); |
|
43 |
+ |
if ( zAt != -1 ) { |
|
44 |
+ |
sListeners = removeAt( sListeners, zAt ); |
|
45 |
+ |
} |
|
46 |
+ |
} |
|
47 |
+ |
} |
|
48 |
+ |
|
|
49 |
+ |
public static void set( String pWinName ) |
|
50 |
+ |
throws IllegalArgumentException { |
|
51 |
+ |
if ( (pWinName == null) || (pWinName.length() == 0) ) { |
|
52 |
+ |
throw new IllegalArgumentException( "WinName null or empty" ); |
|
53 |
+ |
} |
|
54 |
+ |
if ( -1 != pWinName.indexOf( ' ' ) ) { |
|
55 |
+ |
throw new IllegalArgumentException( "WinName '" + pWinName + "' contains a space" ); |
|
56 |
+ |
} |
|
57 |
+ |
if ( pWinName.charAt( 0 ) == '_' ) { |
|
58 |
+ |
for ( String zReservedName : RESERVED_NAMES ) { |
|
59 |
+ |
if ( zReservedName.equals( pWinName ) ) { |
|
60 |
+ |
throw new IllegalArgumentException( "WinName '" + pWinName + "' is a reserved name" ); |
|
61 |
+ |
} |
|
62 |
+ |
} |
|
63 |
+ |
} |
|
64 |
+ |
if ( !pWinName.equals( get() ) ) { |
|
65 |
+ |
LLset( pWinName ); |
|
66 |
+ |
Listener[] zListeners = sListeners; // Snag the List |
|
67 |
+ |
for ( Listener zListener : zListeners ) { |
|
68 |
+ |
zListener.windowNameChanged(); |
|
69 |
+ |
} |
|
70 |
+ |
} |
|
71 |
+ |
} |
|
72 |
+ |
|
|
73 |
+ |
/** |
|
74 |
+ |
* @return null or a non-Empty WindowName |
|
75 |
+ |
*/ |
|
76 |
+ |
public static String get() { |
|
77 |
+ |
return ConstrainTo.significantOrNull( LLget() ); |
|
78 |
+ |
} |
|
79 |
+ |
|
|
80 |
+ |
private static native String LLget()/*-{ |
|
81 |
+ |
return $wnd.name; |
|
82 |
+ |
}-*/; |
|
83 |
+ |
|
|
84 |
+ |
private static native void LLset( String pWinName )/*-{ |
|
85 |
+ |
$wnd.name = pWinName; |
|
86 |
+ |
}-*/; |
|
87 |
+ |
|
|
88 |
+ |
private static Listener[] sListeners = new Listener[0]; |
|
89 |
+ |
|
|
90 |
+ |
private static int indexOf( Listener pListener ) { |
|
91 |
+ |
for ( int i = 0; i < sListeners.length; i++ ) { |
|
92 |
+ |
Listener zListener = sListeners[i]; |
|
93 |
+ |
if ( zListener == pListener ) // Identity |
|
94 |
+ |
{ |
|
95 |
+ |
return i; |
|
96 |
+ |
} |
|
97 |
+ |
} |
|
98 |
+ |
return -1; |
|
99 |
+ |
} |
|
100 |
+ |
|
|
101 |
+ |
private static Listener[] append( Listener[] pArray, Listener pListener ) { |
|
102 |
+ |
int zLength = pArray.length; |
|
103 |
+ |
if ( zLength == 0 ) { |
|
104 |
+ |
return new Listener[]{pListener}; |
|
105 |
+ |
} |
|
106 |
+ |
Listener[] zNew = new Listener[zLength + 1]; |
|
107 |
+ |
System.arraycopy( pArray, 0, zNew, 1, zLength ); |
|
108 |
+ |
zNew[0] = pListener; |
|
109 |
+ |
return zNew; |
|
110 |
+ |
} |
|
111 |
+ |
|
|
112 |
+ |
private static Listener[] removeAt( Listener[] pArray, int pIndexToRemove ) { |
|
113 |
+ |
int zNewLength = pArray.length - 1; |
|
114 |
+ |
Listener[] zNew = new Listener[zNewLength]; |
|
115 |
+ |
if ( pIndexToRemove != 0 ) { |
|
116 |
+ |
System.arraycopy( pArray, 0, zNew, 0, pIndexToRemove ); |
|
117 |
+ |
} |
|
118 |
+ |
if ( pIndexToRemove != zNewLength ) { |
|
119 |
+ |
System.arraycopy( pArray, pIndexToRemove + 1, zNew, pIndexToRemove, zNewLength - pIndexToRemove ); |
|
120 |
+ |
} |
|
121 |
+ |
return zNew; |
|
122 |
+ |
} |
|
123 |
+ |
} |