-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileMenuHandler.java
More file actions
85 lines (80 loc) · 2.94 KB
/
FileMenuHandler.java
File metadata and controls
85 lines (80 loc) · 2.94 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class FileMenuHandler implements ActionListener {
JFrame jframe;
int a = 0;
public FileMenuHandler (JFrame jf) {
jframe = jf;
}
public void actionPerformed(ActionEvent event) {
String menuName;
menuName = event.getActionCommand();
if (menuName.equals("Open"))
openFile();
else if (menuName.equals("Quit"))
System.exit(0);
else if(menuName.equals("Number to Letter"))
convertNum();
else if(menuName.equals("Letter to Number"))
convertLet();
} //actionPerformed
private void openFile( ) {
JFileChooser chooser;
int status;
chooser = new JFileChooser( );
status = chooser.showOpenDialog(null);
if (status == JFileChooser.APPROVE_OPTION)
readSource(chooser.getSelectedFile());
else
JOptionPane.showMessageDialog(null, "Open File dialog canceled");
} //openFile
private void readSource(File chosenFile) {
String chosenFileName = chosenFile.getName();
TextFileInput inFile = new TextFileInput(chosenFileName);
String ssn;
int subscript = 0;
Container myContentPane = jframe.getContentPane();
TextArea myTextArea = new TextArea();
TextArea mySubscripts = new TextArea();
myContentPane.add(myTextArea, BorderLayout.EAST);
myContentPane.add(mySubscripts, BorderLayout.WEST);
ssn = inFile.readLine();
while (ssn != null) {
mySubscripts.append(Integer.toString(subscript++)+"\n");
myTextArea.append(ssn+"\n");
ssn = inFile.readLine();
} //while
jframe.setVisible(true);
}
private void convertNum()
{
String grade = JOptionPane.showInputDialog(null, "Enter a grade number");
Grade gradeInput;
try{
gradeInput = new Grade(Integer.parseInt(grade));
JOptionPane.showMessageDialog(null, "Your grade for this class is a " + gradeInput);
}
catch(NumberFormatException e){
JOptionPane.showMessageDialog(null, "Please enter a valid number");
}
catch(IllegalGradeException f)
{
JOptionPane.showMessageDialog(null, "Please enter a non-negative number");
}
}
private void convertLet()
{
String grade = JOptionPane.showInputDialog(null, "Enter a letter grade");
Grade gradeInput;
try{
if(!grade.matches("A[+]?||A[-]?||B[+]?||B[-]?||C[+]?||C[-]?||D[+]?||F")) throw new IllegalGradeException("illegal letter grade");
gradeInput = new Grade(grade);
JOptionPane.showMessageDialog(null, "Your grade for this class is at least a " + gradeInput.getNum());
}
catch(IllegalGradeException e){
JOptionPane.showMessageDialog(null,"Please enter a valid letter grade");
}
}
}