001: package org.osbl.inventory.gui;
002:
003: import org.osbl.client.wings.form.*;
004: import org.osbl.client.wings.form.editor.ManyToOneAssociationEditor;
005: import org.osbl.client.wings.form.layouter.*;
006: import org.osbl.client.wings.shell.Client;
007: import org.osbl.identity.model.Identity;
008: import org.osbl.identity.gui.IdentityEditor;
009: import org.osbl.identity.gui.IdentityConcreteDomainProvider;
010: import org.osbl.inventory.model.*;
011: import org.osbl.ServiceProvider;
012: import org.osbl.persistence.Persistence;
013: import org.osbl.persistence.LoadCommand;
014: import org.conform.*;
015: import org.conform.modifier.InlineModifier;
016:
017: import java.math.BigDecimal;
018:
019: public class AllocationForm extends GenericObjectForm {
020: private IdentityConcreteDomainProvider<CostCenter> costCenterDomain = new IdentityConcreteDomainProvider<CostCenter>(
021: CostCenter.class);
022: private static final String[] PADDING = { "", "0", "00", "000",
023: "0000", };
024:
025: public AllocationForm() {
026: }
027:
028: protected Instruction createLayoutInstruction() {
029: return new PageSet(new Page(Identity.class.getName(),
030: new Division(new Column(new Title(Allocation.class
031: .getName()), new LabelAndEditor(
032: ComponentProvider.ROOT, "key"),
033: new LabelAndEditor(ComponentProvider.ROOT,
034: "costCenter"), new LabelAndEditor(
035: ComponentProvider.ROOT, "percentage"),
036: new LabelAndEditor(ComponentProvider.ROOT,
037: "amount")), new Column())));
038: }
039:
040: public static BigDecimal percentualCosts(BigDecimal percentage,
041: BigDecimal costs) {
042: if (percentage == null || costs == null)
043: return null;
044: else
045: return costs.multiply(percentage).divide(
046: new BigDecimal("100"));
047: }
048:
049: public static String synthesizeKey(CostType costType,
050: Identity costCenterIdentity) {
051: StringBuilder builder = new StringBuilder();
052:
053: if (costCenterIdentity != null) {
054: Persistence persistence = (Persistence) ServiceProvider
055: .getInstance().getService("IdentityPersistence");
056: LoadCommand loadCommand = (LoadCommand) persistence
057: .createCommand("load");
058: loadCommand.setType(CostCenter.class);
059: loadCommand.setId(costCenterIdentity.getId());
060: CostCenter costCenter = (CostCenter) loadCommand.execute();
061: builder.append(costCenter.getKey());
062: } else
063: builder.append("____");
064:
065: builder.append('.');
066:
067: if (costType != null) {
068: builder.append(costType.getKey());
069: builder.append(PADDING[6 - costType.getKey().length()]);
070: } else
071: builder.append("______");
072:
073: return builder.toString();
074: }
075:
076: protected Object getObject(String path) {
077: return object;
078: }
079:
080: protected void setObject(String path, Object object) {
081: this .object = object;
082: }
083:
084: protected BeanMeta createBeanMeta(String path) {
085: VariationBeanMetaProvider provider = new VariationBeanMetaProvider(
086: Client.getInstance().getBeanMetaProvider());
087: provider.addModifier(new InlineModifier() {
088: protected void configure() {
089: property("key").setWritable(false);
090: property("amount").setWritable(false);
091:
092: property("costCenter").setDomainProvider(
093: costCenterDomain);
094: property("costCenter").setFormat(
095: Client.getInstance().getBeanMetaProvider()
096: .getBeanMeta(Identity.class)
097: .getFormat());
098: property("costCenter").setAttribute(
099: org.conform.wings.Editor.CUSTOM_EDITOR,
100: new ManyToOneAssociationEditor());
101: property("costCenter").setAttribute("objectEditor",
102: new IdentityEditor());
103: property("costCenter").setAttribute(
104: PropertyMeta.ATTRIBUTE_CHANGE_LISTENER,
105: new PropertyChangeHandler() {
106: protected void propertyChange(
107: Object oldValue, Object newValue) {
108: Inventory inventory = (Inventory) getContext()
109: .getObject("inventory");
110: if (inventory == null)
111: return;
112: CostType costType = inventory
113: .getCostType();
114: Identity costCenterIdentity = (Identity) propertyData()
115: .getValue();
116: propertyData("key").setValue(
117: synthesizeKey(costType,
118: costCenterIdentity));
119: }
120: });
121:
122: property("percentage").setAttribute(
123: PropertyMeta.ATTRIBUTE_CHANGE_LISTENER,
124: new PropertyChangeHandler() {
125: protected void propertyChange(
126: Object oldValue, Object newValue) {
127: Inventory inventory = (Inventory) getContext()
128: .getObject("inventory");
129: if (inventory == null)
130: return;
131: BigDecimal costs = inventory.getCosts();
132: BigDecimal percentage = (BigDecimal) propertyData()
133: .getValue();
134: BigDecimal amount = percentualCosts(
135: percentage, costs);
136: propertyData("amount").setValue(amount);
137: }
138: });
139: }
140: });
141: return provider.getBeanMeta(Allocation.class);
142: }
143: }
|