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

158
LINES

< > BotCompany Repo | #1033925 // JConceptsTable - new version of showConceptsTable [backup before filters]

JavaX fragment (include)

sclass JConceptsTable<A extends Concept> implements Swingable {
  Class<? extends A> conceptClass;
  Concepts concepts;
  JTable table;
  
  // options
  S hID = "ID"; // Column header for concept ID
  settable LS dropFields;
  IF1<L> postProcess;
  Runnable afterUpdate;
  bool latestFirst;
  IF1<Cl<A>> sorter = lambda1 defaultSort;
  int idWidth = 50;
  int updateInterval = 100;
  int firstUpdateInterval = 100;
  bool humanizeFieldNames = true;
  Float tableFontSize;
  Int tableRowHeight;
  settable bool addCountToEnclosingTab;
  
  AWTOnConceptChanges changeHandler;
  
  *() {}
  *(Class<? extends A> *conceptClass) {}
  *(Concepts *concepts, Class<? extends A> *conceptClass) {}
  
  swappable MapSO itemToMap(A a) {
    ret putAll(specialFieldsForItem(a), mapValues renderForTable_noStruct(itemToMap_inner2(a)));
  }
  
  swappable MapSO itemToMap_inner2(A a) {
    ret allConceptFieldsAsMapExcept(a, dropFields);
  }
  
  // shown on the left (usually)
  swappable MapSO specialFieldsForItem(A a) {
    MapSO map = litorderedmap(hID, str(a.id));
    mapPut(map, "Java Class", javaClassDescForItem(a));
    ret map;
  }
  
  S javaClassDescForItem(A a) {
    S className = dynShortClassName(a);
    if (neq(className, shortClassName(conceptClass))) {
      S text = className;
      S realClass = shortClassName(a);
      if (neq(className, realClass))
        text += " as " + realClass;
      ret text;
    }
    null;
  }
  
  S defaultTitle() {
    ret plural(shortClassName(conceptClass));
  }
  
  void showAsFrame(S title default defaultTitle()) {
    makeTable();
    showFrame(title, table);
  }
  
  void makeTable {
    if (table != null) ret;
    if (concepts == null) concepts = db_mainConcepts();

    table = sexyTable();
    if (tableFontSize != null) {
      setTableFontSizes(tableFontSize, table);
      if (tableRowHeight == null)
        tableRowHeight = iround(tableFontSize*1.5);
    }
    if (tableRowHeight != null) setRowHeight(table, tableRowHeight);
    
    changeHandler = new AWTOnConceptChanges(concepts, table, lambda0 _update)
      .delay(updateInterval)
      .firstDelay(firstUpdateInterval);
    changeHandler.install();
  }
  
  // e.g. to update enclosing tab when hidden
  void update {
    swing { _update(); }
  }
  
  void _update {
    new L<Map> data;
    Cl<? extends A> l = list(concepts, conceptClass);
    l = postProcess(sorter, l);
    for (A c : l)
      addIfNotNull(data, itemToMap(c));
    if (latestFirst) reverseInPlace(data);
    data = (L) postProcess(postProcess, data);
    dataToTable_uneditable(data, table);
    if (humanizeFieldNames)
      humanizeTableColumns();
    tableColumnMaxWidth(table, 0, idWidth);
 
    if (addCountToEnclosingTab)
      updateEnclosingTabTitle();

    pcallF(afterUpdate);
  }
  
  void updateEnclosingTabTitle {
    updateEnclosingTabTitleWithCount(table, countConcepts(concepts, conceptClass));
  }
  
  void humanizeTableColumns {
    int n = tableColumnCount(table);
    for i to n:
      setColumnName(table, i, humanizeFormLabel(getColumnName(table, i)));
  };

  visual table();

  JTable table() {
    makeTable();
    ret table;
  }
  
  A selectedConcept() {
    ret (A) concepts.getConcept(toLong(selectedTableCell(table, 0)));
  }
  
  A selected() { ret selectedConcept(); }
  
  A getItem(int row) {
    ret (A) concepts.getConcept(toLong(getTableCell(table, row, 0)));
  }
  
  int indexOfConcept(final A c) {
    if (c == null) ret -1;
    ret swing(func -> int {
      int n = tableRowCount(table);
      for row to n:
        if (toLong(getTableCell(table, row, 0)) == c.id)
          ret row;
      ret -1;
    });
  }
  
  L<A> selectedConcepts() {
    int[] rows = table.getSelectedRows();
    new L<A> l;
    for (int row : rows)
      l.add(getItem(row));
    ret l;
  }
  
  Cl<A> defaultSort(Cl<A> l) {
    ret sortByConceptID(l);
  }
  
  /*void dropFields(S... fields) {
    dropFields = asList(fields);
  }*/
}

Author comment

Began life as a copy of #1030725

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1033925
Snippet name: JConceptsTable - new version of showConceptsTable [backup before filters]
Eternal ID of this version: #1033925/1
Text MD5: 52f49c8207a74fdffb8456e39f3d79bd
Author: stefan
Category: javax / gui / concepts
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-12 17:26:22
Source code size: 4101 bytes / 158 lines
Pitched / IR pitched: No / No
Views / Downloads: 59 / 70
Referenced in: [show references]