Program: 71761
Project: 71761
Buggy source:
/** 
 * Renders the legend block based on the legend rendering rules.
 * @param ipr
 * @param lg
 * @param htRenderers
 * @throws ChartException
 */
public void renderLegend(IPrimitiveRenderer ipr,Legend lg,Map htRenderers) throws ChartException {
  if (!lg.isVisible()) {
    return;
  }
  renderBlock(ipr,lg,StructureSource.createLegend(lg));
  final IDisplayServer xs=getDevice().getDisplayServer();
  final double dScale=getDeviceScale();
  Bounds bo=lg.getBounds().scaledInstance(dScale);
  Size sz=null;
  double dX, dY;
  if (lg.getPosition() != Position.INSIDE_LITERAL) {
    try {
      sz=lg.getPreferredSize(xs,cm,rtc);
    }
 catch (    Exception ex) {
      throw new ChartException(ChartEnginePlugin.ID,ChartException.RENDERING,ex);
    }
    sz.scale(dScale);
    dX=bo.getLeft() + (bo.getWidth() - sz.getWidth()) / 2;
    dY=0;
    if (lg.isSetAnchor()) {
      int iAnchor=lg.getAnchor().getValue();
      if (isRightToLeft()) {
        if (iAnchor == Anchor.EAST) {
          iAnchor=Anchor.WEST;
        }
 else         if (iAnchor == Anchor.NORTH_EAST) {
          iAnchor=Anchor.NORTH_WEST;
        }
 else         if (iAnchor == Anchor.SOUTH_EAST) {
          iAnchor=Anchor.SOUTH_WEST;
        }
 else         if (iAnchor == Anchor.WEST) {
          iAnchor=Anchor.EAST;
        }
 else         if (iAnchor == Anchor.NORTH_WEST) {
          iAnchor=Anchor.NORTH_EAST;
        }
 else         if (iAnchor == Anchor.SOUTH_WEST) {
          iAnchor=Anchor.SOUTH_EAST;
        }
      }
switch (iAnchor) {
case Anchor.NORTH:
case Anchor.NORTH_EAST:
case Anchor.NORTH_WEST:
        dY=bo.getTop();
      break;
case Anchor.SOUTH:
case Anchor.SOUTH_EAST:
case Anchor.SOUTH_WEST:
    dY=bo.getTop() + bo.getHeight() - sz.getHeight();
  break;
default :
dY=bo.getTop() + (bo.getHeight() - sz.getHeight()) / 2;
break;
}
switch (iAnchor) {
case Anchor.WEST:
case Anchor.NORTH_WEST:
case Anchor.SOUTH_WEST:
dX=bo.getLeft();
break;
case Anchor.EAST:
case Anchor.SOUTH_EAST:
case Anchor.NORTH_EAST:
dX=bo.getLeft() + bo.getWidth() - sz.getWidth();
break;
default :
dX=bo.getLeft() + (bo.getWidth() - sz.getWidth()) / 2;
break;
}
}
 else {
dX=bo.getLeft() + (bo.getWidth() - sz.getWidth()) / 2;
dY=bo.getTop() + (bo.getHeight() - sz.getHeight()) / 2;
}
}
 else {
dX=bo.getLeft();
dY=bo.getTop();
sz=SizeImpl.create(bo.getWidth(),bo.getHeight());
}
final LegendLayoutHints lilh=rtc.getLegendLayoutHints();
if (lilh == null) {
throw new ChartException(ChartEnginePlugin.ID,ChartException.RENDERING,"String_Node_Str",Messages.getResourceBundle(rtc.getULocale()));
}
Label lgTitle=lg.getTitle();
double lgTitleWidth=0, lgTitleHeight=0;
double yOffset=0, xOffset=0, wOffset=0, hOffset=0;
final boolean bRenderLegendTitle=lgTitle != null && lgTitle.isSetVisible() && lgTitle.isVisible();
int iTitlePos=Position.ABOVE;
if (bRenderLegendTitle) {
lgTitle=LabelImpl.copyInstance(lgTitle);
final String sPreviousValue=lgTitle.getCaption().getValue();
lgTitle.getCaption().setValue(rtc.externalizedMessage(sPreviousValue));
Size titleSize=lilh.getTitleSize();
lgTitleWidth=titleSize.getWidth();
lgTitleHeight=titleSize.getHeight();
iTitlePos=lg.getTitlePosition().getValue();
if (isRightToLeft()) {
if (iTitlePos == Position.LEFT) {
iTitlePos=Position.RIGHT;
}
 else if (iTitlePos == Position.RIGHT) {
iTitlePos=Position.LEFT;
}
}
switch (iTitlePos) {
case Position.ABOVE:
yOffset=lgTitleHeight;
hOffset=-yOffset;
break;
case Position.BELOW:
hOffset=-lgTitleHeight;
break;
case Position.LEFT:
xOffset=lgTitleWidth;
wOffset=-xOffset;
break;
case Position.RIGHT:
wOffset=-lgTitleWidth;
break;
}
}
final ClientArea ca=lg.getClientArea();
final Insets lgIns=lg.getInsets().scaledInstance(dScale);
LineAttributes lia=ca.getOutline();
bo=BoundsImpl.create(dX,dY,sz.getWidth(),sz.getHeight());
bo=bo.adjustedInstance(lgIns);
dX=bo.getLeft();
dY=bo.getTop();
bo.delta(xOffset,yOffset,wOffset,hOffset);
dX=bo.getLeft();
dY=bo.getTop();
final double dBaseX=dX;
final double dBaseY=dY;
final RectangleRenderEvent rre=(RectangleRenderEvent)((EventObjectCache)ir).getEventObject(StructureSource.createLegend(lg),RectangleRenderEvent.class);
if (ca.getShadowColor() != null) {
rre.setBounds(bo.translateInstance(3,3));
rre.setBackground(ca.getShadowColor());
ipr.fillRectangle(rre);
}
rre.setBounds(bo);
rre.setOutline(lia);
rre.setBackground(ca.getBackground());
ipr.fillRectangle(rre);
ipr.drawRectangle(rre);
lia=LineAttributesImpl.copyInstance(lia);
lia.setVisible(true);
LineAttributes liSep=lg.getSeparator() == null ? lia : lg.getSeparator();
final SeriesDefinition[] seda=cm.getSeriesForLegend();
final Orientation o=lg.getOrientation();
final Direction d=lg.getDirection();
final Label la=LabelImpl.create();
la.setCaption(TextImpl.copyInstance(lg.getText()));
la.getCaption().setValue("String_Node_Str");
final ITextMetrics itm=xs.getTextMetrics(la);
try {
final double dItemHeight=itm.getFullHeight();
final double dHorizontalSpacing=4;
Insets insCA=ca.getInsets().scaledInstance(dScale);
Series seBase;
List al;
LegendItemRenderingHints lirh;
Palette pa;
int iPaletteCount;
EList elPaletteEntries;
Fill fPaletteEntry;
final boolean bPaletteByCategory=(cm.getLegend().getItemType().getValue() == LegendItemType.CATEGORIES);
String strNeedInvert=(String)rtc.getState("String_Node_Str");
boolean bNeedInvert=Boolean.valueOf(strNeedInvert).booleanValue();
if (o.getValue() == Orientation.VERTICAL) {
if (bPaletteByCategory) {
SeriesDefinition sdBase=null;
if (cm instanceof ChartWithAxes) {
final Axis axPrimaryBase=((ChartWithAxes)cm).getBaseAxes()[0];
if (axPrimaryBase.getSeriesDefinitions().isEmpty()) {
return;
}
sdBase=(SeriesDefinition)axPrimaryBase.getSeriesDefinitions().get(0);
}
 else if (cm instanceof ChartWithoutAxes) {
if (((ChartWithoutAxes)cm).getSeriesDefinitions().isEmpty()) {
return;
}
sdBase=(SeriesDefinition)((ChartWithoutAxes)cm).getSeriesDefinitions().get(0);
}
seBase=(Series)sdBase.getRunTimeSeries().get(0);
pa=sdBase.getSeriesPalette();
elPaletteEntries=pa.getEntries();
iPaletteCount=elPaletteEntries.size();
if (lilh.getLegendItemHints() == null) {
throw new ChartException(ChartEnginePlugin.ID,ChartException.RENDERING,"String_Node_Str",Messages.getResourceBundle(rtc.getULocale()));
}
LegendItemHints[] liha=lilh.getLegendItemHints();
LegendItemHints lih;
Map columnCache=searchMaxColumnWidth(liha);
for (int i=0; i < liha.length; i++) {
lih=liha[i];
if ((lih.getType() & IConstants.LEGEND_ENTRY) == IConstants.LEGEND_ENTRY) {
la.getCaption().setValue(lih.getText());
fPaletteEntry=(Fill)elPaletteEntries.get(lih.getCategoryIndex() % iPaletteCount);
lirh=(LegendItemRenderingHints)htRenderers.get(seBase);
double columnWidth=bo.getWidth();
Double cachedWidth=(Double)columnCache.get(lih);
if (cachedWidth != null) {
columnWidth=cachedWidth.doubleValue() + 3 * dItemHeight / 2 + 2 * insCA.getLeft();
}
renderLegendItem(ipr,lg,la,null,dBaseX + lih.getLeft(),dBaseY + lih.getTop() + insCA.getTop(),lih.getWidth(),dItemHeight,lih.getHeight(),0,columnWidth,insCA.getLeft(),dHorizontalSpacing,seBase,fPaletteEntry,lirh,lih.getCategoryIndex(),dScale);
}
}
}
 else if (d.getValue() == Direction.TOP_BOTTOM) {
if (lilh.getLegendItemHints() == null) {
throw new ChartException(ChartEnginePlugin.ID,ChartException.RENDERING,"String_Node_Str",Messages.getResourceBundle(rtc.getULocale()));
}
LegendItemHints[] liha=lilh.getLegendItemHints();
LegendItemHints lih;
int k=0;
Map columnCache=searchMaxColumnWidth(liha);
for (int j=0; j < seda.length; j++) {
int iSedaId=bNeedInvert ? seda.length - 1 - j : j;
al=seda[iSedaId].getRunTimeSeries();
pa=seda[iSedaId].getSeriesPalette();
elPaletteEntries=pa.getEntries();
iPaletteCount=elPaletteEntries.size();
for (int i=0; i < al.size(); i++) {
int iSeId=bNeedInvert ? al.size() - 1 - i : i;
seBase=(Series)al.get(iSeId);
if (!seBase.isVisible()) {
continue;
}
lirh=(LegendItemRenderingHints)htRenderers.get(seBase);
if (k < liha.length) {
lih=liha[k++];
if (lih.getType() == IConstants.LEGEND_ENTRY) {
la.getCaption().setValue(lih.getText());
Label valueLa=null;
if (lg.isShowValue()) {
valueLa=LabelImpl.copyInstance(seBase.getLabel());
valueLa.getCaption().setValue(lih.getExtraText());
valueLa.setVisible(true);
}
fPaletteEntry=(Fill)elPaletteEntries.get(lih.getCategoryIndex() % iPaletteCount);
double columnWidth=bo.getWidth();
Double cachedWidth=(Double)columnCache.get(lih);
if (cachedWidth != null) {
columnWidth=cachedWidth.doubleValue() + 3 * dItemHeight / 2 + 2 * insCA.getLeft();
}
renderLegendItem(ipr,lg,la,valueLa,dBaseX + lih.getLeft(),dBaseY + lih.getTop() + insCA.getTop(),lih.getWidth(),dItemHeight,lih.getHeight(),lih.getExtraHeight(),columnWidth,insCA.getLeft(),dHorizontalSpacing,seBase,fPaletteEntry,lirh,i,dScale);
}
}
}
if (j < seda.length - 1 && k < liha.length) {
lih=liha[k];
if (lih.getType() == IConstants.LEGEND_SEPERATOR) {
k++;
renderSeparator(ipr,lg,liSep,dBaseX + lih.getLeft(),dBaseY + lih.getTop(),lih.getWidth(),Orientation.HORIZONTAL_LITERAL);
}
}
}
}
 else if (d.getValue() == Direction.LEFT_RIGHT) {
if (lilh.getLegendItemHints() == null) {
throw new ChartException(ChartEnginePlugin.ID,ChartException.RENDERING,"String_Node_Str",Messages.getResourceBundle(rtc.getULocale()));
}
LegendItemHints[] liha=lilh.getLegendItemHints();
LegendItemHints lih;
int k=0;
Map columnCache=searchMaxColumnWidth(liha);
for (int j=0; j < seda.length; j++) {
int iSedaId=bNeedInvert ? seda.length - 1 - j : j;
al=seda[iSedaId].getRunTimeSeries();
pa=seda[iSedaId].getSeriesPalette();
elPaletteEntries=pa.getEntries();
iPaletteCount=elPaletteEntries.size();
for (int i=0; i < al.size(); i++) {
int iSeId=bNeedInvert ? al.size() - 1 - i : i;
seBase=(Series)al.get(iSeId);
if (!seBase.isVisible()) {
continue;
}
lirh=(LegendItemRenderingHints)htRenderers.get(seBase);
if (k < liha.length) {
lih=liha[k++];
if (lih.getType() == IConstants.LEGEND_ENTRY) {
la.getCaption().setValue(lih.getText());
Label valueLa=null;
if (lg.isShowValue()) {
valueLa=LabelImpl.copyInstance(seBase.getLabel());
valueLa.getCaption().setValue(lih.getExtraText());
valueLa.setVisible(true);
}
fPaletteEntry=(Fill)elPaletteEntries.get(lih.getCategoryIndex() % iPaletteCount);
double columnWidth=bo.getWidth();
Double cachedWidth=(Double)columnCache.get(lih);
if (cachedWidth != null) {
columnWidth=cachedWidth.doubleValue() + 3 * dItemHeight / 2 + 2 * insCA.getLeft();
}
renderLegendItem(ipr,lg,la,valueLa,dBaseX + lih.getLeft(),dBaseY + lih.getTop() + insCA.getTop(),lih.getWidth(),dItemHeight,lih.getHeight(),lih.getExtraHeight(),columnWidth,insCA.getLeft(),dHorizontalSpacing,seBase,fPaletteEntry,lirh,i,dScale);
}
}
}
if (j < seda.length - 1 && k < liha.length) {
lih=liha[k];
if (lih.getType() == IConstants.LEGEND_SEPERATOR) {
k++;
renderSeparator(ipr,lg,liSep,dBaseX + lih.getLeft(),dBaseY + lih.getTop(),bo.getHeight(),Orientation.VERTICAL_LITERAL);
}
}
}
}
 else {
throw new ChartException(ChartEnginePlugin.ID,ChartException.RENDERING,"String_Node_Str",new Object[]{d.getName()},Messages.getResourceBundle(rtc.getULocale()));
}
}
 else if (o.getValue() == Orientation.HORIZONTAL) {
if (bPaletteByCategory) {
SeriesDefinition sdBase=null;
if (cm instanceof ChartWithAxes) {
final Axis axPrimaryBase=((ChartWithAxes)cm).getBaseAxes()[0];
if (axPrimaryBase.getSeriesDefinitions().isEmpty()) {
return;
}
sdBase=(SeriesDefinition)axPrimaryBase.getSeriesDefinitions().get(0);
}
 else if (cm instanceof ChartWithoutAxes) {
if (((ChartWithoutAxes)cm).getSeriesDefinitions().isEmpty()) {
return;
}
sdBase=(SeriesDefinition)((ChartWithoutAxes)cm).getSeriesDefinitions().get(0);
}
seBase=(Series)sdBase.getRunTimeSeries().get(0);
pa=sdBase.getSeriesPalette();
elPaletteEntries=pa.getEntries();
iPaletteCount=elPaletteEntries.size();
if (lilh.getLegendItemHints() == null) {
throw new ChartException(ChartEnginePlugin.ID,ChartException.RENDERING,"String_Node_Str",Messages.getResourceBundle(rtc.getULocale()));
}
LegendItemHints[] liha=lilh.getLegendItemHints();
LegendItemHints lih;
for (int i=0; i < liha.length; i++) {
lih=liha[i];
if ((lih.getType() & IConstants.LEGEND_ENTRY) == IConstants.LEGEND_ENTRY) {
la.getCaption().setValue(lih.getText());
fPaletteEntry=(Fill)elPaletteEntries.get(lih.getCategoryIndex() % iPaletteCount);
lirh=(LegendItemRenderingHints)htRenderers.get(seBase);
renderLegendItem(ipr,lg,la,null,dBaseX + lih.getLeft(),dBaseY + lih.getTop() + insCA.getTop(),lih.getWidth(),dItemHeight,lih.getHeight(),0,lih.getWidth() + 3 * dItemHeight / 2 + 2 * insCA.getLeft(),insCA.getLeft(),dHorizontalSpacing,seBase,fPaletteEntry,lirh,lih.getCategoryIndex(),dScale);
}
}
}
 else if (d.getValue() == Direction.TOP_BOTTOM) {
if (lilh.getLegendItemHints() == null) {
throw new ChartException(ChartEnginePlugin.ID,ChartException.RENDERING,"String_Node_Str",Messages.getResourceBundle(rtc.getULocale()));
}
LegendItemHints[] liha=lilh.getLegendItemHints();
LegendItemHints lih;
int k=0;
for (int j=0; j < seda.length; j++) {
int iSedaId=bNeedInvert ? seda.length - 1 - j : j;
al=seda[iSedaId].getRunTimeSeries();
pa=seda[iSedaId].getSeriesPalette();
elPaletteEntries=pa.getEntries();
iPaletteCount=elPaletteEntries.size();
for (int i=0; i < al.size(); i++) {
int iSeId=bNeedInvert ? al.size() - 1 - i : i;
seBase=(Series)al.get(iSeId);
if (!seBase.isVisible()) {
continue;
}
lirh=(LegendItemRenderingHints)htRenderers.get(seBase);
if (k < liha.length) {
lih=liha[k++];
if (lih.getType() == IConstants.LEGEND_ENTRY) {
la.getCaption().setValue(lih.getText());
Label valueLa=null;
if (lg.isShowValue()) {
valueLa=LabelImpl.copyInstance(seBase.getLabel());
valueLa.getCaption().setValue(lih.getExtraText());
valueLa.setVisible(true);
}
fPaletteEntry=(Fill)elPaletteEntries.get(lih.getCategoryIndex() % iPaletteCount);
renderLegendItem(ipr,lg,la,valueLa,dBaseX + lih.getLeft(),dBaseY + lih.getTop() + insCA.getTop(),lih.getWidth(),dItemHeight,lih.getHeight(),lih.getExtraHeight(),lih.getWidth() + 3 * dItemHeight / 2 + 2 * insCA.getLeft(),insCA.getLeft(),dHorizontalSpacing,seBase,fPaletteEntry,lirh,i,dScale);
}
}
}
if (j < seda.length - 1 && k < liha.length) {
lih=liha[k];
if (lih.getType() == IConstants.LEGEND_SEPERATOR) {
k++;
renderSeparator(ipr,lg,liSep,dBaseX + lih.getLeft(),dBaseY + lih.getTop(),bo.getWidth(),Orientation.HORIZONTAL_LITERAL);
}
}
}
}
 else if (d.getValue() == Direction.LEFT_RIGHT) {
if (lilh.getLegendItemHints() == null) {
throw new ChartException(ChartEnginePlugin.ID,ChartException.RENDERING,"String_Node_Str",Messages.getResourceBundle(rtc.getULocale()));
}
LegendItemHints[] liha=lilh.getLegendItemHints();
LegendItemHints lih;
int k=0;
for (int j=0; j < seda.length; j++) {
int iSedaId=bNeedInvert ? seda.length - 1 - j : j;
al=seda[iSedaId].getRunTimeSeries();
pa=seda[iSedaId].getSeriesPalette();
elPaletteEntries=pa.getEntries();
iPaletteCount=elPaletteEntries.size();
for (int i=0; i < al.size(); i++) {
int iSeId=bNeedInvert ? al.size() - 1 - i : i;
seBase=(Series)al.get(iSeId);
if (!seBase.isVisible()) {
continue;
}
lirh=(LegendItemRenderingHints)htRenderers.get(seBase);
if (k < liha.length) {
lih=liha[k++];
if (lih.getType() == IConstants.LEGEND_ENTRY) {
la.getCaption().setValue(lih.getText());
Label valueLa=null;
if (lg.isShowValue()) {
valueLa=LabelImpl.copyInstance(seBase.getLabel());
valueLa.getCaption().setValue(lih.getExtraText());
valueLa.setVisible(true);
}
fPaletteEntry=(Fill)elPaletteEntries.get(lih.getCategoryIndex() % iPaletteCount);
renderLegendItem(ipr,lg,la,valueLa,dBaseX + lih.getLeft(),dBaseY + lih.getTop() + insCA.getTop(),lih.getWidth(),dItemHeight,lih.getHeight(),lih.getExtraHeight(),lih.getWidth() + 3 * dItemHeight / 2 + 2 * insCA.getLeft(),insCA.getLeft(),dHorizontalSpacing,seBase,fPaletteEntry,lirh,i,dScale);
}
}
}
if (j < seda.length - 1 && k < liha.length) {
lih=liha[k];
if (lih.getType() == IConstants.LEGEND_SEPERATOR) {
k++;
renderSeparator(ipr,lg,liSep,dBaseX + lih.getLeft(),dBaseY + lih.getTop(),lih.getHeight(),Orientation.VERTICAL_LITERAL);
}
}
}
}
 else {
throw new ChartException(ChartEnginePlugin.ID,ChartException.RENDERING,"String_Node_Str",new Object[]{d.getName()},Messages.getResourceBundle(rtc.getULocale()));
}
}
 else {
throw new ChartException(ChartEnginePlugin.ID,ChartException.RENDERING,"String_Node_Str",new Object[]{o.getName()},Messages.getResourceBundle(rtc.getULocale()));
}
if (bRenderLegendTitle) {
double lX=bo.getLeft();
double lY=bo.getTop();
switch (iTitlePos) {
case Position.ABOVE:
lX=bo.getLeft() + (bo.getWidth() - lgTitleWidth) / 2d;
lY=bo.getTop() - lgTitleHeight;
break;
case Position.BELOW:
lX=bo.getLeft() + (bo.getWidth() - lgTitleWidth) / 2d;
lY=bo.getTop() + bo.getHeight();
break;
case Position.LEFT:
lX=bo.getLeft() - lgTitleWidth;
lY=bo.getTop() + (bo.getHeight() - lgTitleHeight) / 2d;
break;
case Position.RIGHT:
lX=bo.getLeft() + bo.getWidth();
lY=bo.getTop() + (bo.getHeight() - lgTitleHeight) / 2d;
break;
}
final TextRenderEvent tre=(TextRenderEvent)((EventObjectCache)ir).getEventObject(WrappedStructureSource.createLegendTitle(lg,lgTitle),TextRenderEvent.class);
tre.setBlockBounds(BoundsImpl.create(lX,lY,lgTitleWidth,lgTitleHeight));
TextAlignment ta=TextAlignmentImpl.create();
ta.setHorizontalAlignment(HorizontalAlignment.CENTER_LITERAL);
ta.setVerticalAlignment(VerticalAlignment.CENTER_LITERAL);
tre.setBlockAlignment(ta);
tre.setLabel(lgTitle);
tre.setAction(TextRenderEvent.RENDER_TEXT_IN_BLOCK);
if (rtc.isRightToLeftText()) {
tre.setRtlCaption();
}
ipr.drawText(tre);
}
}
  finally {
itm.dispose();
}
}

