001: package org.osbl.riskmanagement.gui;
002:
003: import org.conform.wings.Editor;
004: import org.conform.wings.PropertyAdapter;
005: import org.conform.wings.editor.DefaultEditorStyles;
006: import org.conform.wings.editor.ComponentInvalidator;
007: import org.conform.*;
008: import org.wings.*;
009: import org.osbl.riskmanagement.model.Record;
010: import org.osbl.client.wings.XIcons;
011:
012: import java.awt.image.BufferedImage;
013: import java.awt.*;
014: import java.beans.PropertyChangeListener;
015: import java.beans.PropertyChangeEvent;
016: import java.util.List;
017: import java.util.ArrayList;
018: import java.math.BigDecimal;
019:
020: import com.representqueens.spark.LineGraph;
021: import com.representqueens.spark.SizeParams;
022:
023: public class ProgressionEditor implements Editor {
024: public SComponent createComponent(PropertyMeta propertyMeta) {
025: PropertyDataLabel propertyDataLabel = new PropertyDataLabel(
026: propertyMeta);
027: propertyDataLabel.setPreferredSize(SDimension.AUTOAREA);
028: configureComponent(propertyMeta, propertyDataLabel, false);
029: return propertyDataLabel;
030: }
031:
032: public void configureComponent(PropertyMeta propertyMeta,
033: SComponent component, boolean erroneous) {
034: PropertyDataLabel propertyDataLabel = (PropertyDataLabel) component;
035: propertyDataLabel.setVisible(propertyMeta.isReadable());
036: propertyDataLabel.setEnabled(propertyMeta.isWritable());
037: DefaultEditorStyles.applyEditorAlignment(propertyMeta,
038: component);
039: component.setStyle("formlabel_normal");
040: }
041:
042: public void setPropertyData(SComponent component,
043: PropertyData propertyData) {
044: BeanData beanData = propertyData.getBeanData();
045: if (beanData != null) {
046: beanData.addPropertyChangeListener(propertyData
047: .getPropertyMeta().getName(),
048: new ComponentInvalidator(component));
049: beanData.addPropertyChangeListener(propertyData
050: .getPropertyMeta().getName(),
051: ((PropertyDataLabel) component));
052: }
053: ((PropertyDataLabel) component).setPropertyData(propertyData);
054: }
055:
056: public PropertyData getPropertyData(SComponent component) {
057: return ((PropertyDataLabel) component).getPropertyData();
058: }
059:
060: static class PropertyDataLabel extends SLabel implements
061: PropertyAdapter, PropertyChangeListener {
062: private PropertyData propertyData;
063: private PropertyMeta propertyMeta;
064: private SIcon icon;
065:
066: public PropertyDataLabel(PropertyMeta propertyMeta) {
067: this .propertyMeta = propertyMeta;
068: }
069:
070: public void setPropertyData(PropertyData propertyData) {
071: this .propertyData = propertyData;
072: }
073:
074: public PropertyData getPropertyData() {
075: return propertyData;
076: }
077:
078: public SIcon getIcon() {
079: if (icon == null) {
080: List<Record> progression = (List<Record>) propertyData
081: .getValue();
082: if (progression == null || progression.size() < 2)
083: icon = XIcons.EMPTY;
084: else {
085: List<Number> numbers = new ArrayList(progression
086: .size());
087: for (Record record : progression) {
088: BigDecimal amount = record.getAmount();
089: BigDecimal probability = record
090: .getProbability();
091: BigDecimal coverage = record.getCoverage();
092: BigDecimal percentage = amount.multiply(
093: probability).divide(coverage, 2);
094: numbers.add(percentage);
095: }
096: SizeParams params = new SizeParams(50, 12, 1);
097: BufferedImage image = LineGraph.createGraph(numbers
098: .toArray(new Number[numbers.size()]),
099: params, Color.blue.darker());
100: icon = new SImageIcon(image);
101: }
102: }
103: return icon;
104: }
105:
106: public SIcon getDisabledIcon() {
107: return getIcon();
108: }
109:
110: public String getText() {
111: return null;
112: }
113:
114: public void propertyChange(PropertyChangeEvent evt) {
115: icon = null;
116: }
117: }
118: }
|