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
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
package com.wdstechnology.ucci.deviceassist.client.view;

import com.google.gwt.animation.client.Animation;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Overflow;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.wdstechnology.ucci.deviceassist.client.ui.WidgetMouseHandler;
import com.wdstechnology.ucci.deviceassist.client.util.UIUtils;

public class WDSStackMenuView extends AbstractStackMenuView {

   public interface TransparentImages extends ClientBundle {
      @Source("transparent.gif")
      public ImageResource transparentImage();
   }

   public static final String TOP_LEVEL_STYLE = "StackMenuTopLevel";

   protected enum IconStyle {
      ArrowL1, ArrowL2, Plus, Minus
   }

   protected enum SelectionState {
      NotSelected, Selected
   }

   private VerticalPanel mainPanel = new VerticalPanel();

   private String transparentImageUrl;
   
   private TopMenuCategory currentlyOpen = null;

   private SubMenuItem currentlySelected = null;

   public WDSStackMenuView() {
      TransparentImages images = GWT.create(TransparentImages.class);
      transparentImageUrl = images.transparentImage().getURL();
      
      addStyleName("StackMenu");
      add(mainPanel);
   }

   public SubMenu addStyles(String... styles) {
      UIUtils.addStyles(this, styles);
      return this;
   }

   public void createAction(String title, final Command command, String... styles) {
      Command zCommand = new Command() {
         public void execute() {
            closeCategory();
            command.execute();
         }
      };
      addEntry(new OurSimplePanel(new TopMenuItem(title, zCommand, styles)));
   }

   public SubMenu createCategory(String title, String... styles) {
      TopMenuCategory topMenuCategory = new TopMenuCategory(title, styles);
      addEntry(new OurOverflowHiddenPanel(topMenuCategory));
      return topMenuCategory;
   }

   private void addEntry(Widget widget) {
      mainPanel.add(new OurSimplePanel(widget = new OurSimplePanel(widget, TOP_LEVEL_STYLE)));
      widget.addStyleName((mainPanel.getWidgetCount() == 1) ? "StackMenuFirstTopLevel" : "StackMenuNthTopLevel");
   }

   public void clearMenu() {
      mainPanel.clear();
      currentlyOpen = null;
      currentlySelected = null;
   }

   private void select(SubMenuItem selectedItem) {
      deselect();
      selectedItem.setSelected(true);
      currentlySelected = selectedItem;
   }

   private void deselect() {
      if (currentlySelected != null) {
         currentlySelected.setSelected(false);
         currentlySelected = null;
      }
   }

   private void openCategory(TopMenuCategory category) {
      closeCategory();
      category.open();
      currentlyOpen = category;
   }

   private void closeCategory() {
      if (currentlyOpen != null) {
         currentlyOpen.close();
         currentlyOpen = null;
         deselect();
      }
   }

   private abstract class MenuEntry extends HorizontalPanel implements WidgetMouseHandler.MouseClickedListener {

      protected static final String ICON_STYLE_BASE = "StackMenuIcon";

      protected IconStyle currentIconStyle = null;

      protected OurImage iconImage = new OurImage(transparentImageUrl, ICON_STYLE_BASE);

      protected Element mouseCSSElement;

      protected Element selectionCSSElement;

      protected WidgetMouseHandler mouseHandler;

      protected Command command;

      public MenuEntry(IconStyle iconStyle, final String title, Command command, String... styles) {
         this.command = command;
         UIUtils.addStyles(this, styles);
         add(new OurImage(transparentImageUrl, "StackMenuIndent"));
         add(new OurImage(transparentImageUrl, "StackMenuPreIconSpacer"));
         add(iconImage);
         add(new OurImage(transparentImageUrl, "StackMenuPostIconSpacer"));
         add(new OurLabel(title, "StackMenuText"));

         Element table = getElement();
         Element tbody = (Element) table.getChild(0);
         Element tr = (Element) tbody.getChild(0);

         selectionCSSElement = tbody;
         mouseCSSElement = tr;

         setBackgroundIconImageStyle(iconStyle);

         mouseHandler = new WidgetMouseHandler(mouseCSSElement, this, new WidgetMouseHandler.Logger() {
            public void log(String message) {
               System.out.println(title + ": " + message);
            }
         });
         sinkEvents(WidgetMouseHandler.EVENT_BITS);
      }

      protected void setBackgroundIconImageStyle(IconStyle style) {
         if (currentIconStyle != null) {
            iconImage.removeStyleName(makeStyleName(currentIconStyle));
         }
         currentIconStyle = style;
         iconImage.addStyleName(makeStyleName(style));
      }

      private String makeStyleName(IconStyle style) {
         return ICON_STYLE_BASE + "-" + style;
      }

      @Override
      public void onBrowserEvent(Event event) {
         if (!mouseHandler.onBrowserEvent(event)) {
            super.onBrowserEvent(event);
         }
      }

      public void mouseClicked() {
         if (command != null) {
            command.execute();
         }
      }

      @Override
      protected void setCellHorizontalAlignment(Element td, HorizontalAlignmentConstant align) {
         // Remove ALL TD alignment - leave for CSS
      }

      @Override
      protected void setCellVerticalAlignment(Element td, VerticalAlignmentConstant align) {
         // Remove ALL TD alignment - leave for CSS
      }
   }

