Program: 71461
Project: 71461
Buggy source:
/** 
 * Computes the size of the legend. Note the computation relies on the title size, so the title block must be layouted first before this.
 * @param lg
 * @param sea
 * @throws GenerationException
 */
public final Size compute(IDisplayServer xs,Chart cm,SeriesDefinition[] seda,RunTimeContext rtc) throws ChartException {
  ITextMetrics itm=null;
  try {
    final Legend lg=cm.getLegend();
    LegendData legendData=new LegendData();
    if (!lg.isSetOrientation()) {
      throw new ChartException(ChartEnginePlugin.ID,ChartException.GENERATION,"String_Node_Str",Messages.getResourceBundle(xs.getULocale()));
    }
    if (!lg.isSetDirection()) {
      throw new ChartException(ChartEnginePlugin.ID,ChartException.GENERATION,"String_Node_Str",Messages.getResourceBundle(xs.getULocale()));
    }
    final Orientation orientation=lg.getOrientation();
    final Direction direction=lg.getDirection();
    final boolean bPaletteByCategory=(lg.getItemType().getValue() == LegendItemType.CATEGORIES);
    Label la=LabelImpl.create();
    la.setEllipsis(1);
    la.setCaption(TextImpl.copyInstance(lg.getText()));
    ClientArea ca=lg.getClientArea();
    LineAttributes lia=ca.getOutline();
    legendData.dSeparatorThickness=lia.getThickness();
    la.getCaption().setValue("String_Node_Str");
    itm=xs.getTextMetrics(la);
    legendData.dItemHeight=itm.getFullHeight();
    legendData.dScale=xs.getDpiResolution() / 72d;
    legendData.insCa=ca.getInsets().scaledInstance(legendData.dScale);
    legendData.maxWrappingSize=lg.getWrappingSize() * legendData.dScale;
    legendData.dHorizontalSpacing=3 * legendData.dScale;
    legendData.dVerticalSpacing=3 * legendData.dScale;
    legendData.dSafeSpacing=3 * legendData.dScale;
    legendData.dHorizonalReservedSpace=legendData.insCa.getLeft() + legendData.insCa.getRight() + (3 * legendData.dItemHeight) / 2 + legendData.dHorizontalSpacing;
    initAvailableSize(legendData,xs,cm);
    boolean bMinSliceDefined=false;
    if (cm instanceof ChartWithoutAxes) {
      bMinSliceDefined=((ChartWithoutAxes)cm).isSetMinSlice();
      legendData.sMinSliceLabel=((ChartWithoutAxes)cm).getMinSliceLabel();
      if (legendData.sMinSliceLabel == null || legendData.sMinSliceLabel.length() == 0) {
        legendData.sMinSliceLabel=IConstants.UNDEFINED_STRING;
      }
 else {
        legendData.sMinSliceLabel=rtc.externalizedMessage(legendData.sMinSliceLabel);
      }
    }
    if (bMinSliceDefined && bPaletteByCategory && cm instanceof ChartWithoutAxes) {
      calculateExtraLegend(cm,rtc,legendData);
    }
    Label lgTitle=lg.getTitle();
    Size titleSize=null;
    BoundingBox titleBounding=null;
    int iTitlePos=-1;
    if (lgTitle != null && lgTitle.isSetVisible() && lgTitle.isVisible()) {
      lgTitle=LabelImpl.copyInstance(lgTitle);
      final String sPreviousValue=lgTitle.getCaption().getValue();
      lgTitle.getCaption().setValue(rtc.externalizedMessage(sPreviousValue));
      try {
        titleBounding=Methods.computeBox(xs,IConstants.ABOVE,lgTitle,0,0);
      }
 catch (      IllegalArgumentException uiex) {
        throw new ChartException(ChartEnginePlugin.ID,ChartException.RENDERING,uiex);
      }
      iTitlePos=lg.getTitlePosition().getValue();
      if (rtc.isRightToLeft()) {
        if (iTitlePos == Position.LEFT) {
          iTitlePos=Position.RIGHT;
        }
 else         if (iTitlePos == Position.RIGHT) {
          iTitlePos=Position.LEFT;
        }
      }
      double shadowness=3 * legendData.dScale;
switch (iTitlePos) {
case Position.ABOVE:
case Position.BELOW:
        legendData.dAvailableHeight-=titleBounding.getHeight() + 2 * shadowness;
      break;
case Position.LEFT:
case Position.RIGHT:
    legendData.dAvailableWidth-=titleBounding.getWidth() + 2 * shadowness;
  break;
}
titleSize=SizeImpl.create(titleBounding.getWidth() + 2 * shadowness,titleBounding.getHeight() + 2 * shadowness);
}
double[] size=null;
boolean bNeedInvert=needInvert(bPaletteByCategory,cm,seda);
rtc.putState("String_Node_Str",Boolean.toString(bNeedInvert));
if (orientation.getValue() == Orientation.VERTICAL) {
if (bPaletteByCategory) {
size=computeVerticalByCategory(xs,cm,rtc,itm,la,legendData,bNeedInvert);
}
 else if (direction.getValue() == Direction.TOP_BOTTOM) {
size=computeVerticalByValue(xs,cm,seda,rtc,itm,la,legendData,bNeedInvert,false);
}
 else if (direction.getValue() == Direction.LEFT_RIGHT) {
size=computeVerticalByValue(xs,cm,seda,rtc,itm,la,legendData,bNeedInvert,true);
}
 else {
throw new ChartException(ChartEnginePlugin.ID,ChartException.GENERATION,"String_Node_Str",new Object[]{direction.getName()},Messages.getResourceBundle(xs.getULocale()));
}
}
 else if (orientation.getValue() == Orientation.HORIZONTAL) {
if (bPaletteByCategory) {
size=computeHorizalByCategory(xs,cm,rtc,itm,la,legendData,bNeedInvert);
}
 else if (direction.getValue() == Direction.TOP_BOTTOM) {
size=computeHorizalByValue(xs,cm,seda,rtc,itm,la,legendData,bNeedInvert,false);
}
 else if (direction.getValue() == Direction.LEFT_RIGHT) {
size=computeHorizalByValue(xs,cm,seda,rtc,itm,la,legendData,bNeedInvert,true);
}
 else {
throw new ChartException(ChartEnginePlugin.ID,ChartException.GENERATION,"String_Node_Str",new Object[]{direction},Messages.getResourceBundle(xs.getULocale()));
}
}
 else {
throw new ChartException(ChartEnginePlugin.ID,ChartException.GENERATION,"String_Node_Str",new Object[]{orientation},Messages.getResourceBundle(xs.getULocale()));
}
if (size == null) {
return SizeImpl.create(0,0);
}
double dWidth=size[0], dHeight=size[1];
if (iTitlePos != -1) {
double shadowness=3 * legendData.dScale;
switch (iTitlePos) {
case Position.ABOVE:
case Position.BELOW:
dHeight+=titleBounding.getHeight() + 2 * shadowness;
dWidth=Math.max(dWidth,titleBounding.getWidth() + 2 * shadowness);
break;
case Position.LEFT:
case Position.RIGHT:
dWidth+=titleBounding.getWidth() + 2 * shadowness;
dHeight=Math.max(dHeight,titleBounding.getHeight() + 2 * shadowness);
break;
}
}
if (rtc != null) {
List<LegendItemHints> legendItems=legendData.legendItems;
LegendItemHints[] liha=legendItems.toArray(new LegendItemHints[legendItems.size()]);
LegendLayoutHints lilh=new LegendLayoutHints(SizeImpl.create(dWidth,dHeight),titleSize,legendData.bMinSliceApplied,legendData.sMinSliceLabel,liha);
rtc.setLegendLayoutHints(lilh);
}
sz=SizeImpl.create(dWidth,dHeight);
}
  finally {
itm.dispose();
}
return sz;
}

