Java Code Examples for org.eclipse.swt.graphics.Font#dispose()

The following examples show how to use org.eclipse.swt.graphics.Font#dispose() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SyntaxColoringLabel.java    From statecharts with Eclipse Public License 1.0 6 votes vote down vote up
protected int getTextExtend(Font font, String string) {
	if (string.isEmpty())
		return 0;
	if (zoom != 1.0) {
		FontData data = font.getFontData()[0];
		FontDescriptor newFontDescriptor = FontDescriptor.createFrom(font)
				.setHeight((int) (data.getHeight() * zoom));
		font = newFontDescriptor.createFont(Display.getDefault());
	}
	if (gc.getFont() != font)
		gc.setFont(font);
	int offset = gc.textExtent(string).x;
	if (zoom != 1.0) {
		font.dispose();
	}
	return (int) Math.ceil((offset / zoom));
}
 
Example 2
Source File: CalendarComposite.java    From nebula with Eclipse Public License 2.0 6 votes vote down vote up
private void drawChartOntoGC(GC gc) {
	Rectangle bounds = super.getBounds();
	gc.setBackground(mColorManager.getCalendarBackgroundColor());
	gc.fillRectangle(bounds);

	Font used = null;
	if (CalendarCombo.OS_CARBON) {
		used = mSettings.getCarbonDrawFont();
		if (used != null)
			gc.setFont(used);
	}

	// header
	drawHeader(gc);
	// day titles
	drawTitleDays(gc);
	// days
	drawDays(gc);
	// 1 pixel border
	drawBorder(gc);

	gc.dispose();
	if (used != null)
		used.dispose();
}
 
Example 3
Source File: PropsUI.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public static void setGCFont( GC gc, Device device, FontData fontData ) {
  if ( Const.getOS().startsWith( "Windows" ) ) {
    Font font = new Font( device, fontData );
    gc.setFont( font );
    font.dispose();
  } else {
    gc.setFont( device.getSystemFont() );
  }
}
 
Example 4
Source File: ProductSystemPart.java    From olca-app with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void deactivate() {
	IFigure figure = getFigure();
	if (figure instanceof ProductSystemFigure) {
		ProductSystemFigure pFigure = (ProductSystemFigure) figure;
		Font infoFont = pFigure.infoFont;
		if (infoFont != null && !infoFont.isDisposed())
			infoFont.dispose();
	}
	super.deactivate();
}
 
Example 5
Source File: PropsUi.java    From hop with Apache License 2.0 5 votes vote down vote up
public static void setGCFont( GC gc, Device device, FontData fontData ) {
  if ( Const.getOS().startsWith( "Windows" ) ) {
    Font font = new Font( device, fontData );
    gc.setFont( font );
    font.dispose();
  } else {
    gc.setFont( device.getSystemFont() );
  }
}
 
Example 6
Source File: TitleRenderer.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void dispose ()
{
    for ( final Font font : this.fontCache.values () )
    {
        font.dispose ();
    }
    this.fontCache.clear ();

    super.dispose ();
}
 
Example 7
Source File: SWTUtils.java    From buffer_bci with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Create an awt font by converting as much information
 * as possible from the provided swt <code>FontData</code>.
 * <p>Generally speaking, given a font size, an swt font will
 * display differently on the screen than the corresponding awt
 * one. Because the SWT toolkit use native graphical ressources whenever
 * it is possible, this fact is platform dependent. To address
 * this issue, it is possible to enforce the method to return
 * an awt font with the same height as the swt one.
 *
 * @param device The swt device being drawn on (display or gc device).
 * @param fontData The swt font to convert.
 * @param ensureSameSize A boolean used to enforce the same size
 * (in pixels) between the swt font and the newly created awt font.
 * @return An awt font converted from the provided swt font.
 */
