package org.gcube.portlets.admin.accountingmanager.client.filters; import java.util.Date; import org.gcube.portlets.admin.accountingmanager.client.event.StateChangeEvent; import org.gcube.portlets.admin.accountingmanager.client.properties.AccountingPeriodModePropertiesCombo; import org.gcube.portlets.admin.accountingmanager.client.utils.UtilsGXT3; import org.gcube.portlets.admin.accountingmanager.shared.data.AccountingPeriod; import org.gcube.portlets.admin.accountingmanager.shared.data.AccountingPeriodMode; import com.allen_sauer.gwt.log.client.Log; import com.google.gwt.core.client.GWT; import com.google.gwt.event.logical.shared.SelectionEvent; import com.google.gwt.event.logical.shared.SelectionHandler; import com.google.gwt.event.logical.shared.ValueChangeEvent; import com.google.gwt.event.logical.shared.ValueChangeHandler; import com.google.gwt.event.shared.EventBus; import com.google.gwt.i18n.client.DateTimeFormat; import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat; import com.sencha.gxt.cell.core.client.form.ComboBoxCell.TriggerAction; import com.sencha.gxt.core.client.util.Margins; import com.sencha.gxt.data.shared.LabelProvider; import com.sencha.gxt.data.shared.ListStore; import com.sencha.gxt.widget.core.client.container.MarginData; import com.sencha.gxt.widget.core.client.container.SimpleContainer; import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer; import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData; import com.sencha.gxt.widget.core.client.event.ParseErrorEvent; import com.sencha.gxt.widget.core.client.form.ComboBox; import com.sencha.gxt.widget.core.client.form.DateField; import com.sencha.gxt.widget.core.client.form.FieldLabel; import com.sencha.gxt.widget.core.client.form.FieldSet; import com.sencha.gxt.widget.core.client.form.validator.EmptyValidator; import com.sencha.gxt.widget.core.client.form.validator.MaxDateValidator; import com.sencha.gxt.widget.core.client.info.Info; /** * * @author giancarlo email: g.panichi@isti.cnr.it * */ public class AccountingPeriodPanel extends SimpleContainer { private EventBus eventBus; private DateField startDate; private DateField endDate; private ListStore storeCombo; private ComboBox comboPeriodMode; public AccountingPeriodPanel(EventBus eventBus) { super(); Log.debug("AccountingPeriodPanel"); this.eventBus = eventBus; init(); create(); bindToEvents(); } private void init() { } private void create() { startDate = new DateField(); startDate.addValidator(new MaxDateValidator(new Date())); startDate.addValidator(new EmptyValidator()); startDate.addParseErrorHandler(new ParseErrorEvent.ParseErrorHandler() { @Override public void onParseError(ParseErrorEvent event) { Info.display("Parse Error", event.getErrorValue() + " could not be parsed as a date"); } }); startDate.addValueChangeHandler(new ValueChangeHandler() { @Override public void onValueChange(ValueChangeEvent event) { String v = event.getValue() == null ? "Nothing" : DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT) .format(event.getValue()); Info.display("Start Date Selected", v); } }); FieldLabel startDateLabel = new FieldLabel(startDate, "Start Date"); endDate = new DateField(); endDate.addValidator(new EmptyValidator()); endDate.addParseErrorHandler(new ParseErrorEvent.ParseErrorHandler() { @Override public void onParseError(ParseErrorEvent event) { Info.display("Parse Error", event.getErrorValue() + " could not be parsed as a date"); } }); endDate.addValueChangeHandler(new ValueChangeHandler() { @Override public void onValueChange(ValueChangeEvent event) { String v = event.getValue() == null ? "Nothing" : DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT) .format(event.getValue()); Info.display("End Date Selected", v); } }); FieldLabel endDateLabel = new FieldLabel(endDate, "End Date"); // Aggreagation Mode AccountingPeriodModePropertiesCombo props = GWT .create(AccountingPeriodModePropertiesCombo.class); storeCombo = new ListStore(props.id()); storeCombo.addAll(AccountingPeriodMode.asList()); comboPeriodMode = new ComboBox(storeCombo, props.label()); comboPeriodMode.setMinListWidth(250); comboPeriodMode.setEditable(false); comboPeriodMode.setTypeAhead(false); comboPeriodMode.setAllowBlank(false); comboPeriodMode.setTriggerAction(TriggerAction.ALL); comboPeriodMode.setValue(AccountingPeriodMode.DAILY); addHandlersForComboPeriodMode(props.label()); Log.debug("ComboPeriodMode created"); FieldLabel periodModeLabel = new FieldLabel(comboPeriodMode, "Aggregation"); VerticalLayoutContainer vlc = new VerticalLayoutContainer(); vlc.add(startDateLabel, new VerticalLayoutData(1, -1, new Margins(0))); vlc.add(endDateLabel, new VerticalLayoutData(1, -1, new Margins(0))); vlc.add(periodModeLabel, new VerticalLayoutData(1, -1, new Margins(0))); FieldSet fieldSet = new FieldSet(); fieldSet.setHeadingHtml("Temporal Constraint"); fieldSet.setCollapsible(false); fieldSet.add(vlc); add(fieldSet, new MarginData(0)); } // Bind to Events private void bindToEvents() { eventBus.addHandler(StateChangeEvent.TYPE, new StateChangeEvent.StateChangeEventHandler() { @Override public void onStateChange(StateChangeEvent event) { Log.debug("Catch Event State Change"); doStateChangeCommand(event); } }); } private void addHandlersForComboPeriodMode( final LabelProvider labelProvider) { comboPeriodMode .addSelectionHandler(new SelectionHandler() { public void onSelection( SelectionEvent event) { Info.display( "Aggregation", "You selected " + (event.getSelectedItem() == null ? "nothing" : labelProvider.getLabel(event .getSelectedItem()) + "!")); Log.debug("ComboPeriodMode selected: " + event.getSelectedItem()); } }); } private void doStateChangeCommand(StateChangeEvent event) { if (event.getStateChangeType() == null) { return; } switch (event.getStateChangeType()) { case Restore: onRestoreStateChange(event); break; case Update: break; default: break; } } private void onRestoreStateChange(StateChangeEvent event) { if (event.getAccountingStateData() != null && event.getAccountingStateData().getSeriesRequest() != null && event.getAccountingStateData().getSeriesRequest() .getAccountingPeriod() != null) { AccountingPeriod accountingPeriod = event.getAccountingStateData() .getSeriesRequest().getAccountingPeriod(); startDate.setValue(accountingPeriod.getStartDate()); endDate.setValue(accountingPeriod.getEndDate()); comboPeriodMode.setValue(accountingPeriod.getPeriod()); } else { startDate.reset(); endDate.reset(); comboPeriodMode.reset(); comboPeriodMode.setValue(AccountingPeriodMode.DAILY); } forceLayout(); } public AccountingPeriod getAccountingPeriod() { try { if (startDate.validate() && endDate.validate()) { if (endDate.getCurrentValue().compareTo(new Date()) <= 0) { Date startD = startDate.getCurrentValue(); Date endD = endDate.getCurrentValue(); if (startD.compareTo(endD) <= 0) { if (comboPeriodMode.validate() && comboPeriodMode.getCurrentValue() != null) { AccountingPeriod accountingPeriod = new AccountingPeriod( startD, endD, comboPeriodMode.getCurrentValue()); return accountingPeriod; } else { UtilsGXT3.alert("Attention", "Select a valid aggregation mode!"); return null; } } else { UtilsGXT3 .alert("Attention", "The start date must be less than or equal to the end date!"); return null; } } else { String endD=DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT).format(new Date()); UtilsGXT3 .alert("Attention", "The end date must be "+endD+" or earlier!"); return null; } } else { return null; } } catch(Throwable e){ Log.error(e.getLocalizedMessage()); e.printStackTrace(); UtilsGXT3 .alert("Attention", e.getLocalizedMessage()); return null; } } }