Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Detect User Inactivity and Logout (Eclipse RCP)
Detect User Inactivity and Logout (Eclipse RCP) [message #1862488] Thu, 07 December 2023 11:08 Go to next message
Anjali Patil is currently offline Anjali PatilFriend
Messages: 6
Registered: December 2023
Junior Member
Hello Experts,

Requirement:
We have a Rich Client Platform (RCP) (version 4.29) based application which contains combination of many (100+) AWT/Swing and SWT widgets.
Now we have a requirement to detect the logged in user inactivity and take certain actions (like logout) once user is inactive for certain amount of time (say 15 mins).

Our approach:
Currently we are using below approach to achieve this requirement:

  1. To detect inactivity of SWT widgets:

    1. We have added the event listeners on the Workbench display object to handle the events from different SWT widgets. The listener uses a timer object which is reset by the event handlers. Once the timer reaches the idle time out (say 15 mins), it is treated as user inactivity detected.

  2. To defect inactivity of AWT/Swing widgets:

    1. We have added the AWT event listeners on the Frame object to handle the events from different AWT/Swing widgets. The listener uses a timer object which is reset by the event handlers. Once the timer reaches the idle time out (say 15 mins), it is treated as user inactivity detected.

  3. Once the inactivity is detected the respective action (say logout) is taken.


Below is a sample code snippet for your quick reference.
Note:
We will use the global timer object to make sure a common timer object gets used for both the event handling.

SWT code snippet:
/*
* The provided code snippet defines a Timer object that triggers an action event after 15 minutes of inactivity. It also
* creates a TimerListener class that listens for mouse and keyboard events, restarting the timer when any of these
* events occur. This effectively detects idle time for all SWT controls on the RCP application.
*/
Timer timer = new Timer(900000, new ActionListener()   //15 minutes
{
	public void actionPerformed(ActionEvent evt)
	{ 
		System.out.println("Session Timeout...");             
	}
});

class TimerListener implements Listener 
{
	Timer timer = null;
	public TimerListener(Timer timer)
	{
		this.timer = timer;     
	}

	@Override
	public void handleEvent(Event arg0)
	{
		System.out.println("Reset timer..!!");
		timer.restart();         
	}
} 

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


AWT code snippet:
/*
* The provided code snippet defines a Timer object that triggers an action event after 15 minutes of inactivity. It also
* listens for mouse and keyboard events, restarting the timer when any of these events occur.
* This effectively detects idle time for all AWT/Swing widgets on the RCP application.
*/
JFrame frame = new JFrame(...);
frame.setVisible( true );

Action logout = new AbstractAction()
{
	public void actionPerformed(ActionEvent e)
	{
		JFrame frame = (JFrame)e.getSource();
		frame.dispose();
	}
};

InactivityListener listener = new InactivityListener(frame, logout, 15);  //15 minutes
listener.start();

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class InactivityListener implements ActionListener, AWTEventListener
{
	public final static long USER_EVENTS = AWTEvent.KEY_EVENT_MASK + AWTEvent.MOUSE_MOTION_EVENT_MASK 
						+ AWTEvent.MOUSE_EVENT_MASK;

	private Window window;
	private Action action;
	private int interval;
	private long eventMask;
	private Timer timer = new Timer(0, this);

	// Use a default inactivity interval of 1 minute and listen for USER_EVENTS
	public InactivityListener(Window window, Action action)
	{
		this(window, action, 1);
	}

	// Specify the inactivity interval and listen for USER_EVENTS
	public InactivityListener(Window window, Action action, int interval)
	{
		this(window, action, interval, USER_EVENTS);
	}

	// Specify the inactivity interval and the events to listen for
	public InactivityListener(Window window, Action action, int minutes, long eventMask)
	{
		this.window = window;
		setAction( action );
		setInterval( minutes );
		setEventMask( eventMask );
	}

	// The Action to be invoked after the specified inactivity period
	public void setAction(Action action)
	{
		this.action = action;
	}

	// The interval before the Action is invoked specified in minutes
	public void setInterval(int minutes)
	{
		setIntervalInMillis(minutes * 60000);
	}

	// The interval before the Action is invoked specified in milliseconds
	public void setIntervalInMillis(int interval)
	{
		this.interval = interval;
		timer.setInitialDelay(interval);
	}

	// A mask specifying the events to be passed to the AWTEventListener
	public void setEventMask(long eventMask)
	{
		this.eventMask = eventMask;
	}

	// Start listening for events.
	public void start()
	{
		timer.setInitialDelay(interval);
		timer.setRepeats(false);
		timer.start();
		Toolkit.getDefaultToolkit().addAWTEventListener(this, eventMask);
	}

	// Stop listening for events
	public void stop()
	{
		Toolkit.getDefaultToolkit().removeAWTEventListener(this);
		timer.stop();
	}

	// Implement ActionListener for the Timer
	public void actionPerformed(ActionEvent e)
	{
		ActionEvent ae = new ActionEvent(window, ActionEvent.ACTION_PERFORMED, "");
		action.actionPerformed(ae);
	}

	// Implement AWTEventListener
	public void eventDispatched(AWTEvent e)
	{
		if (timer.isRunning())
		timer.restart();
	}
}


For both the frameworks as the event handler gets triggered for frequent events like mouse movement, it adds significant amount of processing (event handling code execution).

Need your expert opinions to check if there is any other effective approach to detect the inactivity with lesser performance overhead.

Thanks,
Anjali
Re: Detect User Inactivity and Logout (Eclipse RCP) [message #1862745 is a reply to message #1862488] Tue, 26 December 2023 13:51 Go to previous message
Anjali Patil is currently offline Anjali PatilFriend
Messages: 6
Registered: December 2023
Junior Member
Hello, any ideas? I would appreciate it a lot!
Previous Topic:Toolbars contributed to the coolbar not visible
Next Topic:Is there a way to add a toolbar item right beside quickaccess
Goto Forum:
  


Current Time: Wed Jun 19 20:06:32 GMT 2024

Powered by FUDForum. Page generated in 0.02525 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top