public static java.awt.Font toAwtFont(Device device, FontData fontData,
        boolean ensureSameSize) {
    int height = (int) Math.round(fontData.getHeight() * device.getDPI().y
            / 72.0);
    // hack to ensure the newly created awt fonts will be rendered with the
    // same height as the swt one
    if (ensureSameSize) {
        GC tmpGC = new GC(device);
        Font tmpFont = new Font(device, fontData);
        tmpGC.setFont(tmpFont);
        JPanel DUMMY_PANEL = new JPanel();
        java.awt.Font tmpAwtFont = new java.awt.Font(fontData.getName(),
                fontData.getStyle(), height);
        if (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az)
                > tmpGC.textExtent(Az).x) {
            while (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az)
                    > tmpGC.textExtent(Az).x) {
                height--;
                tmpAwtFont = new java.awt.Font(fontData.getName(),
                        fontData.getStyle(), height);
            }
        }
        else if (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az)
                < tmpGC.textExtent(Az).x) {
            while (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az)
                    < tmpGC.textExtent(Az).x) {
                height++;
                tmpAwtFont = new java.awt.Font(fontData.getName(),
                        fontData.getStyle(), height);
            }
        }
        tmpFont.dispose();
        tmpGC.dispose();
    }
    return new java.awt.Font(fontData.getName(), fontData.getStyle(),
            height);
}
 
Example 8
Source File: GridItem_Test.java    From nebula with Eclipse Public License 2.0 5 votes vote down vote up
@Test( expected = IllegalArgumentException.class )
public void testSetFont_DisposedFont() {
  GridItem item = new GridItem( grid, SWT.NONE );
  Font font = new Font( display, "Arial", 20, SWT.BOLD );
  font.dispose();
  item.setFont( font );
}
 
Example 9
Source File: GridItem_Test.java    From nebula with Eclipse Public License 2.0 5 votes vote down vote up
@Test( expected = IllegalArgumentException.class )
public void testSetFontByIndex_DisposedFont() {
  createGridColumns( grid, 3, SWT.NONE );
  GridItem item = new GridItem( grid, SWT.NONE );
  Font font = new Font( display, "Arial", 20, SWT.BOLD );
  font.dispose();
  item.setFont( 1, font );
}
 
Example 10
Source File: SWTUtils.java    From openstock with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Create an awt font by converting as much information
 * as possible from the provided swt <code>FontData</code>.
 * <p>Generally speaking, given a font size, an swt font will
 * display differently on the screen than the corresponding awt
 * one. Because the SWT toolkit use native graphical ressources whenever
 * it is possible, this fact is platform dependent. To address
 * this issue, it is possible to enforce the method to return
 * an awt font with the same height as the swt one.
 *
 * @param device The swt device being drawn on (display or gc device).
 * @param fontData The swt font to convert.
 * @param ensureSameSize A boolean used to enforce the same size
 * (in pixels) between the swt font and the newly created awt font.
 * @return An awt font converted from the provided swt font.
 */
public static java.awt.Font toAwtFont(Device device, FontData fontData,
        boolean ensureSameSize) {
    int height = (int) Math.round(fontData.getHeight() * device.getDPI().y
            / 72.0);
    // hack to ensure the newly created awt fonts will be rendered with the
    // same height as the swt one
    if (ensureSameSize) {
        GC tmpGC = new GC(device);
        Font tmpFont = new Font(device, fontData);
        tmpGC.setFont(tmpFont);
        JPanel DUMMY_PANEL = new JPanel();
        java.awt.Font tmpAwtFont = new java.awt.Font(fontData.getName(),
                fontData.getStyle(), height);
        if (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az)
                > tmpGC.textExtent(Az).x) {
            while (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az)
                    > tmpGC.textExtent(Az).x) {
                height--;
                tmpAwtFont = new java.awt.Font(fontData.getName(),
                        fontData.getStyle(), height);
            }
        }
        else if (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az)
                < tmpGC.textExtent(Az).x) {
            while (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az)
                    < tmpGC.textExtent(Az).x) {
                height++;
                tmpAwtFont = new java.awt.Font(fontData.getName(),
                        fontData.getStyle(), height);
            }
        }
        tmpFont.dispose();
        tmpGC.dispose();
    }
    return new java.awt.Font(fontData.getName(), fontData.getStyle(),
            height);
}
 
Example 11
Source File: SWTGC.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public void dispose() {
  gc.dispose();
  if ( transform != null && transform.isDisposed() == false ) {
    transform.dispose();
  }
  for ( Color color : colors ) {
    color.dispose();
  }
  for ( Font font : fonts ) {
    font.dispose();
  }
}
 
