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

124
LINES

< > BotCompany Repo | #1019417 // dm_SimpleCRUD [dev.] - create/read/update/delete for a list in a dynamic module

JavaX fragment (include)

sbool dm_SimpleCRUD_searcher = true;

sclass dm_SimpleCRUD<A> {
  DynModule module;
  L<A> theList;
  Class<A> cc;
  JTable table;
  JPanel buttons, panel;
  O renderer; // optional, func(A) -> Map
  S hID = "ID";
  LS fields;

  *(DynModule *module, L<A> *theList, Class<A> *cc) {}
  
  JPanel make() {
    // next line not in swing part to allow passing arguments
    // from outside like showConceptsTable_afterUpdate
    table = makeConceptsTable(cc, wrapRenderer(renderer));
    swing {
      panel = centerAndSouth(SimpleCRUD_searcher ? tableWithSearcher(table) : table, buttons = jRightAlignedLine(
        jbutton("Add...", r { newConcept() }),
        tableDependButton(table, jbutton("Edit", r {
          editConcept(selectedConcept())
        })),
        tableDependButton(table, jbutton("Delete", r {
          final L<A> l = selectedConcepts();
          withDBLock(r { for (A c : l) c.delete() });
        }))));
      
      O fEdit = voidfunc(int row) {
        editConcept((A) getConcept(toLong(getTableCell(table, row, 0))))
      };
      tablePopupMenuItem(table, "Edit...", fEdit);
      onDoubleClick(table, fEdit);
    }
    ret panel;
  }
  
  O wrapRenderer(fO renderer) {
    ret renderer == null ? null : func(A a) {
      putAll(litorderedmap(hID, str(a.id)), (Map) callF(renderer, a))
    };
  }
  
  void newConcept {
    final A c = nu(cc);
    final Map<S, JComponent> map = makeComponents(c);
    Runnable r = r {
      saveData(c, map);
      theList.add(c); module.change();
    };
    showFormTitled("New " + shortClassName(cc), arrayPlus(mapToObjectArray(map), r));
  }

  void editConcept(final A c) {
    if (c == null) ret;
    final Map<S, JComponent> map = makeComponents(c);
    Runnable r = r { saveData(c, map); module.change(); };
    showFormTitled("Edit " + shortClassName(cc) + " #" + c.id, arrayPlus(mapToObjectArray(map), r));
  }
  
  A selectedConcept() {
    ret (A) getConcept(toLong(selectedTableCell(table, 0)));
  }
  
  L<A> selectedConcepts() {
    int[] rows = table.getSelectedRows();
    new L<A> l;
    for (int row : rows)
      l.add((A) getConcept(toLong(getTableCell(table, row, 0))));
    ret l;
  }
  
  Map<S, JComponent> makeComponents(A c) {
    Map<S, JComponent> map = litorderedmap();
    makeComponents(c, map);
    ret map;
  }
  
  JComponent fieldComponent(A c, S field) {
    Class type = getFieldType(cc, field);
    O value = getOpt(c, field);
    //print("Field type: " + field + " => " + type);
    if (type == bool.class)
      ret jCenteredCheckBox(isTrue(value));
    else try {
      ret autoComboBox(structureOrText_crud(value), new TreeSet<S>(map structureOrText_crud(collect(list(cc), field))));
    } catch e {
      printException(e);
      ret jTextField(structureOrText_crud(value));
    }
  }
  
  void saveComponent(A c, S field, JComponent comp) {
    comp = unwrap(comp);
    if (comp instanceof JTextComponent)
      setOpt(c, field, convertToField(getTextTrim((JTextComponent) comp), cc, field));
    else if (comp instanceof JComboBox)
      setOpt(c, field, convertToField(getTextTrim((JComboBox) comp), cc, field));
    else if (comp instanceof JCheckBox)
      setOpt(c, field, isChecked((JCheckBox) comp));
    /*ifclass ImageChooser
      else if (comp instanceof ImageChooser)
        cUpdatePNGFile(c, field, comp/ImageChooser.getImage(), false);
    endif*/
  }
  
  L<S> fields() {
    if (fields != null) ret fields;
    ret fieldsInOrder(cc);
  }
  
  // override the following two methods to customize edit window
  
  void makeComponents(A c, Map<S, JComponent> map) {
    for (S field : fields())
      map.put(field, fieldComponent(c, field));
  }
  
  void saveData(A c, Map<S, JComponent> components) {
    for (S field : keys(components))
      saveComponent(c, field, components.get(field));
  }
}

Author comment

Began life as a copy of #1006540

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1019417
Snippet name: dm_SimpleCRUD [dev.] - create/read/update/delete for a list in a dynamic module
Eternal ID of this version: #1019417/4
Text MD5: ea40e16ff089881fb736c27839797661
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-06 17:09:20
Source code size: 3944 bytes / 124 lines
Pitched / IR pitched: No / No
Views / Downloads: 303 / 844
Version history: 3 change(s)
Referenced in: #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)