1 18 19 package org.apache.jmeter.gui.util; 20 21 import java.awt.event.FocusEvent ; 22 import java.awt.event.FocusListener ; 23 import java.awt.event.KeyAdapter ; 24 import java.awt.event.KeyEvent ; 25 import java.text.DateFormat ; 26 import java.text.ParseException ; 27 import java.text.SimpleDateFormat ; 28 import java.util.Calendar ; 29 import java.util.Date ; 30 31 import javax.swing.JTextField ; 32 33 45 public class JDateField extends JTextField 46 { 47 private final static DateFormat dateFormat = 48 new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss"); 49 50 61 private static int fieldPositions [] = { 62 Calendar.YEAR, Calendar.YEAR, Calendar.YEAR, Calendar.YEAR, Calendar.YEAR, Calendar.MONTH, Calendar.MONTH, Calendar.MONTH, Calendar.DAY_OF_MONTH, Calendar.DAY_OF_MONTH, Calendar.DAY_OF_MONTH, Calendar.HOUR_OF_DAY, Calendar.HOUR_OF_DAY, Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.MINUTE, Calendar.MINUTE, Calendar.SECOND, Calendar.SECOND, Calendar.SECOND }; 83 84 87 public JDateField(Date date) 88 { 89 super(20); 90 this.addKeyListener(new KeyFocus()); 91 this.addFocusListener(new FocusClass()); 92 String myString = dateFormat.format(date); 93 setText(myString); 94 } 95 96 public JDateField() 98 { 99 this(new Date ()); 100 } 101 102 105 public void setDate(Date date) 106 { 107 setText(dateFormat.format(date)); 108 } 109 110 113 public Date getDate() 114 { 115 try 116 { 117 return dateFormat.parse(getText()); 118 } 119 catch (ParseException e) 120 { 121 return new Date (); 122 } 123 catch (Exception e) 124 { 125 return new Date (); 132 } 133 } 134 135 139 private static int posToField(int pos){ 140 if (pos >= fieldPositions.length) { pos = fieldPositions.length - 1; } 143 return fieldPositions[pos]; 144 } 145 146 147 150 private static Calendar parseDate(String datetime) 151 { 152 Calendar c = Calendar.getInstance(); 153 try 154 { 155 Date dat = dateFormat.parse(datetime); 156 c.setTime(dat); 157 } 158 catch (ParseException e) 159 { 160 } 162 return c; 163 } 164 165 170 private void update(int addend, boolean shifted){ 171 Calendar c = parseDate(getText()); 172 int pos = getCaretPosition(); 173 int field = posToField(pos); 174 if (shifted){ 175 c.roll(field,true); 176 } else { 177 c.add(field,addend); 178 } 179 String newDate =dateFormat.format(c.getTime()); 180 setText(newDate); 181 if (pos > newDate.length()) pos = newDate.length(); 182 setCaretPosition(pos); 184 } 185 189 class KeyFocus extends KeyAdapter 190 { 191 KeyFocus() 192 { 193 } 194 195 public void keyPressed(KeyEvent e) 196 { 197 if (e.getKeyCode() == KeyEvent.VK_UP) 198 { 199 update(1,e.isShiftDown()); 200 } 201 else if (e.getKeyCode() == KeyEvent.VK_DOWN) 202 { 203 update(-1,e.isShiftDown()); 204 } 205 } 206 } 207 208 212 class FocusClass implements FocusListener 213 { 214 FocusClass() 215 { 216 } 217 public void focusGained(FocusEvent e) 218 { 219 } 220 public void focusLost(FocusEvent e) 221 { 222 try 223 { 224 dateFormat.parse(getText()); 225 } 226 catch (ParseException e1) 227 { 228 requestFocus(); 229 } 230 } 231 } 232 } 233 | Popular Tags |