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

Last change on this file since 2282 was 2282, checked in by pharms, 5 years ago
  • Property svn:mime-type set to text/plain
File size: 9.0 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.Iterator;
23import java.util.List;
24import java.util.logging.Level;
25
26import de.ugoe.cs.autoquest.CommandHelpers;
27import de.ugoe.cs.autoquest.SequenceInstanceOf;
28import de.ugoe.cs.autoquest.eventcore.Event;
29import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModel;
30import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement;
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 * Command to add an alternative index to JFCGuiElementSpecs. This index counts child type
42 * separately.
43 * </p>
44 *
45 * @author Daniel May
46 */
47public class CMDgenerateAltIndex implements Command {
48
49    private List<String> menuList;
50
51    /*
52     * (non-Javadoc)
53     *
54     * @see de.ugoe.cs.util.console.Command#run(java.util.List)
55     */
56    @SuppressWarnings("unchecked")
57    @Override
58    public void run(List<Object> parameters) {
59        String sequencesName = "sequences";
60
61        try {
62            sequencesName = (String) parameters.get(0);
63            if (parameters.size() >= 2) {
64                menuList = Files.readAllLines(Paths.get((String) parameters.get(1)),
65                                              Charset.defaultCharset());
66            }
67        }
68        catch (Exception e) {
69            throw new IllegalArgumentException();
70        }
71
72        Collection<List<Event>> sequences = null;
73        Object dataObject = GlobalDataContainer.getInstance().getData(sequencesName);
74        if (dataObject == null) {
75            CommandHelpers.objectNotFoundMessage(sequencesName);
76            return;
77        }
78        if (!SequenceInstanceOf.isCollectionOfSequences(dataObject)) {
79            CommandHelpers.objectNotType(sequencesName, "Collection<List<Event<?>>>");
80            return;
81        }
82        sequences = (Collection<List<Event>>) dataObject;
83        GUIModel model = null;
84
85        for (List<Event> sequence : sequences) {
86            if (model == null) {
87                // generate jacareto indices for the GUI model of this sequence
88                JFCGUIElement element = (JFCGUIElement) sequence.get(0).getTarget();
89                model = element.getGUIModel();
90                generateJacaretoIndices(model.getRootElements(), model);
91            }
92            else {
93                for (Iterator<Event> eventIter = sequence.iterator(); eventIter.hasNext();) {
94                    // find and set the alt index for every target
95                    IGUIElement target = (IGUIElement) eventIter.next().getTarget();
96                    findAltIndex(target, model.getRootElements(), model);
97                }
98            }
99        }
100    }
101
102    private void findAltIndex(IGUIElement target, List<IGUIElement> elements, GUIModel model) {
103        int altIndex = ((JFCGUIElementSpec) target.getSpecification()).getAltIndex();
104
105        if (altIndex < 0) {
106            for (IGUIElement child : elements) {
107                if (target.equals(child)) {
108                    int childAltIndex =
109                        ((JFCGUIElementSpec) child.getSpecification()).getAltIndex();
110
111                    if (childAltIndex > -1) {
112                        ((JFCGUIElementSpec) target.getSpecification()).setAltIndex(childAltIndex);
113                    }
114                    else {
115                        findAltIndex(target, model.getChildren(child), model);
116                    }
117                }
118                else {
119                    findAltIndex(target, model.getChildren(child), model);
120                }
121            }
122        }
123    }
124
125    /*
126     * (non-Javadoc)
127     *
128     * @see de.ugoe.cs.util.console.Command#help()
129     */
130    @Override
131    public String help() {
132        return "generateAltIndex";
133    }
134
135    private int findPopupMenuIndex(IGUIElement item, GUIModel model) {
136        int index = -1;
137        List<IGUIElement> children = model.getChildren(item);
138        IGUIElement menuChild = null;
139
140        // try to identify this popup menu by looking at its children
141        // find the first menu item child of this item
142        for (IGUIElement child : children) {
143            if (child instanceof JFCMenuButton || child instanceof JFCMenu) {
144                menuChild = child;
145                break;
146            }
147        }
148
149        if (menuChild == null) {
150            // this popup menu cannot be identified
151            Console.traceln(Level.WARNING, "Index for popup menu item could not be found.");
152            return -1;
153        }
154
155        // find line that contains this menu item name
156        String itemName = ((JFCGUIElement) menuChild).getName().trim().toLowerCase();
157        int lineOfItem = -1;
158
159        for (int i = 0; i < menuList.size(); i++) {
160            String name = "\"" + menuList.get(i).trim().toLowerCase() + "\"";
161            if (name.equals(itemName)) {
162                lineOfItem = i;
163                break;
164            }
165        }
166
167        if (lineOfItem == -1) {
168            // failed to find this item in the menu file
169            Console.traceln(Level.WARNING, "Menu file seems to be missing an item.");
170            return -1;
171        }
172
173        // find menu item's parent line
174        String stripped = menuList.get(lineOfItem).replaceFirst("^ *", "");
175        int indent = menuList.get(lineOfItem).length() - stripped.length();
176
177        if (indent != 0) {
178            for (int i = lineOfItem; i >= 0; i--) {
179                stripped = menuList.get(i).replaceFirst("^ *", "");
180                int oldIndent = menuList.get(i).length() - stripped.length();
181
182                if (oldIndent < indent) {
183                    lineOfItem = i;
184                    break;
185                }
186            }
187        }
188
189        // get the item's indentation
190        stripped = menuList.get(lineOfItem).replaceFirst("^ *", "");
191        indent = menuList.get(lineOfItem).length() - stripped.length();
192
193        if (indent == 0) {
194            // top level menu item, just count in which position it is
195            for (int i = 0; i <= lineOfItem; i++) {
196                if (!menuList.get(i).startsWith(" ")) {
197                    index++;
198                }
199            }
200        }
201        else {
202            // find the topmenu index in the menu file by going backwards
203            for (int i = lineOfItem; i >= 0; i--) {
204                stripped = menuList.get(i).replaceFirst("^ *", "");
205                int oldIndent = menuList.get(i).length() - stripped.length();
206
207                if (oldIndent < indent) {
208                    index = lineOfItem - i;
209                    break;
210                }
211            }
212        }
213
214        return index;
215    }
216
217    private void generateJacaretoIndices(List<IGUIElement> elements, GUIModel targets) {
218        HashMap<String, Integer> typeCount = new HashMap<>();
219
220        for (IGUIElement child : elements) {
221            String type = child.getSpecification().getType();
222            Integer count = typeCount.get(type);
223
224            if (count == null) {
225                count = 0;
226                typeCount.put(type, count);
227            }
228            else {
229                typeCount.put(type, ++count);
230            }
231
232            if (menuList != null && type.equals("javax.swing.JPopupMenu")) {
233                // try to use a workaround for popup menu index problems
234                int index = findPopupMenuIndex((IGUIElement) child, targets);
235                if (index != -1) {
236                    ((JFCGUIElementSpec) child.getSpecification()).setAltIndex(index);
237                }
238                else {
239                    // workaround failed, use normal method as fallback
240                    ((JFCGUIElementSpec) child.getSpecification()).setAltIndex(count);
241                }
242            }
243            else {
244                ((JFCGUIElementSpec) child.getSpecification()).setAltIndex(count);
245            }
246
247            generateJacaretoIndices(targets.getChildren(child), targets);
248        }
249    }
250
251}
Note: See TracBrowser for help on using the repository browser.