$darkmode
DENOPTIM
GUIFileSaver.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;
23
24import javax.swing.JFileChooser;
25import javax.swing.filechooser.FileNameExtensionFilter;
26import javax.swing.filechooser.FileSystemView;
27
28import denoptim.files.FileAndFormat;
29import denoptim.files.FileFormat;
30import denoptim.files.FileFormat.DataKind;
31
36public class GUIFileSaver
37{
38 private static JFileChooser fileChooser = new JFileChooser(getSystemRoot());
39
40//-----------------------------------------------------------------------------
41
42 private static File getSystemRoot()
43 {
44 File startFile = new File(System.getProperty("user.dir"));
45 while (!FileSystemView.getFileSystemView().isFileSystemRoot(startFile))
46 {
47 startFile = startFile.getParentFile();
48 }
49 return startFile;
50 }
51//-----------------------------------------------------------------------------
52
53 public static FileAndFormat pickFileForSavingGraphs(Component parent)
54 {
55 fileChooser.resetChoosableFileFilters();
56 FileNameExtensionFilter sdfFilter = new FileNameExtensionFilter(
57 "SDF", "sdf");
58 fileChooser.addChoosableFileFilter(sdfFilter);
59 FileNameExtensionFilter jsonFilter = new FileNameExtensionFilter(
60 "JSON", "json");
61 fileChooser.addChoosableFileFilter(jsonFilter);
62 fileChooser.setAcceptAllFileFilterUsed(false);
63 File file;
64 if (fileChooser.showSaveDialog(parent) == JFileChooser.APPROVE_OPTION)
65 {
66 file = fileChooser.getSelectedFile();
67 }
68 else
69 {
70 return null;
71 }
73 fileChooser.getFileFilter().getDescription(), DataKind.GRAPH));
74 return ff;
75 }
76
77//-----------------------------------------------------------------------------
78
79 public static FileAndFormat pickFileForSavingVertexes(Component parent)
80 {
81 fileChooser.resetChoosableFileFilters();
82 FileNameExtensionFilter sdfFilter = new FileNameExtensionFilter(
83 "SDF", "sdf");
84 fileChooser.addChoosableFileFilter(sdfFilter);
85 FileNameExtensionFilter jsonFilter = new FileNameExtensionFilter(
86 "JSON", "json");
87 fileChooser.addChoosableFileFilter(jsonFilter);
88 fileChooser.setAcceptAllFileFilterUsed(false);
89 File file;
90 if (fileChooser.showSaveDialog(parent) == JFileChooser.APPROVE_OPTION)
91 {
92 file = fileChooser.getSelectedFile();
93 }
94 else
95 {
96 return null;
97 }
99 fileChooser.getFileFilter().getDescription(), DataKind.VERTEX));
100 return ff;
101 }
102
103//-----------------------------------------------------------------------------
104
105 public static FileAndFormat pickFileForSavingCuttingRules(Component parent)
106 {
107 fileChooser.resetChoosableFileFilters();
108 File file;
109 if (fileChooser.showSaveDialog(parent) == JFileChooser.APPROVE_OPTION)
110 {
111 file = fileChooser.getSelectedFile();
112 }
113 else
114 {
115 return null;
116 }
118 return ff;
119 }
120
121//-----------------------------------------------------------------------------
122
123}
A file with a conventional representation of its format.
GUI component to provide pathname where to save stuff.
static FileAndFormat pickFileForSavingCuttingRules(Component parent)
static JFileChooser fileChooser
static FileAndFormat pickFileForSavingVertexes(Component parent)
static FileAndFormat pickFileForSavingGraphs(Component parent)
The kind of data found in a file.
File formats identified by DENOPTIM.
Definition: FileFormat.java:32
static FileFormat fromString(String extension, DataKind kind)
Gets the FileFormat from file extension and kind of data.