Skip to content

Commit

Permalink
Improve tag viewer #494
Browse files Browse the repository at this point in the history
  • Loading branch information
nroduit committed Nov 21, 2023
1 parent bf09f16 commit 4b8a828
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@ private static int preferredWidth(JTable table, int col) {
width = Math.max(width, preferredWidth);
}
}
return width + table.getIntercellSpacing().width;
return width + table.getIntercellSpacing().width + 5;
}

public static void pack(JTable table) {

if (!table.isShowing() || table.getColumnCount() == 0) {
return;
}
Expand All @@ -66,7 +65,6 @@ public static void pack(JTable table) {

int extra = table.getVisibleRect().width - total;
if (extra > 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 @@ -14,10 +14,12 @@
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
Expand All @@ -26,9 +28,11 @@
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultCaret;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
Expand All @@ -52,6 +56,7 @@
import org.weasis.core.api.media.data.TagUtil;
import org.weasis.core.api.media.data.TagView;
import org.weasis.core.api.media.data.TagW;
import org.weasis.core.api.util.FontItem;
import org.weasis.core.api.util.ResourceUtil;
import org.weasis.core.api.util.ResourceUtil.ActionIcon;
import org.weasis.core.ui.editor.SeriesViewer;
Expand All @@ -74,17 +79,19 @@ public class DicomFieldsView extends JTabbedPane implements SeriesViewerListener

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

public static final String[] columns = {
Messages.getString("tag.id"), "VR", Messages.getString("tag.name"), Messages.getString("value")
};

private final JScrollPane allPane = new JScrollPane();
private final JScrollPane limitedPane = new JScrollPane();
private final JTextPane jTextPaneLimited = new JTextPane();
private final SeriesViewer<?> viewer;
private MediaElement currentMedia;
private MediaSeries<?> currentSeries;
private boolean anonymize = false;
private final SeriesViewer<?> viewer;
private MediaSeries<?> series;

public static final String[] columns = {
Messages.getString("tag.id"), "VR", Messages.getString("tag.name"), Messages.getString("value")
};
private final DefaultTableModel tableModel =
new DefaultTableModel(columns, 0) {
@Override
Expand All @@ -93,7 +100,20 @@ public boolean isCellEditable(int row, int column) {
}
};

private final JTable jtable = new JTable(tableModel);
private final JTable jtable =
new JTable(tableModel) {
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component comp = super.prepareRenderer(renderer, row, column);

// Get tooltip for the row
Object value = getValueAt(row, column);
if (value != null && comp instanceof JComponent jComponent) {
jComponent.setToolTipText(getColumnName(column) + StringUtil.COLON_AND_SPACE + value);
}
return comp;
}
};
private final TagSearchTablePanel tagSearchTablePanel;
private final TagSearchDocumentPanel tagSearchDocumentPanel;

Expand All @@ -109,6 +129,9 @@ public DicomFieldsView(SeriesViewer<?> viewer) {
jTextPaneLimited.setBorder(new EmptyBorder(5, 5, 5, 5));
jTextPaneLimited.setContentType("text/html");
jTextPaneLimited.setEditable(false);
applyFormatting(jTextPaneLimited.getStyledDocument());
DefaultCaret caret = (DefaultCaret) jTextPaneLimited.getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);

JPanel dump = new JPanel();
dump.setLayout(new BorderLayout());
Expand All @@ -126,6 +149,7 @@ public boolean isSortable(int column) {
dump.add(tagSearchTablePanel, BorderLayout.NORTH);
jtable.getTableHeader().setReorderingAllowed(false);
jtable.setShowVerticalLines(true);
jtable.setFont(FontItem.SMALL.getFont());
dump.add(allPane, BorderLayout.CENTER);

setPreferredSize(GuiUtils.getDimension(400, 300));
Expand Down Expand Up @@ -158,22 +182,34 @@ private void changeDicomInfo(MediaSeries<?> series, MediaElement media) {
} else {
displayAllDicomInfo(series, media);
}
this.series = series;
}

private static void applyFormatting(StyledDocument doc) {
Color color = FlatUIUtils.getUIColor("TextArea.foreground", Color.DARK_GRAY);
Style bold = doc.addStyle("bold", doc.getStyle(StyleContext.DEFAULT_STYLE)); // NON-NLS
StyleConstants.setBold(bold, true);
StyleConstants.setForeground(bold, color);
int fontSize = StyleConstants.getFontSize(bold.getResolveParent());
int fontSize = 11; // Scaling is applied automatically to this value
Style defaultStyle = doc.getStyle(StyleContext.DEFAULT_STYLE);
Style bold = doc.addStyle("normal", defaultStyle); // NON-NLS
StyleConstants.setFontSize(bold, fontSize);
StyleConstants.setForeground(bold, color);

bold = doc.addStyle("bold", bold); // NON-NLS
StyleConstants.setBold(bold, true);

bold = doc.addStyle("h3", bold); // NON-NLS
StyleConstants.setFontSize(bold, fontSize + 3);
StyleConstants.setFontSize(bold, fontSize + 4);
}

private void displayAllDicomInfo(MediaSeries<?> series, MediaElement media) {
tableModel.setRowCount(0);
Point oldPosition = null;
boolean init = jtable.getRowCount() == 0;
if (!init) {
if (Objects.equals(series, this.series)) {
oldPosition = allPane.getViewport().getViewPosition();
}
tableModel.setRowCount(0);
}

if (media != null) {
MediaReader loader = media.getMediaReader();
if (loader instanceof DicomMediaIO dicomMediaIO) {
Expand All @@ -186,10 +222,6 @@ private void displayAllDicomInfo(MediaSeries<?> series, MediaElement media) {
printAttribute(tableModel, reader.getDicomObject());
}
}
jtable.getColumnModel().getColumn(0).setPreferredWidth(100);
jtable.getColumnModel().getColumn(1).setPreferredWidth(30);
jtable.getColumnModel().getColumn(2).setPreferredWidth(250);
jtable.getColumnModel().getColumn(3).setPreferredWidth(300);
jtable.getColumnModel().setColumnMargin(GuiUtils.getScaleLength(7));
int height =
(jtable.getRowHeight() + jtable.getRowMargin()) * jtable.getRowCount()
Expand All @@ -202,10 +234,13 @@ private void displayAllDicomInfo(MediaSeries<?> series, MediaElement media) {
tableContainer.add(jtable.getTableHeader(), BorderLayout.PAGE_START);
tableContainer.add(jtable, BorderLayout.CENTER);
tagSearchTablePanel.filter();
TableColumnAdjuster.pack(jtable);
allPane.setViewportView(tableContainer);
TableColumnAdjuster.pack(jtable);
tableContainer.revalidate();
tableContainer.repaint();
if (oldPosition != null) {
allPane.getViewport().setViewPosition(oldPosition);
}
}

private static void printAttribute(DefaultTableModel model, Attributes dcmObj) {
Expand Down Expand Up @@ -310,8 +345,6 @@ private static void printSequence(Sequence seq, DefaultTableModel model, Object[

private void displayLimitedDicomInfo(MediaSeries<?> series, MediaElement media) {
StyledDocument doc = jTextPaneLimited.getStyledDocument();
applyFormatting(doc);
int oldCaretPosition = jTextPaneLimited.getCaretPosition();
try {
// clear previous text
doc.remove(0, doc.getLength());
Expand All @@ -330,8 +363,6 @@ private void displayLimitedDicomInfo(MediaSeries<?> series, MediaElement media)
}
}
}
oldCaretPosition = Math.min(oldCaretPosition, doc.getLength());
jTextPaneLimited.setCaretPosition(oldCaretPosition);
tagSearchDocumentPanel.filter();
limitedPane.setViewportView(jTextPaneLimited);
}
Expand Down Expand Up @@ -360,7 +391,7 @@ private void writeItems(DicomData dicomData, TagReadable group, StyledDocument d
Object val = TagUtil.getTagValue(tag, group, currentMedia);
if (val != null) {
exist = true;
doc.insertString(doc.getLength(), tag.getDisplayedName(), null);
doc.insertString(doc.getLength(), tag.getDisplayedName(), doc.getStyle("normal"));
doc.insertString(
doc.getLength(),
StringUtil.COLON_AND_SPACE + tag.getFormattedTagValue(val, null) + "\n",
Expand Down

0 comments on commit 4b8a828

Please sign in to comment.