001    package org.hackystat.projectbrowser.page.sensordata;
002    
003    import java.text.SimpleDateFormat;
004    import java.util.ArrayList;
005    import java.util.Calendar;
006    import java.util.List;
007    import java.util.Locale;
008    
009    import org.apache.wicket.markup.html.form.DropDownChoice;
010    import org.apache.wicket.markup.html.form.Form;
011    import org.apache.wicket.markup.html.form.IChoiceRenderer;
012    import org.apache.wicket.model.PropertyModel;
013    import org.hackystat.projectbrowser.ProjectBrowserSession;
014    import org.hackystat.projectbrowser.ProjectChoiceRenderer;
015    
016    /**
017     * Provides the form that specifies a Month and Project name for the SensorData page.  
018     * @author Philip Johnson
019     */
020    public class SensorDataForm extends Form {
021    
022      /** Support serialization. */
023      private static final long serialVersionUID = 1L;
024      
025      /** The page that instantiated this form. */
026      private SensorDataPage page; 
027      
028      /**
029       * Create this form, supplying the wicket:id and the page. 
030       * 
031       * @param id The wicket:id.
032       * @param page The page using this form. 
033       */
034      public SensorDataForm(String id, SensorDataPage page) {
035        super(id);
036        SensorDataSession session = ProjectBrowserSession.get().getSensorDataSession();
037        this.page = page; 
038        
039        // The drop-down menu for months.
040        DropDownChoice monthMenu = 
041          new DropDownChoice("monthMenu", new PropertyModel(session, "month"), getMonths(), 
042              new IChoiceRenderer() {
043                /** Support serialization. */
044                private static final long serialVersionUID = 1L;
045    
046                /**
047                 * Convert the passed integer to its month name.
048                 * @param object the month as an int.
049                 */
050                public Object getDisplayValue(Object object) {
051                  SimpleDateFormat format = new SimpleDateFormat("MMM", Locale.US);
052                  Calendar cal = Calendar.getInstance();
053                  cal.set(Calendar.DAY_OF_MONTH, 1);
054                  cal.set(Calendar.MONTH, ((Integer) object).intValue());
055                  return format.format(cal.getTime());
056                }
057    
058                /** Required for IChoiceRenderer interface. */
059                public String getIdValue(Object arg0, int index) {
060                  return String.valueOf(index);
061                }
062              });
063        add(monthMenu);
064        
065        // Create the drop-down menu for years. 
066        DropDownChoice yearMenu = 
067          new DropDownChoice("yearMenu", new PropertyModel(session, "year"), getYears());
068        add(yearMenu);
069        
070        // Create the drop-down menu for projects. 
071        DropDownChoice projectMenu = 
072          new DropDownChoice ("projectMenu", 
073              new PropertyModel(session, "project"),
074              new PropertyModel(ProjectBrowserSession.get(), "projectList"),
075              new ProjectChoiceRenderer());
076        add(projectMenu);
077      }
078      /**
079       * Create the summary table associated with the user's project and date selection. 
080       */
081      @Override
082      public void onSubmit() {
083        page.onProjectDateSubmit();
084      }
085      
086      /**
087       * Return a list of years, which is a list of ten years where the current year is year 3.
088       * 
089       * @return The list of years.
090       */
091      private List<Integer> getYears() {
092        List<Integer> years = new ArrayList<Integer>(5);
093        Calendar cal = Calendar.getInstance();
094        for (int i = cal.get(Calendar.YEAR) - 5; i <= cal.get(Calendar.YEAR); i++) {
095          years.add(Integer.valueOf(i));
096        }
097        return years;
098      }
099      
100      /**
101       * Return a list of months, which is simply a list of ints from 0 to 11.
102       * 
103       * @return The list of months as integers.
104       */
105      private List<Integer> getMonths() {
106        List<Integer> months = new ArrayList<Integer>(12);
107        for (int i = 0; i < 12; i++) {
108          months.add(Integer.valueOf(i));
109        }
110        return months;
111      }
112    
113    }