alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.tasklist.core.filter;

import java.awt.Component;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;

import org.openide.util.NbBundle;

/**
 * Basic condition class for date comparisons
 *
 * @author Tor Norbye
 * @author Tim Lebedkov
 */
public class DateFilterCondition extends OneOfFilterCondition {
    public static final int EQUALS = 0;
    public static final int NOTEQUALS = 1;
    public static final int EARLIERTHAN = 2;
    public static final int LATERTHAN = 3;
       
    private static String[] NAME_KEYS = {
        "Equals", // NOI18N
        "NotEquals", // NOI18N
        "EarlierThan", // NOI18N
        "LaterThan", // NOI18N
    };
    
    /**
     * Creates an array of filter conditions for the specified property
     *
     * @param index index of the property
     */
    public static DateFilterCondition[] createConditions() {
        return new DateFilterCondition[] {
            new DateFilterCondition(DateFilterCondition.EARLIERTHAN),
            new DateFilterCondition(DateFilterCondition.LATERTHAN),
            new DateFilterCondition(DateFilterCondition.EQUALS),
            new DateFilterCondition(DateFilterCondition.NOTEQUALS)
        };
    };
    
    /** saved constant for comparison */
    private Date constant = new Date();
    
    /** Date format */
    private static SimpleDateFormat sdf = new SimpleDateFormat();
    
    /**
     * Creates a condition with the given name.
     *
     * @param prop index of the property this condition uses
     * @param id one of the constants from this class
     */
    public DateFilterCondition(int id) {
      super( NAME_KEYS, id);
    }


    public DateFilterCondition(final DateFilterCondition rhs) {
        super(rhs);
        this.constant = (rhs.constant == null) ? null : (Date)rhs.constant.clone();
    }

  
    public Object clone() {
        return new DateFilterCondition(this);  
    }
    
    /** for deconvertization **/
    private DateFilterCondition() {
      super(NAME_KEYS);
      this.constant = null;
    }
    
    public JComponent createConstantComponent() {
        JTextField tf = new JTextField();
        tf.setText(sdf.format(constant));
        tf.setToolTipText(Util.getString("date_desc"));
        return tf;
    }

    public void getConstantFrom(JComponent cmp) {
        JTextField tf = (JTextField) cmp;
        try {
            constant = sdf.parse(tf.getText());
        } catch (ParseException e) {
            // ignore TODO 
        }
    }
    
    public boolean isTrue(Object obj) {
        int c = ((Date) obj).compareTo(constant);
        switch (getId()) {
            case EQUALS:
                return c == 0;
            case NOTEQUALS:
                return c != 0;
            case EARLIERTHAN:
                return c < 0;
            case LATERTHAN:
                return c > 0;
            default:
                throw new InternalError("wrong id");
        }
    }    

  private static class Convertor extends OneOfFilterCondition.Convertor {
    private static final String ELEM_DATE_CONDITION = "DateCondition";
    private static final String ATTR_DATE = "date";

    public Convertor() { super(ELEM_DATE_CONDITION, NAME_KEYS);}
    public static DateFilterCondition.Convertor create() { 
      return new DateFilterCondition.Convertor();
    }

    protected Object readElement(org.w3c.dom.Element element) throws java.io.IOException, java.lang.ClassNotFoundException {
      DateFilterCondition cond = new DateFilterCondition();
      super.readCondition(element, cond);
      cond.constant = new Date(Long.parseLong(element.getAttribute(ATTR_DATE)));
      return cond;
    }
   
    // write methods for supported condition types
    protected void writeElement(org.w3c.dom.Document document, org.w3c.dom.Element element, Object obj) 
      throws java.io.IOException, org.w3c.dom.DOMException 
    {
      DateFilterCondition cond = (DateFilterCondition)obj;
      super.writeElement(document, element, cond);
      element.setAttribute(ATTR_DATE, Long.toString(cond.constant.getTime()));

    }   
  }


}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.