Java Code Examples for org.eclipse.swt.widgets.Display#getCurrent()

The following examples show how to use org.eclipse.swt.widgets.Display#getCurrent() . 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: swt-bling   File: DisplaySafeTest.java    License: MIT License 6 votes vote down vote up
@Test
public void getLatestDisplay_CurrentNotNull_DisplayUnchanged() {
  Display display = Display.getCurrent();
  DisplaySafe safe = new DisplaySafe(display);
  DisplaySafe safeSpy = spy(safe);
  when(safeSpy.getCurrent()).thenReturn(display);

  Display sameDisplay = null;

  try {
    sameDisplay = safeSpy.getLatestDisplay();
  } catch (DisplaySafe.NullDisplayException nde) {
    fail();
  }

  assertEquals(display, sameDisplay);
}
 
Example 2
Source Project: tlaplus   File: UIHelper.java    License: MIT License 6 votes vote down vote up
/**
 * This method returns a built in SWT image. This method was mostly copied
 * directly from {@link IconAndMessageDialog}. The argument should be an
 * icon constant defined in {@link SWT}. These constants begin with "ICON".
 * This method returns null if the argument is not such a constant or if the
 * platform does not define and image corresponding to that constant. I
 * would assume you figure that one out by trial and error.
 * 
 * @param imageID
 * @return
 */
public static Image getSWTImage(final int imageID) {

	Shell shell = getShell();
	final Display display;
	if (shell == null || shell.isDisposed()) {
		display = Display.getCurrent();
	} else {
		display = shell.getDisplay();
	}

	final Image[] image = new Image[1];
	display.syncExec(new Runnable() {
		public void run() {
			image[0] = display.getSystemImage(imageID);
		}
	});

	return image[0];

}
 
Example 3
/**
 * Update the horiontal scrollbar.
 */
private void updateXScrollBar() {
    if (Display.getCurrent() != null) {
        Display.getCurrent().syncExec(new Runnable() {
            public void run() {
                ScrollBar scroll = getHorizontalBar();
                // scroll may be null
                if (scroll != null) {
                    _oldHorizontalScroll = -1; // make sure no optimization will be applied
                    scroll.setMinimum(0);
                    scroll.setMaximum(getTotalWidth() - getFixedColumnsWidth());
                    scroll.setThumb(getWidth() - getFixedColumnsWidth());
                    scroll.setIncrement(50); // increment for arrows
                    scroll.setPageIncrement(getWidth()); // page increment areas
                }
            }
        });
    }
}
 
Example 4
/**
 * Display arbitary url
 * 
 * @param url
 */
public synchronized void displayURL( final String url )
{
	Display defaultDisplay = Display.getDefault( );

	if ( defaultDisplay == Display.getCurrent( ) )
	{
		uiDisplayURL( url );
	}
	else
	{
		defaultDisplay.syncExec( new Runnable( ) {

			public void run( )
			{
				uiDisplayURL( url );
			}
		} );
	}
}
 
Example 5
Source Project: nebula   File: TimeSlot.java    License: Eclipse Public License 2.0 6 votes vote down vote up
public Point computeSize(int wHint, int hHint, boolean changed) {
	if (preferredSize.x == -1 || changed) {
		preferredSize.x = getSize().x;
		Display display = Display.getCurrent();
		GC gc = new GC(display);
		try {
			Font font = display.getSystemFont();
			gc.setFont(font);
			FontMetrics fm = gc.getFontMetrics();
			preferredSize.y = fm.getHeight();
		} finally {
			gc.dispose();
		}
	}
	return preferredSize;
}
 
Example 6
/**
 * @return the small {@link Image} that can be used as placeholder for missing image.
 */
private static Image getMissingImage() {
	Image image = new Image(Display.getCurrent(), MISSING_IMAGE_SIZE, MISSING_IMAGE_SIZE);
	//
	GC gc = new GC(image);
	gc.setBackground(getColor(SWT.COLOR_RED));
	gc.fillRectangle(0, 0, MISSING_IMAGE_SIZE, MISSING_IMAGE_SIZE);
	gc.dispose();
	//
	return image;
}
 
Example 7
private static void createImages() {
	images = new Image[5];
	final String[] fileNames = new String[] { "add.png", "bell.png", "feed.png", "house.png", "script.png" };
	for (int i = 0; i < 5; i++) {
		final Image image = new Image(Display.getCurrent(), BreadcrumbSnippet.class.getResourceAsStream(fileNames[i]));
		images[i] = image;
	}
}
 
