Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / swing / textBoxWithCalendar / JCalendarCDatePanel.java @ 40561

History | View | Annotate | Download (6.54 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.gui.beans.swing.textBoxWithCalendar;
25

    
26
import java.awt.BorderLayout;
27
import java.awt.Color;
28
import java.awt.Dimension;
29
import java.text.SimpleDateFormat;
30
import java.util.Calendar;
31
import java.util.Date;
32
import java.util.Locale;
33

    
34
import javax.swing.JPanel;
35
import javax.swing.JToolTip;
36

    
37
import org.freixas.jcalendar.JCalendarCombo;
38
import org.gvsig.gui.beans.Messages;
39
import org.gvsig.gui.beans.controls.MultiLineToolTip;
40

    
41
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
42
 *
43
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
44
 *
45
 * This program is free software; you can redistribute it and/or
46
 * modify it under the terms of the GNU General Public License
47
 * as published by the Free Software Foundation; either version 2
48
 * of the License, or (at your option) any later version.
49
 *
50
 * This program is distributed in the hope that it will be useful,
51
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
52
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
53
 * GNU General Public License for more details.
54
 *
55
 * You should have received a copy of the GNU General Public License
56
 * along with this program; if not, write to the Free Software
57
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
58
 *
59
 * For more information, contact:
60
 *
61
 *  Generalitat Valenciana
62
 *   Conselleria d'Infraestructures i Transport
63
 *   Av. Blasco Ib??ez, 50
64
 *   46010 VALENCIA
65
 *   SPAIN
66
 *
67
 *      +34 963862235
68
 *   gvsig@gva.es
69
 *      www.gvsig.gva.es
70
 *
71
 *    or
72
 *
73
 *   IVER T.I. S.A
74
 *   Salamanca 50
75
 *   46005 Valencia
76
 *   Spain
77
 *
78
 *   +34 963163400
79
 *   dac@iver.es
80
 */
81

    
82
/**
83
 * Creates a Panel for include in other panels -> this panel allows users to set the date they want
84
 * The difference from this class to the JCalendarDatePanel is that in this class the user can move
85
 *    the calendar with the mouse
86
 * 
87
 * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
88
 */
89
public class JCalendarCDatePanel extends JPanel implements IMethodsForGraphicalCalendarComponents, java.io.Serializable {
90
        private static final long serialVersionUID = -1698696825218186886L;
91

    
92
        private final int defaultWidth = 120;
93
        private final int defaultHeight = 19;
94
        private JCalendarCombo calendar = null;
95
        
96
        /**
97
         * This is the default constructor: creates the panel with a JCalendar object
98
         */
99
        public JCalendarCDatePanel() {
100
                        
101
                try
102
                {
103
                        // Set properties to the current panel
104
                        this.setPreferredSize(new Dimension(defaultWidth, defaultHeight));
105
                        this.setLayout(new BorderLayout());
106
                        
107
                    // Adds the JCalendar to the current panel
108
                    this.add(this.getJCalendarCombo());
109
                }
110
                catch(Exception e)
111
                {
112
                        e.printStackTrace();
113
                }
114
        }
115
        
116
        /**
117
         * This is the default constructor with 2 parameters: creates the panel with a JCalendar object
118
         */
119
        public JCalendarCDatePanel(int width, int height) {
120
                        
121
                try {
122
                        
123
                        // Set properties to the current panel
124
                        this.setPreferredSize(new Dimension(width, height));
125
                        this.setLayout(new BorderLayout());                        
126
                    
127
                    // Adds the JCalendar to the current panel
128
                    this.add(this.getJCalendarCombo());
129
                }
130
                catch(Exception e)
131
                {
132
                        e.printStackTrace();
133
                }
134
        }
135

    
136
        /**
137
         * Returns a reference to the JCalendarCombo private attribute
138
         * 
139
         * @return A reference to the calendar
140
         */
141
        private JCalendarCombo getJCalendarCombo() {
142
                if (this.calendar == null) {
143
                        // Create a JCalendar
144
                        calendar = new JCalendarCombo(Calendar.getInstance(), Locale.getDefault(), JCalendarCombo.DISPLAY_DATE, true);
145
                        calendar.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
146
                    calendar.setEditable(false);                    
147
                    calendar.setBackground(Color.WHITE);
148
                    calendar.setToolTipText(Messages.getText("calendarSelectDate"));
149
                    calendar.setToolTipTextToMonthDecrButton(Messages.getText("calendarBackOneMonth"));
150
                    calendar.setToolTipTextToMonthIncrButton(Messages.getText("calendarForwardOneMonth"));
151
                    calendar.setToolTipTextToYearDecrButton(Messages.getText("calendarBackOneYear"));
152
                    calendar.setToolTipTextToYearIncrButton(Messages.getText("calendarForwardOneYear"));
153
                }
154
                
155
                return this.calendar;
156
        }
157
        
158
        /**
159
         * Sets the size of the CalendarDatePanel for resize the JCalendarCommbo and this panel
160
         * 
161
         * @param width (the new Width for the panel)
162
         * @param height (the new Height for the panel)
163
         */
164
        public void setPreferredSizeResize(int width, int height)
165
        {
166
                this.setPreferredSize(new Dimension(width, height));
167
                calendar.setPreferredSize(new Dimension(width, height));                   
168
                calendar.setSize(new Dimension(width, height));
169
                this.revalidate();
170
        }
171
        
172
        /*
173
         *  (non-Javadoc)
174
         * @see org.gvsig.gui.beans.swing.textBoxWithCalendar.IMethodsForGraphicalCalendarComponents#setDate(java.util.Date)
175
         */
176
        public void setDate(Date date)
177
        {
178
                calendar.setDate(date);
179
        }
180
        
181
        /*
182
         *  (non-Javadoc)
183
         * @see org.gvsig.gui.beans.swing.textBoxWithCalendar.IMethodsForGraphicalCalendarComponents#getDate()
184
         */
185
        public Date getDate()
186
        {
187
                return calendar.getDate();
188
        }
189
        
190
        /**
191
         * Allows select the date
192
         */
193
        public void enableCalendar()
194
        {
195
                calendar.setEnabled(true);
196
        }
197
        
198
        /**
199
         * Don't allow select the date
200
         */
201
        public void disableCalendar()
202
        {
203
                calendar.setEnabled(false);
204
        }        
205
        
206
        /*
207
         *  (non-Javadoc)
208
         * @see org.gvsig.gui.beans.swing.textBoxWithCalendar.IMethodsForGraphicalCalendarComponents#getFormattedDate()
209
         */
210
        public String getFormattedDate() {
211
                return new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(this.getDate());
212
        }
213

    
214
        /*
215
         * (non-Javadoc)
216
         * @see javax.swing.JComponent#createToolTip()
217
         */
218
    public JToolTip createToolTip() {
219
            // Multiline support
220
            MultiLineToolTip tip = new MultiLineToolTip();
221
            tip.setComponent(this);
222
            return tip;
223
    }        
224
}