01: /*
02: *
03: * JMoney - A Personal Finance Manager
04: * Copyright (c) 2004 Nigel Westbury <westbury@users.sourceforge.net>
05: *
06: *
07: * This program is free software; you can redistribute it and/or modify
08: * it under the terms of the GNU General Public License as published by
09: * the Free Software Foundation; either version 2 of the License, or
10: * (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public License
18: * along with this program; if not, write to the Free Software
19: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20: *
21: */
22:
23: package net.sf.jmoney.fields;
24:
25: import net.sf.jmoney.model2.ExtendableObject;
26: import net.sf.jmoney.model2.IPropertyControl;
27: import net.sf.jmoney.model2.ScalarPropertyAccessor;
28:
29: import org.eclipse.swt.SWT;
30: import org.eclipse.swt.events.SelectionAdapter;
31: import org.eclipse.swt.events.SelectionEvent;
32: import org.eclipse.swt.widgets.Button;
33: import org.eclipse.swt.widgets.Composite;
34: import org.eclipse.swt.widgets.Control;
35:
36: /**
37: * Editor class for boolean values that are to be edited using a check box.
38: *
39: * @author Nigel Westbury
40: */
41: public class CheckMarkEditor implements IPropertyControl {
42:
43: protected ScalarPropertyAccessor<Boolean> propertyAccessor;
44: protected Button propertyControl;
45: protected ExtendableObject extendableObject;
46:
47: /**
48: * Create a new date editor.
49: */
50: public CheckMarkEditor(Composite parent,
51: ScalarPropertyAccessor<Boolean> propertyAccessor) {
52: propertyControl = new Button(parent, SWT.CHECK);
53: this .propertyAccessor = propertyAccessor;
54:
55: // Selection changes are reflected immediately in the
56: // datastore. This allows controls for other properties,
57: // which may depend on this property, to be immediately made
58: // visible or invisible.
59:
60: propertyControl.addSelectionListener(new SelectionAdapter() {
61: @Override
62: public void widgetSelected(SelectionEvent e) {
63: save();
64: }
65: });
66: }
67:
68: /* (non-Javadoc)
69: * @see net.sf.jmoney.model2.IPropertyControl#load(net.sf.jmoney.model2.ExtendableObject)
70: */
71: public void load(ExtendableObject object) {
72: this .extendableObject = object;
73: if (object == null) {
74: propertyControl.setSelection(false);
75: } else {
76: Boolean value = object.getPropertyValue(propertyAccessor);
77: propertyControl.setSelection(value.booleanValue());
78: }
79: propertyControl.setEnabled(object != null);
80: }
81:
82: /* (non-Javadoc)
83: * @see net.sf.jmoney.model2.IPropertyControl#save()
84: */
85: public void save() {
86: boolean value = propertyControl.getSelection();
87: extendableObject.setPropertyValue(propertyAccessor,
88: new Boolean(value));
89: }
90:
91: /* (non-Javadoc)
92: * @see net.sf.jmoney.model2.IPropertyControl#getControl()
93: */
94: public Control getControl() {
95: return propertyControl;
96: }
97:
98: }
|