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.Date;
026:
027: import net.sf.jmoney.model2.ExtendableObject;
028: import net.sf.jmoney.model2.IPropertyControl;
029: import net.sf.jmoney.model2.ScalarPropertyAccessor;
030: import net.sf.jmoney.model2.SessionChangeAdapter;
031: import net.sf.jmoney.model2.SessionChangeListener;
032:
033: import org.eclipse.swt.events.DisposeEvent;
034: import org.eclipse.swt.events.DisposeListener;
035: import org.eclipse.swt.graphics.Font;
036: import org.eclipse.swt.widgets.Composite;
037: import org.eclipse.swt.widgets.Control;
038:
039: /**
040: * Editor class for date properties. These dates are formatted according to the
041: * selected format in the preferences.
042: *
043: * @author Johann Gyger
044: */
045: public class DateEditor implements IPropertyControl {
046:
047: protected ScalarPropertyAccessor<Date> propertyAccessor;
048: protected DateControl propertyControl;
049: protected ExtendableObject extendableObject;
050:
051: private SessionChangeListener dateChangeListener = new SessionChangeAdapter() {
052: @Override
053: public void objectChanged(ExtendableObject changedObject,
054: ScalarPropertyAccessor changedProperty,
055: Object oldValue, Object newValue) {
056: if (changedObject.equals(extendableObject)
057: && changedProperty == propertyAccessor) {
058: Date date = extendableObject
059: .getPropertyValue(propertyAccessor);
060: propertyControl.setDate(date);
061: }
062: }
063: };
064:
065: /**
066: * Create a new date editor.
067: */
068: public DateEditor(final Composite parent,
069: ScalarPropertyAccessor<Date> propertyAccessor) {
070: this .propertyAccessor = propertyAccessor;
071:
072: Font font = parent.getFont();
073:
074: propertyControl = new DateControl(parent);
075: propertyControl.setFont(font);
076:
077: propertyControl.addDisposeListener(new DisposeListener() {
078: public void widgetDisposed(DisposeEvent e) {
079: if (extendableObject != null) {
080: extendableObject.getDataManager()
081: .removeChangeListener(dateChangeListener);
082: }
083: }
084: });
085: }
086:
087: /* (non-Javadoc)
088: * @see net.sf.jmoney.model2.IPropertyControl#load(net.sf.jmoney.model2.ExtendableObject)
089: */
090: public void load(ExtendableObject object) {
091: if (extendableObject != null) {
092: extendableObject.getDataManager().removeChangeListener(
093: dateChangeListener);
094: }
095:
096: this .extendableObject = object;
097: if (object == null) {
098: propertyControl.setDate(null);
099: } else {
100: Date d = object.getPropertyValue(propertyAccessor);
101: propertyControl.setDate(d);
102:
103: // Listen for changes to the session data.
104: extendableObject.getDataManager().addChangeListener(
105: dateChangeListener);
106: }
107:
108: propertyControl.setEnabled(object != null);
109: }
110:
111: /* (non-Javadoc)
112: * @see net.sf.jmoney.model2.IPropertyControl#save()
113: */
114: public void save() {
115: extendableObject.setPropertyValue(propertyAccessor,
116: propertyControl.getDate());
117: }
118:
119: /* (non-Javadoc)
120: * @see net.sf.jmoney.model2.IPropertyControl#getControl()
121: */
122: public Control getControl() {
123: return propertyControl;
124: }
125: }
|