Java Code Examples for org.eclipse.swt.widgets.Control#setFont()

The following examples show how to use org.eclipse.swt.widgets.Control#setFont() . These examples are extracted from open source projects. 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 Project: google-cloud-eclipse   File: FontUtil.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Converts the font of the control by adding a single style bit, unless the font already have
 * that style.
 * <p>
 * If the font is converted, it will attach a {@link DisposeListener}
 * to the <code>control</code> to dispose the font when it's not needed anymore.
 * <p>
 * <em>If converting fonts is a frequent operation, this method will create
 * several {@link DisposeListener}s that can lead to high resource allocation</em>
 *
 * @param control whose font will be changed
 * @param style e.g. SWT.BOLD or SWT.ITALIC
 */
public static void convertFont(Control control, int style) {
  for (FontData fontData : control.getFont().getFontData()) {
    if (hasStyle(fontData, style)) {
      return;
    }
  }
  FontDescriptor fontDescriptor = FontDescriptor.createFrom(control.getFont()).setStyle(style);
  final Font newFont = fontDescriptor.createFont(control.getDisplay());
  control.setFont(newFont);
  control.addDisposeListener(new DisposeListener() {
    @Override
    public void widgetDisposed(DisposeEvent event) {
      newFont.dispose();
    }
  });
}
 
Example 2
Source Project: saros   File: FontUtils.java    License: GNU General Public License v2.0 6 votes vote down vote up
public static void changeFontSizeBy(Control control, int fontSizeInc) {
  FontData[] fontData = control.getFont().getFontData();
  for (int i = 0; i < fontData.length; ++i)
    fontData[i].setHeight(fontData[i].getHeight() + fontSizeInc);

  final Font newFont = new Font(control.getDisplay(), fontData);
  control.setFont(newFont);

  // Since you created the font, you must dispose it
  control.addDisposeListener(
      new DisposeListener() {
        @Override
        public void widgetDisposed(DisposeEvent e) {
          newFont.dispose();
        }
      });
}
 
Example 3
private void createCostSum(Composite comp, FormToolkit tk) {
	if (costs == Costs.NONE)
		return;
	double v = result.totalCosts;
	String s;
	String c;
	if (costs == Costs.NET_COSTS) {
		s = M.TotalNetcosts;
		c = asCosts(v);
	} else {
		s = M.TotalAddedValue;
		c = asCosts(v == 0 ? 0 : -v);
	}

	Control label = tk.createLabel(comp, s + ": " + c);
	label.setFont(UI.boldFont());
}
 
Example 4
private void applyFont(final Control widget, final FontData fd) {
	if (widget.getFont() != null && !widget.getFont().equals(widget.getDisplay().getSystemFont())) {
		widget.getFont().dispose();
	}
	final Font newFont = new Font(widget.getDisplay(), fd);
	widget.setFont(newFont);
	widget.addListener(SWT.Dispose, e -> {
		if (newFont != null && !newFont.isDisposed()) {
			newFont.dispose();
		}
	});

}
 
Example 5
private void applyFont(final Control widget, final FontData fd) {
	if (widget.getFont() != null && !widget.getFont().equals(widget.getDisplay().getSystemFont())) {
		widget.getFont().dispose();
	}
	final Font newFont = new Font(widget.getDisplay(), fd);
	widget.setFont(newFont);
	widget.addListener(SWT.Dispose, e -> {
		if (newFont != null && !newFont.isDisposed()) {
			newFont.dispose();
		}
	});
}
 
Example 6
private void setColorAndFont(Control control, Color foreground, Color background, Font font) {
	control.setForeground(foreground);
	control.setBackground(background);
	control.setFont(font);

	if (control instanceof Composite) {
		Control[] children= ((Composite) control).getChildren();
		for (int i= 0; i < children.length; i++) {
			setColorAndFont(children[i], foreground, background, font);
		}
	}
}
 
Example 7
private void setColorAndFont(Control control, Color foreground, Color background, Font font) {
    control.setForeground(foreground);
    control.setBackground(background);
    control.setFont(font);
    
    if (control instanceof Composite) {
        Control[] children= ((Composite) control).getChildren();
        for (Control element : children) {
            setColorAndFont(element, foreground, background, font);
        }
    }
}
 
