GWT DateBox - Disable specific date - java

I want to disable a specific date in my date box. I tried it with this code, but the specified date (current day) does not get disabled.
final DateBox dateBox = new DateBox();
dateBox.getDatePicker().addShowRangeHandler(new ShowRangeHandler<Date>() {
#Override
public void onShowRange(final ShowRangeEvent<Date> event) {
List<Date> disabledDates = new ArrayList<Date>();
disabledDates.add(new Date());
dateBox.getDatePicker().setTransientEnabledOnDates(false, disabledDates);
}
});
Is there an other way to do this?
Edit: Following the example of apanizo, the day 29.5 looks greyed out, but is clickable nevertheless.

Really sorry, I did not test the code of my last answer.
I have just tried and your code worked to me, the only thing that I did was pass all the dates that I do not want through the setTransientEnabledOnDates(false, dateToDisable); method.
Example:
public void onModuleLoad() {
final DateBox dateBox = new DateBox();
dateBox.getDatePicker().addShowRangeHandler(new ShowRangeHandler<Date>() {
#Override
public void onShowRange(final ShowRangeEvent<Date> dateShowRangeEvent) {
final Date today = new Date(); //30 May 3014
final Date yesterday = new Date(today.getTime()- 24*60*60*1000);
//disabling yesterday - 29 May of 2014
dateBox.getDatePicker().setTransientEnabledOnDates(false, yesterday);
}
});
RootPanel.get().add(dateBox);
}
If you want to disable the click event on a disabled date, this is a resolved and not delivered bug.
See:
https://code.google.com/p/google-web-toolkit/issues/detail?id=7876
I think in the GWT's 2.7 version it will be released, in the mean time you can patch it introducing in the line 77 of com.google.gwt.user.datepicker.client.CellGridImpl :
addDomHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
if (isActive(Cell.this)) {
setSelected(Cell.this);
}
}
}, ClickEvent.getType());
In detail here

Simply add a value changed listener on date picker to listen for any change in the date.
For example if current date is not allowed and it's selected then simply discard the changes and revert back to previous date or show a warning message as well if needed and add style for current date to look it as disabled.
Here date time format is used to check for date only, time is discarded while checking dates.
Sample code:
private Date prevDate;
...
final DateBox dateBox = new DateBox();
final DateTimeFormat dateTimeFormat=DateTimeFormat.getFormat("MM/dd/yyyy");
dateBox.getDatePicker().addValueChangeHandler(new ValueChangeHandler<Date>() {
#Override
public void onValueChange(ValueChangeEvent<Date> event) {
if (dateTimeFormat.format(event.getValue()).equals(dateTimeFormat.format(new Date()))) {
dateBox.setValue(prevDate);
// show warning message here
}else{
prevDate=event.getValue();
}
}
});

Related

Android cardview place specific card at the end of the list

