summaryrefslogtreecommitdiff
path: root/src/com/sneed/pkrandom/newgui/PresetMakeDialog.java
blob: 27f8850fa63a3f2c3d2d46cbde1544efd5435388 (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
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sneed.pkrandom.newgui;

/*----------------------------------------------------------------------------*/
/*--  PresetMakeDialog.java - a dialog to allow preset pairs to either be   --*/
/*--                          copied down or saved to a binary file for     --*/
/*--                          later use.                                    --*/
/*--                                                                        --*/
/*--  Part of "Universal Pokemon Randomizer ZX" by the UPR-ZX team          --*/
/*--  Originally part of "Universal Pokemon Randomizer" by sneed        --*/
/*--  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.sneed.pkrandom.FileFunctions;
import com.sneed.pkrandom.Settings;
import com.sneed.pkrandom.SysConstants;
import com.sneed.pkrandom.Version;

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.*;

/**
 * 
 * @author Stewart
 */
public class PresetMakeDialog extends javax.swing.JDialog {
    /**
     * 
     */
    private static final long serialVersionUID = 7663903108783731673L;
    private long seed;
    private String configString;

    /**
     * Creates new form PresetMakeDialog
     */
    public PresetMakeDialog(java.awt.Frame parent, long seed, String configString) {
        super(parent, true);
        initComponents();
        randomSeedField.setText(Long.toString(seed));
        configStringField.setText(Version.VERSION + "" + configString);
        this.seed = seed;
        this.configString = configString;
        presetFileChooser.setCurrentDirectory(new File("./"));
        this.randomSeedField.addMouseListener(new SelectTextListener(this.randomSeedField));
        this.configStringField.addMouseListener(new SelectTextListener(this.configStringField));
        setLocationRelativeTo(parent);
        setVisible(true);
    }

    /**
     * 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"
    // <editor-fold defaultstate="collapsed"
    // desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        presetFileChooser = new JFileChooser();
        gameRandomizedLabel = new javax.swing.JLabel();
        settingsToGiveLabel = new javax.swing.JLabel();
        seedFieldLabel = new javax.swing.JLabel();
        randomSeedField = new JTextField();
        configStringFieldLabel = new javax.swing.JLabel();
        configStringField = new JTextField();
        canProduceFileLabel = new javax.swing.JLabel();
        produceFileButton = new javax.swing.JButton();
        doneButton = new javax.swing.JButton();

        presetFileChooser.setFileFilter(new PresetFileFilter());

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

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

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

        seedFieldLabel.setText(bundle.getString("PresetMakeDialog.seedFieldLabel.text")); // NOI18N

        randomSeedField.setEditable(false);

        configStringFieldLabel.setText(bundle.getString("PresetMakeDialog.configStringFieldLabel.text")); // NOI18N

        configStringField.setEditable(false);

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

        produceFileButton.setText(bundle.getString("PresetMakeDialog.produceFileButton.text")); // NOI18N
        produceFileButton.addActionListener(evt -> produceFileButtonActionPerformed());

        doneButton.setText(bundle.getString("PresetMakeDialog.doneButton.text")); // NOI18N
        doneButton.addActionListener(evt -> doneButtonActionPerformed());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(gameRandomizedLabel, javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(settingsToGiveLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 599, Short.MAX_VALUE)
                .addGroup(
                        layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(
                                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(seedFieldLabel).addComponent(configStringFieldLabel))
                                .addGap(18, 18, 18)
                                .addGroup(
                                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(randomSeedField).addComponent(configStringField))
                                .addContainerGap())
                .addComponent(canProduceFileLabel, javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(
                        layout.createSequentialGroup()
                                .addGap(67, 67, 67)
                                .addComponent(produceFileButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(doneButton)
                                .addGap(66, 66, 66)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                layout.createSequentialGroup()
                        .addComponent(gameRandomizedLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(settingsToGiveLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(
                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(seedFieldLabel)
                                        .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(configStringFieldLabel)
                                        .addComponent(configStringField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(canProduceFileLabel)
                        .addGap(18, 18, 18)
                        .addGroup(
                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(produceFileButton).addComponent(doneButton))
                        .addGap(0, 11, Short.MAX_VALUE)));

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

    private void produceFileButtonActionPerformed() {// GEN-FIRST:event_produceFileButtonActionPerformed
        presetFileChooser.setSelectedFile(null);
        int returnVal = presetFileChooser.showSaveDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File fh = presetFileChooser.getSelectedFile();
            // Fix extension?
            fh = FileFunctions.fixFilename(fh, "rndp");
            try {
                DataOutputStream dos = new DataOutputStream(new FileOutputStream(fh));
                dos.writeInt(Version.VERSION);
                dos.writeLong(seed);
                dos.writeUTF(configString);
                byte[] customnames = readFile(FileFunctions.openConfig(SysConstants.customNamesFile));
                dos.write(customnames);
                dos.close();
                JOptionPane.showMessageDialog(this, "Preset file saved to\n" + fh.getAbsolutePath());
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this, "Could not save the preset file.");
            }
        }
    }// GEN-LAST:event_produceFileButtonActionPerformed

    private static byte[] readFile(InputStream is) throws IOException {
        byte[] file = FileFunctions.readFullyIntoBuffer(is, is.available());
        is.close();
        return file;
    }

    private void doneButtonActionPerformed() {// GEN-FIRST:event_doneButtonActionPerformed
        this.setVisible(false);
    }// GEN-LAST:event_doneButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel canProduceFileLabel;
    private JTextField configStringField;
    private javax.swing.JLabel configStringFieldLabel;
    private javax.swing.JButton doneButton;
    private javax.swing.JLabel gameRandomizedLabel;
    private JFileChooser presetFileChooser;
    private javax.swing.JButton produceFileButton;
    private JTextField randomSeedField;
    private javax.swing.JLabel seedFieldLabel;
    private javax.swing.JLabel settingsToGiveLabel;

    // End of variables declaration//GEN-END:variables

    public class SelectTextListener implements MouseListener {

        private JTextField fieldFor;

        public SelectTextListener(JTextField fieldFor) {
            this.fieldFor = fieldFor;
        }

        @Override
        public void mouseClicked(MouseEvent arg0) {
            // select all text
            SwingUtilities.invokeLater(() -> fieldFor.selectAll());
        }

        @Override
        public void mouseEntered(MouseEvent arg0) {
            // do nothing

        }

        @Override
        public void mouseExited(MouseEvent arg0) {
            // do nothing

        }

        @Override
        public void mousePressed(MouseEvent arg0) {
            // do nothing

        }

        @Override
        public void mouseReleased(MouseEvent arg0) {
            // do nothing

        }

    }
}