Example 8
Source Project: swt-bling   File: ColorFactoryTest.java    License: MIT License 5 votes vote down vote up
@Before
public void setup() throws Exception {
  if (Display.getCurrent() != null) Display.getCurrent().dispose();
  DeviceData data = new DeviceData();
  data.tracking = true;
  display = new Display(data);
  shell = new Shell(display);
}
 
Example 9
public Color getBackground(Object element) {
	if (isUnmodifiable(element)) {
		Display display= Display.getCurrent();
		return display.getSystemColor(SWT.COLOR_INFO_BACKGROUND);
	}
	return null;
}
 
Example 10
public static KeyInstanceManager getInstance ( final Display display )
{
    if ( display == null )
    {
        SWT.error ( SWT.ERROR_NULL_ARGUMENT );
    }

    if ( Display.getCurrent () != display )
    {
        SWT.error ( SWT.ERROR_THREAD_INVALID_ACCESS );
    }

    if ( display.isDisposed () )
    {
        SWT.error ( SWT.ERROR_DEVICE_DISPOSED );
    }

    KeyInstanceManager mgr = instanceMap.get ( display );
    if ( mgr != null )
    {
        return mgr;
    }

    mgr = new KeyInstanceManager ( display );
    instanceMap.put ( display, mgr );

    display.disposeExec ( new Runnable () {

        @Override
        public void run ()
        {
            disposeDisplay ( display );
        }
    } );

    return mgr;
}
 
Example 11
/**
 * Returns the current or default display.
 *
 * @return the current or default display
 */
protected static Display getDisplay() {
    Display display = Display.getCurrent();
    // may be null if outside the UI thread
    if (display == null) {
        display = Display.getDefault();
    }
    return display;
}
 
Example 12
/**
 * @return the small {@link Image} that can be used as placeholder for missing image.
 */
private static Image getMissingImage() {
	Image image = new Image(Display.getCurrent(), MISSING_IMAGE_SIZE, MISSING_IMAGE_SIZE);
	//
	GC gc = new GC(image);
	gc.setBackground(getColor(SWT.COLOR_RED));
	gc.fillRectangle(0, 0, MISSING_IMAGE_SIZE, MISSING_IMAGE_SIZE);
	gc.dispose();
	//
	return image;
}
 
Example 13
/**
 * Returns an {@link Image} encoded by the specified {@link InputStream}.
 * 
 * @param stream
 *            the {@link InputStream} encoding the image data
 * @return the {@link Image} encoded by the specified input stream
 */
protected static Image getImage(InputStream stream) throws IOException {
	try {
		Display display = Display.getCurrent();
		ImageData data = new ImageData(stream);
		if (data.transparentPixel > 0) {
			return new Image(display, data, data.getTransparencyMask());
		}
		return new Image(display, data);
	} finally {
		stream.close();
	}
}
 
Example 14
Source Project: ColorMixer   File: SWTResourceManager.java    License: MIT License 5 votes vote down vote up
/**
 * @return the small {@link Image} that can be used as placeholder for missing image.
 */
private static Image getMissingImage() {
	Image image = new Image(Display.getCurrent(), MISSING_IMAGE_SIZE, MISSING_IMAGE_SIZE);
	//
	GC gc = new GC(image);
	gc.setBackground(getColor(SWT.COLOR_RED));
	gc.fillRectangle(0, 0, MISSING_IMAGE_SIZE, MISSING_IMAGE_SIZE);
	gc.dispose();
	//
	return image;
}
 
Example 15
/**
 * {@inheritDoc}
 */
public ImageData getImageData() {
    Image img = new Image(Display.getCurrent(), this.getClass().getResourceAsStream(rscString));
    return img.getImageData();
}
 
