Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libUI / src / org / gvsig / gui / beans / textBoxWithCalendar / JCalendarDatePanel.java @ 9780

History | View | Annotate | Download (6.4 KB)

1 7891 ppiqueras
package org.gvsig.gui.beans.textBoxWithCalendar;
2
3
import java.io.Serializable;
4
import java.text.SimpleDateFormat;
5
import java.util.Date;
6
import java.util.Locale;
7
8
import javax.swing.JButton;
9
import javax.swing.JPanel;
10
import javax.swing.SwingConstants;
11
12 9033 ppiqueras
import org.gvsig.gui.beans.Messages;
13 7891 ppiqueras
14
import java.awt.Color;
15
import java.awt.Dimension;
16
import java.awt.GridBagConstraints;
17
import java.awt.GridBagLayout;
18 7957 ppiqueras
import java.awt.event.ComponentListener;
19 8092 ppiqueras
import java.awt.event.MouseAdapter;
20 7891 ppiqueras
import java.awt.event.MouseEvent;
21
import java.awt.event.MouseListener;
22
23
/**
24
 * Creates a Panel for include in other panels -> this panel allows users to set the date they want
25
 * The difference from this class to the JCalendarDatePanel is that in this class the user can move
26
 *    the calendar with the mouse
27
 *
28
 * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
29
 */
