Gridarta Editor
FileChooserUtils.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.utils;
21 
22 import java.io.File;
23 import javax.swing.JFileChooser;
24 import org.jetbrains.annotations.NotNull;
25 import org.jetbrains.annotations.Nullable;
26 
31 public class FileChooserUtils {
32 
36  private FileChooserUtils() {
37  }
38 
48  public static void setCurrentDirectory(@NotNull final JFileChooser fileChooser, @Nullable final File dir) {
49  fileChooser.setCurrentDirectory(sanitize(dir));
50  }
51 
56  public static void sanitizeCurrentDirectory(@NotNull final JFileChooser fileChooser) {
57  setCurrentDirectory(fileChooser, fileChooser.getCurrentDirectory());
58  }
59 
66  @NotNull
67  private static File sanitize(@Nullable final File dir) {
68  File result = dir == null ? ConfigFileUtils.getHomeDir() : dir.getAbsoluteFile();
69  while (!result.exists() || !result.isDirectory()) {
70  result = result.getParentFile();
71  if (result == null) {
72  result = ConfigFileUtils.getHomeDir();
73  if (!result.exists() || !result.isDirectory()) {
74  result = new File("/");
75  }
76  break;
77  }
78  }
79  return result;
80  }
81 
82 }
net.sf.gridarta.utils.FileChooserUtils.FileChooserUtils
FileChooserUtils()
Private constructor to prevent instantiation.
Definition: FileChooserUtils.java:36
net.sf.gridarta.utils.FileChooserUtils.setCurrentDirectory
static void setCurrentDirectory(@NotNull final JFileChooser fileChooser, @Nullable final File dir)
Calls JFileChooser#setCurrentDirectory(File).
Definition: FileChooserUtils.java:48
net.sf.gridarta.utils.FileChooserUtils.sanitize
static File sanitize(@Nullable final File dir)
Performs sanity checks on the given directory.
Definition: FileChooserUtils.java:67
net.sf.gridarta.utils.FileChooserUtils.sanitizeCurrentDirectory
static void sanitizeCurrentDirectory(@NotNull final JFileChooser fileChooser)
Makes sure the current directory of a JFileChooser is valid.
Definition: FileChooserUtils.java:56
net.sf.gridarta.utils.ConfigFileUtils.getHomeDir
static File getHomeDir()
Returns the user's home directory.
Definition: ConfigFileUtils.java:72
net.sf.gridarta.utils.FileChooserUtils
Utility class for JFileChooser related functions.
Definition: FileChooserUtils.java:31
net.sf.gridarta.utils.ConfigFileUtils
Loader for loading resources the user's home directory.
Definition: ConfigFileUtils.java:29