accounting-manager/src/main/java/org/gcube/portlets/admin/accountingmanager/client/filters/AccountingPeriodPanel.java

244 lines
7.9 KiB
Java

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: <a
* href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
*
*/
public class AccountingPeriodPanel extends SimpleContainer {
private EventBus eventBus;
private DateField startDate;
private DateField endDate;
private ListStore<AccountingPeriodMode> storeCombo;
private ComboBox<AccountingPeriodMode> 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<Date>());
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<Date>() {
@Override
public void onValueChange(ValueChangeEvent<Date> 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 MaxDateValidator(new Date()));
endDate.addValidator(new EmptyValidator<Date>());
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<Date>() {
@Override
public void onValueChange(ValueChangeEvent<Date> 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<AccountingPeriodMode>(props.id());
storeCombo.addAll(AccountingPeriodMode.asList());
comboPeriodMode = new ComboBox<AccountingPeriodMode>(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("<b>Temporal Constraint</b>");
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<AccountingPeriodMode> labelProvider) {
comboPeriodMode.addSelectionHandler(new SelectionHandler<AccountingPeriodMode>() {
public void onSelection(SelectionEvent<AccountingPeriodMode> 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() {
if (startDate.validate() && endDate.validate()) {
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 {
return null;
}
}
}