30
public class JCalendarDatePanel extends JPanel implements Serializable{
31 9780 ppiqueras
        private static final long serialVersionUID = -3708600032851165498L;
32 7891 ppiqueras
        private JButton jButton = null;
33 9780 ppiqueras
        private JCalendarDateDialog jDialogCalendar = null;
34
        private int currentButtonWidth = 120;
35
        private int currentButtonHeight = 19;
36 7891 ppiqueras
        private MouseListener mouseListener;
37
38
        /**
39
         * Default Constructor
40
         */
41
        public JCalendarDatePanel() {
42
43
                try
44
                {
45 9780 ppiqueras
                        this.setPreferredSize(new Dimension(currentButtonWidth, currentButtonHeight));
46 7891 ppiqueras
                        initialize();
47
                }
48
                catch(Exception e)
49
                {
50
                        e.printStackTrace();
51
                }
52
        }
53
54
        /**
55
         * This is the default constructor with 2 parameters for set the size
56
         */
57
        public JCalendarDatePanel(int width, int height) {
58
59
                try
60
                {
61 9780 ppiqueras
                        currentButtonWidth = width;
62
                        currentButtonHeight = height;
63 7891 ppiqueras
64 9780 ppiqueras
                        this.setPreferredSize(new Dimension(currentButtonWidth, currentButtonHeight));
65 7891 ppiqueras
                        initialize();
66
                }
67
                catch(Exception e)
68
                {
69
                        e.printStackTrace();
70
                }
71
        }
72
73
        /**
74
         * This method initializes this
75
         */
76
        private void initialize() {
77
                // Set properties to the current panel
78
                GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
79
                gridBagConstraints1.gridx = 1;
80
                gridBagConstraints1.gridy = 0;
81
82
                GridBagLayout gridBagLayout = new GridBagLayout();
83
                this.setLayout(gridBagLayout);
84
85
                // Add components to this panel:
86
                GridBagConstraints gridBagConstraints = new GridBagConstraints();
87
                gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
88
                gridBagConstraints.gridy = 0;
89
                gridBagConstraints.weightx = 1.0;
90
                gridBagConstraints.gridx = 0;
91
92
                // Add the JButton
93
                this.add(getJButton(), gridBagConstraints1);
94
95
                // Defines the mouseLIstener
96
                this.createMouseListener();
97
98
                // Adds the mouseListener to the jTextField and the jButton
99
                jButton.addMouseListener(mouseListener);
100
        }
101
102
        /**
103 9780 ppiqueras
         * Gets the default height of this panel
104 7891 ppiqueras
         */
105 9780 ppiqueras
        public int getCurrentButtonHeight()
106 7891 ppiqueras
        {
107 9780 ppiqueras
                return this.currentButtonHeight;
108 7891 ppiqueras
        }
109
110
        /**
111 9780 ppiqueras
         * Gets the default width of this panel
112 7891 ppiqueras
         */
113 9780 ppiqueras
        public int getCurrentButtonWidth()
114 7891 ppiqueras
        {
115 9780 ppiqueras
                return this.currentButtonWidth;
116 7891 ppiqueras
        }
117
118
        /**
119
         * This method initializes jButton
120
         *
121
         * @return javax.swing.JButton
122
         */
123
        private JButton getJButton() {
124
                if (jButton == null) {
125
                        jButton = new JButton();
126 9780 ppiqueras
                        jButton.setPreferredSize(new Dimension(currentButtonWidth, currentButtonHeight));
127 7891 ppiqueras
                        jButton.setHorizontalTextPosition(SwingConstants.LEFT);
128
                        jButton.setBackground(Color.WHITE);
129 9780 ppiqueras
                        jButton.setText(this.getFormattedDate(this.getJCalendarDateDialog().getDate()));
130 7891 ppiqueras
                }
131
                return jButton;
132
        }
133
134
        /**
135
         * This method initializes jDialogCalendar
136
         *
137
         * @return javax.swing.JDialog
138
         */
139 9780 ppiqueras
        public JCalendarDateDialog getJCalendarDateDialog() {
140 7891 ppiqueras
                if (jDialogCalendar == null) {
141 9780 ppiqueras
                        jDialogCalendar = new JCalendarDateDialog();
142 7891 ppiqueras
                }
143
                return jDialogCalendar;
144
        }
145
146
147
        /**
148
         * Gets the date of the calendar
149
         *
150
         * @return Date
151
         */
152
        public Date getDate() {
153 9780 ppiqueras
                return this.getJCalendarDateDialog().getDate();
154 7891 ppiqueras
        }
155
156
        /**
157
         * Sets the date to the calendar
158
         *
159
         * @param Date
160
         */
161
        public void setDate(Date date)
162
        {
163 9780 ppiqueras
                this.getJCalendarDateDialog().setDate(date);
164 7891 ppiqueras
        }
165
166
        /**
167
         * Allows select the date
168
         */
169
        public void enableCalendar() {
170
                jButton.setEnabled(true);
171
        }
172
173
        /**
174
         * Don't allow select the date
175
         */
176
        public void disableCalendar() {
177
                jButton.setEnabled(false);
178
        }
179
180
        /**
181
         * Returns the date formatted
182
         *
183
         * @param Date
184
         * @return String The formatted date
185
         */
186
        private String getFormattedDate(Date d) {
187
                return new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(d);
188
        }
189
190
        /**
191 7957 ppiqueras
         * Returns the date selected, formatted
192
         *
193
         * @return String The formatted date
194
         */
195
        public String getFormattedDate() {
196
                return new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(this.getDate());
197
        }
198
199
        /**
200 7891 ppiqueras
         * Gets the title of the JDialog with the calendar component. The title is displayed in the JDialog's border.
201 7957 ppiqueras
         * @return Title
202 7891 ppiqueras
         */
203
        public String getTitle() {
204 9780 ppiqueras
                return this.getJCalendarDateDialog().getTitle();
205 7891 ppiqueras
        }
206
207
        /**
208
         * Sets the title of the JDialog with the calendar component.
209
         *
210
         * @param String
211
         */
212
        public void setTitle(String title) {
213 9780 ppiqueras
                this.getJCalendarDateDialog().setTitle(title);
214 7891 ppiqueras
        }
215
216
        /**
217
         * Sets the default title of the JDialog with the calendar component.
218
         */
219
        public void setDefaultTitle() {
220 9780 ppiqueras
                this.getJCalendarDateDialog().setTitle(Messages.getText("calendarTitle"));
221 7891 ppiqueras
        }
222
223
        /**
224
         * @see java.awt.Dialog#setModal(boolean)
225
         */
226
        public void setModal(boolean b) {
227 9780 ppiqueras
                this.getJCalendarDateDialog().setModal(b);
228 7891 ppiqueras
        }
229
230
        /**
231 9780 ppiqueras
         * Sets new size for the inner JButton
232
         *
233
         * @param width (the new Width for the button)
234
         * @param height (the new Height for the button)
235
         */
236
        public void setSizeResizeJButton(int width, int height) {
237
                this.getJButton().setPreferredSize(new Dimension(width, height));
238
        }
239
240
        /**
241 7957 ppiqueras
         * Adds a component listener for the inner JDialog that cointins the calendar interface
242
         *
243
         * @param componentListener
244
         */
245
        public void addComponentListenerForJDialogCalendar(ComponentListener componentListener) {
246 9780 ppiqueras
                getJCalendarDateDialog().addComponentListener(componentListener);
247 7957 ppiqueras
        }
248
249
        /**
250 7891 ppiqueras
         * This method creates a Mouse Listener object
251
         */
252 8092 ppiqueras
        private void createMouseListener() {
253 7891 ppiqueras
254 8092 ppiqueras
                mouseListener = new MouseAdapter() {
255 7891 ppiqueras
256
                        /*
257
                         *  (non-Javadoc)
258
                         * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
259
                         */
260
                        public void mouseClicked(MouseEvent e) {
261
                                // TODO Auto-generated method stub
262
263
                                // Show the JDialog
264
                                if (getJButton().isEnabled())
265
                                {
266 9780 ppiqueras
                                        getJCalendarDateDialog().setLocationRelativeTo(jButton);
267
                                        getJCalendarDateDialog().show();
268 7891 ppiqueras
                                }
269 8092 ppiqueras
                        }
270 7891 ppiqueras
                };
271
        }
272
273
}  //  @jve:decl-index=0:visual-constraint="10,10"