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

The following examples show how to use org.eclipse.swt.widgets.Display#timerExec() . 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: DelayedChangeListener.java    From arx with Apache License 2.0 6 votes vote down vote up
/**
 * Generic fire event
 */
private void fire() {

    this.time = System.currentTimeMillis() + delay;
        
    if (!event) {
        
        this.event = true;

        // Create repeating task
        final Display display = Display.getCurrent();
        display.timerExec(TICK, new Runnable() {
            @Override
            public void run() {
                if (event && System.currentTimeMillis() > time) {
                    delayedEvent();
                    event = false;
                    return;
                }
                display.timerExec(TICK, this);
            }
        });
    }
}
 
Example 2
Source File: HeapManagerSnippet.java    From nebula with Eclipse Public License 2.0 5 votes vote down vote up
public static void main(final String[] args) {
	final Display display = new Display();
	final Shell shell = new Shell(display);
	shell.setLayout(new FillLayout());

	new HeapManager(shell, SWT.NONE);

	final int[] counter = new int[1];
	counter[0] = 1;

	display.timerExec(10, new Runnable() {

		@Override
		public void run() {
			for (int i = 0; i < 10000; i++) {
				@SuppressWarnings("unused")
				final String[] temp = new String[1000];
			}
			counter[0]++;
			if (counter[0] < 100) {
				display.timerExec(10, this);
			}
		}
	});

	shell.pack();
	shell.open();

	while (!shell.isDisposed()) {
		if (!display.readAndDispatch()) {
			display.sleep();
		}
	}
	display.dispose();
}
 
Example 3
Source File: AsynchronousProgressMonitorDialog.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
private void scheduleTaskNameChange() {
    synchronized (updateStatusLock) {
        if (updateStatus == null) {
            updateStatus = new Runnable() {
                @Override
                public void run() {
                    try {
                        IProgressMonitor monitor = AsynchronousProgressMonitorDialog.super.getProgressMonitor();
                        String s = lastTaskName;
                        if (s != null) {
                            monitor.setTaskName(s);
                        }
                    } finally {
                        updateStatus = null;
                    }
                }
            };
            Display display = Display.getCurrent();
            if (display == null) {
                display = Display.getDefault();
            }
            if (display != null) {
                display.timerExec(AsynchronousProgressMonitorWrapper.UPDATE_INTERVAL_MS, updateStatus);
            } else {
                Log.log("AsynchronousProgressMonitorDialog: No display available!");
            }
        }
    }
}
 
Example 4
Source File: ConsistencyAction.java    From saros with GNU General Public License v2.0 5 votes vote down vote up
/** Must be called within the SWT context. */
private void startFading(final int startValue, final int direction) {
  Display display = SWTUtils.getDisplay();

  isFading = false;

  if (display.isDisposed() || !isEnabled()) return;

  isFading = true;

  display.timerExec(
      FADE_UPDATE_SPEED,
      new Runnable() {

        @Override
        public void run() {

          if (!isEnabled()) {
            setImageDescriptor(IN_SYNC);
            isFading = false;
            return;
          }

          int newValue = startValue + FADE_SPEED_DELTA * direction;
          int newDirection = direction;

          if (newValue > MAX_ALPHA_VALUE) {
            newValue = MAX_ALPHA_VALUE;
            newDirection = FADE_DOWN;
          } else if (newValue < MIN_ALPHA_VALUE) {
            newValue = MIN_ALPHA_VALUE;
            newDirection = FADE_UP;
          }

          setImageDescriptor(new MemoryImageDescriptor(modifyAlphaChannel(OUT_SYNC, newValue)));

          startFading(newValue, newDirection);
        }
      });
}
 
Example 5
Source File: XbaseInformationControl.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void setVisible(boolean visible) {
	Shell shell = getShell();
	if (shell.isVisible() == visible)
		return;

	if (!visible) {
		super.setVisible(false);
		setInput(null);
		return;
	}

	/*
	 * The Browser widget flickers when made visible while it is not completely loaded.
	 * The fix is to delay the call to setVisible until either loading is completed
	 * (see ProgressListener in constructor), or a timeout has been reached.
	 */
	final Display display = shell.getDisplay();

	// Make sure the display wakes from sleep after timeout:
	display.timerExec(100, new Runnable() {
		@Override
		public void run() {
			fCompleted = true;
		}
	});

	while (!fCompleted) {
		// Drive the event loop to process the events required to load the browser widget's contents:
		if (!display.readAndDispatch()) {
			display.sleep();
		}
	}

	shell = getShell();
	if (shell == null || shell.isDisposed())
		return;

	/*
	 * Avoids flickering when replacing hovers, especially on Vista in ON_CLICK mode.
	 * Causes flickering on GTK. Carbon does not care.
	 */
	if ("win32".equals(SWT.getPlatform())) //$NON-NLS-1$
		shell.moveAbove(null);

	super.setVisible(true);
}
 
