Changeset 1834


Ignore:
Timestamp:
11/24/14 01:51:10 (9 years ago)
Author:
dmay
Message:

Work of the last week: Completely redid most of the menu handling, since there were too many cornercases that did not work. Turns out, that in these cases, wrong indices were generated. Fixed.

Location:
trunk/autoquest-plugin-jfc/src/main/java/de/ugoe/cs/autoquest/plugin/jfc/commands
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/autoquest-plugin-jfc/src/main/java/de/ugoe/cs/autoquest/plugin/jfc/commands/CMDgenerateJacaretoReplay.java

    r1825 r1834  
    302302                    // if a menu file was provided, use the improved event 
    303303                    // generation 
     304                    // TODO: check how much of this is still necessary with 
     305                    // the new menu index system 
    304306                    if (menuList != null) { 
    305307                        if (event.getTarget() instanceof JFCMenuButton) { 
     
    423425            if (indent < oldIndent) { 
    424426                // this is a parent submenu 
    425                 elements.add(menuElements.get(stripped)); 
     427                elements.push(menuElements.get(stripped)); 
    426428                oldIndent = indent; 
    427429            } 
  • trunk/autoquest-plugin-jfc/src/main/java/de/ugoe/cs/autoquest/plugin/jfc/commands/CMDparseJFCwithJacaretoIndices.java

    r1722 r1834  
    1515package de.ugoe.cs.autoquest.plugin.jfc.commands; 
    1616 
     17import java.nio.charset.Charset; 
     18import java.nio.file.Files; 
     19import java.nio.file.Paths; 
    1720import java.util.Collection; 
    1821import java.util.HashMap; 
     
    2427import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement; 
    2528import de.ugoe.cs.autoquest.plugin.jfc.JFCSimplifiedLogParser; 
     29import de.ugoe.cs.autoquest.plugin.jfc.guimodel.JFCGUIElement; 
    2630import de.ugoe.cs.autoquest.plugin.jfc.guimodel.JFCGUIElementSpec; 
     31import de.ugoe.cs.autoquest.plugin.jfc.guimodel.JFCMenu; 
     32import de.ugoe.cs.autoquest.plugin.jfc.guimodel.JFCMenuButton; 
    2733import de.ugoe.cs.util.console.Command; 
    2834import de.ugoe.cs.util.console.Console; 
     
    4046public class CMDparseJFCwithJacaretoIndices implements Command { 
    4147 
     48    private List<String> menuList; 
     49 
    4250    @Override 
    4351    public void run(List<Object> parameters) { 
     
    4755            filename = (String) parameters.get(0); 
    4856            if (parameters.size() >= 2) { 
    49                 sequencesName = (String) parameters.get(1); 
     57                menuList = 
     58                    Files.readAllLines(Paths.get((String) parameters.get(1)), 
     59                                       Charset.defaultCharset()); 
    5060            } 
    5161        } 
     
    8090    @Override 
    8191    public String help() { 
    82         return "parseJFCwithJacaretoIndices <filename> {<sequences>}"; 
     92        return "parseJFCwithJacaretoIndices <filename> {<menufile>}"; 
     93    } 
     94 
     95    private int findPopupMenuIndex(IGUIElement item, GUIModel model) { 
     96        // TODO: refactor 
     97        int index = -1; 
     98        List<IGUIElement> children = model.getChildren(item); 
     99        IGUIElement menuChild = null; 
     100 
     101        // try to identify this popup menu by looking at its children 
     102        // find the first menu item child of this item 
     103        for (IGUIElement child : children) { 
     104            if (child instanceof JFCMenuButton || child instanceof JFCMenu) { 
     105                menuChild = child; 
     106                break; 
     107            } 
     108        } 
     109 
     110        if (menuChild == null) { 
     111            // this popup menu cannot be identified 
     112            // TODO: exception, logging etc 
     113            return -1; 
     114        } 
     115 
     116        // find line that contains this menu item name 
     117        String itemName = ((JFCGUIElement) menuChild).getName().trim().toLowerCase(); 
     118        int lineOfItem = -1; 
     119 
     120        for (int i = 0; i < menuList.size(); i++) { 
     121            String name = "\"" + menuList.get(i).trim().toLowerCase() + "\""; 
     122            if (name.equals(itemName)) { 
     123                lineOfItem = i; 
     124                break; 
     125            } 
     126        } 
     127 
     128        if (lineOfItem == -1) { 
     129            // failed to find this item in the menu file 
     130            // TODO: exception, logging etc 
     131            return -1; 
     132        } 
     133 
     134        // find menu item's parent line 
     135        String stripped = menuList.get(lineOfItem).replaceFirst("^ *", ""); 
     136        int indent = menuList.get(lineOfItem).length() - stripped.length(); 
     137 
     138        if (indent != 0) { 
     139            for (int i = lineOfItem; i >= 0; i--) { 
     140                stripped = menuList.get(i).replaceFirst("^ *", ""); 
     141                int oldIndent = menuList.get(i).length() - stripped.length(); 
     142 
     143                if (oldIndent < indent) { 
     144                    lineOfItem = i; 
     145                    break; 
     146                } 
     147            } 
     148        } 
     149 
     150        // get the item's indentation 
     151        stripped = menuList.get(lineOfItem).replaceFirst("^ *", ""); 
     152        indent = menuList.get(lineOfItem).length() - stripped.length(); 
     153 
     154        if (indent == 0) { 
     155            // top level menu item, just count in which position it is 
     156            for (int i = 0; i <= lineOfItem; i++) { 
     157                if (!menuList.get(i).startsWith(" ")) { 
     158                    index++; 
     159                } 
     160            } 
     161        } 
     162        else { 
     163            // find the topmenu index in the menu file by going backwards 
     164            for (int i = lineOfItem; i >= 0; i--) { 
     165                stripped = menuList.get(i).replaceFirst("^ *", ""); 
     166                int oldIndent = menuList.get(i).length() - stripped.length(); 
     167 
     168                if (oldIndent < indent) { 
     169                    index = lineOfItem - i; 
     170                    break; 
     171                } 
     172            } 
     173        } 
     174 
     175        return index; 
    83176    } 
    84177 
     
    98191            } 
    99192 
    100             ((JFCGUIElementSpec) child.getSpecification()).setAltIndex(count); 
     193            if (menuList != null && type.equals("javax.swing.JPopupMenu")) { 
     194                // try to use a workaround for popup menu index problems 
     195                int index = findPopupMenuIndex((IGUIElement) child, targets); 
     196                if (index != -1) { 
     197                    ((JFCGUIElementSpec) child.getSpecification()).setAltIndex(index); 
     198                } 
     199                else { 
     200                    // workaround failed, use normal method as fallback 
     201                    ((JFCGUIElementSpec) child.getSpecification()).setAltIndex(count); 
     202                } 
     203            } 
     204            else { 
     205                ((JFCGUIElementSpec) child.getSpecification()).setAltIndex(count); 
     206            } 
    101207 
    102208            generateJacaretoIndices(targets.getChildren(child), targets); 
Note: See TracChangeset for help on using the changeset viewer.