001: /**
002: *
003: */package net.sf.jmoney.wizards;
004:
005: import java.util.HashMap;
006: import java.util.Map;
007:
008: import net.sf.jmoney.model2.ExtendableObject;
009: import net.sf.jmoney.model2.ExtendablePropertySet;
010: import net.sf.jmoney.model2.IPropertyControl;
011: import net.sf.jmoney.model2.ScalarPropertyAccessor;
012: import net.sf.jmoney.model2.SessionChangeAdapter;
013:
014: import org.eclipse.jface.wizard.WizardPage;
015: import org.eclipse.swt.SWT;
016: import org.eclipse.swt.events.FocusAdapter;
017: import org.eclipse.swt.events.FocusEvent;
018: import org.eclipse.swt.events.ModifyEvent;
019: import org.eclipse.swt.events.ModifyListener;
020: import org.eclipse.swt.layout.GridData;
021: import org.eclipse.swt.layout.GridLayout;
022: import org.eclipse.swt.widgets.Composite;
023: import org.eclipse.swt.widgets.Label;
024: import org.eclipse.swt.widgets.Text;
025: import org.eclipse.ui.PlatformUI;
026:
027: /**
028: * A wizard page that contains edit controls for all the properties
029: * of a given extendable object.
030: *
031: * @author Nigel Westbury
032: */
033: public class WizardPropertyPage extends WizardPage {
034: private ExtendableObject extendableObject;
035: private ExtendablePropertySet<?> propertySet;
036: private ScalarPropertyAccessor<String> namePropertyAccessor;
037:
038: /**
039: * List of the IPropertyControl objects for the
040: * properties that can be edited in this panel.
041: */
042: private Map<ScalarPropertyAccessor, IPropertyControl> propertyControlList = new HashMap<ScalarPropertyAccessor, IPropertyControl>();
043:
044: Text nameTextbox;
045:
046: public WizardPropertyPage(String pageName, String title,
047: String message, ExtendableObject extendableObject,
048: ExtendablePropertySet<?> propertySet,
049: ScalarPropertyAccessor<String> namePropertyAccessor) {
050: super (pageName);
051: this .extendableObject = extendableObject;
052: this .propertySet = propertySet;
053: this .namePropertyAccessor = namePropertyAccessor;
054:
055: setTitle(title);
056: setMessage(message);
057: }
058:
059: public void createControl(Composite parent) {
060: Composite container = new Composite(parent, SWT.NONE);
061:
062: GridLayout layout = new GridLayout();
063: layout.numColumns = 2;
064: container.setLayout(layout);
065:
066: // Create the controls to edit the properties.
067:
068: // Add the properties for the Account objects.
069: for (final ScalarPropertyAccessor<?> propertyAccessor : propertySet
070: .getScalarProperties3()) {
071:
072: Label propertyLabel = new Label(container, SWT.NONE);
073: propertyLabel
074: .setText(propertyAccessor.getDisplayName() + ':');
075: final IPropertyControl propertyControl = propertyAccessor
076: .createPropertyControl(container);
077:
078: // Bit of a kludge. We have special processing for the account
079: // name, so save this one.
080: if (propertyAccessor == namePropertyAccessor) {
081: nameTextbox = (Text) propertyControl.getControl();
082: }
083:
084: /*
085: * If the control factory set up grid data then leave it
086: * alone. Otherwise set up the grid data based on the
087: * properties minimum sizes and expansion weights. <P> The
088: * control widths are set to the minimum width plus 10 times
089: * the expansion weight. (As we are not short of space, we
090: * make them a little bigger than their minimum sizes). A
091: * minimum of 100 pixels is then applied because this makes
092: * the right sides of the smaller controls line up, which
093: * looks a little more tidy.
094: */
095: if (propertyControl.getControl().getLayoutData() == null) {
096: GridData gridData = new GridData();
097: gridData.minimumWidth = propertyAccessor
098: .getMinimumWidth();
099: gridData.widthHint = Math.max(propertyAccessor
100: .getMinimumWidth()
101: + 10 * propertyAccessor.getWeight(), 100);
102: propertyControl.getControl().setLayoutData(gridData);
103: }
104:
105: propertyControl.getControl().addFocusListener(
106: new FocusAdapter() {
107: @Override
108: public void focusLost(FocusEvent e) {
109: // TODO: Verify this is needed. Clean it up?
110: if (extendableObject.getDataManager()
111: .isSessionFiring()) {
112: return;
113: }
114:
115: propertyControl.save();
116: }
117: });
118:
119: // Add to our list of controls.
120: propertyControlList.put(propertyAccessor, propertyControl);
121: }
122:
123: // Set the values from the object into the control fields.
124: for (IPropertyControl propertyControl : propertyControlList
125: .values()) {
126: propertyControl.load(extendableObject);
127: }
128:
129: setApplicability();
130:
131: // This listener code assumes that the applicability of account properties depends only
132: // on property changes???????
133: extendableObject.getDataManager().addChangeListener(
134: new SessionChangeAdapter() {
135: @Override
136: public void objectChanged(
137: ExtendableObject changedObject,
138: ScalarPropertyAccessor changedProperty,
139: Object oldValue, Object newValue) {
140: setApplicability();
141: }
142: }, parent);
143:
144: setPageComplete(false);
145: nameTextbox.addModifyListener(new ModifyListener() {
146: public void modifyText(ModifyEvent e) {
147: setPageComplete(nameTextbox.getText().length() != 0);
148: }
149: });
150:
151: nameTextbox.setFocus();
152:
153: setControl(container);
154: }
155:
156: private void setApplicability() {
157: for (ScalarPropertyAccessor propertyAccessor : propertyControlList
158: .keySet()) {
159: IPropertyControl propertyControl = propertyControlList
160: .get(propertyAccessor);
161: propertyControl.getControl().setEnabled(
162: propertyAccessor
163: .isPropertyApplicable(extendableObject));
164: }
165: }
166:
167: @Override
168: public boolean canFlipToNextPage() {
169: /*
170: * This method controls whether the 'Next' button is enabled.
171: */
172: return nameTextbox.getText().length() != 0;
173: }
174:
175: @Override
176: public void performHelp() {
177: PlatformUI.getWorkbench().getHelpSystem().displayHelp(
178: "net.sf.jmoney.newAccountWizardId");
179: }
180: }
|