Example 12
Source File: SWTUtils.java    From ECG-Viewer with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create an awt font by converting as much information
 * as possible from the provided swt <code>FontData</code>.
 * <p>Generally speaking, given a font size, an swt font will
 * display differently on the screen than the corresponding awt
 * one. Because the SWT toolkit use native graphical ressources whenever
 * it is possible, this fact is platform dependent. To address
 * this issue, it is possible to enforce the method to return
 * an awt font with the same height as the swt one.
 *
 * @param device The swt device being drawn on (display or gc device).
 * @param fontData The swt font to convert.
 * @param ensureSameSize A boolean used to enforce the same size
 * (in pixels) between the swt font and the newly created awt font.
 * @return An awt font converted from the provided swt font.
 */
public static java.awt.Font toAwtFont(Device device, FontData fontData,
        boolean ensureSameSize) {
    int height = (int) Math.round(fontData.getHeight() * device.getDPI().y
            / 72.0);
    // hack to ensure the newly created awt fonts will be rendered with the
    // same height as the swt one
    if (ensureSameSize) {
        GC tmpGC = new GC(device);
        Font tmpFont = new Font(device, fontData);
        tmpGC.setFont(tmpFont);
        JPanel DUMMY_PANEL = new JPanel();
        java.awt.Font tmpAwtFont = new java.awt.Font(fontData.getName(),
                fontData.getStyle(), height);
        if (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az)
                > tmpGC.textExtent(Az).x) {
            while (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az)
                    > tmpGC.textExtent(Az).x) {
                height--;
                tmpAwtFont = new java.awt.Font(fontData.getName(),
                        fontData.getStyle(), height);
            }
        }
        else if (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az)
                < tmpGC.textExtent(Az).x) {
            while (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az)
                    < tmpGC.textExtent(Az).x) {
                height++;
                tmpAwtFont = new java.awt.Font(fontData.getName(),
                        fontData.getStyle(), height);
            }
        }
        tmpFont.dispose();
        tmpGC.dispose();
    }
    return new java.awt.Font(fontData.getName(), fontData.getStyle(),
            height);
}
 
Example 13
Source File: DesignerRepresentation.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Show the exception message that prevented to draw the chart
 * 
 * @param g2d
 * @param ex
 *            The exception that occured
 */
private final void showException( GC g2d, Exception ex )
{
	Point pTLC = new Point( 0, 0 );

	// String sWrappedException = ex.getClass( ).getName( );
	Throwable th = ex;

	String sMessage = null;
	if ( th instanceof BirtException )
	{
		sMessage = ( (BirtException) th ).getLocalizedMessage( );
	}
	else
	{
		sMessage = ex.getMessage( );
	}

	if ( sMessage == null )
	{
		sMessage = "<null>"; //$NON-NLS-1$
	}
	// StackTraceElement[] stea = ex.getStackTrace( );
	Dimension d = getSize( );

	Device dv = Display.getCurrent( );
	Font fo = new Font( dv, "Courier", SWT.BOLD, 12 ); //$NON-NLS-1$
	g2d.setFont( fo );
	FontMetrics fm = g2d.getFontMetrics( );
	g2d.setBackground( dv.getSystemColor( SWT.COLOR_WHITE ) );
	g2d.fillRectangle( pTLC.x + 20,
			pTLC.y + 20,
			d.width - 40,
			d.height - 40 );
	g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) );
	g2d.drawRectangle( pTLC.x + 20,
			pTLC.y + 20,
			d.width - 40,
			d.height - 40 );
	Region rgPrev = new Region( );
	g2d.getClipping( rgPrev );
	g2d.setClipping( pTLC.x + 20, pTLC.y + 20, d.width - 40, d.height - 40 );
	int x = pTLC.x + 25, y = pTLC.y + 20 + fm.getHeight( );
	g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) );
	g2d.drawString( ERROR_MSG, x, y );
	y += fm.getHeight( );
	g2d.setForeground( dv.getSystemColor( SWT.COLOR_RED ) );
	g2d.drawText( sMessage, x, y );

	g2d.setClipping( rgPrev );
	rgPrev.dispose( );
	fo.dispose( );
}
 
