// Copyright 2012 Georg-August-Universität Göttingen, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package de.ugoe.cs.autoquest.plugin.jfc.commands;
import org.apache.commons.lang.StringEscapeUtils;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;
import java.util.UUID;
import java.util.logging.Level;
import javax.swing.UIManager;
import de.ugoe.cs.autoquest.CommandHelpers;
import de.ugoe.cs.autoquest.SequenceInstanceOf;
import de.ugoe.cs.util.console.Command;
import de.ugoe.cs.autoquest.eventcore.Event;
import de.ugoe.cs.autoquest.eventcore.IEventTarget;
import de.ugoe.cs.autoquest.eventcore.gui.*;
import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModel;
import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement;
import de.ugoe.cs.autoquest.keyboardmaps.VirtualKey;
import de.ugoe.cs.autoquest.plugin.jfc.guimodel.JFCGUIElement;
import de.ugoe.cs.autoquest.plugin.jfc.guimodel.JFCMenu;
import de.ugoe.cs.autoquest.plugin.jfc.guimodel.JFCMenuButton;
import de.ugoe.cs.util.console.Console;
import de.ugoe.cs.util.console.GlobalDataContainer;
/**
*
* Command to create a Jacareto xml replay file from stored sessions.
*
*
* @author Daniel May
* @version 1.0
*/
public class CMDgenerateJacaretoReplay implements Command {
/**
*
* Helper class for the tree like structure part within a Jacareto file.
*
*/
private static class StructureNode {
/**
*
* Keeps track of the next structure node id.
*
*/
public static int nextRef = 0;
/**
*
* The node's type packaged into an XML string.
*
*/
public String content;
/**
*
* This node's children.
*
*/
public ArrayList children;
/**
*
* Constructor. Creates a new StructureNode of a specified type and builds its Jacareto XML
* representation.
*
*
* @param type
* the type of this StructureNode, for example: 'MouseDownEvent'
*/
public StructureNode(String type) {
setContent(type);
children = new ArrayList();
}
/**
*
* Constructor. Creates a StructureNode of type 'Recordable' with a valid id and builds its
* Jacareto XML representation.
*
*/
public StructureNode() {
content = "";
children = new ArrayList();
}
/**
*
* Builds the XML representation of a Jacareto structure type.
*
*
* @param type
* the type of this StructureNode, for example: 'MouseDownEvent'
*/
public void setContent(String type) {
content = "";
}
/**
*
* Adds a new StructureNode as a child of this node.
*
*
* @param type
* the type of the child node, for example: 'MouseDownEvent'
*/
public StructureNode add(String type) {
StructureNode node = new StructureNode(type);
children.add(node);
return node;
}
/**
*
* Builds the XML representation of a Jacareto structure type.
*
*
* @param type
* the type of this StructureNode, for example: 'MouseDownEvent'
*/
public void addRecordable() {
children.add(new StructureNode());
}
/**
*
* Returns a Jacareto XML representation of this StructureNode, includin all its children.
*
*/
@Override
public String toString() {
String separator = System.getProperty("line.separator");
String result = content + separator;
for (StructureNode child : children) {
result += child.toString();
}
if (content.endsWith("/>")) {
return result;
}
return result + "" + separator;
}
}
/**
*
* The time it takes for Jacareto to replay an event in ms.
*
*/
private static final int EVENT_DURATION = 150;
/**
*
* The time it takes for Jacareto to replay each part of a double click event in ms.
*
*/
private static final int DOUBLE_CLICK_DURATION = 50;
/**
*
* Application startup time in ms. The application needs to be fully initialized before Jacareto
* can start replaying.
*
*/
private static final int STARTUP_DELAY = 10000;
/**
*
* The GUI element which is currently focused.
*
*/
private JFCGUIElement currentFocus;
/**
*
* A tree of StructureNodes which represents the structure part inside a Jacareto XML file.
*
*/
private StructureNode structure;
/**
*
* XML structure for key events modeled as StructureNodes.
*