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

94
LINES

< > BotCompany Repo | #1006009 // showConceptsTable + makeConceptsTable - show table of concepts (Swing)

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

Libraryless. Click here for Pure Java version (22364L/145K).

// optional parameters
static ThreadLocal<L<S>> showConceptsTable_dropFields = new ThreadLocal;
static new ThreadLocal showConceptsTable_postProcess; // func(L) -> L
static new ThreadLocal showConceptsTable_afterUpdate; // voidfunc(JTable)
static new ThreadLocal<Concepts> showConceptsTable_concepts;
static new ThreadLocal<Bool> showConceptsTable_latestFirst;
static new ThreadLocal<O> showConceptsTable_sorter; // func(Cl<Concept>) -> Cl<Concept>

static int makeConceptsTable_idWidth = 50;

static JTable showConceptsTable(Class<? extends Concept> c) {
  JTable table = makeConceptsTable(c);
  showFrame(plural(shortClassName(c)), table);
  ret table;
}

static JTable makeConceptsTable(Class<? extends Concept> c) {
  LS dropFields = getAndClearThreadLocal(showConceptsTable_dropFields);
  O pp = getAndClearThreadLocal(showConceptsTable_postProcess);
  O afterUpdate = optParam(showConceptsTable_afterUpdate);
  Concepts concepts = optParam(showConceptsTable_concepts, db_mainConcepts());
  bool latestFirst = boolParam(showConceptsTable_latestFirst);
  O sorter = optParam(showConceptsTable_sorter);
  bool useStruct = boolParam(dataToTable_useStruct);

  final L<S> fields = listMinusList(concatLists(ll("id"), conceptFieldsInOrder(c)), dropFields);
  final JTable table = sexyTable();
  tablePopupMenu(table, voidfunc(JPopupMenu menu, final int row) {
    addMenuItem(menu, "Delete", r {
      final long id = toLong(getTableCell(table, row, 0));
      deleteConcept_sync(concepts, id);
    });
  });
  temp tempSetTL(awtOnConceptChanges_concepts, concepts);
  awtOnConceptChanges(table, r {
    //print("Updating concept table");
    new L<L> data;
    Cl<? extends Concept> l = list(concepts, c);
    if (sorter != null) l = (Cl) callF(sorter, l);
    for (Concept cc : l)
      data.add(map(func(S field) {
        O value = cget(cc, field);
        ret useStruct ? renderForTable(value)
                      : renderForTable_noStruct(value);
      }, fields));
    if (latestFirst) reverseInPlace(data);
    data = (L) postProcess(pp, data);
    fillTableWithData(table, data, map humanizeFormLabel(fields));
    if (table.getColumnCount() > 0)
      table.getColumnModel().getColumn(0).setMaxWidth(makeConceptsTable_idWidth);
    pcallF(afterUpdate, table);
  }, true);
  ret table;
}

// renderer takes a concept and makes a map for display
static <A extends Concept> JTable makeConceptsTable(final Class<A> cClass, final O renderer) {
  if (renderer == null) ret makeConceptsTable(cClass);
  fO pp = optParam(showConceptsTable_postProcess);
  fO afterUpdate = optParam(showConceptsTable_afterUpdate);
  final Concepts concepts = optParam(showConceptsTable_concepts, db_mainConcepts());
  fbool latestFirst = boolParam(showConceptsTable_latestFirst);
  O sorter = optParam(showConceptsTable_sorter);
  bool useStruct = boolParam(dataToTable_useStruct);
  
  final JTable table = sexyTable();
  temp tempSetTL(awtOnConceptChanges_concepts, concepts);
  awtOnConceptChanges(table, 1000, r {
    new L<Map> data;
    Cl<A> l = list(concepts, cClass);
    if (sorter != null) l = (Cl) callF(sorter, l);
    for (A c : l)
      addIfNotNull(data, (Map) pcallF(renderer, c));
    if (latestFirst) reverseInPlace(data);
    data = (L) postProcess(pp, data);
    ifdef makeConceptsTable_debug
      printVars makeConceptsTable(+useStruct);
    endifdef
    temp tempSetTL(dataToTable_useStruct, useStruct);
    dataToTable_uneditable(data, table);
    pcallF(afterUpdate, table);
  });
  ret table;
}

static <A extends Concept> JTable showConceptsTable(final Class<A> c, final O f) {
  JTable table = makeConceptsTable(c, f);
  showFrame(plural(shortClassName(c)), table);
  ret table;
}

static <A extends Concept> JTable showConceptsTable(S title, Class<A> cClass, O f) {
  ret setFrameTitle(title, showConceptsTable(cClass, f));
}

download  show line numbers  debug dex  old transpilations   

Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, sawdedvomwva, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1006009
Snippet name: showConceptsTable + makeConceptsTable - show table of concepts (Swing)
Eternal ID of this version: #1006009/47
Text MD5: d8ba1d208039ad36299f71dca05c3741
Transpilation MD5: f4e7dd9feeab541a31985695345e0939
Author: stefan
Category: javax / gui / concepts
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-09-12 04:36:39
Source code size: 3934 bytes / 94 lines
Pitched / IR pitched: No / No
Views / Downloads: 618 / 663
Version history: 46 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)
#1026279 - ConceptTable - better class-based version of showConceptsTable/makeConceptsTable. Probably JConceptsTable is even better
#1030725 - JConceptsTable - new version of showConceptsTable