Index: /trunk/autoquest-plugin-jfc/src/main/java/de/ugoe/cs/autoquest/plugin/jfc/commands/CMDgenerateJacaretoReplay.java
===================================================================
--- /trunk/autoquest-plugin-jfc/src/main/java/de/ugoe/cs/autoquest/plugin/jfc/commands/CMDgenerateJacaretoReplay.java	(revision 1687)
+++ /trunk/autoquest-plugin-jfc/src/main/java/de/ugoe/cs/autoquest/plugin/jfc/commands/CMDgenerateJacaretoReplay.java	(revision 1688)
@@ -36,4 +36,38 @@
 import de.ugoe.cs.util.console.GlobalDataContainer;
 
+
+// helper class for the tree like structure part within a Jacareto file
+class StructureNode {
+    public String content;
+    public ArrayList<StructureNode> children;
+
+    public StructureNode(String content) {
+        this.content = content;
+        this.children = new ArrayList<StructureNode>();
+    }
+
+    public StructureNode add(String content) {
+        StructureNode node = new StructureNode(content);
+        children.add(node);
+        return node;
+    }
+
+    @Override
+    public String toString() {
+        String separator = System.getProperty("line.separator");
+        String result = content + separator;
+
+        for (StructureNode child : children) {
+            result += child.toString();
+        }
+
+        if (content.startsWith("<Recordable")) {
+            return result;
+        }
+
+        return result + "</StructureElement>" + separator;
+    }
+}
+
 /**
  * <p>
@@ -48,4 +82,9 @@
     private int nextRef;
     private JFCGUIElement currentFocus;
+
+    private StructureNode structure;
+    private StructureNode lastMouseClickEvent;
+    private StructureNode lastFocusChangeEvent;
+    private StructureNode lastItemActionEvent;
 
     /*
@@ -118,10 +157,8 @@
     }
 
-    private ArrayList<String> writeJacaretoEvents(BufferedWriter writer,
-                                                  Collection<List<Event>> sequences)
+    private void writeJacaretoEvents(BufferedWriter writer, Collection<List<Event>> sequences)
         throws IOException
     {
-        ArrayList<String> structure = new ArrayList<String>();
-        structure.add("<StructureElement class=\"jacareto.struct.RootElement\">");
+        structure = new StructureNode("<StructureElement class=\"jacareto.struct.RootElement\">");
         // reference the elements that we included in the header
         structure.add("<Recordable ref=\"0\" />"); // Calendar
@@ -137,39 +174,45 @@
 
                 if (event.getType() instanceof MouseButtonDown) {
-                    structure.add("<StructureElement class=\"jacareto.struct.MouseClick\">");
-                    writeMouseClickEvent(writer, structure, event, 501);
+                    lastMouseClickEvent =
+                        new StructureNode("<StructureElement class=\"jacareto.struct.MouseClick\">");
+                    writeMouseClickEvent(writer, event, 501);
                 }
                 else if (event.getType() instanceof MouseButtonUp) {
-                    writeMouseClickEvent(writer, structure, event, 502);
+                    writeMouseClickEvent(writer, event, 502);
                 }
                 else if (event.getType() instanceof MouseClick) {
-                    writeMouseClickEvent(writer, structure, event, 500);
-                    structure.add("</StructureElement>");
+                    writeMouseClickEvent(writer, event, 500);
                     // FIXME: don't always write an item action
-                    writeItemActionEvent(writer, structure, event);
+                    writeItemActionEvent(writer, event);
+                    // FIXME: don't write it all here because there
+                    // might be no item action at all
+                    if (lastFocusChangeEvent == null) {
+                        // write structure sequentially
+                        structure.children.add(lastMouseClickEvent);
+                        structure.children.add(lastItemActionEvent);
+                    }
+                    else {
+                        // with nested structure
+                        structure.children.add(lastItemActionEvent);
+                        lastItemActionEvent.children.add(0, lastFocusChangeEvent);
+                        lastFocusChangeEvent.children.add(0, lastMouseClickEvent);
+
+                        lastFocusChangeEvent = null;
+                    }
                 }
-                /*
                 else if (event.getType() instanceof KeyboardFocusChange) {
-                    writeFocusChangeEvent(writer, structure, event);
+                    writeFocusChangeEvent(writer, event);
                 }
-                */
             }
         }
