-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathShuffleDialog.java
More file actions
463 lines (399 loc) · 18.8 KB
/
ShuffleDialog.java
File metadata and controls
463 lines (399 loc) · 18.8 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
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
package io.github.arrayv.dialogs;
import io.github.arrayv.frames.AppFrame;
import io.github.arrayv.main.ArrayManager;
import io.github.arrayv.panels.ShufflePanel;
import io.github.arrayv.panes.JErrorPane;
import io.github.arrayv.utils.Distributions;
import io.github.arrayv.utils.ShuffleGraph;
import io.github.arrayv.utils.ShuffleInfo;
import io.github.arrayv.utils.Shuffles;
import io.github.arrayv.utils.shuffleutils.GraphReader;
import io.github.arrayv.utils.shuffleutils.GraphReader.MalformedGraphFileException;
import io.github.arrayv.utils.shuffleutils.GraphWriter;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/*
*
MIT License
Copyright (c) 2019 w0rthy
Copyright (c) 2021-2022 ArrayV Team
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*
*/
public final class ShuffleDialog extends javax.swing.JDialog implements AppFrame {
private static final long serialVersionUID = 1L;
private final ArrayManager arrayManager;
private final List<Distributions> distributions;
private static boolean perShuffleDelay = false;
private boolean bypassEvents;
/**
* Creates new form SortPrompt
*/
@SuppressWarnings("unchecked")
public ShuffleDialog(ArrayManager arrayManager, JFrame frame) {
super(frame, "ArrayV Advanced Shuffle Editor", true);
this.arrayManager = arrayManager;
initComponents();
bypassEvents = true;
this.shuffleEditor.setShuffle(arrayManager.getShuffle());
jList4.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jList4.setListData(arrayManager.getDistributionIDs());
for (int i = 0; i < arrayManager.getDistributions().length; i++) {
if (arrayManager.getDistribution().equals(arrayManager.getDistributions()[i])) {
jList4.setSelectedIndex(i);
break;
}
}
distributions = Arrays.stream(arrayManager.getDistributions())
.filter(dist -> dist != Distributions.CUSTOM)
.collect(Collectors.toList());
Object[] distributionNames = distributions.stream()
.map(Distributions::getName).toArray();
jList1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jList1.setListData(distributionNames);
jList3.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jList3.setListData(distributionNames);
jList2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jList2.setListData(arrayManager.getShuffleIDs());
jTextField1.setText(Double.toString(
perShuffleDelay ?
shuffleEditor.getShuffle().getSleepRatio() / shuffleEditor.getShuffle().size() :
shuffleEditor.getShuffle().getSleepRatio()
));
jCheckBox1.setSelected(perShuffleDelay);
bypassEvents = false;
addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
perShuffleDelay = jCheckBox1.isSelected();
if (perShuffleDelay) {
shuffleEditor.getShuffle().setSleepRatio(shuffleEditor.getShuffle().getSleepRatio() * shuffleEditor.getShuffle().size());
}
}
});
setMinimumSize(new Dimension(765, 310));
setAlwaysOnTop(false);
reposition();
setVisible(true);
}
@Override
public void reposition() {
}
/**
* 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({ "rawtypes" })
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
this.shuffleEditor = new ShufflePanel();
javax.swing.JButton jButton1 = new javax.swing.JButton();
javax.swing.JButton jButton2 = new javax.swing.JButton();
javax.swing.JButton jButton3 = new javax.swing.JButton();
this.jTextField1 = new javax.swing.JTextField();
javax.swing.JLabel jLabel5 = new javax.swing.JLabel();
this.jCheckBox1 = new javax.swing.JCheckBox();
javax.swing.JScrollPane jScrollPane4 = new javax.swing.JScrollPane();
this.jList4 = new javax.swing.JList();
javax.swing.JLabel jLabel4 = new javax.swing.JLabel();
javax.swing.JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
this.jList1 = new javax.swing.JList();
javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
javax.swing.JScrollPane jScrollPane3 = new javax.swing.JScrollPane();
this.jList3 = new javax.swing.JList();
javax.swing.JLabel jLabel3 = new javax.swing.JLabel();
javax.swing.JScrollPane jScrollPane2 = new javax.swing.JScrollPane();
this.jList2 = new javax.swing.JList();
javax.swing.JLabel jLabel2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
jButton1.setText("Import...");
jButton1.addActionListener(evt -> jButton1ActionPerformed());
jButton2.setText("Export...");
jButton2.addActionListener(evt -> jButton2ActionPerformed());
jButton3.setText("Clear Disconnected Nodes");
jButton3.addActionListener(evt -> jButton3ActionPerformed());
jLabel5.setText("Sleep Ratio");
jTextField1.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
jTextField1TextChanged();
}
@Override
public void removeUpdate(DocumentEvent e) {
jTextField1TextChanged();
}
@Override
public void changedUpdate(DocumentEvent e) {
jTextField1TextChanged();
}
});
jCheckBox1.setText("Time per sub-shuffle");
jScrollPane4.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
jScrollPane4.setViewportView(this.jList4);
jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
jScrollPane1.setViewportView(this.jList1);
jScrollPane3.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
jScrollPane3.setViewportView(this.jList3);
jScrollPane2.setViewportView(this.jList2);
jScrollPane2.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
jList4.addListSelectionListener(evt -> {
try {
jList4ValueChanged();
} catch (Exception e) {
JErrorPane.invokeErrorMessage(e);
}
});
jLabel4.setText("Base Distribution");
jList1.addListSelectionListener(evt -> {
try {
jList1ValueChanged();
} catch (Exception e) {
JErrorPane.invokeErrorMessage(e);
}
});
jLabel1.setText("Distribution Stretch");
jList3.addListSelectionListener(evt -> {
try {
jList3ValueChanged();
} catch (Exception e) {
JErrorPane.invokeErrorMessage(e);
}
});
jLabel3.setText("Distribution Warp");
jList2.addListSelectionListener(evt -> {
try {
jList2ValueChanged();
} catch (Exception e) {
JErrorPane.invokeErrorMessage(e);
}
});
jLabel2.setText("Shuffle");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createSequentialGroup()
.addGap(10, 10, 10)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
.addComponent(jLabel4)
.addComponent(jScrollPane4, 175, 175, 175)
.addComponent(jButton3)
.addComponent(jLabel5)
.addComponent(this.jTextField1)
.addComponent(this.jCheckBox1))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
.addGroup(layout.createSequentialGroup()
.addGap(10, 10, 10)
.addComponent(this.shuffleEditor)
.addGap(10, 10, 10))
.addGroup(layout.createSequentialGroup()
.addGap(10, 75, 75)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
.addComponent(jLabel1)
.addComponent(jScrollPane1, 175, 175, 175))
.addGap(10, 75, 75)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
.addComponent(jLabel3)
.addComponent(jScrollPane3, 175, 175, 175))
.addGap(10, 75, 75)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
.addComponent(jLabel2)
.addComponent(jScrollPane2, 175, 175, 175))
.addGap(10, 75, 75))
.addGroup(layout.createSequentialGroup()
.addGap(150, 150, 150)
.addComponent(jButton1)
.addGap(20, 20, 20)
.addComponent(jButton2)
.addGap(150, 150, 150)))
);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(10, 30, 30)
.addComponent(jLabel4)
.addComponent(jScrollPane4, 175, 175, 175)
.addGap(20, 20, 20)
.addComponent(jButton3)
.addGap(20, 20, 20)
.addComponent(jLabel5)
.addComponent(this.jTextField1, 20, 20, 20)
.addComponent(this.jCheckBox1))
.addGroup(layout.createSequentialGroup()
.addGap(10, 10, 10)
.addComponent(this.shuffleEditor)
.addGap(10, 10, 10)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addComponent(jScrollPane1, 175, 175, 175))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel3)
.addComponent(jScrollPane3, 175, 175, 175))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addComponent(jScrollPane2, 175, 175, 175)))
.addGap(10, 10, 10)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
.addComponent(jButton1)
.addComponent(jButton2))
.addGap(10, 10, 10))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jButton1ActionPerformed() {//GEN-FIRST:event_jButton1ActionPerformed
FileDialog fileDialog = new ImportShuffleDialog();
ShuffleGraph newShuffle;
try {
newShuffle = new GraphReader().read(fileDialog.file);
} catch (IOException e) {
e.printStackTrace();
JErrorPane.invokeCustomErrorMessage("IO Error: " + e.getMessage());
return;
} catch (MalformedGraphFileException e) {
e.printStackTrace();
JErrorPane.invokeCustomErrorMessage("Error Parsing File: " + e.getMessage());
return;
}
arrayManager.setShuffle(newShuffle);
if (jCheckBox1.isSelected()) {
shuffleEditor.getShuffle().setSleepRatio(shuffleEditor.getShuffle().getSleepRatio() / shuffleEditor.getShuffle().size());
}
jTextField1.setForeground(Color.BLACK);
jTextField1.setText(Double.toString(newShuffle.getSleepRatio()));
this.shuffleEditor.setShuffle(newShuffle);
this.shuffleEditor.repaint();
}//GEN-LAST:event_jButton1ActionPerformed
private void jButton2ActionPerformed() {//GEN-FIRST:event_jButton1ActionPerformed
FileDialog fileDialog = new ExportShuffleDialog();
double oldSleepRatio = shuffleEditor.getShuffle().getSleepRatio();
if (jCheckBox1.isSelected()) {
shuffleEditor.getShuffle().setSleepRatio(shuffleEditor.getShuffle().getSleepRatio() * shuffleEditor.getShuffle().size());
}
try {
new GraphWriter(shuffleEditor.getShuffle()).write(fileDialog.file);
} catch (IOException e) {
shuffleEditor.getShuffle().setSleepRatio(oldSleepRatio);
e.printStackTrace();
JErrorPane.invokeCustomErrorMessage("IO Error: " + e.getMessage());
return;
}
shuffleEditor.getShuffle().setSleepRatio(oldSleepRatio);
JOptionPane.showMessageDialog(null,
"Successfully exported current shuffle to file \"" + fileDialog.file.getAbsolutePath() + "\"",
"Advanced Shuffle Editor", JOptionPane.INFORMATION_MESSAGE);
}//GEN-LAST:event_jButton1ActionPerformed
private void jButton3ActionPerformed() {//GEN-FIRST:event_jButton1ActionPerformed
shuffleEditor.getShuffle().removeAllDisconnected();
shuffleEditor.repaint();
}//GEN-LAST:event_jButton1ActionPerformed
private void jTextField1TextChanged() {//GEN-FIRST:event_jList1ValueChanged
String text = jTextField1.getText();
if (text.length() == 0) return;
double sleepRatio;
try {
sleepRatio = Double.parseDouble(text);
} catch (NumberFormatException ex) {
jTextField1.setForeground(new Color(204, 0, 0));
return;
}
jTextField1.setForeground(Color.BLACK);
shuffleEditor.getShuffle().setSleepRatio(sleepRatio);
}//GEN-LAST:event_jList1ValueChanged
private void addToGraph(ShuffleInfo shuffle) {
Point safePos = shuffleEditor.getShuffle().findSafeCoordinates(100, 100, 20, 20);
shuffleEditor.getShuffle().addDisconnected(shuffle, safePos.x, safePos.y);
}
private int jList4PrevSelection;
// Base Distribution
private void jList4ValueChanged() {//GEN-FIRST:event_jList1ValueChanged
if (bypassEvents || jList4.getValueIsAdjusting())
return;
int selection = jList4.getSelectedIndex();
Distributions[] distributions = arrayManager.getDistributions();
if (selection >= 0 && selection < distributions.length) {
if (arrayManager.setDistribution(distributions[selection])) {
jList4PrevSelection = selection;
} else {
// Selection failed for whatever reason. Need to revert to the previous selection.
bypassEvents = true;
jList4.setSelectedIndex(jList4PrevSelection);
bypassEvents = false;
}
}
}//GEN-LAST:event_jList1ValueChanged
// Distribution Stretch
private void jList1ValueChanged() {//GEN-FIRST:event_jList1ValueChanged
if (bypassEvents || jList1.getValueIsAdjusting())
return;
String selection = (String)jList1.getSelectedValue();
distributions.stream()
.filter(d -> d.getName().equals(selection))
.findFirst().ifPresent(distribution -> addToGraph(new ShuffleInfo(distribution, false)));
shuffleEditor.repaint();
bypassEvents = true;
jList1.clearSelection();
bypassEvents = false;
}//GEN-LAST:event_jList1ValueChanged
// Distribution Warp
private void jList3ValueChanged() {//GEN-FIRST:event_jList1ValueChanged
if (bypassEvents || jList3.getValueIsAdjusting())
return;
String selection = (String)jList3.getSelectedValue();
distributions.stream()
.filter(d -> d.getName().equals(selection))
.findFirst().ifPresent(distribution -> addToGraph(new ShuffleInfo(distribution, true)));
shuffleEditor.repaint();
bypassEvents = true;
jList3.clearSelection();
bypassEvents = false;
}//GEN-LAST:event_jList1ValueChanged
// Shuffle
private void jList2ValueChanged() {//GEN-FIRST:event_jList1ValueChanged
if (bypassEvents || jList2.getValueIsAdjusting())
return;
int selection = jList2.getSelectedIndex();
Shuffles[] shuffles = arrayManager.getShuffles();
if (selection >= 0 && selection < shuffles.length)
addToGraph(new ShuffleInfo(shuffles[selection]));
shuffleEditor.repaint();
bypassEvents = true;
jList2.clearSelection();
bypassEvents = false;
}//GEN-LAST:event_jList1ValueChanged
// Variables declaration - do not modify//GEN-BEGIN:variables
private ShufflePanel shuffleEditor;
private javax.swing.JTextField jTextField1;
private javax.swing.JCheckBox jCheckBox1;
@SuppressWarnings("rawtypes")
private javax.swing.JList jList4;
@SuppressWarnings("rawtypes")
private javax.swing.JList jList1;
@SuppressWarnings("rawtypes")
private javax.swing.JList jList3;
@SuppressWarnings("rawtypes")
private javax.swing.JList jList2;
// End of variables declaration//GEN-END:variables
}