Clover coverage report - brownies library - 1.0-beta-1 Coverage timestamp: 月 8 16 2004 17:14:42 GMT+09:00 FRAMES NO FRAMES |
file stats: | LOC: | 210 |  | Methods: | 18
| NCLOC: | 123 |  | Classes: | 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
Source file | Conditionals | Statements | Methods | TOTAL
DateField.java | 0% | 0% | 0% | 0% |

1 |  |
     /*
2 |  |
      * Created on 2004/05/12
3 |  |
      */
4 |  |
     package org.asyrinx.brownie.swing;
5 |  |
6 |  |
     import java.awt.Color;
7 |  |
     import java.awt.Insets;
8 |  |
     import java.awt.Point;
9 |  |
     import java.awt.event.ActionEvent;
10 |  |
     import java.awt.event.ActionListener;
11 |  |
     import java.awt.event.ComponentAdapter;
12 |  |
     import java.awt.event.ComponentEvent;
13 |  |
     import java.text.DateFormat;
14 |  |
     import java.text.ParseException;
15 |  |
     import java.text.SimpleDateFormat;
16 |  |
     import java.util.Date;
17 |  |
18 |  |
     import javax.swing.JButton;
19 |  |
     import javax.swing.JDialog;
20 |  |
     import javax.swing.JFrame;
21 |  |
     import javax.swing.JPanel;
22 |  |
     import javax.swing.JTextField;
23 |  |
24 |  |
     import org.apache.commons.lang.StringUtils;
25 |  |
     import org.asyrinx.brownie.core.util.jp.JpDateFormat;
