DateWidget / OptionGroup

OptionGroup erweitert, so daß nun auch neue Optionen am Anfang der Liste hinzugefügt werden könnten (prependOption).

DateWidget speichert nun den eingestellten Jahresbereich und verfügt über eine neue Funktion, um weitere Jahreseinträge hinzuzufügen. Diese Methode fügt die Jahreszahl nur ein, wenn sie sich nicht im Bereich der Jahreszahlen befindet. Dabei wird darauf geachtet, daß Jahreszahlen, die kleiner als der definierte Bereich sind, am Anfang der Liste eingefügt werden. Größere Zahlen werden am Ende der Liste eingefügt.
Beim mehrfachen Aufrufen der Methode werden die Jahreszahlen nicht weiter sortiert, so daß die resultierende Liste nicht in der korrekten Reihenfolge erscheint. Das war auf Grund von Bebop nicht so ohne weiteres möglich, da das Widget nach der Initialisierung gesperrt wird, wodurch die bereits vorhandenen Felder nicht mehr geändert werden können. Das sollte aber im Normalfall kein Problem darstellen, da für die CTs, wo das Widget verwendet wird, diese Methode nur einmal aufgerufen werden muß.

git-svn-id: https://svn.libreccm.org/ccm/trunk@702 8810af33-2d31-482b-a856-94f89814c4df
master
quasi 2011-01-22 11:59:40 +00:00
parent d8bce613ca
commit ebbc22fbd5
2 changed files with 71 additions and 34 deletions

View File