Fixed version (ground truth):
/** 
 * Computes the size of the legend. Note the computation relies on the title size, so the title block must be layouted first before this.
 * @param lg
 * @param sea
 * @throws ChartException
 */
public final Size compute(IDisplayServer xs,Chart cm,SeriesDefinition[] seda,RunTimeContext rtc) throws ChartException {
  ITextMetrics itm=null;
  try {
    final Legend lg=cm.getLegend();
    LegendData legendData=new LegendData();
    if (!lg.isSetOrientation()) {
      throw new ChartException(ChartEnginePlugin.ID,ChartException.GENERATION,"String_Node_Str",Messages.getResourceBundle(xs.getULocale()));
    }
    if (!lg.isSetDirection()) {
      throw new ChartException(ChartEnginePlugin.ID,ChartException.GENERATION,"String_Node_Str",Messages.getResourceBundle(xs.getULocale()));
    }
    final Orientation orientation=lg.getOrientation();
    final Direction direction=lg.getDirection();
    final boolean bPaletteByCategory=(lg.getItemType().getValue() == LegendItemType.CATEGORIES);
    Label la=LabelImpl.create();
    la.setEllipsis(1);
    la.setCaption(TextImpl.copyInstance(lg.getText()));
    ClientArea ca=lg.getClientArea();
    LineAttributes lia=ca.getOutline();
    legendData.dSeparatorThickness=lia.getThickness();
    la.getCaption().setValue("String_Node_Str");
    itm=xs.getTextMetrics(la);
    legendData.dItemHeight=itm.getFullHeight();
    legendData.dScale=xs.getDpiResolution() / 72d;
    legendData.insCa=ca.getInsets().scaledInstance(legendData.dScale);
    legendData.maxWrappingSize=lg.getWrappingSize() * legendData.dScale;
    legendData.dHorizontalSpacing=3 * legendData.dScale;
    legendData.dVerticalSpacing=3 * legendData.dScale;
    legendData.dSafeSpacing=3 * legendData.dScale;
    legendData.dHorizonalReservedSpace=legendData.insCa.getLeft() + legendData.insCa.getRight() + (3 * legendData.dItemHeight) / 2 + legendData.dHorizontalSpacing;
    initAvailableSize(legendData,xs,cm);
    boolean bMinSliceDefined=false;
    if (cm instanceof ChartWithoutAxes) {
      bMinSliceDefined=((ChartWithoutAxes)cm).isSetMinSlice();
      legendData.sMinSliceLabel=((ChartWithoutAxes)cm).getMinSliceLabel();
      if (legendData.sMinSliceLabel == null || legendData.sMinSliceLabel.length() == 0) {
        legendData.sMinSliceLabel=IConstants.UNDEFINED_STRING;
      }
 else {
        legendData.sMinSliceLabel=rtc.externalizedMessage(legendData.sMinSliceLabel);
      }
    }
    if (bMinSliceDefined && bPaletteByCategory && cm instanceof ChartWithoutAxes) {
      calculateExtraLegend(cm,rtc,legendData);
    }
    Label lgTitle=lg.getTitle();
    Size titleSize=null;
    BoundingBox titleBounding=null;
    int iTitlePos=-1;
    if (lgTitle != null && lgTitle.isSetVisible() && lgTitle.isVisible()) {
      lgTitle=LabelImpl.copyInstance(lgTitle);
      final String sPreviousValue=lgTitle.getCaption().getValue();
      lgTitle.getCaption().setValue(rtc.externalizedMessage(sPreviousValue));
      try {
        titleBounding=Methods.computeBox(xs,IConstants.ABOVE,lgTitle,0,0);
      }
 catch (      IllegalArgumentException uiex) {
        throw new ChartException(ChartEnginePlugin.ID,ChartException.RENDERING,uiex);
      }
      iTitlePos=lg.getTitlePosition().getValue();
      if (rtc.isRightToLeft()) {
        if (iTitlePos == Position.LEFT) {
          iTitlePos=Position.RIGHT;
        }
 else         if (iTitlePos == Position.RIGHT) {
          iTitlePos=Position.LEFT;
        }
      }
      double shadowness=3 * legendData.dScale;
switch (iTitlePos) {
case Position.ABOVE:
case Position.BELOW:
        legendData.dAvailableHeight-=titleBounding.getHeight() + 2 * shadowness;
      break;
case Position.LEFT:
case Position.RIGHT:
    legendData.dAvailableWidth-=titleBounding.getWidth() + 2 * shadowness;
  break;
}
titleSize=SizeImpl.create(titleBounding.getWidth() + 2 * shadowness,titleBounding.getHeight() + 2 * shadowness);
}
double[] size=null;
boolean bNeedInvert=needInvert(bPaletteByCategory,cm,seda);
rtc.putState("String_Node_Str",Boolean.toString(bNeedInvert));
if (orientation.getValue() == Orientation.VERTICAL) {
if (bPaletteByCategory) {
size=computeVerticalByCategory(xs,cm,rtc,itm,la,legendData,bNeedInvert);
}
 else if (direction.getValue() == Direction.TOP_BOTTOM) {
size=computeVerticalByValue(xs,cm,seda,rtc,itm,la,legendData,bNeedInvert,false);
}
 else if (direction.getValue() == Direction.LEFT_RIGHT) {
size=computeVerticalByValue(xs,cm,seda,rtc,itm,la,legendData,bNeedInvert,true);
}
 else {
throw new ChartException(ChartEnginePlugin.ID,ChartException.GENERATION,"String_Node_Str",new Object[]{direction.getName()},Messages.getResourceBundle(xs.getULocale()));
}
}
 else if (orientation.getValue() == Orientation.HORIZONTAL) {
if (bPaletteByCategory) {
size=computeHorizalByCategory(xs,cm,rtc,itm,la,legendData,bNeedInvert);
}
 else if (direction.getValue() == Direction.TOP_BOTTOM) {
size=computeHorizalByValue(xs,cm,seda,rtc,itm,la,legendData,bNeedInvert,false);
}
 else if (direction.getValue() == Direction.LEFT_RIGHT) {
size=computeHorizalByValue(xs,cm,seda,rtc,itm,la,legendData,bNeedInvert,true);
}
 else {
throw new ChartException(ChartEnginePlugin.ID,ChartException.GENERATION,"String_Node_Str",new Object[]{direction},Messages.getResourceBundle(xs.getULocale()));
}
}
 else {
throw new ChartException(ChartEnginePlugin.ID,ChartException.GENERATION,"String_Node_Str",new Object[]{orientation},Messages.getResourceBundle(xs.getULocale()));
}
if (size == null) {
return SizeImpl.create(0,0);
}
double dWidth=size[0], dHeight=size[1];
if (iTitlePos != -1) {
double shadowness=3 * legendData.dScale;
switch (iTitlePos) {
case Position.ABOVE:
case Position.BELOW:
dHeight+=titleBounding.getHeight() + 2 * shadowness;
dWidth=Math.max(dWidth,titleBounding.getWidth() + 2 * shadowness);
break;
case Position.LEFT:
case Position.RIGHT:
dWidth+=titleBounding.getWidth() + 2 * shadowness;
dHeight=Math.max(dHeight,titleBounding.getHeight() + 2 * shadowness);
break;
}
}
if (rtc != null) {
List<LegendItemHints> legendItems=legendData.legendItems;
LegendItemHints[] liha=legendItems.toArray(new LegendItemHints[legendItems.size()]);
LegendLayoutHints lilh=new LegendLayoutHints(SizeImpl.create(dWidth,dHeight),titleSize,legendData.bMinSliceApplied,legendData.sMinSliceLabel,liha);
rtc.setLegendLayoutHints(lilh);
}
sz=SizeImpl.create(dWidth,dHeight);
}
  finally {
itm.dispose();
}
return sz;
}