Has this been resolved?

Martijn


On 9/7/05, Nick Heudecker <[EMAIL PROTECTED]> wrote:
Okay, I've got this thing just about done, but getModelObject() is final in Component.  How do I take the inputs from three separate DropDownChoices and return a Time object if not through getModelObject()?

And if I use another method, will Page's model object automatically be updated with the appropriate Time value? 

Thanks for your help.  Here's the non-compiling code that I have so far:

import wicket.MarkupContainer;
import wicket.markup.html.form.DropDownChoice;
import wicket.model.Model;

import java.sql.Time;
import java.util.Arrays;
import java.util.List;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class TimeSelector extends MarkupContainer {

    private static final List<String> hourList =
        Arrays.asList(new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"});
    private static final List<String> minuteList =
        Arrays.asList(new String[]{"00", "05", "10", "15", "20", "25", "30", "35", "40", "45", "50", "55"});
    private static final List<String> amPmList =
        Arrays.asList(new String[]{"AM", "PM"});
    private DropDownChoice hours;
    private DropDownChoice minutes;
    private DropDownChoice amPm;
    private Calendar c = Calendar.getInstance();


    public TimeSelector(final String id, Model m) {
        super(id, m);
        Time t = (Time) getModelObject();

        Model hourModel = new Model(getValueFromModel(t, Calendar.HOUR));
        hours = new DropDownChoice("hour", hourModel, hourList);
        add(hours);

        Model minuteModel = new Model(getValueFromModel(t, Calendar.MINUTE));
        minutes = new DropDownChoice("minute", minuteModel, minuteList);
        add(minutes);

        Model amPmModel = new Model(getValueFromModel(t, Calendar.AM_PM));
        amPm = new DropDownChoice("amPm", amPmModel, amPmList);
        add(amPm);
    }

    public TimeSelector(final String id, Time t) {
        this(id, new Model(t));
    }

    private String getValueFromModel(Time t, int component) {
        c.setTime(t);
        switch (component) {
            case Calendar.HOUR:
                return Integer.toString(c.get(Calendar.HOUR));
            case Calendar.MINUTE:
                return Integer.toString(c.get(Calendar.MINUTE));
            default:
                return (c.get(Calendar.AM_PM) == Calendar.AM) ? "AM" : "PM";
        }
    }

    public Object getModelObject() {
        GregorianCalendar gc = new GregorianCalendar();
        String hourValue = hours.getModelObjectAsString();
        String minuteValue = minutes.getModelObjectAsString();
        String amPmValue = amPm.getModelObjectAsString();
        gc.set(Calendar.HOUR, Integer.parseInt(hourValue));
        gc.set(Calendar.MINUTE, Integer.parseInt(minuteValue));
        if (amPmValue.equals("AM")) {
            gc.set(Calendar.AM_PM, Calendar.AM);
        }
        else {
            gc.set(Calendar.AM_PM, Calendar.PM);
        }

        return new Time(gc.getTime().getTime());

    }

}


---------- Forwarded message ----------
From: Nick Heudecker < [EMAIL PROTECTED]>
Date: Sep 6, 2005 4:15 PM
Subject: Time Selection Component
To: [email protected]

I need to build a simple drop-down time component that allows the user to select an hour, minute (in 5-minute increments) and AM/PM.  Ideally, the calling code would look something like:

TimeSelector ts = new TimeSelector("myModelName", myModelObject);
add(ts);

So the TimeSelector class would have three DropDownChoices.  What actually returns the selected Time?  getModelObject()?



Reply via email to