// Path: src/main/java/org/robotframework/formslibrary/operator/LWWindowOperator.java
// public class LWWindowOperator extends AbstractRootComponentOperator {
// 
// 	/**
// 	 * Initialize a window operator with the main oracle forms desktop window.
// 	 */
// 	public LWWindowOperator() {
// 		super(new ByComponentTypeChooser(0, ComponentType.FORM_DESKTOP));
// 	}
// 
// 	public LWWindowOperator(ComponentChooser chooser) {
// 		super(chooser);
// 	}
// 
// 	/**
// 	 * Retrieve a list of all window titles in the root context.
// 	 */
// 	public List<String> getWindowTitles() {
// 
// 		List<String> frameTitles = new ArrayList<String>();
// 		Component[] extendedFrames = ((Container) getSource()).getComponents();
// 		for (Component frame : extendedFrames) {
// 			Component[] children = ((Container) frame).getComponents();
// 			for (Component child : children) {
// 				if (ComponentType.TITLE_BAR.matches(child)) {
// 					boolean visible = ObjectUtil.getBoolean(frame, "isVisible()");
// 					String title = ObjectUtil.getString(child, "getLWWindow().getTitle()");
// 					Logger.debug("Found window '" + title + "' [visible=" + visible + "]");
// 					if (visible) {
// 						frameTitles.add(title);
// 					}
// 				}
// 			}
// 		}
// 
// 		return frameTitles;
// 	}
// 
// 	public String getSelectedWindowTitle() {
// 
// 		Component[] extendedFrames = ((Container) getSource()).getComponents();
// 		for (Component frame : extendedFrames) {
// 			if (frame.isFocusOwner()) {
// 				Component[] children = ((Container) frame).getComponents();
// 				for (Component child : children) {
// 
// 					if (ComponentType.TITLE_BAR.matches(child)) {
// 						boolean visible = ObjectUtil.getBoolean(frame, "isVisible()");
// 						if (visible) {
// 							return ObjectUtil.getString(child, "getLWWindow().getTitle()");
// 						}
// 					}
// 				}
// 			}
// 		}
// 		return null;
// 	}
// 
// 	/**
// 	 * Change the current context to the window with the given title.
// 	 * 
// 	 * @param windowTitle
// 	 *            title of the window.
// 	 */
// 	public void setWindowAsContext(String windowTitle) {
// 
// 		Component[] extendedFrames = ((Container) getSource()).getComponents();
// 		for (Component frame : extendedFrames) {
// 			Component[] children = ((Container) frame).getComponents();
// 			for (Component child : children) {
// 				if (ComponentType.TITLE_BAR.matches(child)) {
// 
// 					String title = ObjectUtil.getString(child, "getLWWindow().getTitle()");
// 					if (TextUtil.matches(title, windowTitle)) {
// 						FormsContext.setContext(new FrameOperator((Container) frame));
// 						frame.requestFocus();
// 						Logger.info("Context set to window '" + title + "'");
// 						return;
// 					}
// 				}
// 			}
// 		}
// 
// 		throw new FormsLibraryException("No window with title '" + windowTitle + "' found.");
// 	}
// 
// 	/**
// 	 * Close all open windows in the application except the main menu.
// 	 */
// 	public void closeOpenWindows() {
// 
// 		// reset context before closing windows
// 		FormsContext.resetContext();
// 
// 		List<Component> framesToClose = new ArrayList<Component>();
// 
// 		Component[] extendedFrames = ((Container) getSource()).getComponents();
// 		for (Component frame : extendedFrames) {
// 			Component[] children = ((Container) frame).getComponents();
// 			for (Component child : children) {
// 				if (ComponentType.TITLE_BAR.matches(child)) {
// 					if (ObjectUtil.getBoolean(frame, "isVisible()")) {
// 						String title = ObjectUtil.getString(child, "getLWWindow().getTitle()");
// 						if (!title.contains("Main Menu")) {
// 							Logger.info("Closing window '" + title + "'");
// 							framesToClose.add(frame);
// 						}
// 					}
// 				}
// 			}
// 		}
// 
// 		for (Component frame : framesToClose) {
// 			ObjectUtil.invokeMethod(frame, "close()");
// 		}
// 	}
// 
// }
// 
// Path: src/main/java/org/robotframework/formslibrary/util/Logger.java
// public class Logger {
// 
//     public static void debug(String message) {
//         if (DebugUtil.isDebugEnabled()) {
//             System.out.println("~ " + message);
//         }
//     }
// 
//     public static void info(String message) {
//         System.out.println(message);
//     }
// 
//     public static void error(Throwable t) {
//         t.printStackTrace();
//     }
// }


import java.util.List;
import org.robotframework.formslibrary.operator.LWWindowOperator;
import org.robotframework.formslibrary.util.Logger;

package org.robotframework.formslibrary.context;



/**
 * Monitor the open windows before and after an action. When a new window is
 * detected or the current window was closed, the current context will be
 * changed accordingly.
 */
public class ContextChangeMonitor {

	private List<String> initialOpenLWWindows;

	/**
	 * Start monitoring for window changes.
	 */
	public void start() {
		if (FormsContext.isFormsServicesApp()) {
			initialOpenLWWindows = new LWWindowOperator().getWindowTitles();
		} else {

		}
	}

