Revision 298 org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.swing/org.gvsig.tools.swing.serv/org.gvsig.tools.swing.serv.field/src/main/java/org/gvsig/tools/swing/serv/field/component/spinner/DynFieldFormatter.java

View differences:

DynFieldFormatter.java
10 10
import java.util.Date;
11 11
import java.util.Locale;
12 12

  
13
import javax.swing.JFormattedTextField;
14

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

  
18 16
public class DynFieldFormatter {
19 17

  
18
    public class DateFormatter extends SimpleDateFormat {
19

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

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

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

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

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

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

  
20 52
    private Locale loc;
21 53
    private int calendarField = Calendar.DAY_OF_MONTH;
22 54
    private SimpleDateFormat df;
23
	private DynField dynField;
24
	private boolean isDate = false;
25
	private boolean isNumber = false;
26
	private DecimalFormat f;
55
    private DynField dynField;
56
    private boolean isDate = false;
27 57

  
28
	public static DynFieldFormatter getInstance(DynField field, Locale loc){
29
		return new DynFieldFormatter(field, loc);
30
	}
31
	
58
    private boolean isNumber = false;
59

  
60
    private DecimalFormat f;
61

  
32 62
    private DynFieldFormatter(DynField field, Locale loc) {
33
    	super();
63
        super();
34 64
        this.loc = loc;
35 65
        this.dynField = field;
36 66
        refresh();
37 67
    }
38
    
39
    public Format toFormat(){
40
        if (isDate())
41
            return df;
42
        else if (isNumber()){
43
            return f;
44
        }
45
        return null;
46
    }
47
    
48
    private SimpleDateFormat toDateFormat(){
49
        return df;
50
    }
51
    
52
    public String toLocalizedPattern(){
53
        if (isDate()){
54
            return this.df.toLocalizedPattern();
55
        }else{
56
            return this.f.toLocalizedPattern();
57
        }
58
    }
59
    
60
    public class DateFormatter extends SimpleDateFormat{
61
    	
62
    	private DynFieldFormatter df;
63 68

  
64
		public DateFormatter(DynFieldFormatter df){
65
    		this.df = df;
66
    	}
67
		
68
		
69
    	
70
    	public String formatDate(Object obj)throws ParseException{
71
    		return df.format(obj);
72
    	}
73
    	
74
    	public Date parseDate(String time) throws ParseException{
75
    		return (Date) df.parse(time);
76
    	}
77
    	
78
    	@Override
79
    	public Date parse(String value){
80
    		try {
81
				return parseDate(value);
82
			} catch (ParseException e) {
83
				return null;
84
			}
85
    	}
86
    }
87
    
88 69
    /**
89
	 * 
90
	 */
91
	private void refresh() {
92
	        switch (getType()) {
93
	        case DataTypes.TIME:
94
	        	this.isDate = true;
95
	            this.calendarField = Calendar.MINUTE;
96
	            this.df =
97
	                (SimpleDateFormat) DateFormat.getTimeInstance(DateFormat.MEDIUM, getLocale());
98
	            break;
99
	        case DataTypes.TIMESTAMP:
100
	        	this.isDate = true;
101
	            this.calendarField = Calendar.SECOND;
102
	            this.df = (SimpleDateFormat) DateFormat.getTimeInstance(DateFormat.MEDIUM, getLocale());
103
	            break;
104
	        case DataTypes.DATE:
105
	        	this.isDate = true;
106
	            this.calendarField = Calendar.DAY_OF_MONTH;
107
	            if (!getSubType().equals(DataTypes.SUBTYPE_DATE)){
108
	                this.df = (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, getLocale());
109
	            }else{
110
	                this.df = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.LONG, getLocale());     
111
	            }
112
	            break;
113
	        case DataTypes.DOUBLE:
114
	        	isNumber = true;
115
	            f = (DecimalFormat) DecimalFormat.getNumberInstance(getLocale());
116
	            f.setMaximumFractionDigits(2);
117
                f.setMinimumFractionDigits(2);
118
	            break;
119
	        case DataTypes.FLOAT:
120
	        	isNumber = true;
121
	            f = (DecimalFormat) DecimalFormat.getNumberInstance(getLocale());
122
	            f.setMaximumFractionDigits(2);
123
                f.setMinimumFractionDigits(2);
124
	            break;
125
	        case DataTypes.LONG:
126
	        	isNumber = true;
127
	            f = (DecimalFormat) DecimalFormat.getNumberInstance(getLocale());
128
	            break;
129
	        case DataTypes.INT:
130
	        	isNumber = true;
131
	            f = (DecimalFormat) DecimalFormat.getIntegerInstance(getLocale());
132
	            break;
133
	        case DataTypes.BYTE:
134
	        	isNumber = true;
135
	            f = (DecimalFormat) DecimalFormat.getIntegerInstance(getLocale());
136
	            break;
137
	        }
138
	}
70
     * @param value
71
     * @return
72
     */
73
    public String format(Object value) {
74
        System.out.println(value);
75
        refresh();
76
        if ((value == null) || (value.equals(""))) {
77
            return "";
78
        }
139 79

  
140
	/**
141
	 * @param value
142
	 * @return
143
	 */
144
	public String format(Object value) {
145
	    System.out.println(value);
146
	    refresh();
147
		if ((value==null)||(value.equals("")))
148
			return "";
149
		
150
		if (isDate()){
151
		    if (value instanceof Date)
152
		        return this.df.format(value);
153
		    else 
154
		        return "0";
155
		  
156
		}
157
		if (isNumber()){
158
		    if (value instanceof Number)
159
		        return this.f.format(value);
160
		}
161
		return "";
162
	}
163
	
164
	/**
165
	 * @param obj
166
	 * @return
167
	 * @throws ParseException 
168
	 */
169
	public Object parse(String value) throws ParseException {
170
		if (isDate()){
171
			return this.df.parse(value);			
172
		}
173
		if (isNumber()){
174
			return this.f.parse(value);
175
		}
176
		return null;
177
	}
80
        if (isDate()) {
81
            if (value instanceof Date) {
82
                return this.df.format(value);
83
            } else {
84
                return "0";
85
            }
178 86

  
179
	private int getType(){
180
    	return dynField.getType();
87
        }
88
        if (isNumber()) {
89
            if (value instanceof Number) {
90
                return this.f.format(value);
91
            }
92
        }
93
        return "";
181 94
    }
182
    
183
    private String getSubType(){
184
    	String str = dynField.getSubtype();
185
    	if (str==null)
186
    		return "";
187
    	return str;
188
    }
189 95

  
190
    public void setLocale(Locale loc) {
191
        this.loc = loc;
192
        refresh();
96
    public Date formatDate(Date date) {
97
        return this.getCalendar(date).getTime();
193 98
    }
194 99

  
195
  
196
    public DateFormatter getDateFormatter() {
197
        if (this.df == null)
198
            this.df = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.MEDIUM, getLocale());
199
        return new DateFormatter(this);
200
    }
201
    
202
    public NumberFormat getNumberFormatter() {
203
        if (this.f == null)
204
        	if (this.df==null)
205
        		this.f = (DecimalFormat) DecimalFormat.getNumberInstance(getLocale());
206
        	else
207
        		return (DecimalFormat) this.df.getNumberFormat();
208
        return this.f;
209
    }
210

  
211
    public Locale getLocale() {
212
        if (this.loc == null)
213
            this.loc = Locale.getDefault();
214
        return this.loc;
215
    }
216

  
217 100
    /**
218 101
     * Output example for FULL date format
219 102
     * For the United States: Thursday, September 23, 2010
......
226 109
        return df.format(date);
227 110
    }
228 111

  
229
    public Date parseDate(String date) throws ParseException {
230
    	return this.getDateFormatter().parse(date);
231
    }
232

  
233
    public Date formatDate(Date date) {
234
        return this.getCalendar(date).getTime();
235
    }
236

  
237 112
    /**
238 113
     * Output example for LONG date format
239 114
     * For the United Kingdom: 23 September 2010
......
268 143
        return df.format(date);
269 144
    }
270 145

  
271
    public Calendar getCalendar(Date date) {
272
        if (date == null)
273
            date = new Date();
274
        getCalendar().setTime(date);
275
        return getCalendar();
146
    /**
147
     * @param i
148
     * @return
149
     */
150
    private Number formatNumber(Number value) {
151
        try {
152
            return this.f.parse(f.format(value));
153
        } catch (ParseException e) {
154
            return null;
155
        }
276 156
    }
277 157

  
278 158
    public Calendar getCalendar() {
279 159
        return this.getDateFormatter().getCalendar();
280 160
    }
281
//
282
//    public String formatByType(int dynType, Date date) {
283
//        return setDynTypeFormatParametters(dynType).format(date);
284
//    }
285
//
286
//    public String formatByType(int dynType, Date date, int style) {
287
//        this.setDynTypeFormatParametters(dynType);
288
//        this.getDateFormatter().getCalendar();
289
//        return format(date);
290
//    }
291 161

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

  
293 170
    public int getCalendarField() {
294 171
        return this.calendarField;
295 172
    }
296 173

  
297
    public void setCalendarField(int calendarField) {
298
        this.calendarField = calendarField;
174
    public DateFormatter getDateFormatter() {
175
        if (this.df == null) {
176
            this.df =
177
                (SimpleDateFormat) DateFormat.getDateInstance(
178
                    DateFormat.MEDIUM, getLocale());
179
        }
180
        return new DateFormatter(this);
299 181
    }
300 182

  
301
    public boolean isDate(){
302
    	return this.isDate;
183
    public Locale getLocale() {
184
        if (this.loc == null) {
185
            this.loc = Locale.getDefault();
186
        }
187
        return this.loc;
303 188
    }
304
	/**
305
	 * @param value
306
	 * @return
307
	 */
308
	public Object getNextValue(Object value) {
309
		if (isDate()) {
310
			if (value == null)
311
				value = new Date();
312
			Calendar cal = getCalendar();
313
			cal.setTime((Date) value);
314
			cal.add(getCalendarField(), 1);
315
			return cal.getTime();
316
		}
317
		if (isNumber()) {
318
			if (value != null) {
319
				String strValue = value.toString();
320
				switch (getType()) {
321
				case DataTypes.DOUBLE:
322
					return formatNumber(Double.valueOf(strValue) + 1);
323
				case DataTypes.FLOAT:
324
					return formatNumber(Float.valueOf(strValue) + 1);
325
				case DataTypes.LONG:
326
					return formatNumber(Long.valueOf(strValue) + 1);
327
				case DataTypes.INT:
328
					return formatNumber(Integer.valueOf(strValue) + 1);
329
				case DataTypes.BYTE:
330
					return formatNumber(Byte.valueOf(strValue) + 1);
331
				}
332
			}
333
			return 0;
334
		}
335
		return null;
336
	}
337 189

  
338
	/**
339
	 * @param value
340
	 * @return
341
	 */
342
	public Object getPreviousValue(Object value) {
343
		if (isDate()){
344
			if (value != null) {
345
				 Calendar cal = getCalendar();
346
				 cal.setTime((Date) value);
347
				 cal.add(getCalendarField(), -1);
348
			     return cal.getTime();
349
			}
350
			return null;
351
		}
352
		if (isNumber()){
353
			if (value!=null){
354
				String strValue = value.toString();
355
				switch (getType()){
356
				  case DataTypes.DOUBLE:
357
			        	return formatNumber(Double.valueOf(strValue)-1);
358
			        case DataTypes.FLOAT:
359
			        	return formatNumber(Float.valueOf(strValue)-1);
360
			        case DataTypes.LONG:
361
			        	return formatNumber(Long.valueOf(strValue)-1);
362
			        case DataTypes.INT:
363
			        	return formatNumber(Integer.valueOf(strValue)-1);
364
			        case DataTypes.BYTE:
365
			        	return formatNumber(Byte.valueOf(strValue)-1);
366
				}
367
			}
368
			return null;
369
		}
370
		return null;
371
	}
190
    /**
191
     * @param value
192
     * @return
193
     */
194
    public Object getNextValue(Object value) {
195
        if (isDate()) {
196
            if (value == null) {
197
                value = new Date();
198
            }
199
            Calendar cal = getCalendar();
200
            cal.setTime((Date) value);
201
            cal.add(getCalendarField(), 1);
202
            return cal.getTime();
203
        }
204
        if (isNumber()) {
205
            if (value != null) {
206
                String strValue = value.toString();
207
                switch (getType()) {
208
                case DataTypes.DOUBLE:
209
                    return formatNumber(Double.valueOf(strValue) + 1);
210
                case DataTypes.FLOAT:
211
                    return formatNumber(Float.valueOf(strValue) + 1);
212
                case DataTypes.LONG:
213
                    return formatNumber(Long.valueOf(strValue) + 1);
214
                case DataTypes.INT:
215
                    return formatNumber(Integer.valueOf(strValue) + 1);
216
                case DataTypes.BYTE:
217
                    return formatNumber(Byte.valueOf(strValue) + 1);
218
                }
219
            }
220
            return 0;
221
        }
222
        return null;
223
    }
372 224

  
373
	/**
374
	 * @param i
375
	 * @return
376
	 */
377
	private Number formatNumber(Number value) {
378
		try {
379
			return this.f.parse(f.format(value));
380
		} catch (ParseException e) {
381
			return null;
382
		}
383
	}
225
    public NumberFormat getNumberFormatter() {
226
        if (this.f == null) {
227
            if (this.df == null) {
228
                this.f =
229
                    (DecimalFormat) NumberFormat.getNumberInstance(getLocale());
230
            } else {
231
                return this.df.getNumberFormat();
232
            }
233
        }
234
        return this.f;
235
    }
384 236

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

  
392
	/**
393
	 * @param value
272
    private String getSubType() {
273
        String str = dynField.getSubtype();
274
        if (str == null) {
275
            return "";
276
        }
277
        return str;
278
    }
279

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

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

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

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

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

  
329
    /**
330
     * @param obj
331
     * @return
332
     * @throws ParseException
333
     */
334
    public Object parse(String value) throws ParseException {
335
        if (isDate()) {
336
            return this.df.parse(value);
337
        }
338
        if (isNumber()) {
339
            return this.f.parse(value);
340
        }
341
        return null;
342
    }
343

  
344
    public Date parseDate(String date) throws ParseException {
345
        return this.getDateFormatter().parse(date);
346
    }
347

  
348
    /**
349
	 * 
394 350
	 */
395
	public boolean isValidType(Object value) {
396
		if (value==null){
397
			return true;		
398
		}
399
		if ((isDate())&&(!(value instanceof Date))) {
400
           return false;
351
    private void refresh() {
352
        switch (getType()) {
353
        case DataTypes.TIME:
354
            this.isDate = true;
355
            this.calendarField = Calendar.MINUTE;
356
            this.df =
357
                (SimpleDateFormat) DateFormat.getTimeInstance(
358
                    DateFormat.MEDIUM, getLocale());
359
            break;
360
        case DataTypes.TIMESTAMP:
361
            this.isDate = true;
362
            this.calendarField = Calendar.SECOND;
363
            this.df =
364
                (SimpleDateFormat) DateFormat.getTimeInstance(
365
                    DateFormat.MEDIUM, getLocale());
366
            break;
367
        case DataTypes.DATE:
368
            this.isDate = true;
369
            this.calendarField = Calendar.DAY_OF_MONTH;
370
            if (!getSubType().equals(DataTypes.SUBTYPE_DATE)) {
371
                this.df =
372
                    (SimpleDateFormat) DateFormat.getDateTimeInstance(
373
                        DateFormat.FULL, DateFormat.FULL, getLocale());
374
            } else {
375
                this.df =
376
                    (SimpleDateFormat) DateFormat.getDateInstance(
377
                        DateFormat.LONG, getLocale());
378
            }
379
            break;
380
        case DataTypes.DOUBLE:
381
            isNumber = true;
382
            f = (DecimalFormat) NumberFormat.getNumberInstance(getLocale());
383
            f.setMaximumFractionDigits(2);
384
            f.setMinimumFractionDigits(2);
385
            break;
386
        case DataTypes.FLOAT:
387
            isNumber = true;
388
            f = (DecimalFormat) NumberFormat.getNumberInstance(getLocale());
389
            f.setMaximumFractionDigits(2);
390
            f.setMinimumFractionDigits(2);
391
            break;
392
        case DataTypes.LONG:
393
            isNumber = true;
394
            f = (DecimalFormat) NumberFormat.getNumberInstance(getLocale());
395
            break;
396
        case DataTypes.INT:
397
            isNumber = true;
398
            f = (DecimalFormat) NumberFormat.getIntegerInstance(getLocale());
399
            break;
400
        case DataTypes.BYTE:
401
            isNumber = true;
402
            f = (DecimalFormat) NumberFormat.getIntegerInstance(getLocale());
403
            break;
401 404
        }
402
		if ((isNumber())&&(!(value instanceof Number))) {
403
			if (value instanceof String){
404
				try {
405
					Number n  = NumberFormat.getInstance().parse((String) value);
406
				} catch (ParseException e) {
407
					return false;
408
				}
409
			}
410
           return false;
405
    }
406

  
407
    public void setCalendarField(int calendarField) {
408
        this.calendarField = calendarField;
409
    }
410

  
411
    public void setLocale(Locale loc) {
412
        this.loc = loc;
413
        refresh();
414
    }
415

  
416
    private SimpleDateFormat toDateFormat() {
417
        return df;
418
    }
419

  
420
    public Format toFormat() {
421
        if (isDate()) {
422
            return df;
423
        } else
424
            if (isNumber()) {
425
                return f;
426
            }
427
        return null;
428
    }
429

  
430
    public String toLocalizedPattern() {
431
        if (isDate()) {
432
            return this.df.toLocalizedPattern();
433
        } else {
434
            return this.f.toLocalizedPattern();
411 435
        }
412
		return true;
413
	}
436
    }
414 437

  
415
//	/**
416
//	 * @param textField
417
//	 * @param value
418
//	 * @return
419
//	 */
420
//	public void setFormattedValue(JFormattedTextField textField, Object value) {
421
//		String text = null;
422
//		if (textField == null) return;
423
//		if ((value==null)||value.equals("")){
424
//			text = "";
425
//		}else if (isDate()){
426
//			text = df.format(value);
427
//		}else if(isNumber()){
428
//			text = f.format(value);
429
//		}
430
//		textField.setText(text);
431
//		textField.validate();
432
//		
433
//	}
438
    // /**
439
    // * @param textField
440
    // * @param value
441
    // * @return
442
    // */
443
    // public void setFormattedValue(JFormattedTextField textField, Object
444
    // value) {
445
    // String text = null;
446
    // if (textField == null) return;
447
    // if ((value==null)||value.equals("")){
448
    // text = "";
449
    // }else if (isDate()){
450
    // text = df.format(value);
451
    // }else if(isNumber()){
452
    // text = f.format(value);
453
    // }
454
    // textField.setText(text);
455
    // textField.validate();
456
    //		
457
    // }
434 458

  
435
//	/**
436
//	 * @param time
437
//	 * @return
438
//	 */
439
//	private Date parseDate(String time) {
440
//		try {
441
//			return this.df.parse(this.df.format(time));
442
//		} catch (ParseException e) {
443
//			return null;
444
//		}
445
//	}
459
    // /**
460
    // * @param time
461
    // * @return
462
    // */
463
    // private Date parseDate(String time) {
464
    // try {
465
    // return this.df.parse(this.df.format(time));
466
    // } catch (ParseException e) {
467
    // return null;
468
    // }
469
    // }
446 470
}
447

  

Also available in: Unified diff