Example 6
Source File: BookmarkRulerColumn.java    From xds-ide with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Scrolls the viewer into the given direction.
 *
 * @param direction the scroll direction
 */
private void autoScroll(int direction) {

    if (fAutoScrollDirection == direction)
        return;

    final int TIMER_INTERVAL= 5;
    final Display display= fCanvas.getDisplay();
    Runnable timer= null;
    switch (direction) {
        case SWT.UP:
            timer= new Runnable() {
                public void run() {
                    if (fAutoScrollDirection == SWT.UP) {
                        int top= getInclusiveTopIndex();
                        if (top > 0) {
                            fCachedTextViewer.setTopIndex(top -1);
                            expandSelection(top -1);
                            display.timerExec(TIMER_INTERVAL, this);
                        }
                    }
                }
            };
            break;
        case  SWT.DOWN:
            timer= new Runnable() {
                public void run() {
                    if (fAutoScrollDirection == SWT.DOWN) {
                        int top= getInclusiveTopIndex();
                        fCachedTextViewer.setTopIndex(top +1);
                        expandSelection(top +1 + fCachedViewportSize);
                        display.timerExec(TIMER_INTERVAL, this);
                    }
                }
            };
            break;
    }

    if (timer != null) {
        fAutoScrollDirection= direction;
        display.timerExec(TIMER_INTERVAL, timer);
    }
}
 
Example 7
Source File: SWTUtilities.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 4 votes vote down vote up
public static void delay(long waitTimeMillis) {
  Display display = Display.getCurrent();

  // If this is the UI thread, then process input.
  if (display != null) {

    /*
     * We set up a timer on the UI thread that fires after the desired wait
     * time. We do this because we want to make sure that the UI thread wakes
     * up from a display.sleep() call. We set a flag in the runnable so that
     * we can terminate the wait loop.
     */
    final boolean[] hasDeadlineTimerFiredPtr = {false};

    display.timerExec((int) waitTimeMillis, new Runnable() {
      public void run() {

        /*
         * We don't have to worry about putting a lock around the update/read
         * of this variable. It is only accessed by the UI thread, and there
         * is only one UI thread.
         */
        hasDeadlineTimerFiredPtr[0] = true;
      }
    });

    while (!hasDeadlineTimerFiredPtr[0]) {

      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }

    display.update();
  } else {
    try {
      // Otherwise, perform a simple sleep.
      Thread.sleep(waitTimeMillis);
    } catch (InterruptedException e) {
      // Ignored
    }
  }
}
 
Example 8
Source File: TestUtil.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Waits for the specified number of milliseconds.
 */
public static void delay(long waitTimeMillis) {
  Display display = Display.getCurrent();

  // If this is the UI thread, then process input.
  if (display != null) {

    /*
     * We set up a timer on the UI thread that fires after the desired wait time. We do this
     * because we want to make sure that the UI thread wakes up from a display.sleep() call. We
     * set a flag in the runnable so that we can terminate the wait loop.
     */
    final boolean[] hasDeadlineTimerFiredPtr = {false};

    display.timerExec((int) waitTimeMillis, new Runnable() {
      @Override
      public void run() {

        /*
         * We don't have to worry about putting a lock around the update/read of this variable. It
         * is only accessed by the UI thread, and there is only one UI thread.
         */
        hasDeadlineTimerFiredPtr[0] = true;
      }
    });

    while (!hasDeadlineTimerFiredPtr[0]) {

      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }

    display.update();
  } else {
    try {
      // Otherwise, perform a simple sleep.
      Thread.sleep(waitTimeMillis);
    } catch (InterruptedException e) {
      // Ignored
    }
  }
}
 
Example 9
Source File: CustomBrowserInformationControl.java    From APICloud-Studio with GNU General Public License v3.0 4 votes vote down vote up
public void setVisible(boolean visible)
{
	Shell shell = getShell();
	if (shell.isVisible() == visible)
		return;

	if (!visible)
	{
		super.setVisible(false);
		setInput(null);
		return;
	}

	/*
	 * The Browser widget flickers when made visible while it is not completely loaded. The fix is to delay the call
	 * to setVisible until either loading is completed (see ProgressListener in constructor), or a timeout has been
	 * reached.
	 */
	final Display display = shell.getDisplay();

	// Make sure the display wakes from sleep after timeout:
	display.timerExec(100, new Runnable()
	{
		public void run()
		{
			fCompleted = true;
		}
	});

	while (!fCompleted)
	{
		// Drive the event loop to process the events required to load the browser widget's contents:
		if (!display.readAndDispatch())
		{
			display.sleep();
		}
	}

	shell = getShell();
	if (shell == null || shell.isDisposed())
		return;

	/*
	 * Avoids flickering when replacing hovers, especially on Vista in ON_CLICK mode. Causes flickering on GTK.
	 * Carbon does not care.
	 */
	if ("win32".equals(SWT.getPlatform())) //$NON-NLS-1$
		shell.moveAbove(null);

	super.setVisible(true);
}
 
