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