wangting
2024-09-27 a3e87f78ee262ca9bb7d9b0c997639d5f3295890
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package com.vci.client.ui.swing.components;
 
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.EventListener;
 
import com.vci.client.ui.date.PopupCalendarPanel;
import com.vci.client.ui.swing.VCISwingUtil;
 
/**
 * 
 * <p>
 * Title:
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2012
 * </p>
 * <p>
 * Company: VCI
 * </p>
 * 
 * @author xchao
 * @time 2012-5-15
 * @version 1.0
 */
public class VCIJCalendarPanel extends VCIJPanel implements ActionListener, EventListener {
 
    /**
     * 
     */
    private static final long serialVersionUID = 2746179740066575026L;
    /**
     * 是否是必须的
     */
    private boolean required = false;
    private boolean hasDate = true;
    /**
     * 是否包含时间
     */
    private boolean hasTime = false;
 
    /**
     * 自定义日期格式字符串
     */
    private String format = "";
    
    private SimpleDateFormat sf = null;
    
    public String itemStyle = null;// add by caill 2016.2.19 增加用来传递附加属性内容的变量
    private VCIJComboBox cbxDate = new VCIJComboBox();
    private VCIJButton btnShowCalendar = VCISwingUtil.createVCIJButton("ShowDateCalendar", "", "", "date.png", this);
 
    /**
     * 构造日期&时间选择对象
     */
    public VCIJCalendarPanel() {
        this(false);
    }
 
    /**
     * 构造日期&时间选择对象
     * 
     * @param giveDefaultValue 是否自动填写日期&时间
     */
    public VCIJCalendarPanel(boolean giveDefaultValue) {
        this(giveDefaultValue ? "yyyy-MM-dd" : "", false, giveDefaultValue);
    }
 
    /**
     * 构造日期&时间选择对象
     * 
     * @param format           日期格式化字符串
     * @param required         是否是必须的
     * @param giveDefaultValue 是否自动填写日期&时间
     */
    public VCIJCalendarPanel(String format, boolean required, boolean giveDefaultValue) {
        this(format, required, giveDefaultValue, false);
    }
 
    /**
     * 构造日期&时间选择对象
     * 
     * @param format           日期格式化字符串
     * @param required         是否是必须的
     * @param giveDefaultValue 是否自动填写日期&时间
     * @param hasTime          是否包含时间
     */
    @Deprecated
    public VCIJCalendarPanel(String format, boolean required, boolean giveDefaultValue, boolean hasTime) {
        this(required, giveDefaultValue, true, hasTime, format);
    }
 
    /**
     * 构造日期&时间选择对象
     * 
     * @param format           日期格式化字符串
     * @param required         是否是必须的
     * @param giveDefaultValue 是否自动填写日期&时间
     * @param hasTime          是否包含时间
     */
    public VCIJCalendarPanel(boolean required, boolean giveDefaultValue, boolean hasDate, boolean hasTime) {
        this(required, giveDefaultValue, hasDate, hasTime, "");
    }
 