Example 10
Source File: CommonLineNumberRulerColumn.java    From APICloud-Studio with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Scrolls the viewer into the given direction.
 *
 * @param direction the scroll direction
 */
private void autoScroll(int direction) {

	if (fAutoScrollDirection == direction)
		return;

	final int TIMER_INTERVAL= 5;
	final Display display= fCanvas.getDisplay();
	Runnable timer= null;
	switch (direction) {
		case SWT.UP:
			timer= new Runnable() {
				public void run() {
					if (fAutoScrollDirection == SWT.UP) {
						int top= getInclusiveTopIndex();
						if (top > 0) {
							fCachedTextViewer.setTopIndex(top -1);
							expandSelection(top -1);
							display.timerExec(TIMER_INTERVAL, this);
						}
					}
				}
			};
			break;
		case  SWT.DOWN:
			timer= new Runnable() {
				public void run() {
					if (fAutoScrollDirection == SWT.DOWN) {
						int top= getInclusiveTopIndex();
						fCachedTextViewer.setTopIndex(top +1);
						expandSelection(top +1 + fCachedViewportSize);
						display.timerExec(TIMER_INTERVAL, this);
					}
				}
			};
			break;
	}

	if (timer != null) {
		fAutoScrollDirection= direction;
		display.timerExec(TIMER_INTERVAL, timer);
	}
}
 
Example 11
Source File: StatusBar.java    From offspring with MIT License 4 votes vote down vote up
@PostConstruct
public void postConstruct(Composite parent, INxtService _nxt,
    final Display display) {
  this.nxt = _nxt;
  mainComposite = new Composite(parent, SWT.NONE);
  GridLayoutFactory.fillDefaults().numColumns(5).spacing(5, 0).margins(5, 5)
      .applyTo(mainComposite);
  pixelConverter = new PixelConverter(mainComposite);
  createContents(mainComposite);

  /* Display the block age each 1/5 th of second */
  display.timerExec(200, new Runnable() {

    @Override
    public void run() {
      if (display != null && !display.isDisposed() && messageText != null
          && !messageText.isDisposed() && blocksText != null
          && !blocksText.isDisposed()) {
        if ((System.currentTimeMillis() - messageTime) < 2000) {
          messageText.setText(messageTextValue);
        }
        else if (lastBlock != null) {
          messageText.setText(createBlockAgeText(lastBlock));
        }
        else {
          messageText.setText("");
        }
        messageText.pack();

        if (!downloadMonitor.isActive() && lastBlock != null) {
          downloadMonitor.blockPushed(lastBlock);
        }
        if (lastBlock != null) {
          blocksText.setText(createBlockText(lastBlock.getHeight(), 0));
          blocksText.pack();
        }
        mainComposite.layout();
        display.timerExec(200, this);
      }
    }
  });
}
 
Example 12
Source File: Saros.java    From saros with GNU General Public License v2.0 4 votes vote down vote up
/** Stops the Saros Eclipse life cycle. */
private void stopLifeCycle() {

  log.debug("stopping lifecycle...");

  if (!isLifecycleStarted) {
    log.debug("lifecycle is already stopped");
    return;
  }

  isLifecycleStarted = false;

  final AtomicBoolean isLifeCycleStopped = new AtomicBoolean(false);
  final AtomicBoolean isTimeout = new AtomicBoolean(false);

  final Display display = Display.getCurrent();

  final Thread shutdownThread =
      ThreadUtils.runSafeAsync(
          "shutdown", //$NON-NLS-1$
          log,
          () -> {
            try {
              lifecycle.stop();
            } finally {
              isLifeCycleStopped.set(true);

              if (display != null) {
                try {
                  display.wake();
                } catch (SWTException ignore) {
                  // NOP
                }
              }
            }
          });

  int threadTimeout = 10000;

  // must run the event loop or stopping the lifecycle will timeout
  if (display != null && !display.isDisposed()) {

    display.timerExec(
        threadTimeout,
        () -> {
          isTimeout.set(true);
          display.wake();
        });

    while (!isLifeCycleStopped.get() && !isTimeout.get()) {
      if (!display.readAndDispatch()) display.sleep();
    }

    if (!isLifeCycleStopped.get()) threadTimeout = 1;

    /*
     * fall through to log an error or wait until the thread terminated
     * even it already signal that the life cycle was stopped
     */
  }

  try {
    shutdownThread.join(threadTimeout);
  } catch (InterruptedException e) {
    log.warn("interrupted while waiting for the current lifecycle to stop");
    Thread.currentThread().interrupt();
  }

  if (shutdownThread.isAlive()) log.error("timeout while stopping lifecycle");

  log.debug("lifecycle stopped");
}