	/**
	 * Stop monitoring for window changes and change the context if needed. When
	 * a new window is detected, it will be automatically set as the new
	 * context. When the current context window was closed, the context will be
	 * reset to the root context.
	 */
	public void stop() {

// Path: src/main/java/org/robotframework/formslibrary/operator/LWWindowOperator.java
// public class LWWindowOperator extends AbstractRootComponentOperator {
// 
// 	/**
// 	 * Initialize a window operator with the main oracle forms desktop window.
// 	 */
// 	public LWWindowOperator() {
// 		super(new ByComponentTypeChooser(0, ComponentType.FORM_DESKTOP));
// 	}
// 
// 	public LWWindowOperator(ComponentChooser chooser) {
// 		super(chooser);
// 	}
// 
// 	/**
// 	 * Retrieve a list of all window titles in the root context.
// 	 */
// 	public List<String> getWindowTitles() {
// 
// 		List<String> frameTitles = new ArrayList<String>();
// 		Component[] extendedFrames = ((Container) getSource()).getComponents();
// 		for (Component frame : extendedFrames) {
// 			Component[] children = ((Container) frame).getComponents();
// 			for (Component child : children) {
// 				if (ComponentType.TITLE_BAR.matches(child)) {
// 					boolean visible = ObjectUtil.getBoolean(frame, "isVisible()");
// 					String title = ObjectUtil.getString(child, "getLWWindow().getTitle()");
// 					Logger.debug("Found window '" + title + "' [visible=" + visible + "]");
// 					if (visible) {
// 						frameTitles.add(title);
// 					}
// 				}
// 			}
// 		}
// 
// 		return frameTitles;
// 	}
// 
// 	public String getSelectedWindowTitle() {
// 
// 		Component[] extendedFrames = ((Container) getSource()).getComponents();
// 		for (Component frame : extendedFrames) {
// 			if (frame.isFocusOwner()) {
// 				Component[] children = ((Container) frame).getComponents();
// 				for (Component child : children) {
// 
// 					if (ComponentType.TITLE_BAR.matches(child)) {
// 						boolean visible = ObjectUtil.getBoolean(frame, "isVisible()");
// 						if (visible) {
// 							return ObjectUtil.getString(child, "getLWWindow().getTitle()");
// 						}
// 					}
// 				}
// 			}
// 		}
// 		return null;
// 	}
// 
// 	/**
// 	 * Change the current context to the window with the given title.
// 	 * 
// 	 * @param windowTitle
// 	 *            title of the window.
// 	 */
// 	public void setWindowAsContext(String windowTitle) {
// 
// 		Component[] extendedFrames = ((Container) getSource()).getComponents();
// 		for (Component frame : extendedFrames) {
// 			Component[] children = ((Container) frame).getComponents();
// 			for (Component child : children) {
// 				if (ComponentType.TITLE_BAR.matches(child)) {
// 
// 					String title = ObjectUtil.getString(child, "getLWWindow().getTitle()");
// 					if (TextUtil.matches(title, windowTitle)) {
// 						FormsContext.setContext(new FrameOperator((Container) frame));
// 						frame.requestFocus();
// 						Logger.info("Context set to window '" + title + "'");
// 						return;
// 					}
// 				}
// 			}
// 		}
// 
// 		throw new FormsLibraryException("No window with title '" + windowTitle + "' found.");
// 	}
// 
// 	/**
// 	 * Close all open windows in the application except the main menu.
// 	 */
// 	public void closeOpenWindows() {
// 
// 		// reset context before closing windows
// 		FormsContext.resetContext();
// 
// 		List<Component> framesToClose = new ArrayList<Component>();
// 
// 		Component[] extendedFrames = ((Container) getSource()).getComponents();
// 		for (Component frame : extendedFrames) {
// 			Component[] children = ((Container) frame).getComponents();
// 			for (Component child : children) {
// 				if (ComponentType.TITLE_BAR.matches(child)) {
// 					if (ObjectUtil.getBoolean(frame, "isVisible()")) {
// 						String title = ObjectUtil.getString(child, "getLWWindow().getTitle()");
// 						if (!title.contains("Main Menu")) {
// 							Logger.info("Closing window '" + title + "'");
// 							framesToClose.add(frame);
// 						}
// 					}
// 				}
// 			}
// 		}
// 
// 		for (Component frame : framesToClose) {
// 			ObjectUtil.invokeMethod(frame, "close()");
// 		}
// 	}
// 
// }
// 
// Path: src/main/java/org/robotframework/formslibrary/util/Logger.java
// public class Logger {
// 
//     public static void debug(String message) {
//         if (DebugUtil.isDebugEnabled()) {
//             System.out.println("~ " + message);
//         }
//     }
// 
//     public static void info(String message) {
//         System.out.println(message);
//     }
// 
//     public static void error(Throwable t) {
//         t.printStackTrace();
//     }
// }


// Path: src/main/java/org/robotframework/formslibrary/context/ContextChangeMonitor.java
import java.util.List;
import org.robotframework.formslibrary.operator.LWWindowOperator;
import org.robotframework.formslibrary.util.Logger;

package org.robotframework.formslibrary.context;



/**
 * Monitor the open windows before and after an action. When a new window is
 * detected or the current window was closed, the current context will be
 * changed accordingly.
 */
public class ContextChangeMonitor {

	private List<String> initialOpenLWWindows;

	/**
	 * Start monitoring for window changes.
	 */
	public void start() {
		if (FormsContext.isFormsServicesApp()) {
			initialOpenLWWindows = new LWWindowOperator().getWindowTitles();
		} else {

		}
	}

	/**
	 * Stop monitoring for window changes and change the context if needed. When
	 * a new window is detected, it will be automatically set as the new
	 * context. When the current context window was closed, the context will be
	 * reset to the root context.
	 */
	public void stop() {

		if (FormsContext.isFormsServicesApp()) {
