Skip to content

Commit f96b45f

Browse files
committed
Change options for algorithm-selection to enum
1 parent 5392268 commit f96b45f

File tree

1 file changed

+48
-24
lines changed

1 file changed

+48
-24
lines changed

src/main/java/de/moritzf/sorting/gui/windows/AlgorithmSelection.java

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import javax.swing.JLabel;
1616
import javax.swing.JOptionPane;
1717
import javax.swing.JPanel;
18-
1918
import de.moritzf.sorting.logic.sorting.*;
2019
import org.jdesktop.swingx.JXTextField;
2120

@@ -67,16 +66,11 @@ public class AlgorithmSelection extends JFrame implements ActionListener {
6766
*/
6867
JXTextField arrayInputField = new JXTextField();
6968

70-
/**
71-
* The algorithmOptions. Defines which algorithms should be shown
72-
*/
73-
String[] algorithmOptions = {"Bubblesort", "Selectionsort", "Radixsort", "Quicksort",
74-
"Heapsort (Minheap)", "Heapsort (Maxheap)", "Insertionsort", "Shellsort ( steplengths=[... 8,4,2,1] )"};
7569

7670
/**
7771
* The algorithmSelectionBox. Dropdown for user selection of the algorithm
7872
*/
79-
JComboBox<String> algorithmSelectionBox = new JComboBox<String>(algorithmOptions);
73+
JComboBox<SelectionItem> algorithmSelectionBox = new JComboBox<SelectionItem>(SelectionItem.values());
8074

8175
/**
8276
* The randomArrayGenerationButton. Starts generation of a random array as input for the sorting algorithm.
@@ -123,23 +117,25 @@ public AlgorithmSelection() {
123117
this.randomArrayGenerationButton.addActionListener(this);
124118
this.aboutButton.addActionListener(this);
125119

120+
this.algorithmSelectionBox.addActionListener(this);
121+
126122
arrayInputField.addKeyListener(new InputListener());
127123

128124

129-
/*
125+
/*
130126
* create the panel where the algorithm selection and the startbutton
131-
* gets displayed
132-
*/
127+
* gets displayed
128+
*/
133129
JPanel buttonPanel = new JPanel();
134130
buttonPanel.setLayout(new BorderLayout());
135131
buttonPanel.add(algorithmSelectionBox, BorderLayout.CENTER);
136132
buttonPanel.add(startbtn, BorderLayout.EAST);
137133
mainelements.add(buttonPanel, BorderLayout.SOUTH);
138134

139-
/*
135+
/*
140136
* create instructions for the user input and the button for generating
141-
* a random input string
142-
*/
137+
* a random input string
138+
*/
143139
JPanel northpanel = new JPanel(new BorderLayout());
144140
northpanel.add(randomArrayGenerationButton, BorderLayout.EAST);
145141
northpanel.add(new JLabel("Enter the number sequence that you want to sort:"), BorderLayout.WEST);
@@ -213,6 +209,8 @@ public void actionPerformed(ActionEvent e) {
213209
new RandomArrayGeneratorWindow(this);
214210
} else if (e.getSource().equals(aboutButton)) {
215211
new AboutWindow(this);
212+
} else if (e.getSource().equals(algorithmSelectionBox)) {
213+
216214
}
217215

218216
}
@@ -237,27 +235,27 @@ private void handleStart() {
237235
JOptionPane.showMessageDialog(this,
238236
"<html>The array used as input contains more than" + maxCount
239237
+ " elements. <br> <br> Because every step has to be displayed and the number of "
240-
+ " steps incrases with the input, <br> sind "
238+
+ " steps increases with the input, <br> sind "
241239
+ " only arrays with a length as high as 100 are allowed.<br> <br>"
242240
+ "But seriously: you did not really want to look at that protocol in detail and"
243241
+ "<br>just tried to cause a software crash ;)</html>",
244242
"Error: Are you serious?", JOptionPane.INFORMATION_MESSAGE);
245243
} else {
246-
if (this.algorithmSelectionBox.getSelectedItem().equals("Bubblesort")) {
244+
if (this.algorithmSelectionBox.getSelectedItem().equals(SelectionItem.BUBBLE_SORT)) {
247245
new SortingWindow(this, new BubbleSort(input));
248-
} else if (this.algorithmSelectionBox.getSelectedItem().equals("Heapsort (Maxheap)")) {
246+
} else if (this.algorithmSelectionBox.getSelectedItem().equals(SelectionItem.HEAP_SORT_MAX)) {
249247
new HeapWindow(this, new HeapSort(input, false));
250-
} else if (this.algorithmSelectionBox.getSelectedItem().equals("Heapsort (Minheap)")) {
248+
} else if (this.algorithmSelectionBox.getSelectedItem().equals(SelectionItem.HEAP_SORT_MIN)) {
251249
new HeapWindow(this, new HeapSort(input, true));
252-
} else if (this.algorithmSelectionBox.getSelectedItem().equals("Quicksort")) {
250+
} else if (this.algorithmSelectionBox.getSelectedItem().equals(SelectionItem.QUICK_SORT)) {
253251
new SortingWindow(this, new QuickSort(input));
254-
} else if (this.algorithmSelectionBox.getSelectedItem().equals("Radixsort")) {
252+
} else if (this.algorithmSelectionBox.getSelectedItem().equals(SelectionItem.RADIX_SORT)) {
255253
new SortingWindow(this, new RadixSort(input));
256-
} else if (this.algorithmSelectionBox.getSelectedItem().equals("Selectionsort")) {
254+
} else if (this.algorithmSelectionBox.getSelectedItem().equals(SelectionItem.SELECTION_SORT)) {
257255
new SortingWindow(this, new SelectionSort(input));
258-
} else if (this.algorithmSelectionBox.getSelectedItem().equals("Insertionsort")) {
256+
} else if (this.algorithmSelectionBox.getSelectedItem().equals(SelectionItem.INSERTION_SORT)) {
259257
new SortingWindow(this, new InsertionSort(input));
260-
} else if (this.algorithmSelectionBox.getSelectedItem().equals("Shellsort ( steplengths=[... 8,4,2,1] )")) {
258+
} else if (this.algorithmSelectionBox.getSelectedItem().equals(SelectionItem.SHELL_SORT_2N)) {
261259
new SortingWindow(this, new ShellSort(input));
262260
} else {
263261
JOptionPane.showMessageDialog(this,
@@ -268,6 +266,7 @@ private void handleStart() {
268266
}
269267
}
270268

269+
271270
/**
272271
* The class for receiving input events in this kind of window
273272
* (AlgorithmSelection). When an input event occurs, the appropriate method
@@ -290,8 +289,8 @@ public void keyTyped(KeyEvent e) {
290289
if (c == KeyEvent.VK_COMMA) {
291290
/*
292291
* when the last input was a comma, we do not need another
293-
* one... only one comma is used to seperate numbers after all.
294-
*/
292+
* one... only one comma is used to seperate numbers after all.
293+
*/
295294
if (arrayInputField.getText().isEmpty() || arrayInputField.getText().endsWith(",")) {
296295
e.consume();
297296
getToolkit().beep();
@@ -322,4 +321,29 @@ public void keyReleased(KeyEvent e) {
322321

323322
}
324323

324+
private enum SelectionItem {
325+
BUBBLE_SORT("Bubblesort"),
326+
SELECTION_SORT("Selectionsort"),
327+
RADIX_SORT("Radixsort"),
328+
QUICK_SORT("Quicksort"),
329+
HEAP_SORT_MIN("Heapsort (Minheap)"),
330+
HEAP_SORT_MAX("Heapsort (Maxheap)"),
331+
INSERTION_SORT("Insertionsort"),
332+
SHELL_SORT_2N("Shellsort ( steplengths=[... 8,4,2,1] )"),
333+
SHELL_SORT_CUSTOM("Shellsort (custom steplengths)");
334+
335+
private final String selectionValue;
336+
337+
SelectionItem(String selectionValue) {
338+
this.selectionValue = selectionValue;
339+
}
340+
341+
public String toString() {
342+
return this.selectionValue;
343+
}
344+
345+
346+
}
347+
348+
325349
}

0 commit comments

Comments
 (0)