summaryrefslogtreecommitdiff
path: root/src/com/pkrandom/newgui/CustomNamesEditorDialog.java
blob: 07954ed75558412cb7ec0cf8a401b6a7844b7e81 (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
package com.pkrandom.newgui;

/*----------------------------------------------------------------------------*/
/*--  CustomNamesEditorDialog.java - a GUI interface to allow users to edit --*/
/*--                                 their custom names for trainers etc.   --*/
/*--                                                                        --*/
/*--  Part of "Universal Pokemon Randomizer ZX" by the UPR-ZX team          --*/
/*--  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.pkrandom.CustomNamesSet;
import com.pkrandom.FileFunctions;
import com.pkrandom.SysConstants;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CustomNamesEditorDialog extends javax.swing.JDialog {

    private static final long serialVersionUID = -1421503126547242929L;
    private boolean pendingChanges;

    /**
     * Creates new form CustomNamesEditorDialog
     */
    public CustomNamesEditorDialog(java.awt.Frame parent) {
        super(parent, true);
        initComponents();
        setLocationRelativeTo(parent);

        java.awt.EventQueue.invokeLater(() -> setVisible(true));

        // load trainer names etc
        try {
            CustomNamesSet cns = FileFunctions.getCustomNames();
            populateNames(trainerNamesText, cns.getTrainerNames());
            populateNames(trainerClassesText, cns.getTrainerClasses());
            populateNames(doublesTrainerNamesText, cns.getDoublesTrainerNames());
            populateNames(doublesTrainerClassesText, cns.getDoublesTrainerClasses());
            populateNames(nicknamesText, cns.getPokemonNicknames());
        } catch (IOException ex) {
            java.awt.EventQueue.invokeLater(() -> JOptionPane.showMessageDialog(CustomNamesEditorDialog.this,
                    "Your custom names file is for a different randomizer version or otherwise corrupt."));
        }

        // dialog if there's no custom names file yet
        if (!new File(SysConstants.ROOT_PATH + SysConstants.customNamesFile).exists()) {
            java.awt.EventQueue.invokeLater(() -> JOptionPane.showMessageDialog(
                    CustomNamesEditorDialog.this,
                    String.format(
                            "Welcome to the custom names editor!\nThis is where you can edit the names used for options like \"Randomize Trainer Names\".\nThe names are initially populated with a few default names included with the randomizer.\nYou can share your customized name sets with others, too!\nJust send them the %s file created in the randomizer directory.",
                            SysConstants.customNamesFile)));
        }

        pendingChanges = false;

        addDocListener(trainerNamesText);
        addDocListener(trainerClassesText);
        addDocListener(doublesTrainerNamesText);
        addDocListener(doublesTrainerClassesText);
        addDocListener(nicknamesText);
    }

    private void addDocListener(JTextArea textArea) {
        textArea.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void insertUpdate(DocumentEvent e) {
                pendingChanges = true;
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                pendingChanges = true;
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                pendingChanges = true;
            }
        });

    }

    private void formWindowClosing() {// GEN-FIRST:event_formWindowClosing
        attemptClose();
    }// GEN-LAST:event_formWindowClosing

    private void saveBtnActionPerformed() {// GEN-FIRST:event_saveBtnActionPerformed
        save();
    }// GEN-LAST:event_saveBtnActionPerformed

    private void closeBtnActionPerformed() {// GEN-FIRST:event_closeBtnActionPerformed
        attemptClose();
    }// GEN-LAST:event_closeBtnActionPerformed

    private boolean save() {
        CustomNamesSet cns = new CustomNamesSet();
        cns.setTrainerNames(getNameList(trainerNamesText));
        cns.setTrainerClasses(getNameList(trainerClassesText));
        cns.setDoublesTrainerNames(getNameList(doublesTrainerNamesText));
        cns.setDoublesTrainerClasses(getNameList(doublesTrainerClassesText));
        cns.setPokemonNicknames(getNameList(nicknamesText));
        try {
            byte[] data = cns.getBytes();
            FileFunctions.writeBytesToFile(SysConstants.ROOT_PATH + SysConstants.customNamesFile, data);
            pendingChanges = false;
            JOptionPane.showMessageDialog(this, "Custom names saved.");
            return true;
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(this, "Could not save changes.");
            return false;
        }
    }

    private void attemptClose() {
        if (pendingChanges) {
            int result = JOptionPane
                    .showConfirmDialog(this,
                            "You've made some unsaved changes to your custom names.\nDo you want to save them before closing the editor?");
            if (result == JOptionPane.YES_OPTION) {
                if (save()) {
                    dispose();
                }
            } else if (result == JOptionPane.NO_OPTION) {
                dispose();
            }
        } else {
            dispose();
        }
    }

    private List<String> getNameList(JTextArea textArea) {
        String contents = textArea.getText();
        // standardize newlines
        contents = contents.replace("\r\n", "\n");
        contents = contents.replace("\r", "\n");
        // split by them
        String[] names = contents.split("\n");
        List<String> results = new ArrayList<>();
        for (String name : names) {
            String ln = name.trim();
            if (!ln.isEmpty()) {
                results.add(ln);
            }
        }
        return results;
    }

    private void populateNames(JTextArea textArea, List<String> names) {
        StringBuilder sb = new StringBuilder();
        boolean first = true;
        for (String name : names) {
            if (!first) {
                sb.append(SysConstants.LINE_SEP);
            }
            first = false;
            sb.append(name);
        }
        textArea.setText(sb.toString());
    }

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

        editorTabsPane = new javax.swing.JTabbedPane();
        trainerNamesSP = new javax.swing.JScrollPane();
        trainerNamesText = new JTextArea();
        trainerClassesSP = new javax.swing.JScrollPane();
        trainerClassesText = new JTextArea();
        doublesTrainerNamesSP = new javax.swing.JScrollPane();
        doublesTrainerNamesText = new JTextArea();
        doublesTrainerClassesSP = new javax.swing.JScrollPane();
        doublesTrainerClassesText = new JTextArea();
        nicknamesSP = new javax.swing.JScrollPane();
        nicknamesText = new JTextArea();
        saveBtn = new javax.swing.JButton();
        closeBtn = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("com/pkrandom/newgui/Bundle");
        setTitle(bundle.getString("CustomNamesEditorDialog.title"));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing();
            }
        });

        trainerNamesSP.setHorizontalScrollBar(null);

        trainerNamesText.setColumns(20);
        trainerNamesText.setRows(5);
        trainerNamesSP.setViewportView(trainerNamesText);

        editorTabsPane.addTab(bundle.getString("CustomNamesEditorDialog.trainerNamesSP.TabConstraints.tabTitle"), trainerNamesSP);

        trainerClassesSP.setHorizontalScrollBar(null);

        trainerClassesText.setColumns(20);
        trainerClassesText.setRows(5);
        trainerClassesSP.setViewportView(trainerClassesText);

        editorTabsPane.addTab(bundle.getString("CustomNamesEditorDialog.trainerClassesSP.TabConstraints.tabTitle"), trainerClassesSP);

        doublesTrainerNamesSP.setHorizontalScrollBar(null);

        doublesTrainerNamesText.setColumns(20);
        doublesTrainerNamesText.setRows(5);
        doublesTrainerNamesSP.setViewportView(doublesTrainerNamesText);

        editorTabsPane.addTab(bundle.getString("CustomNamesEditorDialog.doublesTrainerNamesSP.TabConstraints.tabTitle"), doublesTrainerNamesSP);

        doublesTrainerClassesSP.setHorizontalScrollBar(null);

        doublesTrainerClassesText.setColumns(20);
        doublesTrainerClassesText.setRows(5);
        doublesTrainerClassesSP.setViewportView(doublesTrainerClassesText);

        editorTabsPane.addTab(bundle.getString("CustomNamesEditorDialog.doublesTrainerClassesSP.TabConstraints.tabTitle"), doublesTrainerClassesSP);

        nicknamesSP.setHorizontalScrollBar(null);

        nicknamesText.setColumns(20);
        nicknamesText.setRows(5);
        nicknamesSP.setViewportView(nicknamesText);

        editorTabsPane.addTab(bundle.getString("CustomNamesEditorDialog.nicknamesSP.TabConstraints.tabTitle"), nicknamesSP);

        saveBtn.setText(bundle.getString("CustomNamesEditorDialog.saveBtn.text"));
        saveBtn.addActionListener(evt -> saveBtnActionPerformed());

        closeBtn.setText(bundle.getString("CustomNamesEditorDialog.closeBtn.text"));
        closeBtn.addActionListener(evt -> closeBtnActionPerformed());

        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.LEADING)
                    .addComponent(editorTabsPane, javax.swing.GroupLayout.DEFAULT_SIZE, 616, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(saveBtn)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(closeBtn)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(editorTabsPane, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(saveBtn)
                    .addComponent(closeBtn))
                .addContainerGap())
        );

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

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton closeBtn;
    private javax.swing.JScrollPane doublesTrainerClassesSP;
    private JTextArea doublesTrainerClassesText;
    private javax.swing.JScrollPane doublesTrainerNamesSP;
    private JTextArea doublesTrainerNamesText;
    private javax.swing.JTabbedPane editorTabsPane;
    private javax.swing.JScrollPane nicknamesSP;
    private JTextArea nicknamesText;
    private javax.swing.JButton saveBtn;
    private javax.swing.JScrollPane trainerClassesSP;
    private JTextArea trainerClassesText;
    private javax.swing.JScrollPane trainerNamesSP;
    private JTextArea trainerNamesText;
    // End of variables declaration//GEN-END:variables
    /* @formatter:on */
}