001: package org.osbl.riskmanagement.gui.report;
002:
003: import org.wings.*;
004: import org.wings.session.SessionManager;
005: import org.wings.io.Device;
006: import org.wings.plaf.css.*;
007: import org.jfree.chart.*;
008: import org.jfree.chart.labels.XYToolTipGenerator;
009: import org.jfree.chart.urls.XYURLGenerator;
010: import org.jfree.chart.entity.StandardEntityCollection;
011: import org.jfree.chart.annotations.XYBoxAnnotation;
012: import org.jfree.chart.renderer.xy.*;
013: import org.jfree.chart.axis.*;
014: import org.jfree.chart.plot.*;
015: import org.jfree.data.xy.*;
016: import org.jfree.ui.Layer;
017: import org.osbl.persistence.*;
018: import org.osbl.ServiceProvider;
019: import org.osbl.client.action.ActionProvider;
020: import org.osbl.client.wings.shell.Client;
021: import org.osbl.client.wings.action.ShowEditorAction;
022: import org.osbl.riskmanagement.model.Risk;
023: import org.osbl.riskmanagement.model.Criticality;
024:
025: import java.awt.*;
026: import java.awt.image.BufferedImage;
027: import java.util.*;
028: import java.util.List;
029: import java.math.BigDecimal;
030: import java.io.IOException;
031:
032: public class RiskChart extends SComponent implements
033: LowLevelEventListener {
034: private String imageMap;
035: private SIcon icon;
036:
037: public RiskChart() {
038: setCG(new AbstractComponentCG() {
039: public void writeInternal(Device device,
040: SComponent component) throws IOException {
041: writeTablePrefix(device, component);
042: writeImageMap(device, (RiskChart) component);
043: writeIconWithMap(device, component.getName() + "_map",
044: icon, Utils.isMSIE(component));
045: writeTableSuffix(device, component);
046: }
047:
048: public void writeIconWithMap(Device device, String name,
049: SIcon icon, boolean isMSIE) throws IOException {
050: device.print("<img class=\"nopad\"");
051: if (isMSIE && icon.getURL().toString().endsWith(".png")
052: && icon.getIconWidth() > 0
053: && icon.getIconHeight() > 0) {
054: Utils.optAttribute(device, "style",
055: "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"
056: + icon.getURL()
057: + "', sizingMethod='scale')");
058: Utils.optAttribute(device, "src",
059: AbstractLabelCG.TRANSPARENT_ICON.getURL());
060: } else
061: Utils.optAttribute(device, "src", icon.getURL());
062:
063: Utils
064: .optAttribute(device, "width", icon
065: .getIconWidth());
066: Utils.optAttribute(device, "height", icon
067: .getIconHeight());
068: Utils.optAttribute(device, "usemap", name);
069: device.print(" alt=\"");
070: device.print(icon.getIconTitle());
071: device.print("\"/>");
072: }
073:
074: private void writeImageMap(Device device,
075: RiskChart riskChart) throws IOException {
076: device.print(riskChart.getImageMap());
077: }
078: });
079: setBackground(Color.WHITE);
080: setPreferredSize(new SDimension(600, 400));
081:
082: Persistence persistence = (Persistence) ServiceProvider
083: .getInstance().getService("RiskManagementPersistence");
084: SimpleQueryCommand listCommand = (SimpleQueryCommand) persistence
085: .createCommand("list");
086: listCommand.setType(Risk.class);
087: listCommand.addFilter("obsolete", Operator.NULL, null);
088: listCommand.addFilter("criticality", Operator.NOT_EQUAL,
089: Criticality.UNKNOWN);
090:
091: List<Risk> list = listCommand.execute();
092:
093: JFreeChart chart = createRiskMap(list);
094:
095: SDimension dimension = getPreferredSize();
096: ChartRenderingInfo info = new ChartRenderingInfo(
097: new StandardEntityCollection());
098: BufferedImage bufferedImage = chart
099: .createBufferedImage(dimension.getWidthInt(), dimension
100: .getHeightInt(), info);
101:
102: this .imageMap = ChartUtilities.getImageMap(getName() + "_map",
103: info);
104: this .icon = new SImageIcon(bufferedImage);
105: }
106:
107: private JFreeChart createRiskMap(final List<Risk> risks) {
108: NumberAxis xAxis = new NumberAxis("Amount");
109: xAxis.setRange(0, 160);
110: xAxis.setTickUnit(new NumberTickUnit(40));
111: NumberAxis yAxis = new NumberAxis("Probability");
112: yAxis.setRange(0, 100);
113: yAxis.setTickUnit(new NumberTickUnit(25));
114:
115: XYSeries series = new XYSeries("Risks", false);
116: for (Risk risk : risks) {
117: BigDecimal amount = risk.getAmount();
118: BigDecimal probability = risk.getProbability();
119: BigDecimal coverage = risk.getCoverage();
120: BigDecimal multiplied = amount.multiply(probability);
121: BigDecimal percentage = multiplied.divide(coverage, 2);
122: if (percentage.intValue() > 160)
123: percentage = new BigDecimal(160);
124: series.add(percentage, probability);
125: }
126: XYDataset dataset = new XYSeriesCollection(series);
127:
128: Stroke dashed = XYPlot.DEFAULT_GRIDLINE_STROKE;
129: Paint gray = XYPlot.DEFAULT_GRIDLINE_PAINT;
130: Paint green = ChartColor.VERY_LIGHT_GREEN;
131: Paint red = ChartColor.VERY_LIGHT_RED;
132:
133: StandardXYItemRenderer renderer = new StandardXYItemRenderer(
134: StandardXYItemRenderer.SHAPES,
135: new XYToolTipGenerator() {
136: public String generateToolTip(XYDataset dataset,
137: int series, int item) {
138: Number x = dataset.getX(series, item);
139: Number y = dataset.getY(series, item);
140: Risk risk = risks.get(item);
141: BigDecimal coverage = risk.getCoverage();
142: BigDecimal amount = risk.getAmount();
143: BigDecimal probability = risk.getProbability();
144: BigDecimal multiplied = amount.multiply(
145: probability)
146: .divide(new BigDecimal(100));
147: return risk.getKey() + " " + multiplied + " / "
148: + coverage;
149: }
150: }, new XYURLGenerator() {
151: public String generateURL(XYDataset xyDataset,
152: int series, int item) {
153: Risk risk = risks.get(item);
154: return "javascript:wingS.request.sendEvent(null,true,true,'"
155: + getName()
156: + "', '"
157: + risk.getId()
158: + "');";
159: }
160: });
161: /*
162: XYBubbleRenderer renderer = new XYBubbleRenderer();
163: renderer.setToolTipGenerator(new XYToolTipGenerator() {
164: public String generateToolTip(XYDataset dataset, int series, int item) {
165: Risk risk = risks.get(item);
166: BigDecimal coverage = risk.getCoverage();
167: BigDecimal amount = risk.getAmount();
168: BigDecimal probability = risk.getProbability();
169: BigDecimal multiplied = amount.multiply(probability).divide(new BigDecimal(100));
170: return risk.getKey() + " " + multiplied + " / " + coverage;
171: }
172: });
173: renderer.setURLGenerator(new XYURLGenerator() {
174: public String generateURL(XYDataset xyDataset, int series, int item) {
175: Risk risk = risks.get(item);
176: return "javascript:wingS.request.sendEvent(null,true,true,'" + getName() + "', '" + risk.getId() + "');";
177: }
178: });
179: */
180: renderer.setShapesFilled(Boolean.TRUE);
181: renderer.setPaint(Color.BLUE);
182: renderer.addAnnotation(new XYBoxAnnotation(0, 0, 40, 25,
183: dashed, gray, green), Layer.BACKGROUND);
184: renderer.addAnnotation(new XYBoxAnnotation(0, 25, 40, 50,
185: dashed, gray, green), Layer.BACKGROUND);
186: renderer.addAnnotation(new XYBoxAnnotation(40, 0, 80, 25,
187: dashed, gray, green), Layer.BACKGROUND);
188: renderer.addAnnotation(new XYBoxAnnotation(120, 75, 160, 100,
189: dashed, gray, red), Layer.BACKGROUND);
190: renderer.addAnnotation(new XYBoxAnnotation(120, 50, 160, 75,
191: dashed, gray, red), Layer.BACKGROUND);
192: renderer.addAnnotation(new XYBoxAnnotation(80, 75, 120, 100,
193: dashed, gray, red), Layer.BACKGROUND);
194:
195: XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
196:
197: JFreeChart chart = new JFreeChart(plot);
198: chart.setBackgroundPaint(getBackground());
199: return chart;
200: }
201:
202: /*
203: public static void main(String[] args) {
204:
205: XYSeries series = new XYSeries("Test Data");
206: series.add(10, 10);
207: series.add(10, 90);
208: series.add(90, 90);
209: series.add(90, 10);
210: series.add(50, 50);
211: XYDataset data = new XYSeriesCollection(series);
212:
213: ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
214:
215: JFreeChart chart = createRiskMap(
216: "Propability", "Amount",
217: null);
218:
219: BufferedImage bufferedImage = chart.createBufferedImage(400, 400, info);
220: String imageMap = ChartUtilities.getImageMap("test", info);
221: System.out.println("imageMap = " + imageMap);
222:
223: ChartPanel chartPanel = new ChartPanel(chart);
224: JFrame frame = new JFrame();
225: frame.setContentPane(chartPanel);
226: frame.setSize(new Dimension(400, 400));
227: frame.show();
228: }
229: */
230:
231: public String getImageMap() {
232: return imageMap;
233: }
234:
235: String navigate;
236:
237: public void processLowLevelEvent(String name, String[] values) {
238: System.out.println("name = " + name + " "
239: + Arrays.asList(values));
240: navigate = values[0];
241: SForm.addArmedComponent(this );
242: }
243:
244: public void fireIntermediateEvents() {
245: }
246:
247: public void fireFinalEvents() {
248: super .fireFinalEvents();
249: if (navigate != null) {
250: final ActionProvider sessionActionProvider = Client
251: .getInstance().getActionProvider();
252: ShowEditorAction action = (ShowEditorAction) sessionActionProvider
253: .getAction("org.concern.actions.subjects");
254: if (action != null && action.isEnabled()) {
255: Map query = new HashMap();
256: query.put("process", "RiskManagement");
257: query.put("subject", "" + navigate);
258: query.put("locale", ""
259: + SessionManager.getSession().getLocale()
260: .toString());
261: action.navigate(query);
262: }
263: navigate = null;
264: }
265: }
266:
267: public boolean isEpochCheckEnabled() {
268: return false;
269: }
270: }
|