From cf5bbc8bdd243511719138828798c93fc22e615b Mon Sep 17 00:00:00 2001 From: Nicolas Roduit Date: Fri, 21 Jun 2024 10:45:53 +0200 Subject: [PATCH] Dicomizer: Limit the number of characters in metadata table according to the DICOM attributes #564 --- .../meta/panel/AcquireMetadataPanel.java | 15 ++++--- .../core/ui/editor/image/DefaultView2d.java | 2 +- .../weasis/core/ui/util/LimitedTextField.java | 40 +++++++++++++++++++ .../core/ui/util/TableColumnAdjuster.java | 2 +- .../java/org/weasis/dicom/codec/TagD.java | 22 ++++++++++ 5 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 weasis-core/src/main/java/org/weasis/core/ui/util/LimitedTextField.java diff --git a/weasis-acquire/weasis-acquire-explorer/src/main/java/org/weasis/acquire/explorer/gui/central/meta/panel/AcquireMetadataPanel.java b/weasis-acquire/weasis-acquire-explorer/src/main/java/org/weasis/acquire/explorer/gui/central/meta/panel/AcquireMetadataPanel.java index 3b04c8541..6c1a6e1a7 100755 --- a/weasis-acquire/weasis-acquire-explorer/src/main/java/org/weasis/acquire/explorer/gui/central/meta/panel/AcquireMetadataPanel.java +++ b/weasis-acquire/weasis-acquire-explorer/src/main/java/org/weasis/acquire/explorer/gui/central/meta/panel/AcquireMetadataPanel.java @@ -52,6 +52,7 @@ import org.weasis.core.api.util.FontItem; import org.weasis.core.api.util.ResourceUtil; import org.weasis.core.ui.util.CalendarUtil; +import org.weasis.core.ui.util.LimitedTextField; import org.weasis.core.ui.util.TableColumnAdjuster; import org.weasis.core.util.StringUtil; import org.weasis.dicom.codec.TagD; @@ -213,11 +214,15 @@ public Component getTableCellEditorComponent( int tagID = 0; boolean date = false; boolean time = false; + int limitedChars = 64; if (tag instanceof TagW tagW) { tagID = tagW.getId(); TagType type = tagW.getType(); date = TagType.DICOM_DATE == type || TagType.DATE == type; time = TagType.DICOM_TIME == type || TagType.TIME == type; + if (tag instanceof TagD tagD) { + limitedChars = tagD.getMaximumChars(); + } } if (tagID == Tag.BodyPartExamined) { cellEditor = new DefaultCellEditor(bodyPartsCombo); @@ -226,9 +231,9 @@ public Component getTableCellEditorComponent( } else if (tagID == Tag.Modality) { cellEditor = new DefaultCellEditor(modalityCombo); } else if (tagID == Tag.StudyDescription) { - cellEditor = getCellEditor(studyDescCombo); + cellEditor = getCellEditor(studyDescCombo, limitedChars); } else if (tagID == Tag.SeriesDescription) { - cellEditor = getCellEditor(seriesDescCombo); + cellEditor = getCellEditor(seriesDescCombo, limitedChars); } else if (date) { DateTableEditor datePicker = buildDatePicker(); JTextField picker = datePicker.getDatePicker().getComponentDateTextField(); @@ -252,7 +257,7 @@ public Component getTableCellEditorComponent( tableEditor.getTimePicker().getComponentTimeTextField(), height, height); cellEditor = tableEditor; } else { - cellEditor = new DefaultCellEditor(new JTextField()); + cellEditor = new DefaultCellEditor(new LimitedTextField(limitedChars)); } editor = Optional.of(cellEditor); Component c = cellEditor.getTableCellEditorComponent(table, value, isSelected, row, column); @@ -260,9 +265,9 @@ public Component getTableCellEditorComponent( return c; } - private static DefaultCellEditor getCellEditor(JComboBox combo) { + private static DefaultCellEditor getCellEditor(JComboBox combo, int limitedChars) { if (combo.getItemCount() == 0) { - return new DefaultCellEditor(new JTextField()); + return new DefaultCellEditor(new LimitedTextField(limitedChars)); } else { return new DefaultCellEditor(combo); } diff --git a/weasis-core/src/main/java/org/weasis/core/ui/editor/image/DefaultView2d.java b/weasis-core/src/main/java/org/weasis/core/ui/editor/image/DefaultView2d.java index 95e329eb8..29ba0d47a 100755 --- a/weasis-core/src/main/java/org/weasis/core/ui/editor/image/DefaultView2d.java +++ b/weasis-core/src/main/java/org/weasis/core/ui/editor/image/DefaultView2d.java @@ -1191,7 +1191,7 @@ public List getExportActions() { new DefaultAction( ActionW.EXPORT_VIEW.getTitle(), ActionW.EXPORT_VIEW.getIcon(), - event -> ScreenshotDialog.showDialog(this)); + _ -> ScreenshotDialog.showDialog(this)); list.add(exportAction); return list; } diff --git a/weasis-core/src/main/java/org/weasis/core/ui/util/LimitedTextField.java b/weasis-core/src/main/java/org/weasis/core/ui/util/LimitedTextField.java new file mode 100644 index 000000000..4151d4b0f --- /dev/null +++ b/weasis-core/src/main/java/org/weasis/core/ui/util/LimitedTextField.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2024 Weasis Team and other contributors. + * + * This program and the accompanying materials are made available under the terms of the Eclipse + * Public License 2.0 which is available at https://www.eclipse.org/legal/epl-2.0, or the Apache + * License, Version 2.0 which is available at https://www.apache.org/licenses/LICENSE-2.0. + * + * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 + */ +package org.weasis.core.ui.util; + +import javax.swing.*; +import javax.swing.text.*; + +public class LimitedTextField extends JTextField { + public LimitedTextField(int limit) { + super(); + setDocument(new LimitedDocument(limit)); + } + + private static class LimitedDocument extends PlainDocument { + private final int limit; + + LimitedDocument(int limit) { + this.limit = limit; + } + + @Override + public void insertString(int offset, String str, AttributeSet attr) + throws BadLocationException { + if (str == null) { + return; + } + + if ((getLength() + str.length()) <= limit) { + super.insertString(offset, str, attr); + } + } + } +} diff --git a/weasis-core/src/main/java/org/weasis/core/ui/util/TableColumnAdjuster.java b/weasis-core/src/main/java/org/weasis/core/ui/util/TableColumnAdjuster.java index 3a1653f6e..dc680ab3c 100755 --- a/weasis-core/src/main/java/org/weasis/core/ui/util/TableColumnAdjuster.java +++ b/weasis-core/src/main/java/org/weasis/core/ui/util/TableColumnAdjuster.java @@ -75,7 +75,7 @@ public static void pack(JTable table) { } int extra = table.getVisibleRect().width - total; - if (extra > 0) { + if (extra > 0 && width.length > 0 && table.getColumnCount() > 0) { int bonus = extra / table.getColumnCount(); for (int i = 0; i < width.length; i++) { width[i] += bonus; diff --git a/weasis-dicom/weasis-dicom-codec/src/main/java/org/weasis/dicom/codec/TagD.java b/weasis-dicom/weasis-dicom-codec/src/main/java/org/weasis/dicom/codec/TagD.java index a54cb67fe..32c618a38 100755 --- a/weasis-dicom/weasis-dicom-codec/src/main/java/org/weasis/dicom/codec/TagD.java +++ b/weasis-dicom/weasis-dicom-codec/src/main/java/org/weasis/dicom/codec/TagD.java @@ -60,6 +60,24 @@ public class TagD extends TagW { private static final Logger LOGGER = LoggerFactory.getLogger(TagD.class); + private static final Map vrToMaxChars = new HashMap<>(); + + static { + vrToMaxChars.put(VR.AE, 16); + vrToMaxChars.put(VR.AS, 4); + vrToMaxChars.put(VR.CS, 16); + vrToMaxChars.put(VR.DA, 8); + vrToMaxChars.put(VR.DS, 16); + vrToMaxChars.put(VR.DT, 26); + vrToMaxChars.put(VR.IS, 12); + vrToMaxChars.put(VR.LO, 64); + vrToMaxChars.put(VR.LT, 10240); + vrToMaxChars.put(VR.PN, 129); + vrToMaxChars.put(VR.SH, 16); + vrToMaxChars.put(VR.ST, 1024); + vrToMaxChars.put(VR.UI, 64); + } + static final DateTimeFormatter DICOM_DATE = new DateTimeFormatterBuilder() .appendValue(YEAR, 4) @@ -243,6 +261,10 @@ public int getDicomValueMultiplicity(Object value) { return getValueMultiplicity(value); } + public int getMaximumChars() { + return vrToMaxChars.getOrDefault(vr, 64); + } + @Override public int hashCode() { final int prime = 31;