Example 8
private void setColorAndFont(Control control, Color foreground, Color background, Font font) {
	control.setForeground(foreground);
	control.setBackground(background);
	control.setFont(font);

	if (control instanceof Composite) {
		Control[] children = ((Composite) control).getChildren();
		for (int i = 0; i < children.length; i++) {
			setColorAndFont(children[i], foreground, background, font);
		}
	}
}
 
Example 9
private void setColorAndFont(Control control, Color foreground, Color background, Font font) {
	control.setForeground(foreground);
	control.setBackground(background);
	control.setFont(font);

	if (control instanceof Composite) {
		Control[] children= ((Composite) control).getChildren();
		for (int i= 0; i < children.length; i++) {
			setColorAndFont(children[i], foreground, background, font);
		}
	}
}
 
Example 10
/**
 * Sets the font and the grid data for some control.
 */
private void setFillHorizontalLayoutDataAndFont(Control control, Font font) {
    GridData data = new GridData();
    data.horizontalAlignment = GridData.FILL;
    data.grabExcessHorizontalSpace = true;
    control.setLayoutData(data);
    control.setFont(font);
}
 
Example 11
Source Project: saros   File: FontUtils.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static void makeBold(Control control) {
  FontData[] boldFontData = modifyFontData(control.getFont().getFontData(), SWT.BOLD);

  final Font newFont = new Font(control.getDisplay(), boldFontData);
  control.setFont(newFont);

  // Since you created the font, you must dispose it
  control.addDisposeListener(
      new DisposeListener() {
        @Override
        public void widgetDisposed(DisposeEvent e) {
          newFont.dispose();
        }
      });
}
 
Example 12
private void setColorAndFont(Control control, Color foreground, Color background, Font font) {
	control.setForeground(foreground);
	control.setBackground(background);
	control.setFont(font);

	if (control instanceof Composite) {
		Control[] children= ((Composite) control).getChildren();
		for (int i= 0; i < children.length; i++) {
			setColorAndFont(children[i], foreground, background, font);
		}
	}
}
 
Example 13
Source Project: arx   File: SWTUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Changes a control's font
 * @param control
 * @param style
 */
public static void changeFont(Control control, int style) {
    FontDescriptor boldDescriptor = FontDescriptor.createFrom(control.getFont()).setStyle(style);
    final Font boldFont = boldDescriptor.createFont(control.getDisplay());
    control.setFont(boldFont);
    control.addDisposeListener(new DisposeListener() {
        @Override
        public void widgetDisposed(DisposeEvent arg0) {
            if (boldFont != null && !boldFont.isDisposed()) {
                boldFont.dispose();
            }
        }
        
    });
}
 
Example 14
Source Project: hop   File: PropsUi.java    License: Apache License 2.0 4 votes vote down vote up
public void setLook( final Control control, int style ) {
  if ( this.isOSLookShown() && style != WIDGET_STYLE_FIXED ) {
    return;
  }

  final GuiResource gui = GuiResource.getInstance();
  Font font = null;
  Color background = null;

  switch ( style ) {
    case WIDGET_STYLE_DEFAULT:
      background = gui.getColorBackground();
      if ( control instanceof Group && OS.indexOf( "mac" ) > -1 ) {
        control.addPaintListener( paintEvent -> {
          paintEvent.gc.setBackground( gui.getColorBackground() );
          paintEvent.gc.fillRectangle( 2, 0, control.getBounds().width - 8, control.getBounds().height - 20 );
        } );
      }
      font = null; // GuiResource.getInstance().getFontDefault();
      break;
    case WIDGET_STYLE_FIXED:
      if ( !this.isOSLookShown() ) {
        background = gui.getColorBackground();
      }
      font = gui.getFontFixed();
      break;
    case WIDGET_STYLE_TABLE:
      background = gui.getColorBackground();
      font = null; // gui.getFontGrid();
      break;
    case WIDGET_STYLE_NOTEPAD:
      background = gui.getColorBackground();
      font = gui.getFontNote();
      break;
    case WIDGET_STYLE_GRAPH:
      background = gui.getColorBackground();
      font = gui.getFontGraph();
      break;
    case WIDGET_STYLE_TOOLBAR:
      background = GuiResource.getInstance().getColorDemoGray();
      break;
    case WIDGET_STYLE_TAB:
      background = GuiResource.getInstance().getColorWhite();
      CTabFolder tabFolder = (CTabFolder) control;
      tabFolder.setSimple( false );
      tabFolder.setBorderVisible( true );
      // need to make a copy of the tab selection background color to get around PDI-13940
      Color c = GuiResource.getInstance().getColorTab();
      Color tabColor = new Color( c.getDevice(), c.getRed(), c.getGreen(), c.getBlue() );
      tabFolder.setSelectionBackground( tabColor );
      break;
    default:
      background = gui.getColorBackground();
      font = null; // gui.getFontDefault();
      break;
  }

  if ( font != null && !font.isDisposed() ) {
    control.setFont( font );
  }

  if ( background != null && !background.isDisposed() ) {
    if ( control instanceof Button ) {
      Button b = (Button) control;
      if ( ( b.getStyle() & SWT.PUSH ) != 0 ) {
        return;
      }
    }
    control.setBackground( background );
  }
}
 