I'm making an app where user-input tasks are shown in cardview. Each tasks have their own due dates and when the due date is up, it will show up at the bottom of the list.
In the code below,
The due dates are passed via my task object class. After comparing due dates (.getTaskDate) with current date, I should put the overdue task at the bottom here.
Inside my adapter:
//Overdue tasks
String Currdate = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault()).format(new Date());
if(Currdate.compareTo(tasks.getTaskDate())>0 )
{
taskViewHolder.cardView.setBackgroundColor(mContext.getResources().getColor(R.color.colorRed));
}
How should I implement this feature and is it done inside the MainActivity or my Adapter? I have similar date sorting feature which sorts the date in ascending order inside my MainActivity :
private static void sortDates(final List<TaskObject> listViewItems) {
Collections.sort(listViewItems, new Comparator<TaskObject>() {
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
#Override
public int compare(TaskObject t1, TaskObject t2) {
try {
return dateFormat.parse(t1.getTaskDate()).compareTo(dateFormat.parse(t2.getTaskDate()));
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
}
});
}
Just reverse your sorting and you are done. Change the return statement in the comparator to
return dateFormat.parse(t2.getTaskDate()).compareTo(dateFormat.parse(t1.getTaskDate()));

ChangeHandler not recognising a blank date

I am checking for a change in value of a date. The ValueChangeHandler is recognising a date (e.g. 1/5/2014 is updated to the DB when entered). However, when I delete a date it is not recognised (i.e., the DB is not updated to null - I have tried Backspace, highlight and Del, overtyping with spaces). I then entered a new date (2/5/2014) and this was updated to the DB. Any ideas as to why this code does not recognise that I have removed the date please.
Regards,
Glyn
I have updated this with the code suggested by Braj. Unfortunately this did not work.
final DateBox awardedDate = new DateBox();
awardedDate.setFormat(new DefaultFormat(DateTimeFormat.getFormat("dd/MM/yyyy")));
awardedDate.setValue(ymAwards.getCaAwardedDate());
awardedDate.setWidth("75px");
//Add change handler for the awarded date.
//Only a Leader or Administrator can update the date
if (accountLevel.equals("Leader") || accountLevel.equals("Administrator")) {
awardedDate.addValueChangeHandler(new ValueChangeHandler<java.util.Date>() {
int pog = 0;
public void onValueChange(ValueChangeEvent<java.util.Date> event) {
if (pog == 0) {
pog++;
Window.alert("First change hadler.");
//Check for a null date and handle it for dateBoxArchived and dateBoxPackOut
java.sql.Date sqlDateAwarded = awardedDate.getValue() == null ? null : new java.sql.Date(awardedDate.getValue().getTime());
AsyncCallback<YMAwards> callback = new YMAwardedDateHandler<YMAwards>();
rpc.updateYMAwarded(youthMemberID, returnAwID, sqlDateAwarded, callback);
}else{
pog = 0;
}
}
});
awardedDate.getTextBox().addValueChangeHandler(new ValueChangeHandler<String>() {
#Override
public void onValueChange(ValueChangeEvent<String> event) {
if (event.getValue() == null) {
Window.alert("Second change hadler.");
//Check for a null date and handle it for dateBoxArchived and dateBoxPackOut
java.sql.Date sqlDateAwarded = awardedDate.getValue() == null ? null : new java.sql.Date(awardedDate.getValue().getTime());
AsyncCallback<YMAwards> callback = new YMAwardedDateHandler<YMAwards>();
rpc.updateYMAwarded(youthMemberID, returnAwID, sqlDateAwarded, callback);
}
}
});
}
Add this line:
awardDate.setFireNullValues(true);
This was added in GWT 2.5.
Try this one also
final DateBox dateBox = new DateBox();
dateBox.getTextBox().addValueChangeHandler(new ValueChangeHandler<String>() {
#Override
public void onValueChange(ValueChangeEvent<String> event) {
if (dateBox.getValue() == null) {
System.out.println("date value is empty");
// your code here
}
}
});
output:
date value is empty
DateBox#addValueChangeHandler() fires when there is any change in date via date picker.
You can check the value in text box using TextBox#addValueChangeHandler().

GWT- DateBox.getTextBox().addValueChangeHandler() not fire event in IE

I tried to used DateBox and add a valueChangeHandler to the DateBox.getTextBox().
It works fine in FF, Chrome, but not IE9. The event is not fired even if the value in the textbox is changed.
I tried to just use TextBox.addValueChangeHandler() in IE9, it works as expected.
So the question is that how I can make the DateBox.getTextBox().addValueChangeHandler() work in IE9.
Can anyone confirm this bug? and any ideas to fix it?
Small piece of code to test:
DateBox dateBox = new DateBox();
RootLayoutPanel.get().add(dateBox);
dateBox.getTextBox().addValueChangeHandler(new ValueChangeHandler<String>() {
#Override
public void onValueChange(ValueChangeEvent<String> event) {
Window.alert("event fire");
}
});
Thanks.
Try this :
transactionDate.addValueChangeHandler(new ValueChangeHandler<Date>() {
public void onValueChange(ValueChangeEvent<Date> event) {
Window.alert(transactionDate.getValue().toString());
}
});
It's a known bug, it will be fixed in GWT 2.6.
Meanwhile you can use this workaround:
dateBox.addValueChangeHandler(new ValueChangeHandler<Date>() {
Date lastDate = null;
#Override
public void onValueChange(ValueChangeEvent<Date> arg0) {
Date newDate = dateBox.getValue();
newDate.setHours(0);
newDate.setMinutes(0);
newDate.setSeconds(0);
if (!newDate.equals(lastDate))
{
lastDate = newDate;
doSomething();
}
}});
https://code.google.com/p/google-web-toolkit/issues/detail?id=4785

Document Listener firing some times but not others

this one has been puzzling me for a few days now and I feel that I have barely been able to narrow it down.
I am using Java and have a wizard for the user to step through. One of the steps allows the user to select a start time & date and an end time & date to schedule some work. I thought I had the validation on the dates complete (so that the end date must be after the start date & start date must be after current date etc). However, my validation method only fired once focus was lost on either date TextField so if the user selected a new date and immediately clicked next, an invalid choice could continue -- bug!
The start and end date selectors are widgets which are made up of a JSpinner and a calendar dialog which pops up if button is clicked. I have attached a Document Listener to the text field of the JSpinner:
DocumentListener docListener = new DocumentListener() {
#Override
public void insertUpdate(DocumentEvent e) {
dateChanged();
System.out.println("insertUpdate");
}
#Override
public void removeUpdate(DocumentEvent e) {
dateChanged();
System.out.println("removeUpdate");
}
#Override
public void changedUpdate(DocumentEvent e) {
dateChanged();
System.out.println("changedUpdate");
}
};
((JSpinner.DefaultEditor) jSpinner1.getEditor()).getTextField().getDocument().addDocumentListener(
docListener);
When I run this class using its own main method to test:
public static void main(String[] args) {
DateSelectorWidget test = new DateSelectorWidget();
JFrame f = new JFrame("T E S T ");
f.getContentPane().setLayout(new BorderLayout());
f.getContentPane().add(test, BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
The DocListener fires each time and everything is fine. However this class is part of a bigger program and when it is called in it - the DocListener simply does not fire at all. An instance of the class is simply added to a panel in the wizard and yet it does not function the way it does when tested independently.
Any ideas anyone?
Thanks
My guess is that you directly or indirectly change the editor on the JSpinner after your line to retrieve, cast, get component, get model and add listener.

Date ranges in swingx's JXDatePicker?

Does anyone know if it's possible to selected multiple days with swingx's jxdatepicker? I'm using swingx version 1.6.1 (which looks to be the latest).
Looks like there are deprecated methods in the JXMonthView class...so I was able to get it to return all the selected dates by using the getSelection method of the JXMonthView class.
For instance....
JXDatePicker picker = new JXDatePicker(System.currentTimeMillis());
final JXMonthView monthView = picker.getMonthView();
monthView.setSelectionMode(JXMonthView.SelectionMode.SINGLE_INTERVAL_SELECTION);
monthView.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println((monthView.getSelection()));
}
});

Categories

Resources