PageRenderTime 56ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/pentaho-gwt-widgets/src/org/pentaho/gwt/widgets/client/controls/schededitor/RecurrenceEditor.java

https://github.com/wgorman/pentaho-commons-gwt-modules
Java | 1435 lines | 1078 code | 228 blank | 129 comment | 91 complexity | c1eb981c76f02e45fcd0c0c1c30ed082 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /*
  2. * This program is free software; you can redistribute it and/or modify it under the
  3. * terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
  4. * Foundation.
  5. *
  6. * You should have received a copy of the GNU Lesser General Public License along with this
  7. * program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
  8. * or from the Free Software Foundation, Inc.,
  9. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  12. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU Lesser General Public License for more details.
  14. *
  15. * Copyright 2008 Pentaho Corporation. All rights reserved.
  16. */
  17. package org.pentaho.gwt.widgets.client.controls.schededitor;
  18. import java.util.ArrayList;
  19. import java.util.Date;
  20. import java.util.EnumSet;
  21. import java.util.HashMap;
  22. import java.util.LinkedHashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. import org.pentaho.gwt.widgets.client.controls.DateRangeEditor;
  26. import org.pentaho.gwt.widgets.client.controls.ErrorLabel;
  27. import org.pentaho.gwt.widgets.client.controls.TimePicker;
  28. import org.pentaho.gwt.widgets.client.i18n.WidgetsLocalizedMessages;
  29. import org.pentaho.gwt.widgets.client.i18n.WidgetsLocalizedMessagesSingleton;
  30. import org.pentaho.gwt.widgets.client.ui.ICallback;
  31. import org.pentaho.gwt.widgets.client.ui.IChangeHandler;
  32. import org.pentaho.gwt.widgets.client.utils.CronParseException;
  33. import org.pentaho.gwt.widgets.client.utils.CronParser;
  34. import org.pentaho.gwt.widgets.client.utils.EnumException;
  35. import org.pentaho.gwt.widgets.client.utils.StringUtils;
  36. import org.pentaho.gwt.widgets.client.utils.TimeUtil;
  37. import org.pentaho.gwt.widgets.client.utils.CronParser.RecurrenceType;
  38. import org.pentaho.gwt.widgets.client.utils.TimeUtil.DayOfWeek;
  39. import org.pentaho.gwt.widgets.client.utils.TimeUtil.MonthOfYear;
  40. import org.pentaho.gwt.widgets.client.utils.TimeUtil.TimeOfDay;
  41. import org.pentaho.gwt.widgets.client.utils.TimeUtil.WeekOfMonth;
  42. import com.google.gwt.user.client.ui.CaptionPanel;
  43. import com.google.gwt.user.client.ui.ChangeListener;
  44. import com.google.gwt.user.client.ui.CheckBox;
  45. import com.google.gwt.user.client.ui.ClickListener;
  46. import com.google.gwt.user.client.ui.DeckPanel;
  47. import com.google.gwt.user.client.ui.FlexTable;
  48. import com.google.gwt.user.client.ui.HorizontalPanel;
  49. import com.google.gwt.user.client.ui.KeyboardListener;
  50. import com.google.gwt.user.client.ui.Label;
  51. import com.google.gwt.user.client.ui.ListBox;
  52. import com.google.gwt.user.client.ui.Panel;
  53. import com.google.gwt.user.client.ui.RadioButton;
  54. import com.google.gwt.user.client.ui.TextBox;
  55. import com.google.gwt.user.client.ui.VerticalPanel;
  56. import com.google.gwt.user.client.ui.Widget;
  57. /**
  58. * @author Steven Barkdull
  59. *
  60. */
  61. @SuppressWarnings("deprecation")
  62. public class RecurrenceEditor extends VerticalPanel implements IChangeHandler {
  63. private static final WidgetsLocalizedMessages MSGS = WidgetsLocalizedMessagesSingleton.getInstance().getMessages();
  64. private static final String SCHEDULE_EDITOR_CAPTION_PANEL = "schedule-editor-caption-panel"; //$NON-NLS-1$
  65. private static final String DOW_CHECKBOX = "day-of-week-checkbox"; //$NON-NLS-1$
  66. private TimePicker startTimePicker = null;
  67. private SecondlyRecurrenceEditor secondlyEditor = null;
  68. private MinutelyRecurrenceEditor minutelyEditor = null;
  69. private HourlyRecurrenceEditor hourlyEditor = null;
  70. private DailyRecurrenceEditor dailyEditor = null;
  71. private WeeklyRecurrenceEditor weeklyEditor = null;
  72. private MonthlyRecurrenceEditor monthlyEditor = null;
  73. private YearlyRecurrenceEditor yearlyEditor = null;
  74. private DateRangeEditor dateRangeEditor = null;
  75. private TemporalValue temporalState = null;
  76. private DeckPanel deckPanel = null;
  77. private static final String SPACE = " "; //$NON-NLS-1$
  78. private static int VALUE_OF_SUNDAY = 1;
  79. private ICallback<IChangeHandler> onChangeHandler;
  80. private Map<TemporalValue, Panel> temporalPanelMap = new LinkedHashMap<TemporalValue, Panel>();
  81. public enum TemporalValue {
  82. SECONDS(0, MSGS.seconds()),
  83. MINUTES(1, MSGS.minutes()),
  84. HOURS(2, MSGS.hours()),
  85. DAILY(3, MSGS.daily()),
  86. WEEKLY(4, MSGS.weekly()),
  87. MONTHLY(5, MSGS.monthly()),
  88. YEARLY(6, MSGS.yearly());
  89. private TemporalValue(int value, String name) {
  90. this.value = value;
  91. this.name = name;
  92. }
  93. private final int value;
  94. private final String name;
  95. private static TemporalValue[] temporalValues = {SECONDS,
  96. MINUTES,
  97. HOURS,
  98. DAILY,
  99. WEEKLY,
  100. MONTHLY,
  101. YEARLY};
  102. public int value() {
  103. return value;
  104. }
  105. public String toString() {
  106. return name;
  107. }
  108. public static TemporalValue get(int idx) {
  109. return temporalValues[idx];
  110. }
  111. public static int length() {
  112. return temporalValues.length;
  113. }
  114. public static TemporalValue stringToTemporalValue( String temporalValue ) throws EnumException {
  115. for (TemporalValue v : EnumSet.range(TemporalValue.SECONDS, TemporalValue.YEARLY)) {
  116. if ( v.toString().equals( temporalValue ) ) {
  117. return v;
  118. }
  119. }
  120. throw new EnumException( MSGS.invalidTemporalValue( temporalValue ) );
  121. }
  122. } /* end enum */
  123. private static final String DAILY_RB_GROUP = "daily-group"; //$NON-NLS-1$
  124. private static final String MONTHLY_RB_GROUP = "monthly-group"; //$NON-NLS-1$
  125. public RecurrenceEditor(final TimePicker startTimePicker) {
  126. super();
  127. this.setWidth("100%"); //$NON-NLS-1$
  128. Widget p = createRecurrencePanel();
  129. add(p);
  130. Date now = new Date();
  131. dateRangeEditor = new DateRangeEditor( now );
  132. add( dateRangeEditor );
  133. this.startTimePicker = startTimePicker;
  134. configureOnChangeHandler();
  135. }
  136. public void reset( Date d ) {
  137. startTimePicker.setHour( "12" ); //$NON-NLS-1$
  138. startTimePicker.setMinute( "00" ); //$NON-NLS-1$
  139. startTimePicker.setTimeOfDay( TimeUtil.TimeOfDay.AM );
  140. dateRangeEditor.reset( d );
  141. secondlyEditor.reset();
  142. minutelyEditor.reset();
  143. hourlyEditor.reset();
  144. dailyEditor.reset();
  145. weeklyEditor.reset();
  146. monthlyEditor.reset();
  147. yearlyEditor.reset();
  148. }
  149. /**
  150. *
  151. * @param recurrenceStr
  152. * @throws EnumException thrown if recurrenceTokens[0] is not a valid ScheduleType String.
  153. */
  154. public void inititalizeWithRecurrenceString( String recurrenceStr ) throws EnumException {
  155. String[] recurrenceTokens = recurrenceStr.split( "\\s" ); //$NON-NLS-1$
  156. setStartTime( recurrenceTokens[1], recurrenceTokens[2], recurrenceTokens[3] );
  157. RecurrenceType rt = RecurrenceType.stringToScheduleType( recurrenceTokens[0] );
  158. switch( rt ) {
  159. case EveryWeekday:
  160. setEveryWeekdayRecurrence( recurrenceTokens );
  161. break;
  162. case WeeklyOn:
  163. setWeeklyOnRecurrence( recurrenceTokens );
  164. break;
  165. case DayNOfMonth:
  166. setDayNOfMonthRecurrence( recurrenceTokens );
  167. break;
  168. case NthDayNameOfMonth:
  169. setNthDayNameOfMonthRecurrence( recurrenceTokens );
  170. break;
  171. case LastDayNameOfMonth:
  172. setLastDayNameOfMonthRecurrence( recurrenceTokens );
  173. break;
  174. case EveryMonthNameN:
  175. setEveryMonthNameNRecurrence( recurrenceTokens );
  176. break;
  177. case NthDayNameOfMonthName:
  178. setNthDayNameOfMonthNameRecurrence( recurrenceTokens );
  179. break;
  180. case LastDayNameOfMonthName:
  181. setLastDayNameOfMonthNameRecurrence( recurrenceTokens );
  182. break;
  183. default:
  184. }
  185. }
  186. private void setStartTime( String seconds, String minutes, String hours ) {
  187. TimeOfDay td = TimeUtil.getTimeOfDayBy0To23Hour( hours );
  188. int intHours = Integer.parseInt( hours );
  189. int intTwelveHour = TimeUtil.to12HourClock( intHours ); // returns 0..11
  190. startTimePicker.setHour( Integer.toString( TimeUtil.map0Through11To12Through11( intTwelveHour ) ) );
  191. startTimePicker.setMinute( minutes );
  192. startTimePicker.setTimeOfDay( td );
  193. }
  194. private void setEveryWeekdayRecurrence( String[] recurrenceTokens ) {
  195. setTemporalState( TemporalValue.DAILY );
  196. dailyEditor.setEveryWeekday();
  197. }
  198. private void setWeeklyOnRecurrence( String[] recurrenceTokens ) {
  199. setTemporalState( TemporalValue.WEEKLY );
  200. String days = recurrenceTokens[4];
  201. weeklyEditor.setCheckedDaysAsString( days, VALUE_OF_SUNDAY );
  202. }
  203. private void setDayNOfMonthRecurrence( String[] recurrenceTokens ) {
  204. setTemporalState( TemporalValue.MONTHLY );
  205. monthlyEditor.setDayNOfMonth();
  206. String dayNOfMonth = recurrenceTokens[4];
  207. monthlyEditor.setDayOfMonth( dayNOfMonth );
  208. }
  209. private void setNthDayNameOfMonthRecurrence( String[] recurrenceTokens ) {
  210. setTemporalState( TemporalValue.MONTHLY );
  211. monthlyEditor.setNthDayNameOfMonth();
  212. monthlyEditor.setWeekOfMonth( WeekOfMonth.get( Integer.parseInt( recurrenceTokens[5])-1 ) );
  213. monthlyEditor.setDayOfWeek( DayOfWeek.get( Integer.parseInt( recurrenceTokens[4])-1 ) );
  214. }
  215. private void setLastDayNameOfMonthRecurrence( String[] recurrenceTokens ) {
  216. setTemporalState( TemporalValue.MONTHLY );
  217. monthlyEditor.setNthDayNameOfMonth();
  218. monthlyEditor.setWeekOfMonth( WeekOfMonth.LAST );
  219. monthlyEditor.setDayOfWeek( DayOfWeek.get( Integer.parseInt( recurrenceTokens[4])-1 ) );
  220. }
  221. private void setEveryMonthNameNRecurrence( String[] recurrenceTokens ) {
  222. setTemporalState( TemporalValue.YEARLY );
  223. yearlyEditor.setEveryMonthOnNthDay();
  224. yearlyEditor.setDayOfMonth( recurrenceTokens[4] );
  225. yearlyEditor.setMonthOfYear0( MonthOfYear.get( Integer.parseInt( recurrenceTokens[5] )-1 ) );
  226. }
  227. private void setNthDayNameOfMonthNameRecurrence( String[] recurrenceTokens ) {
  228. setTemporalState( TemporalValue.YEARLY );
  229. yearlyEditor.setNthDayNameOfMonthName();
  230. yearlyEditor.setMonthOfYear1( MonthOfYear.get( Integer.parseInt( recurrenceTokens[6] )-1 ) );
  231. yearlyEditor.setWeekOfMonth( WeekOfMonth.get( Integer.parseInt( recurrenceTokens[5])-1 ) );
  232. yearlyEditor.setDayOfWeek( DayOfWeek.get( Integer.parseInt( recurrenceTokens[4])-1 ) );
  233. }
  234. private void setLastDayNameOfMonthNameRecurrence( String[] recurrenceTokens ) {
  235. setTemporalState( TemporalValue.YEARLY );
  236. yearlyEditor.setNthDayNameOfMonthName();
  237. yearlyEditor.setMonthOfYear1( MonthOfYear.get( Integer.parseInt( recurrenceTokens[5] )-1 ) );
  238. yearlyEditor.setWeekOfMonth( WeekOfMonth.LAST );
  239. yearlyEditor.setDayOfWeek( DayOfWeek.get( Integer.parseInt( recurrenceTokens[4])-1 ) );
  240. }
  241. /**
  242. *
  243. * @param repeatInSecs
  244. */
  245. public void inititalizeWithRepeatInSecs( int repeatInSecs ) {
  246. TemporalValue currentVal;
  247. long repeatTime;
  248. if ( TimeUtil.isSecondsWholeDay( repeatInSecs ) ) {
  249. repeatTime = TimeUtil.secsToDays( repeatInSecs );
  250. currentVal = TemporalValue.DAILY;
  251. dailyEditor.setRepeatValue( Long.toString( repeatTime ) );
  252. } else {
  253. SimpleRecurrencePanel p = null;
  254. if ( TimeUtil.isSecondsWholeHour( repeatInSecs ) ) {
  255. repeatTime = TimeUtil.secsToHours( repeatInSecs );
  256. currentVal = TemporalValue.HOURS;
  257. } else if ( TimeUtil.isSecondsWholeMinute( repeatInSecs ) ) {
  258. repeatTime = TimeUtil.secsToMinutes( repeatInSecs );
  259. currentVal = TemporalValue.MINUTES;
  260. } else {
  261. // the repeat time is seconds
  262. repeatTime = repeatInSecs;
  263. currentVal = TemporalValue.SECONDS;
  264. }
  265. p = (SimpleRecurrencePanel)temporalPanelMap.get(currentVal);
  266. p.setValue( Long.toString( repeatTime ) );
  267. }
  268. setTemporalState( currentVal );
  269. }
  270. private Widget createRecurrencePanel() {
  271. CaptionPanel recurrenceGB = new CaptionPanel(MSGS.recurrencePattern() );
  272. recurrenceGB.setStyleName(SCHEDULE_EDITOR_CAPTION_PANEL);
  273. deckPanel = new DeckPanel();
  274. recurrenceGB.add(deckPanel);
  275. secondlyEditor = new SecondlyRecurrenceEditor();
  276. minutelyEditor = new MinutelyRecurrenceEditor();
  277. hourlyEditor = new HourlyRecurrenceEditor();
  278. dailyEditor = new DailyRecurrenceEditor();
  279. weeklyEditor = new WeeklyRecurrenceEditor();
  280. monthlyEditor = new MonthlyRecurrenceEditor();
  281. yearlyEditor = new YearlyRecurrenceEditor();
  282. createTemporalMap();
  283. deckPanel.add(secondlyEditor);
  284. deckPanel.add(minutelyEditor);
  285. deckPanel.add(hourlyEditor);
  286. deckPanel.add(dailyEditor);
  287. deckPanel.add(weeklyEditor);
  288. deckPanel.add(monthlyEditor);
  289. deckPanel.add(yearlyEditor);
  290. deckPanel.showWidget(0);
  291. return recurrenceGB;
  292. }
  293. private void createTemporalMap() {
  294. // must come after creation of temporal panels
  295. assert dailyEditor != null : "Temporal panels must be initialized before calling createTemporalCombo."; //$NON-NLS-1$
  296. temporalPanelMap.put( TemporalValue.SECONDS, secondlyEditor );
  297. temporalPanelMap.put( TemporalValue.MINUTES, minutelyEditor );
  298. temporalPanelMap.put( TemporalValue.HOURS, hourlyEditor );
  299. temporalPanelMap.put( TemporalValue.DAILY, dailyEditor );
  300. temporalPanelMap.put( TemporalValue.WEEKLY, weeklyEditor);
  301. temporalPanelMap.put( TemporalValue.MONTHLY, monthlyEditor);
  302. temporalPanelMap.put( TemporalValue.YEARLY, yearlyEditor);
  303. }
  304. private class SimpleRecurrencePanel extends VerticalPanel implements IChangeHandler {
  305. private TextBox valueTb = new TextBox();
  306. private ErrorLabel valueLabel = null;
  307. private ICallback<IChangeHandler> onChangeHandler;
  308. public SimpleRecurrencePanel( String strLabel ) {
  309. HorizontalPanel hp = new HorizontalPanel();
  310. Label l = new Label( MSGS.every() );
  311. l.setStyleName("startLabel"); //$NON-NLS-1$
  312. hp.add(l);
  313. valueTb.setWidth( "3em" ); //$NON-NLS-1$
  314. valueTb.setTitle( MSGS.numberOfXToRepeat( strLabel ) );
  315. hp.add(valueTb);
  316. l = new Label( strLabel );
  317. l.setStyleName( "endLabel" ); //$NON-NLS-1$
  318. hp.add(l);
  319. valueLabel = new ErrorLabel( hp );
  320. add( valueLabel );
  321. configureOnChangeHandler();
  322. }
  323. public String getValue() {
  324. return valueTb.getText();
  325. }
  326. public void setValue( String val ) {
  327. valueTb.setText( val );
  328. }
  329. public void reset() {
  330. setValue( "" ); //$NON-NLS-1$
  331. }
  332. public void setValueError( String errorMsg ) {
  333. valueLabel.setErrorMsg( errorMsg );
  334. }
  335. public void setOnChangeHandler( ICallback<IChangeHandler> handler ) {
  336. this.onChangeHandler = handler;
  337. }
  338. private void changeHandler() {
  339. if ( null != onChangeHandler ) {
  340. onChangeHandler.onHandle( this );
  341. }
  342. }
  343. private void configureOnChangeHandler() {
  344. final SimpleRecurrencePanel localThis = this;
  345. KeyboardListener keyboardListener = new KeyboardListener() {
  346. public void onKeyDown(Widget sender, char keyCode, int modifiers) {
  347. }
  348. public void onKeyPress(Widget sender, char keyCode, int modifiers) {
  349. }
  350. public void onKeyUp(Widget sender, char keyCode, int modifiers) {
  351. localThis.changeHandler();
  352. }
  353. };
  354. valueTb.addKeyboardListener( keyboardListener );
  355. }
  356. }
  357. public class SecondlyRecurrenceEditor extends SimpleRecurrencePanel {
  358. public SecondlyRecurrenceEditor() {
  359. super( MSGS.seconds() );
  360. }
  361. }
  362. public class MinutelyRecurrenceEditor extends SimpleRecurrencePanel {
  363. public MinutelyRecurrenceEditor() {
  364. super( MSGS.minutesLabel() );
  365. }
  366. }
  367. public class HourlyRecurrenceEditor extends SimpleRecurrencePanel {
  368. public HourlyRecurrenceEditor() {
  369. super( MSGS.hoursLabel() );
  370. }
  371. }
  372. public class DailyRecurrenceEditor extends VerticalPanel implements IChangeHandler {
  373. private TextBox repeatValueTb = new TextBox();
  374. private RadioButton everyNDaysRb = new RadioButton(DAILY_RB_GROUP, MSGS.every() );
  375. private RadioButton everyWeekdayRb = new RadioButton(DAILY_RB_GROUP, MSGS.everyWeekDay() );
  376. private ErrorLabel repeatLabel = null;
  377. private ICallback<IChangeHandler> onChangeHandler;
  378. public DailyRecurrenceEditor() {
  379. HorizontalPanel hp = new HorizontalPanel();
  380. everyNDaysRb.setStyleName("recurrenceRadioButton"); //$NON-NLS-1$
  381. everyNDaysRb.setChecked(true);
  382. hp.add(everyNDaysRb);
  383. repeatValueTb.setWidth("3em"); //$NON-NLS-1$
  384. repeatValueTb.setTitle( MSGS.numDaysToRepeat() );
  385. hp.add(repeatValueTb);
  386. Label l = new Label( MSGS.daysLabel() );
  387. l.setStyleName("endLabel"); //$NON-NLS-1$
  388. hp.add(l);
  389. repeatLabel = new ErrorLabel( hp );
  390. add( repeatLabel );
  391. everyWeekdayRb.setStyleName("recurrenceRadioButton"); //$NON-NLS-1$
  392. add(everyWeekdayRb);
  393. configureOnChangeHandler();
  394. }
  395. public void reset() {
  396. setRepeatValue( "" ); //$NON-NLS-1$
  397. setEveryNDays();
  398. }
  399. public String getRepeatValue() {
  400. return repeatValueTb.getText();
  401. }
  402. public void setRepeatValue( String repeatValue ) {
  403. repeatValueTb.setText( repeatValue );
  404. }
  405. public void setEveryNDays() {
  406. everyNDaysRb.setChecked( true );
  407. everyWeekdayRb.setChecked( false );
  408. }
  409. public boolean isEveryNDays() {
  410. return everyNDaysRb.isChecked();
  411. }
  412. public void setEveryWeekday() {
  413. everyWeekdayRb.setChecked( true );
  414. everyNDaysRb.setChecked( false );
  415. }
  416. public boolean isEveryWeekday() {
  417. return everyWeekdayRb.isChecked();
  418. }
  419. public void setRepeatError( String errorMsg ) {
  420. repeatLabel.setErrorMsg( errorMsg );
  421. }
  422. public void setOnChangeHandler( ICallback<IChangeHandler> handler ) {
  423. this.onChangeHandler = handler;
  424. }
  425. private void changeHandler() {
  426. if ( null != onChangeHandler ) {
  427. onChangeHandler.onHandle( this );
  428. }
  429. }
  430. private void configureOnChangeHandler() {
  431. final DailyRecurrenceEditor localThis = this;
  432. KeyboardListener keyboardListener = new KeyboardListener() {
  433. public void onKeyDown(Widget sender, char keyCode, int modifiers) {
  434. }
  435. public void onKeyPress(Widget sender, char keyCode, int modifiers) {
  436. }
  437. public void onKeyUp(Widget sender, char keyCode, int modifiers) {
  438. localThis.changeHandler();
  439. }
  440. };
  441. ClickListener clickListener = new ClickListener() {
  442. public void onClick(Widget sender) {
  443. localThis.changeHandler();
  444. }
  445. };
  446. repeatValueTb.addKeyboardListener( keyboardListener );
  447. everyNDaysRb.addClickListener( clickListener );
  448. everyNDaysRb.addKeyboardListener( keyboardListener );
  449. everyWeekdayRb.addClickListener( clickListener );
  450. everyWeekdayRb.addKeyboardListener( keyboardListener );
  451. }
  452. }
  453. public class WeeklyRecurrenceEditor extends VerticalPanel implements IChangeHandler {
  454. private Map<DayOfWeek, CheckBox> dayToCheckBox = new HashMap<DayOfWeek, CheckBox>();
  455. private ErrorLabel everyWeekOnLabel = null;
  456. private ICallback<IChangeHandler> onChangeHandler;
  457. public WeeklyRecurrenceEditor() {
  458. setStyleName("weeklyRecurrencePanel"); //$NON-NLS-1$
  459. Label l = new Label( MSGS.recurEveryWeek() );
  460. everyWeekOnLabel = new ErrorLabel( l );
  461. l.setStyleName("startLabel"); //$NON-NLS-1$
  462. add( everyWeekOnLabel );
  463. FlexTable gp = new FlexTable();
  464. gp.setCellPadding(0);
  465. gp.setCellSpacing(0);
  466. // add Sun - Wed
  467. final int ITEMS_IN_ROW = 4;
  468. for (int ii = 0; ii < ITEMS_IN_ROW; ++ii) {
  469. DayOfWeek day = DayOfWeek.get(ii);
  470. CheckBox cb = new CheckBox(day.toString());
  471. cb.setStylePrimaryName(DOW_CHECKBOX);
  472. gp.setWidget(0, ii, cb);
  473. dayToCheckBox.put(day, cb);
  474. }
  475. // Add Thur - Sat
  476. for (int ii = ITEMS_IN_ROW; ii < DayOfWeek.length(); ++ii) {
  477. DayOfWeek day = DayOfWeek.get(ii);
  478. CheckBox cb = new CheckBox(day.toString());
  479. cb.setStylePrimaryName(DOW_CHECKBOX);
  480. gp.setWidget(1, ii - 4, cb);
  481. dayToCheckBox.put(day, cb);
  482. }
  483. add(gp);
  484. configureOnChangeHandler();
  485. }
  486. public void reset() {
  487. for ( DayOfWeek d : dayToCheckBox.keySet() ) {
  488. CheckBox cb = dayToCheckBox.get( d );
  489. cb.setChecked( false );
  490. }
  491. }
  492. public List<DayOfWeek> getCheckedDays() {
  493. ArrayList<DayOfWeek> checkedDays = new ArrayList<DayOfWeek>();
  494. for ( DayOfWeek d : EnumSet.range( DayOfWeek.SUN, DayOfWeek.SAT) ) {
  495. CheckBox cb = dayToCheckBox.get( d );
  496. if ( cb.isChecked() ) {
  497. checkedDays.add(d);
  498. }
  499. }
  500. return checkedDays;
  501. }
  502. /**
  503. *
  504. * @param valueOfSunday int used to adjust the starting point of the weekday sequence.
  505. * If this value is 0, Sun-Sat maps to 0-6, if this value is 1, Sun-Sat maps to 1-7, etc.
  506. * @return String comma separated list of numeric days of the week.
  507. */
  508. public String getCheckedDaysAsString( int valueOfSunday ) {
  509. StringBuilder sb = new StringBuilder();
  510. for ( DayOfWeek d : getCheckedDays()) {
  511. sb.append( Integer.toString( d.value()+valueOfSunday ) ).append( "," ); //$NON-NLS-1$
  512. }
  513. sb.deleteCharAt( sb.length()-1 );
  514. return sb.toString();
  515. }
  516. /**
  517. *
  518. * @param valueOfSunday int used to adjust the starting point of the weekday sequence.
  519. * If this value is 0, Sun-Sat maps to 0-6, if this value is 1, Sun-Sat maps to 1-7, etc.
  520. * @return String comma separated list of numeric days of the week.
  521. */
  522. public void setCheckedDaysAsString( String strDays, int valueOfSunday ) {
  523. String[] days = strDays.split( "," ); //$NON-NLS-1$
  524. for ( String day : days ) {
  525. int intDay = Integer.parseInt( day ) - valueOfSunday;
  526. DayOfWeek dayOfWeek = DayOfWeek.get( intDay );
  527. CheckBox cb = dayToCheckBox.get( dayOfWeek );
  528. cb.setChecked( true );
  529. }
  530. }
  531. public int getNumCheckedDays() {
  532. int numCheckedDays = 0;
  533. //for ( DayOfWeek d : EnumSet.range( DayOfWeek.SUN, DayOfWeek.SAT) ) {
  534. for ( Map.Entry<DayOfWeek, CheckBox> cbEntry : dayToCheckBox.entrySet() ) {
  535. if ( cbEntry.getValue().isChecked() ) {
  536. numCheckedDays++;
  537. }
  538. }
  539. return numCheckedDays;
  540. }
  541. public void setEveryDayOnError( String errorMsg ) {
  542. everyWeekOnLabel.setErrorMsg( errorMsg );
  543. }
  544. public void setOnChangeHandler( ICallback<IChangeHandler> handler ) {
  545. this.onChangeHandler = handler;
  546. }
  547. private void changeHandler() {
  548. if ( null != onChangeHandler ) {
  549. onChangeHandler.onHandle( this );
  550. }
  551. }
  552. private void configureOnChangeHandler() {
  553. final WeeklyRecurrenceEditor localThis = this;
  554. KeyboardListener keyboardListener = new KeyboardListener() {
  555. public void onKeyDown(Widget sender, char keyCode, int modifiers) {
  556. }
  557. public void onKeyPress(Widget sender, char keyCode, int modifiers) {
  558. }
  559. public void onKeyUp(Widget sender, char keyCode, int modifiers) {
  560. localThis.changeHandler();
  561. }
  562. };
  563. ClickListener clickListener = new ClickListener() {
  564. public void onClick(Widget sender) {
  565. localThis.changeHandler();
  566. }
  567. };
  568. for ( DayOfWeek d : dayToCheckBox.keySet() ) {
  569. CheckBox cb = dayToCheckBox.get( d );
  570. cb.addClickListener( clickListener );
  571. cb.addKeyboardListener( keyboardListener );
  572. }
  573. }
  574. }
  575. public class MonthlyRecurrenceEditor extends VerticalPanel implements IChangeHandler {
  576. private RadioButton dayNOfMonthRb = new RadioButton(MONTHLY_RB_GROUP, MSGS.day());
  577. private RadioButton nthDayNameOfMonthRb = new RadioButton(MONTHLY_RB_GROUP, MSGS.the() );
  578. private TextBox dayOfMonthTb = new TextBox();
  579. private ListBox whichWeekLb = createWhichWeekListBox();
  580. private ListBox dayOfWeekLb = createDayOfWeekListBox();
  581. private ErrorLabel dayNOfMonthLabel = null;
  582. private ICallback<IChangeHandler> onChangeHandler;
  583. public MonthlyRecurrenceEditor() {
  584. setSpacing(6);
  585. HorizontalPanel hp = new HorizontalPanel();
  586. dayNOfMonthRb.setStyleName("recurrenceRadioButton"); //$NON-NLS-1$
  587. dayNOfMonthRb.setChecked( true );
  588. hp.add(dayNOfMonthRb);
  589. dayOfMonthTb.setWidth("3em"); //$NON-NLS-1$
  590. hp.add(dayOfMonthTb);
  591. Label l = new Label( MSGS.ofEveryMonth() );
  592. l.setStyleName("endLabel"); //$NON-NLS-1$
  593. hp.add(l);
  594. dayNOfMonthLabel = new ErrorLabel( hp );
  595. add( dayNOfMonthLabel );
  596. hp = new HorizontalPanel();
  597. nthDayNameOfMonthRb.setStyleName("recurrenceRadioButton"); //$NON-NLS-1$
  598. hp.add(nthDayNameOfMonthRb);
  599. hp.add(whichWeekLb);
  600. hp.add(dayOfWeekLb);
  601. l = new Label( MSGS.ofEveryMonth() );
  602. l.setStyleName("endLabel"); //$NON-NLS-1$
  603. hp.add(l);
  604. add(hp);
  605. configureOnChangeHandler();
  606. }
  607. public void reset() {
  608. setDayNOfMonth();
  609. setDayOfMonth( "" ); //$NON-NLS-1$
  610. setWeekOfMonth( WeekOfMonth.FIRST );
  611. setDayOfWeek( DayOfWeek.SUN );
  612. }
  613. public void setDayNOfMonth() {
  614. dayNOfMonthRb.setChecked( true );
  615. nthDayNameOfMonthRb.setChecked( false );
  616. }
  617. public boolean isDayNOfMonth() {
  618. return dayNOfMonthRb.isChecked();
  619. }
  620. public void setNthDayNameOfMonth() {
  621. nthDayNameOfMonthRb.setChecked( true );
  622. dayNOfMonthRb.setChecked( false );
  623. }
  624. public boolean isNthDayNameOfMonth() {
  625. return nthDayNameOfMonthRb.isChecked();
  626. }
  627. public String getDayOfMonth() {
  628. return dayOfMonthTb.getText();
  629. }
  630. public void setDayOfMonth( String dayOfMonth ) {
  631. dayOfMonthTb.setText( dayOfMonth );
  632. }
  633. public WeekOfMonth getWeekOfMonth() {
  634. return WeekOfMonth.get( whichWeekLb.getSelectedIndex() );
  635. }
  636. public void setWeekOfMonth( WeekOfMonth week ) {
  637. whichWeekLb.setSelectedIndex( week.value() );
  638. }
  639. public DayOfWeek getDayOfWeek() {
  640. return DayOfWeek.get( dayOfWeekLb.getSelectedIndex() );
  641. }
  642. public void setDayOfWeek( DayOfWeek day ) {
  643. dayOfWeekLb.setSelectedIndex( day.value() );
  644. }
  645. public void setDayNOfMonthError( String errorMsg ) {
  646. dayNOfMonthLabel.setErrorMsg( errorMsg );
  647. }
  648. public void setOnChangeHandler( ICallback<IChangeHandler> handler ) {
  649. this.onChangeHandler = handler;
  650. }
  651. private void changeHandler() {
  652. if ( null != onChangeHandler ) {
  653. onChangeHandler.onHandle( this );
  654. }
  655. }
  656. private void configureOnChangeHandler() {
  657. final MonthlyRecurrenceEditor localThis = this;
  658. KeyboardListener keyboardListener = new KeyboardListener() {
  659. public void onKeyDown(Widget sender, char keyCode, int modifiers) {
  660. }
  661. public void onKeyPress(Widget sender, char keyCode, int modifiers) {
  662. }
  663. public void onKeyUp(Widget sender, char keyCode, int modifiers) {
  664. localThis.changeHandler();
  665. }
  666. };
  667. ClickListener clickListener = new ClickListener() {
  668. public void onClick(Widget sender) {
  669. localThis.changeHandler();
  670. }
  671. };
  672. ChangeListener changeListener = new ChangeListener() {
  673. public void onChange(Widget sender) {
  674. localThis.changeHandler();
  675. }
  676. };
  677. dayNOfMonthRb.addClickListener( clickListener );
  678. dayNOfMonthRb.addKeyboardListener( keyboardListener );
  679. nthDayNameOfMonthRb.addClickListener( clickListener );
  680. nthDayNameOfMonthRb.addKeyboardListener( keyboardListener );
  681. dayOfMonthTb.addKeyboardListener( keyboardListener );
  682. whichWeekLb.addChangeListener( changeListener );
  683. dayOfWeekLb.addChangeListener( changeListener );
  684. }
  685. }
  686. public class YearlyRecurrenceEditor extends VerticalPanel implements IChangeHandler {
  687. private RadioButton everyMonthOnNthDayRb = new RadioButton(YEARLY_RB_GROUP, MSGS.every() );
  688. private RadioButton nthDayNameOfMonthNameRb = new RadioButton(YEARLY_RB_GROUP, MSGS.the() );
  689. private TextBox dayOfMonthTb = new TextBox();
  690. private ListBox monthOfYearLb0 = createMonthOfYearListBox();
  691. private ListBox monthOfYearLb1 = createMonthOfYearListBox();
  692. private ListBox whichWeekLb = createWhichWeekListBox();
  693. private ListBox dayOfWeekLb = createDayOfWeekListBox();
  694. private ErrorLabel dayOfMonthLabel = null;
  695. private ICallback<IChangeHandler> onChangeHandler;
  696. private static final String YEARLY_RB_GROUP = "yearly-group"; //$NON-NLS-1$
  697. public YearlyRecurrenceEditor() {
  698. setSpacing(6);
  699. HorizontalPanel p = new HorizontalPanel();
  700. everyMonthOnNthDayRb.setStyleName("recurrenceRadioButton"); //$NON-NLS-1$
  701. everyMonthOnNthDayRb.setChecked(true);
  702. p.add(everyMonthOnNthDayRb);
  703. p.add( monthOfYearLb0 );
  704. dayOfMonthTb.setStylePrimaryName("DAY_OF_MONTH_TB"); //$NON-NLS-1$
  705. dayOfMonthTb.setWidth("3em"); //$NON-NLS-1$
  706. p.add(dayOfMonthTb);
  707. dayOfMonthLabel = new ErrorLabel( p );
  708. add(dayOfMonthLabel);
  709. p = new HorizontalPanel();
  710. nthDayNameOfMonthNameRb.setStyleName("recurrenceRadioButton"); //$NON-NLS-1$
  711. p.add(nthDayNameOfMonthNameRb);
  712. p.add(whichWeekLb);
  713. p.add(dayOfWeekLb);
  714. Label l = new Label( MSGS.of() );
  715. l.setStyleName("middleLabel"); //$NON-NLS-1$
  716. p.add(l);
  717. p.add( monthOfYearLb1 );
  718. add(p);
  719. configureOnChangeHandler();
  720. }
  721. public void reset() {
  722. setEveryMonthOnNthDay();
  723. setMonthOfYear0( MonthOfYear.JAN );
  724. setDayOfMonth( "" ); //$NON-NLS-1$
  725. setWeekOfMonth( WeekOfMonth.FIRST );
  726. setDayOfWeek( DayOfWeek.SUN );
  727. setMonthOfYear1( MonthOfYear.JAN );
  728. }
  729. public boolean isEveryMonthOnNthDay() {
  730. return everyMonthOnNthDayRb.isChecked();
  731. }
  732. public void setEveryMonthOnNthDay() {
  733. everyMonthOnNthDayRb.setChecked( true );
  734. nthDayNameOfMonthNameRb.setChecked( false );
  735. }
  736. public boolean isNthDayNameOfMonthName() {
  737. return nthDayNameOfMonthNameRb.isChecked();
  738. }
  739. public void setNthDayNameOfMonthName() {
  740. nthDayNameOfMonthNameRb.setChecked( true );
  741. everyMonthOnNthDayRb.setChecked( false );
  742. }
  743. public String getDayOfMonth() {
  744. return dayOfMonthTb.getText();
  745. }
  746. public void setDayOfMonth( String dayOfMonth ) {
  747. dayOfMonthTb.setText( dayOfMonth );
  748. }
  749. public WeekOfMonth getWeekOfMonth() {
  750. return WeekOfMonth.get( whichWeekLb.getSelectedIndex() );
  751. }
  752. public void setWeekOfMonth( WeekOfMonth week ) {
  753. whichWeekLb.setSelectedIndex( week.value() );
  754. }
  755. public DayOfWeek getDayOfWeek() {
  756. return DayOfWeek.get( dayOfWeekLb.getSelectedIndex() );
  757. }
  758. public void setDayOfWeek( DayOfWeek day ) {
  759. dayOfWeekLb.setSelectedIndex( day.value() );
  760. }
  761. public MonthOfYear getMonthOfYear0() {
  762. return MonthOfYear.get( monthOfYearLb0.getSelectedIndex() );
  763. }
  764. public void setMonthOfYear0( MonthOfYear month ) {
  765. monthOfYearLb0.setSelectedIndex( month.value() );
  766. }
  767. public MonthOfYear getMonthOfYear1() {
  768. return MonthOfYear.get( monthOfYearLb1.getSelectedIndex() );
  769. }
  770. public void setMonthOfYear1( MonthOfYear month ) {
  771. monthOfYearLb1.setSelectedIndex( month.value() );
  772. }
  773. public void setDayOfMonthError( String errorMsg ) {
  774. dayOfMonthLabel.setErrorMsg( errorMsg );
  775. }
  776. public void setOnChangeHandler( ICallback<IChangeHandler> handler ) {
  777. this.onChangeHandler = handler;
  778. }
  779. private void changeHandler() {
  780. if ( null != onChangeHandler ) {
  781. onChangeHandler.onHandle( this );
  782. }
  783. }
  784. private void configureOnChangeHandler() {
  785. final YearlyRecurrenceEditor localThis = this;
  786. KeyboardListener keyboardListener = new KeyboardListener() {
  787. public void onKeyDown(Widget sender, char keyCode, int modifiers) {
  788. }
  789. public void onKeyPress(Widget sender, char keyCode, int modifiers) {
  790. }
  791. public void onKeyUp(Widget sender, char keyCode, int modifiers) {
  792. localThis.changeHandler();
  793. }
  794. };
  795. ClickListener clickListener = new ClickListener() {
  796. public void onClick(Widget sender) {
  797. localThis.changeHandler();
  798. }
  799. };
  800. ChangeListener changeListener = new ChangeListener() {
  801. public void onChange(Widget sender) {
  802. localThis.changeHandler();
  803. }
  804. };
  805. everyMonthOnNthDayRb.addClickListener( clickListener );
  806. everyMonthOnNthDayRb.addKeyboardListener( keyboardListener );
  807. nthDayNameOfMonthNameRb.addClickListener( clickListener );
  808. nthDayNameOfMonthNameRb.addKeyboardListener( keyboardListener );
  809. dayOfMonthTb.addKeyboardListener( keyboardListener );
  810. monthOfYearLb0.addChangeListener( changeListener );
  811. monthOfYearLb1.addChangeListener( changeListener );
  812. whichWeekLb.addChangeListener( changeListener );
  813. dayOfWeekLb.addChangeListener( changeListener );
  814. }
  815. }
  816. private ListBox createDayOfWeekListBox() {
  817. ListBox l = new ListBox();
  818. for (int ii = 0; ii < DayOfWeek.length(); ++ii) {
  819. DayOfWeek day = DayOfWeek.get(ii);
  820. l.addItem(day.toString());
  821. }
  822. return l;
  823. }
  824. private ListBox createMonthOfYearListBox() {
  825. ListBox l = new ListBox();
  826. for (int ii = 0; ii < MonthOfYear.length(); ++ii) {
  827. MonthOfYear month = MonthOfYear.get(ii);
  828. l.addItem(month.toString());
  829. }
  830. return l;
  831. }
  832. private ListBox createWhichWeekListBox() {
  833. ListBox l = new ListBox();
  834. for ( WeekOfMonth week : EnumSet.range(WeekOfMonth.FIRST, WeekOfMonth.LAST)) {
  835. l.addItem( week.toString() );
  836. }
  837. return l;
  838. }
  839. private void selectTemporalPanel(TemporalValue selectedTemporalValue) {
  840. int i = 0;
  841. for ( Map.Entry<TemporalValue, Panel> me : temporalPanelMap.entrySet() ) {
  842. if (me.getKey().equals( selectedTemporalValue )) {
  843. deckPanel.showWidget(i);
  844. break;
  845. }
  846. i++;
  847. }
  848. }
  849. /**
  850. *
  851. * @return null if the selected schedule does not support repeat-in-seconds, otherwise
  852. * return the number of seconds between schedule execution.
  853. * @throws RuntimeException if the temporal value (tv) is invalid. This
  854. * condition occurs as a result of programmer error.
  855. */
  856. public Long getRepeatInSecs() throws RuntimeException {
  857. switch ( temporalState ) {
  858. case WEEKLY:
  859. // fall through
  860. case MONTHLY:
  861. // fall through
  862. case YEARLY:
  863. return null;
  864. case SECONDS:
  865. return Long.parseLong( secondlyEditor.getValue() );
  866. case MINUTES:
  867. return TimeUtil.minutesToSecs( Long.parseLong( minutelyEditor.getValue() ) );
  868. case HOURS:
  869. return TimeUtil.hoursToSecs( Long.parseLong( hourlyEditor.getValue() ) );
  870. case DAILY:
  871. return TimeUtil.daysToSecs( Long.parseLong( dailyEditor.getRepeatValue() ) );
  872. default:
  873. throw new RuntimeException( MSGS.invalidTemporalValueInGetRepeatInSecs( temporalState.toString() ) );
  874. }
  875. }
  876. /**
  877. *
  878. * @return null if the selected schedule does not support CRON, otherwise
  879. * return the CRON string.
  880. * @throws RuntimeException if the temporal value (tv) is invalid. This
  881. * condition occurs as a result of programmer error.
  882. */
  883. public String getCronString() throws RuntimeException {
  884. switch ( temporalState ) {
  885. case SECONDS:
  886. // fall through
  887. case MINUTES:
  888. // fall through
  889. case HOURS:
  890. return null;
  891. case DAILY:
  892. return getDailyCronString();
  893. case WEEKLY:
  894. return getWeeklyCronString();
  895. case MONTHLY:
  896. return getMonthlyCronString();
  897. case YEARLY:
  898. return getYearlyCronString();
  899. default:
  900. throw new RuntimeException( MSGS.invalidTemporalValueInGetCronString( temporalState.toString() ) );
  901. }
  902. }
  903. public boolean isEveryNDays() {
  904. return (temporalState == TemporalValue.DAILY) && dailyEditor.isEveryNDays();
  905. }
  906. public MonthOfYear getSelectedMonth() {
  907. MonthOfYear selectedMonth = null;
  908. if ((temporalState == TemporalValue.YEARLY) && yearlyEditor.isNthDayNameOfMonthName()) {
  909. selectedMonth = yearlyEditor.getMonthOfYear1();
  910. } else if ((temporalState == TemporalValue.YEARLY) && yearlyEditor.isEveryMonthOnNthDay()) {
  911. selectedMonth = yearlyEditor.getMonthOfYear0();
  912. }
  913. return selectedMonth;
  914. }
  915. public List<DayOfWeek> getSelectedDaysOfWeek() {
  916. ArrayList<DayOfWeek> selectedDaysOfWeek = new ArrayList<DayOfWeek>();
  917. if ((temporalState == TemporalValue.DAILY) && !dailyEditor.isEveryNDays()) {
  918. selectedDaysOfWeek.add(DayOfWeek.MON);
  919. selectedDaysOfWeek.add(DayOfWeek.TUE);
  920. selectedDaysOfWeek.add(DayOfWeek.WED);
  921. selectedDaysOfWeek.add(DayOfWeek.THU);
  922. selectedDaysOfWeek.add(DayOfWeek.FRI);
  923. } else if (temporalState == TemporalValue.WEEKLY) {
  924. selectedDaysOfWeek.addAll(weeklyEditor.getCheckedDays());
  925. } else if ((temporalState == TemporalValue.MONTHLY) && monthlyEditor.isNthDayNameOfMonth()) {
  926. selectedDaysOfWeek.add(monthlyEditor.getDayOfWeek());
  927. } else if ((temporalState == TemporalValue.YEARLY) && yearlyEditor.isNthDayNameOfMonthName()) {
  928. selectedDaysOfWeek.add(yearlyEditor.getDayOfWeek());
  929. }
  930. return selectedDaysOfWeek;
  931. }
  932. public WeekOfMonth getSelectedWeekOfMonth() {
  933. WeekOfMonth selectedWeekOfMonth = null;
  934. if ((temporalState == TemporalValue.MONTHLY) && monthlyEditor.isNthDayNameOfMonth()) {
  935. selectedWeekOfMonth = monthlyEditor.getWeekOfMonth();
  936. } else if ((temporalState == TemporalValue.YEARLY) && yearlyEditor.isNthDayNameOfMonthName()) {
  937. selectedWeekOfMonth = yearlyEditor.getWeekOfMonth();
  938. }
  939. return selectedWeekOfMonth;
  940. }
  941. public Integer getSelectedDayOfMonth() {
  942. Integer selectedDayOfMonth = null;
  943. if ((temporalState == TemporalValue.MONTHLY) && monthlyEditor.isDayNOfMonth()) {
  944. try {
  945. selectedDayOfMonth = Integer.parseInt(monthlyEditor.getDayOfMonth());
  946. } catch (Exception ex) {
  947. }
  948. } else if ((temporalState == TemporalValue.YEARLY) && yearlyEditor.isEveryMonthOnNthDay()) {
  949. try {
  950. selectedDayOfMonth = Integer.parseInt(yearlyEditor.getDayOfMonth());
  951. } catch (Exception ex) {
  952. }
  953. }
  954. return selectedDayOfMonth;
  955. }
  956. /**
  957. *
  958. * @return
  959. * @throws RuntimeException
  960. */
  961. private String getDailyCronString() throws RuntimeException {
  962. String cronStr;
  963. StringBuilder recurrenceSb = new StringBuilder();
  964. if ( dailyEditor.isEveryNDays() ) {
  965. return null;
  966. } else {
  967. // must be every weekday
  968. recurrenceSb.append( RecurrenceType.EveryWeekday ).append( SPACE )
  969. .append( getTimeOfRecurrence() );
  970. try {
  971. cronStr = CronParser.recurrenceStringToCronString( recurrenceSb.toString() );
  972. } catch (CronParseException e) {
  973. throw new RuntimeException( MSGS.invalidRecurrenceString( recurrenceSb.toString() ) );
  974. }
  975. return cronStr;
  976. }
  977. }
  978. private String getWeeklyCronString() throws RuntimeException {
  979. String cronStr;
  980. StringBuilder recurrenceSb = new StringBuilder();
  981. // WeeklyOn 0 33 6 1,3,5
  982. recurrenceSb.append( RecurrenceType.WeeklyOn ).append( SPACE )
  983. .append( getTimeOfRecurrence() ).append( SPACE )
  984. .append( weeklyEditor.getCheckedDaysAsString( VALUE_OF_SUNDAY ) );
  985. try {
  986. cronStr = CronParser.recurrenceStringToCronString( recurrenceSb.toString() );
  987. } catch (CronParseException e) {
  988. throw new RuntimeException( MSGS.invalidRecurrenceString( recurrenceSb.toString() ) );
  989. }
  990. return cronStr;
  991. }
  992. private String getMonthlyCronString() throws RuntimeException {
  993. String cronStr;
  994. StringBuilder recurrenceSb = new StringBuilder();
  995. if ( monthlyEditor.isDayNOfMonth() ) {
  996. recurrenceSb.append( RecurrenceType.DayNOfMonth ).append( SPACE )
  997. .append( getTimeOfRecurrence() ).append( SPACE )
  998. .append( monthlyEditor.getDayOfMonth() );
  999. } else if ( monthlyEditor.isNthDayNameOfMonth() ) {
  1000. if ( monthlyEditor.getWeekOfMonth() != WeekOfMonth.LAST ) {
  1001. String weekOfMonth = Integer.toString( monthlyEditor.getWeekOfMonth().value() + 1 );
  1002. String dayOfWeek = Integer.toString( monthlyEditor.getDayOfWeek().value() + 1 );
  1003. recurrenceSb.append( RecurrenceType.NthDayNameOfMonth ).append( SPACE )
  1004. .append( getTimeOfRecurrence() ).append( SPACE )
  1005. .append( dayOfWeek ).append( SPACE )
  1006. .append( weekOfMonth );
  1007. } else {
  1008. String dayOfWeek = Integer.toString( monthlyEditor.getDayOfWeek().value() + 1 );
  1009. recurrenceSb.append( RecurrenceType.LastDayNameOfMonth ).append( SPACE )
  1010. .append( getTimeOfRecurrence() ).append( SPACE )
  1011. .append( dayOfWeek );
  1012. }
  1013. } else {
  1014. throw new RuntimeException( MSGS.noRadioBtnsSelected() );
  1015. }
  1016. try {
  1017. cronStr = CronParser.recurrenceStringToCronString( recurrenceSb.toString() );
  1018. } catch (CronParseException e) {
  1019. throw new RuntimeException( MSGS.invalidRecurrenceString( recurrenceSb.toString() ) );
  1020. }
  1021. return cronStr;
  1022. }
  1023. private String getYearlyCronString() throws RuntimeException {
  1024. String cronStr;
  1025. StringBuilder recurrenceSb = new StringBuilder();
  1026. if ( yearlyEditor.isEveryMonthOnNthDay() ) {
  1027. String monthOfYear = Integer.toString( yearlyEditor.getMonthOfYear0().value() + 1 );
  1028. recurrenceSb.append( RecurrenceType.EveryMonthNameN ).append( SPACE )
  1029. .append( getTimeOfRecurrence() ).append( SPACE )
  1030. .append( yearlyEditor.getDayOfMonth() ).append( SPACE )
  1031. .append( monthOfYear );
  1032. } else if ( yearlyEditor.isNthDayNameOfMonthName() ) {
  1033. if ( yearlyEditor.getWeekOfMonth() != WeekOfMonth.LAST ) {
  1034. String monthOfYear = Integer.toString( yearlyEditor.getMonthOfYear1().value() + 1 );
  1035. String dayOfWeek = Integer.toString( yearlyEditor.getDayOfWeek().value() + 1 );
  1036. String weekOfMonth = Integer.toString( yearlyEditor.getWeekOfMonth().value() + 1 );
  1037. recurrenceSb.append( RecurrenceType.NthDayNameOfMonthName ).append( SPACE )
  1038. .append( getTimeOfRecurrence() ).append( SPACE )
  1039. .append( dayOfWeek ).append( SPACE )
  1040. .append( weekOfMonth ).append( SPACE )
  1041. .append( monthOfYear );
  1042. } else {
  1043. String monthOfYear = Integer.toString( yearlyEditor.getMonthOfYear1().value() + 1 );
  1044. String dayOfWeek = Integer.toString( yearlyEditor.getDayOfWeek().value() + 1 );
  1045. recurrenceSb.append( RecurrenceType.LastDayNameOfMonthName ).append( SPACE )
  1046. .append( getTimeOfRecurrence() ).append( SPACE )
  1047. .append( dayOfWeek ).append( SPACE )
  1048. .append( monthOfYear );
  1049. }
  1050. } else {
  1051. throw new RuntimeException( MSGS.noRadioBtnsSelected() );
  1052. }
  1053. try {
  1054. cronStr = CronParser.recurrenceStringToCronString( recurrenceSb.toString() );
  1055. } catch (CronParseException e) {
  1056. throw new RuntimeException( MSGS.invalidRecurrenceString( recurrenceSb.toString() ) );
  1057. }
  1058. return cronStr;
  1059. }
  1060. private StringBuilder getTimeOfRecurrence() {
  1061. int timeOfDayAdjust = ( startTimePicker.getTimeOfDay().equals( TimeUtil.TimeOfDay.AM ) )
  1062. ? TimeUtil.MIN_HOUR // 0
  1063. : TimeUtil.MAX_HOUR; // 12
  1064. String strHour = StringUtils.addStringToInt( startTimePicker.getHour(), timeOfDayAdjust );
  1065. return new StringBuilder().append( "00" ).append( SPACE ) //$NON-NLS-1$
  1066. .append( startTimePicker.getMinute() ).append( SPACE )
  1067. .append( strHour );
  1068. }
  1069. // TODO sbarkdull
  1070. //private static DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.LONG, Locale.getDefault());
  1071. public void setStartTime( String startTime ) {
  1072. startTimePicker.setTime( startTime );
  1073. }
  1074. public String getStartTime() {
  1075. return startTimePicker.getTime();
  1076. }
  1077. public void setStartDate( Date startDate ) {
  1078. dateRangeEditor.setStartDate( startDate );
  1079. }
  1080. public Date getStartDate() {
  1081. return dateRangeEditor.getStartDate();
  1082. }
  1083. public void setEndDate( Date endDate ) {
  1084. dateRangeEditor.setEndDate( endDate );
  1085. }
  1086. public Date getEndDate() {
  1087. return dateRangeEditor.getEndDate();
  1088. }
  1089. public void setNoEndDate() {
  1090. dateRangeEditor.setNoEndDate();
  1091. }
  1092. public void setEndBy() {
  1093. dateRangeEditor.setEndBy();
  1094. }
  1095. public TemporalValue getTemporalState() {
  1096. return temporalState;
  1097. }
  1098. public void setTemporalState(TemporalValue temporalState) {
  1099. this.temporalState = temporalState;
  1100. selectTemporalPanel( temporalState );
  1101. }
  1102. /**
  1103. * NOTE: should only ever be used by validators. This is a backdoor
  1104. * into this class that shouldn't be here, do not use this method
  1105. * unless you are validating.
  1106. *
  1107. * @return DateRangeEditor
  1108. */
  1109. public DateRangeEditor getDateRangeEditor() {
  1110. return dateRangeEditor;
  1111. }
  1112. /**
  1113. * NOTE: should only ever be used by validators. This is a backdoor
  1114. * into this class that shouldn't be here, do not use this method
  1115. * unless you are validating.
  1116. *
  1117. * @return SecondlyRecurrencePanel
  1118. */
  1119. public SecondlyRecurrenceEditor getSecondlyEditor() {
  1120. return secondlyEditor;
  1121. }
  1122. /**
  1123. * NOTE: should only ever be used by validators. This is a backdoor
  1124. * into this class that shouldn't be here, do not use this method
  1125. * unless you are validating.
  1126. *
  1127. * @return MinutelyRecurrencePanel
  1128. */
  1129. public MinutelyRecurrenceEditor getMinutelyEditor() {
  1130. return minutelyEditor;
  1131. }
  1132. /**
  1133. * NOTE: should only ever be used by validators. This is a backdoor
  1134. * into this class that shouldn't be here, do not use this method
  1135. * unless you are validating.
  1136. *
  1137. * @return HourlyRecurrencePanel
  1138. */
  1139. public HourlyRecurrenceEditor getHourlyEditor() {
  1140. return hourlyEditor;
  1141. }
  1142. /**
  1143. * NOTE: should only ever be used by validators. This is a backdoor
  1144. * into this class that shouldn't be here, do not use this method
  1145. * unless you are validating.
  1146. *
  1147. * @return DailyRecurrencePanel
  1148. */
  1149. public DailyRecurrenceEditor getDailyEditor() {
  1150. return dailyEditor;
  1151. }
  1152. /**
  1153. * NOTE: should only ever be used by validators. This is a backdoor
  1154. * into this class that shouldn't be here, do not use this method
  1155. * unless you are validating.
  1156. *
  1157. * @return WeeklyRecurrencePanel
  1158. */
  1159. public WeeklyRecurrenceEditor getWeeklyEditor() {
  1160. return weeklyEditor;
  1161. }
  1162. /**
  1163. * NOTE: should only ever be used by validators. This is a backdoor
  1164. * into this class that shouldn't be here, do not use this method
  1165. * unless you are validating.
  1166. *
  1167. * @return MonthlyRecurrencePanel
  1168. */
  1169. public MonthlyRecurrenceEditor getMonthlyEditor() {
  1170. return monthlyEditor;
  1171. }
  1172. /**
  1173. * NOTE: should only ever be used by validators. This is a backdoor
  1174. * into this class that shouldn't be here, do not use this method
  1175. * unless you are validating.
  1176. *
  1177. * @return YearlyRecurrencePanel
  1178. */
  1179. public YearlyRecurrenceEditor getYearlyEditor() {
  1180. return yearlyEditor;
  1181. }
  1182. public void setOnChangeHandler( ICallback<IChangeHandler> handler ) {
  1183. this.onChangeHandler = handler;
  1184. }
  1185. private void changeHandler() {
  1186. if ( null != onChangeHandler ) {
  1187. onChangeHandler.onHandle( this );
  1188. }
  1189. }
  1190. private void configureOnChangeHandler() {
  1191. final RecurrenceEditor localThis = this;
  1192. ICallback<IChangeHandler> handler = new ICallback<IChangeHandler>() {
  1193. public void onHandle(IChangeHandler o) {
  1194. localThis.changeHandler();
  1195. }
  1196. };
  1197. startTimePicker.setOnChangeHandler(handler);
  1198. dateRangeEditor.setOnChangeHandler(handler);
  1199. secondlyEditor.setOnChangeHandler(handler);
  1200. minutelyEditor.setOnChangeHandler(handler);
  1201. hourlyEditor.setOnChangeHandler(handler);
  1202. dailyEditor.setOnChangeHandler(handler);
  1203. weeklyEditor.setOnChangeHandler(handler);
  1204. monthlyEditor.setOnChangeHandler(handler);
  1205. yearlyEditor.setOnChangeHandler(handler);
  1206. }
  1207. }