Question

I want to do 'logout' or "close" the application if the workbench is idle for an amount of time (30 mins, etc), but I don't know how to implement that yet. Does anyone has some suggestions?

Was it helpful?

Solution

I had a similar requirement, where I scrached my head to implement that.

Requirement:

Lock the Application Window, if idle for some pre-configured amount of time.

But, mine was a pure SWT application where I had full control to Create and Dispose the Shell

PFB the excerpt from my code, which is well documented, it may be helpful or provide an insight to you.

    //Boilerplate code for SWT Shell
   //Event loop modified in our case
   boolean readAndDispatch;
        while (!shell.isDisposed()) { //UI Thread continuously loop in Processing and waiting for Events until shell is disposed.
            try {
                readAndDispatch = shell.getDisplay().readAndDispatch(); // reads an Event and dispatches, also returns true if there is some work else false.
                if (!readAndDispatch) { //There is no work to do, means, System is idle
                    timer.schedule(worker, delay); // So, schedule a Timer which triggers some work(lock or logout code) after specified time(delay)
                    shell.getDisplay().sleep(); 
                } else{
                    timer.reschedule(delay); //This means System is not idle. So, reset your Timer
                }
            } catch (Throwable e) {
                log.error(ExceptionUtils.getStackTrace(e));
                MessageDialog.openError(shell, "Error !!", ExceptionUtils.getRootCauseMessage(e));

            }
        }

Note

I had a custom Implementation of java.util.Timer to provide the reschedule capability by cancelling the existing TimerTask, creating a new Timertask and then scheduling it again.

OTHER TIPS

I did the same in RCP application.Add the following lines in Application.java class.

Display display = PlatformUI.createDisplay();
display.addFilter(SWT.MouseDown, new TimerListener());
display.addFilter(SWT.MouseMove, new TimerListener());
display.addFilter(SWT.KeyDown, new TimerListener());

class TimerListener implements Listener 
{
public void handleEvent(Event e) 
  {      
    if (Resources.timer != null ) 
    {
     // Restart the timer on any UI interactions
        Resources.timer.restart();
    }
  }   
} 

Here is the timer implementation. I am using Swing timer but you can use SWT timer.

Resources.timer = new javax.swing.Timer(1000, new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        Display.getDefault().asyncExec(new Runnable() {
            @Override
            public void run() {                 
                // Log off you application here
            }
        });

    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top