Example 14
Source File: PrinterFacade.java    From http4e with Apache License 2.0 4 votes vote down vote up
public void print(){
   if (printer.startJob("Text")) { // the string is the job name - shows up

      try {// in the printer's job list
         Rectangle clientArea = printer.getClientArea();
         Rectangle trim = printer.computeTrim(0, 0, 0, 0);
         Point dpi = printer.getDPI();
         leftMargin = dpi.x + trim.x; // one inch from left side of paper
         rightMargin = clientArea.width - dpi.x + trim.x + trim.width; // one
         // inch
         // from
         // right
         // side
         // of
         // paper
         topMargin = dpi.y + trim.y; // one inch from top edge of paper
         bottomMargin = clientArea.height - dpi.y + trim.y + trim.height; // one
         // inch
         // from
         // bottom
         // edge
         // of
         // paper

         /* Create a buffer for computing tab width. */
         int tabSize = 4; // is tab width a user setting in your UI?
         StringBuilder tabBuffer = new StringBuilder(tabSize);
         for (int i = 0; i < tabSize; i++)
            tabBuffer.append(' ');
         tabs = tabBuffer.toString();

         /*
          * Create printer GC, and create and set the printer font &
          * foreground color.
          */
         gc = new GC(printer);

         font = printer.getSystemFont(); // ResourceUtils.getFont(Styles.getInstance().FONT_COURIER);
         // foregroundColor = ResourceUtils.getColor(Styles.DARK_RGB_TEXT);
         // backgroundColor =
         // ResourceUtils.getColor(Styles.BACKGROUND_ENABLED);

         FontData fontData = font.getFontData()[0];
         printerFont = new Font(printer, fontData.getName(), fontData.getHeight(), fontData.getStyle());
         gc.setFont(printerFont);
         tabWidth = gc.stringExtent(tabs).x;
         lineHeight = gc.getFontMetrics().getHeight();

         // RGB rgb = foregroundColor.getRGB();
         // printerForegroundColor = new Color(printer, rgb);
         // gc.setForeground(printerForegroundColor);

         // rgb = backgroundColor.getRGB();
         // printerBackgroundColor = new Color(printer, rgb);
         // gc.setBackground(printerBackgroundColor);

         /* Print text to current gc using word wrap */
         printText(printer);
         printer.endJob();

      } catch (Exception e) {
         throw new RuntimeException(e);

      } finally {
         /* Cleanup graphics resources used in printing */
         if (printerFont != null)
            printerFont.dispose();
         // if(printerForegroundColor != null)
         // printerForegroundColor.dispose();
         // if(printerBackgroundColor != null)
         // printerBackgroundColor.dispose();
         if (gc != null)
            gc.dispose();
      }
   }
}
 
Example 15
Source File: JavaScriptViewer.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
private final void showException( GC g2d, Exception ex )
{
	String sWrappedException = ex.getClass( ).getName( );
	Throwable th = ex;
	while ( ex.getCause( ) != null )
	{
		ex = (Exception) ex.getCause( );
	}
	String sException = ex.getClass( ).getName( );
	if ( sWrappedException.equals( sException ) )
	{
		sWrappedException = null;
	}

	String sMessage = null;
	if ( th instanceof BirtException )
	{
		sMessage = ( (BirtException) th ).getLocalizedMessage( );
	}
	else
	{
		sMessage = ex.getMessage( );
	}

	if ( sMessage == null )
	{
		sMessage = "<null>";//$NON-NLS-1$
	}
	StackTraceElement[] stea = ex.getStackTrace( );
	Point d = this.getSize( );

	Device dv = Display.getCurrent( );
	Font fo = new Font( dv, "Courier", SWT.BOLD, 16 );//$NON-NLS-1$
	g2d.setFont( fo );
	FontMetrics fm = g2d.getFontMetrics( );
	g2d.setBackground( dv.getSystemColor( SWT.COLOR_WHITE ) );
	g2d.fillRectangle( 20, 20, d.x - 40, d.y - 40 );
	g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) );
	g2d.drawRectangle( 20, 20, d.x - 40, d.y - 40 );
	g2d.setClipping( 20, 20, d.x - 40, d.y - 40 );
	int x = 25, y = 20 + fm.getHeight( );
	g2d.drawString( "Exception:", x, y );//$NON-NLS-1$
	x += g2d.textExtent( "Exception:" ).x + 5;//$NON-NLS-1$
	g2d.setForeground( dv.getSystemColor( SWT.COLOR_RED ) );
	g2d.drawString( sException, x, y );
	x = 25;
	y += fm.getHeight( );
	if ( sWrappedException != null )
	{
		g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) );
		g2d.drawString( "Wrapped In:", x, y );//$NON-NLS-1$
		x += g2d.textExtent( "Wrapped In:" ).x + 5;//$NON-NLS-1$
		g2d.setForeground( dv.getSystemColor( SWT.COLOR_RED ) );
		g2d.drawString( sWrappedException, x, y );
		x = 25;
		y += fm.getHeight( );
	}
	g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) );
	y += 10;
	g2d.drawString( "Message:", x, y );//$NON-NLS-1$
	x += g2d.textExtent( "Message:" ).x + 5;//$NON-NLS-1$
	g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLUE ) );
	g2d.drawString( sMessage, x, y );
	x = 25;
	y += fm.getHeight( );
	g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) );
	y += 10;
	g2d.drawString( "Trace:", x, y );//$NON-NLS-1$
	x = 40;
	y += fm.getHeight( );
	g2d.setForeground( dv.getSystemColor( SWT.COLOR_DARK_GREEN ) );
	for ( int i = 0; i < stea.length; i++ )
	{
		g2d.drawString( stea[i].getClassName( ) + ":"//$NON-NLS-1$
				+ stea[i].getMethodName( )
				+ "(...):"//$NON-NLS-1$
				+ stea[i].getLineNumber( ), x, y );
		x = 40;
		y += fm.getHeight( );
	}
	fo.dispose( );
}
 
