i );
i, dScale );
	public void renderLegend( IPrimitiveRenderer ipr, Legend lg, Map htRenderers )			throws ChartException	{		if ( !lg.isVisible( ) ) // CHECK VISIBILITY		{			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;		/* --- Start bound computing --- */		// TODO Refactoring: create function		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 );			// USE ANCHOR IN POSITIONING THE LEGEND CLIENT AREA WITHIN THE BLOCK			// SLACK SPACE			dX = bo.getLeft( ) + ( bo.getWidth( ) - sz.getWidth( ) ) / 2;			dY = 0;			if ( lg.isSetAnchor( ) )			{				int iAnchor = lg.getAnchor( ).getValue( );				// swap west/east				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 : // CENTERED						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 : // CENTERED						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		{			// USE PREVIOUSLY COMPUTED POSITION IN THE GENERATOR FOR LEGEND			// 'INSIDE' PLOT			dX = bo.getLeft( );			dY = bo.getTop( );			sz = SizeImpl.create( bo.getWidth( ), bo.getHeight( ) );		}		// get cached legend info.		final LegendLayoutHints lilh = rtc.getLegendLayoutHints( );		if ( lilh == null )		{			throw new ChartException( ChartEnginePlugin.ID,					ChartException.RENDERING,					"exception.null.legend.layout.hints", //$NON-NLS-1$					Messages.getResourceBundle( rtc.getULocale( ) ) );		}		// consider legend title size.		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 );			// handle external resource string			final String sPreviousValue = lgTitle.getCaption( ).getValue( );			lgTitle.getCaption( )					.setValue( rtc.externalizedMessage( sPreviousValue ) );			// use cached value			Size titleSize = lilh.getTitleSize( );			lgTitleWidth = titleSize.getWidth( );			lgTitleHeight = titleSize.getHeight( );			iTitlePos = lg.getTitlePosition( ).getValue( );			// swap left/right			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;			}		}		// RENDER THE LEGEND CLIENT AREA		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( );		// Adjust bounds.		bo.delta( xOffset, yOffset, wOffset, hOffset );		dX = bo.getLeft( );		dY = bo.getTop( );		/* --- End of bounds computing --- */		final double dBaseX = dX;		final double dBaseY = dY;		final RectangleRenderEvent rre = (RectangleRenderEvent) ( (EventObjectCache) ir ).getEventObject( StructureSource.createLegend( lg ),				RectangleRenderEvent.class );		// render client area shadow.		if ( ca.getShadowColor( ) != null )		{			rre.setBounds( bo.translateInstance( 3, 3 ) );			rre.setBackground( ca.getShadowColor( ) );			ipr.fillRectangle( rre );		}		// render client area		rre.setBounds( bo );		rre.setOutline( lia );		rre.setBackground( ca.getBackground( ) );		ipr.fillRectangle( rre );		ipr.drawRectangle( rre );		lia = LineAttributesImpl.copyInstance( lia );		lia.setVisible( true ); // SEPARATOR LINES MUST BE VISIBLE		LineAttributes liSep = lg.getSeparator( ) == null ? lia				: lg.getSeparator( );		final SeriesDefinition[] seda = cm.getSeriesForLegend( );		// INITIALIZATION OF VARS USED IN FOLLOWING LOOPS		final Orientation o = lg.getOrientation( );		final Direction d = lg.getDirection( );		final Label la = LabelImpl.create( );		la.setCaption( TextImpl.copyInstance( lg.getText( ) ) );		la.getCaption( ).setValue( "X" ); //$NON-NLS-1$		final ITextMetrics itm = xs.getTextMetrics( la );		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 );		// COMPUTATIONS HERE MUST BE IN SYNC WITH THE ACTUAL RENDERER		if ( o.getValue( ) == Orientation.VERTICAL )		{			if ( bPaletteByCategory )			{				SeriesDefinition sdBase = null;				if ( cm instanceof ChartWithAxes )				{					// ONLY SUPPORT 1 BASE AXIS FOR NOW					final Axis axPrimaryBase = ( (ChartWithAxes) cm ).getBaseAxes( )[0];					if ( axPrimaryBase.getSeriesDefinitions( ).isEmpty( ) )					{						// NOTHING TO RENDER (BASE AXIS HAS NO SERIES						// DEFINITIONS)						return;					}					// OK TO ASSUME THAT 1 BASE SERIES DEFINITION EXISTS					sdBase = (SeriesDefinition) axPrimaryBase.getSeriesDefinitions( )							.get( 0 );				}				else if ( cm instanceof ChartWithoutAxes )				{					if ( ( (ChartWithoutAxes) cm ).getSeriesDefinitions( )							.isEmpty( ) )					{						// NOTHING TO RENDER (BASE AXIS HAS NO SERIES						// DEFINITIONS)						return;					}					// OK TO ASSUME THAT 1 BASE SERIES DEFINITION EXISTS					sdBase = (SeriesDefinition) ( (ChartWithoutAxes) cm ).getSeriesDefinitions( )							.get( 0 );				}				// OK TO ASSUME THAT 1 BASE RUNTIME SERIES EXISTS				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,							"exception.null.legend.item.hints", //$NON-NLS-1$							Messages.getResourceBundle( rtc.getULocale( ) ) );				}				// use cached value				LegendItemHints[] liha = lilh.getLegendItemHints( );				LegendItemHints lih;				Map columnCache = searchMaxColumnWidth( liha );				for ( int i = 0; i < liha.length; i++ )				{					// render each legend item.					lih = liha[i];					if ( ( lih.getType( ) & IConstants.LEGEND_ENTRY ) == IConstants.LEGEND_ENTRY )					{						la.getCaption( ).setValue( lih.getText( ) );						// CYCLE THROUGH THE PALETTE						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,								i );					}				}			}			else if ( d.getValue( ) == Direction.TOP_BOTTOM )			{				if ( lilh.getLegendItemHints( ) == null )				{					throw new ChartException( ChartEnginePlugin.ID,							ChartException.RENDERING,							"exception.null.legend.item.hints", //$NON-NLS-1$							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++ )				{					al = seda[j].getRunTimeSeries( );					pa = seda[j].getSeriesPalette( );					elPaletteEntries = pa.getEntries( );					iPaletteCount = elPaletteEntries.size( );					for ( int i = 0; i < al.size( ); i++ )					{						seBase = (Series) al.get( i );						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( ) );								}								// CYCLE THROUGH THE PALETTE								fPaletteEntry = (Fill) elPaletteEntries.get( i										% 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 );							}						}					}					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,							"exception.null.legend.item.hints", //$NON-NLS-1$							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++ )				{					al = seda[j].getRunTimeSeries( );					pa = seda[j].getSeriesPalette( );					elPaletteEntries = pa.getEntries( );					iPaletteCount = elPaletteEntries.size( );					for ( int i = 0; i < al.size( ); i++ )					{						seBase = (Series) al.get( i );						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( ) );								}								// CYCLE THROUGH THE PALETTE								fPaletteEntry = (Fill) elPaletteEntries.get( i										% 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 );							}						}					}					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,						"exception.illegal.legend.direction", //$NON-NLS-1$						new Object[]{							d.getName( )						},						Messages.getResourceBundle( rtc.getULocale( ) ) );			}		}		else if ( o.getValue( ) == Orientation.HORIZONTAL )		{			if ( bPaletteByCategory )			{				SeriesDefinition sdBase = null;				if ( cm instanceof ChartWithAxes )				{					// ONLY SUPPORT 1 BASE AXIS FOR NOW					final Axis axPrimaryBase = ( (ChartWithAxes) cm ).getBaseAxes( )[0];					if ( axPrimaryBase.getSeriesDefinitions( ).isEmpty( ) )					{						// NOTHING TO RENDER (BASE AXIS HAS NO SERIES						// DEFINITIONS)						return;					}					// OK TO ASSUME THAT 1 BASE SERIES DEFINITION EXISTS					sdBase = (SeriesDefinition) axPrimaryBase.getSeriesDefinitions( )							.get( 0 );				}				else if ( cm instanceof ChartWithoutAxes )				{					if ( ( (ChartWithoutAxes) cm ).getSeriesDefinitions( )							.isEmpty( ) )					{						// NOTHING TO RENDER (BASE AXIS HAS NO SERIES						// DEFINITIONS)						return;					}					// OK TO ASSUME THAT 1 BASE SERIES DEFINITION EXISTS					sdBase = (SeriesDefinition) ( (ChartWithoutAxes) cm ).getSeriesDefinitions( )							.get( 0 );				}				// OK TO ASSUME THAT 1 BASE RUNTIME SERIES EXISTS				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,							"exception.null.legend.item.hints", //$NON-NLS-1$							Messages.getResourceBundle( rtc.getULocale( ) ) );				}				// use cached value				LegendItemHints[] liha = lilh.getLegendItemHints( );				LegendItemHints lih;				for ( int i = 0; i < liha.length; i++ )				{					// render each legend item.					lih = liha[i];					if ( ( lih.getType( ) & IConstants.LEGEND_ENTRY ) == IConstants.LEGEND_ENTRY )					{						la.getCaption( ).setValue( lih.getText( ) );						// CYCLE THROUGH THE PALETTE						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,								i );					}				}			}			else if ( d.getValue( ) == Direction.TOP_BOTTOM )			{				if ( lilh.getLegendItemHints( ) == null )				{					throw new ChartException( ChartEnginePlugin.ID,							ChartException.RENDERING,							"exception.null.legend.item.hints", //$NON-NLS-1$							Messages.getResourceBundle( rtc.getULocale( ) ) );				}				LegendItemHints[] liha = lilh.getLegendItemHints( );				LegendItemHints lih;				int k = 0;				for ( int j = 0; j < seda.length; j++ )				{					al = seda[j].getRunTimeSeries( );					pa = seda[j].getSeriesPalette( );					elPaletteEntries = pa.getEntries( );					iPaletteCount = elPaletteEntries.size( );					for ( int i = 0; i < al.size( ); i++ )					{						seBase = (Series) al.get( i );						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( ) );								}								// CYCLE THROUGH THE PALETTE								fPaletteEntry = (Fill) elPaletteEntries.get( i										% 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 );							}						}					}					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,							"exception.null.legend.item.hints", //$NON-NLS-1$							Messages.getResourceBundle( rtc.getULocale( ) ) );				}				LegendItemHints[] liha = lilh.getLegendItemHints( );				LegendItemHints lih;				int k = 0;				for ( int j = 0; j < seda.length; j++ )				{					al = seda[j].getRunTimeSeries( );					pa = seda[j].getSeriesPalette( );					elPaletteEntries = pa.getEntries( );					iPaletteCount = elPaletteEntries.size( );					for ( int i = 0; i < al.size( ); i++ )					{						seBase = (Series) al.get( i );						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( ) );								}								// CYCLE THROUGH THE PALETTE								fPaletteEntry = (Fill) elPaletteEntries.get( i										% 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 );							}						}					}					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,						"exception.illegal.legend.direction", //$NON-NLS-1$						new Object[]{							d.getName( )						},						Messages.getResourceBundle( rtc.getULocale( ) ) );			}		}		else		{			throw new ChartException( ChartEnginePlugin.ID,					ChartException.RENDERING,					"exception.illegal.legend.orientation", //$NON-NLS-1$ 					new Object[]{						o.getName( )					},					Messages.getResourceBundle( rtc.getULocale( ) ) );		}		// Render legend title if defined.		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 );			ipr.drawText( tre );		}		itm.dispose( ); // DISPOSE RESOURCES AFTER USE	}