1 19 20 21 package org.netbeans.modules.search.types; 22 23 24 import java.text.DateFormat ; 25 import java.text.DecimalFormat ; 26 import java.text.ParseException ; 27 import java.text.SimpleDateFormat ; 28 import java.util.Date ; 29 import java.util.GregorianCalendar ; 30 31 import org.openide.filesystems.FileObject; 32 import org.openide.loaders.DataObject; 33 import org.openide.util.HelpCtx; 34 import org.openide.util.NbBundle; 35 36 37 49 public class ModificationDateType extends DataObjectType { 50 51 private static final long serialVersionUID = 4L; 52 54 55 private Date matchBefore; 56 57 58 private Date matchAfter; 59 60 61 private Short days; 62 63 64 66 protected String displayName() { 67 72 73 return NbBundle.getMessage(ModificationDateType.class, 74 "TEXT_DATE_CRITERION"); } 76 77 81 public boolean testDataObject(DataObject dobj) { 82 83 FileObject fo = dobj.getPrimaryFile(); 84 85 if(fo == null) 87 return false; 88 89 Date date = fo.lastModified(); 91 92 boolean hit = testDays(date) && testAfter(date) && testBefore(date); 93 94 if(hit) 95 return true; 96 else 97 return false; 98 } 99 100 101 private boolean testAfter(Date date) { 102 if (matchAfter == null) return true; 103 return date.compareTo(matchAfter)>=0; 104 } 105 106 107 private boolean testBefore(Date date) { 108 if (matchBefore == null) return true; 109 return date.compareTo(matchBefore)<=0; 110 } 111 112 113 private boolean testDays(Date date) { 114 if (days == null) return true; 115 return (System.currentTimeMillis() - date.getTime()) < days.shortValue()*1000L*60L*60L*24L; 116 } 117 118 121 public Date getMatchBeforeAsDate() { 122 return new FormattedDate(matchBefore); 123 } 124 125 126 public String getMatchBefore() { 127 if (matchBefore == null) 128 return ""; else 130 return getMatchBeforeAsDate().toString(); 131 } 132 133 135 public void setMatchBefore(String before) { 136 try { 137 setMatchBeforeImpl(before); 138 139 setValid (this.matchBefore != null); 140 } catch (IllegalArgumentException ex) { 141 setValid(false); 142 throw ex; 143 } 144 } 145 146 147 private void setMatchBeforeImpl(String matchBefore) { 148 if(matchBefore == null) throw new IllegalArgumentException (); 149 150 if(matchBefore.equals("")) { setMatchBeforeByDate(null); 152 return; 153 } 154 155 try { 156 setMatchBeforeByDate(new FormattedDate(matchBefore)); 157 158 } catch (ParseException ex) { 159 throw new IllegalArgumentException (); 160 } 161 162 } 163 164 166 public void setMatchBeforeByDate(Date matchBefore) { 167 if (matchBefore != null) { 169 170 GregorianCalendar cal = new GregorianCalendar (); 171 cal.setTime(matchBefore); 172 173 cal.set(cal.HOUR_OF_DAY, cal.getActualMaximum(cal.HOUR_OF_DAY)); 174 cal.set(cal.MINUTE, cal.getActualMaximum(cal.MINUTE)); 175 cal.set(cal.SECOND, cal.getActualMaximum(cal.SECOND)); 176 cal.set(cal.MILLISECOND, cal.getActualMaximum(cal.MILLISECOND)); 177 178 matchBefore = cal.getTime(); 179 } 180 181 183 this.matchBefore = matchBefore; 184 days = null; 185 } 186 187 188 191 public Date getMatchAfterAsDate() { 192 return new FormattedDate(matchAfter); 193 } 194 195 196 public String getMatchAfter() { 197 198 if (matchAfter == null) 199 return ""; else 201 return getMatchAfterAsDate().toString(); 202 } 203 204 206 public void setMatchAfter(String after) { 207 try { 208 setMatchAfterImpl(after); 209 210 setValid (this.matchAfter != null); 211 } catch (IllegalArgumentException ex) { 212 setValid(false); 213 throw ex; 214 } 215 } 216 217 218 private void setMatchAfterImpl(String matchAfter) { 219 220 if (matchAfter == null) throw new IllegalArgumentException (); 221 222 if (matchAfter.equals("")) { setMatchAfterByDate(null); 224 return; 225 } 226 227 try { 228 setMatchAfterByDate(new FormattedDate(matchAfter)); 229 230 } catch (ParseException ex) { 231 throw new IllegalArgumentException (); 232 } 233 234 } 235 236 239 public void setMatchAfterByDate(Date matchAfter) { 240 241 243 if (matchAfter != null) { 244 245 GregorianCalendar cal = new GregorianCalendar (); 246 cal.setTime(matchAfter); 247 248 cal.set(cal.HOUR_OF_DAY, cal.getActualMinimum(cal.HOUR_OF_DAY)); 249 cal.set(cal.MINUTE, cal.getActualMinimum(cal.MINUTE)); 250 cal.set(cal.SECOND, cal.getActualMinimum(cal.SECOND)); 251 cal.set(cal.MILLISECOND, cal.getActualMinimum(cal.MILLISECOND)); 252 253 matchAfter = cal.getTime(); 254 } 255 256 258 this.matchAfter = matchAfter; 259 days = null; 260 } 261 262 265 public Short getDaysAsShort() { 266 return days; 267 } 268 269 270 public String getDays() { 271 if(days == null) { 272 return ""; } else { 274 return days.toString(); 275 } 276 } 277 278 280 public void setDays(String days) { 281 try { 282 setDaysImpl(days); 283 284 setValid (this.days != null); 285 } catch (IllegalArgumentException ex) { 286 setValid(false); 287 throw ex; 288 } 289 } 290 291 292 private void setDaysImpl(String days) { 293 294 if("".equals(days)) { setDaysByShort(null); 296 return; 297 } 298 299 try { 300 DecimalFormat format = new DecimalFormat (); 301 setDaysByShort(new Short (format.parse(days).shortValue())); 302 303 } catch (ParseException ex) { 304 throw new IllegalArgumentException (); 305 } 306 307 } 308 309 311 private void setDaysByShort(Short days) { 312 this.days = days; 313 314 matchAfter = null; 315 matchBefore = null; 316 } 317 318 320 public HelpCtx getHelpCtx() { 321 return new HelpCtx(ModificationDateType.class); 322 } 323 324 325 328 static class FormattedDate extends Date { 329 330 331 private static DateFormat format; 332 333 private transient boolean isNull = true; 334 335 336 static { 337 format = new SimpleDateFormat ().getDateInstance(); 338 } 339 340 343 public FormattedDate(Date date) { 344 super(date == null ? new Date ().getTime() : date.getTime() ); 345 isNull = date == null; 346 } 347 348 350 public FormattedDate(String date) throws ParseException { 351 super( format.parse(date).getTime() ); 352 isNull = date == null; 353 } 354 355 356 public String toString() { 357 return format.format(this); 358 } 359 360 363 public boolean equals(Object obj){ 364 if (obj == null && isNull) return true; 365 else return super.equals(obj); 366 } 367 } 368 369 } 370 | Popular Tags |