Example 16
Source File: SWTUtils.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Create an awt font by converting as much information 
 * as possible from the provided swt <code>FontData</code>.
 * <p>Generally speaking, given a font size, an swt font will 
 * display differently on the screen than the corresponding awt 
 * one. Because the SWT toolkit use native graphical ressources whenever 
 * it is possible, this fact is platform dependent. To address 
 * this issue, it is possible to enforce the method to return 
 * an awt font with the same height as the swt one.
 * 
 * @param device The swt device being drawn on (display or gc device).
 * @param fontData The swt font to convert.
 * @param ensureSameSize A boolean used to enforce the same size 
 * (in pixels) between the swt font and the newly created awt font.
 * @return An awt font converted from the provided swt font.
 */
public static java.awt.Font toAwtFont(Device device, FontData fontData, 
        boolean ensureSameSize) {
    int style;
    switch (fontData.getStyle()) {
        case SWT.NORMAL:
            style = java.awt.Font.PLAIN;
            break;
        case SWT.ITALIC:
            style = java.awt.Font.ITALIC;
            break;
        case SWT.BOLD:
            style = java.awt.Font.BOLD;
            break;
        default:
            style = java.awt.Font.PLAIN;
            break;
    }
    int height = (int) Math.round(fontData.getHeight() * device.getDPI().y 
            / 72.0);
    // hack to ensure the newly created awt fonts will be rendered with the
    // same height as the swt one
    if (ensureSameSize) {
        GC tmpGC = new GC(device);
        Font tmpFont = new Font(device, fontData);
        tmpGC.setFont(tmpFont);
        JPanel DUMMY_PANEL = new JPanel();
        java.awt.Font tmpAwtFont = new java.awt.Font(fontData.getName(), 
                style, height);
        if (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) 
                > tmpGC.textExtent(Az).x) {
            while (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) 
                    > tmpGC.textExtent(Az).x) {
                height--;                
                tmpAwtFont = new java.awt.Font(fontData.getName(), style, 
                        height);
            }
        }
        else if (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) 
                < tmpGC.textExtent(Az).x) {
            while (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) 
                    < tmpGC.textExtent(Az).x) {
                height++;
                tmpAwtFont = new java.awt.Font(fontData.getName(), style, 
                        height);
            }
        }
        tmpFont.dispose();
        tmpGC.dispose();
    }
    return new java.awt.Font(fontData.getName(), style, height);
}
 
Example 17
Source File: LoginDialog.java    From nebula with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Create a default image. It is a port of the image used by the Login Box
 * in the project SwingX
 *
 * @param w width
 * @param h height
 * @return a default image (blue wave)
 */
