View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.example.demo;
21  
22  import org.apache.myfaces.tobago.component.UIBar;
23  import org.apache.myfaces.tobago.component.UIBox;
24  import org.apache.myfaces.tobago.component.UIButton;
25  import org.apache.myfaces.tobago.component.UIButtons;
26  import org.apache.myfaces.tobago.component.UIColumn;
27  import org.apache.myfaces.tobago.component.UIColumnNode;
28  import org.apache.myfaces.tobago.component.UIColumnSelector;
29  import org.apache.myfaces.tobago.component.UIDate;
30  import org.apache.myfaces.tobago.component.UIEvent;
31  import org.apache.myfaces.tobago.component.UIFigure;
32  import org.apache.myfaces.tobago.component.UIFile;
33  import org.apache.myfaces.tobago.component.UIFlexLayout;
34  import org.apache.myfaces.tobago.component.UIFlowLayout;
35  import org.apache.myfaces.tobago.component.UIFooter;
36  import org.apache.myfaces.tobago.component.UIForm;
37  import org.apache.myfaces.tobago.component.UIGridLayout;
38  import org.apache.myfaces.tobago.component.UIHeader;
39  import org.apache.myfaces.tobago.component.UIHidden;
40  import org.apache.myfaces.tobago.component.UIImage;
41  import org.apache.myfaces.tobago.component.UIIn;
42  import org.apache.myfaces.tobago.component.UILabel;
43  import org.apache.myfaces.tobago.component.UILink;
44  import org.apache.myfaces.tobago.component.UILinks;
45  import org.apache.myfaces.tobago.component.UIMessages;
46  import org.apache.myfaces.tobago.component.UIObject;
47  import org.apache.myfaces.tobago.component.UIOut;
48  import org.apache.myfaces.tobago.component.UIPage;
49  import org.apache.myfaces.tobago.component.UIPanel;
50  import org.apache.myfaces.tobago.component.UIPopup;
51  import org.apache.myfaces.tobago.component.UIProgress;
52  import org.apache.myfaces.tobago.component.UIRow;
53  import org.apache.myfaces.tobago.component.UISection;
54  import org.apache.myfaces.tobago.component.UISegmentLayout;
55  import org.apache.myfaces.tobago.component.UISelectBooleanCheckbox;
56  import org.apache.myfaces.tobago.component.UISelectManyCheckbox;
57  import org.apache.myfaces.tobago.component.UISelectManyListbox;
58  import org.apache.myfaces.tobago.component.UISelectManyShuttle;
59  import org.apache.myfaces.tobago.component.UISelectOneChoice;
60  import org.apache.myfaces.tobago.component.UISelectOneListbox;
61  import org.apache.myfaces.tobago.component.UISelectOneRadio;
62  import org.apache.myfaces.tobago.component.UISeparator;
63  import org.apache.myfaces.tobago.component.UISheet;
64  import org.apache.myfaces.tobago.component.UISuggest;
65  import org.apache.myfaces.tobago.component.UITab;
66  import org.apache.myfaces.tobago.component.UITabGroup;
67  import org.apache.myfaces.tobago.component.UITextarea;
68  import org.apache.myfaces.tobago.component.UITree;
69  import org.apache.myfaces.tobago.component.UITreeIcon;
70  import org.apache.myfaces.tobago.component.UITreeIndent;
71  import org.apache.myfaces.tobago.component.UITreeLabel;
72  import org.apache.myfaces.tobago.component.UITreeListbox;
73  import org.apache.myfaces.tobago.component.UITreeSelect;
74  import org.apache.myfaces.tobago.util.ComponentUtils;
75  
76  import javax.enterprise.context.SessionScoped;
77  import javax.faces.component.UIComponentBase;
78  import javax.faces.component.UIData;
79  import javax.faces.event.ActionEvent;
80  import javax.faces.event.AjaxBehaviorEvent;
81  import javax.faces.event.ValueChangeEvent;
82  import javax.inject.Named;
83  import java.io.Serializable;
84  import java.util.ArrayList;
85  import java.util.Collection;
86  import java.util.Date;
87  import java.util.List;
88  import java.util.Set;
89  import java.util.TreeSet;
90  
91  @SessionScoped
92  @Named
93  public class EventController implements Serializable {
94  
95    private final List<EventsOnComponent> eventsOnComponents = new ArrayList<>();
96    private EventsOnComponent selectedComponent;
97    private String eventName;
98    private int action = 0;
99    private int actionListener = 0;
100   private int ajaxListener = 0;
101   private int valueChangeListener = 0;
102   private final List<SolarObject> planets = new ArrayList<>();
103 
104   public EventController() {
105     eventsOnComponents.add(new EventsOnComponent(new UIBar()));
106     eventsOnComponents.add(new EventsOnComponent(new UIBox()));
107     eventsOnComponents.add(new EventsOnComponent(new UIButton()));
108     eventsOnComponents.add(new EventsOnComponent(new UIButtons()));
109     eventsOnComponents.add(new EventsOnComponent(new UIColumn()));
110     eventsOnComponents.add(new EventsOnComponent(new UIColumnNode()));
111     eventsOnComponents.add(new EventsOnComponent(new UIColumnSelector()));
112     eventsOnComponents.add(new EventsOnComponent(new UIDate()));
113     eventsOnComponents.add(new EventsOnComponent(new UIEvent()));
114     eventsOnComponents.add(new EventsOnComponent(new UIFigure()));
115     eventsOnComponents.add(new EventsOnComponent(new UIFile()));
116     eventsOnComponents.add(new EventsOnComponent(new UIFlexLayout()));
117     eventsOnComponents.add(new EventsOnComponent(new UIFlowLayout()));
118     eventsOnComponents.add(new EventsOnComponent(new UIFooter()));
119     eventsOnComponents.add(new EventsOnComponent(new UIForm()));
120     eventsOnComponents.add(new EventsOnComponent(new UIGridLayout()));
121     eventsOnComponents.add(new EventsOnComponent(new UIHeader()));
122     eventsOnComponents.add(new EventsOnComponent(new UIHidden()));
123     eventsOnComponents.add(new EventsOnComponent(new UIImage()));
124     eventsOnComponents.add(new EventsOnComponent(new UIIn()));
125     eventsOnComponents.add(new EventsOnComponent(new UILabel()));
126     eventsOnComponents.add(new EventsOnComponent(new UILink()));
127     eventsOnComponents.add(new EventsOnComponent(new UILinks()));
128     eventsOnComponents.add(new EventsOnComponent(new UIMessages()));
129     eventsOnComponents.add(new EventsOnComponent(new UIObject()));
130     eventsOnComponents.add(new EventsOnComponent(new UIOut()));
131     eventsOnComponents.add(new EventsOnComponent(new UIPage()));
132     eventsOnComponents.add(new EventsOnComponent(new UIPanel()));
133     eventsOnComponents.add(new EventsOnComponent(new UIPopup()));
134     eventsOnComponents.add(new EventsOnComponent(new UIProgress()));
135     eventsOnComponents.add(new EventsOnComponent(new UIRow()));
136     eventsOnComponents.add(new EventsOnComponent(new UISection()));
137     eventsOnComponents.add(new EventsOnComponent(new UISegmentLayout()));
138     eventsOnComponents.add(new EventsOnComponent(new UISelectBooleanCheckbox()));
139     eventsOnComponents.add(new EventsOnComponent(new UISelectManyCheckbox()));
140     eventsOnComponents.add(new EventsOnComponent(new UISelectManyListbox()));
141     eventsOnComponents.add(new EventsOnComponent(new UISelectManyShuttle()));
142     eventsOnComponents.add(new EventsOnComponent(new UISelectOneChoice()));
143     eventsOnComponents.add(new EventsOnComponent(new UISelectOneListbox()));
144     eventsOnComponents.add(new EventsOnComponent(new UISelectOneRadio()));
145     eventsOnComponents.add(new EventsOnComponent(new UISeparator()));
146     eventsOnComponents.add(new EventsOnComponent(new UISheet()));
147     eventsOnComponents.add(new EventsOnComponent(new UISuggest()));
148     eventsOnComponents.add(new EventsOnComponent(new UITab()));
149     eventsOnComponents.add(new EventsOnComponent(new UITabGroup()));
150     eventsOnComponents.add(new EventsOnComponent(new UITextarea()));
151     eventsOnComponents.add(new EventsOnComponent(new UITree()));
152     eventsOnComponents.add(new EventsOnComponent(new UITreeIcon()));
153     eventsOnComponents.add(new EventsOnComponent(new UITreeIndent()));
154     eventsOnComponents.add(new EventsOnComponent(new UITreeLabel()));
155     eventsOnComponents.add(new EventsOnComponent(new UITreeListbox()));
156     eventsOnComponents.add(new EventsOnComponent(new UITreeSelect()));
157 
158 
159     planets.add(new SolarObject("Mercury", "I", "Sun", 57910, 87.97, 7.00, 0.21, "-", null));
160     planets.add(new SolarObject("Venus", "II", "Sun", 108200, 224.70, 3.39, 0.01, "-", null));
161     planets.add(new SolarObject("Earth", "III", "Sun", 149600, 365.26, 0.00, 0.02, "-", null));
162     planets.add(new SolarObject("Mars", "IV", "Sun", 227940, 686.98, 1.85, 0.09, "-", null));
163     planets.add(new SolarObject("Jupiter", "V", "Sun", 778330, 4332.71, 1.31, 0.05, "-", null));
164     planets.add(new SolarObject("Saturn", "VI", "Sun", 1429400, 10759.50, 2.49, 0.06, "-", null));
165     planets.add(new SolarObject("Uranus", "VII", "Sun", 2870990, 30685.0, 0.77, 0.05, "Herschel", 1781));
166     planets.add(new SolarObject("Neptune", "VIII", "Sun", 4504300, 60190.0, 1.77, 0.01, "Adams", 1846));
167   }
168 
169   public void reset() {
170     action = 0;
171     actionListener = 0;
172     ajaxListener = 0;
173     valueChangeListener = 0;
174   }
175 
176   public List<EventsOnComponent> getEventsOnComponents() {
177     return eventsOnComponents;
178   }
179 
180   public EventsOnComponent getSelectedComponent() {
181     return selectedComponent;
182   }
183 
184   public String getEventName() {
185     return eventName;
186   }
187 
188   public void selectComponent(final ActionEvent actionEvent) {
189     final UIData data = ComponentUtils.findAncestor(actionEvent.getComponent(), UIData.class);
190     selectedComponent = data != null ? ((EventsOnComponent) data.getRowData()) : null;
191     eventName = actionEvent.getComponent().getAttributes().get("eventName").toString();
192   }
193 
194   public void action() {
195     action++;
196   }
197 
198   public void actionListener(final ActionEvent event) {
199     actionListener++;
200   }
201 
202   public void ajaxListener(final AjaxBehaviorEvent event) {
203     ajaxListener++;
204   }
205 
206   public void valueChangeListener(final ValueChangeEvent event) {
207     valueChangeListener++;
208   }
209 
210   public int getActionCount() {
211     return action;
212   }
213 
214   public int getActionListenerCount() {
215     return actionListener;
216   }
217 
218   public int getAjaxListenerCount() {
219     return ajaxListener;
220   }
221 
222   public int getValueChangeListenerCount() {
223     return valueChangeListener;
224   }
225 
226   public long getCurrentTimestamp() {
227     return new Date().getTime();
228   }
229 
230   public List<SolarObject> getPlanets() {
231     return planets;
232   }
233 
234   public static class EventsOnComponent implements Serializable {
235     private final String tagName;
236     private final Collection<String> eventNames = new TreeSet<>();
237 
238     EventsOnComponent(final UIComponentBase component) {
239       final String simpleName = component.getClass().getSimpleName();
240       tagName = simpleName.substring(2, 3).toLowerCase() + simpleName.substring(3);
241       if (component.getEventNames() != null) {
242         this.eventNames.addAll(component.getEventNames());
243       }
244     }
245 
246     public String getTagName() {
247       return tagName;
248     }
249 
250     public boolean hasBlurEvent() {
251       return eventNames.contains(CommonEvent.blur.name());
252     }
253 
254     public boolean hasChangeEvent() {
255       return eventNames.contains(CommonEvent.change.name());
256     }
257 
258     public boolean hasClickEvent() {
259       return eventNames.contains(CommonEvent.click.name());
260     }
261 
262     public boolean hasDblclickEvent() {
263       return eventNames.contains(CommonEvent.dblclick.name());
264     }
265 
266     public boolean hasFocusEvent() {
267       return eventNames.contains(CommonEvent.focus.name());
268     }
269 
270     public boolean hasKeydownEvent() {
271       return eventNames.contains(CommonEvent.keydown.name());
272     }
273 
274     public boolean hasKeypressEvent() {
275       return eventNames.contains(CommonEvent.keypress.name());
276     }
277 
278     public boolean hasKeyupEvent() {
279       return eventNames.contains(CommonEvent.keyup.name());
280     }
281 
282     public boolean hasMousedownEvent() {
283       return eventNames.contains(CommonEvent.mousedown.name());
284     }
285 
286     public boolean hasMousemoveEvent() {
287       return eventNames.contains(CommonEvent.mousemove.name());
288     }
289 
290     public boolean hasMouseoutEvent() {
291       return eventNames.contains(CommonEvent.mouseout.name());
292     }
293 
294     public boolean hasMouseoverEvent() {
295       return eventNames.contains(CommonEvent.mouseover.name());
296     }
297 
298     public boolean hasMouseupEvent() {
299       return eventNames.contains(CommonEvent.mouseup.name());
300     }
301 
302     public boolean hasSelectEvent() {
303       return eventNames.contains(CommonEvent.select.name());
304     }
305 
306     public String getSpecialEvents() {
307       final Set<String> specialEventNames = new TreeSet<>();
308 
309       for (final String name : eventNames) {
310         boolean isSpecialEvent = true;
311         for (final CommonEvent commonEvent : CommonEvent.values()) {
312           if (name.equals(commonEvent.name())) {
313             isSpecialEvent = false;
314             break;
315           }
316         }
317 
318         if (isSpecialEvent) {
319           specialEventNames.add(name);
320         }
321       }
322 
323       return specialEventNames.size() > 0 ? concatStrings(specialEventNames) : "";
324     }
325 
326     private String concatStrings(final Set<String> strings) {
327       final StringBuilder stringBuilder = new StringBuilder();
328 
329       int i = 0;
330       for (final String string : strings) {
331         i++;
332         stringBuilder.append(string);
333         if (i < strings.size()) {
334           stringBuilder.append(", ");
335         }
336       }
337       return stringBuilder.toString();
338     }
339   }
340 
341   private enum CommonEvent {
342     change,
343     click, dblclick,
344     focus, blur,
345     keydown, keypress, keyup,
346     mousedown, mousemove, mouseout, mouseover, mouseup,
347     select
348   }
349 }