|
| 1 | +package dev.ikm.komet.kview.mvvm.view.properties; |
| 2 | + |
| 3 | +import dev.ikm.komet.framework.view.ViewProperties; |
| 4 | +import dev.ikm.komet.kview.events.CreateConceptEvent; |
| 5 | +import dev.ikm.komet.kview.mvvm.model.DescriptionFormType; |
| 6 | +import dev.ikm.komet.kview.mvvm.viewmodel.OtherNameViewModel; |
| 7 | +import dev.ikm.tinkar.entity.ConceptEntity; |
| 8 | +import dev.ikm.tinkar.entity.Entity; |
| 9 | +import dev.ikm.tinkar.terms.EntityFacade; |
| 10 | +import dev.ikm.tinkar.terms.State; |
| 11 | +import dev.ikm.tinkar.terms.TinkarTerm; |
| 12 | +import javafx.beans.InvalidationListener; |
| 13 | +import javafx.scene.control.ComboBox; |
| 14 | +import org.carlfx.cognitive.loader.InjectViewModel; |
| 15 | +import org.carlfx.cognitive.viewmodel.ViewModel; |
| 16 | +import org.slf4j.Logger; |
| 17 | +import org.slf4j.LoggerFactory; |
| 18 | + |
| 19 | +import java.util.Collection; |
| 20 | +import java.util.UUID; |
| 21 | + |
| 22 | +import static dev.ikm.komet.kview.mvvm.viewmodel.DescrNameViewModel.*; |
| 23 | +import static dev.ikm.komet.kview.mvvm.viewmodel.DescrNameViewModel.CASE_SIGNIFICANCE; |
| 24 | +import static dev.ikm.komet.kview.mvvm.viewmodel.DescrNameViewModel.LANGUAGE; |
| 25 | +import static dev.ikm.komet.kview.mvvm.viewmodel.DescrNameViewModel.MODULE; |
| 26 | +import static dev.ikm.komet.kview.mvvm.viewmodel.OtherNameViewModel.OtherNameProperties.*; |
| 27 | + |
| 28 | +public class DescriptionAddOtherController extends DescriptionBaseController<EntityFacade>{ |
| 29 | + private static final Logger LOG = LoggerFactory.getLogger(DescriptionAddOtherController.class); |
| 30 | + @InjectViewModel |
| 31 | + private OtherNameViewModel otherNameViewModel; // TODO why we need that wrapper only for this class but can get away in all 3 others |
| 32 | + |
| 33 | + |
| 34 | + // TODO: how does it work with no UUID |
| 35 | + //public DescriptionAddOtherController() { |
| 36 | + // super(DescriptionFormType.ADD_OTHER_NAME, conceptTopic); |
| 37 | + //} |
| 38 | + |
| 39 | + public DescriptionAddOtherController(UUID conceptTopic) { |
| 40 | + super(DescriptionFormType.ADD_OTHER_NAME, conceptTopic); |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | + @Override |
| 45 | + protected void initializeData() { |
| 46 | + configureDialectVisibility(false); |
| 47 | + |
| 48 | + submitButton.setDisable(true); |
| 49 | + |
| 50 | + titleLabel.setText("Add New Description: Other Name"); // TODO: check if needed ? |
| 51 | + otherNameViewModel |
| 52 | + .setPropertyValue(NAME_TYPE, TinkarTerm.REGULAR_NAME_DESCRIPTION_TYPE) |
| 53 | + .setPropertyValue(STATUS, TinkarTerm.ACTIVE_STATE); |
| 54 | + // register listeners |
| 55 | + InvalidationListener formValid = (obs) -> { |
| 56 | + boolean isFormValid = isFormPopulated(); |
| 57 | + if (isFormValid) { |
| 58 | + copyUIToViewModelProperties(); |
| 59 | + } |
| 60 | + submitButton.setDisable(!isFormValid); |
| 61 | + }; |
| 62 | + |
| 63 | + nameTextField.textProperty().addListener(formValid); |
| 64 | + setupComboBoxAdd(moduleComboBox, formValid); |
| 65 | + setupComboBoxAdd(statusComboBox, formValid); |
| 66 | + setupComboBoxAdd(caseSignificanceComboBox, formValid); |
| 67 | + setupComboBoxAdd(languageComboBox, formValid); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void updateView() { |
| 72 | + // populate form combo fields module, status, case significance, lang. |
| 73 | + populate(moduleComboBox, otherNameViewModel.findAllModules(getViewProperties())); |
| 74 | + populate(statusComboBox, otherNameViewModel.findAllStatuses(getViewProperties())); |
| 75 | + populate(caseSignificanceComboBox, otherNameViewModel.findAllCaseSignificants(getViewProperties())); |
| 76 | + populate(languageComboBox, otherNameViewModel.findAllLanguages(getViewProperties())); |
| 77 | + |
| 78 | + boolean hasOtherName = getViewModel().getValue(HAS_OTHER_NAME); |
| 79 | + |
| 80 | + if (hasOtherName) { |
| 81 | + caseSignificanceComboBox.setValue(getViewModel().getValue(FQN_CASE_SIGNIFICANCE)); |
| 82 | + } else { |
| 83 | + caseSignificanceComboBox.setValue(TinkarTerm.DESCRIPTION_NOT_CASE_SENSITIVE); |
| 84 | + } |
| 85 | + statusComboBox.setValue(Entity.getFast(State.ACTIVE.nid())); |
| 86 | + moduleComboBox.setValue(TinkarTerm.DEVELOPMENT_MODULE); |
| 87 | + if (hasOtherName) { |
| 88 | + languageComboBox.setValue(getViewModel().getValue(FQN_LANGUAGE)); |
| 89 | + } else { |
| 90 | + languageComboBox.setValue(TinkarTerm.ENGLISH_LANGUAGE); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + public void updateModel(final ViewProperties viewProperties) { |
| 95 | + this.viewProperties = viewProperties; |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void clearView() { |
| 100 | + nameTextField.setText(""); |
| 101 | + moduleComboBox.setValue(null); |
| 102 | + statusComboBox.setValue(null); |
| 103 | + caseSignificanceComboBox.setValue(null); |
| 104 | + languageComboBox.setValue(null); |
| 105 | + moduleComboBox.getItems().clear(); |
| 106 | + statusComboBox.getItems().clear(); |
| 107 | + caseSignificanceComboBox.getItems().clear(); |
| 108 | + languageComboBox.getItems().clear(); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + protected void onCancel() { |
| 113 | + close(cancelButton); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + protected void onSubmit() { |
| 118 | + // TODO assuming it's valid save() check for errors and publish event |
| 119 | + otherNameViewModel.setPropertyValue(IS_SUBMITTED, true); |
| 120 | + otherNameViewModel.save(); |
| 121 | + if (otherNameViewModel.hasNoErrorMsgs()) { |
| 122 | + // publish event with the otherNameViewModel. |
| 123 | + // ... |
| 124 | + LOG.info("Ready to add to the concept view model: " + otherNameViewModel); |
| 125 | + eventBus.publish(conceptTopic, new CreateConceptEvent(this, CreateConceptEvent.ADD_OTHER_NAME, |
| 126 | + otherNameViewModel.create())); |
| 127 | + clearView(); |
| 128 | + close(submitButton); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public OtherNameViewModel getViewModel() { |
| 134 | + return otherNameViewModel; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * TODO: This is appropriate. A better solution is binding properties on the view model. If so, we'd need to unbind. |
| 139 | + * This copies form values into the ViewModel's property values. It does not save or validate. |
| 140 | + */ |
| 141 | + private void copyUIToViewModelProperties() { |
| 142 | + if (otherNameViewModel != null) { |
| 143 | + otherNameViewModel.setPropertyValue(NAME_TEXT, nameTextField.getText()) |
| 144 | + .setPropertyValue(NAME_TYPE, TinkarTerm.REGULAR_NAME_DESCRIPTION_TYPE) |
| 145 | + .setPropertyValue(CASE_SIGNIFICANCE, caseSignificanceComboBox.getSelectionModel().getSelectedItem()) |
| 146 | + .setPropertyValue(STATUS, statusComboBox.getSelectionModel().getSelectedItem()) |
| 147 | + .setPropertyValue(MODULE, moduleComboBox.getSelectionModel().getSelectedItem()) |
| 148 | + .setPropertyValue(LANGUAGE, languageComboBox.getSelectionModel().getSelectedItem()); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + public void setAddOtherNameTitleLabel(String addDescriptionTitleLabelText) { |
| 153 | + this.titleLabel.setText(addDescriptionTitleLabelText); |
| 154 | + } |
| 155 | + |
| 156 | + public void populate(ComboBox comboBox, Collection<ConceptEntity> entities) { |
| 157 | + comboBox.getItems().addAll(entities); |
| 158 | + } |
| 159 | + |
| 160 | + public void hideNonUsed() { |
| 161 | + dialect1.setVisible(false); |
| 162 | + dialectComboBox1.setVisible(false); |
| 163 | + |
| 164 | + } |
| 165 | +} |
0 commit comments