[1722] | 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 | |
---|
| 15 | package de.ugoe.cs.autoquest.plugin.jfc.commands; |
---|
| 16 | |
---|
[1834] | 17 | import java.nio.charset.Charset; |
---|
| 18 | import java.nio.file.Files; |
---|
| 19 | import java.nio.file.Paths; |
---|
[1722] | 20 | import java.util.Collection; |
---|
| 21 | import java.util.HashMap; |
---|
| 22 | import java.util.List; |
---|
| 23 | |
---|
| 24 | import de.ugoe.cs.autoquest.CommandHelpers; |
---|
| 25 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
| 26 | import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModel; |
---|
| 27 | import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement; |
---|
| 28 | import de.ugoe.cs.autoquest.plugin.jfc.JFCSimplifiedLogParser; |
---|
[1834] | 29 | import de.ugoe.cs.autoquest.plugin.jfc.guimodel.JFCGUIElement; |
---|
[1722] | 30 | import de.ugoe.cs.autoquest.plugin.jfc.guimodel.JFCGUIElementSpec; |
---|
[1834] | 31 | import de.ugoe.cs.autoquest.plugin.jfc.guimodel.JFCMenu; |
---|
| 32 | import de.ugoe.cs.autoquest.plugin.jfc.guimodel.JFCMenuButton; |
---|
[1722] | 33 | import de.ugoe.cs.util.console.Command; |
---|
| 34 | import de.ugoe.cs.util.console.Console; |
---|
| 35 | import de.ugoe.cs.util.console.GlobalDataContainer; |
---|
| 36 | |
---|
| 37 | /** |
---|
| 38 | * <p> |
---|
| 39 | * Command to generate sessions in which the JFCGUIElements contain a special index that is required |
---|
| 40 | * to replay the sequences with Jacareto. |
---|
| 41 | * </p> |
---|
| 42 | * |
---|
| 43 | * @author Daniel May |
---|
| 44 | * @version 1.0 |
---|
| 45 | */ |
---|
| 46 | public class CMDparseJFCwithJacaretoIndices implements Command { |
---|
| 47 | |
---|
[1834] | 48 | private List<String> menuList; |
---|
| 49 | |
---|
[1722] | 50 | @Override |
---|
| 51 | public void run(List<Object> parameters) { |
---|
| 52 | String filename; |
---|
| 53 | String sequencesName = "sequences"; |
---|
| 54 | try { |
---|
| 55 | filename = (String) parameters.get(0); |
---|
| 56 | if (parameters.size() >= 2) { |
---|
[1834] | 57 | menuList = |
---|
| 58 | Files.readAllLines(Paths.get((String) parameters.get(1)), |
---|
| 59 | Charset.defaultCharset()); |
---|
[1722] | 60 | } |
---|
| 61 | } |
---|
| 62 | catch (Exception e) { |
---|
| 63 | throw new IllegalArgumentException(); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | JFCSimplifiedLogParser parser = new JFCSimplifiedLogParser(); |
---|
| 67 | |
---|
| 68 | try { |
---|
| 69 | parser.parseFile(filename); |
---|
| 70 | } |
---|
| 71 | catch (Exception e) { |
---|
| 72 | Console.printerrln("Could not parse " + filename + ": " + e.getMessage()); |
---|
| 73 | return; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | Collection<List<Event>> sequences = parser.getSequences(); |
---|
| 77 | GUIModel targets = parser.getGuiModel(); |
---|
[1902] | 78 | targets.condenseModel(); |
---|
[1722] | 79 | |
---|
| 80 | generateJacaretoIndices(targets.getRootElements(), targets); |
---|
| 81 | |
---|
| 82 | if (GlobalDataContainer.getInstance().addData(sequencesName, sequences)) { |
---|
| 83 | CommandHelpers.dataOverwritten(sequencesName); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | if (GlobalDataContainer.getInstance().addData(sequencesName + "_targets", targets)) { |
---|
| 87 | CommandHelpers.dataOverwritten(sequencesName + "_targets"); |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | @Override |
---|
| 92 | public String help() { |
---|
[1834] | 93 | return "parseJFCwithJacaretoIndices <filename> {<menufile>}"; |
---|
[1722] | 94 | } |
---|
| 95 | |
---|
[1834] | 96 | private int findPopupMenuIndex(IGUIElement item, GUIModel model) { |
---|
| 97 | // TODO: refactor |
---|
| 98 | int index = -1; |
---|
| 99 | List<IGUIElement> children = model.getChildren(item); |
---|
| 100 | IGUIElement menuChild = null; |
---|
| 101 | |
---|
| 102 | // try to identify this popup menu by looking at its children |
---|
| 103 | // find the first menu item child of this item |
---|
| 104 | for (IGUIElement child : children) { |
---|
| 105 | if (child instanceof JFCMenuButton || child instanceof JFCMenu) { |
---|
| 106 | menuChild = child; |
---|
| 107 | break; |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | if (menuChild == null) { |
---|
| 112 | // this popup menu cannot be identified |
---|
| 113 | // TODO: exception, logging etc |
---|
| 114 | return -1; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | // find line that contains this menu item name |
---|
| 118 | String itemName = ((JFCGUIElement) menuChild).getName().trim().toLowerCase(); |
---|
| 119 | int lineOfItem = -1; |
---|
| 120 | |
---|
| 121 | for (int i = 0; i < menuList.size(); i++) { |
---|
| 122 | String name = "\"" + menuList.get(i).trim().toLowerCase() + "\""; |
---|
| 123 | if (name.equals(itemName)) { |
---|
| 124 | lineOfItem = i; |
---|
| 125 | break; |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | if (lineOfItem == -1) { |
---|
| 130 | // failed to find this item in the menu file |
---|
| 131 | // TODO: exception, logging etc |
---|
| 132 | return -1; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | // find menu item's parent line |
---|
| 136 | String stripped = menuList.get(lineOfItem).replaceFirst("^ *", ""); |
---|
| 137 | int indent = menuList.get(lineOfItem).length() - stripped.length(); |
---|
| 138 | |
---|
| 139 | if (indent != 0) { |
---|
| 140 | for (int i = lineOfItem; i >= 0; i--) { |
---|
| 141 | stripped = menuList.get(i).replaceFirst("^ *", ""); |
---|
| 142 | int oldIndent = menuList.get(i).length() - stripped.length(); |
---|
| 143 | |
---|
| 144 | if (oldIndent < indent) { |
---|
| 145 | lineOfItem = i; |
---|
| 146 | break; |
---|
| 147 | } |
---|
| 148 | } |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | // get the item's indentation |
---|
| 152 | stripped = menuList.get(lineOfItem).replaceFirst("^ *", ""); |
---|
| 153 | indent = menuList.get(lineOfItem).length() - stripped.length(); |
---|
| 154 | |
---|
| 155 | if (indent == 0) { |
---|
| 156 | // top level menu item, just count in which position it is |
---|
| 157 | for (int i = 0; i <= lineOfItem; i++) { |
---|
| 158 | if (!menuList.get(i).startsWith(" ")) { |
---|
| 159 | index++; |
---|
| 160 | } |
---|
| 161 | } |
---|
| 162 | } |
---|
| 163 | else { |
---|
| 164 | // find the topmenu index in the menu file by going backwards |
---|
| 165 | for (int i = lineOfItem; i >= 0; i--) { |
---|
| 166 | stripped = menuList.get(i).replaceFirst("^ *", ""); |
---|
| 167 | int oldIndent = menuList.get(i).length() - stripped.length(); |
---|
| 168 | |
---|
| 169 | if (oldIndent < indent) { |
---|
| 170 | index = lineOfItem - i; |
---|
| 171 | break; |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | return index; |
---|
| 177 | } |
---|
| 178 | |
---|
[1722] | 179 | private void generateJacaretoIndices(List<IGUIElement> elements, GUIModel targets) { |
---|
| 180 | HashMap<String, Integer> typeCount = new HashMap<>(); |
---|
| 181 | |
---|
| 182 | for (IGUIElement child : elements) { |
---|
| 183 | String type = child.getSpecification().getType(); |
---|
| 184 | Integer count = typeCount.get(type); |
---|
| 185 | |
---|
| 186 | if (count == null) { |
---|
| 187 | count = 0; |
---|
| 188 | typeCount.put(type, count); |
---|
| 189 | } |
---|
| 190 | else { |
---|
| 191 | typeCount.put(type, ++count); |
---|
| 192 | } |
---|
| 193 | |
---|
[1834] | 194 | if (menuList != null && type.equals("javax.swing.JPopupMenu")) { |
---|
| 195 | // try to use a workaround for popup menu index problems |
---|
| 196 | int index = findPopupMenuIndex((IGUIElement) child, targets); |
---|
| 197 | if (index != -1) { |
---|
| 198 | ((JFCGUIElementSpec) child.getSpecification()).setAltIndex(index); |
---|
| 199 | } |
---|
| 200 | else { |
---|
| 201 | // workaround failed, use normal method as fallback |
---|
| 202 | ((JFCGUIElementSpec) child.getSpecification()).setAltIndex(count); |
---|
| 203 | } |
---|
| 204 | } |
---|
| 205 | else { |
---|
| 206 | ((JFCGUIElementSpec) child.getSpecification()).setAltIndex(count); |
---|
| 207 | } |
---|
[1722] | 208 | |
---|
| 209 | generateJacaretoIndices(targets.getChildren(child), targets); |
---|
| 210 | } |
---|
| 211 | } |
---|
| 212 | } |
---|