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.context.Markup;
23  import org.apache.myfaces.tobago.model.SelectItem;
24  import org.apache.myfaces.tobago.model.Selectable;
25  import org.apache.myfaces.tobago.model.SheetState;
26  import org.apache.myfaces.tobago.util.ComponentUtils;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  import javax.annotation.PostConstruct;
31  import javax.enterprise.context.SessionScoped;
32  import javax.faces.component.UIComponent;
33  import javax.faces.component.UIData;
34  import javax.faces.context.FacesContext;
35  import javax.faces.convert.DateTimeConverter;
36  import javax.faces.event.FacesEvent;
37  import javax.inject.Inject;
38  import javax.inject.Named;
39  import java.io.Serializable;
40  import java.lang.invoke.MethodHandles;
41  import java.util.ArrayList;
42  import java.util.Calendar;
43  import java.util.Date;
44  import java.util.GregorianCalendar;
45  import java.util.List;
46  import java.util.stream.Collectors;
47  
48  // XXX former @ViewAccessScoped
49  @SessionScoped
50  @Named
51  public class SheetController implements Serializable {
52  
53    private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
54  
55    private static final SelectItem[] SHEET_SELECTABLE;
56  
57    @Inject
58    private AstroData astroData;
59  
60    static {
61      final List<Selectable> collect = new ArrayList<>();
62      for (final Selectable selectable : Selectable.values()) {
63        if (selectable.isSupportedBySheet()) {
64          collect.add(selectable);
65        }
66      }
67      SHEET_SELECTABLE = new SelectItem[collect.size()];
68      for (int i = 0; i < collect.size(); i++) {
69        final Selectable selectable = collect.get(i);
70        SHEET_SELECTABLE[i] = new SelectItem(selectable, selectable.name());
71      }
72    }
73  
74    private List<SolarObject> solarList;
75    private List<SolarObject> hugeSolarList;
76    private SheetState sheetState;
77    private SolarObject selectedSolarObject;
78    private boolean automaticLayout;
79    private List<Markup> markup;
80    private int columnEventSample;
81    private Selectable selectable = Selectable.multi;
82  
83    @PostConstruct
84    private void init() {
85      solarList = astroData.findAll().collect(Collectors.toList());
86  
87      int j = 1;
88      hugeSolarList = new ArrayList<>();
89      while (true) {
90        for (final SolarObject solarObject : solarList) {
91          final SolarObject solarObjectClone = new SolarObject(solarObject);
92          hugeSolarList.add(solarObjectClone);
93          solarObjectClone.setName("#" + j++ + " " + solarObject.getName());
94  
95          if (j > 10000) {
96            return;
97          }
98        }
99      }
100   }
101 
102   public List<SolarObject> getSolarList() {
103     return solarList;
104   }
105 
106   public List<SolarObject> getHugeSolarList() {
107     return hugeSolarList;
108   }
109 
110   public SheetState getSheetState() {
111     return sheetState;
112   }
113 
114   public void setSheetState(final SheetState sheetState) {
115     this.sheetState = sheetState;
116   }
117 
118   public void selectSolarObject(final FacesEvent actionEvent) {
119     LOG.info("actionEvent=" + actionEvent);
120     final UIData data = ComponentUtils.findAncestor(actionEvent.getComponent(), UIData.class);
121     if (data != null) {
122       selectedSolarObject = (SolarObject) data.getRowData();
123       LOG.info("Selected: " + selectedSolarObject.getName());
124     } else {
125       selectedSolarObject = null;
126       LOG.info("Deselect.");
127     }
128   }
129 
130   public SolarObject getSelectedSolarObject() {
131     return selectedSolarObject;
132   }
133 
134   public boolean isAutomaticLayout() {
135     return automaticLayout;
136   }
137 
138   public void setAutomaticLayout(final boolean automaticLayout) {
139     this.automaticLayout = automaticLayout;
140   }
141 
142   public List<Markup> getMarkup() {
143     return markup;
144   }
145 
146   public void setMarkup(final List<Markup> markup) {
147     this.markup = markup;
148   }
149 
150   public void setColumnEventSample(final int columnEventSample) {
151     this.columnEventSample = columnEventSample;
152   }
153 
154   public int getColumnEventSample() {
155     return columnEventSample;
156   }
157 
158   public javax.faces.convert.Converter getYearConverter() {
159 
160     final DateTimeConverter dateTimeConverter = new DateTimeConverter() {
161 
162       @Override
163       public Object getAsObject(final FacesContext facesContext, final UIComponent uiComponent, final String value) {
164         final Date date = (Date) super.getAsObject(facesContext, uiComponent, value);
165         final Calendar calendar = GregorianCalendar.getInstance(facesContext.getViewRoot().getLocale());
166         calendar.setTime(date);
167         return calendar.get(Calendar.YEAR);
168       }
169 
170       @Override
171       public String getAsString(final FacesContext facesContext, final UIComponent uiComponent, final Object value) {
172         final Calendar calendar = GregorianCalendar.getInstance(facesContext.getViewRoot().getLocale());
173         calendar.set(Calendar.YEAR, (Integer) value);
174         final Date date = calendar.getTime();
175         return super.getAsString(facesContext, uiComponent, date);
176       }
177     };
178 
179     dateTimeConverter.setPattern("yyyy");
180     return dateTimeConverter;
181   }
182 
183   public Selectable getSelectable() {
184     return selectable;
185   }
186 
187   public void setSelectable(final Selectable selectable) {
188     this.selectable = selectable;
189   }
190 
191   public SelectItem[] getSelectableModes() {
192    return SHEET_SELECTABLE;
193   }
194 }