-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModifyCategoryScreen.java
205 lines (184 loc) · 5.86 KB
/
ModifyCategoryScreen.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
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
package ws.thurn.dossier;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
/*
* .
*/
/**
* The ModifyCategoryScreen class implements the GUI for saving modifications to
* a category.
*
* @author Derek Thurn
*/
public class ModifyCategoryScreen implements ActionListener
{
Klass theClass;
// The class which the category is in.
Category theAssignment;
// The category which is being modified.
String originalName;
// The original name of the category.
JFrame aFrame;
private JLabel nameL;
private JTextField nameF;
private JButton cancel;
private JTextField weightF;
private JButton save;
private JLabel weightL;
private JPanel buttonPanel;
private JPanel weightPanel;
private JPanel namePanel;
/**
* Creates a new screen for modifying categories
*
* @param k the class which is under modification
* @param a the category which is being changed.
*/
public ModifyCategoryScreen( Klass k, Category a )
{
theClass = k;
theAssignment = a;
originalName = theAssignment.getName();
displayScreen();
}
/**
* Displays the GUI for the screen
*
* @pre the correct objects have been declared
* @post the GUI is displayed.
*/
public void displayScreen()
{
aFrame = new JFrame();
aFrame.setTitle( "Modify Category" );
aFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
aFrame.setSize( 400, 400 );
GradeBook.center( aFrame );
BoxLayout aFrameLayout = new BoxLayout( aFrame.getContentPane(),
javax.swing.BoxLayout.Y_AXIS );
aFrame.getContentPane().setLayout( aFrameLayout );
aFrame.setVisible( true );
{
namePanel = new JPanel();
aFrame.getContentPane().add( namePanel );
{
nameL = new JLabel();
namePanel.add( nameL );
nameL.setText( "Category Name:" );
}
{
nameF = new JTextField();
namePanel.add( nameF );
nameF.setPreferredSize( new java.awt.Dimension( 180, 20 ) );
}
}
{
weightPanel = new JPanel();
aFrame.getContentPane().add( weightPanel );
{
weightL = new JLabel();
weightPanel.add( weightL );
weightL.setText( "Weight:" );
}
{
weightF = new JTextField();
weightPanel.add( weightF );
weightF.setPreferredSize( new java.awt.Dimension( 50, 20 ) );
}
}
{
buttonPanel = new JPanel();
aFrame.getContentPane().add( buttonPanel );
{
save = new JButton();
buttonPanel.add( save );
save.setText( "Save Changes" );
save.addActionListener( this );
}
{
cancel = new JButton();
buttonPanel.add( cancel );
cancel.setText( "Cancel" );
cancel.addActionListener( this );
}
}
nameF.setText( theAssignment.getName() );
weightF.setText( "" + theAssignment.getWeight() );
}
/**
* Listens for button presses
*
* @pre listeners have been added
* @post the correct action is taken after an event.
*/
public void actionPerformed( ActionEvent evt )
{
if( evt.getSource().equals( cancel ) )
{
aFrame.dispose();
new AssignmentsScreen();
return;
}
if( evt.getSource().equals( save ) )
{
if( nameF.getText().equals( "" ) )
{
GradeBook.error( "You must specify a name for the category" );
return;
}
int w;
if( weightF.getText().equals( "" ) )
{
w = 1;
}
else
{
try
{
w = Integer.parseInt( weightF.getText() );
}
catch( Exception e )
{
GradeBook
.error( "You must specify an integer value for the weight." );
return;
}
if( w < 1 )
{
GradeBook
.error( "The weight value must be greater than one" );
return;
}
}
Category temp = new Category( nameF.getText(), w );
int q = -1;
if( theClass.isDuplicateCategory( temp )
&& !( nameF.getText().equals( originalName ) ) )
{
q = JOptionPane.showConfirmDialog( null,
"Do you want to overwrite the category "
+ nameF.getText() + "?", "Confirm",
JOptionPane.YES_NO_OPTION );
if( q == 0 )
{
theClass.deleteCategory( nameF.getText() );
}
if( q == 1 )
{
return;
}
}
theAssignment.setName( nameF.getText() );
theAssignment.setWeight( Integer.parseInt( weightF.getText() ) );
aFrame.dispose();
new AssignmentsScreen();
}
}
}