-
-        return structure;
-    }
-
-    private void writeJacaretoTail(BufferedWriter writer, ArrayList<String> structure)
-        throws IOException
-    {
+    }
+
+    private void writeJacaretoTail(BufferedWriter writer) throws IOException {
         writeLine(writer, "</Record>");
 
         // write the recording's structure
         writeLine(writer, "<Structure>");
-        for (String element : structure) {
-            writeLine(writer, element);
-        }
+        writer.write(structure.toString());
         // close root element
-        writeLine(writer, "</StructureElement>");
         writeLine(writer, "</Structure>");
     }
@@ -180,6 +223,6 @@
         try {
             writeJacaretoHead(writer);
-            ArrayList<String> structure = writeJacaretoEvents(writer, sequences);
-            writeJacaretoTail(writer, structure);
+            writeJacaretoEvents(writer, sequences);
+            writeJacaretoTail(writer);
             writeLine(writer, "</JacaretoStructure>");
 
@@ -230,8 +273,5 @@
     }
 
-    private void writeItemActionEvent(BufferedWriter writer,
-                                      ArrayList<String> structure,
-                                      Event event) throws IOException
-    {
+    private void writeItemActionEvent(BufferedWriter writer, Event event) throws IOException {
         JFCGUIElement target = (JFCGUIElement) event.getTarget();
         MouseButtonInteraction info = (MouseButtonInteraction) event.getType();
@@ -261,22 +301,22 @@
         );
         //@formatter:on
-        structure.add("<StructureElement class=\"jacareto.struct.ItemStateChange\">");
-        structure.add("<Recordable ref=\"" + (nextRef++) + "\" />");
-        structure.add("<Recordable ref=\"" + (nextRef++) + "\" />");
-        structure.add("</StructureElement>");
-    }
-
-    private void writeFocusChangeEvent(BufferedWriter writer,
-                                       ArrayList<String> structure,
-                                       Event event) throws IOException
-    {
+        lastItemActionEvent =
+            new StructureNode("<StructureElement class=\"jacareto.struct.ItemStateChange\">");
+        lastItemActionEvent.add("<Recordable ref=\"" + (nextRef++) + "\" />");
+        lastItemActionEvent.add("<Recordable ref=\"" + (nextRef++) + "\" />");
+    }
+
+    private void writeFocusChangeEvent(BufferedWriter writer, Event event) throws IOException {
         KeyboardFocusChange info = (KeyboardFocusChange) event.getType();
         JFCGUIElement target = (JFCGUIElement) event.getTarget();
 
         if (currentFocus != null) {
+            lastFocusChangeEvent =
+                new StructureNode("<StructureElement class=\"jacareto.struct.FocusChange\">");
+
             // focus lost on old target
-            writeFocusEvent(writer, structure, info, currentFocus, 1005);
+            writeFocusEvent(writer, info, currentFocus, 1005);
             // focus gained on new target
-            writeFocusEvent(writer, structure, info, target, 1004);
+            writeFocusEvent(writer, info, target, 1004);
         }
         else {
@@ -290,5 +330,4 @@
 
     private void writeFocusEvent(BufferedWriter writer,
-                                 ArrayList<String> structure,
                                  KeyboardFocusChange info,
                                  JFCGUIElement target,
@@ -313,10 +352,9 @@
         );
         //@formatter:on
-    }
-
-    private void writeMouseClickEvent(BufferedWriter writer,
-                                      ArrayList<String> structure,
-                                      Event event,
-                                      int jacId) throws IOException
+        lastFocusChangeEvent.add("<Recordable ref=\"" + (nextRef++) + "\" />");
+    }
+
+    private void writeMouseClickEvent(BufferedWriter writer, Event event, int jacId)
+        throws IOException
     {
         MouseButtonInteraction info = (MouseButtonInteraction) event.getType();
@@ -356,5 +394,5 @@
         //@formatter:on
 
-        structure.add("<Recordable ref=\"" + (nextRef++) + "\" />");
+        lastMouseClickEvent.add("<Recordable ref=\"" + (nextRef++) + "\" />");
     }
 
