Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.impl / src / main / java / org / gvsig / tools / swing / impl / usability / spinner / editor / DynFieldFormatter.java @ 728

History | View | Annotate | Download (13.3 KB)

1
package org.gvsig.tools.swing.impl.usability.spinner.editor;
2

    
3
import java.text.DateFormat;
4
import java.text.DecimalFormat;
5
import java.text.Format;
6
import java.text.NumberFormat;
7
import java.text.ParseException;
8
import java.text.SimpleDateFormat;
9
import java.util.Calendar;
10
import java.util.Date;
11
import java.util.Locale;
12
import java.sql.Timestamp;
13

    
14
import org.gvsig.tools.dataTypes.DataTypes;
15
import org.gvsig.tools.dynobject.DynField;
16

    
17
public class DynFieldFormatter {
18

    
19
    public class DateFormatter extends SimpleDateFormat {
20

    
21
        /**
22
         * 
23
         */
24
        private static final long serialVersionUID = -1744865595768091192L;
25
        private DynFieldFormatter df;
26

    
27
        public DateFormatter(DynFieldFormatter df) {
28
            this.df = df;
29
        }
30

    
31
        public String formatDate(Object obj) throws ParseException {
32
            return df.format(obj);
33
        }
34

    
35
        @Override
36
        public Date parse(String value) {
37
            try {
38
                return parseDate(value);
39
            } catch (ParseException e) {
40
                return null;
41
            }
42
        }
43

    
44
        public Date parseDate(String time) throws ParseException {
45
            return (Date) df.parse(time);
46
        }
47
    }
48

    
49
    public static DynFieldFormatter getInstance(DynField field, Locale loc) {
50
        return new DynFieldFormatter(field, loc);
51
    }
52

    
53
    private Locale loc;
54
    private int calendarField = Calendar.DAY_OF_MONTH;
55
    private SimpleDateFormat df;
56
    private DynField dynField;
57
    private boolean isDate = false;
58

    
59
    private boolean isNumber = false;
60

    
61
    private DecimalFormat f;
62

    
63
    private DynFieldFormatter(DynField field, Locale loc) {
64
        super();
65
        this.loc = loc;
66
        this.dynField = field;
67
        refresh();
68
    }
69

    
70
    /**
71
     * @param value
72
     * @return
73
     */
74
    public String format(Object value) {
75
        System.out.println(value);
76
        refresh();
77
        if ((value == null) || (value.equals(""))) {
78
            return "";
79
        }
80

    
81
        if (isDate()) {
82
            if (value instanceof Date) {
83
                return this.df.format(value);
84
            } else {
85
                return "0";
86
            }
87

    
88
        }
89
        if (isNumber()) {
90
            if (value instanceof Number) {
91
                return this.f.format(value);
92
            }
93
        }
94
        return "";
95
    }
96

    
97
    public Date formatDate(Date date) {
98
        return this.getCalendar(date).getTime();
99
    }
100

    
101
    /**
102
     * Output example for FULL date format
103
     * For the United States: Thursday, September 23, 2010
104
     * For France: jeudi 23 septembre 2010
105
     * For Spain: jueves 23 de septiembre de 2010
106
     */
107
    public String formatDateFull(Date date) {
108
        DateFormat df =
109
            DateFormat.getDateInstance(DateFormat.FULL, getLocale());
110
        return df.format(date);
111
    }
112

    
113
    /**
114
     * Output example for LONG date format
115
     * For the United Kingdom: 23 September 2010
116
     * For France: 23 septembre 2010
117
     * For Spain: 23 de septiembre de 2010
118
     */
119
    public String formatDateLong(Date date) {
120
        DateFormat df =
121
            DateFormat.getDateInstance(DateFormat.LONG, getLocale());
122
        return df.format(date);
123
    }
124

    
125
    /**
126
     * Output example for MEDIUM date format
127
     * For Korea: 2010. 9. 23
128
     * For Spain: 23-sep-2010
129
     */
130
    public String formatDateMedium(Date date) {
131
        DateFormat df =
132
            DateFormat.getDateInstance(DateFormat.MEDIUM, getLocale());
133
        return df.format(date);
134
    }
135

    
136
    /**
137
     * Output example for SHORT date format
138
     * For Japan: 10/09/23 (YY/MM/DD)
139
     * For Spain: 23/09/10 (DD/MM/YY)
140
     */
141
    public String formatDateShort(Date date) {
142
        DateFormat df =
143
            DateFormat.getDateInstance(DateFormat.SHORT, getLocale());
144
        return df.format(date);
145
    }
146

    
147
    /**
148
     * @param i
149
     * @return
150
     */
151
    private Number formatNumber(Number value) {
152
        try {
153
            return this.f.parse(f.format(value));
154
        } catch (ParseException e) {
155
            return null;
156
        }
157
    }
158

    
159
    public Calendar getCalendar() {
160
        return this.getDateFormatter().getCalendar();
161
    }
162

    
163
    public Calendar getCalendar(Date date) {
164
        if (date == null) {
165
            date = new Date();
166
        }
167
        getCalendar().setTime(date);
168
        return getCalendar();
169
    }
170

    
171
    public int getCalendarField() {
172
        return this.calendarField;
173
    }
174

    
175
    public DateFormatter getDateFormatter() {
176
        if (this.df == null) {
177
            this.df =
178
                (SimpleDateFormat) DateFormat.getDateInstance(
179
                    DateFormat.MEDIUM, getLocale());
180
        }
181
        return new DateFormatter(this);
182
    }
183

    
184
    public Locale getLocale() {
185
        if (this.loc == null) {
186
            this.loc = Locale.getDefault();
187
        }
188
        return this.loc;
189
    }
190

    
191
    /**
192
     * @param value
193
     * @return
194
     */
195
    public Object getNextValue(Object value) {
196
        if (isDate()) {
197
            if (value == null) {
198
                value = new Date();
199
            }
200
            Calendar cal = getCalendar();
201
            cal.setTime((Date) value);
202
            cal.add(getCalendarField(), 1);
203
            return cal.getTime();
204
        }
205
        if (isNumber()) {
206
            if (value != null) {
207
                String strValue = value.toString();
208
                switch (getType()) {
209
                case DataTypes.DOUBLE:
210
                    return formatNumber(Double.valueOf(strValue) + 1);
211
                case DataTypes.FLOAT:
212
                    return formatNumber(Float.valueOf(strValue) + 1);
213
                case DataTypes.LONG:
214
                    return formatNumber(Long.valueOf(strValue) + 1);
215
                case DataTypes.INT:
216
                    return formatNumber(Integer.valueOf(strValue) + 1);
217
                case DataTypes.BYTE:
218
                    return formatNumber(Byte.valueOf(strValue) + 1);
219
                }
220
            }
221
            return 0;
222
        }
223
        return null;
224
    }
225

    
226
    public NumberFormat getNumberFormatter() {
227
        if (this.f == null) {
228
            if (this.df == null) {
229
                this.f =
230
                    (DecimalFormat) NumberFormat.getNumberInstance(getLocale());
231
            } else {
232
                return this.df.getNumberFormat();
233
            }
234
        }
235
        return this.f;
236
    }
237

    
238
    /**
239
     * @param value
240
     * @return
241
     */
242
    public Object getPreviousValue(Object value) {
243
        if (isDate()) {
244
            if (value != null) {
245
                Calendar cal = getCalendar();
246
                cal.setTime((Date) value);
247
                cal.add(getCalendarField(), -1);
248
                return cal.getTime();
249
            }
250
            return null;
251
        }
252
        if (isNumber()) {
253
            if (value != null) {
254
                String strValue = value.toString();
255
                switch (getType()) {
256
                case DataTypes.DOUBLE:
257
                    return formatNumber(Double.valueOf(strValue) - 1);
258
                case DataTypes.FLOAT:
259
                    return formatNumber(Float.valueOf(strValue) - 1);
260
                case DataTypes.LONG:
261
                    return formatNumber(Long.valueOf(strValue) - 1);
262
                case DataTypes.INT:
263
                    return formatNumber(Integer.valueOf(strValue) - 1);
264
                case DataTypes.BYTE:
265
                    return formatNumber(Byte.valueOf(strValue) - 1);
266
                }
267
            }
268
            return null;
269
        }
270
        return null;
271
    }
272

    
273
    private String getSubType() {
274
        String str = dynField.getSubtype();
275
        if (str == null) {
276
            return "";
277
        }
278
        return str;
279
    }
280

    
281
    private int getType() {
282
        return dynField.getType();
283
    }
284

    
285
    public boolean isDate() {
286
        return this.isDate;
287
    }
288

    
289
    /**
290
     * @return
291
     */
292
    public boolean isNumber() {
293
        return this.isNumber;
294
    }
295

    
296
    /**
297
     * @param value
298
     */
299
    public boolean isValidType(Object value) {
300
        if (value == null) {
301
            return true;
302
        }
303
        if ((isDate()) && (!(value instanceof Date))) {
304
            return false;
305
        }
306
        if ((isNumber()) && (!(value instanceof Number))) {
307
            if (value instanceof String) {
308
                try {
309
                    Number n = NumberFormat.getInstance().parse((String) value);
310
                } catch (ParseException e) {
311
                    return false;
312
                }
313
            }
314
            return false;
315
        }
316
        return true;
317
    }
318

    
319
    //
320
    // public String formatByType(int dynType, Date date) {
321
    // return setDynTypeFormatParametters(dynType).format(date);
322
    // }
323
    //
324
    // public String formatByType(int dynType, Date date, int style) {
325
    // this.setDynTypeFormatParametters(dynType);
326
    // this.getDateFormatter().getCalendar();
327
    // return format(date);
328
    // }
329

    
330
    /**
331
     * @param obj
332
     * @return
333
     * @throws ParseException
334
     */
335
    public Object parse(String value) throws ParseException {
336
                if ((value == null) || (value.equals(""))) {
337
                        return null;
338
                }
339

    
340
                if (isDate()) {
341
                    if (getType() == DataTypes.TIMESTAMP) {
342
                        /*
343
                         * Parse as timestamp
344
                         */
345
                        return Timestamp.valueOf(value);
346

    
347
                    } else {
348
                        return this.df.parse(value);
349
                    }
350
        }
351
        if (isNumber()) {
352
            return this.f.parse(value);
353
        }
354
        return null;
355
    }
356

    
357
    public Date parseDate(String date) throws ParseException {
358
        return this.getDateFormatter().parse(date);
359
    }
360

    
361
    /**
362
         * 
363
         */
364
    private void refresh() {
365
        switch (getType()) {
366
        case DataTypes.TIME:
367
            this.isDate = true;
368
            this.calendarField = Calendar.MINUTE;
369
            this.df =
370
                (SimpleDateFormat) DateFormat.getTimeInstance(
371
                    DateFormat.MEDIUM, getLocale());
372
            break;
373
        case DataTypes.TIMESTAMP:
374
            this.isDate = true;
375
            this.calendarField = Calendar.SECOND;
376
            /*
377
             * This 'df' will not be used because Timestamp
378
             * is parsed in another way
379
             */
380
            this.df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.f");
381
            
382
            break;
383
        case DataTypes.DATE:
384
            this.isDate = true;
385
            this.calendarField = Calendar.DAY_OF_MONTH;
386
            if (!getSubType().equals(DataTypes.SUBTYPE_DATE)) {
387
                this.df =
388
                    (SimpleDateFormat) DateFormat.getDateTimeInstance(
389
                        DateFormat.FULL, DateFormat.FULL, getLocale());
390
            } else {
391
                this.df =
392
                    (SimpleDateFormat) DateFormat.getDateInstance(
393
                        DateFormat.LONG, getLocale());
394
            }
395
            break;
396
        case DataTypes.DOUBLE:
397
            isNumber = true;
398
            f = (DecimalFormat) NumberFormat.getNumberInstance(getLocale());
399
            f.setMaximumFractionDigits(2);
400
            f.setMinimumFractionDigits(2);
401
            break;
402
        case DataTypes.FLOAT:
403
            isNumber = true;
404
            f = (DecimalFormat) NumberFormat.getNumberInstance(getLocale());
405
            f.setMaximumFractionDigits(2);
406
            f.setMinimumFractionDigits(2);
407
            break;
408
        case DataTypes.LONG:
409
            isNumber = true;
410
            f = (DecimalFormat) NumberFormat.getNumberInstance(getLocale());
411
            break;
412
        case DataTypes.INT:
413
            isNumber = true;
414
            f = (DecimalFormat) NumberFormat.getIntegerInstance(getLocale());
415
            break;
416
        case DataTypes.BYTE:
417
            isNumber = true;
418
            f = (DecimalFormat) NumberFormat.getIntegerInstance(getLocale());
419
            break;
420
        }
421
    }
422

    
423
    public void setCalendarField(int calendarField) {
424
        this.calendarField = calendarField;
425
    }
426

    
427
    public void setLocale(Locale loc) {
428
        this.loc = loc;
429
        refresh();
430
    }
431

    
432
    private SimpleDateFormat toDateFormat() {
433
        return df;
434
    }
435

    
436
    public Format toFormat() {
437
        if (isDate()) {
438
            return df;
439
        } else
440
            if (isNumber()) {
441
                return f;
442
            }
443
        return null;
444
    }
445

    
446
    public String toLocalizedPattern() {
447
        if (isDate()) {
448
            return this.df.toLocalizedPattern();
449
        } else {
450
            return this.f.toLocalizedPattern();
451
        }
452
    }
453

    
454
    // /**
455
    // * @param textField
456
    // * @param value
457
    // * @return
458
    // */
459
    // public void setFormattedValue(JFormattedTextField textField, Object
460
    // value) {
461
    // String text = null;
462
    // if (textField == null) return;
463
    // if ((value==null)||value.equals("")){
464
    // text = "";
465
    // }else if (isDate()){
466
    // text = df.format(value);
467
    // }else if(isNumber()){
468
    // text = f.format(value);
469
    // }
470
    // textField.setText(text);
471
    // textField.validate();
472
    //                
473
    // }
474

    
475
    // /**
476
    // * @param time
477
    // * @return
478
    // */
479
    // private Date parseDate(String time) {
480
    // try {
481
    // return this.df.parse(this.df.format(time));
482
    // } catch (ParseException e) {
483
    // return null;
484
    // }
485
    // }
486
}