private Image createDefaultImage(final int w, final int h) {
	final Display display = Display.getCurrent();
	final Color backgroundColor = new Color(display, 49, 121, 242);
	final Color gradientColor1 = new Color(display, 155, 185, 245);
	final Color gradientColor2 = new Color(display, 53, 123, 242);

	final Image img = new Image(display, w, h);
	final GC gc = new GC(img);
	gc.setAdvanced(true);
	gc.setAntialias(SWT.ON);
	gc.setBackground(backgroundColor);
	gc.fillRectangle(0, 0, w, h);

	final Path curveShape = new Path(display);
	curveShape.moveTo(0, h * .6f);
	curveShape.cubicTo(w * .167f, h * 1.2f, w * .667f, h * -.5f, w, h * .75f);
	curveShape.lineTo(w, h);
	curveShape.lineTo(0, h);
	curveShape.lineTo(0, h * .8f);
	curveShape.close();

	final Pattern pattern = new Pattern(display, 0, 0, 1, h * 1.2f, gradientColor1, gradientColor2);
	gc.setBackgroundPattern(pattern);
	gc.fillPath(curveShape);

	final Font font = new Font(display, "Arial Bold", 30, SWT.NONE);
	gc.setFont(font);
	gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
	final Point textSize = gc.stringExtent(ResourceManager.getLabel(ResourceManager.LOGIN));
	gc.drawString(ResourceManager.getLabel(ResourceManager.LOGIN), (int) (w * .05f), (h - textSize.y) / 2, true);

	font.dispose();
	curveShape.dispose();
	pattern.dispose();
	backgroundColor.dispose();
	gradientColor1.dispose();
	gradientColor2.dispose();
	gc.dispose();
	return img;
}
 
Example 18
Source File: AbstractButtonPainter.java    From nebula with Eclipse Public License 2.0 4 votes vote down vote up
public void paintText(GC gc, IColorManager colorManager, ISettings settings, Rectangle bounds, Rectangle imageBounds, boolean hover, boolean selected, String text) {
		if (text == null || text.length() == 0)
			return;
		
		int imgWidth = 0;
		if (imageBounds != null)
			imgWidth = imageBounds.width;		

		Point se = gc.stringExtent(text);
		int textTop = (CustomButton.BUTTON_HEIGHT - se.y) / 2;
		//int textWidth = se.x;
		
		Font oldFont = gc.getFont();
		Font newFont = settings.getButtonTextFont(oldFont);
		gc.setFont(newFont);
		
		// skip image if image is null (TODO: feature to let users align imageless button text with image button text?
		//int spaceNeeded = (imgWidth > 0 ? mLeftSpacer + imgWidth : 0) + mRightSpacer + textWidth;
		
		String textToUse = text;
		
		// out of bounds?
/*		if (spaceNeeded > bounds.width) {
			//TODO: truncate text
			char[] total = mText.toCharArray();
			StringBuffer fin = new StringBuffer();
			int loop = 0;
			while (gc.stringExtent(fin.toString()).x < (bounds.width - gc.stringExtent("...").x)) {
				fin.append(total[loop]);
				loop++;
			}
			fin.append("...");
			textToUse = fin.toString();
		}
*/				
		gc.setForeground(settings.getButtonTextColor());
		gc.drawString(textToUse, (imgWidth > 0 ? settings.getLeftButtonTextSpacing() + imgWidth : 0) + settings.getButtonTextImageSpacing(), textTop-1, true);
		
		// reset fonts
		newFont.dispose();
		gc.setFont(oldFont);
	}
 
Example 19
Source File: GraphicsHelper.java    From gama with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Create an awt font by converting as much information as possible from the provided swt <code>FontData</code>.
 * <p>
 * Generally speaking, given a font size, an swt font will display differently on the screen than the corresponding
 * awt one. Because the SWT toolkit use native graphical ressources whenever it is possible, this fact is platform
 * dependent. To address this issue, it is possible to enforce the method to return an awt font with the same height
 * as the swt one.
 *
 * @param device
 *            The swt device being drawn on (display or gc device).
 * @param fontData
 *            The swt font to convert.
 * @param ensureSameSize
 *            A boolean used to enforce the same size (in pixels) between the swt font and the newly created awt
 *            font.
 * @return An awt font converted from the provided swt font.
 */
