Skip to content

Commit 1c8586d

Browse files
author
tliebeck
committed
Added ActionListener support to CalendarSelect.
1 parent bb132e0 commit 1c8586d

5 files changed

Lines changed: 127 additions & 2 deletions

File tree

src/client/extras/Application.CalendarSelect.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* showing the currently selected month/year. May not contain child components.
44
*
55
* @cp {Date} date the selected date
6+
* @sp {String} actionCommand the action command fired in action events when a date is selected
67
* @sp {#Color} adjacentMonthDateBackground background color for dates in previous/next months
78
* @sp {#Color} adjacentMonthDateForeground foreground color for dates in previous/next months
89
* @sp {#Border} border the border wrapping the calendar
@@ -25,6 +26,8 @@
2526
* @sp {#Border} selectedDateBorder border of selected date
2627
* @sp {#FillImage} selectedDateBackgroundImage background image of selected date
2728
* @sp {#Color} selectedDateForeground foreground color of selected date
29+
* @event action An event fired when the date selection changes. The <code>actionCommand</code> property of the pressed
30+
* button is provided as a property.
2831
*/
2932
Extras.CalendarSelect = Core.extend(Echo.Component, {
3033

@@ -33,5 +36,12 @@ Extras.CalendarSelect = Core.extend(Echo.Component, {
3336
},
3437

3538
/** @see Echo.Component#componentType */
36-
componentType: "Extras.CalendarSelect"
37-
});
39+
componentType: "Extras.CalendarSelect",
40+
41+
/**
42+
* Programmatically performs a date selection action.
43+
*/
44+
doAction: function() {
45+
this.fireEvent({type: "action", source: this, actionCommand: this.get("actionCommand")});
46+
}
47+
});

src/client/extras/Sync.CalendarSelect.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,8 @@ Extras.Sync.CalendarSelect = Core.extend(Echo.Render.ComponentSync, {
10641064
this._updateMonthYearSelection();
10651065

10661066
this._storeValue();
1067+
1068+
this.component.doAction();
10671069
},
10681070

10691071
/**

src/server-java/app/nextapp/echo/extras/app/CalendarSelect.java

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,27 @@
3030
package nextapp.echo.extras.app;
3131

3232
import java.util.Date;
33+
import java.util.EventListener;
3334

3435
import nextapp.echo.app.Border;
3536
import nextapp.echo.app.Color;
3637
import nextapp.echo.app.Component;
3738
import nextapp.echo.app.FillImage;
39+
import nextapp.echo.app.event.ActionEvent;
40+
import nextapp.echo.app.event.ActionListener;
3841

3942
/**
4043
* <code>CalendarSelect</code> component: an input component which allows selection of a single date. Displays a representation of a
4144
* calendar, showing the currently selected month/year. May not contain child components.
4245
*/
4346
public class CalendarSelect extends Component {
4447

48+
public static final String ACTION_LISTENERS_CHANGED_PROPERTY = "actionListeners";
49+
50+
public static final String INPUT_ACTION = "action";
51+
4552
public static final String DATE_CHANGED_PROPERTY = "date";
53+
public static final String PROPERTY_ACTION_COMMAND = "actionCommand";
4654
public static final String PROPERTY_ADJACENT_MONTH_DATE_BACKGROUND = "adjacentMonthDateBackground";
4755
public static final String PROPERTY_ADJACENT_MONTH_DATE_FOREGROUND = "adjacentMonthDateForeground";
4856
public static final String PROPERTY_BORDER = "border";
@@ -87,6 +95,51 @@ public CalendarSelect(Date date) {
8795
this.date = date;
8896
}
8997

98+
/**
99+
* Adds an <code>ActionListener</code> to the <code>CalendarSelect</code>.
100+
* The <code>ActionListener</code> will be invoked each time a date is selected,
101+
* either by changing month, day, or year.
102+
* <p>
103+
* Beware of using this listener to *enforce* date selection within a range, as
104+
* the user may intend to modify one or parameter of a date such that it may
105+
* be temporarily invalid.
106+
*
107+
* @param l the <code>ActionListener</code> to add
108+
*/
109+
public void addActionListener(ActionListener l) {
110+
getEventListenerList().addListener(ActionListener.class, l);
111+
// Notification of action listener changes is provided due to
112+
// existence of hasActionListeners() method.
113+
firePropertyChange(ACTION_LISTENERS_CHANGED_PROPERTY, null, l);
114+
}
115+
116+
/**
117+
* Fires an action event to all listeners.
118+
*/
119+
private void fireActionEvent() {
120+
if (!hasEventListenerList()) {
121+
return;
122+
}
123+
EventListener[] listeners = getEventListenerList().getListeners(ActionListener.class);
124+
ActionEvent e = null;
125+
for (int i = 0; i < listeners.length; ++i) {
126+
if (e == null) {
127+
e = new ActionEvent(this, (String) getRenderProperty(PROPERTY_ACTION_COMMAND));
128+
}
129+
((ActionListener) listeners[i]).actionPerformed(e);
130+
}
131+
}
132+
133+
/**
134+
* Returns the action command which will be provided in
135+
* <code>ActionEvent</code>s fired by this <code>CalendarSelect</code>.
136+
*
137+
* @return the action command
138+
*/
139+
public String getActionCommand() {
140+
return (String) get(PROPERTY_ACTION_COMMAND);
141+
}
142+
90143
/**
91144
* Returns the background color of dates in adjacent (previous/next) months.
92145
*
@@ -279,13 +332,49 @@ public Color getSelectedDateForeground() {
279332
return (Color) get(PROPERTY_SELECTED_DATE_FOREGROUND);
280333
}
281334

335+
/**
336+
* Determines if any <code>ActionListener</code>s are registered.
337+
*
338+
* @return true if any action listeners are registered
339+
*/
340+
public boolean hasActionListeners() {
341+
return hasEventListenerList() && getEventListenerList().getListenerCount(ActionListener.class) != 0;
342+
}
343+
282344
/**
283345
* @see nextapp.echo.app.Component#processInput(java.lang.String, java.lang.Object)
284346
*/
285347
public void processInput(String inputName, Object inputValue) {
286348
if (DATE_CHANGED_PROPERTY.equals(inputName)) {
287349
setDate((Date) inputValue);
350+
} else if (INPUT_ACTION.equals(inputName)) {
351+
fireActionEvent();
352+
}
353+
}
354+
355+
/**
356+
* Removes an <code>ActionListener</code> from the <code>CalendarSelect</code>.
357+
*
358+
* @param l the <code>ActionListener</code> to remove
359+
*/
360+
public void removeActionListener(ActionListener l) {
361+
if (!hasEventListenerList()) {
362+
return;
288363
}
364+
getEventListenerList().removeListener(ActionListener.class, l);
365+
// Notification of action listener changes is provided due to
366+
// existence of hasActionListeners() method.
367+
firePropertyChange(ACTION_LISTENERS_CHANGED_PROPERTY, l, null);
368+
}
369+
370+
/**
371+
* Sets the action command which will be provided in
372+
* <code>ActionEvent</code>s fired by this <code>CalendarSelect</code>.
373+
*
374+
* @param newValue the new action command
375+
*/
376+
public void setActionCommand(String newValue) {
377+
set(PROPERTY_ACTION_COMMAND, newValue);
289378
}
290379

291380
/**

src/server-java/testapp-interactive/lib/nextapp/echo/extras/testapp/testscreen/CalendarSelectTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,25 @@ public void actionPerformed(ActionEvent e) {
198198
}
199199
});
200200

201+
// Listener Tests
202+
203+
testControlsPane.addButton(TestControlPane.CATEGORY_LISTENERS, "Set ActionCommand", new ActionListener(){
204+
public void actionPerformed(ActionEvent e) {
205+
calendarSelect.setActionCommand("ActionCommand " + (int) (Math.random() * 1000));
206+
}
207+
});
208+
209+
testControlsPane.addButton(TestControlPane.CATEGORY_LISTENERS, "Add ActionListener", new ActionListener(){
210+
public void actionPerformed(ActionEvent e) {
211+
calendarSelect.addActionListener(new ActionListener() {
212+
213+
public void actionPerformed(ActionEvent e) {
214+
InteractiveApp.getApp().consoleWrite("Action event received: " + e.toString());
215+
}
216+
});
217+
}
218+
});
219+
201220
addStandardIntegrationTests();
202221

203222
}

src/server-java/webcontainer/nextapp/echo/extras/webcontainer/sync/component/CalendarSelectPeer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ private static void addLocaleService(Locale locale, String localeCode) {
8080
public CalendarSelectPeer() {
8181
super();
8282
addOutputProperty(CalendarSelect.DATE_CHANGED_PROPERTY);
83+
addEvent(new EventPeer(CalendarSelect.INPUT_ACTION, CalendarSelect.ACTION_LISTENERS_CHANGED_PROPERTY) {
84+
public boolean hasListeners(Context context, Component c) {
85+
return ((CalendarSelect) c).hasActionListeners();
86+
}
87+
});
8388
}
8489

8590
/**

0 commit comments

Comments
 (0)