001: /*
002: *
003: * JMoney - A Personal Finance Manager
004: * Copyright (c) 2007 Nigel Westbury <westbury@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.entrytable;
024:
025: import java.util.Comparator;
026:
027: import net.sf.jmoney.model2.ExtendableObject;
028: import net.sf.jmoney.model2.IPropertyControl;
029: import net.sf.jmoney.model2.ScalarPropertyAccessor;
030:
031: import org.eclipse.swt.events.FocusListener;
032: import org.eclipse.swt.widgets.Composite;
033: import org.eclipse.swt.widgets.Control;
034:
035: /**
036: * Represents a property that can be displayed in the entries table,
037: * edited by the user, or used in the filter.
038: * <P>
039: * Only properties where a single value exists in the cell are supported
040: * by this class.
041: * <P>
042: * The credit, debit, and balance columns are hard coded at the end
043: * of the table and are not represented by objects of this class.
044: *
045: * @author Nigel Westbury
046: */
047: abstract public class PropertyBlock<T extends EntryData> extends
048: IndividualBlock<T, Composite> {
049: private ScalarPropertyAccessor<?> accessor;
050: private String id;
051:
052: public PropertyBlock(ScalarPropertyAccessor accessor, String source) {
053: super (accessor.getDisplayName(), accessor.getMinimumWidth(),
054: accessor.getWeight());
055:
056: this .accessor = accessor;
057: this .id = source + '.' + accessor.getName();
058: }
059:
060: public PropertyBlock(ScalarPropertyAccessor accessor,
061: String source, String displayName) {
062: super (displayName, accessor.getMinimumWidth(), accessor
063: .getWeight());
064:
065: this .accessor = accessor;
066: this .id = source + '.' + accessor.getName();
067: }
068:
069: public String getId() {
070: return id;
071: }
072:
073: public abstract ExtendableObject getObjectContainingProperty(T data);
074:
075: @Override
076: public ICellControl<T> createCellControl(Composite parent,
077: Composite rowControl) {
078: final IPropertyControl propertyControl = accessor
079: .createPropertyControl(parent);
080:
081: return new ICellControl<T>() {
082:
083: public Control getControl() {
084: return propertyControl.getControl();
085: }
086:
087: public void load(T data) {
088: propertyControl.load(getObjectContainingProperty(data));
089: }
090:
091: public void save() {
092: propertyControl.save();
093: }
094:
095: public void setFocusListener(
096: FocusListener controlFocusListener) {
097: // Nothing to do
098: }
099: };
100: }
101:
102: @Override
103: public Comparator<T> getComparator() {
104: final Comparator<ExtendableObject> subComparator = accessor
105: .getComparator();
106: if (subComparator == null) {
107: return null;
108: } else {
109: return new Comparator<T>() {
110: public int compare(T entryData1, T entryData2) {
111: ExtendableObject extendableObject1 = getObjectContainingProperty(entryData1);
112: ExtendableObject extendableObject2 = getObjectContainingProperty(entryData2);
113: if (extendableObject1 == null
114: && extendableObject2 == null)
115: return 0;
116: if (extendableObject1 == null)
117: return 1;
118: if (extendableObject2 == null)
119: return -1;
120: return subComparator.compare(extendableObject1,
121: extendableObject2);
122: }
123: };
124: }
125: }
126:
127: public static PropertyBlock<EntryData> createTransactionColumn(
128: ScalarPropertyAccessor<?> propertyAccessor) {
129: return new PropertyBlock<EntryData>(propertyAccessor,
130: "transaction") {
131: @Override
132: public ExtendableObject getObjectContainingProperty(
133: EntryData data) {
134: return data.getEntry().getTransaction();
135: }
136: };
137: }
138:
139: public static PropertyBlock<EntryData> createEntryColumn(
140: ScalarPropertyAccessor<?> propertyAccessor) {
141: return new PropertyBlock<EntryData>(propertyAccessor, "entry") {
142: @Override
143: public ExtendableObject getObjectContainingProperty(
144: EntryData data) {
145: return data.getEntry();
146: }
147: };
148: }
149:
150: /**
151: * This version allows the caller to override the text used in the header.
152: * @param propertyAccessor
153: * @param displayName the text to use in the header
154: * @return
155: */
156: public static PropertyBlock<EntryData> createEntryColumn(
157: ScalarPropertyAccessor<?> propertyAccessor,
158: String displayName) {
159: return new PropertyBlock<EntryData>(propertyAccessor, "entry",
160: displayName) {
161: @Override
162: public ExtendableObject getObjectContainingProperty(
163: EntryData data) {
164: return data.getEntry();
165: }
166: };
167: }
168: }
|