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  import java.util.Date;
57  
58  /**
59   * This class provides a simple constraint to limit the selectable date to be inside a given range.
60   *
61   * @author Frankenberger Simon
62   */
63  public class RangeConstraint implements DateSelectionConstraint {
64  
65      /**
66       * The lower bound of selectable dates.
67       */
68      private final Calendar after;
69  
70      /**
71       * The upper bound of selectable dates.
72       */
73      private final Calendar before;
74  
75      /**
76       * Create a new constraint for values between (and excluding) the given dates.
77       *
78       * @param after  Lower bound for values, excluding.
79       * @param before Upper bound for values, excluding.
80       */
81      public RangeConstraint(Calendar after, Calendar before) {
82          this.after = after;
83          this.before = before;
84  
85          // remove hours / minutes / seconds from dates
86          cleanTime();
87      }
88  
89      /**
90       * Create a new constraint for values between the given dates.
91       *
92       * @param after  Lower bound for values, including.
93       * @param before Upper bound for values, including.
94       */
95      public RangeConstraint(Date after, Date before) {
96          Calendar _after = Calendar.getInstance();
97          Calendar _before = Calendar.getInstance();
98  
99          _after.setTime(after);
100         _before.setTime(before);
101 
102         this.after = _after;
103         this.before = _before;
104 
105         // remove hours / minutes / seconds from dates
106         cleanTime();
107     }
108 
109     /**
110      * Simple helper method to remove the time the date bounds.
111      */
112     private void cleanTime() {
113         if (after != null) {
114             after.set(Calendar.HOUR_OF_DAY, 0);
115             after.set(Calendar.MINUTE, 0);
116             after.set(Calendar.SECOND, 0);
117             after.set(Calendar.MILLISECOND, 0);
118         }
119 
120         if (before != null) {
121             before.set(Calendar.HOUR_OF_DAY, 23);
122             before.set(Calendar.MINUTE, 59);
123             before.set(Calendar.SECOND, 59);
124             before.set(Calendar.MILLISECOND, 999);
125         }
126     }
127 
128     /** {@inheritDoc} */
129     public boolean isValidSelection(DateModel model) {
130         boolean result = true;
131 
132         if (model.isSelected() && after != null) {
133             Calendar value = Calendar.getInstance();
134             value.set(model.getYear(), model.getMonth(), model.getDay());
135             value.set(Calendar.HOUR_OF_DAY, 0);
136             value.set(Calendar.MINUTE, 0);
137             value.set(Calendar.SECOND, 0);
138             value.set(Calendar.MILLISECOND, 0);
139             result = value.after(after);
140         }
141         if (model.isSelected() && before != null) {
142             Calendar value = Calendar.getInstance();
143             value.set(model.getYear(), model.getMonth(), model.getDay());
144             value.set(Calendar.HOUR_OF_DAY, 0);
145             value.set(Calendar.MINUTE, 0);
146             value.set(Calendar.SECOND, 0);
147             value.set(Calendar.MILLISECOND, 0);
148             result &= value.before(before);
149         }
150 
151         return result;
152     }
153 
154     /** {@inheritDoc} */
155     @Override
156     // Generated with eclipse depending on: after, before
157     public int hashCode() {
158         final int prime = 31;
159         int result = 1;
160         result = prime * result + ((after == null) ? 0 : after.hashCode());
161         result = prime * result + ((before == null) ? 0 : before.hashCode());
162         return result;
163     }
164 
165     /** {@inheritDoc} */
166     @Override
167     public boolean equals(Object obj) {
168         if (this == obj) {
169             return true;
170         }
171         if (obj == null) {
172             return false;
173         }
174         if (getClass() != obj.getClass()) {
175             return false;
176         }
177 
178         RangeConstraint other = (RangeConstraint) obj;
179         if (after == null) {
180             if (other.after != null) {
181                 return false;
182             }
183         } else if (!after.equals(other.after)) {
184             return false;
185         }
186         if (before == null) {
187             return other.before == null;
188         } else return before.equals(other.before);
189     }
190 
191 }