Changeset 32700

Show
Ignore:
Timestamp:
16.12.2018 22:49:11 (6 months ago)
Author:
ak19
Message:

1. I'm hoping that setNamesRecursively now properly deals with cyclical references to GUI components (especially GLI's custom GUI components) by skipping those whose names are already set. The code should have set the entire subbranch (all descendants) of an already named GUI component, so skipping should be fine. 2. printComponentNames() sadly wasn't printing all components: submenu items of a menubar were missed out. This seems to have to do with how getComponents() is implemented on a MenuBar?? Rather than calling the getSubElements() in the case of MenuBars? and Menu(Item)s, I'm now just printing Component names out from within setNamesRecursively() instead as soon as a name has been set (or is known to have been set).

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • main/trunk/gli/src/org/greenstone/gatherer/gui/TestingPreparation.java

    r32695 r32700  
    3434    root.setName(className); 
    3535 
    36     setNamesRecursively("", root); 
    37     if (DEBUGGING_TEST_MODE) printComponentNames(root, ""); 
     36    setNamesRecursively("", root, "", true); 
     37    //if (DEBUGGING_TEST_MODE) printComponentNames(root, ""); 
    3838    } 
    3939     
    40     public static void setNamesRecursively(String prefix, Component root) { 
    41     if(!TEST_MODE) return; 
    42      
     40    private static void setNamesRecursively(String prefix, Component root, String tabSpace, boolean isTrueRoot) { 
     41    if(!TEST_MODE) return;   
     42         
    4343    // root starts off as GLI's JFrame, so we know that all its GUI children are specifically JComponents 
    4444     
     
    5050        if(root.getName() == null || root.getName().equals("")) { // if name not already set 
    5151        if(!prefix.equals("")){ 
    52             // TODO: fix this 
    53             root.setName(prefix + "." + className); 
     52            // TODO: set this to something more meaningful? 
     53            root.setName(prefix + "." + className);          
    5454        } 
    5555        else { 
     
    5757        } 
    5858        } // else swing Component name already set 
    59          
     59 
     60        // now we can print out this element's name for debugging 
     61        if (DEBUGGING_TEST_MODE) System.err.println(tabSpace + root.getName()); 
    6062    } else { // root is in GLI GUI package, use its classname for name 
    6163         
    6264        if(root.getName() == null || root.getName().equals("")) { 
    6365        root.setName(className); 
     66        } else if(!isTrueRoot) {         
     67        // then the name of this GLI GUI element (i.e. of GLI GUI package) was already set 
     68        // we've recursively dealt with this GLI element and its children already 
     69        // Prevents cyclic widget references being processed in a cyclic loop ending 
     70        // in a StackOverflowError 
     71        return; 
    6472        } 
     73        // now we can print out this element's name for debugging 
     74        if (DEBUGGING_TEST_MODE) System.err.println(tabSpace + root.getName()); 
     75         
    6576        prefix = className; 
    6677         
    6778        // check member vars 
    68         Field[] memberVars = root.getClass().getDeclaredFields(); // private to public member vars, but not inherited ones       
     79        Field[] memberVars = root.getClass().getDeclaredFields(); // private to public member vars, but not inherited ones 
     80         
    6981        for(int i = 0; i < memberVars.length; i++) { 
    7082        memberVars[i].setAccessible(true); // make private/protected etc fields accessible 
     
    7385        Class jCompClass = Container.class; 
    7486        Class memberVarClass = memberVars[i].getType(); 
    75  
    76         // memberVarClass is a JComponen (subclass) 
     87         
     88        // memberVarClass is a JComponent (JComponent or subclass) 
    7789        if(jCompClass.isAssignableFrom(memberVarClass)) { 
    7890             
     
    90102                if(!memberVarClass.getPackage().getName().contains("org.greenstone.gatherer.gui")) { 
    91103                String memberprefix = prefix + "." + memberVars[i].getName(); // append member var name 
     104                 
    92105                // now can call setName() on the actual member variable 
    93106                memberComponent.setName(memberprefix); 
    94                 setNamesRecursively(memberprefix, memberComponent); 
     107                setNamesRecursively(memberprefix, memberComponent, tabSpace+"  ", false); 
    95108                } 
    96109 
    97110                //else  the member variable is a GLI GUI class, use its className as prefix for child components 
    98111                // Skip this step to avoid circular references to our own member vars 
    99                 /*else { // member variable is a GLI GUI class, use its className as prefix for child components 
    100                 String memberprefix = memberVarClass.getSimpleName(); 
    101                 memberComponent.setName(memberprefix); 
    102                 setNamesRecursively(memberprefix, memberComponent); 
    103                 }*/ 
    104              
     112                 
     113                else { // member variable is a GLI GUI class. 
     114                // will be using its className as prefix for child components 
     115                 
     116                //String memberprefix = memberVarClass.getSimpleName(); 
     117                //memberComponent.setName(memberprefix); 
     118                setNamesRecursively("", memberComponent, tabSpace+"  ", false); 
     119                } 
     120                             
    105121                 
    106122            } 
     
    117133    // No worries about AWT tree lock: setNamesRecursively is called by openGUI 
    118134    // which is specifically called by GathererProg on the Event Dispatch Thread (EDT) 
     135     
     136         
    119137    Component[] children = ((Container)root).getComponents(); 
     138     
    120139    for(int i = 0; i < children.length; i++) { 
    121140        // if we haven't already set a name for any child JComponents with the above, 
    122141        // then the following will do so now 
    123         setNamesRecursively(className, children[i]); 
    124     } 
    125      
     142        setNamesRecursively(className, children[i], tabSpace+"  ", false); 
     143    }     
     144 
    126145    } 
    127146 
    128147     
    129     // For debugging swing Component names assigned with Container's setName()     
     148    // For debugging swing Component names assigned with Container's setName() 
     149    // Wasted a lot of my time debugging why menu bar items' names weren't set by setNamesRecursively(). 
     150    // Turns out they were being set, but that sadly 
     151    // this method doesn't seem to print all components, e.g. JMenuItems of JMenus of a JMenuBar 
     152    // Perhaps because those are available only through getSubElements() rather than 
     153    // through getComponents()? 
     154    // So have now shifted the printing of elements that we manually set into setNamesRecursively() above. 
     155    // I'd have preferred this printing method since it prints out the component names in 
     156    // hierarchical order rather than the sometimes arbitrary order that GUI member vars are 
     157    // declared in GLI's own GUI classes. 
    130158    public static void printComponentNames(Component root, String tabbing) { 
    131159    if(!TEST_MODE) return;