Skip to content

Commit

Permalink
Dicomizer: Limit the number of characters in metadata table according…
Browse files Browse the repository at this point in the history
… to the DICOM attributes nroduit#564
  • Loading branch information
nroduit committed Jun 22, 2024
1 parent 74a99c6 commit cf5bbc8
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -252,17 +257,17 @@ 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);
c.setFont(SMALL_FONT);
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1191,7 +1191,7 @@ public List<Action> getExportActions() {
new DefaultAction(
ActionW.EXPORT_VIEW.getTitle(),
ActionW.EXPORT_VIEW.getIcon(),
event -> ScreenshotDialog.showDialog(this));
_ -> ScreenshotDialog.showDialog(this));
list.add(exportAction);
return list;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@ public class TagD extends TagW {

private static final Logger LOGGER = LoggerFactory.getLogger(TagD.class);

private static final Map<VR, Integer> 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)
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit cf5bbc8

Please sign in to comment.