From: Ben Soares Date: Wed, 14 Jun 2023 11:17:17 +0000 (+0100) Subject: JAL-4206 Added recentlyOpenedCellRenderer to better display the Recently Opened JList... X-Git-Tag: Release_2_11_4_0~213^2~4 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=67d5b44cc1fef4ed332d8f52da4af181b9ecfd25;p=jalview.git JAL-4206 Added recentlyOpenedCellRenderer to better display the Recently Opened JList. Added an 'All known alignment files' option for file types to show. --- diff --git a/resources/lang/Messages.properties b/resources/lang/Messages.properties index 4200a46..a13dbb0 100644 --- a/resources/lang/Messages.properties +++ b/resources/lang/Messages.properties @@ -1460,4 +1460,4 @@ action.show_tree_for_matrix_tooltip = Opens a tree viewer to display the average action.cluster_matrix = Cluster matrix action.clustering_matrix_for = Calculating tree for matrix {0} and clustering at {1} action.cluster_matrix_tooltip = Computes an average distance tree for the matrix and displays it - +label.all_known_alignment_files = All known alignment files diff --git a/resources/lang/Messages_es.properties b/resources/lang/Messages_es.properties index b3c6988..9bc4684 100644 --- a/resources/lang/Messages_es.properties +++ b/resources/lang/Messages_es.properties @@ -1434,3 +1434,5 @@ label.tftype_default = Default label.tftype_plddt = pLDDT label.add_pae_matrix_file = AƱadir un fichero de matriz PAE label.nothing_selected = Nada seleccionado +label.all_known_alignment_files = Todos los archivos de alineaciĆ³n conocidos + diff --git a/src/jalview/io/JalviewFileChooser.java b/src/jalview/io/JalviewFileChooser.java index cb47610..ef7a2b8 100755 --- a/src/jalview/io/JalviewFileChooser.java +++ b/src/jalview/io/JalviewFileChooser.java @@ -40,15 +40,17 @@ import java.util.StringTokenizer; import java.util.Vector; import javax.swing.BoxLayout; -import javax.swing.DefaultListCellRenderer; import javax.swing.JCheckBox; import javax.swing.JDialog; import javax.swing.JFileChooser; +import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; +import javax.swing.ListCellRenderer; import javax.swing.SpringLayout; +import javax.swing.SwingConstants; import javax.swing.filechooser.FileFilter; import javax.swing.plaf.basic.BasicFileChooserUI; @@ -262,6 +264,26 @@ public class JalviewFileChooser extends JFileChooser // file filters to fix bug on Mac OSX setAcceptAllFileFilterUsed(acceptAny); + // add a "All known alignment files" option + List allExtensions = new ArrayList<>(); + for (String[] format : formats) + { + String[] extensions = format[0].split(","); + for (String ext : extensions) + { + if (!allExtensions.contains(ext)) + { + allExtensions.add(ext); + } + } + } + allExtensions.sort(null); + JalviewFileFilter alljvf = new JalviewFileFilter( + allExtensions.toArray(new String[] {}), + MessageManager.getString("label.all_known_alignment_files")); + alljvf.setExtensionListInDescription(false); + addChoosableFileFilter(alljvf); + for (String[] format : formats) { JalviewFileFilter jvf = new JalviewFileFilter(format[0], format[1]); @@ -552,10 +574,13 @@ public class JalviewFileChooser extends JFileChooser } list = new JList<>(recent); + list.setCellRenderer(new recentlyOpenedCellRenderer()); + /* DefaultListCellRenderer dlcr = new DefaultListCellRenderer(); dlcr.setHorizontalAlignment(DefaultListCellRenderer.RIGHT); list.setCellRenderer(dlcr); + */ list.addMouseListener(new MouseAdapter() { @@ -577,13 +602,15 @@ public class JalviewFileChooser extends JFileChooser layout.putConstraint(SpringLayout.NORTH, scroller, 5, SpringLayout.NORTH, this); + // TODO are we now all using FlatLaf -- check same dimensions for + // everyone? if (Platform.isAMacAndNotJS()) { - scroller.setPreferredSize(new Dimension(500, 100)); + scroller.setPreferredSize(new Dimension(280, 100)); } else { - scroller.setPreferredSize(new Dimension(530, 200)); + scroller.setPreferredSize(new Dimension(500, 200)); } this.add(scroller); @@ -602,6 +629,77 @@ public class JalviewFileChooser extends JFileChooser } + class recentlyOpenedCellRenderer extends JLabel + implements ListCellRenderer + { + private final static int maxChars = 46; + + private final static String ellipsis = "..."; + + @Override + public Component getListCellRendererComponent( + JList list, String value, int index, + boolean isSelected, boolean cellHasFocus) + { + String filename = value.toString(); + String displayFilename; + if (filename.length() > maxChars) + { + StringBuilder displayFileSB = new StringBuilder(); + File file = new File(filename); + displayFileSB.append(file.getName()); + if (file.getParent() != null) + { + File parent = file; + boolean spaceleft = true; + while (spaceleft && parent.getParent() != null) + { + parent = parent.getParentFile(); + String name = parent.getName(); + displayFileSB.insert(0, File.separator); + if (displayFileSB.length() + name.length() < maxChars - 1) + { + displayFileSB.insert(0, name); + } + else + { + displayFileSB.insert(0, ellipsis); + spaceleft = false; + } + } + if (spaceleft && filename.startsWith(File.separator) + && !(displayFileSB.charAt(0) == File.separatorChar)) + { + displayFileSB.insert(0, File.separator); + } + } + displayFilename = displayFileSB.toString(); + } + else + { + displayFilename = filename; + } + this.setText(displayFilename.toString()); + this.setToolTipText(filename); + if (isSelected) + { + setBackground(list.getSelectionBackground()); + setForeground(list.getSelectionForeground()); + } + else + { + setBackground(list.getBackground()); + setForeground(list.getForeground()); + } + this.setHorizontalAlignment(SwingConstants.TRAILING); + this.setEnabled(list.isEnabled()); + this.setFont(list.getFont().deriveFont(12f)); + this.setOpaque(true); + return this; + } + + } + /* @Override public JalviewFileChooser setResponseHandler(Object response, diff --git a/src/jalview/io/JalviewFileFilter.java b/src/jalview/io/JalviewFileFilter.java index 10dc9ea..5e9a242 100755 --- a/src/jalview/io/JalviewFileFilter.java +++ b/src/jalview/io/JalviewFileFilter.java @@ -20,12 +20,11 @@ */ package jalview.io; -import java.util.Locale; - import java.io.File; import java.util.Hashtable; import java.util.Iterator; import java.util.LinkedHashMap; +import java.util.Locale; import java.util.Map; import java.util.StringTokenizer; @@ -175,7 +174,7 @@ public class JalviewFileFilter extends FileFilter while (extensions.hasNext()) { - fullDescription += (", " + extensions.next()); + fullDescription += (", ." + extensions.next()); } }