/
OptionsDialog.java
356 lines (310 loc) · 12 KB
/
OptionsDialog.java
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* SK's Minecraft Launcher
* Copyright (C) 2010, 2011 Albert Pham <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.mclauncher;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingUtilities;
import javax.swing.text.DefaultCaret;
import com.sk89q.mclauncher.config.Configuration;
import com.sk89q.mclauncher.config.ConfigurationsManager;
import com.sk89q.mclauncher.config.LauncherOptions;
import com.sk89q.mclauncher.util.UIUtil;
/**
* The dialog for launcher options.
*
* @author sk89q
*/
public class OptionsDialog extends JDialog {
private static final long serialVersionUID = -1889866989693082182L;
private LauncherOptions options;
private JTable configsTable;
private JTabbedPane tabs;
private List<OptionsPanel> optionsPanels = new ArrayList<OptionsPanel>();
/**
* Construct the dialog.
*
* @param owner owning frame
* @param configuration configuration
* @param options options object
* @param initialTab index of the initial tab, 0 for the first
*/
public OptionsDialog(LauncherFrame owner, Configuration configuration,
LauncherOptions options, int initialTab) {
super(owner, "Launcher Options", true);
this.options = options;
setResizable(true);
buildUI();
pack();
setSize(400, 540);
setLocationRelativeTo(owner);
tabs.setSelectedIndex(initialTab);
for (OptionsPanel panel : optionsPanels) {
panel.copySettingsToFields();
}
}
/**
* Save the options.
*
* @param copySettings true to also copy settings from the fields
* @return true if successful
*/
boolean save(boolean copySettings) {
if (copySettings) {
for (OptionsPanel panel : optionsPanels) {
panel.copyFieldsToSettings();
}
}
if (!options.save()) {
JOptionPane.showMessageDialog(this,
"Your options could not be saved to disk.", "Save error",
JOptionPane.ERROR_MESSAGE);
return false;
}
return true;
}
/**
* Adds the panel to the list of options panles.
*
* @param panel panel
* @return panel
*/
private <T extends OptionsPanel> T wrap(T panel) {
optionsPanels.add(panel);
return panel;
}
/**
* Build the interface.
*/
private void buildUI() {
final OptionsDialog self = this;
JPanel container = new JPanel();
container.setBorder(BorderFactory.createEmptyBorder(8, 8, 5, 8));
container.setLayout(new BorderLayout(3, 3));
tabs = new JTabbedPane();
tabs.addTab("Launcher",
wrap(new LauncherOptionsPanel(options.getSettings(), false)));
tabs.addTab("Environment",
wrap(new EnvironmentOptionsPanel(options.getSettings(), false)));
tabs.addTab("Configurations", buildConfigurationsPanel());
tabs.addTab("About", buildAboutPanel());
container.add(tabs, BorderLayout.CENTER);
JPanel buttonsPanel = new JPanel();
buttonsPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
JButton okButton = new JButton("OK");
JButton cancelButton = new JButton("Cancel");
UIUtil.equalWidth(okButton, cancelButton);
buttonsPanel.add(okButton);
buttonsPanel.add(cancelButton);
container.add(buttonsPanel, BorderLayout.SOUTH);
okButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (save(true)) {
self.dispose();
}
}
});
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
self.dispose();
}
});
add(container, BorderLayout.CENTER);
}
/**
* Build the configurations panel.
*
* @return created panel
*/
private JPanel buildConfigurationsPanel() {
final OptionsDialog self = this;
JPanel panel = new JPanel();
panel.setOpaque(false);
panel.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JPanel buttonsPanel = new JPanel();
buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS));
JButton newBtn = new JButton("New...");
buttonsPanel.add(newBtn);
buttonsPanel.add(Box.createHorizontalStrut(3));
JButton modifyBtn = new JButton("Modify...");
buttonsPanel.add(modifyBtn);
buttonsPanel.add(Box.createHorizontalGlue());
JButton removeBtn = new JButton("Delete...");
buttonsPanel.add(removeBtn);
panel.add(buttonsPanel);
panel.add(Box.createVerticalStrut(3));
configsTable = new JTable();
configsTable.setShowGrid(false);
configsTable.setRowHeight(configsTable.getRowHeight() + 2);
configsTable.setIntercellSpacing(new Dimension(0, 0));
configsTable.setFillsViewportHeight(true);
configsTable
.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
configsTable.setModel(options.getConfigurations());
configsTable.getColumnModel().getColumn(0).setPreferredWidth(30);
JScrollPane tableScroll = new JScrollPane(configsTable);
panel.add(tableScroll, BorderLayout.CENTER);
newBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new ConfigurationDialog(self, options.getConfigurations())
.setVisible(true);
}
});
modifyBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int min = configsTable.getSelectionModel()
.getMinSelectionIndex();
int max = configsTable.getSelectionModel()
.getMaxSelectionIndex();
if (min == -1 || max == -1) {
UIUtil.showError(self, "Selection error",
"You have not selected any configurations.");
return;
}
if (min != max) {
UIUtil.showError(self, "Selection error",
"Select one configuration at a time.");
return;
}
Configuration config = options.getConfigurations()
.getConfigurationAt(min);
new ConfigurationDialog(self, options.getConfigurations(),
config).setVisible(true);
}
});
removeBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int min = configsTable.getSelectionModel()
.getMinSelectionIndex();
int max = configsTable.getSelectionModel()
.getMaxSelectionIndex();
if (min == -1 || max == -1) {
UIUtil.showError(self, "Selection error",
"You have not selected any configurations.");
return;
}
if (JOptionPane
.showConfirmDialog(
self,
"Are you sure you want to remove the selected configuration? No files will be deleted.",
"Remove", JOptionPane.YES_NO_OPTION) != 0)
return;
ConfigurationsManager configsManager = options
.getConfigurations();
for (int i = min; i <= max; i++) {
Configuration config = configsManager.getConfigurationAt(i);
if (config.isBuiltIn()) {
UIUtil.showError(
self,
"Built-in configuration",
"The configuration '"
+ config.getName()
+ "' is built-in and cannot be removed.");
} else {
configsManager.remove(config);
}
}
save(false);
}
});
UIUtil.removeOpaqueness(buttonsPanel);
return panel;
}
/**
* Build the about panel.
*
* @return panel
*/
private JPanel buildAboutPanel() {
final OptionsDialog self = this;
JPanel panel = new JPanel();
panel.setOpaque(false);
panel.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JLabel label;
label = new JLabel("SK's Minecraft Launcher");
label.setAlignmentX(Component.LEFT_ALIGNMENT);
panel.add(label);
label = new JLabel("Version " + Launcher.VERSION);
label.setAlignmentX(Component.LEFT_ALIGNMENT);
panel.add(label);
LinkButton btn = new LinkButton("http://www.sk89q.com");
btn.setAlignmentX(Component.LEFT_ALIGNMENT);
panel.add(btn);
panel.add(Box.createVerticalStrut(20));
final JTextArea text = new JTextArea();
text.setEditable(false);
text.setWrapStyleWord(true);
text.setLineWrap(true);
text.setFont(label.getFont());
DefaultCaret caret = (DefaultCaret) text.getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
JScrollPane scroll = new JScrollPane(text);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroll.setAlignmentX(Component.LEFT_ALIGNMENT);
scroll.setPreferredSize(new Dimension(text.getPreferredSize().width, 200));
panel.add(scroll);
panel.add(Box.createVerticalGlue());
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
UIUtil.openURL("http://www.sk89q.com", self);
}
});
// Fetch notices
new Thread(new Runnable() {
@Override
public void run() {
final String notices = Launcher.getNotices();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
text.setText(notices);
}
});
}
}).start();
return panel;
}
}