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

Last change on this file was 2282, checked in by pharms, 5 years ago
  • Property svn:mime-type set to text/plain
File size: 8.5 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        String[] children = folder.list();
86       
87        if (children != null) {
88            for (String filename : children) {
89                String source = absolutPath + File.separator + filename;
90                Console.traceln(Level.INFO, "Processing file: " + source);
91
92                try {
93                        parser.parseFile(source);
94                }
95                catch (Exception e) {
96                        Console.printerrln("Could not parse " + source + ": " + e.getMessage());
97                }
98            }
99        }
100
101        Collection<List<Event>> sequences = parser.getSequences();
102
103        GUIModel targets = parser.getGuiModel();
104        targets.condenseModel();
105
106        // TODO: throw our stuff before this
107       
108        generateJacaretoIndices(targets.getRootElements(), targets);
109       
110        // TODO: iterate over all sequences
111        // TODO: iterate over all events in the sequences
112        // TODO: for each event: find representing node in GUI element
113        // TODO: then update altIndex
114
115        if (GlobalDataContainer.getInstance().addData(sequencesName, sequences)) {
116            CommandHelpers.dataOverwritten(sequencesName);
117        }
118
119        if (GlobalDataContainer.getInstance().addData(sequencesName + "_targets", targets)) {
120            CommandHelpers.dataOverwritten(sequencesName + "_targets");
121        }
122    }
123
124    /*
125     * (non-Javadoc)
126     *
127     * @see de.ugoe.cs.util.console.Command#help()
128     */
129    @Override
130    public String help() {
131        return "parseJFCDirwithJacaretoIndices <path> {<sequencesName>} {<menuFile>}";
132    }
133
134    // duplicates to parseJFCwithJacaretoIndices
135    private int findPopupMenuIndex(IGUIElement item, GUIModel model) {
136        // TODO: refactor
137        int index = -1;
138        List<IGUIElement> children = model.getChildren(item);
139        IGUIElement menuChild = null;
140
141        // try to identify this popup menu by looking at its children
142        // find the first menu item child of this item
143        for (IGUIElement child : children) {
144            if (child instanceof JFCMenuButton || child instanceof JFCMenu) {
145                menuChild = child;
146                break;
147            }
148        }
149
150        if (menuChild == null) {
151            // this popup menu cannot be identified
152            // TODO: exception, logging etc
153            return -1;
154        }
155
156        // find line that contains this menu item name
157        String itemName = ((JFCGUIElement) menuChild).getName().trim().toLowerCase();
158        int lineOfItem = -1;
159
160        for (int i = 0; i < menuList.size(); i++) {
161            String name = "\"" + menuList.get(i).trim().toLowerCase() + "\"";
162            if (name.equals(itemName)) {
163                lineOfItem = i;
164                break;
165            }
166        }
167
168        if (lineOfItem == -1) {
169            // failed to find this item in the menu file
170            // TODO: exception, logging etc
171            return -1;
172        }
173
174        // find menu item's parent line
175        String stripped = menuList.get(lineOfItem).replaceFirst("^ *", "");
176        int indent = menuList.get(lineOfItem).length() - stripped.length();
177
178        if (indent != 0) {
179            for (int i = lineOfItem; i >= 0; i--) {
180                stripped = menuList.get(i).replaceFirst("^ *", "");
181                int oldIndent = menuList.get(i).length() - stripped.length();
182
183                if (oldIndent < indent) {
184                    lineOfItem = i;
185                    break;
186                }
187            }
188        }
189
190        // get the item's indentation
191        stripped = menuList.get(lineOfItem).replaceFirst("^ *", "");
192        indent = menuList.get(lineOfItem).length() - stripped.length();
193
194        if (indent == 0) {
195            // top level menu item, just count in which position it is
196            for (int i = 0; i <= lineOfItem; i++) {
197                if (!menuList.get(i).startsWith(" ")) {
198                    index++;
199                }
200            }
201        }
202        else {
203            // find the topmenu index in the menu file by going backwards
204            for (int i = lineOfItem; i >= 0; i--) {
205                stripped = menuList.get(i).replaceFirst("^ *", "");
206                int oldIndent = menuList.get(i).length() - stripped.length();
207
208                if (oldIndent < indent) {
209                    index = lineOfItem - i;
210                    break;
211                }
212            }
213        }
214
215        return index;
216    }
217
218    private void generateJacaretoIndices(List<IGUIElement> elements, GUIModel targets) {
219        HashMap<String, Integer> typeCount = new HashMap<>();
220
221        for (IGUIElement child : elements) {
222            String type = child.getSpecification().getType();
223            Integer count = typeCount.get(type);
224
225            if (count == null) {
226                count = 0;
227                typeCount.put(type, count);
228            }
229            else {
230                typeCount.put(type, ++count);
231            }
232
233            if (menuList != null && type.equals("javax.swing.JPopupMenu")) {
234                // try to use a workaround for popup menu index problems
235                int index = findPopupMenuIndex((IGUIElement) child, targets);
236                if (index != -1) {
237                    ((JFCGUIElementSpec) child.getSpecification()).setAltIndex(index);
238                }
239                else {
240                    // workaround failed, use normal method as fallback
241                    ((JFCGUIElementSpec) child.getSpecification()).setAltIndex(count);
242                }
243            }
244            else {
245                ((JFCGUIElementSpec) child.getSpecification()).setAltIndex(count);
246            }
247
248            generateJacaretoIndices(targets.getChildren(child), targets);
249        }
250    }
251
252}
Note: See TracBrowser for help on using the repository browser.