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

Last change on this file since 1902 was 1902, checked in by dmay, 9 years ago

condense gui model before generating jacareto indices

  • 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        targets.condenseModel();
101
102        generateJacaretoIndices(targets.getRootElements(), targets);
103
104        if (GlobalDataContainer.getInstance().addData(sequencesName, sequences)) {
105            CommandHelpers.dataOverwritten(sequencesName);
106        }
107
108        if (GlobalDataContainer.getInstance().addData(sequencesName + "_targets", targets)) {
109            CommandHelpers.dataOverwritten(sequencesName + "_targets");
110        }
111    }
112
113    /*
114     * (non-Javadoc)
115     *
116     * @see de.ugoe.cs.util.console.Command#help()
117     */
118    @Override
119    public String help() {
120        // TODO Auto-generated method stub
121        System.out.println("parseJFCDirwithJacaretoIndices <path> {<sequencesName>} {<menuFile>}");
122        return null;
123    }
124
125    // duplicates to parseJFCwithJacaretoIndices
126    private int findPopupMenuIndex(IGUIElement item, GUIModel model) {
127        // TODO: refactor
128        int index = -1;
129        List<IGUIElement> children = model.getChildren(item);
130        IGUIElement menuChild = null;
131
132        // try to identify this popup menu by looking at its children
133        // find the first menu item child of this item
134        for (IGUIElement child : children) {
135            if (child instanceof JFCMenuButton || child instanceof JFCMenu) {
136                menuChild = child;
137                break;
138            }
139        }
140
141        if (menuChild == null) {
142            // this popup menu cannot be identified
143            // TODO: exception, logging etc
144            return -1;
145        }
146
147        // find line that contains this menu item name
148        String itemName = ((JFCGUIElement) menuChild).getName().trim().toLowerCase();
149        int lineOfItem = -1;
150
151        for (int i = 0; i < menuList.size(); i++) {
152            String name = "\"" + menuList.get(i).trim().toLowerCase() + "\"";
153            if (name.equals(itemName)) {
154                lineOfItem = i;
155                break;
156            }
157        }
158
159        if (lineOfItem == -1) {
160            // failed to find this item in the menu file
161            // TODO: exception, logging etc
162            return -1;
163        }
164
165        // find menu item's parent line
166        String stripped = menuList.get(lineOfItem).replaceFirst("^ *", "");
167        int indent = menuList.get(lineOfItem).length() - stripped.length();
168
169        if (indent != 0) {
170            for (int i = lineOfItem; i >= 0; i--) {
171                stripped = menuList.get(i).replaceFirst("^ *", "");
172                int oldIndent = menuList.get(i).length() - stripped.length();
173
174                if (oldIndent < indent) {
175                    lineOfItem = i;
176                    break;
177                }
178            }
179        }
180
181        // get the item's indentation
182        stripped = menuList.get(lineOfItem).replaceFirst("^ *", "");
183        indent = menuList.get(lineOfItem).length() - stripped.length();
184
185        if (indent == 0) {
186            // top level menu item, just count in which position it is
187            for (int i = 0; i <= lineOfItem; i++) {
188                if (!menuList.get(i).startsWith(" ")) {
189                    index++;
190                }
191            }
192        }
193        else {
194            // find the topmenu index in the menu file by going backwards
195            for (int i = lineOfItem; i >= 0; i--) {
196                stripped = menuList.get(i).replaceFirst("^ *", "");
197                int oldIndent = menuList.get(i).length() - stripped.length();
198
199                if (oldIndent < indent) {
200                    index = lineOfItem - i;
201                    break;
202                }
203            }
204        }
205
206        return index;
207    }
208
209    private void generateJacaretoIndices(List<IGUIElement> elements, GUIModel targets) {
210        HashMap<String, Integer> typeCount = new HashMap<>();
211
212        for (IGUIElement child : elements) {
213            String type = child.getSpecification().getType();
214            Integer count = typeCount.get(type);
215
216            if (count == null) {
217                count = 0;
218                typeCount.put(type, count);
219            }
220            else {
221                typeCount.put(type, ++count);
222            }
223
224            if (menuList != null && type.equals("javax.swing.JPopupMenu")) {
225                // try to use a workaround for popup menu index problems
226                int index = findPopupMenuIndex((IGUIElement) child, targets);
227                if (index != -1) {
228                    ((JFCGUIElementSpec) child.getSpecification()).setAltIndex(index);
229                }
230                else {
231                    // workaround failed, use normal method as fallback
232                    ((JFCGUIElementSpec) child.getSpecification()).setAltIndex(count);
233                }
234            }
235            else {
236                ((JFCGUIElementSpec) child.getSpecification()).setAltIndex(count);
237            }
238
239            generateJacaretoIndices(targets.getChildren(child), targets);
240        }
241    }
242
243}
Note: See TracBrowser for help on using the repository browser.