@Override public void init(Map<String, Object> params) { initBrowseItemChangeListener(); initBrowseCreateAction(); initBrowseEditAction(); initBrowseRemoveAction(); initShortcuts(); disableEditControls(); }
@Override protected void internalOpenEditor(CollectionDatasource datasource, Entity existingItem, Datasource parentDs, Map<String, Object> params) { refreshOptionsForLookupFields(); enableEditControls(false); }
/** * Returns the tab sheet with edit components. Can be null if the screen contains a field group only. * Override if the tab sheet id differs from "tabSheet". */ @Nullable protected TabSheet getTabSheet() { return (TabSheet) getComponent("tabSheet"); }
/** * Enables controls for editing. * @param creating indicates that a new instance is being created */ protected void enableEditControls(boolean creating) { this.editing = true; this.creating = creating; initEditComponents(true); getFieldGroup().requestFocus(); }
/** * Method that is invoked by clicking Ok button after editing an existing or creating a new record. */ @SuppressWarnings("unchecked") public void save() { if (!editing) return; FieldGroup fieldGroup = getFieldGroup(); List<Validatable> components = new ArrayList<>(); for (Component component: fieldGroup.getComponents()) { if (component instanceof Validatable) { components.add((Validatable)component); } } if (!validate(components)) { return; } getDsContext().commit(); ListComponent table = getTable(); CollectionDatasource browseDs = table.getDatasource(); Entity editedItem = fieldGroup.getDatasource().getItem(); if (creating) { browseDs.includeItem(editedItem); } else { browseDs.updateItem(editedItem); } table.setSelected(editedItem); releaseLock(); disableEditControls(); }
@SuppressWarnings("unchecked") @Override protected void internalOpenEditor(CollectionDatasource datasource, Entity newItem, Datasource parentDs, Map<String, Object> params) { initNewItem(newItem); table.setSelected(Collections.emptyList()); getFieldGroup().getDatasource().setItem(newItem); refreshOptionsForLookupFields(); enableEditControls(true); } });
/** * Initializes edit controls, depending on if they should be enabled or disabled. * @param enabled if true - enables edit controls and disables controls on the left side of the splitter * if false - vice versa */ protected void initEditComponents(boolean enabled) { TabSheet tabSheet = getTabSheet(); if (tabSheet != null) { ComponentsHelper.walkComponents(tabSheet, (component, name) -> { if (component instanceof FieldGroup) { ((FieldGroup) component).setEditable(enabled); } else if (component instanceof Table) { ((Table) component).getActions().forEach(action -> action.setEnabled(enabled)); } else if (!(component instanceof ComponentContainer)) { component.setEnabled(enabled); } }); } else { getFieldGroup().setEditable(enabled); } getActionsPane().setVisible(enabled); getLookupBox().setEnabled(!enabled); }
/** * Adds a listener that reloads the selected record with the specified view and sets it to editDs. */ @SuppressWarnings("unchecked") protected void initBrowseItemChangeListener() { CollectionDatasource browseDs = getTable().getDatasource(); Datasource editDs = getFieldGroup().getDatasource(); browseDs.addItemChangeListener(e -> { if (e.getItem() != null) { Entity reloadedItem = getDsContext().getDataSupplier().reload( e.getDs().getItem(), editDs.getView(), null, e.getDs().getLoadDynamicAttributes()); editDs.setItem(reloadedItem); } }); }
/** * Adds ESCAPE shortcut that invokes cancel() method. */ protected void initShortcuts() { ComponentContainer editBox = getEditBox(); if (editBox instanceof ShortcutNotifier) { ((ShortcutNotifier) editBox).addShortcutAction( new ShortcutAction(new KeyCombination(KeyCombination.Key.ESCAPE), shortcutTriggeredEvent -> cancel())); } }
/** * Adds AfterRemoveHandler for table's Remove action to reset the record contained in editDs. */ @SuppressWarnings("unchecked") protected void initBrowseRemoveAction() { ListComponent table = getTable(); Datasource editDs = getFieldGroup().getDatasource(); RemoveAction removeAction = (RemoveAction) table.getAction(RemoveAction.ACTION_ID); if (removeAction != null) removeAction.setAfterRemoveHandler(removedItems -> editDs.setItem(null)); }
/** * Release pessimistic lock if it was applied. */ protected void releaseLock() { if (justLocked) { Datasource ds = getFieldGroup().getDatasource(); Entity entity = ds.getItem(); if (entity != null) { MetaClass metaClass = getMetaClassForLocking(entity); AppBeans.get(LockService.class).unlock(metaClass.getName(), entity.getId().toString()); } } }
/** * Returns the browse table. Override if the table id differs from "table". */ protected ListComponent getTable() { return (ListComponent) getComponentNN("table"); }
protected void refreshOptionsForLookupFields() { for (Component component : getFieldGroup().getOwnComponents()) { if (component instanceof LookupField) { CollectionDatasource optionsDatasource = ((LookupField) component).getOptionsDatasource(); if (optionsDatasource != null) { optionsDatasource.refresh(); } } } }
/** * Method that is invoked by clicking Cancel button, discards changes and disables controls for editing. */ @SuppressWarnings("unchecked") public void cancel() { CollectionDatasource browseDs = getTable().getDatasource(); Datasource editDs = getFieldGroup().getDatasource(); Entity selectedItem = browseDs.getItem(); if (selectedItem != null) { Entity reloadedItem = getDsContext().getDataSupplier().reload( selectedItem, editDs.getView(), null, editDs.getLoadDynamicAttributes()); browseDs.setItem(reloadedItem); } else { editDs.setItem(null); } releaseLock(); disableEditControls(); } }
/** * Returns the right container with edit components. Override if the container id differs from "editBox". */ protected ComponentContainer getEditBox() { return (ComponentContainer) getComponentNN("editBox"); }
/** * Returns the field group. Override if the field group id differs from "fieldGroup". */ protected FieldGroup getFieldGroup() { return (FieldGroup) getComponentNN("fieldGroup"); }
/** * Returns the left container with browse components. Override if the container id differs from "lookupBox". */ protected ComponentContainer getLookupBox() { return (ComponentContainer) getComponentNN("lookupBox"); }
/** * Returns the container with edit actions (save, cancel). Override if the container id differs from "actionsPane". */ protected ComponentContainer getActionsPane() { return (ComponentContainer) getComponentNN("actionsPane"); }