JAL-4206 Added recentlyOpenedCellRenderer to better display the Recently Opened JList...
authorBen Soares <b.soares@dundee.ac.uk>
Wed, 14 Jun 2023 11:17:17 +0000 (12:17 +0100)
committerBen Soares <b.soares@dundee.ac.uk>
Wed, 14 Jun 2023 11:17:17 +0000 (12:17 +0100)
resources/lang/Messages.properties
resources/lang/Messages_es.properties
src/jalview/io/JalviewFileChooser.java
src/jalview/io/JalviewFileFilter.java

index 4200a46..a13dbb0 100644 (file)
@@ -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
index b3c6988..9bc4684 100644 (file)
@@ -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
+
index cb47610..ef7a2b8 100755 (executable)
@@ -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<String> 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<String>
+  {
+    private final static int maxChars = 46;
+
+    private final static String ellipsis = "...";
+
+    @Override
+    public Component getListCellRendererComponent(
+            JList<? extends String> 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,
index 10dc9ea..5e9a242 100755 (executable)
  */
 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());
           }
         }