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

Last change on this file since 1857 was 1857, checked in by sherbold, 9 years ago
  • added command parseJFCDirwithJacaretoIndicies
  • modified command generateJacaretoReplay such that it now can handle multiple sequences at once
  • added new GUI mappings
  • Property svn:mime-type set to text/plain
File size: 8.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.io.File;
18import java.nio.charset.Charset;
19import java.nio.file.Files;
20import java.nio.file.Paths;
21import java.util.Collection;
22import java.util.HashMap;
23import java.util.List;
24import java.util.logging.Level;
25
26import de.ugoe.cs.autoquest.CommandHelpers;
27import de.ugoe.cs.autoquest.eventcore.Event;
28import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModel;
29import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement;
30import de.ugoe.cs.autoquest.plugin.jfc.JFCSimplifiedLogParser;
31import de.ugoe.cs.autoquest.plugin.jfc.guimodel.JFCGUIElement;
32import de.ugoe.cs.autoquest.plugin.jfc.guimodel.JFCGUIElementSpec;
33import de.ugoe.cs.autoquest.plugin.jfc.guimodel.JFCMenu;
34import de.ugoe.cs.autoquest.plugin.jfc.guimodel.JFCMenuButton;
35import de.ugoe.cs.util.console.Command;
36import de.ugoe.cs.util.console.Console;
37import de.ugoe.cs.util.console.GlobalDataContainer;
38
39/**
40 * <p>
41 * TODO comment
42 * </p>
43 *
44 * @author Steffen Herbold
45 */
46public class CMDparseJFCDirwithJacaretoIndices implements Command {
47
48    private List<String> menuList;
49
50    /*
51     * (non-Javadoc)
52     *
53     * @see de.ugoe.cs.util.console.Command#run(java.util.List)
54     */
55    @Override
56    public void run(List<Object> parameters) {
57        String path;
58        String sequencesName = "sequences";
59
60        try {
61            path = (String) parameters.get(0);
62            if (parameters.size() >= 2) {
63                sequencesName = (String) parameters.get(1);
64            }
65            if (parameters.size() >= 3) {
66                menuList =
67                    Files.readAllLines(Paths.get((String) parameters.get(2)),
68                                       Charset.defaultCharset());
69            }
70
71        }
72        catch (Exception e) {
73            throw new IllegalArgumentException();
74        }
75
76        File folder = new File(path);
77        if (!folder.isDirectory()) {
78            Console.printerrln(path + " is not a directory");
79            return;
80        }
81
82        JFCSimplifiedLogParser parser = new JFCSimplifiedLogParser();
83
84        String absolutPath = folder.getAbsolutePath();
85        for (String filename : folder.list()) {
86            String source = absolutPath + File.separator + filename;
87            Console.traceln(Level.INFO, "Processing file: " + source);
88
89            try {
90                parser.parseFile(source);
91            }
92            catch (Exception e) {
93                Console.printerrln("Could not parse " + source + ": " + e.getMessage());
94            }
95        }
96
97        Collection<List<Event>> sequences = parser.getSequences();
98
99        GUIModel targets = parser.getGuiModel();
100
101        generateJacaretoIndices(targets.getRootElements(), targets);
102
103        if (GlobalDataContainer.getInstance().addData(sequencesName, sequences)) {
104            CommandHelpers.dataOverwritten(sequencesName);
105        }
106
107        if (GlobalDataContainer.getInstance().addData(sequencesName + "_targets", targets)) {
108            CommandHelpers.dataOverwritten(sequencesName + "_targets");
109        }
110    }
111
112    /*
113     * (non-Javadoc)
114     *
115     * @see de.ugoe.cs.util.console.Command#help()
116     */
117    @Override
118    public String help() {
119        // TODO Auto-generated method stub
120        System.out.println("parseJFCDirwithJacaretoIndices <path> {<sequencesName>} {<menuFile>}");
121        return null;
122    }
123
124    // duplicates to parseJFCwithJacaretoIndices
125    private int findPopupMenuIndex(IGUIElement item, GUIModel model) {
126        // TODO: refactor
127        int index = -1;
128        List<IGUIElement> children = model.getChildren(item);
129        IGUIElement menuChild = null;
130
131        // try to identify this popup menu by looking at its children
132        // find the first menu item child of this item
133        for (IGUIElement child : children) {
134            if (child instanceof JFCMenuButton || child instanceof JFCMenu) {
135                menuChild = child;
136                break;
137            }
138        }
139
140        if (menuChild == null) {
141            // this popup menu cannot be identified
142            // TODO: exception, logging etc
143            return -1;
144        }
145
146        // find line that contains this menu item name
147        String itemName = ((JFCGUIElement) menuChild).getName().trim().toLowerCase();
148        int lineOfItem = -1;
149
150        for (int i = 0; i < menuList.size(); i++) {
151            String name = "\"" + menuList.get(i).trim().toLowerCase() + "\"";
152            if (name.equals(itemName)) {
153                lineOfItem = i;
154                break;
155            }
156        }
157
158        if (lineOfItem == -1) {
159            // failed to find this item in the menu file
160            // TODO: exception, logging etc
161            return -1;
162        }
163
164        // find menu item's parent line
165        String stripped = menuList.get(lineOfItem).replaceFirst("^ *", "");
166        int indent = menuList.get(lineOfItem).length() - stripped.length();
167
168        if (indent != 0) {
169            for (int i = lineOfItem; i >= 0; i--) {
170                stripped = menuList.get(i).replaceFirst("^ *", "");
171                int oldIndent = menuList.get(i).length() - stripped.length();
172
173                if (oldIndent < indent) {
174                    lineOfItem = i;
175                    break;
176                }
177            }
178        }
179
180        // get the item's indentation
181        stripped = menuList.get(lineOfItem).replaceFirst("^ *", "");
182        indent = menuList.get(lineOfItem).length() - stripped.length();
183
184        if (indent == 0) {
185            // top level menu item, just count in which position it is
186            for (int i = 0; i <= lineOfItem; i++) {
187                if (!menuList.get(i).startsWith(" ")) {
188                    index++;
189                }
190            }
191        }
192        else {
193            // find the topmenu index in the menu file by going backwards
194            for (int i = lineOfItem; i >= 0; i--) {
195                stripped = menuList.get(i).replaceFirst("^ *", "");
196                int oldIndent = menuList.get(i).length() - stripped.length();
197
198                if (oldIndent < indent) {
199                    index = lineOfItem - i;
200                    break;
201                }
202            }
203        }
204
205        return index;
206    }
207
208    private void generateJacaretoIndices(List<IGUIElement> elements, GUIModel targets) {
209        HashMap<String, Integer> typeCount = new HashMap<>();
210
211        for (IGUIElement child : elements) {
212            String type = child.getSpecification().getType();
213            Integer count = typeCount.get(type);
214
215            if (count == null) {
216                count = 0;
217                typeCount.put(type, count);
218            }
219            else {
220                typeCount.put(type, ++count);
221            }
222
223            if (menuList != null && type.equals("javax.swing.JPopupMenu")) {
224                // try to use a workaround for popup menu index problems
225                int index = findPopupMenuIndex((IGUIElement) child, targets);
226                if (index != -1) {
227                    ((JFCGUIElementSpec) child.getSpecification()).setAltIndex(index);
228                }
229                else {
230                    // workaround failed, use normal method as fallback
231                    ((JFCGUIElementSpec) child.getSpecification()).setAltIndex(count);
232                }
233            }
234            else {
235                ((JFCGUIElementSpec) child.getSpecification()).setAltIndex(count);
236            }
237
238            generateJacaretoIndices(targets.getChildren(child), targets);
239        }
240    }
241
242}
Note: See TracBrowser for help on using the repository browser.