-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwitchesGUI.java
More file actions
220 lines (197 loc) · 8.24 KB
/
SwitchesGUI.java
File metadata and controls
220 lines (197 loc) · 8.24 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
/**
* SwitchesGUI.java
* A program to filter a list of keyboard switches from a properly-formatted .csv file. Now with GUI!
*
* @author Jordan Rudman
* @version 1.0
*/
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.util.List;
public class SwitchesGUI extends JFrame {
// GUI components
private JTextField actuForceMinField = new JTextField(5);
private JTextField actuForceMaxField = new JTextField(5);
private JTextField preTravelMinField = new JTextField(5);
private JTextField preTravelMaxField = new JTextField(5);
private JTextField totalTravelMinField = new JTextField(5);
private JTextField totalTravelMaxField = new JTextField(5);
private JComboBox<String> feelBox = new JComboBox<>(new String[]{"Any", "tactile", "clicky", "linear", "silent tactile", "silent linear"});
private JComboBox<String> mountBox = new JComboBox<>(new String[]{"Any", "plate", "PCB"});
private JTextArea outputArea = new JTextArea(15, 40);
// Filters (stored from fields)
private static String actuForceMin;
private static String actuForceMax;
private static String preTravelMin;
private static String preTravelMax;
private static String totalTravelMin;
private static String totalTravelMax;
private static String feel;
private static String mount;
public SwitchesGUI() {
setTitle("Keyboard Switch Filter");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// Input fields
JPanel inputPanel = new JPanel(new GridLayout(0, 2, 5, 5));
inputPanel.add(new JLabel("Actuation Force Min (g):"));
inputPanel.add(actuForceMinField);
inputPanel.add(new JLabel("Actuation Force Max (g):"));
inputPanel.add(actuForceMaxField);
inputPanel.add(new JLabel("Pre-Travel Min (mm):"));
inputPanel.add(preTravelMinField);
inputPanel.add(new JLabel("Pre-Travel Max (mm):"));
inputPanel.add(preTravelMaxField);
inputPanel.add(new JLabel("Total Travel Min (mm):"));
inputPanel.add(totalTravelMinField);
inputPanel.add(new JLabel("Total Travel Max (mm):"));
inputPanel.add(totalTravelMaxField);
inputPanel.add(new JLabel("Feel:"));
inputPanel.add(feelBox);
inputPanel.add(new JLabel("Mount:"));
inputPanel.add(mountBox);
JButton filterButton = new JButton("Filter Switches");
filterButton.addActionListener(e -> filterSwitches());
outputArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(outputArea);
add(inputPanel, BorderLayout.NORTH);
add(filterButton, BorderLayout.CENTER);
add(scrollPane, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
}
/**
* Pulls info from .csv and user input
*/
private void filterSwitches() {
try {
// Grab values from fields
actuForceMin = actuForceMinField.getText().isEmpty() ? "0" : actuForceMinField.getText();
actuForceMax = actuForceMaxField.getText().isEmpty() ? "999" : actuForceMaxField.getText();
preTravelMin = preTravelMinField.getText().isEmpty() ? "0" : preTravelMinField.getText();
preTravelMax = preTravelMaxField.getText().isEmpty() ? "999" : preTravelMaxField.getText();
totalTravelMin = totalTravelMinField.getText().isEmpty() ? "0" : totalTravelMinField.getText();
totalTravelMax = totalTravelMaxField.getText().isEmpty() ? "999" : totalTravelMaxField.getText();
feel = feelBox.getSelectedItem().toString();
if(feel.equals("Any")) {
feel = "none";
}
mount = mountBox.getSelectedItem().toString();
if(mount.equals("Any")) {
mount = "none";
}
List<String> list = new ArrayList<>();
InputStream input = SwitchesGUI.class.getResourceAsStream("/switchesformatted.csv");
if (input == null) {
throw new FileNotFoundException("switchesformatted.csv not found in JAR!");
}
Scanner sc = new Scanner(input);
sc.nextLine(); // skip header
while(sc.hasNextLine()) {
String[] arr = sc.nextLine().split(",");
boolean feelCheck = feelCheck(arr[1]);
boolean forceCheck = actuationCheck(arr[2]);
boolean preTCheck = preTCheck(arr[3]);
boolean totalTCheck = totalTCheck(arr[4]);
boolean mountCheck = mountCheck(arr[5]);
if (forceCheck && preTCheck && mountCheck && feelCheck && totalTCheck) {
list.add(arr[0]);
}
}
sc.close();
Collections.sort(list);
outputArea.setText(String.join("\n", list) + "\n\nSwitch count: " + list.size());
} catch(Exception ex) {
outputArea.setText("Error: " + ex.getMessage());
}
}
/**
* Filters switches by feel
* @param String The feel type to check
* @return True if feel type matches, false otherwise
*/
public static boolean feelCheck(String str) {
if(feel.equalsIgnoreCase("none")) {
return true;
}
if(str.equalsIgnoreCase(feel))
{
return true;
}
else if(str.contains("/")) {
for(String aFeel : str.split("/")) {
if(aFeel.equalsIgnoreCase(feel)) return true;
}
}
return false;
}
/**
* Filters switches by actuation force
* @param String The actuation force to check
* @return True if actuation force is within range, false otherwise
*/
public static boolean actuationCheck(String str) {
String force = str.replaceAll("g", "");
if(force.contains("/")) {
for(String aForce : force.split("/")) {
if(Double.parseDouble(aForce) >= Double.parseDouble(actuForceMin)
&& Double.parseDouble(aForce) <= Double.parseDouble(actuForceMax)) {
return true;
}
}
} else if(Double.parseDouble(force) >= Double.parseDouble(actuForceMin)
&& Double.parseDouble(force) <= Double.parseDouble(actuForceMax)) {
return true;
}
return false;
}
/**
* Filters switches by pre-travel distance
* @param String The pre-travel distance to check
* @return True if pre-travel distance is within range, false otherwise
*/
public static boolean preTCheck(String str) {
String val = str.replaceAll("mm", "");
return Double.parseDouble(val) >= Double.parseDouble(preTravelMin)
&& Double.parseDouble(val) <= Double.parseDouble(preTravelMax);
}
/**
* Filters switches by total travel distance before bottoming out
* @param String The total travel distance to check
* @return True if total travel distance is within range, false otherwise
*/
public static boolean totalTCheck(String str) {
String val = str.replaceAll("mm", "");
return Double.parseDouble(val) >= Double.parseDouble(totalTravelMin)
&& Double.parseDouble(val) <= Double.parseDouble(totalTravelMax);
}
/**
* Filters switches by mount type
* @param String The mount type to check
* @return True if mount type matches, false otherwise
*/
public static boolean mountCheck(String str) {
if (mount.equalsIgnoreCase("none")) {
return true;
}
if (str.equalsIgnoreCase(mount)) {
return true;
}
if (str.contains("/")) {
for (String aType : str.split("/")) {
if (aType.equalsIgnoreCase(mount)){
return true;
}
}
}
return false;
}
/**
* Main method, runs the program.
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new SwitchesGUI().setVisible(true));
}
}