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

202
LINES

< > BotCompany Repo | #1034494 // G22JavaObjectVisualizer

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (53150L) is out of date.

srecord noeq G22JavaObjectVisualizer(G22Utils g22utils) is Swingable {
  settable bool withTypeAndTime = true;
  settable bool singleLineLayout; // TODO
  settable int maxElements = 1000; // max elements of collection to show
  settable bool horizontal;
  settable int floatingPointDigits = 4;
  
  Set seen = identityHashSet();
  
  transient settable O object;
  transient settable long nanos = -1;
  
  *(G22Utils *g22utils, O *object) {}

  // call after visualizeObject (so properties are filled)  
  S objectInfos() {
    new LS infos;
    infos.add(str(shortClassName(object))); // shows "null" for null value
    for (key, val : mainVisualizer().properties) {
      S s = n2OrStrOrNull(val);
      if (nempty(s))
        infos.add(key + " " + s);
    }

    if (nanos >= 0) {
      S timeDesc = nanos < 0 ? "" : formatElapsedTimeWithAppropriateUnit(nanosToSeconds(nanos));
      infos.add(timeDesc);
    }

    ret joinNemptiesWithComma(infos);
  }

  cachedVisualize {
    printVars("Visualizing", +this, +withTypeAndTime);
    
    JComponent c = visualizeObject(object);

    if (withTypeAndTime) {
      S type = objectInfos();
      c = northAndCenterWithMargins(
        withLabel("Type:", jlabel(type)),
        c);
    }
    
    ret c;
  }
  
  JComponent visualizeObject(O object) {
    ret mainVisualizer().visualize(object);
  }
  
  simplyCached ObjectVisualizer mainVisualizer() {
    ret new ObjectVisualizer().recurse(true).bigFont(true).horizontal(horizontal);
  }
  
  class ObjectVisualizer {
    settable bool recurse;
    settable bool bigFont;
    settable bool horizontal;
    
    new LinkedHashMap<S, O> properties;
  
    // only one recursion level so far
    ObjectVisualizer subVisualizer() {
      ret new ObjectVisualizer().horizontal(!horizontal);
    }
    
    JComponent makeBig(JComponent c) {
      if (bigFont) ret fontSizePlus(10, c);
      ret c;
    }

    JComponent visualize(O object) {
      try {
        ret visualize2(object);
      } catch e {
        ret jErrorView(new RuntimeException("Visualization error", e));
      }
    }
    
    JComponent visualize2(O object) {
      // exception
      
      if (object cast Throwable)
        ret jErrorView(object);
        
      // null or empty string
      
      if (object == null || eq(object, ""))
        ret jpanel();
        
      // array to list
      
      object = arrayToList(object);
          
      // Swing component
      
      if (object cast Swingable)
        object = object.visualize();
        
      if (object cast JComponent && getParent(object) == null)
        ret object;
        
      // number
      
      if (object cast Int) object = toLong(object);
      if (object cast Long)
        ret bigText(n2(object));
      if (object cast Double)
        ret toolTip(str(object), bigText(formatDouble(object, floatingPointDigits)));
      if (object cast Number)
        ret bigText(object);
        
      if (object cast Pair) {
        if (horizontal)
          ret jcenteredline(jlabel("<"), visualize(object.a), jlabel(","),
            visualize(object.b), jlabel(">"));
        else
          ret vstackWithSpacing(visualize(object.a), visualize(object.b));
      }
        
      // image
      
      if (object cast MakesBufferedImage)      
        object = toBufferedImage(object);
      if (object cast BufferedImage) {
        var is = g22utils.stdImageSurface(object);
        if (!recurse) is.setAutoZoomToDisplay(false);
        ret jscroll_centered_borderless(is);
      }
        
      if (recurse) {
        // TODO
        /*if (!seen.add(object))
          ret jlabel("Object already seen");*/
        
        // multi-map
        
        ifclass MultiMap
        if (object cast MultiMap) {
          propertyLength(l(object));
          propertyKeyCount(object.keyCount());
          object = multiMapToMapPairs(object);
        }
        endif
        
        ifclass MultiSetMap
        if (object cast MultiSetMap) {
          propertyLength(l(object));
          propertyKeyCount(object.keyCount());
          object = multiSetMapToMapPairs(object);
        }
        endif
        
        // map
        
        if (object cast Map) {
          propertyLength(l(object));
          object = mapToPairs(object);
        }
        
        // array / collection
      
        if (object cast Collection) {
          propertyLength(l(object));
          var sub = subVisualizer();
          var elements = map(cloneTakeFirst(maxElements, object),
            element -> sub.visualize(element));
          ret jscroll_centered_borderless(horizontal
            ? hstackWithSpacing(elements)
            : vstackWithSpacing(elements);
        }
      } // end of if recurse
      
      if (object cast S) {
        propertyLength(l(object));
      }
    
      S string = str(object);
      if (containsNewLine(string))
        ret jscroll_borderless(wordWrapTypeWriterTextArea(string));
        
      var lbl = jcenterNarrowLabel(shorten(string));
      if (l(string) <= 10)
        ret makeBig(lbl);
      ret lbl;
    } // end of visualize2
    
    void propertyLength(int l) { properties.put("size", l); }
    void propertyKeyCount(int l) { properties.put("keys", l); }
    
    JComponent bigText(O o) { 
      ret makeBig(jcenteredLabel(str(o)));
    }

    O arrayToList(O object) {    
      if (isArray(object))
        object = wrapArrayAsImmutableList(object);
      ret object;
    }
  } // end of ObjectVisualizer
}

download  show line numbers  debug dex  old transpilations   

Travelled to 5 computer(s): bhatertpkbcr, ekrmjmnbrukm, iveijnkanddl, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034494
Snippet name: G22JavaObjectVisualizer
Eternal ID of this version: #1034494/59
Text MD5: 6f925e6802bfb172e74abaeccad7f97a
Author: stefan
Category: javax / gazelle 22
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-06-11 00:26:59
Source code size: 5704 bytes / 202 lines
Pitched / IR pitched: No / No
Views / Downloads: 156 / 558
Version history: 58 change(s)
Referenced in: [show references]