-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserInterface.java
More file actions
625 lines (558 loc) · 22.6 KB
/
Copy pathUserInterface.java
File metadata and controls
625 lines (558 loc) · 22.6 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
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.border.SoftBevelBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.io.File;
public class UserInterface {
private static final ArrayList<DataObj> campaigns = new ArrayList<>();
private int campaignNum;
private ArrayList<DataObj> data;
private int dataNum;
private ArrayList<GraphParam> params;
private boolean isTimeGraph;
private boolean isGregorian;
private boolean lowPassFilter;
private JPanel chartPanel;
private JPanel buttonPanel;
private JPanel outputPanel;
private JLabel contentLabel;
private JLabel algPane;
private JScrollPane campaignDataSP;
private final Container pane;
private GraphComplexCurve complexCurve;
private boolean run;
private boolean ranAlg;
private double accuracy; //reads from a file that is updated when algorithm tests; value from 0 to 1
// void setCampaignNum(int myCampaignNum, final boolean startAtBeginning) {
// if (myCampaignNum < 0 || myCampaignNum >= 22) {
// myCampaignNum = 0;
// }
// campaignNum = myCampaignNum;
//
// SwingWorker sw1 = new SwingWorker() {
//
// @Override
// protected String doInBackground() throws Exception {
// // define what thread will do here
// data = campaigns.get(campaignNum).readURL();
// dataNum = startAtBeginning ? 0 : data.size() - 1;
// return "Finished";
// }
//
// @Override
// protected void done() {
// // this method is called when the background thread finishes execution
// setScrollPane();
// graph();
// namePanel();
// }
// };
// // executes the swingworker on worker thread
// sw1.execute();
// }
void setCampaignNum(int myCampaignNum, boolean startAtBeginning) {
if (myCampaignNum < 0 || myCampaignNum >= 22) {
myCampaignNum = 0;
}
campaignNum = myCampaignNum;
data = campaigns.get(campaignNum).readURL();
dataNum = startAtBeginning ? 0 : data.size() - 1;
setScrollPane();
graph();
namePanel();
}
void setDataNum(int num) {
if (num >= 0 && num < data.size()) {
dataNum = num;
graph();
namePanel();
} else if (num < 0 && campaignNum > 0) {
setCampaignNum(campaignNum - 1, false);
} else if (num >= data.size()) {
setCampaignNum(campaignNum + 1, true);
}
}
public UserInterface(Container pane) {
this.pane = pane;
pane.setLayout(new GridBagLayout());
initializeCampaigns();
isTimeGraph = true;
isGregorian = true;
initializeTime();
dataNum = 0;
setCampaignNum(0, true);
ranAlg = false;
}
private void setScrollPane() {
if (campaignDataSP != null) {
pane.remove(campaignDataSP);
}
GridBagConstraints constraints = new GridBagConstraints();
String[] list = new String[data.size()];
int count = 1;
for (DataObj i : data) {
list[count - 1] = ("Set " + count + ": " + i.getName());
count++;
}
JList<String> l = new JList<>(list);
campaignDataSP = new JScrollPane(l);
constraints.fill = GridBagConstraints.BOTH;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.ipady = 0; //make this component tall
constraints.ipadx = 0;
constraints.weightx = 0;
constraints.weighty = 1.0;
constraints.gridx = 0;
constraints.gridy = 3;
constraints.anchor = GridBagConstraints.CENTER;
constraints.insets = new Insets(10, 10, 10, 10);
pane.add(campaignDataSP, constraints);
l.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent listSelectionEvent) {
setDataNum(l.getSelectedIndex());
}
});
}
private void graph() {
if (chartPanel != null) {
pane.remove(chartPanel);
}
GridBagConstraints constraints = new GridBagConstraints();
try {
DataObj obj = data.get(dataNum);
if (isTimeGraph && lowPassFilter) {
// params is already assigned to "cleaned" list of data - don't reassign until the graph is changed
lowPassFilter = false;
} else {
params = GraphParam.getTable(obj);
}
if (isTimeGraph) {
if (isGregorian) {
GraphTimeCurve curve = new GraphTimeCurve("Graph", params);
chartPanel = curve.getPanel();
} else {
GraphLineCurve curve = new GraphLineCurve("Graph", params);
chartPanel = curve.getPanel();
}
} else if (lowPassFilter) {
complexCurve = complexCurve.lowPassFilter("myFixedGraph", 0.002);
chartPanel = complexCurve.getPanel();
params = (ArrayList<GraphParam>) GraphComplexCurve.toParamList(FourierTransform.toTime(complexCurve.getParams()), params);
} else {
List<Double> domain = new ArrayList<>();
for (double x = 0; x < params.size(); x += 1) {
domain.add(x/(params.size() * 30));
}
complexCurve =
new GraphComplexCurve("Graph", domain, FourierTransform.toFrequency(GraphLineCurve.toComplexList(params)));
chartPanel = complexCurve.getPanel();
}
constraints.fill = GridBagConstraints.BOTH;
constraints.ipady = 100;
constraints.weightx = 0.1;
constraints.weighty = 1.0;
constraints.gridwidth = 1;
constraints.gridheight = 3;
constraints.gridx = 1;
constraints.gridy = 1;
constraints.insets = new Insets(10, 0, 10, 10);
pane.add(chartPanel, constraints);
chartPanel.updateUI();
} catch (Exception e) {
e.printStackTrace();
}
}
private void initializeTime() {
if (buttonPanel != null) {
pane.remove(buttonPanel);
}
buttonPanel = new JPanel();
GridBagConstraints constraints = new GridBagConstraints();
JButton button;
constraints.fill = GridBagConstraints.BOTH;
button = new JButton("Run Algorithm");
constraints.weightx = 0;
constraints.ipadx = 40;
constraints.ipady = 15;
constraints.gridx = 1;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.anchor = GridBagConstraints.PAGE_START;
buttonPanel.add(button, constraints);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
run = true;
resultPanel();
}
});
constraints.fill = GridBagConstraints.BOTH;
button = new JButton("To Frequency Domain");
constraints.weightx = 0;
constraints.ipadx = 40;
constraints.ipady = 15;
constraints.gridx = 2;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.anchor = GridBagConstraints.PAGE_START;
buttonPanel.add(button, constraints);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
isTimeGraph = false;
initializeFreq();
graph();
}
});
constraints.fill = GridBagConstraints.BOTH;
button = new JButton("Switch Time Domain");
constraints.weightx = 0;
constraints.ipadx = 40;
constraints.ipady = 15;
constraints.gridx = 3;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.anchor = GridBagConstraints.PAGE_START;
buttonPanel.add(button, constraints);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
isGregorian = !isGregorian;
graph();
}
});
constraints.fill = GridBagConstraints.BOTH;
button = new JButton("Run all processes");
constraints.weightx = 0;
constraints.ipadx = 40;
constraints.ipady = 15;
constraints.gridx = 4;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.anchor = GridBagConstraints.PAGE_START;
buttonPanel.add(button, constraints);
constraints.fill = GridBagConstraints.BOTH;
button = new JButton("<— Previous Graph");
constraints.weightx = 1;
constraints.weighty = 0;
constraints.ipadx = 40;
constraints.ipady = 15;
constraints.gridx = 5;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.anchor = GridBagConstraints.PAGE_START;
buttonPanel.add(button, constraints);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
setDataNum(dataNum - 1);
}
});
constraints.fill = GridBagConstraints.BOTH;
button = new JButton("Next Graph —>");
constraints.weightx = 1;
constraints.weighty = 0;
constraints.ipadx = 50;
constraints.ipady = 15;
constraints.gridx = 6;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.anchor = GridBagConstraints.PAGE_START;
buttonPanel.add(button, constraints);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
setDataNum(dataNum + 1);
}
});
constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 0;
constraints.ipadx = 40;
constraints.ipady = 15;
constraints.gridx = 1;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.anchor = GridBagConstraints.PAGE_START;
pane.add(buttonPanel, constraints);
resultPanel();
}
// for now has saved buttons and constraints of initializeTime in case u fuk up // nvm u didn't fuk up // nvm u did
private void initializeFreq() {
if (buttonPanel != null) {
pane.remove(buttonPanel);
}
buttonPanel = new JPanel();
GridBagConstraints constraints = new GridBagConstraints();
JButton button;
constraints.fill = GridBagConstraints.BOTH;
button = new JButton("To Time Domain");
constraints.weightx = 0;
constraints.ipadx = 40;
constraints.ipady = 15;
constraints.gridx = 1;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.anchor = GridBagConstraints.PAGE_START;
buttonPanel.add(button, constraints);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
isTimeGraph = true;
isGregorian = true;
initializeTime();
graph();
}
});
constraints.fill = GridBagConstraints.BOTH;
button = new JButton("Apply Low-Pass Filter");
constraints.weightx = 0;
constraints.ipadx = 40;
constraints.ipady = 15;
constraints.gridx = 2;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.anchor = GridBagConstraints.PAGE_START;
buttonPanel.add(button, constraints);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
lowPassFilter = true;
graph();
}
});
constraints.fill = GridBagConstraints.BOTH;
button = new JButton("<— Previous Graph");
constraints.weightx = 1;
constraints.weighty = 0;
constraints.ipadx = 40;
constraints.ipady = 15;
constraints.gridx = 3;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.anchor = GridBagConstraints.PAGE_START;
buttonPanel.add(button, constraints);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
setDataNum(dataNum - 1);
}
});
constraints.fill = GridBagConstraints.BOTH;
button = new JButton("Next Graph —>");
constraints.weightx = 1;
constraints.weighty = 0;
constraints.ipadx = 50;
constraints.ipady = 15;
constraints.gridx = 4;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.anchor = GridBagConstraints.PAGE_START;
buttonPanel.add(button, constraints);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
setDataNum(dataNum + 1);
}
});
constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 0;
constraints.ipadx = 40;
constraints.ipady = 15;
constraints.gridx = 1;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.anchor = GridBagConstraints.PAGE_START;
pane.add(buttonPanel, constraints);
}
private double runAlg() {
MLAlgorithm alg = new MLAlgorithm();
return 0;
}
private void resultPanel() {
double prob = 0;
if (!ranAlg) {
if (run) {
prob = runAlg();
run = false;
ranAlg = true;
}
String text = "<html><div style='text-align:center'><br><br>The probability/confidence <br><br>that the graph representing<br><br>the star contains at least<br><br>1 exoplanet = " + ((prob == 0) ? "-" : prob * 100) + "%</div><html>";
// <div style='color:purple'>
if (outputPanel == null) { // assume that if outputPanel is null, so is algPane
outputPanel = new JPanel();
outputPanel.setBackground(Color.WHITE);
outputPanel.setBorder(new SoftBevelBorder(0, Color.LIGHT_GRAY, Color.LIGHT_GRAY));
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
algPane = new JLabel();
algPane.setText(text);
constraints.weightx = 0;
constraints.weighty = 1;
constraints.ipadx = 40;
constraints.ipady = 0;
constraints.gridx = 7;
constraints.gridy = 1;
constraints.gridwidth = 1;
constraints.gridheight = 3;
constraints.anchor = GridBagConstraints.PAGE_START;
constraints.insets = new Insets(20, 0, 10, 10);
outputPanel.add(algPane); //, constraints);
pane.add(outputPanel, constraints);
} else {
algPane.setText(text);
}
ranAlg = true;
}
}
private void namePanel() {
String text = "<html><div style='text-align:center'>Campaign " + campaigns.get(campaignNum).getName().substring(1) + "<br><br>Dataset " + (dataNum + 1) + "</div></html>";
if (contentLabel == null) {
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
contentLabel = new JLabel(text);
contentLabel.setVerticalAlignment(JLabel.TOP);
contentLabel.setHorizontalAlignment(JLabel.CENTER);
constraints.weightx = 0;
constraints.weighty = 0;
constraints.ipadx = 80;
constraints.ipady = 0;
constraints.gridx = 7;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 2;
constraints.anchor = GridBagConstraints.PAGE_START;
constraints.insets = new Insets(10, 0, 10, 0);
pane.add(contentLabel, constraints);
} else {
contentLabel.setText(text);
}
}
public static void runProgram() {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("UIPanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
Container pane = frame.getContentPane();
pane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
UserInterface myUI = new UserInterface(pane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
runProgram();
}
private void initializeCampaigns() {
// hard-code each link to campaigns
campaigns.add(new DataObj("c0", "https://www.cfa.harvard.edu/~avanderb/C0asciiwget.sh"));
campaigns.add(new DataObj("c1", "https://www.cfa.harvard.edu/~avanderb/C1asciiwget.sh"));
campaigns.add(new DataObj("c2", "https://www.cfa.harvard.edu/~avanderb/C2asciiwget.sh"));
campaigns.add(new DataObj("c3 - test data", "https://www.cfa.harvard.edu/~avanderb/C3asciiwget.sh"));
campaigns.add(new DataObj("c4", "https://www.cfa.harvard.edu/~avanderb/C4asciiwget.sh"));
campaigns.add(new DataObj("c5", "https://www.cfa.harvard.edu/~avanderb/C5asciiwget.sh"));
campaigns.add(new DataObj("c6", "https://www.cfa.harvard.edu/~avanderb/C6asciiwget.sh"));
campaigns.add(new DataObj("c7", "https://www.cfa.harvard.edu/~avanderb/C7asciiwget.sh"));
campaigns.add(new DataObj("c8", "https://www.cfa.harvard.edu/~avanderb/C8asciiwget.sh"));
campaigns.add(new DataObj("c9a", "https://www.cfa.harvard.edu/~avanderb/C91asciiwget.sh"));
campaigns.add(new DataObj("c9b", "https://www.cfa.harvard.edu/~avanderb/C92asciiwget.sh"));
campaigns.add(new DataObj("c10", "https://www.cfa.harvard.edu/~avanderb/C10asciiwget.sh"));
campaigns.add(new DataObj("c11a", "https://www.cfa.harvard.edu/~avanderb/C111asciiwget.sh"));
campaigns.add(new DataObj("c11b", "https://www.cfa.harvard.edu/~avanderb/C112asciiwget.sh"));
campaigns.add(new DataObj("c12", "https://www.cfa.harvard.edu/~avanderb/C12asciiwget.sh"));
campaigns.add(new DataObj("c13", "https://www.cfa.harvard.edu/~avanderb/C13asciiwget.sh"));
campaigns.add(new DataObj("c14", "https://www.cfa.harvard.edu/~avanderb/C14asciiwget.sh"));
campaigns.add(new DataObj("c15", "https://www.cfa.harvard.edu/~avanderb/C15asciiwget.sh"));
campaigns.add(new DataObj("c16", "https://www.cfa.harvard.edu/~avanderb/C16asciiwget.sh"));
campaigns.add(new DataObj("c17", "https://www.cfa.harvard.edu/~avanderb/C17asciiwget.sh"));
campaigns.add(new DataObj("c18", "https://www.cfa.harvard.edu/~avanderb/C18asciiwget.sh"));
campaigns.add(new DataObj("c19", "https://www.cfa.harvard.edu/~avanderb/C19asciiwget.sh"));
// add text block about the campaign scroll list
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.HORIZONTAL;
JLabel label = new JLabel("Campaigns");
constraints.weightx = 0;
constraints.weighty = 0;
constraints.ipadx = 80;
constraints.ipady = 20;
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.anchor = GridBagConstraints.PAGE_START;
constraints.insets.left = 10;
constraints.insets.top = 10;
pane.add(label, constraints);
// add scroll pane with the data from campaigns
String[] list = new String[campaigns.size()];
int count = 0;
for (DataObj i : campaigns) {
list[count] = (i.getName());
count++;
}
JList<String> l = new JList<>(list);
JScrollPane sp = new JScrollPane(l);
sp.setMinimumSize(new Dimension(100, 200));
//sp.add(l, GridBagConstraints.NORTH, 0);
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.ipady = 0; //make this component tall
constraints.ipadx = 0;
constraints.weightx = 0;
constraints.weighty = 0;
constraints.gridx = 0;
constraints.gridy = 1;
constraints.anchor = GridBagConstraints.PAGE_START;
constraints.insets = new Insets(10, 10, 10, 10);
pane.add(sp, constraints);
l.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent listSelectionEvent) {
setCampaignNum(l.getSelectedIndex(), true);
}
});
// add text block about datasets within the selected campaign
constraints.fill = GridBagConstraints.HORIZONTAL;
label = new JLabel("Datasets in Campaign");
constraints.weightx = 0;
constraints.weighty = 0;
constraints.ipadx = 80;
constraints.ipady = 0;
constraints.gridx = 0;
constraints.gridy = 2;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.anchor = GridBagConstraints.PAGE_START;
constraints.insets.left = 10;
constraints.insets.top = 10;
pane.add(label, constraints);
}
}