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.Vector;
026:
027: import net.sf.jmoney.model2.Entry;
028: import net.sf.jmoney.model2.ExtendableObject;
029: import net.sf.jmoney.model2.IPropertyControl;
030: import net.sf.jmoney.model2.ScalarPropertyAccessor;
031:
032: import org.eclipse.swt.SWT;
033: import org.eclipse.swt.events.FocusListener;
034: import org.eclipse.swt.layout.GridData;
035: import org.eclipse.swt.layout.GridLayout;
036: import org.eclipse.swt.widgets.Composite;
037: import org.eclipse.swt.widgets.Control;
038:
039: /**
040: * This class represents columns containing data from the other
041: * entries. If there are multiple other entries (split entries)
042: * then these values are displayed in a list. This creates variable
043: * height rows.
044: *
045: * @author Nigel Westbury
046: */
047: public class OtherEntriesPropertyBlock extends
048: IndividualBlock<EntryData, EntryRowControl> {
049: protected ScalarPropertyAccessor<?> accessor;
050: private String id;
051:
052: public OtherEntriesPropertyBlock(ScalarPropertyAccessor accessor) {
053: super (accessor.getDisplayName(), accessor.getMinimumWidth(),
054: accessor.getWeight());
055:
056: this .accessor = accessor;
057: this .id = "other." + accessor.getName();
058: }
059:
060: /**
061: * This version of the constructor allows the display name to be overwritten.
062: *
063: * @param accessor
064: * @param displayName
065: */
066: public OtherEntriesPropertyBlock(ScalarPropertyAccessor accessor,
067: String displayName) {
068: super (displayName, accessor.getMinimumWidth(), accessor
069: .getWeight());
070:
071: this .accessor = accessor;
072: this .id = "other." + accessor.getName();
073: }
074:
075: public String getId() {
076: return id;
077: }
078:
079: @Override
080: public ICellControl<EntryData> createCellControl(Composite parent,
081: EntryRowControl rowControl) {
082: // Because this may be multi-valued, setup the container only.
083: final Composite composite = new Composite(parent, SWT.NONE);
084:
085: composite.setBackgroundMode(SWT.INHERIT_FORCE);
086:
087: GridLayout layout = new GridLayout(1, false);
088: layout.marginHeight = 1;
089: layout.verticalSpacing = 1;
090: composite.setLayout(layout);
091:
092: return new ICellControl<EntryData>() {
093:
094: private Vector<IPropertyControl> propertyControls = new Vector<IPropertyControl>();
095: private FocusListener controlFocusListener;
096:
097: public Control getControl() {
098: return composite;
099: }
100:
101: public void load(EntryData data) {
102: for (Control child : composite.getChildren()) {
103: child.dispose();
104: }
105: propertyControls.clear();
106:
107: for (Entry entry : data.getSplitEntries()) {
108: IPropertyControl propertyControl = accessor
109: .createPropertyControl(composite);
110: propertyControl.load(entry);
111: propertyControls.add(propertyControl);
112:
113: propertyControl.getControl().setLayoutData(
114: new GridData(GridData.FILL, GridData.FILL,
115: true, true));
116:
117: addFocusListenerRecursively(propertyControl
118: .getControl(), controlFocusListener);
119: }
120: }
121:
122: public void save() {
123: for (IPropertyControl propertyControl : propertyControls) {
124: propertyControl.save();
125: }
126: }
127:
128: public void setFocusListener(
129: FocusListener controlFocusListener) {
130: this .controlFocusListener = controlFocusListener;
131: }
132:
133: private void addFocusListenerRecursively(Control control,
134: FocusListener listener) {
135: control.addFocusListener(listener);
136: if (control instanceof Composite) {
137: for (Control child : ((Composite) control)
138: .getChildren()) {
139: addFocusListenerRecursively(child, listener);
140: }
141: }
142: }
143: };
144: }
145:
146: public int compare(EntryData entryData1, EntryData entryData2) {
147: if (entryData1.hasSplitEntries()) {
148: if (entryData2.hasSplitEntries()) {
149: return 0;
150: } else {
151: return 1;
152: }
153: } else {
154: if (entryData2.hasSplitEntries()) {
155: return -1;
156: } else {
157: ExtendableObject extendableObject1 = entryData1
158: .getOtherEntry();
159: ExtendableObject extendableObject2 = entryData2
160: .getOtherEntry();
161: return accessor.getComparator().compare(
162: extendableObject1, extendableObject2);
163: }
164: }
165: }
166: }
|