source: trunk/autoquest-plugin-jfc/src/main/java/de/ugoe/cs/autoquest/plugin/jfc/commands/CMDparseJFCwithJacaretoIndices.java @ 1834

Last change on this file since 1834 was 1834, checked in by dmay, 10 years ago

Work of the last week: Completely redid most of the menu handling, since there were too many cornercases that did not work. Turns out, that in these cases, wrong indices were generated. Fixed.

  • Property svn:mime-type set to text/plain
File size: 7.2 KB
Line 
1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.autoquest.plugin.jfc.commands;
16
17import java.nio.charset.Charset;
18import java.nio.file.Files;
19import java.nio.file.Paths;
20import java.util.Collection;
21import java.util.HashMap;
22import java.util.List;
23
24import de.ugoe.cs.autoquest.CommandHelpers;
25import de.ugoe.cs.autoquest.eventcore.Event;
26import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModel;
27import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement;
28import de.ugoe.cs.autoquest.plugin.jfc.JFCSimplifiedLogParser;
29import de.ugoe.cs.autoquest.plugin.jfc.guimodel.JFCGUIElement;
30import de.ugoe.cs.autoquest.plugin.jfc.guimodel.JFCGUIElementSpec;
31import de.ugoe.cs.autoquest.plugin.jfc.guimodel.JFCMenu;
32import de.ugoe.cs.autoquest.plugin.jfc.guimodel.JFCMenuButton;
33import de.ugoe.cs.util.console.Command;
34import de.ugoe.cs.util.console.Console;
35import de.ugoe.cs.util.console.GlobalDataContainer;
36
37/**
38 * <p>
39 * Command to generate sessions in which the JFCGUIElements contain a special index that is required
40 * to replay the sequences with Jacareto.
41 * </p>
42 *
43 * @author Daniel May
44 * @version 1.0
45 */
46public class CMDparseJFCwithJacaretoIndices implements Command {
47
48    private List<String> menuList;
49
50    @Override
51    public void run(List<Object> parameters) {
52        String filename;
53        String sequencesName = "sequences";
54        try {
55            filename = (String) parameters.get(0);
56            if (parameters.size() >= 2) {
57                menuList =
58                    Files.readAllLines(Paths.get((String) parameters.get(1)),
59                                       Charset.defaultCharset());
60            }
61        }
62        catch (Exception e) {
63            throw new IllegalArgumentException();
64        }
65
66        JFCSimplifiedLogParser parser = new JFCSimplifiedLogParser();
67
68        try {
69            parser.parseFile(filename);
70        }
71        catch (Exception e) {
72            Console.printerrln("Could not parse " + filename + ": " + e.getMessage());
73            return;
74        }
75
76        Collection<List<Event>> sequences = parser.getSequences();
77        GUIModel targets = parser.getGuiModel();
78
79        generateJacaretoIndices(targets.getRootElements(), targets);
80
81        if (GlobalDataContainer.getInstance().addData(sequencesName, sequences)) {
82            CommandHelpers.dataOverwritten(sequencesName);
83        }
84
85        if (GlobalDataContainer.getInstance().addData(sequencesName + "_targets", targets)) {
86            CommandHelpers.dataOverwritten(sequencesName + "_targets");
87        }
88    }
89
90    @Override
91    public String help() {
92        return "parseJFCwithJacaretoIndices <filename> {<menufile>}";
93    }
94
95    private int findPopupMenuIndex(IGUIElement item, GUIModel model) {
96        // TODO: refactor
97        int index = -1;
98        List<IGUIElement> children = model.getChildren(item);
99        IGUIElement menuChild = null;
100
101        // try to identify this popup menu by looking at its children
102        // find the first menu item child of this item
103        for (IGUIElement child : children) {
104            if (child instanceof JFCMenuButton || child instanceof JFCMenu) {
105                menuChild = child;
106                break;
107            }
108        }
109
110        if (menuChild == null) {
111            // this popup menu cannot be identified
112            // TODO: exception, logging etc
113            return -1;
114        }
115
116        // find line that contains this menu item name
117        String itemName = ((JFCGUIElement) menuChild).getName().trim().toLowerCase();
118        int lineOfItem = -1;
119
120        for (int i = 0; i < menuList.size(); i++) {
121            String name = "\"" + menuList.get(i).trim().toLowerCase() + "\"";
122            if (name.equals(itemName)) {
123                lineOfItem = i;
124                break;
125            }
126        }
127
128        if (lineOfItem == -1) {
129            // failed to find this item in the menu file
130            // TODO: exception, logging etc
131            return -1;
132        }
133
134        // find menu item's parent line
135        String stripped = menuList.get(lineOfItem).replaceFirst("^ *", "");
136        int indent = menuList.get(lineOfItem).length() - stripped.length();
137
138        if (indent != 0) {
139            for (int i = lineOfItem; i >= 0; i--) {
140                stripped = menuList.get(i).replaceFirst("^ *", "");
141                int oldIndent = menuList.get(i).length() - stripped.length();
142
143                if (oldIndent < indent) {
144                    lineOfItem = i;
145                    break;
146                }
147            }
148        }
149
150        // get the item's indentation
151        stripped = menuList.get(lineOfItem).replaceFirst("^ *", "");
152        indent = menuList.get(lineOfItem).length() - stripped.length();
153
154        if (indent == 0) {
155            // top level menu item, just count in which position it is
156            for (int i = 0; i <= lineOfItem; i++) {
157                if (!menuList.get(i).startsWith(" ")) {
158                    index++;
159                }
160            }
161        }
162        else {
163            // find the topmenu index in the menu file by going backwards
164            for (int i = lineOfItem; i >= 0; i--) {
165                stripped = menuList.get(i).replaceFirst("^ *", "");
166                int oldIndent = menuList.get(i).length() - stripped.length();
167
168                if (oldIndent < indent) {
169                    index = lineOfItem - i;
170                    break;
171                }
172            }
173        }
174
175        return index;
176    }
177
178    private void generateJacaretoIndices(List<IGUIElement> elements, GUIModel targets) {
179        HashMap<String, Integer> typeCount = new HashMap<>();
180
181        for (IGUIElement child : elements) {
182            String type = child.getSpecification().getType();
183            Integer count = typeCount.get(type);
184
185            if (count == null) {
186                count = 0;
187                typeCount.put(type, count);
188            }
189            else {
190                typeCount.put(type, ++count);
191            }
192
193            if (menuList != null && type.equals("javax.swing.JPopupMenu")) {
194                // try to use a workaround for popup menu index problems
195                int index = findPopupMenuIndex((IGUIElement) child, targets);
196                if (index != -1) {
197                    ((JFCGUIElementSpec) child.getSpecification()).setAltIndex(index);
198                }
199                else {
200                    // workaround failed, use normal method as fallback
201                    ((JFCGUIElementSpec) child.getSpecification()).setAltIndex(count);
202                }
203            }
204            else {
205                ((JFCGUIElementSpec) child.getSpecification()).setAltIndex(count);
206            }
207
208            generateJacaretoIndices(targets.getChildren(child), targets);
209        }
210    }
211}
Note: See TracBrowser for help on using the repository browser.