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

159
LINES

< > BotCompany Repo | #1016978 - DynObjectTable

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

Libraryless. Click here for Pure Java version (18422L/136K).

1  
abstract sclass DynObjectTable<A> extends DynModule {
2  
  new L<A> data;
3  
  
4  
  transient JTable table;
5  
  transient F1<A, Map> itemToMap;
6  
  transient VF1<A> defaultAction;
7  
  transient bool debug, fieldsInOrder = true, withSearcher;
8  
  transient TableSearcher searcher;
9  
  transient L onListChanged;
10  
  transient Set<S> hideFields;
11  
  transient bool useStruct = false; // possible breaking change for older modules
12  
  
13  
  start {
14  
    itemToMap = func(A a) -> Map {
15  
      if (a instanceof S) ret litorderedmap("" := (S) a);
16  
      Map map = humanizeKeys(fieldsInOrder ? objectToMap_inOrder_withoutFields(a, hideFields) : objectToMap(a));
17  
      if (!useStruct) map = mapValues strOrEmpty(map);
18  
      ret map;
19  
    };
20  
    onChange(r updateTable);
21  
  }
22  
  
23  
  void addCountToName {
24  
    onListChanged = addDyn_sync(onListChanged, r addCountToNameNow);
25  
    addCountToNameNow();
26  
  }
27  
  
28  
  void addCountToNameNow() enter {
29  
    setModuleName(programTitle() + " (" + count() + ")");
30  
  }
31  
  
32  
  visualize {
33  
    L<Map> l = map(itemToMap, data);
34  
    table = dataToTable_uneditable(sexyTable(), l);
35  
    onDoubleClickOrEnter(table, voidfunc(int row) {
36  
      temp enter();
37  
      A a = _get(data, row);
38  
      if (a != null) onDoubleClick(a);
39  
    });
40  
    if (withSearcher)
41  
      ret (searcher = tableWithSearcher2(table)).panel;
42  
    ret table;
43  
  }
44  
  
45  
  void unvisualize() { super.unvisualize(); searcher = null; }
46  
  
47  
  void onDoubleClick(A line) { callF(defaultAction, line); }
48  
  
49  
  void updateTable() enter {
50  
    if (table != null) swing {
51  
      Point scrollPosition = enclosingViewPosition(table);
52  
      if (debug) print("Scroll position: " + scrollPosition);
53  
      dataToTable_uneditable(table, map(itemToMap, data));
54  
      setEnclosingViewPosition(table, scrollPosition);
55  
      if (searcher != null) searcher.rowIndices = null;
56  
      if (debug) print("dataToTable done, alerting " + n2(onListChanged, "listener"));
57  
    }
58  
    pcallFAll(onListChanged);
59  
  }
60  
  
61  
  /*TODO void itemChanged(int row, A entry) {
62  
    if (getRow(row) != entry) {
63  
      fireDataChanged();
64  
      warn("Item index mismatch, recreating table");
65  
    }
66  
    rows.add(dataToTable_makeRow(x, cols));
67  
  }*/
68  
  
69  
  void dontPersist() { _persistenceInfo = mapPlus(_persistenceInfo, data := false); }
70  
  
71  
  void clear() { syncClear(data); fireDataChanged(); }
72  
  void add(A a) { syncAdd(data, a); fireDataChanged(); }
73  
  void add(int idx, A a) { syncAdd(data, idx, a); fireDataChanged(); }
74  
  void addAll(Collection<A> l) { if (empty(l)) ret; syncAddAll(data, l); fireDataChanged(); }
75  
  void addAndScrollDown(A a) { add(a); scrollDown(); }
76  
  void remove(A a) { syncRemove(data, a); fireDataChanged(); }
77  
  void removeAll(L<A> a) { syncRemoveAll(data, a); fireDataChanged(); }
78  
  void setList(Iterable<A> data) { setData(data); }
79  
  void removeSelected() { removeAll(allSelected()); }
80  
  
81  
  // rare legacy binary compatibility workaround
82  
  void setData(Collection<A> data) { setData((Iterable) data); }
83  
  
84  
  void setData(Iterable<A> data) {
85  
    setData(data, false);
86  
  }
87  
  
88  
  void setData(Iterable<A> data, bool force) {
89  
    // ATTN: new logic. test if it works in all cases
90  
    swing {
91  
      //int selection = selectedIndex();
92  
      int[] selection = selectedTableRows_array(table);
93  
      L<A> cloned = cloneList(data);
94  
      if (setField(data := cloned) || force) {
95  
        updateTable();
96  
        dm_vmBus_send listChanged(module(), cloned);
97  
      }
98  
      //selectRow(table, selection);
99  
      selectTableRows(table, selection);
100  
    }
101  
  }
102  
  int count() { ret syncL(data); }
103  
  
104  
  void setData_force(Collection<A> data) {
105  
    setData(data, true);
106  
  }
107  
  
108  
  // tries to keep selection
109  
  void fireDataChanged() {
110  
    setData_force(data);
111  
  }
112  
  
113  
  int rowFromSearcher(int i) {
114  
    ret searcher == null || searcher.rowIndices == null ? i : or(get(searcher.rowIndices, i), -1);
115  
  }
116  
  
117  
  A selected() { ret syncGet(data, rowFromSearcher(selectedTableRowInModel(table))); }
118  
  L<A> allSelected() { ret syncListGetMulti(data, selectedIndices()); }
119  
  int selectedIndex() { ret selectedTableRow(table); }
120  
  L<Int> selectedIndices() { ret map(i -> rowFromSearcher(i), selectedTableRowsInModel(table)); }
121  
  bool selectItem(A a) { int i = indexOf(data, a); selectRow(table, i); ret i >= 0; } // true if item exists
122  
  void doubleClickItem(A a) { if (selectItem(a)) onDoubleClick(a); }
123  
  
124  
  // r : Runnable or VF1<A>
125  
  // run only in visualize()!
126  
  void popupMenuItem(S text, O r) {
127  
    tablePopupMenuItemsThreaded(table, text, _convertRunnable(r));
128  
  }
129  
  
130  
  void popupMenuItem_top(S text, O r) {
131  
    tablePopupMenuItemsThreaded_top(table, text, _convertRunnable(r));
132  
  }
133  
  
134  
  // row index => element
135  
  O _convertRunnable(fO r) {
136  
    if (r == null || r instanceof Runnable) ret r;
137  
    ret voidfunc(int idx) { callF(r, syncGet(data, idx)) };
138  
  }
139  
  
140  
  JTable table() { ret table; }
141  
  
142  
  void hideFields(S... fields) {
143  
    if (hideFields == null) hideFields = new Set;
144  
    _addAll(hideFields, fields);
145  
  }
146  
  
147  
  A getRow(int row) { ret get(data, row); }
148  
  
149  
  void scrollDown { scrollTableDownNow(table); }
150  
  
151  
  // API
152  
  
153  
  L<A> getData() { ret data; } // data is usually immutable
154  
  L<A> data() { ret getData(); }
155  
  L<A> list() { ret getData(); }
156  
  L<A> getList() { ret getData(); }
157  
  
158  
  L<A> clonedList() { ret cloneList(data); }
159  
}

Author comment

Began life as a copy of #1016065

download  show line numbers  debug dex   

Travelled to 11 computer(s): aoiabmzegqzx, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, tslmcundralx, tvejysmllsmz

No comments. add comment

Snippet ID: #1016978
Snippet name: DynObjectTable
Eternal ID of this version: #1016978/96
Text MD5: b1e7b1b102aadad21daa8530907bb943
Transpilation MD5: 0fbdd710e2a8a097ce40b602978f8c9f
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-01-23 16:56:42
Source code size: 5386 bytes / 159 lines
Pitched / IR pitched: No / No
Views / Downloads: 480 / 1601
Version history: 95 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1016978 & http://1016978.tinybrain.de