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
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
package com.temp.foundation.client.widget.dialog;

import org.litesoft.externalization.shared.*;

import com.google.gwt.event.dom.client.*;
import com.google.gwt.user.client.ui.*;
import com.temp.foundation.client.handler.*;
import com.temp.foundation.client.util.*;
import com.temp.foundation.client.widget.*;
import com.temp.common.shared.utils.*;

public class SEDIDialogBox {
    protected interface BodySource {
        Widget createBody();
    }

    protected final DialogBox dialogBox = new DialogBox();
    protected final String iconUrl;
    protected final Integer contentWidth;
    protected final String title;
    protected final String ourClassName;
    protected final HorizontalPanel titleBarCenterPanel = new HorizontalPanel();

    protected DialogClose closeListener;

    protected boolean centerAllContent;
    protected BodySource bodySource;
    private boolean classInjected, widgetBuilt;

    protected final ClickHandler hideClickHandler = new ClickHandler() {
        @Override
        public void onClick( ClickEvent event ) {
            hide();
        }
    };

    public SEDIDialogBox( String iconUrl, Integer contentWidth, String title ) {
        this.iconUrl = iconUrl;
        this.contentWidth = contentWidth;
        this.title = StringUtils.noEmpty( title );
        this.ourClassName = ObjectUtils.getSimpleClassName( this );

        titleBarCenterPanel.add( new Spacer().size( "1px" ) );

        dialogBox.addStyleName( "SEDIDialogBox" );

        dialogBox.setAnimationEnabled( true );
        dialogBox.setGlassEnabled( true );
    }

    /**
     * add a style "classname", and allow chaining calls.
     *
     * @param style "classname"
     *
     * @return this
     */
    public SEDIDialogBox style( String style ) {
        if ( null != (style = StringUtils.noEmpty( style )) ) {
            dialogBox.addStyleName( style );
        }
        return this;
    }

    public SEDIDialogBox setCloseListener( DialogClose closeListener ) {
        this.closeListener = closeListener;
        return this;
    }

    public boolean isShowing() {
        return dialogBox.isShowing();
    }

    public void show() {
        if ( !isShowing() ) {
            if ( !widgetBuilt ) {
                Widget fullDialogContent = buildFullContentWidgetForDialogBox( bodySource.createBody() );
                widgetBuilt = true;
                dialogBox.setWidget( new OurSimplePanel( fullDialogContent ).style( "SEDIDialogBoxContent" ) );
                if ( !classInjected ) {
                    classInjected = true;
                    CommonElementHelper.findElementWithTagNameUpDOM( fullDialogContent.getElement(), CommonElementHelper.TABLE ).addClassName( ourClassName );
                }
                dialogBox.center();
            }
            dialogBox.show();
        }
    }

    public void hide() {
        DialogClose listener = closeListener;
        closeListener = null;
        dialogBox.hide();
        if ( listener != null ) {
            try {
                listener.closed();
            }
            catch ( RuntimeException whatever ) {
                // Protecting the UI from ...
            }
        }
    }

    protected void setBodySource( BodySource bodySource ) {
        widgetBuilt = false;
        this.bodySource = bodySource;
    }

    protected void setBody( final String[] bodyTextBlocksAboveControlButtons, final ButtonBase... bottomControlButtons ) {
        setBodySource( new BodySource() {
            @Override
            public Widget createBody() {
                return createBodyWidget( addLines( createVerticalContentPanel(), bodyTextBlocksAboveControlButtons ), bottomControlButtons );
            }
        } );
    }

    protected void setBody( final Widget bodyAboveControlButtons, final ButtonBase... bottomControlButtons ) {
        setBodySource( new BodySource() {
            @Override
            public Widget createBody() {
                return createBodyWidget( bodyAboveControlButtons, bottomControlButtons );
            }
        } );
    }

    protected void setBody( final Widget body ) {
        setBodySource( new BodySource() {
            @Override
            public Widget createBody() {
                return body;
            }
        } );
    }

    /**
     * Build the Full Content Widget for the DialogBox by adding the TitleBar
     * above the bodyWidget.
     *
     * @param bodyWidget to be added below the TitleBar
     *
     * @return a Vertical Panel with all the widgets that make up the Full
     * Content Widget for the DialogBox.
     */
    protected VerticalPanel buildFullContentWidgetForDialogBox( Widget bodyWidget ) {
        VerticalPanel panel = new VerticalPanel();
        Widget titleBar = createTitleBar();
        addWidget( panel, titleBar );
        if ( (titleBar != null) && (bodyWidget != null) ) {
            addTitleBodySpacer( panel );
        }
        addBoxedContent( panel, bodyWidget );
        return panel;
    }

    protected void addBoxedContent( VerticalPanel panel, Widget widget ) {
        if ( widget != null ) {
            panel.add( new OurSimplePanel( widget ).style( "SEDIDialogBoxSpecificContent" ) );
            CommonElementHelper.findElementWithTagNameUpDOM( widget.getElement(), CommonElementHelper.TD ).addClassName( ourClassName + "Content" );
        }
    }

    protected void addTitleBodySpacer( VerticalPanel panel ) {
        addWidget( panel, new Spacer().style( "dialogTitleBodySpacer" ) );
    }

    protected void addWidget( VerticalPanel panel, Widget widget ) {
        if ( widget != null ) {
            panel.add( widget );
        }
    }

