View Javadoc
1   /*******************************************************************************
2    * JDateButton: Java Swing Date Button
3    * Copyright 2012,2014 Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   * ------------------------------------------------------------
17   * SubVersion Revision Information:
18   * $URL: https://svn.code.sf.net/p/jdatebutton/code/tags/v2.1.0-b3/jdatebutton-swing/src/main/java/net/sourceforge/jdatebutton/swing/JDateConfig.java $
19   * $Revision: 34 $
20   * $Author: tonywasher $
21   * $Date: 2015-12-01 16:21:13 +0000 (Tue, 01 Dec 2015) $
22   ******************************************************************************/
23  package net.sourceforge.jdatebutton.swing;
24  
25  import java.time.LocalDate;
26  
27  import net.sourceforge.jdatebutton.JDateBaseConfig;
28  import net.sourceforge.jdatebutton.JDateFormatter;
29  
30  /**
31   * Provides Date Management support for {@link JDateButton}.
32   */
33  public class JDateConfig
34          extends JDateBaseConfig<JDateButton> {
35      /**
36       * The maximum day name length.
37       */
38      public static final int MAX_DAY_NAME_LEN = 3;
39  
40      /**
41       * Maximum length of weekday name (e.g. for Wednesday, 3 = Wed, 2 = We, 1 = W).
42       */
43      private int theMaxDayLen = MAX_DAY_NAME_LEN;
44  
45      /**
46       * Shrink long weekday names from right(true) or left(false).
47       */
48      private boolean doShrinkFromRight = true;
49  
50      /**
51       * Make Weekday names pretty (Capitalise first char, and lower-case the rest).
52       */
53      private boolean doPretty = true;
54  
55      /**
56       * Fire selection event on date selection by function call.
57       */
58      private boolean fireOnAllDateChanges = true;
59  
60      /**
61       * The selected Date.
62       */
63      private LocalDate theSelected = null;
64  
65      /**
66       * Constructor.
67       */
68      protected JDateConfig() {
69          /* Set default locale and format */
70          super();
71      }
72  
73      /**
74       * Constructor.
75       * @param pFormatter the date formatter
76       */
77      public JDateConfig(final JDateFormatter pFormatter) {
78          /* Set formatter */
79          super(pFormatter);
80      }
81  
82      @Override
83      public final LocalDate getSelectedDate() {
84          return theSelected;
85      }
86  
87      /**
88       * Do we make weekday names pretty (Capitalise first char, and lower-case the rest).
89       * @return true/false
90       */
91      public boolean doPretty() {
92          return doPretty;
93      }
94  
95      /**
96       * Maximum length of weekday name (e.g. for Wednesday, 3 = Wed, 2 = We, 1 = W).
97       * @return the maximum length
98       */
99      public int getMaxDayLen() {
100         return theMaxDayLen;
101     }
102 
103     /**
104      * Shrink long weekday names from right(true) or left(false).
105      * @return true/false
106      */
107     public boolean doShrinkFromRight() {
108         return doShrinkFromRight;
109     }
110 
111     /**
112      * Do we Fire Property Change events on all date changes.
113      * @return true/false
114      */
115     public boolean fireOnAllDateChanges() {
116         return fireOnAllDateChanges;
117     }
118 
119     /**
120      * Set the format options.
121      * @param pMaxDayLen the maximum string length for the weekday
122      * @param bShrinkFromRight remove excess weekday characters from right
123      * @param bPretty capitalise the first character of weekday/month
124      */
125     public void setFormatOptions(final int pMaxDayLen,
126                                  final boolean bShrinkFromRight,
127                                  final boolean bPretty) {
128         /* Store options */
129         theMaxDayLen = pMaxDayLen;
130         doShrinkFromRight = bShrinkFromRight;
131         doPretty = bPretty;
132 
133         /* Request rebuild of names */
134         rebuildNames();
135     }
136 
137     /**
138      * Set/Clear fire on all Date change flag. If this flag is not set then only changes made by the
139      * dialog will result in PropertyChange events being sent to listeners. Explicit Changes made by
140      * the application will not result in events.
141      * @param pFireOnAllDateChanges true/false
142      */
143     public void setFireOnAllDateChanges(final boolean pFireOnAllDateChanges) {
144         fireOnAllDateChanges = pFireOnAllDateChanges;
145     }
146 
147     @Override
148     public final void storeSelectedDate(final LocalDate pDate) {
149         /* Store the date */
150         LocalDate myOld = theSelected;
151         theSelected = pDate;
152 
153         /* Fire property change */
154         JDateButton myOwner = getOwner();
155         if (myOwner != null) {
156             myOwner.fireDatePropertyChange(myOld, pDate);
157         }
158     }
159 
160     @Override
161     public final void storeExplicitDate(final LocalDate pDate) {
162         /* Store the date */
163         LocalDate myOld = theSelected;
164         theSelected = pDate;
165 
166         /* Fire a Property change if required */
167         JDateButton myOwner = getOwner();
168         if ((myOwner != null)
169             && (fireOnAllDateChanges)) {
170             myOwner.fireDatePropertyChange(myOld, pDate);
171         }
172     }
173 
174     @Override
175     protected LocalDate getInitialDate() {
176         return (theSelected == null)
177                                      ? currentDate()
178                                      : theSelected;
179     }
180 }