JAL-3438 spotless for 2.11.2.0
[jalview.git] / src / jalview / io / JalviewFileView.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.io;
22
23 import java.util.Locale;
24
25 import jalview.util.MessageManager;
26
27 import java.io.File;
28 import java.net.URL;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.Map;
32
33 import javax.swing.Icon;
34 import javax.swing.ImageIcon;
35 import javax.swing.filechooser.FileView;
36
37 public class JalviewFileView extends FileView
38 {
39   private static Map<String, String> extensions;
40
41   private static Map<String, ImageIcon> icons;
42
43   private void loadExtensions()
44   {
45     extensions = new HashMap<>();
46     for (FileFormatI ff : FileFormats.getInstance().getFormats())
47     {
48       String desc = ff.getName() + " file";
49       String exts = ff.getExtensions();
50       for (String ext : exts.split(","))
51       {
52         ext = ext.trim().toLowerCase(Locale.ROOT);
53         extensions.put(ext, desc + ("jar".equals(ext) ? " (old)" : ""));
54       }
55     }
56   }
57
58   @Override
59   public String getTypeDescription(File f)
60   {
61     String extension = getExtension(f);
62
63     String type = getDescriptionForExtension(extension);
64
65     if (extension != null)
66     {
67       if (extensions.containsKey(extension))
68       {
69         type = extensions.get(extension).toString();
70       }
71     }
72
73     return type;
74   }
75
76   private String getDescriptionForExtension(String extension)
77   {
78     synchronized (this)
79     {
80       if (extensions == null)
81       {
82         loadExtensions();
83       }
84     }
85     return extensions.get(extension);
86   }
87
88   @Override
89   public Icon getIcon(File f)
90   {
91     String extension = getExtension(f);
92     Icon icon = null;
93     String type = getDescriptionForExtension(extension);
94
95     if (type == null)
96     {
97       Iterator<String> it = extensions.keySet().iterator();
98       EXTENSION: while (it.hasNext())
99       {
100         String ext = it.next();
101
102         // quick negative test
103         if (!f.getName().contains(ext))
104         {
105           continue EXTENSION;
106         }
107
108         BackupFilenameParts bfp = BackupFilenameParts
109                 .currentBackupFilenameParts(f.getName(), ext, true);
110         if (bfp.isBackupFile())
111         {
112           extension = ext;
113           type = getDescriptionForExtension(extension)
114                   + MessageManager.getString("label.backup");
115           break;
116         }
117       }
118     }
119
120     if (type != null)
121     {
122       icon = getImageIcon("/images/file.png");
123     }
124
125     return icon;
126   }
127
128   /**
129    * Returns the extension of a file (part of the name after the last period),
130    * in lower case, or null if the name ends in or does not include a period.
131    */
132   public static String getExtension(File f)
133   {
134     String ext = null;
135     String s = f.getName();
136     int i = s.lastIndexOf('.');
137
138     if ((i > 0) && (i < (s.length() - 1)))
139     {
140       ext = s.substring(i + 1).toLowerCase(Locale.ROOT);
141     }
142
143     return ext;
144   }
145
146   /**
147    * Returns an ImageIcon, or null if the file was not found
148    * 
149    * @param filePath
150    */
151   protected ImageIcon getImageIcon(String filePath)
152   {
153     /*
154      * we reuse a single icon object per path here
155      */
156     synchronized (this)
157     {
158       if (icons == null)
159       {
160         icons = new HashMap<>();
161       }
162       if (!icons.containsKey(filePath))
163       {
164         ImageIcon icon = null;
165         URL imgURL = JalviewFileView.class.getResource(filePath);
166         if (imgURL != null)
167         {
168           icon = new ImageIcon(imgURL);
169         }
170         else
171         {
172           System.err.println(
173                   "JalviewFileView.createImageIcon: Couldn't find file: "
174                           + filePath);
175         }
176         icons.put(filePath, icon);
177       }
178     }
179
180     /*
181      * return the image from the table (which may be null if
182      * icon creation failed)
183      */
184     return icons.get(filePath);
185   }
186 }