KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > designer > model > DateInput


1 /*
2  * (c) Rob Gordon 2005
3  */

4 package org.oddjob.designer.model;
5
6 import java.text.ParseException JavaDoc;
7 import java.util.Date JavaDoc;
8 import java.util.Observable JavaDoc;
9 import java.util.Observer JavaDoc;
10
11 import org.oddjob.util.DateHelper;
12
13
14 /**
15  * A model for a visual component is a file name that can be populated via a
16  * file selection.
17  */

18 public class DateInput extends Observable JavaDoc
19 implements DesignDefinition, FormDefinition {
20
21     private final String JavaDoc heading;
22     private final SimpleAttribute designElement;
23     
24     public DateInput(String JavaDoc heading, SimpleAttribute de) {
25         this.heading = heading;
26         this.designElement = de;
27         designElement.addObserver(new Observer JavaDoc() {
28             public void update(Observable JavaDoc o, Object JavaDoc arg) {
29                 setChanged();
30                 notifyObservers();
31             }
32         });
33     }
34
35     public String JavaDoc getTitle() {
36         return heading;
37     }
38
39     /**
40      * Used by the view to set text value of the field.
41      *
42      * @param date The date.
43      */

44     public void setDate(Date JavaDoc date) {
45         if (date == null) {
46             designElement.attribute(null);
47         }
48         else {
49             designElement.attribute(DateHelper.formatDate(date));
50         }
51     }
52
53     /**
54      * Used by the view to get the text value for the field.
55      *
56      * @return The date.
57      */

58     public Date JavaDoc getDate() {
59         if (designElement.attribute() == null) {
60             return null;
61         }
62         try {
63             return DateHelper.parseDate(designElement.attribute());
64         } catch (ParseException JavaDoc e) {
65             return null;
66         }
67     }
68     
69     /*
70      * (non-Javadoc)
71      * @see org.oddjob.designer.model.DesignDefinition#accept(org.oddjob.designer.model.DesignProcessor)
72      */

73     public void accept(DesignProcessor processor) {
74         processor.onDateInput(this);
75     }
76     
77     /* (non-Javadoc)
78      * @see org.oddjob.designer.model.DesignDefinition#isPopulated()
79      */

80     public boolean isPopulated() {
81         return designElement.attribute() != null;
82     }
83 }
84
Popular Tags