26 |  |
27 |  |
     public final class DateField extends JPanel {
28 |  |
29 |  |
         protected final JTextField dateText = new JTextField();
30 |  |
31 |  |
         private final JButton dropdownButton = new JButton();
32 |  |
33 | 0 |
         public DateField() {
34 | 0 |
             super();
35 | 0 |
             initialize();
36 |  |
         }
37 |  |
38 |  |
         /**
39 |  |
          * Alternate constructor that initializes the currently selected date to the
40 |  |
          * specified date.
41 |  |
          *
42 |  |
          * @param initialDate
43 |  |
          */
44 | 0 |
         public DateField(final Date initialDate) {
45 | 0 |
             super();
46 | 0 |
             initialize();
47 | 0 |
             dateText.setText(dateToString(initialDate));
48 |  |
         }
49 |  |
50 |  |
         /**
51 |  |
          * This method initializes this
52 |  |
          *
53 |  |
          * @return void
54 |  |
          */
55 | 0 |
         private void initialize() {
56 | 0 |
             this.setLayout(new javax.swing.BoxLayout(this,
57 |  |
                     javax.swing.BoxLayout.X_AXIS));
58 | 0 |
             this.add(dateText, null);
59 | 0 |
             this.add(dropdownButton, null);
60 | 0 |
             dateText.setText("");
61 | 0 |
             dateText.setEditable(true);
62 | 0 |
             dateText.setBackground(new Color(255, 255, 255));
63 | 0 |
             dropdownButton.setText("...");
64 | 0 |
             dropdownButton.setMargin(new Insets(2, 2, 2, 2));
65 | 0 |
             dropdownButton.addActionListener(new ActionListener() {
66 | 0 |
                 public void actionPerformed(final ActionEvent evt) {
67 | 0 |
                     onButtonClick(evt);
68 |  |
                 }
69 |  |
             });
70 | 0 |
             this.setSize(201, 37);
71 | 0 |
             this.setPreferredSize(new java.awt.Dimension(25, 21));
72 | 0 |
             dateText.setPreferredSize(new java.awt.Dimension(6, 18));
73 |  |
         }
74 |  |
75 |  |
         /**
76 |  |
          * Returns the currently selected date or null if not set.
77 |  |
          *
78 |  |
          * @return date
79 |  |
          */
80 | 0 |
         public Date getDate() {
81 | 0 |
             return stringToDate(dateText.getText());
82 |  |
         }
83 |  |
84 | 0 |
         public void setDate(Date date) {
85 | 0 |
             dateText.setText(dateToString(date));
86 |  |
         }
87 |  |
88 |  |
         /**
89 |  |
          * Event handler that displays the DatePicker when the button is clicked.
90 |  |
          *
91 |  |
          * @param e
92 |  |
          */
93 | 0 |
         protected void onButtonClick(final ActionEvent e) {
94 | 0 |
             final JDialog dlg = new JDialog(new JFrame(), true);
95 | 0 |
             final DatePicker dp = (StringUtils.isEmpty(dateText.getText())) ? new DatePicker()
96 |  |
                     : new DatePicker(stringToDate(dateText.getText()));
97 | 0 |
             dp.setHideOnSelect(true);
98 | 0 |
             dp.addComponentListener(new ComponentAdapter() {
99 | 0 |
                 public void componentHidden(final ComponentEvent evt) {
100 | 0 |
                     final Date dt = ((DatePicker) evt.getSource()).getDate();
101 | 0 |
                     if (null != dt)
102 | 0 |
                         dateText.setText(dateToString(dt));
103 | 0 |
                     dlg.dispose();
104 |  |
                 }
105 |  |
             });
106 | 0 |
             final Point p = dateText.getLocationOnScreen();
107 | 0 |
             p.setLocation(p.getX(), p.getY() - 1 + dateText.getSize().getHeight());
108 | 0 |
             dlg.setLocation(p);
109 | 0 |
             dlg.setResizable(false);
110 | 0 |
             dlg.setUndecorated(true);
111 | 0 |
             dlg.getContentPane().add(dp);
112 | 0 |
             dlg.pack();
113 | 0 |
             dlg.show();
114 |  |
         }
115 |  |
116 |  |
         /**
117 |  |
          * Returns a short string representation for the specified date (January 1,
118 |  |
          * 2003).
119 |  |
          *
120 |  |
          * @param dt
121 |  |
          * @return short string
122 |  |
          */
123 | 0 |
         protected String dateToString(final Date dt) {
124 | 0 |
             return (dt == null) ? null : dateFormat.format(dt);
125 |  |
         }
126 |  |
127 |  |
         /**
128 |  |
          * Constructs a Date object from a short date (January 1, 2003).
129 |  |
          *
130 |  |
          * @param s
131 |  |
          * @return date
132 |  |
          */
133 | 0 |
         private Date stringToDate(final String s) {
134 | 0 |
             if (StringUtils.isEmpty(s))
135 | 0 |
                 return null;
136 | 0 |
             try {
137 | 0 |
                 return dateFormat.parse(s);
138 |  |
             } catch (ParseException e) {
139 | 0 |
                 return null;
140 |  |
             }
141 |  |
         }
142 |  |
143 |  |
         private DateFormat dateFormat = new JpDateFormat("yyyy/MM/dd HH:mm:ss");
144 |  |
145 |  |
         /**
146 |  |
          * @return Returns the dateFormat.
147 |  |
          */
148 | 0 |
         public DateFormat getDateFormat() {
149 | 0 |
             return dateFormat;
150 |  |
         }
151 |  |
152 |  |
         /**
153 |  |
          * @param dateFormat
154 |  |
          *               The dateFormat to set.
155 |  |
          */
156 | 0 |
         public void setDateFormat(DateFormat dateFormat) {
157 | 0 |
             this.dateFormat = dateFormat;
158 |  |
         }
159 |  |
160 |  |
         /**
161 |  |
          * @return Returns the pattern.
162 |  |
          */
163 | 0 |
         public String getPattern() {
164 | 0 |
             if (dateFormat instanceof JpDateFormat)
165 | 0 |
                 return ((JpDateFormat) dateFormat).toPattern();
166 | 0 |
             else if (dateFormat instanceof SimpleDateFormat)
167 | 0 |
                 return ((SimpleDateFormat) dateFormat).toPattern();
168 | 0 |
             return null;
169 |  |
         }
170 |  |
171 |  |
         /**
172 |  |
          * @param pattern
173 |  |
          *               The pattern to set.
174 |  |
          */
175 | 0 |
         public void setPattern(String pattern) {
176 | 0 |
             if (dateFormat instanceof SimpleDateFormat)
177 | 0 |
                 dateFormat = new SimpleDateFormat(pattern);
178 |  |
             else
179 | 0 |
                 dateFormat = new JpDateFormat(pattern);
180 |  |
         }
181 |  |
182 |  |
         /**
183 |  |
          * @return
184 |  |
          */
185 | 0 |
         public String getText() {
186 | 0 |
             return dateText.getText();
187 |  |
         }
188 |  |
189 |  |
         /**
190 |  |
          * @return
191 |  |
          */
192 | 0 |
         public boolean isEditable() {
193 | 0 |
             return dateText.isEditable();
194 |  |
         }
195 |  |
196 |  |
         /**
197 |  |
          * @param b
198 |  |
          */
199 | 0 |
         public void setEditable(boolean b) {
200 | 0 |
             dateText.setEditable(b);
201 | 0 |
             dropdownButton.setEnabled(b);
202 |  |
         }
203 |  |
204 |  |
         /**
205 |  |
          * @param t
206 |  |
          */
207 | 0 |
         public void setText(String t) {
208 | 0 |
             dateText.setText(t);
209 |  |
         }
210 |  |
     }