- Timestamp:
- 2018-12-16T19:57:11+13:00 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/gli/src/org/greenstone/gatherer/gui/GUIManager.java
r32686 r32689 144 144 } 145 145 146 /*147 For GUI swing testing of GLI, it's very handy for components to have names148 I use the GLI classname as name or if it's not a GLI class, then:149 - if a member var of a GLI class, then GLIclassName.memberVarName150 - else GLIClassName.swingComponentName151 Google: "Java swing automatically setName"152 -> https://stackoverflow.com/questions/3628218/strategy-for-naming-swing-components153 https://stackoverflow.com/questions/1782598/with-java-reflection-how-to-instantiate-a-new-object-then-call-a-method-on-it154 - https://stackoverflow.com/questions/4163500/how-to-automatic-set-name-attr-of-gui-components/4164479#4164479155 - https://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.wb.swing.doc.user%2Fhtml%2Fwizards%2Fswing%2Fautomatic_databinding.html156 - https://stackoverflow.com/questions/28639294/in-java-how-to-get-name-of-component-which-is-clicked157 */158 public static void setNamesRecursively(String prefix, Component root) {159 160 // root starts off as GLI's JFrame, so we know that all its GUI children are specifically JComponents161 162 // https://docs.oracle.com/javase/7/docs/api/index.html?java/lang/reflect/package-summary.html163 String className = root.getClass().getSimpleName();164 165 String packageName = root.getClass().getPackage().getName();166 if(!packageName.contains("org.greenstone.gatherer.gui")) {167 if(root.getName() == null || root.getName().equals("")) { // if name not already set168 if(!prefix.equals("")){169 // TODO: fix this170 root.setName(prefix + "." + className);171 }172 else {173 root.setName(className);174 }175 } // else swing Component name already set176 177 } else { // root is in GLI GUI package, use its classname for name178 179 if(root.getName() == null || root.getName().equals("")) {180 root.setName(className);181 }182 prefix = className;183 184 // check member vars185 Field[] memberVars = root.getClass().getDeclaredFields(); // private to public member vars, but not inherited ones186 for(int i = 0; i < memberVars.length; i++) {187 memberVars[i].setAccessible(true); // make private/protected etc fields accessible188 189 // https://www.tutorialspoint.com/java/lang/class_isinstance.htm190 Class jCompClass = Container.class;191 Class memberVarClass = memberVars[i].getType();192 193 // memberVarClass is a JComponen (subclass)194 if(jCompClass.isAssignableFrom(memberVarClass)) {195 196 // get the actual member variable denoted by memberVars[i]197 // on the current JComponent instance 'root'.198 // We now know this member var to be a JComponent199 // Having the actual member variable of the instantiated instance,200 // we can call setName on it201 try {202 Container memberComponent = (Container)memberVars[i].get(root);203 204 if(memberComponent != null) {205 206 // member var is a JComponent but not of GLI package, so locally instantiated207 if(!memberVarClass.getPackage().getName().contains("org.greenstone.gatherer.gui")) {208 String memberprefix = prefix + "." + memberVars[i].getName(); // append member var name209 // now can call setName() on the actual member variable210 memberComponent.setName(memberprefix);211 setNamesRecursively(memberprefix, memberComponent);212 }213 214 //else the member variable is a GLI GUI class, use its className as prefix for child components215 // Skip this step to avoid circular references to our own member vars216 /*else { // member variable is a GLI GUI class, use its className as prefix for child components217 String memberprefix = memberVarClass.getSimpleName();218 memberComponent.setName(memberprefix);219 setNamesRecursively(memberprefix, memberComponent);220 }*/221 222 223 }224 } catch(Exception e) {225 e.printStackTrace();226 }227 }228 // else not a JComponent, skip229 }230 }231 232 233 // https://stackoverflow.com/questions/33927349/could-someone-please-explain-note-this-method-should-be-called-under-awt-tree234 // No worries about AWT tree lock: setNamesRecursively is called by openGUI235 // which is specifically called by GathererProg on the Event Dispatch Thread (EDT)236 Component[] children = ((Container)root).getComponents();237 for(int i = 0; i < children.length; i++) {238 // if we haven't already set a name for any child JComponents with the above,239 // then the following will do so now240 setNamesRecursively(className, children[i]);241 }242 243 }244 245 public static void printComponentNames(Component root, String tabbing) {246 System.err.println(tabbing + root.getName());247 Component[] children = ((Container)root).getComponents();248 // recursive call249 for(int i = 0; i < children.length; i++) {250 printComponentNames(children[i], tabbing + " ");251 }252 }253 146 254 147 public void windowGainedFocus(WindowEvent e)
Note:
See TracChangeset
for help on using the changeset viewer.