View Javadoc
1   /**
2    * Copyright 2014 Juan Heyns. All rights reserved.
3    * <p/>
4    * Redistribution and use in source and binary forms, with or without modification, are
5    * permitted provided that the following conditions are met:
6    * <p/>
7    * 1. Redistributions of source code must retain the above copyright notice, this list of
8    * conditions and the following disclaimer.
9    * <p/>
10   * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11   * of conditions and the following disclaimer in the documentation and/or other materials
12   * provided with the distribution.
13   * <p/>
14   * THIS SOFTWARE IS PROVIDED BY JUAN HEYNS ``AS IS'' AND ANY EXPRESS OR IMPLIED
15   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JUAN HEYNS OR
17   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23   * <p/>
24   * The views and conclusions contained in the software and documentation are those of the
25   * authors and should not be interpreted as representing official policies, either expressed
26   * or implied, of Juan Heyns.
27   */
28  package fr.ifremer.quadrige3.ui.swing.component.date.constraints;
29  
30  /*
31   * #%L
32   * Reef DB :: UI
33   * $Id:$
34   * $HeadURL:$
35   * %%
36   * Copyright (C) 2014 - 2015 Ifremer
37   * %%
38   * This program is free software: you can redistribute it and/or modify
39   * it under the terms of the GNU Affero General Public License as published by
40   * the Free Software Foundation, either version 3 of the License, or
41   * (at your option) any later version.
42   * 
43   * This program is distributed in the hope that it will be useful,
44   * but WITHOUT ANY WARRANTY; without even the implied warranty of
45   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
46   * GNU General Public License for more details.
47   * 
48   * You should have received a copy of the GNU Affero General Public License
49   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
50   * #L%
51   */
52  
53  import fr.ifremer.quadrige3.ui.swing.component.date.DateModel;
54  
55  import java.util.Calendar;
56  
57  /**
58   * This class provides a simple constraint to limit the selectable date to be a
59   * workday (Monday - Friday).
60   *
61   * @author Frankenberger Simon
62   */
63  public class WeekdayConstraint implements DateSelectionConstraint {
64  
65      /** {@inheritDoc} */
66      public boolean isValidSelection(DateModel model) {
67          if (model.isSelected()) {
68              Calendar value = Calendar.getInstance();
69              value.set(model.getYear(), model.getMonth(), model.getDay());
70              value.set(Calendar.HOUR_OF_DAY, 0);
71              value.set(Calendar.MINUTE, 0);
72              value.set(Calendar.SECOND, 0);
73              value.set(Calendar.MILLISECOND, 0);
74  
75              switch (value.get(Calendar.DAY_OF_WEEK)) {
76                  case Calendar.MONDAY:
77                  case Calendar.TUESDAY:
78                  case Calendar.WEDNESDAY:
79                  case Calendar.THURSDAY:
80                  case Calendar.FRIDAY:
81                      return true;
82                  default:
83                      return false;
84              }
85          } else {
86              return true;
87          }
88      }
89  
90  }