1 | package org.greenstone.gatherer.gui;
|
---|
2 |
|
---|
3 | import java.awt.*;
|
---|
4 | import javax.swing.*;
|
---|
5 | import java.lang.reflect.*;
|
---|
6 |
|
---|
7 | public class TestingPreparation {
|
---|
8 | public static boolean TEST_MODE = true; // TODO: set to true for debugging. Reset to false when committing to svn
|
---|
9 | public static boolean DEBUGGING_TEST_MODE = true;
|
---|
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(Component root) {
|
---|
24 | if(!TEST_MODE) return;
|
---|
25 |
|
---|
26 | // setNamesRecursively won't assign a name if one is already set
|
---|
27 | // But GUIManager for instance g_man has a name "frame0" assigned automatically
|
---|
28 | // Setting explicitly since GUIManager is preferred for its name
|
---|
29 |
|
---|
30 | // Workaround to force replacement of non-informative names like frame0 for GUIManager
|
---|
31 | // and dialog0 for OpenCollectioNDialog
|
---|
32 | // Workaround is to not check if name already set for root, but to set it ourselves
|
---|
33 | String className = root.getClass().getSimpleName();
|
---|
34 | root.setName(className);
|
---|
35 |
|
---|
36 | setNamesRecursively("", root);
|
---|
37 | if (DEBUGGING_TEST_MODE) printComponentNames(root, "");
|
---|
38 | }
|
---|
39 |
|
---|
40 | public static void setNamesRecursively(String prefix, Component root) {
|
---|
41 | if(!TEST_MODE) return;
|
---|
42 |
|
---|
43 | // root starts off as GLI's JFrame, so we know that all its GUI children are specifically JComponents
|
---|
44 |
|
---|
45 | // https://docs.oracle.com/javase/7/docs/api/index.html?java/lang/reflect/package-summary.html
|
---|
46 | String className = root.getClass().getSimpleName();
|
---|
47 |
|
---|
48 | String packageName = root.getClass().getPackage().getName();
|
---|
49 | if(!packageName.contains("org.greenstone.gatherer.gui")) {
|
---|
50 | if(root.getName() == null || root.getName().equals("")) { // if name not already set
|
---|
51 | if(!prefix.equals("")){
|
---|
52 | // TODO: fix this
|
---|
53 | root.setName(prefix + "." + className);
|
---|
54 | }
|
---|
55 | else {
|
---|
56 | root.setName(className);
|
---|
57 | }
|
---|
58 | } // else swing Component name already set
|
---|
59 |
|
---|
60 | } else { // root is in GLI GUI package, use its classname for name
|
---|
61 |
|
---|
62 | if(root.getName() == null || root.getName().equals("")) {
|
---|
63 | root.setName(className);
|
---|
64 | }
|
---|
65 | prefix = className;
|
---|
66 |
|
---|
67 | // check member vars
|
---|
68 | Field[] memberVars = root.getClass().getDeclaredFields(); // private to public member vars, but not inherited ones
|
---|
69 | for(int i = 0; i < memberVars.length; i++) {
|
---|
70 | memberVars[i].setAccessible(true); // make private/protected etc fields accessible
|
---|
71 |
|
---|
72 | // https://www.tutorialspoint.com/java/lang/class_isinstance.htm
|
---|
73 | Class jCompClass = Container.class;
|
---|
74 | Class memberVarClass = memberVars[i].getType();
|
---|
75 |
|
---|
76 | // memberVarClass is a JComponen (subclass)
|
---|
77 | if(jCompClass.isAssignableFrom(memberVarClass)) {
|
---|
78 |
|
---|
79 | // get the actual member variable denoted by memberVars[i]
|
---|
80 | // on the current JComponent instance 'root'.
|
---|
81 | // We now know this member var to be a JComponent
|
---|
82 | // Having the actual member variable of the instantiated instance,
|
---|
83 | // we can call setName on it
|
---|
84 | try {
|
---|
85 | Container memberComponent = (Container)memberVars[i].get(root);
|
---|
86 |
|
---|
87 | if(memberComponent != null) {
|
---|
88 |
|
---|
89 | // member var is a JComponent but not of GLI package, so locally instantiated
|
---|
90 | if(!memberVarClass.getPackage().getName().contains("org.greenstone.gatherer.gui")) {
|
---|
91 | String memberprefix = prefix + "." + memberVars[i].getName(); // append member var name
|
---|
92 | // now can call setName() on the actual member variable
|
---|
93 | memberComponent.setName(memberprefix);
|
---|
94 | setNamesRecursively(memberprefix, memberComponent);
|
---|
95 | }
|
---|
96 |
|
---|
97 | //else the member variable is a GLI GUI class, use its className as prefix for child components
|
---|
98 | // 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 |
|
---|
105 |
|
---|
106 | }
|
---|
107 | } catch(Exception e) {
|
---|
108 | e.printStackTrace();
|
---|
109 | }
|
---|
110 | }
|
---|
111 | // else not a JComponent, skip
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | // https://stackoverflow.com/questions/33927349/could-someone-please-explain-note-this-method-should-be-called-under-awt-tree
|
---|
117 | // No worries about AWT tree lock: setNamesRecursively is called by openGUI
|
---|
118 | // which is specifically called by GathererProg on the Event Dispatch Thread (EDT)
|
---|
119 | Component[] children = ((Container)root).getComponents();
|
---|
120 | for(int i = 0; i < children.length; i++) {
|
---|
121 | // if we haven't already set a name for any child JComponents with the above,
|
---|
122 | // then the following will do so now
|
---|
123 | setNamesRecursively(className, children[i]);
|
---|
124 | }
|
---|
125 |
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | // For debugging swing Component names assigned with Container's setName()
|
---|
130 | public static void printComponentNames(Component root, String tabbing) {
|
---|
131 | if(!TEST_MODE) return;
|
---|
132 |
|
---|
133 | System.err.println(tabbing + root.getName());
|
---|
134 | Component[] children = ((Container)root).getComponents();
|
---|
135 | // recursive call
|
---|
136 | for(int i = 0; i < children.length; i++) {
|
---|
137 | printComponentNames(children[i], tabbing + " ");
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|