Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » SWTBot » Closing CTabs
Closing CTabs [message #495977] Fri, 06 November 2009 20:32 Go to next message
Mr. Gaffo is currently offline Mr. GaffoFriend
Messages: 9
Registered: October 2009
Junior Member
What's the method for closing a CTab with SWTBot?

SWTBot bot = new SWTBot();
SWTBotCTabItem item = bot.cTabItem("New Tab 2");

There is no close or anything on item.

Thanks!
Re: Closing CTabs [message #496017 is a reply to message #495977] Sat, 07 November 2009 04:40 Go to previous messageGo to next message
Ketan Padegaonkar is currently offline Ketan PadegaonkarFriend
Messages: 873
Registered: July 2009
Senior Member
Seems like a feature request. Please file a bug and I'll look at it :)

-- Ketan

Mr. Gaffo wrote:
> What's the method for closing a CTab with SWTBot?
> SWTBot bot = new SWTBot();
> SWTBotCTabItem item = bot.cTabItem("New Tab 2");
>
> There is no close or anything on item.
>
> Thanks!
>
Re: Closing CTabs [message #496643 is a reply to message #496017] Tue, 10 November 2009 18:48 Go to previous message
Wade Walker is currently offline Wade WalkerFriend
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$
    }
}
Previous Topic:How do I call another test within a test?
Next Topic:Problem by tests with Editor
Goto Forum:
  


Current Time: Wed Nov 26 01:49:12 GMT 2014

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

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