|
|
Re: Closing CTabs [message #496643 is a reply to message #496017] |
Tue, 10 November 2009 18:48 |
Wade Walker Messages: 4 Registered: July 2009 |
Junior Member |
|
|
Here is some code that clicks the close box of a CTabItem to close it. Sorry I can't submit this as a patch, but I can't get SWTBot to build correctly yet. However, this code could easily be added to SWTBotCTabItem instead of being defined as a separate class. I've tested this and it works great.
I define a wrapper class for SWTBotCTabItem called SWTBotCTabItemEx, which can be used like this:
SWTBotCTabItem swtbotctabitem = swtworkbenchbot.cTabItem( sFileName );
SWTBotCTabItemEx swtbotctabitemex = new SWTBotCTabItemEx( swtbotctabitem.widget );
swtbotctabitemex.clickCloseBox();
Here's the code for SWTBotCTabItemEx:
package com.arm.viewer.test;
import java.lang.reflect.Field;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
import org.eclipse.swtbot.swt.finder.results.Result;
import org.eclipse.swtbot.swt.finder.utils.MessageFormat;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotCTabItem;
/**
* Wraps SWTBot's CTabItem to add the ability to click the close box.
* TODO: remove once SWTBot has this ability.
*
* @author Wade Walker
* @version 1.0
*/
public class SWTBotCTabItemEx extends SWTBotCTabItem {
/**
* Constructor.
*
* @param ctabitem Item we're wrapping.
* @throws WidgetNotFoundException if the widget is null or widget has been disposed.
*/
public SWTBotCTabItemEx( CTabItem ctabitem ) throws WidgetNotFoundException {
super( ctabitem );
}
/**
* Clicks the close box of this CTabItem.
* @return this CTabItem.
*/
public SWTBotCTabItemEx clickCloseBox() {
assertEnabled();
Rectangle rectangleCloseBox = syncExec( new Result<Rectangle>() {
public Rectangle run() {
// get SWT CTabItem's private field through reflection;
// ugly, but better than trying to duplicate CTabField's
// complex internal logic that sets the close rects
// TODO: remove if SWT adds getCloseRect() to CTabItem
Rectangle rectangle = null;
try {
Field field = CTabItem.class.getDeclaredField( "closeRect" );
field.setAccessible( true );
rectangle = (Rectangle)field.get( widget );
}
// if any of these exceptions happens, CTabItem has changed
// its internal structure, and a null rectangle will
// be returned
catch( SecurityException securityexception ) {
securityexception.printStackTrace();
}
catch( NoSuchFieldException nosuchfieldexception ) {
nosuchfieldexception.printStackTrace();
}
catch( IllegalAccessException illegalaccessexception ) {
illegalaccessexception.printStackTrace();
}
return( rectangle );
}
});
// click the center of the close box
clickXY( rectangleCloseBox.x + (rectangleCloseBox.width / 2),
rectangleCloseBox.y + (rectangleCloseBox.height / 2));
return this;
}
/**
* Sends a non-blocking notification of the specified type to the {@link #widget}.
* This override is needed because the CTabItem's parent CTabFolder is
* the widget that contains all the event handlers.
*
* @param eventType the type of event.
* @param createEvent the event to be sent to the {@link #widget}.
*/
protected void notify(final int eventType, final Event createEvent) {
notify(eventType, createEvent, widget.getParent());
}
/**
* Click on the tab item at given coordinates
*
* @param x the x co-ordinate of the click
* @param y the y co-ordinate of the click
* @since 2.0
*/
protected void clickXY(int x, int y) {
log.debug(MessageFormat.format("Clicking on {0}", this)); //$NON-NLS-1$
notify(SWT.MouseEnter);
notify(SWT.MouseMove);
notify(SWT.Activate);
notify(SWT.FocusIn);
notify(SWT.MouseDown, createMouseEvent(x, y, 1, SWT.BUTTON1, 1));
// this event being button 1 is what allows CTabItem to close
notify(SWT.MouseUp, createMouseEvent(x, y, 1, SWT.BUTTON1, 1));
notify(SWT.Selection);
notify(SWT.MouseHover);
notify(SWT.MouseMove);
notify(SWT.MouseExit);
notify(SWT.Deactivate);
notify(SWT.FocusOut);
log.debug(MessageFormat.format("Clicked on {0}", this)); //$NON-NLS-1$
}
}
|
|
|
Powered by
FUDForum. Page generated in 0.02263 seconds