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

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

refactorings

  • Property svn:mime-type set to text/plain
File size: 9.1 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        System.out.println("parseJFCDirwithJacaretoIndices <path> {<sequencesName>} {<menuFile>}");
133        return null;
134    }
135
136    private int findPopupMenuIndex(IGUIElement item, GUIModel model) {
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            Console.traceln(Level.WARNING, "Index for popup menu item could not be found.");
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            Console.traceln(Level.WARNING, "Menu file seems to be missing an item.");
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.