diff --git a/src/main/java/org/isf/medicalinventory/gui/InventoryEdit.java b/src/main/java/org/isf/medicalinventory/gui/InventoryEdit.java index fd96f6dfb2..73a6c8c691 100644 --- a/src/main/java/org/isf/medicalinventory/gui/InventoryEdit.java +++ b/src/main/java/org/isf/medicalinventory/gui/InventoryEdit.java @@ -527,16 +527,18 @@ private JButton getSelectedButton() { radioGroup.add(getAllRadioButton()); radioGroup.add(getMedicalWithNonZeroQuantityRadioButton()); radioGroup.add(getMedicalWithMovementRadioButton()); - + // Map actions to buttons initializeActions(); - // Convertir la HashMap en TreeMap pour trier les clés + + // Convert HashMap to TreeMap to sort keys Map sortedActionMap = new TreeMap<>(Comparator.comparing(AbstractButton::getText)); sortedActionMap.putAll(actions); + // Add ActionListener to each button - sortedActionMap.forEach((key, value) -> { - rightPanel.add(key); - }); + sortedActionMap.forEach((key, value) -> { + rightPanel.add(key); + }); JPanel bottomPanel = new JPanel(); bottomPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); @@ -798,7 +800,6 @@ private JButton getDeleteButton() { } } jTableInventoryRow.clearSelection(); - adjustWidth(); } else { return; } @@ -848,7 +849,7 @@ private JButton getLotButton() { code = lot.getCode(); if (selectedInventoryRow.getLot() == null) { List invRows = inventoryRowSearchList.stream() - .filter(inv -> inv.getLot() != null && inv.getLot().getCode().equals(code)).collect(Collectors.toList()); + .filter(inv -> inv.getLot() != null && inv.getLot().getCode().equals(code)).collect(Collectors.toList()); if (invRows.isEmpty() || code.equals("")) { selectedInventoryRow.setNewLot(true); selectedInventoryRow.setLot(lot); @@ -864,7 +865,7 @@ private JButton getLotButton() { } } else { List invRows = inventoryRowSearchList.stream() - .filter(inv -> inv.getMedical().getCode().equals(selectedInventoryRow.getMedical().getCode())).collect(Collectors.toList()); + .filter(inv -> inv.getMedical().getCode().equals(selectedInventoryRow.getMedical().getCode())).collect(Collectors.toList()); invRows = invRows.stream().filter(inv -> inv.getLot() != null && inv.getLot().getCode().equals(code)).collect(Collectors.toList()); if (invRows.isEmpty() || code.equals("")) { selectedInventoryRow.setNewLot(true); @@ -963,7 +964,6 @@ private JButton getCleanTableButton() { model.setRowCount(0); model.setColumnCount(0); jTableInventoryRow.updateUI(); - adjustWidth(); } }); return resetButton; @@ -994,7 +994,7 @@ private JButton getValidateButton() { String lastReference = inventory.getInventoryReference(); LocalDateTime lastDate = inventory.getInventoryDate(); if (checkParameters(wardCode, chargeCode, dischargeCode, supplierId, lastReference, lastDate) || !lotsSaved.isEmpty() - || !inventoryRowListAdded.isEmpty() || !inventoryRowsToDelete.isEmpty()) { + || !inventoryRowListAdded.isEmpty() || !inventoryRowsToDelete.isEmpty()) { saveButton.doClick(); chargeCode = inventory.getChargeType(); dischargeCode = inventory.getDischargeType(); @@ -1032,7 +1032,6 @@ private JButton getValidateButton() { confirmButton.setEnabled(true); jTableInventoryRow.setModel(new InventoryRowModel()); fireInventoryUpdated(); - adjustWidth(); } catch (OHServiceException e1) { OHServiceExceptionUtil.showMessages(e1); } @@ -1122,6 +1121,7 @@ private JTable getJTableInventoryRow() throws OHServiceException { jTableInventoryRow.setFillsViewportHeight(true); model = new InventoryRowModel(); jTableInventoryRow.setModel(model); + jTableInventoryRow.setAutoCreateColumnsFromModel(false); for (int i = 0; i < pColumnVisible.length; i++) { jTableInventoryRow.getColumnModel().getColumn(i).setCellRenderer(new EnabledTableCellRenderer()); jTableInventoryRow.getColumnModel().getColumn(i).setPreferredWidth(pColumwidth[i]); @@ -1200,6 +1200,7 @@ public InventoryRowModel(boolean add) throws OHServiceException { } public InventoryRowModel() throws OHServiceException { + inventoryRowList.clear(); inventoryRowSearchList.clear(); if (inventory != null) { inventoryRowList = medicalInventoryRowManager.getMedicalInventoryRowByInventoryId(inventory.getId()); @@ -1378,20 +1379,6 @@ public boolean isCellEditable(int rowIndex, int columnIndex) { } } - private void adjustWidth() { - for (int i = 0; i < jTableInventoryRow.getColumnModel().getColumnCount(); i++) { - jTableInventoryRow.getColumnModel().getColumn(i).setPreferredWidth(pColumwidth[i]); - if (i == 0 || !pColumnVisible[i]) { - jTableInventoryRow.getColumnModel().getColumn(i).setMinWidth(0); - jTableInventoryRow.getColumnModel().getColumn(i).setMaxWidth(0); - jTableInventoryRow.getColumnModel().getColumn(i).setPreferredWidth(0); - } - } - DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer(); - centerRenderer.setHorizontalAlignment(JLabel.CENTER); - jTableInventoryRow.getColumnModel().getColumn(3).setCellRenderer(centerRenderer); - } - private Lot getLot(Lot lotToUpdate) throws OHServiceException { Lot lot = null; if (isAutomaticLotIn()) { @@ -1443,10 +1430,10 @@ private Lot askLot(Lot lotToUpdate) { panel.add(expireDateChooser); do { int ok = JOptionPane.showConfirmDialog( - this, - panel, - MessageBundle.getMessage("angal.medicalstock.multiplecharging.lotinformations"), - JOptionPane.OK_CANCEL_OPTION); + this, + panel, + MessageBundle.getMessage("angal.medicalstock.multiplecharging.lotinformations"), + JOptionPane.OK_CANCEL_OPTION); if (ok == JOptionPane.OK_OPTION) { String lotName = lotNameTextField.getText(); @@ -1481,8 +1468,8 @@ private BigDecimal askCost(int qty, BigDecimal lastCost) { double cost = 0.; do { String input = JOptionPane.showInputDialog(this, - MessageBundle.getMessage("angal.medicalstock.multiplecharging.unitcost"), - lastCost); + MessageBundle.getMessage("angal.medicalstock.multiplecharging.unitcost"), + lastCost); if (input != null) { try { cost = Double.parseDouble(input); @@ -1510,8 +1497,8 @@ protected LocalDateTime askExpiringDate() { panel.add(expireDateChooser); int ok = JOptionPane.showConfirmDialog(this, panel, - MessageBundle.getMessage("angal.medicalstock.multiplecharging.expiringdate"), - JOptionPane.OK_CANCEL_OPTION); + MessageBundle.getMessage("angal.medicalstock.multiplecharging.expiringdate"), + JOptionPane.OK_CANCEL_OPTION); if (ok == JOptionPane.OK_OPTION) { date = expireDateChooser.getLocalDateTime(); @@ -1521,8 +1508,8 @@ protected LocalDateTime askExpiringDate() { protected double askTotalCost() { String input = JOptionPane.showInputDialog(this, - MessageBundle.getMessage("angal.medicalstock.multiplecharging.totalcost"), - 0.); + MessageBundle.getMessage("angal.medicalstock.multiplecharging.totalcost"), + 0.); double total = 0.; if (input != null) { try { @@ -2035,7 +2022,7 @@ private int getPosition(MedicalInventoryRow inventoryRow) { private boolean existInInventorySearchList(MedicalInventoryRow inventoryRow) { boolean found = false; List invRows = inventoryRowSearchList.stream() - .filter(inv -> inv.getMedical().getCode().equals(inventoryRow.getMedical().getCode())).collect(Collectors.toList()); + .filter(inv -> inv.getMedical().getCode().equals(inventoryRow.getMedical().getCode())).collect(Collectors.toList()); if (!invRows.isEmpty()) { for (MedicalInventoryRow invR : invRows) { if (inventoryRow.getLot() != null && invR.getLot() != null) { @@ -2054,7 +2041,7 @@ private boolean existInInventorySearchList(MedicalInventoryRow inventoryRow) { return found; } - + private void addMedInRowInInventorySearchList(MedicalInventoryRow inventoryRow) { int position = getPosition(inventoryRow); if (position == -1) { @@ -2067,7 +2054,7 @@ private void addMedInRowInInventorySearchList(MedicalInventoryRow inventoryRow) inventoryRowListAdded.add(inventoryRow); } } - + private void resetVariable() { inventoryRowsToDelete.clear(); lotsDeleted.clear(); @@ -2077,13 +2064,13 @@ private void resetVariable() { private boolean checkParameters(String wardCode, String chargeCode, String dischargeCode, Integer suplierId, String reference, LocalDateTime date) { if (!lotsSaved.isEmpty() || !inventoryRowListAdded.isEmpty() || !lotsDeleted.isEmpty() || !inventoryRowsToDelete.isEmpty() - || (destination != null && !destination.getCode().equals(wardCode)) - || (chargeType != null && !chargeType.getCode().equals(chargeCode)) - || (dischargeType != null && !dischargeType.getCode().equals(dischargeCode)) - || (supplier != null && supplier.getSupId() != suplierId) || (destination == null && wardCode != null) - || (chargeType == null && chargeCode != null) || (dischargeType == null && dischargeCode != null) - || (supplier == null && suplierId != null) || (reference != null && !reference.equals(newReference)) - || !date.toLocalDate().equals(dateInventory.toLocalDate())) { + || (destination != null && !destination.getCode().equals(wardCode)) + || (chargeType != null && !chargeType.getCode().equals(chargeCode)) + || (dischargeType != null && !dischargeType.getCode().equals(dischargeCode)) + || (supplier != null && supplier.getSupId() != suplierId) || (destination == null && wardCode != null) + || (chargeType == null && chargeCode != null) || (dischargeType == null && dischargeCode != null) + || (supplier == null && suplierId != null) || (reference != null && !reference.equals(newReference)) + || !date.toLocalDate().equals(dateInventory.toLocalDate())) { return true; } return false; @@ -2140,7 +2127,7 @@ private JButton getCancelButton() { jButtonCancel.addActionListener(actionEvent -> frame.dispose()); return jButtonCancel; } - + private List loadNewInventoryTable(boolean withNonZeroQty, MedicalType medicalTypeSelected) throws OHServiceException { List inventoryRowsList = getMedicalInventoryRows(null); if (withNonZeroQty) { @@ -2148,8 +2135,8 @@ private List loadNewInventoryTable(boolean withNonZeroQty, } if (medicalTypeSelected != null) { inventoryRowsList = inventoryRowsList.stream() - .filter(inv -> inv.getMedical().getType().getDescription().equals(medicalTypeSelected.getDescription())) - .collect(Collectors.toList()); + .filter(inv -> inv.getMedical().getType().getDescription().equals(medicalTypeSelected.getDescription())) + .collect(Collectors.toList()); } return inventoryRowsList; } @@ -2158,8 +2145,8 @@ private List loadNewInventoryTable(MedicalType medicalTypeS List inventoryRowsList = getMedicalInventoryRowsWithMovement(); if (medicalTypeSelected != null) { inventoryRowsList = inventoryRowsList.stream() - .filter(inv -> inv.getMedical().getType().getDescription().equals(medicalTypeSelected.getDescription())) - .collect(Collectors.toList()); + .filter(inv -> inv.getMedical().getType().getDescription().equals(medicalTypeSelected.getDescription())) + .collect(Collectors.toList()); } return inventoryRowsList; } @@ -2170,16 +2157,16 @@ private JButton getOkButton() { jButtonOk.setMnemonic(MessageBundle.getMnemonic("angal.common.ok.btn.key")); jButtonOk.addActionListener(actionEvent -> { actions.forEach((key, value) -> { - if (key.isSelected()) { - actions.get(key).run(); - } - }); + if (key.isSelected()) { + actions.get(key).run(); + } + }); jButtonCancel.doClick(); }); } return jButtonOk; } - + private boolean areAllMedicalsInInventory() throws OHServiceException { Set inventorySet = new HashSet<>(); for (MedicalInventoryRow row : inventoryRowSearchList) { @@ -2190,18 +2177,19 @@ private boolean areAllMedicalsInInventory() throws OHServiceException { } return false; } - + private void initializeActions() { actions.put(radioButtonAll, () -> { if (medicalTypeSelected.getDescription().equals(MessageBundle.getMessage("angal.common.all.txt"))) { try { if (!areAllMedicalsInInventory()) { - int info = (!inventoryRowSearchList.isEmpty()) ? MessageDialog.yesNo(null, "angal.inventoryrow.doyouwanttoaddallnotyetlistedproducts.msg") : JOptionPane.YES_OPTION; + int info = (!inventoryRowSearchList.isEmpty()) + ? MessageDialog.yesNo(null, "angal.inventoryrow.doyouwanttoaddallnotyetlistedproducts.msg") + : JOptionPane.YES_OPTION; if (info == JOptionPane.YES_OPTION) { jTableInventoryRow.setModel(new InventoryRowModel(true)); } fireInventoryUpdated(); - adjustWidth(); MessageDialog.info(null, "angal.invetory.allmedicaladdedsuccessfully.msg"); } else { MessageDialog.info(null, "angal.inventory.youhavealreadyaddedallproduct.msg"); @@ -2212,13 +2200,14 @@ private void initializeActions() { } else { try { if (!areAllMedicalsInInventory()) { - int info = (!inventoryRowSearchList.isEmpty()) ? MessageDialog.yesNo(null, "angal.inventoryrow.doyouwanttoaddallnotyetlistedproducts.msg") : JOptionPane.YES_OPTION; + int info = (!inventoryRowSearchList.isEmpty()) + ? MessageDialog.yesNo(null, "angal.inventoryrow.doyouwanttoaddallnotyetlistedproducts.msg") + : JOptionPane.YES_OPTION; if (info == JOptionPane.YES_OPTION) { - MedicalType medType = (MedicalType)medicalTypeComboBox.getSelectedItem(); + MedicalType medType = (MedicalType) medicalTypeComboBox.getSelectedItem(); jTableInventoryRow.setModel(new InventoryRowModel(medType)); } fireInventoryUpdated(); - adjustWidth(); MessageDialog.info(null, "angal.invetory.tablehasbeenupdated.msg"); } else { MessageDialog.info(null, "angal.inventory.youhavealreadyaddedallproduct.msg"); @@ -2228,17 +2217,18 @@ private void initializeActions() { } } }); - + actions.put(radioOnlyNonZero, () -> { if (medicalTypeSelected.getDescription().equals(MessageBundle.getMessage("angal.common.all.txt"))) { try { if (!areAllMedicalsInInventory()) { - int info = (!inventoryRowSearchList.isEmpty()) ? MessageDialog.yesNo(null, "angal.inventoryrow.doyouwanttoaddallnotyetlistedproducts.msg") : JOptionPane.YES_OPTION; + int info = (!inventoryRowSearchList.isEmpty()) + ? MessageDialog.yesNo(null, "angal.inventoryrow.doyouwanttoaddallnotyetlistedproducts.msg") + : JOptionPane.YES_OPTION; if (info == JOptionPane.YES_OPTION) { jTableInventoryRow.setModel(new InventoryRowModel(true, null)); } fireInventoryUpdated(); - adjustWidth(); if (!inventoryRowList.isEmpty()) { MessageDialog.info(null, "angal.invetory.tablehasbeenupdated.msg"); } @@ -2251,16 +2241,17 @@ private void initializeActions() { } else { try { if (!areAllMedicalsInInventory()) { - int info = (!inventoryRowSearchList.isEmpty()) ? MessageDialog.yesNo(null, "angal.inventoryrow.doyouwanttoaddallnotyetlistedproducts.msg") : JOptionPane.YES_OPTION; + int info = (!inventoryRowSearchList.isEmpty()) + ? MessageDialog.yesNo(null, "angal.inventoryrow.doyouwanttoaddallnotyetlistedproducts.msg") + : JOptionPane.YES_OPTION; if (info == JOptionPane.YES_OPTION) { - MedicalType medType = (MedicalType)medicalTypeComboBox.getSelectedItem(); + MedicalType medType = (MedicalType) medicalTypeComboBox.getSelectedItem(); jTableInventoryRow.setModel(new InventoryRowModel(true, medType)); fireInventoryUpdated(); - adjustWidth(); if (!inventoryRowList.isEmpty()) { MessageDialog.info(null, "angal.invetory.tablehasbeenupdated.msg"); } - } + } } else { MessageDialog.info(null, "angal.inventory.youhavealreadyaddedallproduct.msg"); } @@ -2269,16 +2260,17 @@ private void initializeActions() { } } }); - + actions.put(radioWithMovement, () -> { if (medicalTypeSelected.getDescription().equals(MessageBundle.getMessage("angal.common.all.txt"))) { try { if (!areAllMedicalsInInventory()) { - int info = (!inventoryRowSearchList.isEmpty()) ? MessageDialog.yesNo(null, "angal.inventoryrow.doyouwanttoaddallnotyetlistedproducts.msg") : JOptionPane.YES_OPTION; + int info = (!inventoryRowSearchList.isEmpty()) + ? MessageDialog.yesNo(null, "angal.inventoryrow.doyouwanttoaddallnotyetlistedproducts.msg") + : JOptionPane.YES_OPTION; if (info == JOptionPane.YES_OPTION) { jTableInventoryRow.setModel(new InventoryRowModel(null, true)); fireInventoryUpdated(); - adjustWidth(); if (!inventoryRowList.isEmpty()) { MessageDialog.info(null, "angal.invetory.tablehasbeenupdated.msg"); } @@ -2292,16 +2284,17 @@ private void initializeActions() { } else { try { if (!areAllMedicalsInInventory()) { - int info = (!inventoryRowSearchList.isEmpty()) ? MessageDialog.yesNo(null, "angal.inventoryrow.doyouwanttoaddallnotyetlistedproducts.msg") : JOptionPane.YES_OPTION; + int info = (!inventoryRowSearchList.isEmpty()) + ? MessageDialog.yesNo(null, "angal.inventoryrow.doyouwanttoaddallnotyetlistedproducts.msg") + : JOptionPane.YES_OPTION; if (info == JOptionPane.YES_OPTION) { - MedicalType medType = (MedicalType)medicalTypeComboBox.getSelectedItem(); + MedicalType medType = (MedicalType) medicalTypeComboBox.getSelectedItem(); jTableInventoryRow.setModel(new InventoryRowModel(medType, true)); fireInventoryUpdated(); - adjustWidth(); if (!inventoryRowList.isEmpty()) { MessageDialog.info(null, "angal.invetory.tablehasbeenupdated.msg"); } - } + } } else { MessageDialog.info(null, "angal.inventory.youhavealreadyaddedallproduct.msg"); }