Subversion Repository Public Repository

litesoft

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
package org.litesoft.GWT.client;

import org.litesoft.core.util.*;

/**
 * Window Name is the name of the window.
 * <p/>
 * This name cannot contain spaces or special characters, but can be quite long (on the order of 2MB).
 * <p/>
 * Known good characters are US Letters (both upper & lower case), digits, and underscore ('_').
 * <p/>
 * Special / reserved Names when used as a 'target' in an 'href' (will load the document into...) are:
 * . . . _blank  . . a new blank window. This window is not named.
 * . . . _parent . . the immediate parent of the document the link is in.
 * . . . _self . . . the same window the link was clicked in (the active window).
 * . . . _top  . . . the topmost window.
 * . . . _search . . the browser's search pane. (Available in IE 5+)
 */
public class WindowName
{
    public interface Listener
    {
        public void windowNameChanged();
    }

    public static final String[] RESERVED_NAMES = //
            { //
              "_blank", //. . new blank window. This window is not named.
              "_parent", // . immediate parent of the document the link is in.
              "_self", // . . same window the link was clicked in (the active window).
              "_top", //. . . topmost window.
              "_search", // . browser's search pane. (Available in Internet Explorer 5 or later)
            };

    public static void add( Listener pListener )
    {
        if ( (pListener != null) && (-1 == indexOf( pListener )) )
        {
            sListeners = append( sListeners, pListener );
        }
    }

    public static void remove( Listener pListener )
    {
        if ( pListener != null )
        {
            int zAt = indexOf( pListener );
            if ( zAt != -1 )
            {
                sListeners = removeAt( sListeners, zAt );
            }
        }
    }

    public static void set( String pWinName )
            throws IllegalArgumentException
    {
        if ( (pWinName == null) || (pWinName.length() == 0) )
        {
            throw new IllegalArgumentException( "WinName null or empty" );
        }
        if ( -1 != pWinName.indexOf( ' ' ) )
        {
            throw new IllegalArgumentException( "WinName '" + pWinName + "' contains a space" );
        }
        if ( pWinName.charAt( 0 ) == '_' )
        {
            for ( String zReservedName : RESERVED_NAMES )
            {
                if ( zReservedName.equals( pWinName ) )
                {
                    throw new IllegalArgumentException( "WinName '" + pWinName + "' is a reserved name" );
                }
            }
        }
        if ( !pWinName.equals( get() ) )
        {
            LLset( pWinName );
            Listener[] zListeners = sListeners; // Snag the List
            for ( Listener zListener : zListeners )
            {
                zListener.windowNameChanged();
            }
        }
    }

    /**
     * @return null or a non-Empty WindowName
     */
    public static String get()
    {
        return UtilsCommon.noEmpty( LLget() );
    }

    private static native String LLget()/*-{
        return $wnd.name;
    }-*/;

    private static native void LLset( String pWinName )/*-{
        $wnd.name = pWinName;
    }-*/;

    private static Listener[] sListeners = new Listener[0];

    private static int indexOf( Listener pListener )
    {
        for ( int i = 0; i < sListeners.length; i++ )
        {
            Listener zListener = sListeners[i];
            if ( zListener == pListener ) // Identity
            {
                return i;
            }
        }
        return -1;
    }

    private static Listener[] append( Listener[] pArray, Listener pListener )
    {
        int zLength = pArray.length;
        if ( zLength == 0 )
        {
            return new Listener[]{pListener};
        }
        Listener[] zNew = new Listener[zLength + 1];
        System.arraycopy( pArray, 0, zNew, 1, zLength );
        zNew[0] = pListener;
        return zNew;
    }

    private static Listener[] removeAt( Listener[] pArray, int pIndexToRemove )
    {
        int zNewLength = pArray.length - 1;
        Listener[] zNew = new Listener[zNewLength];
        if ( pIndexToRemove != 0 )
        {
            System.arraycopy( pArray, 0, zNew, 0, pIndexToRemove );
        }
        if ( pIndexToRemove != zNewLength )
        {
            System.arraycopy( pArray, pIndexToRemove + 1, zNew, pIndexToRemove, zNewLength - pIndexToRemove );
        }
        return zNew;
    }
}

Commits for litesoft/trunk/Java/GWT/Client/src/org/litesoft/GWT/client/WindowName.java

Diff revisions: vs.
Revision Author Commited Message
2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000