Fixed version (ground truth):
/** 
 * Renders the legend block based on the legend rendering rules.
 * @param ipr
 * @param lg
 * @param htRenderers
 * @throws ChartException
 */
public void renderLegend(IPrimitiveRenderer ipr,Legend lg,Map htRenderers) throws ChartException {
  if (!lg.isVisible()) {
    return;
  }
  renderBlock(ipr,lg,StructureSource.createLegend(lg));
  final IDisplayServer xs=getDevice().getDisplayServer();
  final double dScale=getDeviceScale();
  Bounds bo=lg.getBounds().scaledInstance(dScale);
  Size sz=null;
  double dX, dY;
  if (lg.getPosition() != Position.INSIDE_LITERAL) {
    try {
      sz=lg.getPreferredSize(xs,cm,rtc);
    }
 catch (    Exception ex) {
      throw new ChartException(ChartEnginePlugin.ID,ChartException.RENDERING,ex);
    }
    sz.scale(dScale);
    dX=bo.getLeft() + (bo.getWidth() - sz.getWidth()) / 2;
    dY=0;
    if (lg.isSetAnchor()) {
      int iAnchor=lg.getAnchor().getValue();
      if (isRightToLeft()) {
        if (iAnchor == Anchor.EAST) {
          iAnchor=Anchor.WEST;
        }
 else         if (iAnchor == Anchor.NORTH_EAST) {
          iAnchor=Anchor.NORTH_WEST;
        }
 else         if (iAnchor == Anchor.SOUTH_EAST) {
          iAnchor=Anchor.SOUTH_WEST;
        }
 else         if (iAnchor == Anchor.WEST) {
          iAnchor=Anchor.EAST;
        }
 else         if (iAnchor == Anchor.NORTH_WEST) {
          iAnchor=Anchor.NORTH_EAST;
        }
 else         if (iAnchor == Anchor.SOUTH_WEST) {
          iAnchor=Anchor.SOUTH_EAST;
        }
      }
switch (iAnchor) {
case Anchor.NORTH:
case Anchor.NORTH_EAST:
case Anchor.NORTH_WEST:
        dY=bo.getTop();
      break;
case Anchor.SOUTH:
case Anchor.SOUTH_EAST:
case Anchor.SOUTH_WEST:
    dY=bo.getTop() + bo.getHeight() - sz.getHeight();
  break;
default :
dY=bo.getTop() + (bo.getHeight() - sz.getHeight()) / 2;
break;
}
switch (iAnchor) {
case Anchor.WEST:
case Anchor.NORTH_WEST:
case Anchor.SOUTH_WEST:
dX=bo.getLeft();
break;
case Anchor.EAST:
case Anchor.SOUTH_EAST:
case Anchor.NORTH_EAST:
dX=bo.getLeft() + bo.getWidth() - sz.getWidth();
break;
default :
dX=bo.getLeft() + (bo.getWidth() - sz.getWidth()) / 2;
break;
}
}
 else {
dX=bo.getLeft() + (bo.getWidth() - sz.getWidth()) / 2;
dY=bo.getTop() + (bo.getHeight() - sz.getHeight()) / 2;
}
}
 else {
dX=bo.getLeft();
dY=bo.getTop();
sz=SizeImpl.create(bo.getWidth(),bo.getHeight());
}
final LegendLayoutHints lilh=rtc.getLegendLayoutHints();
if (lilh == null) {
throw new ChartException(ChartEnginePlugin.ID,ChartException.RENDERING,"String_Node_Str",Messages.getResourceBundle(rtc.getULocale()));
}
Label lgTitle=lg.getTitle();
double lgTitleWidth=0, lgTitleHeight=0;
double yOffset=0, xOffset=0, wOffset=0, hOffset=0;
final boolean bRenderLegendTitle=lgTitle != null && lgTitle.isSetVisible() && lgTitle.isVisible();
int iTitlePos=Position.ABOVE;
if (bRenderLegendTitle) {
lgTitle=LabelImpl.copyInstance(lgTitle);
final String sPreviousValue=lgTitle.getCaption().getValue();
lgTitle.getCaption().setValue(rtc.externalizedMessage(sPreviousValue));
Size titleSize=lilh.getTitleSize();
lgTitleWidth=titleSize.getWidth();
lgTitleHeight=titleSize.getHeight();
iTitlePos=lg.getTitlePosition().getValue();
if (isRightToLeft()) {
if (iTitlePos == Position.LEFT) {
iTitlePos=Position.RIGHT;
}
 else if (iTitlePos == Position.RIGHT) {
iTitlePos=Position.LEFT;
}
}
switch (iTitlePos) {
case Position.ABOVE:
yOffset=lgTitleHeight;
hOffset=-yOffset;
break;
case Position.BELOW:
hOffset=-lgTitleHeight;
break;
case Position.LEFT:
xOffset=lgTitleWidth;
wOffset=-xOffset;
break;
case Position.RIGHT:
wOffset=-lgTitleWidth;
break;
}
}
final ClientArea ca=lg.getClientArea();
final Insets lgIns=lg.getInsets().scaledInstance(dScale);
LineAttributes lia=ca.getOutline();
bo=BoundsImpl.create(dX,dY,sz.getWidth(),sz.getHeight());
bo=bo.adjustedInstance(lgIns);
dX=bo.getLeft();
dY=bo.getTop();
bo.delta(xOffset,yOffset,wOffset,hOffset);
dX=bo.getLeft();
dY=bo.getTop();
final double dBaseX=dX;
final double dBaseY=dY;
final RectangleRenderEvent rre=(RectangleRenderEvent)((EventObjectCache)ir).getEventObject(StructureSource.createLegend(lg),RectangleRenderEvent.class);
if (ca.getShadowColor() != null) {
rre.setBounds(bo.translateInstance(3,3));
rre.setBackground(ca.getShadowColor());
ipr.fillRectangle(rre);
}
rre.setBounds(bo);
rre.setOutline(lia);
rre.setBackground(ca.getBackground());
ipr.fillRectangle(rre);
ipr.drawRectangle(rre);
lia=LineAttributesImpl.copyInstance(lia);
lia.setVisible(true);
LineAttributes liSep=lg.getSeparator() == null ? lia : lg.getSeparator();
final SeriesDefinition[] seda=cm.getSeriesForLegend();
final Orientation o=lg.getOrientation();
final Direction d=lg.getDirection();
final Label la=LabelImpl.create();
la.setCaption(TextImpl.copyInstance(lg.getText()));
la.getCaption().setValue("String_Node_Str");
final ITextMetrics itm=xs.getTextMetrics(la);
try {
final double dItemHeight=itm.getFullHeight();
final double dHorizontalSpacing=4;
Insets insCA=ca.getInsets().scaledInstance(dScale);
Series seBase;
List al;
LegendItemRenderingHints lirh;
Palette pa;
int iPaletteCount;
EList elPaletteEntries;
Fill fPaletteEntry;
final boolean bPaletteByCategory=(cm.getLegend().getItemType().getValue() == LegendItemType.CATEGORIES);
String strNeedInvert=(String)rtc.getState("String_Node_Str");
boolean bNeedInvert=Boolean.valueOf(strNeedInvert).booleanValue();
if (o.getValue() == Orientation.VERTICAL) {
if (bPaletteByCategory) {
SeriesDefinition sdBase=null;
if (cm instanceof ChartWithAxes) {
final Axis axPrimaryBase=((ChartWithAxes)cm).getBaseAxes()[0];
if (axPrimaryBase.getSeriesDefinitions().isEmpty()) {
return;
}
sdBase=(SeriesDefinition)axPrimaryBase.getSeriesDefinitions().get(0);
}
 else if (cm instanceof ChartWithoutAxes) {
if (((ChartWithoutAxes)cm).getSeriesDefinitions().isEmpty()) {
return;
}
sdBase=(SeriesDefinition)((ChartWithoutAxes)cm).getSeriesDefinitions().get(0);
}
if (sdBase.getRunTimeSeries().size() == 0) {
return;
}
 else {
seBase=(Series)sdBase.getRunTimeSeries().get(0);
}
pa=sdBase.getSeriesPalette();
elPaletteEntries=pa.getEntries();
iPaletteCount=elPaletteEntries.size();
if (lilh.getLegendItemHints() == null) {
throw new ChartException(ChartEnginePlugin.ID,ChartException.RENDERING,"String_Node_Str",Messages.getResourceBundle(rtc.getULocale()));
}
LegendItemHints[] liha=lilh.getLegendItemHints();
LegendItemHints lih;
Map columnCache=searchMaxColumnWidth(liha);
for (int i=0; i < liha.length; i++) {
lih=liha[i];
if ((lih.getType() & IConstants.LEGEND_ENTRY) == IConstants.LEGEND_ENTRY) {
la.getCaption().setValue(lih.getText());
fPaletteEntry=(Fill)elPaletteEntries.get(lih.getCategoryIndex() % iPaletteCount);
lirh=(LegendItemRenderingHints)htRenderers.get(seBase);
double columnWidth=bo.getWidth();
Double cachedWidth=(Double)columnCache.get(lih);
if (cachedWidth != null) {
columnWidth=cachedWidth.doubleValue() + 3 * dItemHeight / 2 + 2 * insCA.getLeft();
}
renderLegendItem(ipr,lg,la,null,dBaseX + lih.getLeft(),dBaseY + lih.getTop() + insCA.getTop(),lih.getWidth(),dItemHeight,lih.getHeight(),0,columnWidth,insCA.getLeft(),dHorizontalSpacing,seBase,fPaletteEntry,lirh,lih.getCategoryIndex(),dScale);
}
}
}
 else if (d.getValue() == Direction.TOP_BOTTOM) {
if (lilh.getLegendItemHints() == null) {
throw new ChartException(ChartEnginePlugin.ID,ChartException.RENDERING,"String_Node_Str",Messages.getResourceBundle(rtc.getULocale()));
}
LegendItemHints[] liha=lilh.getLegendItemHints();
LegendItemHints lih;
int k=0;
Map columnCache=searchMaxColumnWidth(liha);
for (int j=0; j < seda.length; j++) {
int iSedaId=bNeedInvert ? seda.length - 1 - j : j;
al=seda[iSedaId].getRunTimeSeries();
pa=seda[iSedaId].getSeriesPalette();
elPaletteEntries=pa.getEntries();
iPaletteCount=elPaletteEntries.size();
for (int i=0; i < al.size(); i++) {
int iSeId=bNeedInvert ? al.size() - 1 - i : i;
seBase=(Series)al.get(iSeId);
if (!seBase.isVisible()) {
continue;
}
lirh=(LegendItemRenderingHints)htRenderers.get(seBase);
if (k < liha.length) {
lih=liha[k++];
if (lih.getType() == IConstants.LEGEND_ENTRY) {
la.getCaption().setValue(lih.getText());
Label valueLa=null;
if (lg.isShowValue()) {
valueLa=LabelImpl.copyInstance(seBase.getLabel());
valueLa.getCaption().setValue(lih.getExtraText());
valueLa.setVisible(true);
}
fPaletteEntry=(Fill)elPaletteEntries.get(lih.getCategoryIndex() % iPaletteCount);
double columnWidth=bo.getWidth();
Double cachedWidth=(Double)columnCache.get(lih);
if (cachedWidth != null) {
columnWidth=cachedWidth.doubleValue() + 3 * dItemHeight / 2 + 2 * insCA.getLeft();
}
renderLegendItem(ipr,lg,la,valueLa,dBaseX + lih.getLeft(),dBaseY + lih.getTop() + insCA.getTop(),lih.getWidth(),dItemHeight,lih.getHeight(),lih.getExtraHeight(),columnWidth,insCA.getLeft(),dHorizontalSpacing,seBase,fPaletteEntry,lirh,i,dScale);
}
}
}
if (j < seda.length - 1 && k < liha.length) {
lih=liha[k];
if (lih.getType() == IConstants.LEGEND_SEPERATOR) {
k++;
renderSeparator(ipr,lg,liSep,dBaseX + lih.getLeft(),dBaseY + lih.getTop(),lih.getWidth(),Orientation.HORIZONTAL_LITERAL);
}
}
}
}
 else if (d.getValue() == Direction.LEFT_RIGHT) {
if (lilh.getLegendItemHints() == null) {
throw new ChartException(ChartEnginePlugin.ID,ChartException.RENDERING,"String_Node_Str",Messages.getResourceBundle(rtc.getULocale()));
}
LegendItemHints[] liha=lilh.getLegendItemHints();
LegendItemHints lih;
int k=0;
Map columnCache=searchMaxColumnWidth(liha);
for (int j=0; j < seda.length; j++) {
int iSedaId=bNeedInvert ? seda.length - 1 - j : j;
al=seda[iSedaId].getRunTimeSeries();
pa=seda[iSedaId].getSeriesPalette();
elPaletteEntries=pa.getEntries();
iPaletteCount=elPaletteEntries.size();
for (int i=0; i < al.size(); i++) {
int iSeId=bNeedInvert ? al.size() - 1 - i : i;
seBase=(Series)al.get(iSeId);
if (!seBase.isVisible()) {
continue;
}
lirh=(LegendItemRenderingHints)htRenderers.get(seBase);
if (k < liha.length) {
lih=liha[k++];
if (lih.getType() == IConstants.LEGEND_ENTRY) {
la.getCaption().setValue(lih.getText());
Label valueLa=null;
if (lg.isShowValue()) {
valueLa=LabelImpl.copyInstance(seBase.getLabel());
valueLa.getCaption().setValue(lih.getExtraText());
valueLa.setVisible(true);
}
fPaletteEntry=(Fill)elPaletteEntries.get(lih.getCategoryIndex() % iPaletteCount);
double columnWidth=bo.getWidth();
Double cachedWidth=(Double)columnCache.get(lih);
if (cachedWidth != null) {
columnWidth=cachedWidth.doubleValue() + 3 * dItemHeight / 2 + 2 * insCA.getLeft();
}
renderLegendItem(ipr,lg,la,valueLa,dBaseX + lih.getLeft(),dBaseY + lih.getTop() + insCA.getTop(),lih.getWidth(),dItemHeight,lih.getHeight(),lih.getExtraHeight(),columnWidth,insCA.getLeft(),dHorizontalSpacing,seBase,fPaletteEntry,lirh,i,dScale);
}
}
}
if (j < seda.length - 1 && k < liha.length) {
lih=liha[k];
if (lih.getType() == IConstants.LEGEND_SEPERATOR) {
k++;
renderSeparator(ipr,lg,liSep,dBaseX + lih.getLeft(),dBaseY + lih.getTop(),bo.getHeight(),Orientation.VERTICAL_LITERAL);
}
}
}
}
 else {
throw new ChartException(ChartEnginePlugin.ID,ChartException.RENDERING,"String_Node_Str",new Object[]{d.getName()},Messages.getResourceBundle(rtc.getULocale()));
}
}
 else if (o.getValue() == Orientation.HORIZONTAL) {
if (bPaletteByCategory) {
SeriesDefinition sdBase=null;
if (cm instanceof ChartWithAxes) {
final Axis axPrimaryBase=((ChartWithAxes)cm).getBaseAxes()[0];
if (axPrimaryBase.getSeriesDefinitions().isEmpty()) {
return;
}
sdBase=(SeriesDefinition)axPrimaryBase.getSeriesDefinitions().get(0);
}
 else if (cm instanceof ChartWithoutAxes) {
if (((ChartWithoutAxes)cm).getSeriesDefinitions().isEmpty()) {
return;
}
sdBase=(SeriesDefinition)((ChartWithoutAxes)cm).getSeriesDefinitions().get(0);
}
seBase=(Series)sdBase.getRunTimeSeries().get(0);
pa=sdBase.getSeriesPalette();
elPaletteEntries=pa.getEntries();
iPaletteCount=elPaletteEntries.size();
if (lilh.getLegendItemHints() == null) {
throw new ChartException(ChartEnginePlugin.ID,ChartException.RENDERING,"String_Node_Str",Messages.getResourceBundle(rtc.getULocale()));
}
LegendItemHints[] liha=lilh.getLegendItemHints();
LegendItemHints lih;
for (int i=0; i < liha.length; i++) {
lih=liha[i];
if ((lih.getType() & IConstants.LEGEND_ENTRY) == IConstants.LEGEND_ENTRY) {
la.getCaption().setValue(lih.getText());
fPaletteEntry=(Fill)elPaletteEntries.get(lih.getCategoryIndex() % iPaletteCount);
lirh=(LegendItemRenderingHints)htRenderers.get(seBase);
renderLegendItem(ipr,lg,la,null,dBaseX + lih.getLeft(),dBaseY + lih.getTop() + insCA.getTop(),lih.getWidth(),dItemHeight,lih.getHeight(),0,lih.getWidth() + 3 * dItemHeight / 2 + 2 * insCA.getLeft(),insCA.getLeft(),dHorizontalSpacing,seBase,fPaletteEntry,lirh,lih.getCategoryIndex(),dScale);
}
}
}
 else if (d.getValue() == Direction.TOP_BOTTOM) {
if (lilh.getLegendItemHints() == null) {
throw new ChartException(ChartEnginePlugin.ID,ChartException.RENDERING,"String_Node_Str",Messages.getResourceBundle(rtc.getULocale()));
}
LegendItemHints[] liha=lilh.getLegendItemHints();
LegendItemHints lih;
int k=0;
for (int j=0; j < seda.length; j++) {
int iSedaId=bNeedInvert ? seda.length - 1 - j : j;
al=seda[iSedaId].getRunTimeSeries();
pa=seda[iSedaId].getSeriesPalette();
elPaletteEntries=pa.getEntries();
iPaletteCount=elPaletteEntries.size();
for (int i=0; i < al.size(); i++) {
int iSeId=bNeedInvert ? al.size() - 1 - i : i;
seBase=(Series)al.get(iSeId);
if (!seBase.isVisible()) {
continue;
}
lirh=(LegendItemRenderingHints)htRenderers.get(seBase);
if (k < liha.length) {
lih=liha[k++];
if (lih.getType() == IConstants.LEGEND_ENTRY) {
la.getCaption().setValue(lih.getText());
Label valueLa=null;
if (lg.isShowValue()) {
valueLa=LabelImpl.copyInstance(seBase.getLabel());
valueLa.getCaption().setValue(lih.getExtraText());
valueLa.setVisible(true);
}
fPaletteEntry=(Fill)elPaletteEntries.get(lih.getCategoryIndex() % iPaletteCount);
renderLegendItem(ipr,lg,la,valueLa,dBaseX + lih.getLeft(),dBaseY + lih.getTop() + insCA.getTop(),lih.getWidth(),dItemHeight,lih.getHeight(),lih.getExtraHeight(),lih.getWidth() + 3 * dItemHeight / 2 + 2 * insCA.getLeft(),insCA.getLeft(),dHorizontalSpacing,seBase,fPaletteEntry,lirh,i,dScale);
}
}
}
if (j < seda.length - 1 && k < liha.length) {
lih=liha[k];
if (lih.getType() == IConstants.LEGEND_SEPERATOR) {
k++;
renderSeparator(ipr,lg,liSep,dBaseX + lih.getLeft(),dBaseY + lih.getTop(),bo.getWidth(),Orientation.HORIZONTAL_LITERAL);
}
}
}
}
 else if (d.getValue() == Direction.LEFT_RIGHT) {
if (lilh.getLegendItemHints() == null) {
throw new ChartException(ChartEnginePlugin.ID,ChartException.RENDERING,"String_Node_Str",Messages.getResourceBundle(rtc.getULocale()));
}
LegendItemHints[] liha=lilh.getLegendItemHints();
LegendItemHints lih;
int k=0;
for (int j=0; j < seda.length; j++) {
int iSedaId=bNeedInvert ? seda.length - 1 - j : j;
al=seda[iSedaId].getRunTimeSeries();
pa=seda[iSedaId].getSeriesPalette();
elPaletteEntries=pa.getEntries();
iPaletteCount=elPaletteEntries.size();
for (int i=0; i < al.size(); i++) {
int iSeId=bNeedInvert ? al.size() - 1 - i : i;
seBase=(Series)al.get(iSeId);
if (!seBase.isVisible()) {
continue;
}
lirh=(LegendItemRenderingHints)htRenderers.get(seBase);
if (k < liha.length) {
lih=liha[k++];
if (lih.getType() == IConstants.LEGEND_ENTRY) {
la.getCaption().setValue(lih.getText());
Label valueLa=null;
if (lg.isShowValue()) {
valueLa=LabelImpl.copyInstance(seBase.getLabel());
valueLa.getCaption().setValue(lih.getExtraText());
valueLa.setVisible(true);
}
fPaletteEntry=(Fill)elPaletteEntries.get(lih.getCategoryIndex() % iPaletteCount);
renderLegendItem(ipr,lg,la,valueLa,dBaseX + lih.getLeft(),dBaseY + lih.getTop() + insCA.getTop(),lih.getWidth(),dItemHeight,lih.getHeight(),lih.getExtraHeight(),lih.getWidth() + 3 * dItemHeight / 2 + 2 * insCA.getLeft(),insCA.getLeft(),dHorizontalSpacing,seBase,fPaletteEntry,lirh,i,dScale);
}
}
}
if (j < seda.length - 1 && k < liha.length) {
lih=liha[k];
if (lih.getType() == IConstants.LEGEND_SEPERATOR) {
k++;
renderSeparator(ipr,lg,liSep,dBaseX + lih.getLeft(),dBaseY + lih.getTop(),lih.getHeight(),Orientation.VERTICAL_LITERAL);
}
}
}
}
 else {
throw new ChartException(ChartEnginePlugin.ID,ChartException.RENDERING,"String_Node_Str",new Object[]{d.getName()},Messages.getResourceBundle(rtc.getULocale()));
}
}
 else {
throw new ChartException(ChartEnginePlugin.ID,ChartException.RENDERING,"String_Node_Str",new Object[]{o.getName()},Messages.getResourceBundle(rtc.getULocale()));
}
if (bRenderLegendTitle) {
double lX=bo.getLeft();
double lY=bo.getTop();
switch (iTitlePos) {
case Position.ABOVE:
lX=bo.getLeft() + (bo.getWidth() - lgTitleWidth) / 2d;
lY=bo.getTop() - lgTitleHeight;
break;
case Position.BELOW:
lX=bo.getLeft() + (bo.getWidth() - lgTitleWidth) / 2d;
lY=bo.getTop() + bo.getHeight();
break;
case Position.LEFT:
lX=bo.getLeft() - lgTitleWidth;
lY=bo.getTop() + (bo.getHeight() - lgTitleHeight) / 2d;
break;
case Position.RIGHT:
lX=bo.getLeft() + bo.getWidth();
lY=bo.getTop() + (bo.getHeight() - lgTitleHeight) / 2d;
break;
}
final TextRenderEvent tre=(TextRenderEvent)((EventObjectCache)ir).getEventObject(WrappedStructureSource.createLegendTitle(lg,lgTitle),TextRenderEvent.class);
tre.setBlockBounds(BoundsImpl.create(lX,lY,lgTitleWidth,lgTitleHeight));
TextAlignment ta=TextAlignmentImpl.create();
ta.setHorizontalAlignment(HorizontalAlignment.CENTER_LITERAL);
ta.setVerticalAlignment(VerticalAlignment.CENTER_LITERAL);
tre.setBlockAlignment(ta);
tre.setLabel(lgTitle);
tre.setAction(TextRenderEvent.RENDER_TEXT_IN_BLOCK);
if (rtc.isRightToLeftText()) {
tre.setRtlCaption();
}
ipr.drawText(tre);
}
}
  finally {
itm.dispose();
}
}