Example usage for org.eclipse.swt.widgets Display getCurrent

List of usage examples for org.eclipse.swt.widgets Display getCurrent

Introduction

In this page you can find the example usage for org.eclipse.swt.widgets Display getCurrent.

Prototype

public static Display getCurrent() 

Source Link

Document

Returns the display which the currently running thread is the user-interface thread for, or null if the currently running thread is not a user-interface thread for any display.

Usage

From source file:org.mwc.debrief.editable.test.EditableTests.java

/**
 * Process UI input but do not return for the specified time interval.
 * //from w  w w  . j  a va 2 s .  c o  m
 * @param waitTimeMillis
 *          the number of milliseconds
 */
protected static void delay(final long waitTimeMillis) {
    final Display display = Display.getCurrent();

    // If this is the user interface thread, then process input
    if (display != null) {
        final long endTimeMillis = System.currentTimeMillis() + waitTimeMillis;
        while (System.currentTimeMillis() < endTimeMillis) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.update();
    }

    // Otherwise perform a simple sleep
    else {
        try {
            Thread.sleep(waitTimeMillis);
        } catch (final InterruptedException e) {
            // ignored
        }
    }
}

From source file:com.planetmayo.debrief.satc_rcp.views.MaintainContributionsView.java

private Group initPerformanceGraph(Composite parent) {
    GridData gridData = new GridData();
    gridData.horizontalAlignment = SWT.FILL;
    gridData.grabExcessHorizontalSpace = true;
    gridData.heightHint = 200;//w  w w .j a  va2 s  . c  o m

    Group group = new Group(parent, SWT.SHADOW_ETCHED_IN);
    FillLayout fillLayout = new FillLayout();
    fillLayout.marginWidth = 5;
    fillLayout.marginHeight = 5;
    group.setLayout(fillLayout);
    group.setLayoutData(gridData);
    // group.setText("Performance");

    // we need the color black several times
    _colorBlack = new Color(Display.getCurrent(), 0, 0, 0);

    // generate the chart
    performanceChart = new Chart(group, SWT.NONE);

    // format the chart
    performanceChart.getLegend().setVisible(false);
    performanceChart.getTitle().setVisible(true);
    performanceChart.setForeground(_colorBlack);
    performanceChart.getTitle().setText(PERFORMANCE_TITLE + " Pending");
    performanceChart.getTitle().setForeground(_colorBlack);

    // now give the chart our data series
    // ok, now for the x axis
    IAxis xAxis = performanceChart.getAxisSet().getXAxis(0);
    xAxis.getTitle().setVisible(false);
    xAxis.adjustRange();
    xAxis.getTick().setForeground(_colorBlack);

    // and the y axis
    IAxis yAxis = performanceChart.getAxisSet().getYAxis(0);
    yAxis.adjustRange();
    yAxis.enableLogScale(true);
    yAxis.getTick().setForeground(_colorBlack);
    yAxis.getTitle().setForeground(_colorBlack);
    yAxis.getTitle().setText("Weighted error");

    return group;
}

From source file:GraphicsExample.java

public void paint(GC gc, int width, int height) {
    if (!example.checkAdvancedGraphics())
        return;/*  w w  w.jav a  2  s.c  o  m*/
    Display display = Display.getCurrent();
    if (image == null) {
        image = example.loadImage(Display.getCurrent(), "irmaos.jpg");
        Rectangle rect = image.getBounds();
        FontData fd = display.getSystemFont().getFontData()[0];
        font = new Font(display, fd.getName(), rect.height / 4, SWT.BOLD);
        gc.setFont(font);
        Point size = gc.stringExtent(text);
        textWidth = size.x;
        textHeight = size.y;
    }
    Path path = new Path(display);
    path.addString(text, x, y, font);
    gc.setClipping(path);
    Rectangle rect = image.getBounds();
    gc.drawImage(image, 0, 0, rect.width, rect.height, 0, 0, width, height);
    gc.setClipping((Rectangle) null);
    gc.setForeground(display.getSystemColor(SWT.COLOR_BLUE));
    gc.drawPath(path);
}