Example 15
private static void applyWidget ( final Control label, final CurrentStyle style )
{
    label.setForeground ( style.foreground );
    label.setBackground ( style.background );
    label.setFont ( style.font );
}
 
Example 16
Source Project: pentaho-kettle   File: PropsUI.java    License: Apache License 2.0 4 votes vote down vote up
public void setLook( final Control control, int style ) {
  if ( this.isOSLookShown() && style != WIDGET_STYLE_FIXED ) {
    return;
  }

  final GUIResource gui = GUIResource.getInstance();
  Font font = null;
  Color background = null;

  switch ( style ) {
    case WIDGET_STYLE_DEFAULT:
      background = gui.getColorBackground();
      if ( control instanceof Group && OS.indexOf( "mac" ) > -1 ) {
        control.addPaintListener( new PaintListener() {
          @Override
          public void paintControl( PaintEvent paintEvent ) {
            paintEvent.gc.setBackground( gui.getColorBackground() );
            paintEvent.gc.fillRectangle( 2, 0, control.getBounds().width - 8, control.getBounds().height - 20 );
          }
        } );
      }
      font = null; // GUIResource.getInstance().getFontDefault();
      break;
    case WIDGET_STYLE_FIXED:
      if ( !this.isOSLookShown() ) {
        background = gui.getColorBackground();
      }
      font = gui.getFontFixed();
      break;
    case WIDGET_STYLE_TABLE:
      background = gui.getColorBackground();
      font = null; // gui.getFontGrid();
      break;
    case WIDGET_STYLE_NOTEPAD:
      background = gui.getColorBackground();
      font = gui.getFontNote();
      break;
    case WIDGET_STYLE_GRAPH:
      background = gui.getColorBackground();
      font = gui.getFontGraph();
      break;
    case WIDGET_STYLE_TOOLBAR:
      background = GUIResource.getInstance().getColorDemoGray();
      break;
    case WIDGET_STYLE_TAB:
      background = GUIResource.getInstance().getColorWhite();
      CTabFolder tabFolder = (CTabFolder) control;
      tabFolder.setSimple( false );
      tabFolder.setBorderVisible( true );
      // need to make a copy of the tab selection background color to get around PDI-13940
      Color c = GUIResource.getInstance().getColorTab();
      Color tabColor = new Color( c.getDevice(), c.getRed(), c.getGreen(), c.getBlue() );
      tabFolder.setSelectionBackground( tabColor );
      break;
    default:
      background = gui.getColorBackground();
      font = null; // gui.getFontDefault();
      break;
  }

  if ( font != null && !font.isDisposed() ) {
    control.setFont( font );
  }

  if ( background != null && !background.isDisposed() ) {
    if ( control instanceof Button ) {
      Button b = (Button) control;
      if ( ( b.getStyle() & SWT.PUSH ) != 0 ) {
        return;
      }
    }
    control.setBackground( background );
  }
}
 
Example 17
public void show() {
	super.show();

	IFigure fig = getEditPart().getFigure();

	Control control = getCellEditor().getControl();
	this.zoomLevelFont = getZoomLevelFont(fig.getFont(), control.getDisplay());

	control.setFont(this.zoomLevelFont);

	// since the font's have been resized, we need to resize the Text
	// control...
	getLocator().relocate(getCellEditor());

}