-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathExportRangeDialog.java
More file actions
210 lines (183 loc) · 6.76 KB
/
ExportRangeDialog.java
File metadata and controls
210 lines (183 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package nodebox.client;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
/**
* Dialog presented when exporting a range of frames.
*/
public class ExportRangeDialog extends JDialog implements ActionListener {
private boolean dialogSuccessful = false;
private JTextField fromField;
private JTextField toField;
private JTextField directoryField;
private JComboBox formatBox;
private String exportPrefix;
private File exportDirectory;
private int fromValue;
private int toValue;
private ImageFormat format;
private JTextField prefixField;
private JButton exportButton;
public ExportRangeDialog(Frame frame, File exportDirectory) {
super(frame, "Export Range");
setModal(true);
setResizable(false);
this.exportDirectory = exportDirectory;
// Main
setLayout(new BorderLayout(5, 5));
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
mainPanel.setBorder(new Theme.InsetsBorder(10, 10, 10, 10));
add(mainPanel, BorderLayout.CENTER);
// Prefix
JPanel prefixPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 0, 0));
prefixPanel.add(new JLabel("File Prefix: "));
prefixField = new JTextField("export", 20);
prefixPanel.add(prefixField);
mainPanel.add(prefixPanel);
mainPanel.add(Box.createVerticalStrut(10));
// Directory
JPanel directoryPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 0, 0));
directoryPanel.add(new JLabel("Directory: "));
directoryField = new JTextField(20);
directoryField.setEditable(false);
directoryPanel.add(directoryField);
JButton chooseButton = new JButton("...");
chooseButton.putClientProperty("JButton.buttonType", "gradient");
chooseButton.setPreferredSize(new Dimension(30, 27));
chooseButton.addActionListener(this);
directoryPanel.add(chooseButton);
mainPanel.add(directoryPanel);
mainPanel.add(Box.createVerticalStrut(10));
// Range
JPanel rangePanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 10, 0));
rangePanel.add(new JLabel("From:"));
fromField = new JTextField("1", 5);
rangePanel.add(fromField);
rangePanel.add(new JLabel("To:"));
toField = new JTextField("100", 5);
rangePanel.add(toField);
mainPanel.add(rangePanel);
// Format
JPanel formatPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 0, 0));
formatPanel.add(new JLabel("Format:"));
formatBox = new JComboBox();
formatBox.addItem("PDF");
formatBox.addItem("PNG");
formatBox.setSelectedItem("PDF");
formatPanel.add(formatBox);
mainPanel.add(formatPanel);
mainPanel.add(Box.createVerticalGlue());
// Buttons
mainPanel.add(Box.createVerticalStrut(10));
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 10, 0));
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
doCancel();
}
});
buttonPanel.add(cancelButton);
exportButton = new JButton("Export");
exportButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
doExport();
}
});
if (exportDirectory == null)
exportButton.setEnabled(false);
buttonPanel.add(exportButton);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
pack();
getRootPane().setDefaultButton(exportButton);
setExportDirectory(exportDirectory);
}
private void doCancel() {
setVisible(false);
}
private void doExport() {
exportPrefix = prefixField.getText();
try {
fromValue = Integer.valueOf(fromField.getText());
} catch (NumberFormatException e) {
fromValue = 1;
}
try {
toValue = Integer.valueOf(toField.getText());
} catch (NumberFormatException e) {
toValue = 100;
}
dialogSuccessful = true;
setVisible(false);
}
public boolean isDialogSuccessful() {
return dialogSuccessful;
}
public String getExportPrefix() {
return exportPrefix;
}
public File getExportDirectory() {
return exportDirectory;
}
private void setExportDirectory(File d) {
this.exportDirectory = d;
if (this.exportDirectory == null) {
directoryField.setText("");
} else {
directoryField.setText(this.exportDirectory.getAbsolutePath());
}
exportButton.setEnabled(this.exportDirectory != null);
}
public int getFromValue() {
return fromValue;
}
public int getToValue() {
return toValue;
}
public ImageFormat getFormat() {
return ImageFormat.of(formatBox.getSelectedItem().toString());
}
/**
* Called when a directory needs to be chosen.
*
* @param e the action event
*/
public void actionPerformed(ActionEvent e) {
if (PlatformUtils.onMac()) {
// On Mac, we can use the native FileDialog to choose a directory using a special property.
FileDialog fileDialog = new FileDialog((Frame) null);
System.setProperty("apple.awt.fileDialogForDirectories", "true");
fileDialog.setVisible(true);
System.setProperty("apple.awt.fileDialogForDirectories", "false");
String chosenFile = fileDialog.getFile();
if (chosenFile == null) {
setExportDirectory(null);
return;
}
String dir = fileDialog.getDirectory();
File f = new File(dir, chosenFile);
if (!f.isDirectory()) {
setExportDirectory(f.getParentFile());
} else {
setExportDirectory(f);
}
} else {
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int retVal = chooser.showOpenDialog(null);
if (retVal == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
assert file.isDirectory();
setExportDirectory(file);
} else {
setExportDirectory(null);
}
}
}
public static void main(String[] args) {
ExportRangeDialog d = new ExportRangeDialog(null, null);
d.setVisible(true);
}
}