Skip to content

Commit

Permalink
Merge pull request #2223 from diamonddevgroup/master
Browse files Browse the repository at this point in the history
Introduced monthChangedListener that fires only when a new month is selected
  • Loading branch information
shai-almog authored Sep 21, 2017
2 parents 36d50c9 + e6dc03d commit 99843a2
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion CodenameOne/src/com/codename1/ui/Calendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public class Calendar extends Container {
static final long WEEK = DAY * 7;
private EventDispatcher dispatcher = new EventDispatcher();
private EventDispatcher dataChangedListeners = new EventDispatcher();
private EventDispatcher monthChangedListeners = new EventDispatcher();
private long[] dates = new long[42];
private boolean changesSelectedDateEnabled = true;
private TimeZone tmz;
Expand Down Expand Up @@ -124,7 +125,8 @@ public Calendar(long time, TimeZone tmz) {
}

/**
* Constructs a calendar with the current date and time with left and right images set
* Constructs a calendar with the current date and time with left and right
* images set
*
* @param leftArrowImage an image for calendar left arrow
* @param rightArrowImage an image for calendar right arrow
Expand Down Expand Up @@ -437,6 +439,24 @@ public void removeActionListener(ActionListener l) {
mv.removeActionListener(l);
}

/**
* Fires when a new month is selected
*
* @param l listener to add
*/
public void addMonthChangedListener(ActionListener l) {
mv.addMonthChangedListener(l);
}

/**
* Fires when a new month is selected
*
* @param l listener to remove
*/
public void removeMonthChangedListener(ActionListener l) {
mv.removeMonthChangedListener(l);
}

/**
* Adds an ActionListener to the day buttons. This is different from
* {@code Calendar.addActionListener} and will only fire when an active day
Expand Down Expand Up @@ -1128,6 +1148,7 @@ public void setSelectedDay(long selectedDay) {
}

private void setMonth(int year, int month) {
fireMonthChangedEvent();
java.util.Calendar cal = java.util.Calendar.getInstance(tmz);
cal.setTimeZone(TimeZone.getDefault());
cal.set(java.util.Calendar.MONTH, month);
Expand Down Expand Up @@ -1171,6 +1192,14 @@ public void removeActionListener(ActionListener l) {
dispatcher.removeListener(l);
}

public void addMonthChangedListener(ActionListener l) {
monthChangedListeners.addListener(l);
}

public void removeMonthChangedListener(ActionListener l) {
monthChangedListeners.removeListener(l);
}

public void addDayActionListener(ActionListener l) {
dayListeners.add(l);
for (Component cmp : components) {
Expand Down Expand Up @@ -1209,6 +1238,10 @@ protected void fireActionEvent() {
dispatcher.fireActionEvent(new ActionEvent(Calendar.this, ActionEvent.Type.Calendar));
}

protected void fireMonthChangedEvent() {
monthChangedListeners.fireActionEvent(new ActionEvent(Calendar.this, ActionEvent.Type.Calendar));
}

public void actionPerformed(ActionEvent evt) {
Object src = evt.getSource();
if (src instanceof ComboBox) {
Expand Down

0 comments on commit 99843a2

Please sign in to comment.