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

Last change on this file since 2249 was 2233, checked in by pharms, 7 years ago
  • solved some findbugs issues
  • Property svn:mime-type set to text/plain
File size: 8.6 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        // TODO Auto-generated method stub
132        System.out.println("parseJFCDirwithJacaretoIndices <path> {<sequencesName>} {<menuFile>}");
133        return null;
134    }
135
136    // duplicates to parseJFCwithJacaretoIndices
137    private int findPopupMenuIndex(IGUIElement item, GUIModel model) {
138        // TODO: refactor
139        int index = -1;
140        List<IGUIElement> children = model.getChildren(item);
141        IGUIElement menuChild = null;
142
143        // try to identify this popup menu by looking at its children
144        // find the first menu item child of this item
145        for (IGUIElement child : children) {
146            if (child instanceof JFCMenuButton || child instanceof JFCMenu) {
147                menuChild = child;
148                break;
149            }
150        }
151
152        if (menuChild == null) {
153            // this popup menu cannot be identified
154            // TODO: exception, logging etc
155            return -1;
156        }
157
158        // find line that contains this menu item name
159        String itemName = ((JFCGUIElement) menuChild).getName().trim().toLowerCase();
160        int lineOfItem = -1;
161
162        for (int i = 0; i < menuList.size(); i++) {
163            String name = "\"" + menuList.get(i).trim().toLowerCase() + "\"";
164            if (name.equals(itemName)) {
165                lineOfItem = i;
166                break;
167            }
168        }
169
170        if (lineOfItem == -1) {
171            // failed to find this item in the menu file
172            // TODO: exception, logging etc
173            return -1;
174        }
175
176        // find menu item's parent line
177        String stripped = menuList.get(lineOfItem).replaceFirst("^ *", "");
178        int indent = menuList.get(lineOfItem).length() - stripped.length();
179
180        if (indent != 0) {
181            for (int i = lineOfItem; i >= 0; i--) {
182                stripped = menuList.get(i).replaceFirst("^ *", "");
183                int oldIndent = menuList.get(i).length() - stripped.length();
184
185                if (oldIndent < indent) {
186                    lineOfItem = i;
187                    break;
188                }
189            }
190        }
191
192        // get the item's indentation
193        stripped = menuList.get(lineOfItem).replaceFirst("^ *", "");
194        indent = menuList.get(lineOfItem).length() - stripped.length();
195
196        if (indent == 0) {
197            // top level menu item, just count in which position it is
198            for (int i = 0; i <= lineOfItem; i++) {
199                if (!menuList.get(i).startsWith(" ")) {
200                    index++;
201                }
202            }
203        }
204        else {
205            // find the topmenu index in the menu file by going backwards
206            for (int i = lineOfItem; i >= 0; i--) {
207                stripped = menuList.get(i).replaceFirst("^ *", "");
208                int oldIndent = menuList.get(i).length() - stripped.length();
209
210                if (oldIndent < indent) {
211                    index = lineOfItem - i;
212                    break;
213                }
214            }
215        }
216
217        return index;
218    }
219
220    private void generateJacaretoIndices(List<IGUIElement> elements, GUIModel targets) {
221        HashMap<String, Integer> typeCount = new HashMap<>();
222
223        for (IGUIElement child : elements) {
224            String type = child.getSpecification().getType();
225            Integer count = typeCount.get(type);
226
227            if (count == null) {
228                count = 0;
229                typeCount.put(type, count);
230            }
231            else {
232                typeCount.put(type, ++count);
233            }
234
235            if (menuList != null && type.equals("javax.swing.JPopupMenu")) {
236                // try to use a workaround for popup menu index problems
237                int index = findPopupMenuIndex((IGUIElement) child, targets);
238                if (index != -1) {
239                    ((JFCGUIElementSpec) child.getSpecification()).setAltIndex(index);
240                }
241                else {
242                    // workaround failed, use normal method as fallback
243                    ((JFCGUIElementSpec) child.getSpecification()).setAltIndex(count);
244                }
245            }
246            else {
247                ((JFCGUIElementSpec) child.getSpecification()).setAltIndex(count);
248            }
249
250            generateJacaretoIndices(targets.getChildren(child), targets);
251        }
252    }
253
254}
Note: See TracBrowser for help on using the repository browser.