public static java.awt.Font toAwtFont(final Device device, final FontData fontData, final boolean ensureSameSize) {
	int style;
	switch (fontData.getStyle()) {
		case SWT.NORMAL:
			style = java.awt.Font.PLAIN;
			break;
		case SWT.ITALIC:
			style = java.awt.Font.ITALIC;
			break;
		case SWT.BOLD:
			style = java.awt.Font.BOLD;
			break;
		default:
			style = java.awt.Font.PLAIN;
			break;
	}
	int height = (int) Math.round(fontData.getHeight() * device.getDPI().y / 72.0);
	// hack to ensure the newly created awt fonts will be rendered with the
	// same height as the swt one
	if (ensureSameSize) {
		final GC tmpGC = new GC(device);
		final Font tmpFont = new Font(device, fontData);
		tmpGC.setFont(tmpFont);
		final JPanel DUMMY_PANEL = new JPanel();
		java.awt.Font tmpAwtFont = new java.awt.Font(fontData.getName(), style, height);
		if (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) > tmpGC.textExtent(Az).x) {
			while (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) > tmpGC.textExtent(Az).x) {
				height--;
				tmpAwtFont = new java.awt.Font(fontData.getName(), style, height);
			}
		} else if (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) < tmpGC.textExtent(Az).x) {
			while (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) < tmpGC.textExtent(Az).x) {
				height++;
				tmpAwtFont = new java.awt.Font(fontData.getName(), style, height);
			}
		}
		tmpFont.dispose();
		tmpGC.dispose();
	}
	return new java.awt.Font(fontData.getName(), style, height);
}
 
Example 20
Source File: SwtChartViewerSelector.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
private final void showException( GC g2d, Exception ex )
{
	String sWrappedException = ex.getClass( ).getName( );
	Throwable th = ex;
	while ( ex.getCause( ) != null )
	{
		ex = (Exception) ex.getCause( );
	}
	String sException = ex.getClass( ).getName( );
	if ( sWrappedException.equals( sException ) )
	{
		sWrappedException = null;
	}

	String sMessage = null;
	if ( th instanceof BirtException )
	{
		sMessage = ( (BirtException) th ).getLocalizedMessage( );
	}
	else
	{
		sMessage = ex.getMessage( );
	}

	if ( sMessage == null )
	{
		sMessage = "<null>";//$NON-NLS-1$
	}
	StackTraceElement[] stea = ex.getStackTrace( );
	Point d = this.getSize( );

	Device dv = Display.getCurrent( );
	Font fo = new Font( dv, "Courier", SWT.BOLD, 16 );//$NON-NLS-1$
	g2d.setFont( fo );
	FontMetrics fm = g2d.getFontMetrics( );
	g2d.setBackground( dv.getSystemColor( SWT.COLOR_WHITE ) );
	g2d.fillRectangle( 20, 20, d.x - 40, d.y - 40 );
	g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) );
	g2d.drawRectangle( 20, 20, d.x - 40, d.y - 40 );
	g2d.setClipping( 20, 20, d.x - 40, d.y - 40 );
	int x = 25, y = 20 + fm.getHeight( );
	g2d.drawString( "Exception:", x, y );//$NON-NLS-1$
	x += g2d.textExtent( "Exception:" ).x + 5;//$NON-NLS-1$
	g2d.setForeground( dv.getSystemColor( SWT.COLOR_RED ) );
	g2d.drawString( sException, x, y );
	x = 25;
	y += fm.getHeight( );
	if ( sWrappedException != null )
	{
		g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) );
		g2d.drawString( "Wrapped In:", x, y );//$NON-NLS-1$
		x += g2d.textExtent( "Wrapped In:" ).x + 5;//$NON-NLS-1$
		g2d.setForeground( dv.getSystemColor( SWT.COLOR_RED ) );
		g2d.drawString( sWrappedException, x, y );
		x = 25;
		y += fm.getHeight( );
	}
	g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) );
	y += 10;
	g2d.drawString( "Message:", x, y );//$NON-NLS-1$
	x += g2d.textExtent( "Message:" ).x + 5;//$NON-NLS-1$
	g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLUE ) );
	g2d.drawString( sMessage, x, y );
	x = 25;
	y += fm.getHeight( );
	g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) );
	y += 10;
	g2d.drawString( "Trace:", x, y );//$NON-NLS-1$
	x = 40;
	y += fm.getHeight( );
	g2d.setForeground( dv.getSystemColor( SWT.COLOR_DARK_GREEN ) );
	for ( int i = 0; i < stea.length; i++ )
	{
		g2d.drawString( stea[i].getClassName( ) + ":"//$NON-NLS-1$
				+ stea[i].getMethodName( ) + "(...):"//$NON-NLS-1$
				+ stea[i].getLineNumber( ), x, y );
		x = 40;
		y += fm.getHeight( );
	}
	fo.dispose( );
}