001: /*
002: *
003: * JMoney - A Personal Finance Manager
004: * Copyright (c) 2004 Johann Gyger <jgyger@users.sf.net>
005: *
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
020: *
021: */
022:
023: package net.sf.jmoney.fields;
024:
025: import java.util.Comparator;
026: import java.util.Date;
027:
028: import net.sf.jmoney.JMoneyPlugin;
029: import net.sf.jmoney.VerySimpleDateFormat;
030: import net.sf.jmoney.model2.ExtendableObject;
031: import net.sf.jmoney.model2.IPropertyControl;
032: import net.sf.jmoney.model2.IPropertyControlFactory;
033: import net.sf.jmoney.model2.ScalarPropertyAccessor;
034:
035: import org.eclipse.swt.widgets.Composite;
036:
037: /**
038: * A control factory to edit date values.
039: *
040: * @author Johann Gyger
041: */
042: public class DateControlFactory implements
043: IPropertyControlFactory<Date> {
044:
045: protected VerySimpleDateFormat fDateFormat = new VerySimpleDateFormat(
046: JMoneyPlugin.getDefault().getDateFormat());
047:
048: // Listen to date format changes so we keep up to date
049: // Johann, 2005-07-02: Shouldn't this be done in the control?
050: /*
051: static {
052: JMoneyPlugin.getDefault().getPreferenceStore()
053: .addPropertyChangeListener(new IPropertyChangeListener() {
054: public void propertyChange(PropertyChangeEvent event) {
055: if (event.getProperty().equals("dateFormat")) {
056: fDateFormat = new VerySimpleDateFormat(JMoneyPlugin
057: .getDefault().getDateFormat());
058: }
059: }
060: });
061: }
062: */
063:
064: protected boolean readOnly;
065:
066: public DateControlFactory() {
067: this .readOnly = false;
068: }
069:
070: public DateControlFactory(boolean readOnly) {
071: this .readOnly = readOnly;
072: }
073:
074: public IPropertyControl createPropertyControl(Composite parent,
075: ScalarPropertyAccessor<Date> propertyAccessor) {
076: return new DateEditor(parent, propertyAccessor);
077: }
078:
079: public String formatValueForMessage(
080: ExtendableObject extendableObject,
081: ScalarPropertyAccessor<? extends Date> propertyAccessor) {
082: Date value = extendableObject
083: .getPropertyValue(propertyAccessor);
084: if (value == null) {
085: return "none";
086: } else {
087: return "'" + fDateFormat.format(value) + "'";
088: }
089: }
090:
091: public String formatValueForTable(
092: ExtendableObject extendableObject,
093: ScalarPropertyAccessor<? extends Date> propertyAccessor) {
094: Date value = extendableObject
095: .getPropertyValue(propertyAccessor);
096: return fDateFormat.format(value);
097: }
098:
099: public Date getDefaultValue() {
100: return null;
101: }
102:
103: public boolean isEditable() {
104: return !readOnly;
105: }
106:
107: public Comparator<Date> getComparator() {
108: return new Comparator<Date>() {
109: public int compare(Date date1, Date date2) {
110: return date1.compareTo(date2);
111: }
112: };
113: }
114: }
|