   private class TopMenuItem extends MenuEntry {

      public TopMenuItem(String title, Command command, String... styles) {
         super(IconStyle.ArrowL1, title, command, styles);
         addStyleName("L1");
      }
   }

   private class SubMenuItem extends MenuEntry {

      public SubMenuItem(String title, Command command, String... styles) {
         super(IconStyle.ArrowL2, title, command, styles);
         addStyleName("L2");
      }

      @Override
      public void mouseClicked() {
         select(this);
         super.mouseClicked();
      }

      protected void setSelected(boolean selected) {
         selectionCSSElement.removeClassName(makeStyleName(SelectionState.Selected));
         selectionCSSElement.removeClassName(makeStyleName(SelectionState.NotSelected));
         selectionCSSElement
               .addClassName(makeStyleName(selected ? SelectionState.Selected : SelectionState.NotSelected));
      }

      private String makeStyleName(SelectionState style) {
         return ICON_STYLE_BASE + "-" + style;
      }
   }

   private class TopMenuCategoryItem extends MenuEntry {

      public TopMenuCategoryItem(String title, Command command, String... styles) {
         super(IconStyle.Plus, title, command, styles);
         addStyleName("L1");
         setOpenState(false);
      }

      public void setOpenState(boolean open) {
         setBackgroundIconImageStyle(open ? IconStyle.Minus : IconStyle.Plus);
      }
   }

   private class TopMenuCategory extends VerticalPanel implements SubMenu {

      private final TopMenuCategoryItem ourCategoryItem;

      private final VerticalPanel actions = new VerticalPanel();

      public TopMenuCategory(String title, String... styles) {
         add(ourCategoryItem = new TopMenuCategoryItem(title, new Command() {
            public void execute() {
               if (TopMenuCategory.this.equals(currentlyOpen)) {
                  closeCategory();
               } else {
                  openCategory(TopMenuCategory.this);
               }
            }
         }, styles));
      }

      @Override
      protected void onAttach() {
         super.onAttach();
         new Timer() {
            @Override
            public void run() {
               int baseHeight = ourCategoryItem.getOffsetHeight();
               if (baseHeight < 10) {
                  schedule(50);
                  return;
               }
               getParent().setHeight(baseHeight + "px");
               add(actions);
            }
         }.schedule(50);
      }

      public void open() {
         ourCategoryItem.setOpenState(true);
         transitionHider(ourCategoryItem.getOffsetHeight(), getOffsetHeight());
      }

      public void close() {
         ourCategoryItem.setOpenState(false);
         transitionHider(getOffsetHeight(), ourCategoryItem.getOffsetHeight());
      }

      private void transitionHider(int fromHeight, int toHeight) {
         new CategoryAnimation(getParent(), fromHeight, toHeight);
      }

      public SubMenu addStyles(String... styles) {
         UIUtils.addStyles(this, styles);
         return this;
      }

      public void createAction(String title, Command command, String... styles) {
         actions.add(new SubMenuItem(title, command, styles));
      }

      public SubMenu createCategory(String title, String... styles) {
         throw new UnsupportedOperationException("Currently SubMenus do not support SubMenus");
      }

   }

   private static class CategoryAnimation extends Animation {

      private Widget widget;

      private int fromHeight;

      private int toHeight;

      private int overhead;

      private int height;

      private int adjuster;

      public CategoryAnimation(Widget widget, int fromHeight, int toHeight) {
         this.widget = widget;
         this.fromHeight = fromHeight;
         this.toHeight = toHeight;
         if (fromHeight < toHeight) { // Opening
            overhead = fromHeight;
            height = toHeight - fromHeight;
            adjuster = 0;
         } else { // Closing
            overhead = toHeight;
            height = fromHeight - toHeight;
            adjuster = height;
         }
         run(Math.min(700, 7 * height));
      }

      @Override
      protected void onUpdate(double progress) {
         int height = Math.abs(adjuster - ((int) (progress * this.height)));

         widget.setHeight((overhead + height) + "px");
      }

      @Override
      protected void onComplete() {
         super.onComplete();
         widget.setHeight(toHeight + "px");
      }

      @Override
      protected void onStart() {
         super.onStart();
         widget.setHeight(fromHeight + "px");
      }
   }

   private static class OurImage extends Image {
      public OurImage(String imageRef, String style) {
         super(imageRef);
         addStyleName(style);
      }
   }

   private static class OurLabel extends Label {
      public OurLabel(String text, String style) {
         super(text);
         addStyleName(style);
      }
   }

   private static class OurSimplePanel extends SimplePanel {
      public OurSimplePanel(Widget widget) {
         add(widget);
      }

      public OurSimplePanel(Widget widget, String style) {
         this(widget);
         addStyleName(style);
      }
   }

   private static class OurOverflowHiddenPanel extends OurSimplePanel {
      public OurOverflowHiddenPanel(Widget widget) {
         super(widget);

         getElement().getStyle().setOverflow(Overflow.HIDDEN);
      }
   }
}

Commits for litesoft/trunk/WDS-Temp/WDSStackMenuView.java

Diff revisions: vs.
Revision Author Commited Message
104 GeorgeS picture GeorgeS Tue 15 Feb, 2011 01:56:06 +0000