-
Notifications
You must be signed in to change notification settings - Fork 1
/
DimensionsInputDialog.java
43 lines (39 loc) · 1.94 KB
/
DimensionsInputDialog.java
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
import javax.swing.JOptionPane;
import java.awt.Component;
import java.util.Arrays;
public class DimensionsInputDialog
{
static int[] showDialog(Component parent){
return showDialog(parent, "Please imput dimensions"+UsefulStrings.getLineSeparator()+"Input should be of the form # of cells in the X coordinate,# of cells in the Y coordinate.", "30,30");
}
static int[] showDialogWithMessage(Component parent, String message){
return showDialog(parent, message, "30,30");
}
static int[] showDialog(Component parent, String defaultValue){
return showDialogWithDefault(parent, defaultValue);
}
static int[] showDialogWithDefault(Component parent, String defaultValue){
return showDialog(parent, "Please imput dimensions"+UsefulStrings.getLineSeparator()+"Input should be of the form # of cells in the X coordinate,# of cells in the Y coordinate.", defaultValue);
}
static int[] showDialog(Component parent, String message, String defaultValue){
String d = JOptionPane.showInputDialog(message, defaultValue);
if(d==null) return null;
String[] dimensions = d.split(",");
int[] rVal = null;
try{
rVal = new int[]{new Integer(dimensions[0]),new Integer(dimensions[1])};
}catch(NumberFormatException nfe){
System.err.println(Arrays.toString(dimensions)+" is not a valid input");
rVal = DimensionsInputDialog.showDialog(parent, message, defaultValue);
}
if(rVal.length!=2){
System.err.println(Arrays.toString(dimensions)+" is not the right length");
rVal = DimensionsInputDialog.showDialog(parent, message, defaultValue);
}
if(!(rVal[0]>0&&rVal[1]>0)){
System.err.println(Arrays.toString(dimensions)+" is out of bounds (too small)");
rVal = DimensionsInputDialog.showDialog(parent, message, defaultValue);
}
return rVal;
}
}