summaryrefslogtreecommitdiff
path: root/src/com/dabomstew/pkrandom/newgui/PresetLoadDialog.java
blob: 11b843630b8d160c4fc5d4ee5fcde5aaaed93ea5 (plain)
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dabomstew.pkrandom.newgui;

/*----------------------------------------------------------------------------*/
/*--  PresetLoadDialog.java - a dialog to allow use of preset files or      --*/
/*--                          random seed/config string pairs to produce    --*/
/*--                          premade ROMs.                                 --*/
/*--                                                                        --*/
/*--  Part of "Universal Pokemon Randomizer ZX" by the UPR-ZX team          --*/
/*--  Originally part of "Universal Pokemon Randomizer" by Dabomstew        --*/
/*--  Pokemon and any associated names and the like are                     --*/
/*--  trademark and (C) Nintendo 1996-2020.                                 --*/
/*--                                                                        --*/
/*--  The custom code written here is licensed under the terms of the GPL:  --*/
/*--                                                                        --*/
/*--  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/>.  --*/
/*----------------------------------------------------------------------------*/

import com.dabomstew.pkrandom.*;
import com.dabomstew.pkrandom.exceptions.InvalidSupplementFilesException;
import com.dabomstew.pkrandom.romhandlers.Abstract3DSRomHandler;
import com.dabomstew.pkrandom.romhandlers.RomHandler;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * 
 * @author Stewart
 */
public class PresetLoadDialog extends JDialog {

    /**
     *
     */
    private static final long serialVersionUID = -7898067118947765260L;
    private NewRandomizerGUI parentGUI;
    private RomHandler currentROM;
    private boolean completed = false;
    private String requiredName = null;
    private volatile boolean changeFieldsWithoutCheck = false;
    private CustomNamesSet customNames;
    private java.util.ResourceBundle bundle;

    /**
     * Creates new form PresetLoadDialog
     */
    public PresetLoadDialog(NewRandomizerGUI parent, JFrame frame) {
        super(frame, true);
        bundle = java.util.ResourceBundle.getBundle("com/dabomstew/pkrandom/newgui/Bundle"); // NOI18N
        initComponents();
        this.parentGUI = parent;
        this.presetFileChooser.setCurrentDirectory(new File("./"));
        this.romFileChooser.setCurrentDirectory(new File("./"));
        initialState();
        setLocationRelativeTo(frame);
        setVisible(true);
    }

    private void initialState() {
        this.romFileButton.setEnabled(false);
        this.acceptButton.setEnabled(false);
        addChangeListener(this.randomSeedField);
        addChangeListener(this.configStringField);
    }