From source file:org.gumtree.vis.swt.PlotComposite.java

public void handleException(final Exception e) {
    Display display = Display.getCurrent();
    if (display == null) {
        display = Display.getDefault();//from  w  w w  .  j a  v a2 s .  c  o m
    }
    display.asyncExec(new Runnable() {
        @Override
        public void run() {
            if (getShell() != null) {
                MessageBox messageBox = new MessageBox(getShell(), SWT.ICON_WARNING | SWT.OK);
                messageBox.setText("Failed to Save");
                messageBox.setMessage("failed : " + e.getMessage());
                messageBox.open();
                //               MessageDialog.openError(getShell(), "Failed to Save", "failed to save " +
                //                     "the image: " + e.getMessage());

            }
        }
    });
}

From source file:org.eclipse.swt.snippets.SnippetExplorer.java

/**
 * Process UI event queue until explorer is closed or otherwise ended.
 *///ww w .  j  a  va 2  s  .  c o m
private void runEventLoop() {
    // Apart from the usual "dispatch events until closed" pattern the
    // SnippetExplorer supports the special workflow where it close itself, run one
    // or more Snippets one after another and then restarts the explorer itself
    // which is all handled in this method.
    try {
        while (true) {
            serialSnippets = null;
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch()) {
                    display.sleep();
                }
            }

            if (serialSnippets == null || serialSnippets.isEmpty()) {
                break;
            }

            display.dispose();
            int i = 0;
            for (Snippet snippet : serialSnippets) {
                System.out
                        .println(String.format("(%d/%d) %s", ++i, serialSnippets.size(), snippet.snippetName));
                runSnippetInCurrentThread(snippet);
            }
            final Display currentDisplay = Display.getCurrent();
            if (currentDisplay != null) {
                // left over from the snippet run
                currentDisplay.dispose();
            }
            initialize();
            final int index = runnerMapping.indexOf(null);
            if (index != -1) {
                runnerCombo.select(index);
            }
        }
    } finally {
        stopSnippets();
    }
}

From source file:org.mwc.cmap.xyplot.views.XYPlotView.java

private void refreshPlot() {
    Runnable runnable = new Runnable() {

        @Override// w  w  w .j  a va 2  s . c om
        public void run() {
            if (_plotControl != null && !_plotControl.isDisposed()) {
                _plotControl.setSize(0, 0);
                _plotControl.getParent().layout(true, true);
                _plotControl.redraw();
                _plotControl.update();
            }
        }
    };
    if (Display.getCurrent() != null) {
        runnable.run();
    } else {
        Display.getDefault().syncExec(runnable);
    }
}

From source file:PrintKTableExample.java

/**
 * @param parent/*from   w  ww. j a v  a 2 s.  c  om*/
 * @param title
 * @param icon
 */
public PrintPreview(Shell parent, String title, Image icon, PDocument doc) {
    super(parent, title, icon);
    createContents();
    document = doc;
    page = 1;
    percent = 100;
    layoutNeccessary = true;

    addToolItem("print", "Drucken ...", IconSource.getImage("print"));
    addToolItem("first", "erste Seite", IconSource.getImage("i2"));
    addToolItem("prev", "vorherige Seite", IconSource.getImage("i3"));
    addToolItem("next", "nachste Seite", IconSource.getImage("i4"));
    addToolItem("last", "letzte Seite", IconSource.getImage("i5"));
    Button close = addButtonRight("&SchlieBen", "");
    // addButtonRight("Seite &einrichten","");
    close.setFocus();

    guiShell.addShellListener(new ShellAdapter() {
        public void shellClosed(ShellEvent arg0) {
            onClose();
        }
    });

    Composite comp = new Composite(guiToolBarArea, SWT.BORDER);
    comp.setLayout(new FillLayout());
    guiPageLabel = new CLabel(comp, SWT.NONE);
    guiPageLabel.setText(guiPageLabel.getText() + "        ");
    guiPageLabel.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
    adjustToToolBar(comp);

    guiZoom = new Combo(guiToolBarArea, SWT.BORDER | SWT.READ_ONLY);
    guiZoom.add("500%");
    guiZoom.add("200%");
    guiZoom.add("100%");
    guiZoom.add("80%");
    guiZoom.add("50%");
    guiZoom.add("20%");
    guiZoom.add("passend");
    adjustToToolBar(guiZoom);
    guiZoom.setToolTipText("VorschaugroBe");
    guiZoom.select(2);
    guiZoom.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent arg0) {
            onCombo(((Combo) arg0.widget).getText());
        }
    });
    guiMainArea.setLayout(new FillLayout());
    guiScrollArea = new ScrolledComposite(guiMainArea, SWT.H_SCROLL | SWT.V_SCROLL);
    guiImageLabel = new Label(guiScrollArea, SWT.NONE);
    guiScrollArea.setContent(guiImageLabel);
    if (guiImageLabel.getImage() != null)
        guiImageLabel.getImage().dispose();
    guiImageLabel.setImage(getPageImage(page));
    guiPageLabel.setText(" Seite " + page + " von " + document.getNumOfPages() + "       ");
    guiImageLabel.setSize(guiImageLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT));

}

