Highlight deepest component under mouse cursor and print parent hierarchy to console [message #1760724] |
Tue, 02 May 2017 12:30 |
Frank Gruendel Messages: 22 Registered: January 2014 |
Junior Member |
|
|
This is the answer to https://www.eclipse.org/forums/index.php/t/1085551/. It works under RAP (at least version 3.0.2, which is the only version I tested it with).
Unfortunately, MouseMove events aren't supported in RAP since the network latency would make them barely usable. See https://eclipse.org/rap/developers-guide/devguide.php?topic=key-and-mouse-events.html&version=2.0 for details. So the highlighting of the controls will not follow the mouse position automatically. Instead, the mouse must be placed over the control and a pre-defined key combination must be typed.
It might not be such a hot idea to put this into something you intend to sell since it was not thoroughly tested. Of course, all comments and improvement suggestions are highly welcome.
First, we'll create a listener that takes care of printing and highlighting the control. The key combination to invoke it is defined outside of the listener since we need it at some other places in the code.
private static final String INVOKE_KEYS = "CTRL+ALT+SHIFT+F";
private class ComponentHierarchyListener implements Listener
{
private final int HIGHLIGHT_TIME = 2000; // Two seconds
private final Color HIGHLIGHT_COLOR = GraphicsFacade.getColor(0, 255, 0); // Green
@Override
public void handleEvent(Event event)
{
final Display display = Display.getCurrent();
final Control control = getDeepestComponent(display);
if (null != control)
{
this.printComponentHierarchy(control);
this.highlightControl(display, control);
}
}
private Control getDeepestComponent(final Display display)
{
if (null == display)
{
System.err.println("No display, cannot determine control under cursor.");
return null;
}
final Control control = display.getCursorControl();
if (null == control)
{
System.err.println("display.getCursorControl() returned null. Cannot determine control under cursor.");
return null;
}
return control;
}
private void printComponentHierarchy(final Control control)
{
final StringBuffer stbr = new StringBuffer(getClassName(control));
Control parent = control.getParent();
while (null != parent)
{
stbr.append(" - " + getClassName(parent));
parent = parent.getParent();
}
System.err.println(stbr);
}
private void highlightControl(final Display display, final Control control)
{
final Color oldBackground = control.getBackground();
control.setBackground(HIGHLIGHT_COLOR);
final ServerPushSession pushSession = new ServerPushSession();
final Runnable bgRunnable = new Runnable()
{
public void run()
{
try
{
Thread.sleep(HIGHLIGHT_TIME);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
display.asyncExec(new Runnable()
{
public void run()
{
control.setBackground(oldBackground);
}
});
}
};
pushSession.start();
final Thread backgroundThread = new Thread(bgRunnable);
backgroundThread.setDaemon(true);
backgroundThread.start();
pushSession.stop();
}
private String getClassName(final Object object)
{
final String classAndPackage = object.getClass().getName();
final int lastPeriodIndex = classAndPackage.lastIndexOf('.');
final String className = classAndPackage.substring(lastPeriodIndex + 1);
return className;
}
}
The only thing left to do is adding a checkbox to our code that will turn this stuff on and off:
private void addControls(final Composite parent)
{
final Button cbEnableVisualDebugging = new Button(parent, SWT.CHECK);
cbEnableVisualDebugging.setText("Enable Visual Debugging. To invoke, type " + INVOKE_KEYS);
cbEnableVisualDebugging.setToolTipText("If selected, the component under the mouse cursor will be highlighted and its parent hierarchy will be printed to the console on clicking " + INVOKE_KEYS);
cbEnableVisualDebugging.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected(final SelectionEvent se)
{
final Display display = Display.getCurrent();
if (null != display)
{
if (null == m_componentHierarchyListener)
{
m_componentHierarchyListener = new ComponentHierarchyListener();
}
if (cbEnableVisualDebugging.getSelection())
{
//System.err.println("Adding listener");
display.setData(RWT.ACTIVE_KEYS, new String[] { INVOKE_KEYS });
display.addFilter(SWT.KeyDown, m_componentHierarchyListener);
}
else
{
//System.err.println("Removing listener");
display.setData(RWT.ACTIVE_KEYS, null);
display.removeFilter(SWT.KeyDown, m_componentHierarchyListener);
}
}
}
});
}
[Updated on: Tue, 02 May 2017 12:32] Report message to a moderator
|
|
|
|
Powered by
FUDForum. Page generated in 0.61566 seconds