$darkmode
DENOPTIM
GUIFileOpener.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2020 Marco Foscato <marco.foscato@uib.no>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package denoptim.gui;
20
21import java.awt.Component;
22import java.io.File;
23import java.util.HashSet;
24import java.util.Set;
25
26import javax.swing.JFileChooser;
27import javax.swing.JTextField;
28import javax.swing.filechooser.FileNameExtensionFilter;
29import javax.swing.filechooser.FileSystemView;
30
35public class GUIFileOpener
36{
37 protected static JFileChooser fileChooser = new JFileChooser(getSystemRoot());
38
39//-----------------------------------------------------------------------------
40
41 private static File getSystemRoot()
42 {
43 File startFile = new File(System.getProperty("user.dir"));
44 while (!FileSystemView.getFileSystemView().isFileSystemRoot(startFile))
45 {
46 startFile = startFile.getParentFile();
47 }
48 return startFile;
49 }
50
51//-----------------------------------------------------------------------------
52
53 public static File pickFileOrFolder(Component parent)
54 {
55 fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
56 fileChooser.setFileHidingEnabled(false);
57 File file = null;
58 if (fileChooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION)
59 {
60 file = fileChooser.getSelectedFile();
61 }
62 fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
63 return file;
64 }
65
66//-----------------------------------------------------------------------------
67
68 public static File pickFileForTxtField(JTextField txtField, Component parent)
69 {
70 File file = pickFile(parent);
71 if (file != null)
72 {
73 txtField.setText(file.getAbsolutePath());
74 }
75 return file;
76 }
77
78//-----------------------------------------------------------------------------
79
80 public static Set<File> pickManyFiles(Component parent)
81 {
82 fileChooser.setMultiSelectionEnabled(true);
83 fileChooser.setFileHidingEnabled(false);
84 fileChooser.setDialogTitle("Select one or more files");
85 Set<File> files = null;
86 if (fileChooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION)
87 {
88 files = new HashSet<File>();
89 File[] arr = fileChooser.getSelectedFiles();
90 for (int i=0; i<arr.length; i++)
91 {
92 files.add(arr[i]);
93 }
94 }
95 fileChooser.setDialogTitle("Open");
96 fileChooser.setMultiSelectionEnabled(false);
97 return files;
98 }
99
100//-----------------------------------------------------------------------------
101
102 public static File pickFile(Component parent)
103 {
104 fileChooser.resetChoosableFileFilters();
105 fileChooser.setFileHidingEnabled(false);
106 fileChooser.setAcceptAllFileFilterUsed(true);
107 fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
108 File file = null;
109 if (fileChooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION)
110 {
111 file = fileChooser.getSelectedFile();
112 }
113 return file;
114 }
115
116//-----------------------------------------------------------------------------
117
118 public static File pickFileWithGraph(Component parent)
119 {
120 fileChooser.resetChoosableFileFilters();
121 fileChooser.setFileHidingEnabled(false);
122 fileChooser.setAcceptAllFileFilterUsed(true);
123 FileNameExtensionFilter jsonFilter = new FileNameExtensionFilter(
124 "JSON", "json");
125 fileChooser.addChoosableFileFilter(jsonFilter);
126 FileNameExtensionFilter sdfFilter = new FileNameExtensionFilter(
127 "SDF", "sdf");
128 fileChooser.addChoosableFileFilter(sdfFilter);
129 fileChooser.setAcceptAllFileFilterUsed(true);
130 fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
131 File file = null;
132 if (fileChooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION)
133 {
134 file = fileChooser.getSelectedFile();
135 }
136 return file;
137 }
138
139//-----------------------------------------------------------------------------
140
141 public static File pickFolderForTxtField(JTextField txtField,
142 Component parent)
143 {
144 fileChooser.setFileHidingEnabled(false);
145 File file = pickFolder(parent);
146 if (file != null)
147 {
148 txtField.setText(file.getAbsolutePath());
149 }
150 return file;
151 }
152
153//-----------------------------------------------------------------------------
154
155 public static File pickFolder(Component parent)
156 {
157 fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
158 fileChooser.setFileHidingEnabled(false);
159 fileChooser.setDialogTitle("Choose Folder to Load");
160 File file = null;
161 if (fileChooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION)
162 {
163 file = fileChooser.getSelectedFile();
164 }
165 fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
166 fileChooser.setDialogTitle("Open");
167 return file;
168 }
169
170//-----------------------------------------------------------------------------
171
172 public static File pickFileForSaving(Component parent)
173 {
174 File file;
175 fileChooser.setFileHidingEnabled(false);
176 if (fileChooser.showSaveDialog(parent) == JFileChooser.APPROVE_OPTION)
177 {
178 file = fileChooser.getSelectedFile();
179 }
180 else
181 {
182 /*
183 JOptionPane.showMessageDialog(parent,
184 "Could not save. Try again.",
185 "Error",
186 JOptionPane.ERROR_MESSAGE,
187 UIManager.getIcon("OptionPane.errorIcon"));
188 */
189 return null;
190 }
191 return file;
192 }
193
194//-----------------------------------------------------------------------------
195
196}
File opener for DENOPTIM GUI.
static File pickFolderForTxtField(JTextField txtField, Component parent)
static File pickFolder(Component parent)
static JFileChooser fileChooser
static Set< File > pickManyFiles(Component parent)
static File pickFileWithGraph(Component parent)
static File pickFile(Component parent)
static File pickFileOrFolder(Component parent)
static File pickFileForSaving(Component parent)
static File pickFileForTxtField(JTextField txtField, Component parent)