From source file:PrintKTableExample.java

protected void doLayout() {
    int x = Display.getCurrent().getBounds().width - 100;
    int y = Display.getCurrent().getBounds().height - 10;
    guiShell.setSize(x, y);//from   w  ww.jav  a  2s . co m
    guiShell.setMaximized(true);
}

From source file:PrintKTableExample.java

public Image getPageImage(int page) {
    Point dpi = Display.getCurrent().getDPI();

    try {/*  ww w . jav a2 s .com*/
        int h = (int) Math.round(document.getPageHeight() / 2.54 * dpi.y * percent / 100 + 5);
        int w = (int) Math.round(document.getPageWidth() / 2.54 * dpi.x * percent / 100 + 5);
        Image newImage = new Image(Display.getCurrent(), w, h);
        GC gc = new GC(newImage);

        PBox.setParameters(gc, Display.getCurrent(), dpi, percent);
        if (layoutNeccessary)
            document.layout();
        layoutNeccessary = false;
        document.draw(page);

        // Schatten

        gc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_DARK_GRAY));
        gc.fillRectangle(0, newImage.getBounds().height - 5, newImage.getBounds().width,
                newImage.getBounds().height);
        gc.fillRectangle(newImage.getBounds().width - 5, 0, newImage.getBounds().width - 5,
                newImage.getBounds().height);

        gc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
        gc.fillRectangle(0, newImage.getBounds().height - 5, 5, newImage.getBounds().height);
        gc.fillRectangle(newImage.getBounds().width - 5, 0, newImage.getBounds().width, 5);

        gc.dispose();
        return newImage;
    } catch (Exception e) {
        System.out.println(e.getMessage());
        return null;
    }

}

From source file:PrintKTableExample.java

protected void onCombo(String text) {
    if (text.startsWith("passend")) {
        long ypixel = Math.round(document.getPageHeight() / 2.54 * Display.getCurrent().getDPI().y);
        long xpixel = Math.round(document.getPageWidth() / 2.54 * Display.getCurrent().getDPI().x);

        int yscale = (int) (100 * (guiScrollArea.getBounds().height) / ypixel);
        int xscale = (int) (100 * (guiScrollArea.getBounds().width) / xpixel);

        percent = Math.min(yscale, xscale);
    } else {//from   ww  w  . j a v  a2  s.  c o  m
        text = text.substring(0, text.length() - 1);
        percent = 100;
        try {
            percent = Integer.parseInt(text);
        } catch (Exception e1) {
            MsgBox.show("'" + text + "' ist keine gultige Zahl.");
            guiZoom.select(3);
        }
    }
    layoutNeccessary = true;
    if (guiImageLabel.getImage() != null)
        guiImageLabel.getImage().dispose();
    guiImageLabel.setImage(getPageImage(page));
    guiImageLabel.setSize(guiImageLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT));

}