    public VCIJCalendarPanel(boolean required, boolean giveDefaultValue, boolean hasDate, boolean hasTime,
            String specFormat) {
        super();
        String format = "";
        if (hasDate && hasTime) {
            format = "yyyy-MM-dd HH:mm:ss";
        } else if (hasDate) {
            format = "yyyy-MM-dd";
        } else if (hasTime) {
            format = "HH:mm:ss";
        }
        if (specFormat != null && !"".equals(specFormat)) {
            format = specFormat;
        }
        setFormat(format);
        setRequired(required);
        setHasDate(hasDate);
        setHasTime(hasTime);
        if (required) {
            giveDefaultValue = true;
        }
        init();
        if (giveDefaultValue && isValidDateFormat(getFormat())) {
            SimpleDateFormat sdf = new SimpleDateFormat(getFormat());
            String dateString = sdf.format(Calendar.getInstance().getTime());
            setDateString(dateString);
        }
        cbxDate.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                firePropertyChange(evt.getPropertyName(), evt.getOldValue(), evt.getNewValue());
            }
        });
    }
 
    private void init() {
        initConmponents();
    }
 
    private void initConmponents() {
        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.gridheight = 1;
        c.gridwidth = 1;
        c.weightx = 1;
        c.weighty = 0;
        c.anchor = GridBagConstraints.NORTHWEST;
        c.fill = GridBagConstraints.BOTH;
        // cbxDate.setPreferredSize(new Dimension(100, 23));
        add(cbxDate, c);
 
        c.gridx = 1;
        c.gridheight = 1;
        c.gridwidth = 1;
        c.weightx = 0;
        c.weighty = 0;
        c.anchor = GridBagConstraints.EAST;
        c.fill = GridBagConstraints.BOTH;
 
        btnShowCalendar.setMinimumSize(new Dimension(27, 23));
        btnShowCalendar.setMaximumSize(new Dimension(27, 23));
        btnShowCalendar.setPreferredSize(new Dimension(27, 23));
 
        add(btnShowCalendar, c);
        btnShowCalendar.setToolTipText(getToolTipString());
 
        c.gridx = 2;
        c.gridheight = 1;
        c.gridwidth = 1;
        c.weightx = 0;
        c.weighty = 0;
        c.anchor = GridBagConstraints.EAST;
        c.fill = GridBagConstraints.BOTH;
        VCIJLabel lbl = new VCIJLabel("");// 为显示
        lbl.setMinimumSize(new Dimension(9, 23));
        lbl.setPreferredSize(new Dimension(9, 23));
        add(lbl, c);
    }
 
    private String getToolTipString() {
        return isHasTime() ? "点击选择日期及时间" : "点击选择日期";
    }
 
    /**
     * 返回自定义日期格式字符串
     * <p>
     * Description:
     * </p>
     * 
     * @author xchao
     * @time 2012-11-21
     * @return
     */
    public String getFormat() {
        return format;
    }
 
    /**
     * 设置 自定义日期格式字符串
     * <p>
     * Description:
     * </p>
     * 
     * @author xchao
     * @time 2012-11-21
     * @param format
     */
    public void setFormat(String format) {
        this.format = format;
        this.sf = new SimpleDateFormat(this.format);
    }
 
    /**
     * 返回是否是必须的
     * <p>
     * Description:
     * </p>
     * 
     * @author xchao
     * @time 2012-11-21
     * @return
     */
    public boolean isRequired() {
        return required;
    }
 
    /**
     * 设置是否是必须的
     * <p>
     * Description:
     * </p>
     * 
     * @author xchao
     * @time 2012-11-21
     * @param required
     */
    public void setRequired(boolean required) {
        this.required = required;
    }
 
    public boolean isHasDate() {
        return hasDate;
    }
 
    public void setHasDate(boolean hasDate) {
        this.hasDate = hasDate;
    }
 
    /**
     * 返回是否包含时间
     * <p>
     * Description:
     * </p>
     * 
     * @author xchao
     * @time 2012-11-21
     * @return
     */
    public boolean isHasTime() {
        return hasTime;
    }
 
    /**
     * 设置是否包含时间
     * <p>
     * Description:
     * </p>
     * 
     * @author xchao
     * @time 2012-11-21
     * @param hasTime
     */
    public void setHasTime(boolean hasTime) {
        this.hasTime = hasTime;
    }
 
    /**
     * 返回日期&时间字符串
     * <p>
     * Description:
     * </p>
     * 
     * @author xchao
     * @time 2012-11-21
     * @return
     */
    public String getDateString() {
        String res = null;
        Object item = cbxDate.getSelectedItem();
        if (item != null) {
            res = item.toString().trim();
        }
        if ("".equals(res)) {
            res = null;
        }
        return res;
    }
    
    public Date getDate() {
        String res = null;
        Object item = cbxDate.getSelectedItem();
        if (item != null) {
            res = item.toString().trim();
        }
        if ("".equals(res)) {
            res = null;
        }
        
        try {
            Date dt = sf.parse(res);
            return dt;
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
 
    // add by caill start 2016.2.19 添加方法用来传递附加条件内容
    public void setItemStyleString(String itemStyleString) {
        itemStyle = itemStyleString;
    }
 
    // add by caill end
    
    public void setDate(Date date){
        cbxDate.removeAllItems();
        if(!isRequired()){
            cbxDate.addItem(" ");
        }
        String itemobj = sf.format(date);
        cbxDate.addItem(itemobj);
        cbxDate.setSelectedItem(itemobj);
    }
 
    
    /**
     * 设置日期格式字符串的值
     * <p>
     * Description:
     * </p>
     * 
     * @author xchao
     * @time 2012-11-21
     * @param dateString
     */
    public void setDateString(String dateString) {
        if (!isDateFormatString(dateString))
            return;
        String itemobj = dateString;
        cbxDate.removeAllItems();
        if (!isRequired()) {
            cbxDate.addItem(" ");
        }
        cbxDate.addItem(itemobj);
        cbxDate.setSelectedItem(itemobj);
    }
 
    private boolean isDateFormatString(String dateString) {
        boolean res = true;
        if (getFormat() != null) {
            if (isValidDateFormat(getFormat())) {
                SimpleDateFormat sdf = new SimpleDateFormat(getFormat());
                try {
                    sdf.parse(dateString);
                } catch (ParseException e) {
                    res = false;
                }
            } else {
                res = false;
            }
        }
        return res;
    }
 
    private boolean isValidDateFormat(String format) {
        boolean res = true;
        try {
            new SimpleDateFormat(getFormat());
        } catch (Exception e) {
            res = false;
        }
        return res;
    }
 
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        if (isRequired()) {
            drawRequiredFlag(g);
        }
    }
 
    private void drawRequiredFlag(Graphics g) {
        int x = getWidth() - 9;
        int y = 1;
        g.setColor(Color.RED);
        g.fillArc(x, y, 7, 7, 0, 360);
//        int x = getWidth() - 10;
//        int y = 10;
//        g.setColor(Color.RED);
//        g.drawString("*", x, y);
    }
 
    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        cbxDate.setEnabled(enabled);
        if (enabled) {
            cbxDate.setBackground(Color.WHITE);
        }
        btnShowCalendar.setEnabled(enabled);
    }
 
    public void popupCaneldar() {
        btnShowCalendar.doClick();
    }
 
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("ShowDateCalendar")) {
            new PopupCalendarPanel(cbxDate, isHasDate(), isHasTime(), isRequired(), itemStyle).show();// add by caill
                                                                                                        // 2016.2.19增加了ietemStyle参数用来传递附加属性的内容
        }
    }
 
    /**
     * 将日期清空
     * <p>
     * Description:
     * </p>
     * 
     * @author ligang
     * @time 2013-3-5
     * @param dateString
     */
    public void setDateString() {
        cbxDate.removeAllItems();
        cbxDate.addItem(" ");
    }
 
    public static final String DATE_TIME_VALUE = "DateTimeValue";
}