Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

187
LINES

< > BotCompany Repo | #1035311 // G22JavaObjectVisualizer backup

JavaX fragment (include)

1  
srecord noeq G22JavaObjectVisualizer(G22Utils g22utils) is Swingable {
2  
  settable bool withTypeAndTime = true;
3  
  settable bool singleLineLayout; // TODO
4  
  settable int maxElements = 1000; // max elements of collection to show
5  
  settable bool horizontal;
6  
  
7  
  Set seen = identityHashSet();
8  
  
9  
  transient settable O object;
10  
  transient settable long nanos = -1;
11  
  
12  
  *(G22Utils *g22utils, O *object) {}
13  
14  
  // call after visualizeObject (so properties are filled)  
15  
  S objectInfos() {
16  
    new LS infos;
17  
    infos.add(str(shortClassName(object))); // shows "null" for null value
18  
    for (key, val : mainVisualizer().properties) {
19  
      S s = n2OrStrOrNull(val);
20  
      if (nempty(s))
21  
        infos.add(key + " " + s);
22  
    }
23  
    ret joinNemptiesWithComma(infos);
24  
  }
25  
26  
  cachedVisualize {
27  
    printVars("Visualizing", +this, +withTypeAndTime);
28  
    
29  
    JComponent c = visualizeObject(object);
30  
31  
    if (withTypeAndTime) {
32  
      S type = objectInfos();
33  
      S timeDesc = nanos < 0 ? "" : formatElapsedTimeWithAppropriateUnit(nanosToSeconds(nanos));
34  
      printVars(+nanos, +timeDesc);
35  
      c = northAndCenterWithMargins(
36  
        westAndEastWithMargin(
37  
          withLabel("Type:", jlabel(type)),
38  
          empty(timeDesc) ? jpanel() : withLabel("Execution time:", jlabel(timeDesc))
39  
        ),
40  
        c);
41  
    }
42  
    
43  
    ret c;
44  
  }
45  
  
46  
  JComponent visualizeObject(O object) {
47  
    ret mainVisualizer().visualize(object);
48  
  }
49  
  
50  
  simplyCached ObjectVisualizer mainVisualizer() {
51  
    ret new ObjectVisualizer().recurse(true).bigFont(true).horizontal(horizontal);
52  
  }
53  
  
54  
  class ObjectVisualizer {
55  
    settable bool recurse;
56  
    settable bool bigFont;
57  
    settable bool horizontal;
58  
    
59  
    new LinkedHashMap<S, O> properties;
60  
  
61  
    // only one recursion level so far
62  
    ObjectVisualizer subVisualizer() {
63  
      ret new ObjectVisualizer().horizontal(!horizontal);
64  
    }
65  
    
66  
    JComponent makeBig(JComponent c) {
67  
      if (bigFont) ret fontSizePlus(10, c);
68  
      ret c;
69  
    }
70  
71  
    JComponent visualize(O object) {
72  
      try {
73  
        ret visualize2(object);
74  
      } catch e {
75  
        ret jErrorView(new RuntimeException("Visualization error", e));
76  
      }
77  
    }
78  
    
79  
    JComponent visualize2(O object) {
80  
      // exception
81  
      
82  
      if (object cast Throwable)
83  
        ret jErrorView(object);
84  
        
85  
      // null or empty string
86  
      
87  
      if (object == null || eq(object, ""))
88  
        ret jpanel();
89  
        
90  
      // Swing component
91  
      
92  
      if (object cast Swingable)
93  
        object = object.visualize();
94  
        
95  
      if (object cast JComponent && getParent(object) == null)
96  
        ret object;
97  
        
98  
      // number
99  
      
100  
      if (object cast Int) object = toLong(object);
101  
      if (object cast Long)
102  
        ret makeBig(jcenteredLabel(n2(object)));
103  
      if (object cast Number)
104  
        ret makeBig(jcenterNarrowLabel(str(object)));
105  
        
106  
      if (object cast Pair) {
107  
        if (horizontal)
108  
          ret jcenteredline(jlabel("<"), visualize(object.a), jlabel(","),
109  
            visualize(object.b), jlabel(">"));
110  
        else
111  
          ret vstackWithSpacing(visualize(object.a), visualize(object.b));
112  
      }
113  
        
114  
      // image
115  
      
116  
      if (object cast MakesBufferedImage)      
117  
        object = toBufferedImage(object);
118  
      if (object cast BufferedImage) {
119  
        var is = g22utils.stdImageSurface(object);
120  
        if (!recurse) is.setAutoZoomToDisplay(false);
121  
        ret jscroll_centered_borderless(is);
122  
      }
123  
        
124  
      if (recurse) {
125  
        // TODO
126  
        /*if (!seen.add(object))
127  
          ret jlabel("Object already seen");*/
128  
        
129  
        // multi-map
130  
        
131  
        ifclass MultiMap
132  
        if (object cast MultiMap) {
133  
          propertyLength(l(object));
134  
          propertyKeyCount(object.keyCount());
135  
          object = multiMapToMapPairs(object);
136  
        }
137  
        endif
138  
        
139  
        ifclass MultiSetMap
140  
        if (object cast MultiSetMap) {
141  
          propertyLength(l(object));
142  
          propertyKeyCount(object.keyCount());
143  
          object = multiSetMapToMapPairs(object);
144  
        }
145  
        endif
146  
        
147  
        // map
148  
        
149  
        if (object cast Map) {
150  
          propertyLength(l(object));
151  
          object = mapToPairs(object);
152  
        }
153  
        
154  
        // collection
155  
      
156  
        if (object cast Collection) {
157  
          propertyLength(l(object));
158  
          var sub = subVisualizer();
159  
          var elements = map(cloneTakeFirst(maxElements, object),
160  
            element -> sub.visualize(element));
161  
          ret jscroll_centered_borderless(horizontal
162  
            ? hstackWithSpacing(elements)
163  
            : vstackWithSpacing(elements);
164  
        }
165  
        
166  
        if (isArray(object))
167  
          propertyLength(arrayLength(object));
168  
      }
169  
      
170  
      if (object cast S) {
171  
        propertyLength(l(object));
172  
      }
173  
    
174  
      S string = str(object);
175  
      if (containsNewLine(string))
176  
        ret jscroll_borderless(wordWrapTypeWriterTextArea(string));
177  
        
178  
      var lbl = jcenterNarrowLabel(shorten(string));
179  
      if (l(string) <= 10)
180  
        ret makeBig(lbl);
181  
      ret lbl;
182  
    } // end of visualize2
183  
    
184  
    void propertyLength(int l) { properties.put("size", l); }
185  
    void propertyKeyCount(int l) { properties.put("keys", l); }
186  
  } // end of ObjectVisualizer
187  
}

Author comment

Began life as a copy of #1034494

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035311
Snippet name: G22JavaObjectVisualizer backup
Eternal ID of this version: #1035311/1
Text MD5: 47f67762859709cc51b98c7910fa6da4
Author: stefan
Category: javax / gazelle 22
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-04-30 18:37:07
Source code size: 5440 bytes / 187 lines
Pitched / IR pitched: No / No
Views / Downloads: 50 / 62
Referenced in: [show references]