package org.jboss.reddeer.swt.impl.button;

import org.apache.log4j.Logger;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotButton;
import org.eclipse.swtbot.swt.finder.widgets.TimeoutException;
import org.jboss.reddeer.swt.api.Button;
import org.jboss.reddeer.swt.condition.WaitCondition;
import org.jboss.reddeer.swt.exception.SWTLayerException;
import org.jboss.reddeer.swt.util.Display;
import org.jboss.reddeer.swt.util.ResultRunnable;
import org.jboss.reddeer.swt.wait.WaitUntil;

/**
 * Basic Button class is abstract class for all Button implementations
 * 
 * @author Jiri Peterka
 * 
 */
public abstract class AbstractButton implements Button {

	protected final Logger log = Logger.getLogger(this.getClass());

	protected SWTBotButton button;

	@Override
	public void click() {
		log.info("Click on the button "
				+ (button.getText() != null ? button.getText() : (button
						.getToolTipText() != null ? button.getToolTipText()
						: "with no text or tooltip")));
		waitUntilButtonIsActive();
		button.click();
	}

	@Override
	public String getText() {
		return button.getText();
	}

	@Override
	public boolean isEnabled() {
		// TODO waits need to completely rewritten 
		try {
			waitUntilButtonIsActive();
		} catch (TimeoutException e){
		}
		
		return button.isEnabled();
	}

	private void waitUntilButtonIsActive() {
		try {
			new WaitUntil(new WaitCondition() {

				@Override
				public boolean test() {
					return Display.syncExec(new ResultRunnable<Boolean>() {

						@Override
						public Boolean run() {
							return ((org.eclipse.swt.widgets.Button) button.widget)
									.isEnabled();
						}
					});
				}

				@Override
				public String description() {
					return "Button '" + getText() + "' was not enabled";
				}
			});
		} catch (TimeoutException te) {
			throw new SWTLayerException(te.getLocalizedMessage());
		}
	}
}

@@ -290,66 +290,8 @@
 on;%0A
-import org.jboss.reddeer.swt.exception.SWTLayerException;%0A
 impo
@@ -1123,17 +1123,16 @@
 ewritten
- 
 %0A%09%09try %7B
@@ -1196,16 +1196,15 @@
 n e)
+ 
 %7B%0A%09%09%7D%0A
-%09%09
 %0A%09%09r
@@ -1275,25 +1275,17 @@
 ive() %7B%0A
-%09%09try %7B%0A%09
+%0A
 %09%09new Wa
@@ -1314,25 +1314,24 @@
 ion() %7B%0A%0A%09%09%09
-%09
 @Override%0A%09%09
@@ -1320,33 +1320,32 @@
 %7B%0A%0A%09%09%09@Override%0A
-%09
 %09%09%09public boolea
@@ -1351,25 +1351,24 @@
 an test() %7B%0A
-%09
 %09%09%09%09return D
@@ -1416,17 +1416,16 @@
 n%3E() %7B%0A%0A
-%09
 %09%09%09%09%09@Ov
@@ -1436,17 +1436,16 @@
 de%0A%09%09%09%09%09
-%09
 public B
@@ -1455,25 +1455,24 @@
 ean run() %7B%0A
-%09
 %09%09%09%09%09%09return
@@ -1521,17 +1521,16 @@
 widget)%0A
-%09
 %09%09%09%09%09%09%09%09
@@ -1548,20 +1548,18 @@
 );%0A%09%09%09%09%09
-%09
 %7D%0A
-%09
 %09%09%09%09%7D);%0A
@@ -1561,21 +1561,19 @@
 %09%7D);%0A%09%09%09
-%09
 %7D%0A%0A
-%09
 %09%09%09@Over
@@ -1580,17 +1580,16 @@
 ride%0A%09%09%09
-%09
 public S
@@ -1606,25 +1606,24 @@
 ription() %7B%0A
-%09
 %09%09%09%09return %22
@@ -1674,113 +1674,16 @@
 %0A%09%09%09
-%09
 %7D%0A
-%09
 %09%09%7D);%0A
-%09%09%7D catch (TimeoutException te) %7B%0A%09%09%09throw new SWTLayerException(te.getLocalizedMessage());%0A%09%09%7D
 %0A%09%7D%0A

Fix in waitUntilButtonIsActive to throw expected TimeOutException