001    package org.apache.myfaces.tobago.example.demo;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    /*
021     * $Id: TobagoDemoController.java 765261 2009-04-15 16:24:07Z lofwyr $
022     */
023    
024    import org.apache.commons.logging.Log;
025    import org.apache.commons.logging.LogFactory;
026    import static org.apache.myfaces.tobago.TobagoConstants.ATTR_VALUE;
027    import static org.apache.myfaces.tobago.TobagoConstants.FACET_ITEMS;
028    import static org.apache.myfaces.tobago.TobagoConstants.RENDERER_TYPE_IN;
029    import org.apache.myfaces.tobago.component.ComponentUtil;
030    import org.apache.myfaces.tobago.component.UIInput;
031    import org.apache.myfaces.tobago.component.UISelectOne;
032    import org.apache.myfaces.tobago.context.ResourceManagerUtil;
033    import org.apache.myfaces.tobago.event.SheetStateChangeEvent;
034    import org.apache.myfaces.tobago.event.TabChangeListener;
035    import org.apache.myfaces.tobago.example.demo.model.solar.Solar;
036    import org.apache.myfaces.tobago.example.demo.model.solar.SolarObject;
037    import org.apache.myfaces.tobago.model.SheetState;
038    import org.apache.myfaces.tobago.model.TreeState;
039    import org.apache.myfaces.tobago.taglib.component.ToolBarTag;
040    
041    import javax.faces.application.FacesMessage;
042    import javax.faces.component.UIColumn;
043    import javax.faces.component.UIComponent;
044    import javax.faces.context.ExternalContext;
045    import javax.faces.context.FacesContext;
046    import javax.faces.event.ActionEvent;
047    import javax.faces.model.SelectItem;
048    import javax.faces.validator.ValidatorException;
049    import javax.servlet.http.HttpSession;
050    import javax.swing.tree.DefaultMutableTreeNode;
051    import javax.swing.tree.MutableTreeNode;
052    import java.io.IOException;
053    import java.util.ArrayList;
054    import java.util.Date;
055    import java.util.List;
056    
057    public class TobagoDemoController {
058    
059      private static final Log LOG = LogFactory.getLog(TobagoDemoController.class);
060    
061      private String[] salutation;
062    
063      private SelectItem[] salutationItems;
064    
065      private boolean[] bool;
066    
067      private boolean update;
068    
069      private Boolean boolTest;
070    
071      private String[] text;
072    
073      private SolarObject[] solarArray;
074    
075      private List<SolarObject> solarList;
076    
077      private List<UIColumn> solarArrayColumns;
078    
079      private String solarArrayColumnLayout;
080    
081      private TreeState treeState;
082    
083      private DefaultMutableTreeNode tree;
084    
085      private boolean showJunctions;
086    
087      private int value;
088    
089      private boolean showIcons;
090    
091      private boolean showRoot;
092    
093      private boolean showRootJunction;
094    
095      private boolean multiselect;
096    
097      private String selectionType;
098    
099      private SelectItem[] selectionItems;
100    
101      private boolean mutable;
102    
103      private boolean required;
104    
105      private Solar solar;
106    
107      private Date date = new Date();
108    
109      private Object tabState0;
110    
111      private Object tabState1;
112    
113      private Object tabState2;
114    
115      private Object tabState3;
116    
117      private SheetState sheetState;
118    
119      private String toolbarIconSize;
120    
121      private SelectItem[] toolbarIconItems;
122    
123      private String toolbarTextPosition;
124    
125      private SelectItem[] toolbarTextItems;
126    
127      private TabChangeListener tabChangeListener;
128    
129    
130      public TobagoDemoController() {
131    
132        String[] salutationKeys = {
133          "salutation_unknown",
134          "salutation_mr",
135          "salutation_mrs",
136          "salutation_family"
137        };
138    
139        salutationItems = getSelectItems(salutationKeys, "demo");
140        this.salutation = new String[]{"", "", "", ""};
141    
142        bool = new boolean[10];
143        bool[0] = true;
144        bool[1] = false;
145        bool[2] = true;
146        bool[3] = false;
147        bool[4] = true;
148        bool[5] = false;
149        bool[6] = true;
150        bool[7] = false;
151        bool[8] = true;
152        bool[9] = false;
153        boolTest = Boolean.TRUE;
154    
155        text = new String[11];
156        text[0] = "Value 1";
157        text[1] = "Value 2";
158        text[2] = "Value 3";
159        text[3] = "Value 4";
160        text[4] = "Value 5";
161        text[5] = "longer \nmultiline\nText";
162        text[6] = "you can't edit me";
163        text[7] = "inline";
164        text[8] = "labeled";
165        text[9] = "longer Text abcdefg hijklmnop";
166        text[10]
167            = "**strong text**\n\n__emphasis__\n\nnormaler text\n\n__dieses "
168            + "ist emphasis__\n\n**und nochmal strong**\n\n**__ strong und emphasis__**";
169        solarArray = SolarObject.getArray();
170        solarList = SolarObject.getList();
171        solarArrayColumns = createSolarArrayColumns();
172        solarArrayColumnLayout = "3*; 3*; 3*";
173    
174        tree = new DefaultMutableTreeNode(
175            new Node("Root Node", "root"));
176        tree.insert(new DefaultMutableTreeNode(new Node("Sports", "sports")), 0);
177        tree.insert(new DefaultMutableTreeNode(new Node("Movies", "movies")), 0);
178        DefaultMutableTreeNode music = new DefaultMutableTreeNode(
179            new Node("Music", "music"));
180        tree.insert(music, 0);
181        tree.insert(new DefaultMutableTreeNode(new Node("Games", "games")), 0);
182        MutableTreeNode temp = new DefaultMutableTreeNode(
183            new Node("Science", "science"));
184        temp.insert(
185            new DefaultMutableTreeNode(new Node("Geography", "geography")), 0);
186        temp.insert(
187            new DefaultMutableTreeNode(new Node("Mathematics", "math")), 0);
188        DefaultMutableTreeNode temp2 = new DefaultMutableTreeNode(
189            new Node("Astronomy", "astro"));
190        temp2.insert(new DefaultMutableTreeNode(new Node("Education", "edu")), 0);
191        temp2.insert(new DefaultMutableTreeNode(new Node("Pictures", "pic")), 0);
192        temp.insert(temp2, 2);
193        tree.insert(temp, 2);
194        treeState = new TreeState();
195        treeState.addExpandState(tree);
196        treeState.addSelection(temp2);
197        treeState.setMarker(music);
198        String[] values = {"none", "single", "singleLeafOnly", "multi", "multiLeafOnly"};
199        selectionItems = getSelectItems(values, "demo");
200        selectionType = (String) selectionItems[0].getValue();
201    
202        showIcons = true;
203        showJunctions = true;
204        showRoot = true;
205        showRootJunction = true;
206    
207        solar = new Solar();
208    
209        String[] toolbarIconKeys
210            = {ToolBarTag.ICON_OFF, ToolBarTag.ICON_SMALL, ToolBarTag.ICON_BIG};
211        toolbarIconItems = getSelectItems(toolbarIconKeys, "demo");
212        toolbarIconSize = ToolBarTag.ICON_SMALL;
213    
214        String[] toolbarTextKeys =
215            {ToolBarTag.LABEL_OFF, ToolBarTag.LABEL_BOTTOM, ToolBarTag.LABEL_RIGHT};
216        toolbarTextItems = getSelectItems(toolbarTextKeys, "demo");
217        toolbarTextPosition = ToolBarTag.LABEL_BOTTOM;
218      }
219    
220    
221      public boolean isUpdate() {
222        update = !update;
223        return update;
224      }
225    
226      public void resetSession() throws IOException {
227        FacesContext facesContext = FacesContext.getCurrentInstance();
228        HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);
229        if (session != null) {
230          session.invalidate();
231        }
232        ExternalContext externalContext = facesContext.getExternalContext();
233        externalContext.redirect(externalContext.getRequestContextPath());
234        facesContext.responseComplete(); 
235      }
236    
237      public TabChangeListener getTabChangeListener() {
238        LOG.error("getTabChangeListener " + tabChangeListener);
239        return tabChangeListener;
240      }
241    
242      public void setTabChangeListener(TabChangeListener tabChangeListener) {
243        LOG.error("Setting TabChangeListener " + tabChangeListener);
244        this.tabChangeListener = tabChangeListener;
245      }
246    
247      private List<UIColumn> createSolarArrayColumns() {
248    
249        List<UIColumn> columns = new ArrayList<UIColumn>(3);
250    
251        UIInput textbox = (UIInput)
252            ComponentUtil.createComponent(UIInput.COMPONENT_TYPE, RENDERER_TYPE_IN);
253        ComponentUtil.setStringProperty(
254            textbox, ATTR_VALUE, "#{luminary.population}");
255        columns.add(ComponentUtil.createColumn(
256            "#{overviewBundle.solarArrayPopulation}", "true", null, textbox));
257    
258        columns.add(ComponentUtil.createTextColumn(
259            "#{overviewBundle.solarArrayDistance}", "true", "right", "#{luminary.distance}"));
260    
261        columns.add(ComponentUtil.createTextColumn(
262            "#{overviewBundle.solarArrayPeriod}", "true", "right", "#{luminary.period}"));
263    
264        return columns;
265      }
266    
267      public static SelectItem[] getSelectItems(String[] keys, String bundle) {
268        FacesContext facesContext = FacesContext.getCurrentInstance();
269        SelectItem[] items = new SelectItem[keys.length];
270        for (int i = 0; i < items.length; i++) {
271          String label = ResourceManagerUtil.getPropertyNotNull(facesContext, bundle, keys[i]);
272          items[i] = new SelectItem(keys[i], label);
273        }
274        return items;
275      }
276    
277      public void updateTree() {
278    
279      }
280    
281      public static Node createNode(String name, String id) {
282        return new Node(name, id);
283      }
284    
285      public String clickButton() {
286        LOG.info("clickButton");
287        return "display";
288      }
289    
290      public String getSelectable() {
291        return multiselect ? "multi" : "none";
292      }
293    
294      public String[] getSalutation() {
295        return salutation;
296      }
297    
298      public void setSalutation(String[] salutation) {
299        this.salutation = salutation;
300      }
301    
302      public SelectItem[] getSalutationItems() {
303        return salutationItems;
304      }
305    
306      public void setSalutationItems(SelectItem[] salutationItems) {
307        this.salutationItems = salutationItems;
308      }
309    
310      public boolean[] getBool() {
311        return this.bool;
312      }
313    
314      public void setBool(boolean[] argBool) {
315        this.bool = argBool;
316      }
317    
318      public Boolean getBoolTest() {
319        return boolTest;
320      }
321    
322      public void setBoolTest(Boolean boolTest) {
323        this.boolTest = boolTest;
324      }
325    
326      public String[] getText() {
327        return text;
328      }
329    
330      public void setText(String[] text) {
331        this.text = text;
332      }
333    
334      public SolarObject[] getSolarArray() {
335        return solarArray;
336      }
337    
338      public void setSolarArray(SolarObject[] solarArray) {
339        this.solarArray = solarArray;
340      }
341    
342      public List<SolarObject> getSolarList() {
343        solarList.get(0).setDistance(value++);
344        if (value > 100) {
345          value = 0;
346        }
347        return filteredList == null ? solarList : filteredList;
348      }
349    
350      public void setSolarList(List<SolarObject> solarList) {
351        this.solarList = solarList;
352      }
353    
354      public List<UIColumn> getSolarArrayColumns() {
355        return solarArrayColumns;
356      }
357    
358      public void setSolarArrayColumns(List<UIColumn> solarArrayColumns) {
359        this.solarArrayColumns = solarArrayColumns;
360      }
361    
362      public String getSolarArrayColumnLayout() {
363        return solarArrayColumnLayout;
364      }
365    
366      public void setSolarArrayColumnLayout(String solarArrayColumnLayout) {
367        this.solarArrayColumnLayout = solarArrayColumnLayout;
368      }
369    
370      public DefaultMutableTreeNode getTree() {
371        return tree;
372      }
373    
374      public void setTree(DefaultMutableTreeNode tree) {
375        this.tree = tree;
376      }
377    
378      public TreeState getTreeState() {
379        return treeState;
380      }
381    
382      public void setTreeState(TreeState treeState) {
383        this.treeState = treeState;
384      }
385    
386      public boolean isShowJunctions() {
387        return showJunctions;
388      }
389    
390      public void setShowJunctions(boolean showJunctions) {
391        this.showJunctions = showJunctions;
392      }
393    
394      public boolean isShowIcons() {
395        return showIcons;
396      }
397    
398      public void setShowIcons(boolean showIcons) {
399        this.showIcons = showIcons;
400      }
401    
402      public boolean isShowRoot() {
403        return showRoot;
404      }
405    
406      public void setShowRoot(boolean showRoot) {
407        this.showRoot = showRoot;
408      }
409    
410      public boolean isShowRootJunction() {
411        return showRootJunction;
412      }
413    
414      public void setShowRootJunction(boolean showRootJunction) {
415        this.showRootJunction = showRootJunction;
416      }
417    
418      public boolean isMultiselect() {
419        return multiselect;
420      }
421    
422      public void setMultiselect(boolean multiselect) {
423        this.multiselect = multiselect;
424      }
425    
426      public String getSelectionType() {
427        return selectionType;
428      }
429    
430      public void setSelectionType(String selectionType) {
431        this.selectionType = selectionType;
432      }
433    
434      public SelectItem[] getSelectionItems() {
435        return selectionItems;
436      }
437    
438      public void setSelectionItems(SelectItem[] selectionItems) {
439        this.selectionItems = selectionItems;
440      }
441    
442      public boolean isMutable() {
443        return mutable;
444      }
445    
446      public void setMutable(boolean mutable) {
447        this.mutable = mutable;
448      }
449    
450      public boolean isRequired() {
451        return required;
452      }
453    
454      public void setRequired(boolean required) {
455        this.required = required;
456      }
457    
458      public Solar getSolar() {
459        return solar;
460      }
461    
462      public void setSolar(Solar solar) {
463        this.solar = solar;
464      }
465    
466      public Date getDate() {
467        return date;
468      }
469    
470      public void setDate(Date date) {
471        this.date = date;
472      }
473    
474      public Object getTabState0() {
475        return tabState0;
476      }
477    
478      public void setTabState0(Object tabState0) {
479        this.tabState0 = tabState0;
480      }
481    
482      public Object getTabState1() {
483        return tabState1;
484      }
485    
486      public void setTabState1(Object tabState1) {
487        this.tabState1 = tabState1;
488      }
489    
490      public Object getTabState2() {
491        return tabState2;
492      }
493    
494      public void setTabState2(Object tabState2) {
495        this.tabState2 = tabState2;
496      }
497    
498      public Object getTabState3() {
499        return tabState3;
500      }
501    
502      public void setTabState3(Object tabState3) {
503        this.tabState3 = tabState3;
504      }
505    
506      public SheetState getSheetState() {
507        return sheetState;
508      }
509    
510      public void setSheetState(SheetState sheetState) {
511        this.sheetState = sheetState;
512      }
513    
514      public void stateChangeListener(SheetStateChangeEvent e) {
515        LOG.error("SheetState has Changed: " + e);
516    
517      }
518    
519      public void customValidator(FacesContext context, UIComponent component, Object value) throws ValidatorException {
520        if (value == null) {
521          return;
522        }
523        if (!"tobago".equalsIgnoreCase(value.toString())) {
524          throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Please type in 'Tobago'",
525              "Please type in 'Tobago'"));
526        }
527      }
528    
529      public SelectItem[] getToolbarIconItems() {
530        return toolbarIconItems;
531      }
532    
533      public String getToolbarIconSize() {
534        return toolbarIconSize;
535      }
536    
537      public void setToolbarIconSize(String toolbarIconSize) {
538        this.toolbarIconSize = toolbarIconSize;
539      }
540    
541      public SelectItem[] getToolbarTextItems() {
542        return toolbarTextItems;
543      }
544    
545      public String getToolbarTextPosition() {
546        return toolbarTextPosition;
547      }
548    
549      public void setToolbarTextPosition(String toolbarTextPosition) {
550        this.toolbarTextPosition = toolbarTextPosition;
551      }
552    
553      public Object getNull() {
554        return null;
555      }
556    
557      public void setNull(Object o) {
558    
559      }
560    
561      private List<SolarObject> filteredList;
562      private SelectItem[] orbitItems;
563      private static final String SHOW_ALL_ORBITS = "Show all";
564    
565      public void filterOrbit(ActionEvent event) {
566        UISelectOne selectOne = (UISelectOne) event.getComponent().getFacet(FACET_ITEMS);
567        if (filteredList == null) {
568          filteredList = new ArrayList<SolarObject>();
569        } else {
570          filteredList.clear();
571        }
572    
573        String filter = (String) selectOne.getValue();
574        if (SHOW_ALL_ORBITS.equals(filter)) {
575          filteredList.addAll(solarList);
576        } else {
577          for (SolarObject solarObject : solarList) {
578            if (solarObject.getOrbit().equalsIgnoreCase(filter)) {
579              filteredList.add(solarObject);
580            }
581          }
582        }
583      }
584    
585      public SelectItem[] getOrbitItems() {
586        if (orbitItems == null) {
587          createOrbitItems();
588        }
589        return orbitItems;
590      }
591    
592      private SelectItem[] createOrbitItems() {
593        orbitItems = new SelectItem[11];
594        orbitItems[0] = new org.apache.myfaces.tobago.model.SelectItem(SHOW_ALL_ORBITS);
595        orbitItems[1] = new org.apache.myfaces.tobago.model.SelectItem("Sun");
596        orbitItems[2] = new org.apache.myfaces.tobago.model.SelectItem("Mercury");
597        orbitItems[3] = new org.apache.myfaces.tobago.model.SelectItem("Venus");
598        orbitItems[4] = new org.apache.myfaces.tobago.model.SelectItem("Earth");
599        orbitItems[5] = new org.apache.myfaces.tobago.model.SelectItem("Mars");
600        orbitItems[6] = new org.apache.myfaces.tobago.model.SelectItem("Jupiter");
601        orbitItems[7] = new org.apache.myfaces.tobago.model.SelectItem("Saturn");
602        orbitItems[8] = new org.apache.myfaces.tobago.model.SelectItem("Uranus");
603        orbitItems[9] = new org.apache.myfaces.tobago.model.SelectItem("Neptune");
604        orbitItems[10] = new org.apache.myfaces.tobago.model.SelectItem("Pluto");
605        return orbitItems;
606      }
607    
608    //  public void setOrbitFilter(String orbitFilter) {
609    //    this.orbitFilter = orbitFilter;
610    //  }
611    //
612    //  public String getOrbitFilter() {
613    //    return orbitFilter;
614    //  }
615    
616      public static class Node {
617    
618        private String name;
619    
620        private String id;
621    
622        public Node(String name, String id) {
623          this.name = name;
624          this.id = id;
625        }
626    
627        public String getName() {
628          return name;
629        }
630    
631        public void setName(String name) {
632          this.name = name;
633        }
634    
635        public String getId() {
636          return id;
637        }
638    
639        public void setId(String id) {
640          this.id = id;
641        }
642    
643        public String toString() {
644          return "Node name="+name+" id="+id;
645        }
646      }
647    }