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)

sbool SimpleCRUD_searcher = true;

sclass SimpleCRUD<A extends Concept> {
  Class<A> cc;
  JTable table;
  JPanel buttons, panel;
  O renderer; // optional, func(A) -> Map
  S hID = "ID";
  Set<S> excludeFieldsFromEditing;

  *(Class<A> *cc) {}
  
  SimpleCRUD<A> show(S frameTitle) {
    make();
    showFrame(frameTitle, panel);
    this;
  }
  
  SimpleCRUD<A> show() {
    ret show(plural(shortClassName(cc)));
  }
  
  SimpleCRUD<A> showMaximized() { show(); maximizeFrame(panel); ret this; }
  
  JPanel make() {
    db();
    framesBot();
    // next line not in swing part to allow passing arguments
    // from outside like showConceptsTable_afterUpdate
    table = makeConceptsTable(cc, wrapRenderer(renderer));
    swing {
      panel = centerAndSouthWithMargin(SimpleCRUD_searcher ? tableWithSearcher(table, withMargin := true) : 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 = unlisted(cc);
    final Map<S, JComponent> map = makeComponents(c);
    Runnable r = r {
      register(c);
      saveData(c, map);
    };
    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) };
    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)
      cset(c, field, convertToField(getTextTrim((JTextComponent) comp), cc, field));
    else if (comp instanceof JComboBox)
      cset(c, field, convertToField(getTextTrim((JComboBox) comp), cc, field));
    else if (comp instanceof JCheckBox)
      cset(c, field, isChecked((JCheckBox) comp));
    ifclass ImageChooser
      else if (comp instanceof ImageChooser)
        cUpdatePNGFile(c, field, comp/ImageChooser.getImage(), false);
    endif
  }
  
  L<S> fields() {
    ret listWithoutSet([S field : conceptFieldsInOrder(cc) | fieldType(cc, field) != Concept.Ref.class], excludeFieldsFromEditing);
  }
  
  // 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 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: 261 / 280
Referenced in: [show references]