package org.jboss.reddeer.workbench;

import org.jboss.reddeer.junit.logging.Logger;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;
import org.jboss.reddeer.swt.util.Display;
import org.jboss.reddeer.swt.util.ResultRunnable;
import org.jboss.reddeer.workbench.exception.WorkbenchPartNotFound;

/**
 * Superclass of Editor and View with ability to be closed, minimized and
 * maximized
 * 
 * @author jjankovi
 * @author rhopp
 * 
 */
public abstract class WorkbenchPart{

	protected IWorkbenchPart workbenchPart;

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

	/**
	 * Initialize to currently active part (either IEditorPart or IViewPart).
	 * Subclasses are supposed to override this constructor.
	 * 
	 * @throws WorkbenchPartNotFound
	 *             if there is no WorkbenchPart active
	 */
	public WorkbenchPart() throws WorkbenchPartNotFound {
		workbenchPart = getActiveWorkbenchPart();
		if (workbenchPart == null) {
			throw new WorkbenchPartNotFound(
					"There is no active WorkbenchPart on currently active Page");
		}
	}

	/**
	 * Initialize part given title
	 * 
	 * @param title title of part to initialize
	 */
	
	public WorkbenchPart(final String title) {
		workbenchPart = getPartByTitle(title);
	}

	abstract public void close();

	/**
	 * 
	 * @return title text
	 */
	
	public String getTitle() {
		return workbenchPart.getTitle();
	}

	public void minimize() {
		throw new UnsupportedOperationException("not implemented yet");
	}

	public void maximize() {
		throw new UnsupportedOperationException("not implemented yet");
	}

	abstract protected IWorkbenchPart getPartByTitle(String title);

	protected IWorkbenchPart getActiveWorkbenchPart() {
		return Display.syncExec(new ResultRunnable<IWorkbenchPart>() {

			@Override
			public IWorkbenchPart run() {
				return PlatformUI.getWorkbench().getActiveWorkbenchWindow()
						.getActivePage().getActivePart();
			}
		});
	}
	
}

@@ -1369,31 +1369,411 @@
 urn 
-workbenchPart.getTitle(
+Display.syncExec(new ResultRunnable%3CString%3E() %7B%0A%0A%09%09%09@Override%0A%09%09%09public String run() %7B%0A%09%09%09%09return workbenchPart.getTitle();%0A%09%09%09%7D%0A%09%09%7D);%0A%09%7D%0A%09%0A%09/**%0A%09 * %0A%09 * @return the workbench part title tool tip (not %7B@code null%7D)%0A%09 */%0A%09public String getTitleToolTip() %7B%0A%09%09return Display.syncExec(new ResultRunnable%3CString%3E() %7B%0A%0A%09%09%09@Override%0A%09%09%09public String run() %7B%0A%09%09%09%09return workbenchPart.getTitleToolTip();%0A%09%09%09%7D%0A%09%09%7D
 );%0A%09

fix #274 add getTitleToolTip() method to WorkbenchPart, works in UI thread