-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddPartyView.java
237 lines (208 loc) · 6.81 KB
/
AddPartyView.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/* AddPartyView.java
*
* Version:
* $Id$
*
* Revisions:
* $Log: AddPartyView.java,v $
* Revision 1.7 2003/02/20 02:05:53 ???
* Fixed addPatron so that duplicates won't be created.
*
* Revision 1.6 2003/02/09 20:52:46 ???
* Added comments.
*
* Revision 1.5 2003/02/02 17:42:09 ???
* Made updates to migrate to observer model.
*
* Revision 1.4 2003/02/02 16:29:52 ???
* Added ControlDeskEvent and ControlDeskObserver. Updated Queue to allow access to Vector so that contents could be viewed without destroying. Implemented observer model for most of ControlDesk.
*
*
*/
/**
* Class for GUI components need to add a party
*
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.util.*;
/**
* Constructor for GUI used to Add Parties to the waiting party queue.
*
*/
public class AddPartyView {
private int maxSize;
private ButtonCommand command;
private JFrame win;
private JButton addPatron, newPatron, remPatron, finished;
private JList<String> partyList, allBowlers;
private Vector<String> party, bowlerdb;
private ControlDeskView controlDesk;
private String selectedNick, selectedMember;
public AddPartyView(ControlDeskView controlDesk, int max) {
this.controlDesk = controlDesk;
maxSize = max;
win = new JFrame("Add Party");
win.getContentPane().setLayout(new BorderLayout());
((JPanel) win.getContentPane()).setOpaque(false);
JPanel colPanel = new JPanel();
colPanel.setLayout(new GridLayout(1, 3));
// Party Panel
JPanel partyPanel = new JPanel();
partyPanel.setLayout(new FlowLayout());
partyPanel.setBorder(new TitledBorder("Your Party"));
party = new Vector<String>();
Vector<String> empty = new Vector<String>();
empty.add("(Empty)");
AddPartyViewClickEvent listener = new AddPartyViewClickEvent();
partyList = new JList<>(empty);
partyList.setFixedCellWidth(120);
partyList.setVisibleRowCount(5);
partyList.addListSelectionListener(listener);
JScrollPane partyPane = new JScrollPane(partyList);
// partyPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
partyPanel.add(partyPane);
// Bowler Database
JPanel bowlerPanel = new JPanel();
bowlerPanel.setLayout(new FlowLayout());
bowlerPanel.setBorder(new TitledBorder("Bowler Database"));
try {
bowlerdb = new Vector<String>(BowlerFile.getInstance().getBowlers());
} catch (Exception e) {
System.err.println("File Error");
bowlerdb = new Vector<String>();
}
allBowlers = new JList<>(bowlerdb);
allBowlers.setVisibleRowCount(8);
allBowlers.setFixedCellWidth(120);
JScrollPane bowlerPane = new JScrollPane(allBowlers);
bowlerPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
allBowlers.addListSelectionListener(listener);
bowlerPanel.add(bowlerPane);
// Button Panel
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 1));
addPatron = createButton("Add to Party", new JPanel(), listener);
remPatron = createButton("Remove Member", new JPanel(), listener);
newPatron = createButton("New Patron", new JPanel(), listener);
finished = createButton("Finished", new JPanel(), listener);
buttonPanel.add((JPanel) addPatron.getParent());
buttonPanel.add((JPanel) remPatron.getParent());
buttonPanel.add((JPanel) newPatron.getParent());
buttonPanel.add((JPanel) finished.getParent());
// Clean up main panel
colPanel.add(partyPanel);
colPanel.add(bowlerPanel);
colPanel.add(buttonPanel);
win.getContentPane().add("Center", colPanel);
win.pack();
// Center Window on Screen
Dimension screenSize = (Toolkit.getDefaultToolkit()).getScreenSize();
win.setLocation(
((screenSize.width) / 2) - ((win.getSize().width) / 2),
((screenSize.height) / 2) - ((win.getSize().height) / 2));
win.setVisible(true);
}
private JButton createButton(String buttonText, JPanel panel, AddPartyViewClickEvent listener) { // 버튼 객체 생성
JButton button = new JButton(buttonText);
button.addActionListener(listener);
panel.setLayout(new FlowLayout());
panel.add(button);
return button;
}
public void setCommand(ButtonCommand command) {
this.command = command;
}
public void buttonPressed() {
command.execute();
}
public String getSelectedNick() {
return selectedNick;
}
public String getSelectedMember() {
return selectedMember;
}
public int getMaxSize() {
return maxSize;
}
public JList<String> getPartyList() {
return partyList;
}
public JFrame getWindow() {
return win;
}
public ControlDeskView getControlDesk() {
return controlDesk;
}
/**
* Accessor for Party
*/
public Vector<String> getParty() {
return party;
}
public class AddPartyViewClickEvent implements ActionListener, ListSelectionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(addPatron)) {
setCommand(new AddPatronCommand(AddPartyView.this));
}
if (e.getSource().equals(remPatron)) {
setCommand(new RemovePatronCommand(AddPartyView.this));
}
if (e.getSource().equals(newPatron)) {
setCommand(new NewPatronCommand(AddPartyView.this));
}
if (e.getSource().equals(finished)) {
setCommand(new FinishedAddPartyCommand(AddPartyView.this));
}
buttonPressed();
}
/**
* Handler for List actions
* @param e the ListActionEvent that triggered the handler
*/
public void valueChanged(ListSelectionEvent e) {
if (e.getSource().equals(allBowlers)) {
if (e.getSource() instanceof JList) {
JList<?> selectedNicks = (JList<?>) e.getSource();
selectedNick = (String) selectedNicks.getSelectedValue();
}
}
if (e.getSource().equals(partyList)) {
if (e.getSource() instanceof JList) {
JList<?> source = (JList<?>) e.getSource();
selectedMember = (String) source.getSelectedValue();
}
}
}
/**
* Called by NewPatronView to notify AddPartyView to update
*
* @param newPatron the NewPatronView that called this method
*/
public void updateNewPatron(NewPatronView newPatron) {
try {
Bowler checkBowler = BowlerFile.getInstance().getBowlerInfo( newPatron.getNick() );
if ( checkBowler == null ) {
BowlerFile.getInstance().putBowlerInfo(
newPatron.getNick(),
newPatron.getFull(),
newPatron.getEmail());
bowlerdb = new Vector<String>(BowlerFile.getInstance().getBowlers());
allBowlers.setListData(bowlerdb);
party.add(newPatron.getNick());
partyList.setListData(party);
} else {
String errMsg = "A Bowler with that name already exists.";
System.err.println(errMsg);
JOptionPane.showMessageDialog(win, errMsg, "Error", JOptionPane.ERROR_MESSAGE);
}
} catch (Exception e2) {
System.err.println("File I/O Error");
}
}
}
}