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

135
LINES

< > BotCompany Repo | #1019715 // SimpleCRUD - older version

JavaX fragment (include)

1  
sbool SimpleCRUD_searcher = true;
2  
3  
sclass SimpleCRUD<A extends Concept> {
4  
  Class<A> cc;
5  
  JTable table;
6  
  JPanel buttons, panel;
7  
  O renderer; // optional, func(A) -> Map
8  
  S hID = "ID";
9  
  Set<S> excludeFieldsFromEditing;
10  
11  
  *(Class<A> *cc) {}
12  
  
13  
  SimpleCRUD<A> show(S frameTitle) {
14  
    make();
15  
    showFrame(frameTitle, panel);
16  
    this;
17  
  }
18  
  
19  
  SimpleCRUD<A> show() {
20  
    ret show(plural(shortClassName(cc)));
21  
  }
22  
  
23  
  SimpleCRUD<A> showMaximized() { show(); maximizeFrame(panel); ret this; }
24  
  
25  
  JPanel make() {
26  
    db();
27  
    framesBot();
28  
    // next line not in swing part to allow passing arguments
29  
    // from outside like showConceptsTable_afterUpdate
30  
    table = makeConceptsTable(cc, wrapRenderer(renderer));
31  
    swing {
32  
      panel = centerAndSouthWithMargin(SimpleCRUD_searcher ? tableWithSearcher(table, withMargin := true) : table, buttons = jRightAlignedLine(
33  
        jbutton("Add...", r { newConcept() }),
34  
        tableDependButton(table, jbutton("Edit", r {
35  
          editConcept(selectedConcept())
36  
        })),
37  
        tableDependButton(table, jbutton("Delete", r {
38  
          final L<A> l = selectedConcepts();
39  
          withDBLock(r { for (A c : l) c.delete() });
40  
        }))));
41  
      
42  
      O fEdit = voidfunc(int row) {
43  
        editConcept((A) getConcept(toLong(getTableCell(table, row, 0))))
44  
      };
45  
      tablePopupMenuItem(table, "Edit...", fEdit);
46  
      onDoubleClick(table, fEdit);
47  
    }
48  
    ret panel;
49  
  }
50  
  
51  
  O wrapRenderer(fO renderer) {
52  
    ret renderer == null ? null : func(A a) {
53  
      putAll(litorderedmap(hID, str(a.id)), (Map) callF(renderer, a))
54  
    };
55  
  }
56  
  
57  
  void newConcept {
58  
    final A c = unlisted(cc);
59  
    final Map<S, JComponent> map = makeComponents(c);
60  
    Runnable r = r {
61  
      register(c);
62  
      saveData(c, map);
63  
    };
64  
    showFormTitled("New " + shortClassName(cc), arrayPlus(mapToObjectArray(map), r));
65  
  }
66  
67  
  void editConcept(final A c) {
68  
    if (c == null) ret;
69  
    final Map<S, JComponent> map = makeComponents(c);
70  
    Runnable r = r { saveData(c, map) };
71  
    showFormTitled("Edit " + shortClassName(cc) + " #" + c.id, arrayPlus(mapToObjectArray(map), r));
72  
  }
73  
  
74  
  A selectedConcept() {
75  
    ret (A) getConcept(toLong(selectedTableCell(table, 0)));
76  
  }
77  
  
78  
  L<A> selectedConcepts() {
79  
    int[] rows = table.getSelectedRows();
80  
    new L<A> l;
81  
    for (int row : rows)
82  
      l.add((A) getConcept(toLong(getTableCell(table, row, 0))));
83  
    ret l;
84  
  }
85  
  
86  
  Map<S, JComponent> makeComponents(A c) {
87  
    Map<S, JComponent> map = litorderedmap();
88  
    makeComponents(c, map);
89  
    ret map;
90  
  }
91  
  
92  
  JComponent fieldComponent(A c, S field) {
93  
    Class type = getFieldType(cc, field);
94  
    O value = getOpt(c, field);
95  
    //print("Field type: " + field + " => " + type);
96  
    if (type == bool.class)
97  
      ret jCenteredCheckBox(isTrue(value));
98  
    else try {
99  
      ret autoComboBox(structureOrText_crud(value), new TreeSet<S>(map structureOrText_crud(collect(list(cc), field))));
100  
    } catch e {
101  
      printException(e);
102  
      ret jTextField(structureOrText_crud(value));
103  
    }
104  
  }
105  
  
106  
  void saveComponent(A c, S field, JComponent comp) {
107  
    comp = unwrap(comp);
108  
    if (comp instanceof JTextComponent)
109  
      cset(c, field, convertToField(getTextTrim((JTextComponent) comp), cc, field));
110  
    else if (comp instanceof JComboBox)
111  
      cset(c, field, convertToField(getTextTrim((JComboBox) comp), cc, field));
112  
    else if (comp instanceof JCheckBox)
113  
      cset(c, field, isChecked((JCheckBox) comp));
114  
    ifclass ImageChooser
115  
      else if (comp instanceof ImageChooser)
116  
        cUpdatePNGFile(c, field, comp/ImageChooser.getImage(), false);
117  
    endif
118  
  }
119  
  
120  
  L<S> fields() {
121  
    ret listWithoutSet([S field : conceptFieldsInOrder(cc) | fieldType(cc, field) != Concept.Ref.class], excludeFieldsFromEditing);
122  
  }
123  
  
124  
  // override the following two methods to customize edit window
125  
  
126  
  void makeComponents(A c, Map<S, JComponent> map) {
127  
    for (S field : fields())
128  
      map.put(field, fieldComponent(c, field));
129  
  }
130  
  
131  
  void saveData(A c, Map<S, JComponent> components) {
132  
    for (S field : keys(components))
133  
      saveComponent(c, field, components.get(field));
134  
  }
135  
}

Author comment

Began life as a copy of #1006540

download  show line numbers  debug dex  old transpilations   

Travelled to 11 computer(s): bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1019715
Snippet name: SimpleCRUD - older version
Eternal ID of this version: #1019715/1
Text MD5: a590fc349d23e561ce13bcb3f0de8a52
Author: stefan
Category: javax / concepts / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-11-23 15:42:14
Source code size: 4254 bytes / 135 lines
Pitched / IR pitched: No / No
Views / Downloads: 262 / 281
Referenced in: [show references]