1 19 20 package org.netbeans.modules.tasklist.core.filter; 21 22 import java.text.ParseException ; 23 import java.text.SimpleDateFormat ; 24 import java.util.Calendar ; 25 import java.util.Date ; 26 import javax.swing.*; 27 28 29 35 public class DateFilterCondition extends OneOfFilterCondition { 36 public static final int EQUALS = 0; 37 public static final int NOTEQUALS = 1; 38 public static final int EARLIERTHAN = 2; 39 public static final int LATERTHAN = 3; 40 public static final int ISTODAY = 4; 41 public static final int ISUNDEFINED = 5; 42 43 private static String [] NAME_KEYS = { 44 "Equals", "NotEquals", "EarlierThan", "LaterThan", "IsToday", "IsUndefined" }; 51 52 57 public static DateFilterCondition[] createConditions() { 58 return new DateFilterCondition[] { 59 new DateFilterCondition(DateFilterCondition.EARLIERTHAN), 60 new DateFilterCondition(DateFilterCondition.LATERTHAN), 61 new DateFilterCondition(DateFilterCondition.EQUALS), 62 new DateFilterCondition(DateFilterCondition.NOTEQUALS), 63 new DateFilterCondition(DateFilterCondition.ISTODAY), 64 new DateFilterCondition(DateFilterCondition.ISUNDEFINED) 65 }; 66 }; 67 68 69 private Date constant = new Date (); 70 71 72 private static SimpleDateFormat sdf = new SimpleDateFormat (); 73 74 75 private long today = -1; 76 77 83 public DateFilterCondition(int id) { 84 super(NAME_KEYS, id); 85 } 86 87 88 public DateFilterCondition(final DateFilterCondition rhs) { 89 super(rhs); 90 this.constant = (rhs.constant == null) ? null : (Date )rhs.constant.clone(); 91 } 92 93 94 public Object clone() { 95 return new DateFilterCondition(this); 96 } 97 98 99 DateFilterCondition() { 100 super(NAME_KEYS); 101 this.constant = null; 102 } 103 104 public JComponent createConstantComponent() { 105 if (getId() != ISTODAY && getId() != ISUNDEFINED) { 106 JTextField tf = new JTextField(); 107 tf.setText(sdf.format(constant)); 108 tf.setToolTipText(Util.getString("date_desc")); 109 return tf; 110 } else { 111 return null; 112 } 113 } 114 115 public void getConstantFrom(JComponent cmp) { 116 JTextField tf = (JTextField) cmp; 117 try { 118 constant = sdf.parse(tf.getText()); 119 } catch (ParseException e) { 120 } 122 } 123 124 public boolean isTrue(Object obj) { 125 int c; 126 if (obj != null) 127 c = ((Date ) obj).compareTo(constant); 128 else 129 c = 0; 130 switch (getId()) { 131 case EQUALS: 132 return obj != null && c == 0; 133 case NOTEQUALS: 134 return obj != null && c != 0; 135 case EARLIERTHAN: 136 return obj != null && c < 0; 137 case LATERTHAN: 138 return obj != null && c > 0; 139 case ISTODAY: { 140 if (obj == null) 141 return false; 142 143 if (today < 0 || System.currentTimeMillis() - today >= 144 24 * 60 * 60 * 1000) { 145 Calendar cal = Calendar.getInstance(); 146 cal.set(Calendar.HOUR_OF_DAY, 0); 147 cal.set(Calendar.MINUTE, 0); 148 cal.set(Calendar.SECOND, 0); 149 cal.set(Calendar.MILLISECOND, 0); 150 today = cal.getTimeInMillis(); 151 } 152 long diff = ((Date ) obj).getTime() - today; 153 return diff >= 0 && diff < 24 * 60 * 60 * 1000; 154 } 155 case ISUNDEFINED: 156 return obj == null; 157 default: 158 throw new InternalError ("wrong id"); 159 } 160 } 161 162 private static class Convertor extends OneOfFilterCondition.Convertor { 163 private static final String ELEM_DATE_CONDITION = "DateCondition"; 164 private static final String ATTR_DATE = "date"; 165 166 public Convertor() { 167 super(ELEM_DATE_CONDITION, NAME_KEYS); 168 } 169 170 public static DateFilterCondition.Convertor create() { 171 return new DateFilterCondition.Convertor(); 172 } 173 174 protected Object readElement(org.w3c.dom.Element element) throws java.io.IOException , java.lang.ClassNotFoundException { 175 DateFilterCondition cond = new DateFilterCondition(); 176 super.readCondition(element, cond); 177 cond.constant = new Date (Long.parseLong(element.getAttribute(ATTR_DATE))); 178 return cond; 179 } 180 181 protected void writeElement(org.w3c.dom.Document document, org.w3c.dom.Element element, Object obj) 183 throws java.io.IOException , org.w3c.dom.DOMException { 184 DateFilterCondition cond = (DateFilterCondition)obj; 185 super.writeElement(document, element, cond); 186 element.setAttribute(ATTR_DATE, Long.toString(cond.constant.getTime())); 187 188 } 189 } 190 } 191 | Popular Tags |