List of usage examples for org.eclipse.swt.widgets Display timerExec
public void timerExec(int milliseconds, Runnable runnable)
run()
method of the runnable to be invoked by the user-interface thread after the specified number of milliseconds have elapsed. From source file:org.eclipse.swt.examples.browser.demos.views.PawnTab.java
public static void play(final Display display, final Browser browser, int delay) { ttr.playRequest(game, BLACK);/*from w w w .j ava 2 s .c o m*/ display.timerExec(3000, () -> { ttr.getBestMove(move); boolean hasMore = add(move[0], move[1], BLACK); isWhite = true; browser.setText(getHtml(hasMore ? TYPE_BOARD : TYPE_BOARD_OVER)); }); }
From source file:Animator.java
/** * Runs the application/*from w ww. j ava 2s . c o m*/ */ public void run() { final Display display = new Display(); Shell shell = new Shell(display); shell.setText("Animator"); createContents(shell); shell.open(); // Set up the timer for the animation Runnable runnable = new Runnable() { public void run() { animate(); display.timerExec(TIMER_INTERVAL, this); } }; // Launch the timer display.timerExec(TIMER_INTERVAL, runnable); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } // Kill the timer display.timerExec(-1, runnable); display.dispose(); }
From source file:AnimatorDoubleBuffer.java
/** * Runs the application/* ww w . ja v a 2 s . c om*/ */ public void run() { final Display display = new Display(); Shell shell = new Shell(display); shell.setText("Animator Double Buffer"); createContents(shell); shell.open(); // Set up the timer for the animation Runnable runnable = new Runnable() { public void run() { animate(); display.timerExec(TIMER_INTERVAL, this); } }; // Launch the timer display.timerExec(TIMER_INTERVAL, runnable); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } // Kill the timer display.timerExec(-1, runnable); display.dispose(); }
From source file:org.eclipse.swt.examples.paint.ContinuousPaintSession.java
/** * Prepare the retrigger timer/*from ww w . j a v a 2s . c o m*/ */ private final void prepareRetrigger() { if (retriggerInterval > 0) { /* * timerExec() provides a lightweight mechanism for running code at intervals from within * the event loop when timing accuracy is not important. * * Since it is not possible to cancel a timerExec(), we remember the Runnable that is * active in order to distinguish the valid one from the stale ones. In practice, * if the interval is 1/100th of a second, then creating a few hundred new RetriggerHandlers * each second will not cause a significant performance hit. */ Display display = getPaintSurface().getDisplay(); retriggerHandler = new Runnable() { @Override public void run() { if (retriggerHandler == this) { render(points[0]); prepareRetrigger(); } } }; display.timerExec(retriggerInterval, retriggerHandler); } }
From source file:org.eclipse.swt.examples.graphics.GraphicsExample.java
/** * Starts the animation if the animate flag is set. */// ww w. j a v a2s. c o m void startAnimationTimer() { final Display display = parent.getDisplay(); display.timerExec(TIMER, new Runnable() { @Override public void run() { if (canvas.isDisposed()) return; int timeout = TIMER; GraphicsTab tab = getTab(); if (tab instanceof AnimatedGraphicsTab) { AnimatedGraphicsTab animTab = (AnimatedGraphicsTab) tab; if (animate && animTab.getAnimation()) { Rectangle rect = canvas.getClientArea(); animTab.next(rect.width, rect.height); canvas.redraw(); canvas.update(); } timeout = animTab.getAnimationTime(); } display.timerExec(timeout, this); } }); }
From source file:GraphicsExample.java
void startAnimationTimer() { final Display display = Display.getCurrent(); display.timerExec(timerSpinner.getSelection(), new Runnable() { public void run() { if (canvas.isDisposed()) return; if (animate) { GraphicsTab tab = getTab(); if (tab != null && tab.isAnimated()) { Rectangle rect = canvas.getClientArea(); tab.next(rect.width, rect.height); canvas.redraw();//from ww w .j a v a 2 s. co m canvas.update(); } } display.timerExec(timerSpinner.getSelection(), this); } }); }
From source file:org.eclipse.swt.examples.browserexample.BrowserExample.java
void show(boolean owned, Point location, Point size, boolean addressBar, boolean menuBar, boolean statusBar, boolean toolBar) { final Shell shell = browser.getShell(); if (owned) {// w w w.ja v a 2 s.c om if (location != null) shell.setLocation(location); if (size != null) shell.setSize(shell.computeSize(size.x, size.y)); } FormData data = null; if (toolBar) { toolbar = new ToolBar(parent, SWT.NONE); data = new FormData(); data.top = new FormAttachment(0, 5); toolbar.setLayoutData(data); itemBack = new ToolItem(toolbar, SWT.PUSH); itemBack.setText(getResourceString("Back")); itemForward = new ToolItem(toolbar, SWT.PUSH); itemForward.setText(getResourceString("Forward")); final ToolItem itemStop = new ToolItem(toolbar, SWT.PUSH); itemStop.setText(getResourceString("Stop")); final ToolItem itemRefresh = new ToolItem(toolbar, SWT.PUSH); itemRefresh.setText(getResourceString("Refresh")); final ToolItem itemGo = new ToolItem(toolbar, SWT.PUSH); itemGo.setText(getResourceString("Go")); itemBack.setEnabled(browser.isBackEnabled()); itemForward.setEnabled(browser.isForwardEnabled()); Listener listener = event -> { ToolItem item = (ToolItem) event.widget; if (item == itemBack) browser.back(); else if (item == itemForward) browser.forward(); else if (item == itemStop) browser.stop(); else if (item == itemRefresh) browser.refresh(); else if (item == itemGo) browser.setUrl(locationBar.getText()); }; itemBack.addListener(SWT.Selection, listener); itemForward.addListener(SWT.Selection, listener); itemStop.addListener(SWT.Selection, listener); itemRefresh.addListener(SWT.Selection, listener); itemGo.addListener(SWT.Selection, listener); canvas = new Canvas(parent, SWT.NO_BACKGROUND); data = new FormData(); data.width = 24; data.height = 24; data.top = new FormAttachment(0, 5); data.right = new FormAttachment(100, -5); canvas.setLayoutData(data); final Rectangle rect = images[0].getBounds(); canvas.addListener(SWT.Paint, e -> { Point pt = ((Canvas) e.widget).getSize(); e.gc.drawImage(images[index], 0, 0, rect.width, rect.height, 0, 0, pt.x, pt.y); }); canvas.addListener(SWT.MouseDown, e -> browser.setUrl(getResourceString("Startup"))); final Display display = parent.getDisplay(); display.asyncExec(new Runnable() { @Override public void run() { if (canvas.isDisposed()) return; if (busy) { index++; if (index == images.length) index = 0; canvas.redraw(); } display.timerExec(150, this); } }); } if (addressBar) { locationBar = new Text(parent, SWT.BORDER); data = new FormData(); if (toolbar != null) { data.top = new FormAttachment(toolbar, 0, SWT.TOP); data.left = new FormAttachment(toolbar, 5, SWT.RIGHT); data.right = new FormAttachment(canvas, -5, SWT.DEFAULT); } else { data.top = new FormAttachment(0, 0); data.left = new FormAttachment(0, 0); data.right = new FormAttachment(100, 0); } locationBar.setLayoutData(data); locationBar.addListener(SWT.DefaultSelection, e -> browser.setUrl(locationBar.getText())); } if (statusBar) { status = new Label(parent, SWT.NONE); progressBar = new ProgressBar(parent, SWT.NONE); data = new FormData(); data.left = new FormAttachment(0, 5); data.right = new FormAttachment(progressBar, 0, SWT.DEFAULT); data.bottom = new FormAttachment(100, -5); status.setLayoutData(data); data = new FormData(); data.right = new FormAttachment(100, -5); data.bottom = new FormAttachment(100, -5); progressBar.setLayoutData(data); browser.addStatusTextListener(event -> status.setText(event.text)); } parent.setLayout(new FormLayout()); Control aboveBrowser = toolBar ? (Control) canvas : (addressBar ? (Control) locationBar : null); data = new FormData(); data.left = new FormAttachment(0, 0); data.top = aboveBrowser != null ? new FormAttachment(aboveBrowser, 5, SWT.DEFAULT) : new FormAttachment(0, 0); data.right = new FormAttachment(100, 0); data.bottom = status != null ? new FormAttachment(status, -5, SWT.DEFAULT) : new FormAttachment(100, 0); browser.setLayoutData(data); if (statusBar || toolBar) { browser.addProgressListener(new ProgressListener() { @Override public void changed(ProgressEvent event) { if (event.total == 0) return; int ratio = event.current * 100 / event.total; if (progressBar != null) progressBar.setSelection(ratio); busy = event.current != event.total; if (!busy) { index = 0; if (canvas != null) canvas.redraw(); } } @Override public void completed(ProgressEvent event) { if (progressBar != null) progressBar.setSelection(0); busy = false; index = 0; if (canvas != null) { itemBack.setEnabled(browser.isBackEnabled()); itemForward.setEnabled(browser.isForwardEnabled()); canvas.redraw(); } } }); } if (addressBar || statusBar || toolBar) { browser.addLocationListener(LocationListener.changedAdapter(event -> { busy = true; if (event.top && locationBar != null) locationBar.setText(event.location); })); } if (title) { browser.addTitleListener( event -> shell.setText(event.title + " - " + getResourceString("window.title"))); } parent.layout(true); if (owned) shell.open(); }
From source file:SWTBrowserDemo.java
void show(boolean owned, Point location, Point size, boolean addressBar, boolean menuBar, boolean statusBar, boolean toolBar) { final Shell shell = browser.getShell(); if (owned) {/*from w ww.ja v a 2 s . c o m*/ if (location != null) shell.setLocation(location); if (size != null) shell.setSize(shell.computeSize(size.x, size.y)); } FormData data = null; if (toolBar) { toolbar = new ToolBar(parent, SWT.NONE); data = new FormData(); data.top = new FormAttachment(0, 5); toolbar.setLayoutData(data); itemBack = new ToolItem(toolbar, SWT.PUSH); itemBack.setText(getResourceString("Back")); itemForward = new ToolItem(toolbar, SWT.PUSH); itemForward.setText(getResourceString("Forward")); final ToolItem itemStop = new ToolItem(toolbar, SWT.PUSH); itemStop.setText(getResourceString("Stop")); final ToolItem itemRefresh = new ToolItem(toolbar, SWT.PUSH); itemRefresh.setText(getResourceString("Refresh")); final ToolItem itemGo = new ToolItem(toolbar, SWT.PUSH); itemGo.setText(getResourceString("Go")); itemBack.setEnabled(browser.isBackEnabled()); itemForward.setEnabled(browser.isForwardEnabled()); Listener listener = new Listener() { public void handleEvent(Event event) { ToolItem item = (ToolItem) event.widget; if (item == itemBack) browser.back(); else if (item == itemForward) browser.forward(); else if (item == itemStop) browser.stop(); else if (item == itemRefresh) browser.refresh(); else if (item == itemGo) browser.setUrl(locationBar.getText()); } }; itemBack.addListener(SWT.Selection, listener); itemForward.addListener(SWT.Selection, listener); itemStop.addListener(SWT.Selection, listener); itemRefresh.addListener(SWT.Selection, listener); itemGo.addListener(SWT.Selection, listener); canvas = new Canvas(parent, SWT.NO_BACKGROUND); data = new FormData(); data.width = 24; data.height = 24; data.top = new FormAttachment(0, 5); data.right = new FormAttachment(100, -5); canvas.setLayoutData(data); final Rectangle rect = images[0].getBounds(); canvas.addListener(SWT.Paint, new Listener() { public void handleEvent(Event e) { Point pt = ((Canvas) e.widget).getSize(); e.gc.drawImage(images[index], 0, 0, rect.width, rect.height, 0, 0, pt.x, pt.y); } }); canvas.addListener(SWT.MouseDown, new Listener() { public void handleEvent(Event e) { browser.setUrl(getResourceString("Startup")); } }); final Display display = parent.getDisplay(); display.asyncExec(new Runnable() { public void run() { if (canvas.isDisposed()) return; if (busy) { index++; if (index == images.length) index = 0; canvas.redraw(); } display.timerExec(150, this); } }); } if (addressBar) { locationBar = new Text(parent, SWT.BORDER); data = new FormData(); if (toolbar != null) { data.top = new FormAttachment(toolbar, 0, SWT.TOP); data.left = new FormAttachment(toolbar, 5, SWT.RIGHT); data.right = new FormAttachment(canvas, -5, SWT.DEFAULT); } else { data.top = new FormAttachment(0, 0); data.left = new FormAttachment(0, 0); data.right = new FormAttachment(100, 0); } locationBar.setLayoutData(data); locationBar.addListener(SWT.DefaultSelection, new Listener() { public void handleEvent(Event e) { browser.setUrl(locationBar.getText()); } }); } if (statusBar) { status = new Label(parent, SWT.NONE); progressBar = new ProgressBar(parent, SWT.NONE); data = new FormData(); data.left = new FormAttachment(0, 5); data.right = new FormAttachment(progressBar, 0, SWT.DEFAULT); data.bottom = new FormAttachment(100, -5); status.setLayoutData(data); data = new FormData(); data.right = new FormAttachment(100, -5); data.bottom = new FormAttachment(100, -5); progressBar.setLayoutData(data); browser.addStatusTextListener(new StatusTextListener() { public void changed(StatusTextEvent event) { status.setText(event.text); } }); } parent.setLayout(new FormLayout()); Control aboveBrowser = toolBar ? (Control) canvas : (addressBar ? (Control) locationBar : null); data = new FormData(); data.left = new FormAttachment(0, 0); data.top = aboveBrowser != null ? new FormAttachment(aboveBrowser, 5, SWT.DEFAULT) : new FormAttachment(0, 0); data.right = new FormAttachment(100, 0); data.bottom = status != null ? new FormAttachment(status, -5, SWT.DEFAULT) : new FormAttachment(100, 0); browser.setLayoutData(data); if (statusBar || toolBar) { browser.addProgressListener(new ProgressListener() { public void changed(ProgressEvent event) { if (event.total == 0) return; int ratio = event.current * 100 / event.total; if (progressBar != null) progressBar.setSelection(ratio); busy = event.current != event.total; if (!busy) { index = 0; if (canvas != null) canvas.redraw(); } } public void completed(ProgressEvent event) { if (progressBar != null) progressBar.setSelection(0); busy = false; index = 0; if (canvas != null) { itemBack.setEnabled(browser.isBackEnabled()); itemForward.setEnabled(browser.isForwardEnabled()); canvas.redraw(); } } }); } if (addressBar || statusBar || toolBar) { browser.addLocationListener(new LocationListener() { public void changed(LocationEvent event) { busy = true; if (event.top && locationBar != null) locationBar.setText(event.location); } public void changing(LocationEvent event) { } }); } if (title) { browser.addTitleListener(new TitleListener() { public void changed(TitleEvent event) { shell.setText(event.title + " - " + getResourceString("window.title")); } }); } parent.layout(true); if (owned) shell.open(); }
From source file:BrowserExample.java
/** * Creates an instance of a ControlExample embedded inside the supplied * parent Composite.// ww w . j ava 2 s.c om * * @param parent * the container of the example */ public BrowserExample(Composite parent) { final Display display = parent.getDisplay(); FormLayout layout = new FormLayout(); parent.setLayout(layout); ToolBar toolbar = new ToolBar(parent, SWT.NONE); final ToolItem itemBack = new ToolItem(toolbar, SWT.PUSH); itemBack.setText(getResourceString("Back")); final ToolItem itemForward = new ToolItem(toolbar, SWT.PUSH); itemForward.setText(getResourceString("Forward")); final ToolItem itemStop = new ToolItem(toolbar, SWT.PUSH); itemStop.setText(getResourceString("Stop")); final ToolItem itemRefresh = new ToolItem(toolbar, SWT.PUSH); itemRefresh.setText(getResourceString("Refresh")); final ToolItem itemGo = new ToolItem(toolbar, SWT.PUSH); itemGo.setText(getResourceString("Go")); location = new Text(parent, SWT.BORDER); images = new Image[] { new Image(display, "java2s.gif") }; final Canvas canvas = new Canvas(parent, SWT.NO_BACKGROUND); final Rectangle rect = images[0].getBounds(); canvas.addListener(SWT.Paint, new Listener() { public void handleEvent(Event e) { Point pt = canvas.getSize(); e.gc.drawImage(images[index], 0, 0, rect.width, rect.height, 0, 0, pt.x, pt.y); } }); canvas.addListener(SWT.MouseDown, new Listener() { public void handleEvent(Event e) { browser.setUrl(getResourceString("Startup")); } }); display.asyncExec(new Runnable() { public void run() { if (canvas.isDisposed()) return; if (busy) { index++; if (index == images.length) index = 0; canvas.redraw(); } display.timerExec(150, this); } }); final Label status = new Label(parent, SWT.NONE); final ProgressBar progressBar = new ProgressBar(parent, SWT.NONE); FormData data = new FormData(); data.top = new FormAttachment(0, 5); toolbar.setLayoutData(data); data = new FormData(); data.left = new FormAttachment(0, 0); data.right = new FormAttachment(100, 0); data.top = new FormAttachment(canvas, 5, SWT.DEFAULT); data.bottom = new FormAttachment(status, -5, SWT.DEFAULT); try { browser = new Browser(parent, SWT.NONE); browser.setLayoutData(data); } catch (SWTError e) { /* Browser widget could not be instantiated */ Label label = new Label(parent, SWT.CENTER | SWT.WRAP); label.setText(getResourceString("BrowserNotCreated")); label.setLayoutData(data); } data = new FormData(); data.width = 24; data.height = 24; data.top = new FormAttachment(0, 5); data.right = new FormAttachment(100, -5); canvas.setLayoutData(data); data = new FormData(); data.top = new FormAttachment(toolbar, 0, SWT.TOP); data.left = new FormAttachment(toolbar, 5, SWT.RIGHT); data.right = new FormAttachment(canvas, -5, SWT.DEFAULT); location.setLayoutData(data); data = new FormData(); data.left = new FormAttachment(0, 5); data.right = new FormAttachment(progressBar, 0, SWT.DEFAULT); data.bottom = new FormAttachment(100, -5); status.setLayoutData(data); data = new FormData(); data.right = new FormAttachment(100, -5); data.bottom = new FormAttachment(100, -5); progressBar.setLayoutData(data); if (browser != null) { itemBack.setEnabled(browser.isBackEnabled()); itemForward.setEnabled(browser.isForwardEnabled()); Listener listener = new Listener() { public void handleEvent(Event event) { ToolItem item = (ToolItem) event.widget; if (item == itemBack) browser.back(); else if (item == itemForward) browser.forward(); else if (item == itemStop) browser.stop(); else if (item == itemRefresh) browser.refresh(); else if (item == itemGo) browser.setUrl(location.getText()); } }; browser.addLocationListener(new LocationListener() { public void changed(LocationEvent event) { busy = true; if (event.top) location.setText(event.location); } public void changing(LocationEvent event) { } }); browser.addProgressListener(new ProgressListener() { public void changed(ProgressEvent event) { if (event.total == 0) return; int ratio = event.current * 100 / event.total; progressBar.setSelection(ratio); busy = event.current != event.total; if (!busy) { index = 0; canvas.redraw(); } } public void completed(ProgressEvent event) { itemBack.setEnabled(browser.isBackEnabled()); itemForward.setEnabled(browser.isForwardEnabled()); progressBar.setSelection(0); busy = false; index = 0; canvas.redraw(); } }); browser.addStatusTextListener(new StatusTextListener() { public void changed(StatusTextEvent event) { status.setText(event.text); } }); if (parent instanceof Shell) { final Shell shell = (Shell) parent; browser.addTitleListener(new TitleListener() { public void changed(TitleEvent event) { shell.setText(event.title + " - " + getResourceString("window.title")); } }); } itemBack.addListener(SWT.Selection, listener); itemForward.addListener(SWT.Selection, listener); itemStop.addListener(SWT.Selection, listener); itemRefresh.addListener(SWT.Selection, listener); itemGo.addListener(SWT.Selection, listener); location.addListener(SWT.DefaultSelection, new Listener() { public void handleEvent(Event e) { browser.setUrl(location.getText()); } }); initialize(display, browser); browser.setUrl(getResourceString("Startup")); } }
From source file:PaintExample.java
/** * Prepare the retrigger timer//from w ww. j a v a 2s . com */ private final void prepareRetrigger() { if (retriggerInterval > 0) { /* * timerExec() provides a lightweight mechanism for running code at intervals from within * the event loop when timing accuracy is not important. * * Since it is not possible to cancel a timerExec(), we remember the Runnable that is * active in order to distinguish the valid one from the stale ones. In practice, * if the interval is 1/100th of a second, then creating a few hundred new RetriggerHandlers * each second will not cause a significant performance hit. */ Display display = getPaintSurface().getDisplay(); retriggerHandler = new Runnable() { public void run() { if (retriggerHandler == this) { render(points[0]); prepareRetrigger(); } } }; display.timerExec(retriggerInterval, retriggerHandler); } }