    protected Widget createTitleBar() {
        NamedHtmlLabel label = new NamedHtmlLabel( SafeHtmlizer.getInstance().noEmpty1stLine( title ) ).style( "SEDIDialogBoxTitle" );
        ButtonBase button = createXButton();
        Widget right = (button == null) ? new Spacer().size( "1px" ) : UtilsGwt.horizontal( new Spacer().height( "1px" ).width( "10px" ), button );
        return new TitleBar( createIconLabelPanel( label ), titleBarCenterPanel, right ).style( "SEDIDialogBoxTitlePanel" );
    }

    protected NamedImageButton createXButton() {
        return new NamedImageButton( "x", hideClickHandler ).style( "SEDIDialogBoxXButton" );
    }

    protected Widget createIconLabelPanel( Widget label ) {
        if ( iconUrl == null ) {
            return label;
        }
        HorizontalPanel panel = new HorizontalPanel();
        panel.setVerticalAlignment( HasVerticalAlignment.ALIGN_MIDDLE );
        panel.add( new Image( iconUrl ) );
        panel.add( new Spacer().width( "5px" ) );
        panel.add( label );
        return panel;
    }

    protected Widget center( Widget widget ) {
        if ( widget == null ) {
            return null;
        }
        HorizontalPanel panel = new HorizontalPanel();
        panel.setWidth( "100%" );
        panel.setHorizontalAlignment( HasHorizontalAlignment.ALIGN_CENTER );
        panel.add( widget );
        return panel;
    }

    protected Widget createButtons( ButtonBase[] bottomControlButtons ) {
        if ( CollectionsUtils.isEmpty( bottomControlButtons ) ) {
            return null;
        }
        HorizontalPanel panel = new HorizontalPanel();
        panel.setVerticalAlignment( HasVerticalAlignment.ALIGN_MIDDLE );
        panel.add( new Spacer().width( "5px" ) );
        if ( bottomControlButtons != null ) {
            for ( ButtonBase button : bottomControlButtons ) {
                if ( button != null ) {
                    panel.add( button );
                }
                panel.add( new Spacer().width( "5px" ) );
            }
        }
        return panel;
    }

    protected VerticalPanel createVerticalContentPanel() {
        VerticalPanel panel = new VerticalPanel();
        if ( contentWidth != null ) {
            panel.setWidth( contentWidth + "px" );
        }
        return panel;
    }

    protected VerticalPanel addLines( VerticalPanel panel, String[] bodyTextBlocksAboveControlButtons ) {
        if ( bodyTextBlocksAboveControlButtons != null ) {
            for ( String block : bodyTextBlocksAboveControlButtons ) {
                Widget widget = makeLabel( block );
                if ( centerAllContent ) {
                    widget = center( new OurWidgetConstrainer( widget ) );
                }
                addWithSpacing( panel, widget );
            }
        }
        return panel;
    }

    protected Widget makeLabel( String block ) {
        block = StringUtils.replaceAll( block, E13nResolver.SPACE_SUBSTITUTION_ID, ' ' );
        return new NamedHtmlLabel( SafeHtmlizer.getInstance().noEmpty( block, 93 ) ).style( "SEDIDialogBoxText" );
    }

    protected VerticalPanel addWithSpacing( VerticalPanel panel, Widget widget ) {
        if ( widget != null ) {
            if ( panel.getWidgetCount() != 0 ) {
                panel.add( new Spacer().style( "dialogInterLineSpacer" ) );
            }
            panel.add( widget );
        }
        return panel;
    }

    protected Widget createBodyWidget( Widget bodyAboveControlButtons, ButtonBase... bottomControlButtons ) {
        Widget centeredButtons = center( createButtons( bottomControlButtons ) );
        if ( centeredButtons == null ) {
            return bodyAboveControlButtons;
        }
        VerticalPanel panel;
        if ( bodyAboveControlButtons instanceof VerticalPanel ) {
            panel = (VerticalPanel) bodyAboveControlButtons;
        } else {
            panel = createVerticalContentPanel();
            panel.add( bodyAboveControlButtons );
        }
        return addWithSpacing( panel, centeredButtons );
    }

    public class TitleBar extends Composite {

        public TitleBar( Widget left, Widget center, Widget right ) {
            String Id = HTMLPanel.createUniqueId();
            String leftId = "left-" + Id;
            String centerId = "center-" + Id;
            String rightId = "right-" + Id;
            HTMLPanel htmlPanel = new HTMLPanel( "<table border=0 cellpadding=0 cellspacing=0 width='100%'><tr>" + //
                                                 "<td id='" + leftId + "' align='left' valign='middle'></td>" + //
                                                 "<td id='" + centerId + "' align='left' valign='middle'></td>" + //
                                                 "<td id='" + rightId + "' align='right' valign='middle'></td>" + //
                                                 "</tr></table>" );
            htmlPanel.add( deNull( left ), leftId );
            htmlPanel.add( deNull( center ), centerId );
            htmlPanel.add( deNull( right ), rightId );
            initWidget( htmlPanel );
        }

        public TitleBar style( String className ) {
            addStyleName( className );
            return this;
        }

        private Widget deNull( Widget widget ) {
            if ( widget == null ) {
                widget = new Spacer( "1px", "1px" );
            }
            return widget;
        }
    }
}

Commits for litesoft/trunk/GWT_Sandbox/FormEngine/src/com/temp/foundation/client/widget/dialog/SEDIDialogBox.java

Diff revisions: vs.
Revision Author Commited Message
965 GeorgeS picture GeorgeS Fri 01 Aug, 2014 03:20:35 +0000

!