package com.ibm.nmon.gui.chart;

import java.util.Set;

import java.awt.Font;

import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import org.jfree.chart.JFreeChart;

import org.jfree.chart.entity.CategoryItemEntity;

import org.jfree.chart.ChartRenderingInfo;

import org.jfree.chart.plot.Plot;
import org.jfree.chart.plot.CategoryPlot;

import org.jfree.chart.renderer.category.CategoryItemRenderer;

public final class HighlightableBarChart extends JFreeChart {
    private static final long serialVersionUID = -5389731065593870345L;

    private final Set<CategoryItemEntity> selectedEntities = new java.util.HashSet<CategoryItemEntity>();

    public HighlightableBarChart(Plot plot) {
        super(plot);
    }

    public HighlightableBarChart(String title, Plot plot) {
        super(title, plot);
    }

    public HighlightableBarChart(String title, Font titleFont, Plot plot, boolean createLegend) {
        super(title, titleFont, plot, createLegend);
    }

    void highlightEntity(CategoryItemEntity entity) {
        selectedEntities.add(entity);
    }

    boolean isHighlighted(CategoryItemEntity entity) {
        return selectedEntities.contains(entity);
    }

    public void clearHighlights() {
        selectedEntities.clear();
    }

    @Override
    public void draw(Graphics2D g2, Rectangle2D chartArea, Point2D anchor, ChartRenderingInfo info) {
        super.draw(g2, chartArea, anchor, info);

        // for each CategoryItem, highlight the bar
        // this has to be done during painting so it carries over on repaints
        for (CategoryItemEntity entity : selectedEntities) {
            Rectangle2D area = ((Rectangle2D) entity.getArea()).createIntersection(info.getPlotInfo().getDataArea());

            CategoryItemRenderer renderer = ((CategoryPlot) getPlot()).getRenderer();
            int row = entity.getDataset().getRowIndex(entity.getRowKey());
            int column = entity.getDataset().getColumnIndex(entity.getColumnKey());

            java.awt.Color baseColor = (java.awt.Color) renderer.getItemPaint(row, column);

            // redraw the bar with the base color
            // assuming bar was drawn with SimpleGradientBarPainter, this will be a flat color
            g2.setPaint(baseColor);
            g2.fill(area);

            // draw a brighter outline around the bar
            g2.setStroke(renderer.getBaseOutlineStroke());
            g2.setPaint(baseColor.darker());
            g2.draw(area);
        }
    }
}

@@ -1510,24 +1510,57 @@
 , info);%0D%0A%0D%0A
+        if (info != null) %7B%0D%0A    
         // f
@@ -1608,16 +1608,20 @@
         
+ 
+   
 // this 
@@ -1691,16 +1691,20 @@
         
+    
 for (Cat
@@ -1761,16 +1761,20 @@
         
+  
+  
 Rectangl
@@ -1816,16 +1816,42 @@
 tArea())
+%0D%0A                        
 .createI
@@ -1892,32 +1892,36 @@
 DataArea());%0D%0A%0D%0A
+    
             Cate
@@ -1999,24 +1999,28 @@
             
+ 
+   
 int row = en
@@ -2067,24 +2067,28 @@
 RowKey());%0D%0A
+    
             
@@ -2174,16 +2174,20 @@
         
+ 
+   
 java.awt
@@ -2273,16 +2273,20 @@
         
+    
 // redra
@@ -2328,16 +2328,20 @@
         
+  
+  
 // assum
@@ -2420,32 +2420,36 @@
 or%0D%0A            
+    
 g2.setPaint(base
@@ -2469,16 +2469,20 @@
         
+  
+  
 g2.fill(
@@ -2487,24 +2487,28 @@
 l(area);%0D%0A%0D%0A
+    
             
@@ -2558,24 +2558,28 @@
             
+   
+ 
 g2.setStroke
@@ -2618,32 +2618,36 @@
 );%0D%0A            
+    
 g2.setPaint(base
@@ -2676,16 +2676,20 @@
         
+   
+ 
 g2.draw(
@@ -2692,24 +2692,39 @@
 raw(area);%0D%0A
+            %7D%0D%0A
         %7D%0D%0A 

Fix for saving with highlighted bars