Example 16
Source Project: Rel   File: Editor.java    License: Apache License 2.0 4 votes vote down vote up
private void registerMultiLineEditorColumn(IConfigRegistry configRegistry, String columnLabel) {
	// configure the multi line text editor
	configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR,
			new MultiLineTextCellEditor(false) {
				protected Control activateCell(Composite parent, Object originalCanonicalValue) {
					editorBeenOpened(getRowIndex(), getColumnIndex());
					return super.activateCell(parent, originalCanonicalValue);
				}

				public void close() {
					editorBeenClosed(getRowIndex(), getColumnIndex());
					super.close();
				}
			}, DisplayMode.NORMAL, columnLabel);

	Style cellStyle = new Style();
	cellStyle.setAttributeValue(CellStyleAttributes.HORIZONTAL_ALIGNMENT, HorizontalAlignmentEnum.LEFT);
	configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, cellStyle, DisplayMode.NORMAL,
			columnLabel);
	configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, cellStyle, DisplayMode.EDIT,
			columnLabel);

	// configure custom dialog settings
	Display display = Display.getCurrent();
	Map<String, Object> editDialogSettings = new HashMap<String, Object>();
	editDialogSettings.put(ICellEditDialog.DIALOG_SHELL_TITLE, "Edit");
	editDialogSettings.put(ICellEditDialog.DIALOG_SHELL_ICON, display.getSystemImage(SWT.ICON_WARNING));
	editDialogSettings.put(ICellEditDialog.DIALOG_SHELL_RESIZABLE, Boolean.TRUE);

	Point size = new Point(400, 300);
	editDialogSettings.put(ICellEditDialog.DIALOG_SHELL_SIZE, size);

	int screenWidth = display.getBounds().width;
	int screenHeight = display.getBounds().height;
	Point location = new Point((screenWidth / (2 * display.getMonitors().length)) - (size.x / 2),
			(screenHeight / 2) - (size.y / 2));
	editDialogSettings.put(ICellEditDialog.DIALOG_SHELL_LOCATION, location);

	configRegistry.registerConfigAttribute(EditConfigAttributes.EDIT_DIALOG_SETTINGS, editDialogSettings,
			DisplayMode.EDIT, columnLabel);
}
 
Example 17
Source Project: birt   File: FormWidgetFactory.java    License: Eclipse Public License 1.0 4 votes vote down vote up
/**
 * private constructor.
 */
private FormWidgetFactory( )
{
	super( Display.getCurrent( ) );
}
 
Example 18
Source Project: birt   File: JavaScriptViewer.java    License: 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 19
private void drawGridLines(PaintEvent e, boolean isHeader) {
	Color oldColor = e.gc.getForeground();
	try {
		// Get the colors we need
		Display display = Display.getCurrent();
		Color lineColor = display
				.getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW);
		Color secondaryColor = display
				.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW);
		Color hilightColor = display
				.getSystemColor(SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW);
		if (!isHeader) {
			lineColor = display
					.getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW);
		}

		// Get the control
		Control toPaint = (Control) e.widget;
		Point controlSize = toPaint.getSize();

		// Draw the bottom line(s)
		e.gc.setForeground(lineColor);
		e.gc.drawLine(0, controlSize.y - 1, controlSize.x,
				controlSize.y - 1);
		if (isHeader) {
			e.gc.setForeground(secondaryColor);
			e.gc.drawLine(0, controlSize.y - 2, controlSize.x,
					controlSize.y - 2);
			e.gc.setForeground(hilightColor);
			e.gc.drawLine(0, 1, controlSize.x, 1);
		}

		// Now draw lines around the child controls, if there are any
		if (toPaint instanceof Composite) {
			Composite row = (Composite) toPaint;
			Control[] children = row.getChildren();
			for (int i = 0; i < children.length; i++) {
				Rectangle childBounds = children[i].getBounds();

				// Paint the beginning lines
				if (isHeader) {
					e.gc.setForeground(hilightColor);
					e.gc.drawLine(childBounds.x - 2, 1, childBounds.x - 2,
							controlSize.y - 2);
				}

				// Paint the ending lines
				e.gc.setForeground(lineColor);
				int lineLeft = childBounds.x + childBounds.width + 1;
				e.gc.drawLine(lineLeft, 0, lineLeft, controlSize.y);
				if (isHeader) {
					e.gc.setForeground(secondaryColor);
					e.gc.drawLine(lineLeft - 1, 0, lineLeft - 1,
							controlSize.y - 1);
				}
			}
		}
	} finally {
		e.gc.setForeground(oldColor);
	}
}
 
Example 20
/**
 * Returns the system {@link Color} matching the specific ID.
 * 
 * @param systemColorID
 *            the ID value for the color
 * @return the system {@link Color} matching the specific ID
 */
public static Color getColor(int systemColorID) {
	Display display = Display.getCurrent();
	return display.getSystemColor(systemColorID);
}