1 14 package org.compiere.grid.ed; 15 16 import javax.swing.text.*; 17 import javax.swing.event.*; 18 import java.text.*; 19 import java.util.*; 20 import java.awt.*; 21 import java.sql.Timestamp ; 22 23 import org.compiere.util.*; 24 25 33 public final class MDocDate extends PlainDocument implements CaretListener 34 { 35 42 public MDocDate (int displayType, SimpleDateFormat format, 43 JTextComponent tc, String title) 44 { 45 super(); 46 m_displayType = displayType; 47 m_tc = tc; 48 m_tc.addCaretListener(this); 49 m_format = format; 51 if (m_format == null) 52 m_format = new SimpleDateFormat(); 53 m_format.setLenient(false); 54 55 char[] pattern = m_format.toPattern().toCharArray(); 57 for (int i = 0; i < pattern.length; i++) 58 { 59 if ("Mdy".indexOf(pattern[i]) == -1) 61 pattern[i] = DELIMITER; 62 } 63 m_mask = new String (pattern); 64 m_title = title; 66 if (m_title == null) 67 m_title = ""; 68 } 70 private JTextComponent m_tc; 71 private SimpleDateFormat m_format; 72 private String m_mask; 73 private static final char DELIMITER = '^'; 74 private String m_title; 76 private int m_displayType; 77 private int m_lastDot = 0; 79 86 public void insertString (int offset, String string, AttributeSet attr) 87 throws BadLocationException 88 { 89 93 if (string != null && string.length() == 1) 96 { 97 if (offset >= m_mask.length()) 99 return; 100 101 int length = getText().length(); 103 if (offset == 0 && length == 0) 104 { 105 Date today = new Date(System.currentTimeMillis()); 106 String dateStr = m_format.format(today); 107 super.insertString(0, string + dateStr.substring(1), attr); 108 m_tc.setCaretPosition(1); 109 return; 110 } 111 112 try 114 { 115 Integer.parseInt(string); 116 } 117 catch (Exception pe) 118 { 119 startDateDialog(); 120 return; 121 } 122 123 135 136 if (offset != m_mask.length()-1 && m_mask.charAt(offset+1) == DELIMITER) 138 m_tc.setCaretPosition(offset+2); 139 140 if (m_mask.charAt(offset) == DELIMITER) 142 { 143 offset++; 144 m_tc.setCaretPosition(offset+1); 145 } 146 super.remove(offset, 1); } 148 149 super.insertString(offset, string, attr); 151 } 153 159 public void remove (int offset, int length) 160 throws BadLocationException 161 { 162 165 if (offset == 0 || length == 0) 167 { 168 if (length == m_mask.length() || length == 0) 170 super.remove(offset, length); 171 return; 172 } 173 174 if (offset-1 >= 0 && offset-1 < m_mask.length() 176 && m_mask.charAt(offset-1) == DELIMITER) 177 { 178 if (offset-2 >= 0) 179 m_tc.setCaretPosition(offset-2); 180 else 181 return; 182 } 183 else 184 m_tc.setCaretPosition(offset-1); 185 } 187 191 public void caretUpdate(CaretEvent e) 192 { 193 if (e.getDot() != e.getMark()) 195 { 196 m_lastDot = e.getDot(); 197 return; 198 } 199 203 if (e.getDot()+1 > m_mask.length() || m_mask.charAt(e.getDot()) != DELIMITER) 205 { 206 m_lastDot = e.getDot(); 207 return; 208 } 209 210 int newDot = -1; 212 if (m_lastDot > e.getDot()) newDot = e.getDot() - 1; 214 else newDot = e.getDot() + 1; 216 if (e.getDot() == 0) newDot = 1; 218 else if (e.getDot() == m_mask.length()-1) newDot = e.getDot() - 1; 220 m_lastDot = e.getDot(); 225 if (newDot >= 0 && newDot < getText().length()) 226 m_tc.setCaretPosition(newDot); 227 } 229 233 private String getText() 234 { 235 String str = ""; 236 try 237 { 238 str = getContent().getString(0, getContent().length()-1); } 240 catch (Exception e) 241 { 242 str = ""; 243 } 244 return str; 245 } 247 250 private void startDateDialog() 251 { 252 Log.trace(Log.l3_Util, "MDocDate.startDateDialog"); 253 254 String result = getText(); 256 Timestamp ts = null; 257 try 258 { 259 ts = new Timestamp (m_format.parse(result).getTime()); 260 } 261 catch (Exception pe) 262 { 263 ts = new Timestamp (System.currentTimeMillis()); 264 } 265 ts = VDate.startCalendar(m_tc, ts, m_format, m_displayType, m_title); 266 result = m_format.format(ts); 267 268 try 270 { 271 super.remove(0, getText().length()); 272 super.insertString(0, result, null); 273 } 274 catch (BadLocationException ble) 275 { 276 Log.error("MDocDate.startDateDialog", ble); 277 } 278 } 280 } | Popular Tags |