From 1eed2135278e34f4105f2bdd44eb15bd4e44d910 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Fri, 11 Mar 2016 15:06:17 +0000 Subject: [PATCH] Added MediaInfo tool in Tools panel --- .../ui/analyze/AnalyzePanelBuilder.java | 4 +- .../net/filebot/ui/analyze/MediaInfoTool.java | 98 ++++++++++++------- 2 files changed, 62 insertions(+), 40 deletions(-) diff --git a/source/net/filebot/ui/analyze/AnalyzePanelBuilder.java b/source/net/filebot/ui/analyze/AnalyzePanelBuilder.java index 9fcff711..da8c2707 100644 --- a/source/net/filebot/ui/analyze/AnalyzePanelBuilder.java +++ b/source/net/filebot/ui/analyze/AnalyzePanelBuilder.java @@ -22,11 +22,11 @@ public class AnalyzePanelBuilder implements PanelBuilder { @Override public JComponent create() { AnalyzePanel panel = new AnalyzePanel(); - panel.addTool(new MediaInfoTool()); + panel.addTool(new ExtractTool()); panel.addTool(new TypeTool()); panel.addTool(new SplitTool()); panel.addTool(new AttributeTool()); - panel.addTool(new ExtractTool()); + panel.addTool(new MediaInfoTool()); return panel; } diff --git a/source/net/filebot/ui/analyze/MediaInfoTool.java b/source/net/filebot/ui/analyze/MediaInfoTool.java index 457cde66..a194b089 100644 --- a/source/net/filebot/ui/analyze/MediaInfoTool.java +++ b/source/net/filebot/ui/analyze/MediaInfoTool.java @@ -9,7 +9,9 @@ import java.awt.Color; import java.io.File; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.TreeMap; +import java.util.regex.Pattern; import java.util.stream.IntStream; import javax.swing.BorderFactory; @@ -86,20 +88,10 @@ class MediaInfoTool extends Tool { @Override protected void setModel(TableModel model) { table.setModel(model); + table.setAutoResizeMode(table.getRowCount() > 0 ? JTable.AUTO_RESIZE_OFF : JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS); - // set column preferences TableColumnModel columnModel = table.getColumnModel(); - columnModel.getColumn(0).setMaxWidth(20); - columnModel.getColumn(1).setMaxWidth(70); - columnModel.getColumn(2).setMinWidth(140); - - if (columnModel.getColumnCount() > 4) { - table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); - IntStream.range(0, columnModel.getColumnCount()).forEach(i -> columnModel.getColumn(i).setMinWidth(140)); - } else { - table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); - } - + IntStream.range(0, columnModel.getColumnCount()).forEach(i -> columnModel.getColumn(i).setMinWidth(150)); } private static class MediaInfoKey implements Comparable { @@ -108,10 +100,12 @@ class MediaInfoTool extends Tool { public final int stream; public final String name; + private static final Pattern strip = Pattern.compile("[^a-z]", Pattern.CASE_INSENSITIVE); + public MediaInfoKey(StreamKind kind, int stream, String name) { this.kind = kind; this.stream = stream; - this.name = name; + this.name = strip.matcher(name).replaceAll(""); } @Override @@ -138,71 +132,99 @@ class MediaInfoTool extends Tool { return name.compareTo(other.name); } + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + if (kind != StreamKind.General) { + sb.append(kind.name()); + if (stream > 0) { + sb.append('[').append(stream).append(']'); + } + sb.append('.'); + } + return sb.append(name).toString(); + } + } private static class MediaInfoTableModel extends AbstractTableModel { - private final File[] files; private final MediaInfoKey[] keys; - private final String[][] rows; + private final String[][] values; + private final String[] files; + private final Class[] columnClass; public MediaInfoTableModel() { this(emptyList(), emptyMap()); } public MediaInfoTableModel(List files, Map values) { - this.files = files.toArray(new File[0]); this.keys = values.keySet().toArray(new MediaInfoKey[0]); - this.rows = values.values().toArray(new String[0][]); + this.values = values.values().toArray(new String[0][]); + this.files = files.stream().map(File::getName).toArray(String[]::new); + this.columnClass = new Class[getColumnCount()]; + } + + public int getHeaderColumnCount() { + return 1; } @Override public int getColumnCount() { - return files.length + 3; + return keys.length + getHeaderColumnCount(); } @Override public String getColumnName(int column) { switch (column) { - case 1: - return "Stream"; case 0: - return "#"; - case 2: - return "Property"; + return "File"; default: - return files[column - 3].getName(); + return keys[column - getHeaderColumnCount()].toString(); + } + } + + private boolean isNumber(String s) { + try { + Double.parseDouble(s); + return true; + } catch (Exception e) { + return false; } } @Override public Class getColumnClass(int column) { - switch (column) { - case 1: - return StreamKind.class; - case 0: - return Integer.class; - default: + int c = column - getHeaderColumnCount(); + if (c < 0) { return String.class; } + + if (columnClass[c] != null) { + return columnClass[c]; + } + + if (IntStream.range(0, files.length).mapToObj(i -> values[c][i]).filter(Objects::nonNull).allMatch(this::isNumber)) { + columnClass[c] = Number.class; + return columnClass[c]; + } + + columnClass[c] = String.class; + return columnClass[c]; } @Override public int getRowCount() { - return keys.length; + return files.length; } @Override - public Object getValueAt(int row, int column) { + public String getValueAt(int row, int column) { switch (column) { - case 1: - return keys[row].kind; case 0: - return keys[row].stream; - case 2: - return keys[row].name; + return files[row]; default: - return rows[row][column - 3]; + return values[column - getHeaderColumnCount()][row]; } }