    private void addChangeListener(JTextField field) {
        field.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void insertUpdate(DocumentEvent e) {
                if (!changeFieldsWithoutCheck)
                    PresetLoadDialog.this.checkValues();

            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                if (!changeFieldsWithoutCheck)
                    PresetLoadDialog.this.checkValues();

            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                if (!changeFieldsWithoutCheck)
                    PresetLoadDialog.this.checkValues();

            }
        });

    }

    private boolean checkValues() {
        String name;
        try {
            Long.parseLong(this.randomSeedField.getText());
        } catch (NumberFormatException ex) {
            invalidValues();
            return false;
        }

        // 161 onwards: look for version number
        String configString = this.configStringField.getText();
        if (configString.length() < 3) {
            invalidValues();
            return false;
        }

        try {
            int presetVersionNumber = Integer.parseInt(configString.substring(0, 3));
            if (presetVersionNumber != Version.VERSION) {
                promptForDifferentRandomizerVersion(presetVersionNumber);
                safelyClearFields();
                invalidValues();
                return false;
            }
        } catch (NumberFormatException ex) {
            invalidValues();
            return false;
        }

        try {
            name = this.parentGUI.getValidRequiredROMName(configString.substring(3), customNames);
        } catch (InvalidSupplementFilesException ex) {
            safelyClearFields();
            invalidValues();
            return false;
        } catch (Exception ex) {
            // other exception, just call it invalid for now
            invalidValues();
            return false;
        }
        if (name == null) {
            invalidValues();
            return false;
        }
        requiredName = name;
        this.romRequiredLabel.setText(String.format(bundle.getString("PresetLoadDialog.romRequiredLabel.textWithROM"),
                name));
        this.romFileButton.setEnabled(true);

        if (currentROM != null && !currentROM.getROMName().equals(name)) {
            this.currentROM = null;
            this.acceptButton.setEnabled(false);
            this.romFileField.setText("");
        }
        return true;
    }

    private void promptForDifferentRandomizerVersion(int presetVN) {
        // so what version number was it?
        if (presetVN > Version.VERSION) {
            // it's for a newer version
            JOptionPane.showMessageDialog(this, bundle.getString("PresetLoadDialog.newerVersionRequired"));
        } else {
            // tell them which older version to use to load this preset
            // this should be the newest version that used that value
            // for the constant PRESET_FILE_VERSION
            String versionWanted = Version.oldVersions.getOrDefault(presetVN,"Unknown");
            JOptionPane.showMessageDialog(this,
                    String.format(bundle.getString("PresetLoadDialog.olderVersionRequired"), versionWanted));
        }
    }

    private void safelyClearFields() {
        SwingUtilities.invokeLater(() -> {
            changeFieldsWithoutCheck = true;
            configStringField.setText("");
            randomSeedField.setText("");
            changeFieldsWithoutCheck = false;
        });
    }

    private void invalidValues() {
        this.currentROM = null;
        this.romFileField.setText("");
        this.romRequiredLabel.setText(bundle.getString("PresetLoadDialog.romRequiredLabel.text"));
        this.romFileButton.setEnabled(false);
        this.acceptButton.setEnabled(false);
        this.requiredName = null;

    }

    public boolean isCompleted() {
        return completed;
    }

    public RomHandler getROM() {
        return currentROM;
    }

    public long getSeed() {
        return Long.parseLong(this.randomSeedField.getText());
    }

    public String getConfigString() {
        return this.configStringField.getText().substring(3);
    }

    public CustomNamesSet getCustomNames() {
        return customNames;
    }

    private void presetFileButtonActionPerformed() {// GEN-FIRST:event_presetFileButtonActionPerformed
        presetFileChooser.setSelectedFile(null);
        int returnVal = presetFileChooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File fh = presetFileChooser.getSelectedFile();
            try {
                DataInputStream dis = new DataInputStream(new FileInputStream(fh));
                int checkInt = dis.readInt();
                if (checkInt != Version.VERSION) {
                    dis.close();
                    promptForDifferentRandomizerVersion(checkInt);
                    return;
                }
                long seed = dis.readLong();
                String preset = dis.readUTF();
                customNames = new CustomNamesSet(dis);
                changeFieldsWithoutCheck = true;
                this.randomSeedField.setText(Long.toString(seed));
                this.configStringField.setText(checkInt + "" + preset);
                changeFieldsWithoutCheck = false;
                if (checkValues()) {
                    this.randomSeedField.setEnabled(false);
                    this.configStringField.setEnabled(false);
                    this.presetFileField.setText(fh.getAbsolutePath());
                } else {
                    this.randomSeedField.setText("");
                    this.configStringField.setText("");
                    this.randomSeedField.setEnabled(true);
                    this.configStringField.setEnabled(true);
                    this.presetFileField.setText("");
                    customNames = null;
                    JOptionPane.showMessageDialog(this, bundle.getString("PresetLoadDialog.invalidSeedFile"));
                }
                dis.close();
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this, bundle.getString("PresetLoadDialog.loadingSeedFileFailed"));
            }
        }
    }// GEN-LAST:event_presetFileButtonActionPerformed

    private void romFileButtonActionPerformed() {// GEN-FIRST:event_romFileButtonActionPerformed
        romFileChooser.setSelectedFile(null);
        int returnVal = romFileChooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            final File fh = romFileChooser.getSelectedFile();
            for (RomHandler.Factory rhf : parentGUI.checkHandlers) {
                if (rhf.isLoadable(fh.getAbsolutePath())) {
                    final RomHandler checkHandler = rhf.create(RandomSource.instance());
                    if (!NewRandomizerGUI.usedLauncher && checkHandler instanceof Abstract3DSRomHandler) {
                        String message = bundle.getString("GUI.pleaseUseTheLauncher");
                        Object[] messages = {message};
                        JOptionPane.showMessageDialog(this, messages);
                        return;
                    }
                    final JDialog opDialog = new OperationDialog(bundle.getString("GUI.loadingText"), this,
                            true);
                    Thread t = new Thread(() -> {
                        SwingUtilities.invokeLater(() -> opDialog.setVisible(true));
                        try {
                            checkHandler.loadRom(fh.getAbsolutePath());
                        } catch (Exception ex) {
                            JOptionPane.showMessageDialog(PresetLoadDialog.this,
                                    bundle.getString("GUI.loadFailedNoLog"));
                        }
                        SwingUtilities.invokeLater(() -> {
                            opDialog.setVisible(false);
                            if (checkHandler.getROMName().equals(requiredName)) {
                                // Got it
                                romFileField.setText(fh.getAbsolutePath());
                                currentROM = checkHandler;
                                acceptButton.setEnabled(true);
                                return;
                            } else {
                                JOptionPane.showMessageDialog(PresetLoadDialog.this, String.format(
                                        bundle.getString("PresetLoadDialog.notRequiredROM"), requiredName,
                                        checkHandler.getROMName()));
                                return;
                            }
                        });
                    });
                    t.start();
                    return;
                }
            }
            JOptionPane.showMessageDialog(this,
                    String.format(bundle.getString("GUI.unsupportedRom"), fh.getName()));
        }
    }// GEN-LAST:event_romFileButtonActionPerformed

    private void acceptButtonActionPerformed() {// GEN-FIRST:event_acceptButtonActionPerformed
        if (customNames == null) {
            try {
                customNames = FileFunctions.getCustomNames();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        completed = true;
        this.setVisible(false);
    }// GEN-LAST:event_acceptButtonActionPerformed

    private void cancelButtonActionPerformed() {// GEN-FIRST:event_cancelButtonActionPerformed
        completed = false;
        this.setVisible(false);
    }// GEN-LAST:event_cancelButtonActionPerformed

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed"
    // desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        presetFileChooser = new JFileChooser();
        romFileChooser = new JFileChooser();
        presetFileLabel = new javax.swing.JLabel();
        presetFileField = new JTextField();
        presetFileButton = new javax.swing.JButton();
        orLabel = new javax.swing.JLabel();
        seedBoxLabel = new javax.swing.JLabel();
        randomSeedField = new JTextField();
        configStringBoxLabel = new javax.swing.JLabel();
        configStringField = new JTextField();
        romRequiredLabel = new javax.swing.JLabel();
        romFileBoxLabel = new javax.swing.JLabel();
        romFileField = new JTextField();
        romFileButton = new javax.swing.JButton();
        acceptButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        presetFileChooser.setFileFilter(new PresetFileFilter());

        romFileChooser.setFileFilter(new ROMFilter());

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("com/dabomstew/pkrandom/newgui/Bundle"); // NOI18N
        setTitle(bundle.getString("PresetLoadDialog.title")); // NOI18N
        setModal(true);
        setResizable(false);

        presetFileLabel.setText(bundle.getString("PresetLoadDialog.presetFileLabel.text")); // NOI18N

        presetFileField.setEditable(false);

        presetFileButton.setText(bundle.getString("PresetLoadDialog.presetFileButton.text")); // NOI18N
        presetFileButton.addActionListener(evt -> presetFileButtonActionPerformed());

        orLabel.setFont(new java.awt.Font("Tahoma", Font.BOLD, 11)); // NOI18N
        orLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        orLabel.setText(bundle.getString("PresetLoadDialog.orLabel.text")); // NOI18N

        seedBoxLabel.setText(bundle.getString("PresetLoadDialog.seedBoxLabel.text")); // NOI18N

        configStringBoxLabel.setText(bundle.getString("PresetLoadDialog.configStringBoxLabel.text")); // NOI18N

        romRequiredLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        romRequiredLabel.setText(bundle.getString("PresetLoadDialog.romRequiredLabel.text")); // NOI18N

        romFileBoxLabel.setText(bundle.getString("PresetLoadDialog.romFileBoxLabel.text")); // NOI18N

        romFileField.setEditable(false);

        romFileButton.setText(bundle.getString("PresetLoadDialog.romFileButton.text")); // NOI18N
        romFileButton.addActionListener(evt -> romFileButtonActionPerformed());

        acceptButton.setText(bundle.getString("PresetLoadDialog.acceptButton.text")); // NOI18N
        acceptButton.addActionListener(evt -> acceptButtonActionPerformed());

        cancelButton.setText(bundle.getString("PresetLoadDialog.cancelButton.text")); // NOI18N
        cancelButton.addActionListener(evt -> cancelButtonActionPerformed());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(
                        layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(
                                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                .addComponent(romFileBoxLabel,
                                                        javax.swing.GroupLayout.Alignment.LEADING,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(presetFileLabel,
                                                        javax.swing.GroupLayout.Alignment.LEADING,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(seedBoxLabel, javax.swing.GroupLayout.Alignment.LEADING,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(configStringBoxLabel,
                                                        javax.swing.GroupLayout.Alignment.LEADING,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGap(18, 18, 18)
                                .addGroup(
                                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(
                                                        layout.createSequentialGroup()
                                                                .addComponent(acceptButton)
                                                                .addPreferredGap(
                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                                        169, Short.MAX_VALUE)
                                                                .addComponent(cancelButton))
                                                .addComponent(randomSeedField).addComponent(configStringField)
                                                .addComponent(presetFileField).addComponent(romFileField))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(
                                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                .addComponent(presetFileButton, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        26, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(romFileButton, javax.swing.GroupLayout.PREFERRED_SIZE, 1,
                                                        Short.MAX_VALUE)).addGap(12, 12, 12))
                .addComponent(orLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE)
                .addComponent(romRequiredLabel, javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(
                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(presetFileLabel)
                                        .addComponent(presetFileField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(presetFileButton))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(orLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(
                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(seedBoxLabel)
                                        .addComponent(randomSeedField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(
                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(configStringBoxLabel)
                                        .addComponent(configStringField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(romRequiredLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(
                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(romFileBoxLabel)
                                        .addComponent(romFileField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(romFileButton))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
                        .addGroup(
                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(acceptButton).addComponent(cancelButton)).addContainerGap()));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton acceptButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel configStringBoxLabel;
    private JTextField configStringField;
    private javax.swing.JLabel orLabel;
    private javax.swing.JButton presetFileButton;
    private JFileChooser presetFileChooser;
    private JTextField presetFileField;
    private javax.swing.JLabel presetFileLabel;
    private JTextField randomSeedField;
    private javax.swing.JLabel romFileBoxLabel;
    private javax.swing.JButton romFileButton;
    private JFileChooser romFileChooser;
    private JTextField romFileField;
    private javax.swing.JLabel romRequiredLabel;
    private javax.swing.JLabel seedBoxLabel;
    // End of variables declaration//GEN-END:variables
}