source: main/trunk/gli/src/org/greenstone/gatherer/gui/TestingPreparation.java@ 32689

Last change on this file since 32689 was 32689, checked in by ak19, 5 years ago

If not testing, don't need GLI's GUI components to be assigned names. Leaves GLI more efficient when run as GLI proper, while GLI will do a bit more work when running GLI for testing.

File size: 4.9 KB
Line 
1package org.greenstone.gatherer.gui;
2
3import java.awt.*;
4import javax.swing.*;
5import java.lang.reflect.*;
6
7public class TestingPreparation {
8 public static boolean TEST_MODE = false;
9
10
11 /*
12 For GUI swing testing of GLI, it's very handy for components to have names
13 I use the GLI classname as name or if it's not a GLI class, then:
14 - if a member var of a GLI class, then GLIclassName.memberVarName
15 - else GLIClassName.swingComponentName
16 Google: "Java swing automatically setName"
17 -> https://stackoverflow.com/questions/3628218/strategy-for-naming-swing-components
18 https://stackoverflow.com/questions/1782598/with-java-reflection-how-to-instantiate-a-new-object-then-call-a-method-on-it
19 - https://stackoverflow.com/questions/4163500/how-to-automatic-set-name-attr-of-gui-components/4164479#4164479
20 - https://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.wb.swing.doc.user%2Fhtml%2Fwizards%2Fswing%2Fautomatic_databinding.html
21 - https://stackoverflow.com/questions/28639294/in-java-how-to-get-name-of-component-which-is-clicked
22 */
23 public static void setNamesRecursively(String prefix, Component root) {
24 if(!TEST_MODE) return;
25
26 // root starts off as GLI's JFrame, so we know that all its GUI children are specifically JComponents
27
28 // https://docs.oracle.com/javase/7/docs/api/index.html?java/lang/reflect/package-summary.html
29 String className = root.getClass().getSimpleName();
30
31 String packageName = root.getClass().getPackage().getName();
32 if(!packageName.contains("org.greenstone.gatherer.gui")) {
33 if(root.getName() == null || root.getName().equals("")) { // if name not already set
34 if(!prefix.equals("")){
35 // TODO: fix this
36 root.setName(prefix + "." + className);
37 }
38 else {
39 root.setName(className);
40 }
41 } // else swing Component name already set
42
43 } else { // root is in GLI GUI package, use its classname for name
44
45 if(root.getName() == null || root.getName().equals("")) {
46 root.setName(className);
47 }
48 prefix = className;
49
50 // check member vars
51 Field[] memberVars = root.getClass().getDeclaredFields(); // private to public member vars, but not inherited ones
52 for(int i = 0; i < memberVars.length; i++) {
53 memberVars[i].setAccessible(true); // make private/protected etc fields accessible
54
55 // https://www.tutorialspoint.com/java/lang/class_isinstance.htm
56 Class jCompClass = Container.class;
57 Class memberVarClass = memberVars[i].getType();
58
59 // memberVarClass is a JComponen (subclass)
60 if(jCompClass.isAssignableFrom(memberVarClass)) {
61
62 // get the actual member variable denoted by memberVars[i]
63 // on the current JComponent instance 'root'.
64 // We now know this member var to be a JComponent
65 // Having the actual member variable of the instantiated instance,
66 // we can call setName on it
67 try {
68 Container memberComponent = (Container)memberVars[i].get(root);
69
70 if(memberComponent != null) {
71
72 // member var is a JComponent but not of GLI package, so locally instantiated
73 if(!memberVarClass.getPackage().getName().contains("org.greenstone.gatherer.gui")) {
74 String memberprefix = prefix + "." + memberVars[i].getName(); // append member var name
75 // now can call setName() on the actual member variable
76 memberComponent.setName(memberprefix);
77 setNamesRecursively(memberprefix, memberComponent);
78 }
79
80 //else the member variable is a GLI GUI class, use its className as prefix for child components
81 // Skip this step to avoid circular references to our own member vars
82 /*else { // member variable is a GLI GUI class, use its className as prefix for child components
83 String memberprefix = memberVarClass.getSimpleName();
84 memberComponent.setName(memberprefix);
85 setNamesRecursively(memberprefix, memberComponent);
86 }*/
87
88
89 }
90 } catch(Exception e) {
91 e.printStackTrace();
92 }
93 }
94 // else not a JComponent, skip
95 }
96 }
97
98
99 // https://stackoverflow.com/questions/33927349/could-someone-please-explain-note-this-method-should-be-called-under-awt-tree
100 // No worries about AWT tree lock: setNamesRecursively is called by openGUI
101 // which is specifically called by GathererProg on the Event Dispatch Thread (EDT)
102 Component[] children = ((Container)root).getComponents();
103 for(int i = 0; i < children.length; i++) {
104 // if we haven't already set a name for any child JComponents with the above,
105 // then the following will do so now
106 setNamesRecursively(className, children[i]);
107 }
108
109 }
110
111
112 // For debugging swing Component names assigned with Container's setName()
113 public static void printComponentNames(Component root, String tabbing) {
114 if(!TEST_MODE) return;
115
116 System.err.println(tabbing + root.getName());
117 Component[] children = ((Container)root).getComponents();
118 // recursive call
119 for(int i = 0; i < children.length; i++) {
120 printComponentNames(children[i], tabbing + " ");
121 }
122 }
123}
Note: See TracBrowser for help on using the repository browser.