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)

1  
sclass JConceptsTable<A extends Concept> implements Swingable {
2  
  Class<? extends A> conceptClass;
3  
  Concepts concepts;
4  
  JTable table;
5  
  
6  
  // options
7  
  S hID = "ID"; // Column header for concept ID
8  
  settable LS dropFields;
9  
  IF1<L> postProcess;
10  
  Runnable afterUpdate;
11  
  bool latestFirst;
12  
  IF1<Cl<A>> sorter = lambda1 defaultSort;
13  
  int idWidth = 50;
14  
  int updateInterval = 100;
15  
  int firstUpdateInterval = 100;
16  
  bool humanizeFieldNames = true;
17  
  Float tableFontSize;
18  
  Int tableRowHeight;
19  
  settable bool addCountToEnclosingTab;
20  
  
21  
  AWTOnConceptChanges changeHandler;
22  
  
23  
  *() {}
24  
  *(Class<? extends A> *conceptClass) {}
25  
  *(Concepts *concepts, Class<? extends A> *conceptClass) {}
26  
  
27  
  swappable MapSO itemToMap(A a) {
28  
    ret putAll(specialFieldsForItem(a), mapValues renderForTable_noStruct(itemToMap_inner2(a)));
29  
  }
30  
  
31  
  swappable MapSO itemToMap_inner2(A a) {
32  
    ret allConceptFieldsAsMapExcept(a, dropFields);
33  
  }
34  
  
35  
  // shown on the left (usually)
36  
  swappable MapSO specialFieldsForItem(A a) {
37  
    MapSO map = litorderedmap(hID, str(a.id));
38  
    mapPut(map, "Java Class", javaClassDescForItem(a));
39  
    ret map;
40  
  }
41  
  
42  
  S javaClassDescForItem(A a) {
43  
    S className = dynShortClassName(a);
44  
    if (neq(className, shortClassName(conceptClass))) {
45  
      S text = className;
46  
      S realClass = shortClassName(a);
47  
      if (neq(className, realClass))
48  
        text += " as " + realClass;
49  
      ret text;
50  
    }
51  
    null;
52  
  }
53  
  
54  
  S defaultTitle() {
55  
    ret plural(shortClassName(conceptClass));
56  
  }
57  
  
58  
  void showAsFrame(S title default defaultTitle()) {
59  
    makeTable();
60  
    showFrame(title, table);
61  
  }
62  
  
63  
  void makeTable {
64  
    if (table != null) ret;
65  
    if (concepts == null) concepts = db_mainConcepts();
66  
67  
    table = sexyTable();
68  
    if (tableFontSize != null) {
69  
      setTableFontSizes(tableFontSize, table);
70  
      if (tableRowHeight == null)
71  
        tableRowHeight = iround(tableFontSize*1.5);
72  
    }
73  
    if (tableRowHeight != null) setRowHeight(table, tableRowHeight);
74  
    
75  
    changeHandler = new AWTOnConceptChanges(concepts, table, lambda0 _update)
76  
      .delay(updateInterval)
77  
      .firstDelay(firstUpdateInterval);
78  
    changeHandler.install();
79  
  }
80  
  
81  
  // e.g. to update enclosing tab when hidden
82  
  void update {
83  
    swing { _update(); }
84  
  }
85  
  
86  
  void _update {
87  
    new L<Map> data;
88  
    Cl<? extends A> l = list(concepts, conceptClass);
89  
    l = postProcess(sorter, l);
90  
    for (A c : l)
91  
      addIfNotNull(data, itemToMap(c));
92  
    if (latestFirst) reverseInPlace(data);
93  
    data = (L) postProcess(postProcess, data);
94  
    dataToTable_uneditable(data, table);
95  
    if (humanizeFieldNames)
96  
      humanizeTableColumns();
97  
    tableColumnMaxWidth(table, 0, idWidth);
98  
 
99  
    if (addCountToEnclosingTab)
100  
      updateEnclosingTabTitle();
101  
102  
    pcallF(afterUpdate);
103  
  }
104  
  
105  
  void updateEnclosingTabTitle {
106  
    updateEnclosingTabTitleWithCount(table, countConcepts(concepts, conceptClass));
107  
  }
108  
  
109  
  void humanizeTableColumns {
110  
    int n = tableColumnCount(table);
111  
    for i to n:
112  
      setColumnName(table, i, humanizeFormLabel(getColumnName(table, i)));
113  
  };
114  
115  
  visual table();
116  
117  
  JTable table() {
118  
    makeTable();
119  
    ret table;
120  
  }
121  
  
122  
  A selectedConcept() {
123  
    ret (A) concepts.getConcept(toLong(selectedTableCell(table, 0)));
124  
  }
125  
  
126  
  A selected() { ret selectedConcept(); }
127  
  
128  
  A getItem(int row) {
129  
    ret (A) concepts.getConcept(toLong(getTableCell(table, row, 0)));
130  
  }
131  
  
132  
  int indexOfConcept(final A c) {
133  
    if (c == null) ret -1;
134  
    ret swing(func -> int {
135  
      int n = tableRowCount(table);
136  
      for row to n:
137  
        if (toLong(getTableCell(table, row, 0)) == c.id)
138  
          ret row;
139  
      ret -1;
140  
    });
141  
  }
142  
  
143  
  L<A> selectedConcepts() {
144  
    int[] rows = table.getSelectedRows();
145  
    new L<A> l;
146  
    for (int row : rows)
147  
      l.add(getItem(row));
148  
    ret l;
149  
  }
150  
  
151  
  Cl<A> defaultSort(Cl<A> l) {
152  
    ret sortByConceptID(l);
153  
  }
154  
  
155  
  /*void dropFields(S... fields) {
156  
    dropFields = asList(fields);
157  
  }*/
158  
}

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: 152 / 150
Referenced in: [show references]