@ -18,7 +18,6 @@
*/
package com.arsdigita.bebop.form;
import java.text.DateFormatSymbols;
import java.util.Calendar;
@ -55,7 +54,9 @@ public class Date extends Widget implements BebopConstants {
protected OptionGroup m_year;
protected OptionGroup m_month;
protected TextField m_day;
protected TextField m_day;
private int m_year_begin;
private int m_year_end;
// Inner classes for the fragment widgets
protected class YearFragment extends SingleSelect {
@ -78,15 +79,14 @@ public class Date extends Widget implements BebopConstants {
@Override
public Object getValue(PageState ps) {
Object value = parent.getFragmentValue(ps, Calendar.YEAR);
if (value == null) {
Calendar currentTime = GregorianCalendar.getInstance();
int currentYear = currentTime.get(Calendar.YEAR);
value = new Integer(currentYear);
}
return value;
Object value = parent.getFragmentValue(ps, Calendar.YEAR);
if (value == null) {
Calendar currentTime = GregorianCalendar.getInstance();
int currentYear = currentTime.get(Calendar.YEAR);
value = new Integer(currentYear);
}
return value;
}
}
protected class MonthFragment extends SingleSelect {
@ -111,7 +111,6 @@ public class Date extends Widget implements BebopConstants {
public Object getValue(PageState ps) {
return parent.getFragmentValue(ps, Calendar.MONTH);
}
}
protected class DayFragment extends TextField {
@ -144,10 +143,10 @@ public class Date extends Widget implements BebopConstants {
public Date(ParameterModel model) {
super(model);
if ( ! (model instanceof DateParameter)) {
if (!(model instanceof DateParameter)) {
throw new IllegalArgumentException(
"The Date widget " + model.getName() +
" must be backed by a DateParameter parmeter model");
"The Date widget " + model.getName()
+ " must be backed by a DateParameter parmeter model");
}
String name = model.getName();
@ -170,14 +169,14 @@ public class Date extends Widget implements BebopConstants {
m_day.setMaxLength(2);
m_day.setSize(2);
String [] months = dfs.getMonths();
String[] months = dfs.getMonths();
for (int i=0; i<months.length; i+=1) {
for (int i = 0; i < months.length; i += 1) {
// This check is necessary because
// java.text.DateFormatSymbols.getMonths() returns an array
// of 13 Strings: 12 month names and an empty string.
if ( months[i].length() > 0 ) {
m_month.addOption(new Option(String.valueOf(i),months[i]));
if (months[i].length() > 0) {
m_month.addOption(new Option(String.valueOf(i), months[i]));
}
}
int currentYear = currentTime.get(Calendar.YEAR);
@ -189,14 +188,32 @@ public class Date extends Widget implements BebopConstants {
this(new DateParameter(name));
}
public void setYearRange(int startYear, int endYear) {
public void setYearRange(int yearBegin, int yearEnd) {
Assert.isUnlocked(this);
m_year.clearOptions();
for (int j= startYear; j<=endYear; j+=1) {
m_year.addOption(new Option(String.valueOf(j)));
if (yearBegin != m_year_begin || yearEnd != m_year_end) {
m_year_begin = yearBegin;
m_year_end = yearEnd;
m_year.clearOptions();
for (int year = m_year_begin; year <= m_year_end; year += 1) {
m_year.addOption(new Option(String.valueOf(year)));
}
}
}
public void addYear(java.util.Date date) {
Calendar cal = new GregorianCalendar();
cal.setTime(date);
int year = (cal.get(Calendar.YEAR));
if (year < m_year_begin) {
m_year.prependOption(new Option(String.valueOf(year)));
}
if (year > m_year_end) {
m_year.addOption(new Option(String.valueOf(year)));
}
}
/**
* Returns a string naming the type of this widget.
*/
@ -226,7 +243,7 @@ public class Date extends Widget implements BebopConstants {
@Override
public void generateWidget(PageState ps, Element parent) {
if ( ! isVisible(ps) ) {
if (!isVisible(ps)) {
return;
}
@ -279,17 +296,17 @@ public class Date extends Widget implements BebopConstants {
*/
@Override
public void setForm(Form f) {
super .setForm(f);
m_year .setForm(f);
super.setForm(f);
m_year.setForm(f);
m_month.setForm(f);
m_day .setForm(f);
m_day.setForm(f);
}
public Object getFragmentValue(PageState ps, int field) {
Assert.exists(ps, "PageState");
FormData f = getForm().getFormData(ps);
if (f != null) {
java.util.Date value = (java.util.Date)f.get(getName());
java.util.Date value = (java.util.Date) f.get(getName());
if (value != null) {
Calendar c = Calendar.getInstance();
c.setTime(value);
@ -306,8 +323,4 @@ public class Date extends Widget implements BebopConstants {
m_day.setClassAttr(at);
super.setClassAttr(at);
}
// Don't lock
// @Override
// public void lock() {}
}

View File

@ -130,7 +130,25 @@ public abstract class OptionGroup extends Widget
* what its group was.
*/
public void addOption(Option opt) {
addOption(opt, null);
addOption(opt, null, false);
}
public void addOption(Option opt, PageState ps) {
addOption(opt, ps, false);
}
/**
* Adds a new option.at the beginning of the list
* @param opt The {@link Option} to be added. Note: the argument
* is modified and associated with this OptionGroup, regardless of
* what its group was.
*/
public void prependOption(Option opt) {
addOption(opt, null, true);
}
public void prependOption(Option opt, PageState ps) {
addOption(opt, ps, true);
}
public void removeOption(Option opt) {
@ -146,8 +164,9 @@ public abstract class OptionGroup extends Widget
* what its group was.
* @param ps the current page state. if ps is null, adds option to the
* default option list.
* @param prepend If true, prepend option to the list instead of appending it
*/
public void addOption(Option opt, PageState ps) {
public void addOption(Option opt, PageState ps, boolean prepend) {
ArrayList list = m_options;
if (ps == null) {
Assert.isUnlocked(this);
@ -155,7 +174,12 @@ public abstract class OptionGroup extends Widget
list = (ArrayList)m_requestOptions.get(ps);
}
opt.setGroup( this );
if(prepend == true) {
list.add(0, opt);
} else {
list.add(opt);
}
}