/** Refresh fields with values from tempModel or editedModel. */ private void refreshDisplay() { Session s = Session.current; Model tmp = s.tempModel; int structIndex = -1; for (int j = 0; j < this.table.getChildCount(); j++) { structIndex++; View v = this.table.getChildAt(j); if (!FormViewFactory.isFieldView(v)) { // Check if it is a x2many label Model field = this.view.getStructure().get(structIndex); if (field.hasAttribute("type")) { String type = field.getString("type"); if (type.equals("many2many") || type.equals("one2many")) { // This is the label of a x2many field // It is not present in structure and next // will be the true widget. // Get back in structure for next pass // to point on the x2many field (and not next one) structIndex--; } } continue; } Model field = this.view.getStructure().get(structIndex); FormViewFactory.setValue(v, field, this.view, tmp, s.editedModel, s.prefs, this); } }
/** Update temp model to current values */ private void updateTempModel() { Model tmp; Model origin; tmp = Session.current.tempModel; origin = Session.current.editedModel; int structIndex = -1; for (int j = 0; j < this.table.getChildCount(); j++) { structIndex++; View v = this.table.getChildAt(j); if (!FormViewFactory.isFieldView(v)) { // Check if it is a x2many label Model field = this.view.getStructure().get(structIndex); if (field.hasAttribute("type")) { String type = field.getString("type"); if (type.equals("many2many") || type.equals("one2many")) { // This is the label of a x2many field // It is not present in structure and next // will be the true widget. // Get back in structure for next pass // to point on the x2many field (and not next one) structIndex--; } } // Ignore continue; } Model field = this.view.getStructure().get(structIndex); Object value = FormViewFactory.getValue(v, field, Session.current.prefs); if (value != FormViewFactory.NO_VALUE) { tmp.set(field.getString("name"), value); } else { // If NO_VALUE (not null) the value is ignored // but still set null to create the field if necessary String fieldName = field.getString("name"); if (origin != null) { // Origin always has a value, it will be merged } else { // Origin doesn't have a value, set explicit null if (!tmp.hasAttribute(fieldName)) { tmp.set(fieldName, null); } } } } }
/** Update rec_name locally as it is computed on the server side. */ private void updateRecName(Model model) { if (model.hasAttribute("name")) { model.set("rec_name", model.get("name")); } else { for (String key : model.getAttributeNames()) { if (model.get(key) instanceof String) { model.set("rec_name", model.get(key)); break; } } } }
/** * Set session to edit a one2many subrecord. ParentField is the name of the field that links to * new model. */ public void editOne2Many(Model data, String parentField, String childField) { this.tempModel = new Model(data.getClassName()); if (data.hasAttribute("id")) { // When editing a new one2many record, the record has no id this.tempModel.set("id", data.get("id")); } if (this.editedModel != null && this.editedModel.hasAttribute("id")) { this.tempModel.set(childField, (Integer) this.editedModel.get("id")); } this.editedModel = data; this.linkToParent = parentField; this.linkToSelf = childField; this.pushStack(); }
private void initView() { for (Model view : this.view.getStructure()) { Session s = Session.current; if (view.hasAttribute("type") && (view.getString("type").equals("many2many") || view.getString("type").equals("one2many"))) { // Special case of x2many: they embed their own title // Thus occupies a full line TextView label = new TextView(this); if (view.hasAttribute("string")) { label.setText(view.getString("string")); } else { label.setText(view.getString("name")); } this.table.addView(label); } View v = FormViewFactory.getView(view, this.view, s.editedModel, s.prefs, this); if (view.hasAttribute("name") && view.getString("name").equals(Session.current.linkToSelf)) { // Hide many2one parent field v.setVisibility(View.GONE); } this.table.addView(v); } }