Statistics
| Revision:

root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / swing / textBoxWithCalendar / JCalendarCDatePanel.java @ 13136

History | View | Annotate | Download (5.27 KB)

1
package org.gvsig.gui.beans.swing.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
        private static final long serialVersionUID = 5913389251907361930L;
67
        private final int defaultWidth = 120;
68
        private final 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
                        // Set properties to the current panel
99
                        this.setPreferredSize(new Dimension(width, height));
100
                        this.setLayout(new BorderLayout());                        
101
                    
102
                    // Adds the JCalendar to the current panel
103
                    this.add(this.getJCalendarCombo());
104
                }
105
                catch(Exception e)
106
                {
107
                        e.printStackTrace();
108
                }
109
        }
110

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