ProgressionEditor.java :  » Workflow-Engines » osbl-1_0 » org » osbl » riskmanagement » gui » Java Open Source

Java Open Source » Workflow Engines » osbl 1_0 
osbl 1_0 » org » osbl » riskmanagement » gui » ProgressionEditor.java
package org.osbl.riskmanagement.gui;

import org.conform.wings.Editor;
import org.conform.wings.PropertyAdapter;
import org.conform.wings.editor.DefaultEditorStyles;
import org.conform.wings.editor.ComponentInvalidator;
import org.conform.*;
import org.wings.*;
import org.osbl.riskmanagement.model.Record;
import org.osbl.client.wings.XIcons;

import java.awt.image.BufferedImage;
import java.awt.*;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.util.List;
import java.util.ArrayList;
import java.math.BigDecimal;

import com.representqueens.spark.LineGraph;
import com.representqueens.spark.SizeParams;

public class ProgressionEditor
    implements Editor
{
    public SComponent createComponent(PropertyMeta propertyMeta) {
        PropertyDataLabel propertyDataLabel = new PropertyDataLabel(propertyMeta);
        propertyDataLabel.setPreferredSize(SDimension.AUTOAREA);
        configureComponent(propertyMeta, propertyDataLabel, false);
        return propertyDataLabel;
    }

    public void configureComponent(PropertyMeta propertyMeta, SComponent component, boolean erroneous) {
        PropertyDataLabel propertyDataLabel = (PropertyDataLabel)component;
        propertyDataLabel.setVisible(propertyMeta.isReadable());
        propertyDataLabel.setEnabled(propertyMeta.isWritable());
        DefaultEditorStyles.applyEditorAlignment(propertyMeta, component);
        component.setStyle("formlabel_normal");
    }

    public void setPropertyData(SComponent component, PropertyData propertyData) {
        BeanData beanData = propertyData.getBeanData();
        if (beanData != null) {
            beanData.addPropertyChangeListener(propertyData.getPropertyMeta().getName(), new ComponentInvalidator(component));
            beanData.addPropertyChangeListener(propertyData.getPropertyMeta().getName(), ((PropertyDataLabel)component));
        }
        ((PropertyDataLabel)component).setPropertyData(propertyData);
    }

    public PropertyData getPropertyData(SComponent component) {
        return ((PropertyDataLabel)component).getPropertyData();
    }

    static class PropertyDataLabel
        extends SLabel
        implements PropertyAdapter, PropertyChangeListener
    {
        private PropertyData propertyData;
        private PropertyMeta propertyMeta;
        private SIcon icon;

        public PropertyDataLabel(PropertyMeta propertyMeta) {
            this.propertyMeta = propertyMeta;
        }

        public void setPropertyData(PropertyData propertyData) {
            this.propertyData = propertyData;
        }

        public PropertyData getPropertyData() {
            return propertyData;
        }

        public SIcon getIcon() {
            if (icon == null) {
                List<Record> progression = (List<Record>)propertyData.getValue();
                if (progression == null || progression.size() < 2)
                    icon = XIcons.EMPTY;
                else {
                    List<Number> numbers = new ArrayList(progression.size());
                    for (Record record : progression) {
                        BigDecimal amount = record.getAmount();
                        BigDecimal probability = record.getProbability();
                        BigDecimal coverage = record.getCoverage();
                        BigDecimal percentage = amount.multiply(probability).divide(coverage, 2);
                        numbers.add(percentage);
                    }
                    SizeParams params = new SizeParams(50, 12, 1);
                    BufferedImage image = LineGraph.createGraph(numbers.toArray(new Number[numbers.size()]), params, Color.blue.darker());
                    icon = new SImageIcon(image);
                }
            }
            return icon;
        }

        public SIcon getDisabledIcon() {
            return getIcon();
        }

        public String getText() {
            return null;
        }

        public void propertyChange(PropertyChangeEvent evt) {
            icon = null;
        }
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.