Statistics
| Revision:

root / trunk / libraries / libUI / src / org / gvsig / gui / beans / textBoxWithCalendar / JCalendarCDatePanel.java @ 9788

History | View | Annotate | Download (5.4 KB)

1
package org.gvsig.gui.beans.textBoxWithCalendar;
2

    
3
import java.awt.BorderLayout;
4
import java.awt.Color;
5
import java.awt.Dimension;
6

    
7
import javax.swing.JPanel;
8

    
9
import org.freixas.jcalendar.*;
10
import org.gvsig.gui.beans.Messages;
11

    
12
import java.text.SimpleDateFormat;
13
import java.util.Calendar;
14
import java.util.Date;
15
import java.util.Locale;
16

    
17
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
18
 *
19
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
20
 *
21
 * This program is free software; you can redistribute it and/or
22
 * modify it under the terms of the GNU General Public License
23
 * as published by the Free Software Foundation; either version 2
24
 * of the License, or (at your option) any later version.
25
 *
26
 * This program is distributed in the hope that it will be useful,
27
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29
 * GNU General Public License for more details.
30
 *
31
 * You should have received a copy of the GNU General Public License
32
 * along with this program; if not, write to the Free Software
33
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
34
 *
35
 * For more information, contact:
36
 *
37
 *  Generalitat Valenciana
38
 *   Conselleria d'Infraestructures i Transport
39
 *   Av. Blasco Ib??ez, 50
40
 *   46010 VALENCIA
41
 *   SPAIN
42
 *
43
 *      +34 963862235
44
 *   gvsig@gva.es
45
 *      www.gvsig.gva.es
46
 *
47
 *    or
48
 *
49
 *   IVER T.I. S.A
50
 *   Salamanca 50
51
 *   46005 Valencia
52
 *   Spain
53
 *
54
 *   +34 963163400
55
 *   dac@iver.es
56
 */
57

    
58
/**
59
 * Creates a Panel for include in other panels -> this panel allows users to set the date they want
60
 * The difference from this class to the JCalendarDatePanel is that in this class the user can move
61
 *    the calendar with the mouse
62
 * 
63
 * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
64
 */
65
public class JCalendarCDatePanel extends JPanel implements IMethodsForGraphicalCalendarComponents, java.io.Serializable{
66
        
67
        private int defaultWidth = 120;
68
        private int defaultHeight = 19;
69
        private JCalendarCombo calendar = null;
70
        
71
        /**
72
         * This is the default constructor: creates the panel with a JCalendar object
73
         */
74
        public JCalendarCDatePanel() {
75
                        
76
                try
77
                {
78
                        // Set properties to the current panel
79
                        this.setPreferredSize(new Dimension(defaultWidth, defaultHeight));
80
                        this.setLayout(new BorderLayout());
81
                        
82
                    // Adds the JCalendar to the current panel
83
                    this.add(this.getJCalendarCombo());
84
                }
85
                catch(Exception e)
86
                {
87
                        e.printStackTrace();
88
                }
89
        }
90
        
91
        /**
92
         * This is the default constructor with 2 parameters: creates the panel with a JCalendar object
93
         */
94
        public JCalendarCDatePanel(int width, int height) {
95
                        
96
                try
97
                {                        
98
                        defaultWidth = width;
99
                        defaultHeight = height;
100
                        
101
                        // Set properties to the current panel
102
                        this.setPreferredSize(new Dimension(defaultWidth, defaultHeight));
103
                        this.setLayout(new BorderLayout());                        
104
                    
105
                    // Adds the JCalendar to the current panel
106
                    this.add(this.getJCalendarCombo());
107
                }
108
                catch(Exception e)
109
                {
110
                        e.printStackTrace();
111
                }
112
        }
113

    
114
        /**
115
         * Returns a reference to the JCalendarCombo private attribute
116
         * 
117
         * @return A reference to the calendar
118
         */
119
        private JCalendarCombo getJCalendarCombo() {
120
                if (this.calendar == null) {
121
                        // Create a JCalendar
122
                        calendar = new JCalendarCombo(Calendar.getInstance(), Locale.getDefault(), JCalendarCombo.DISPLAY_DATE, true);
123
                        calendar.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
124
                        calendar.setPreferredSize(new Dimension(defaultWidth, defaultHeight));
125
                    calendar.setEditable(false);                    
126
                    calendar.setBackground(Color.WHITE);
127
                    calendar.setToolTipText(Messages.getText("calendarSelectDate"));
128
                    calendar.setToolTipTextToMonthDecrButton(Messages.getText("calendarBackOneMonth"));
129
                    calendar.setToolTipTextToMonthIncrButton(Messages.getText("calendarForwardOneMonth"));
130
                    calendar.setToolTipTextToYearDecrButton(Messages.getText("calendarBackOneYear"));
131
                    calendar.setToolTipTextToYearIncrButton(Messages.getText("calendarForwardOneYear"));
132
                }
133
                
134
                return this.calendar;
135
        }
136
        
137
        /**
138
         * Sets the size of the CalendarDatePanel for resize the JCalendarCommbo and this panel
139
         * 
140
         * @param width (the new Width for the panel)
141
         * @param height (the new Height for the panel)
142
         */
143
        public void setPreferredSizeResize(int width, int height)
144
        {
145
                defaultWidth = width;
146
                defaultHeight = height;
147
                this.setPreferredSize(new Dimension(defaultWidth, defaultHeight));
148
                calendar.setPreferredSize(new Dimension(defaultWidth, defaultHeight));                   
149
                calendar.setSize(new Dimension(defaultWidth, defaultHeight));
150
                this.revalidate();
151
        }
152
        
153
        /*
154
         *  (non-Javadoc)
155
         * @see org.gvsig.gui.beans.textBoxWithCalendar.IMethodsForGraphicalCalendarComponents#setDate(java.util.Date)
156
         */
157
        public void setDate(Date date)
158
        {
159
                calendar.setDate(date);
160
        }
161
        
162
        /*
163
         *  (non-Javadoc)
164
         * @see org.gvsig.gui.beans.textBoxWithCalendar.IMethodsForGraphicalCalendarComponents#getDate()
165
         */
166
        public Date getDate()
167
        {
168
                return calendar.getDate();
169
        }
170
        
171
        /**
172
         * Allows select the date
173
         */
174
        public void enableCalendar()
175
        {
176
                calendar.setEnabled(true);
177
        }
178
        
179
        /**
180
         * Don't allow select the date
181
         */
182
        public void disableCalendar()
183
        {
184
                calendar.setEnabled(false);
185
        }        
186
        
187
        /*
188
         *  (non-Javadoc)
189
         * @see org.gvsig.gui.beans.textBoxWithCalendar.IMethodsForGraphicalCalendarComponents#getFormattedDate()
190
         */
191
        public String getFormattedDate() {
192
                return new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(this.getDate());
193
        }
194
}