-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBidsGUI.java
More file actions
332 lines (295 loc) · 16.6 KB
/
Copy pathBidsGUI.java
File metadata and controls
332 lines (295 loc) · 16.6 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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import java.awt.event.*;
import java.rmi.RemoteException;
import java.security.spec.InvalidKeySpecException;
import java.util.HashMap;
public class BidsGUI extends GUIPage implements ActionListener {
private String title = "Bids";
private AuctionClient client;
// Page Components:
private JPanel detailsPanel; // Listing details panel components
private JLabel nameTag;
private JLabel name;
private JLabel currentBidTag;
private JLabel currentBid;
private JLabel conditionTag;
private JLabel myBidTag;
private JLabel myBid;
private JLabel condition;
private JLabel descriptionTag;
private JLabel description;
private JLabel statusTag;
private JLabel status;
private JPanel allResultsPanel; // Results panel components
private JButton refreshButton;
private JTable listingsTable;
private JScrollPane tableScrollPane;
private JLabel selectedName;
private JLabel selectedPrice;
private JButton bidButton;
private JTextField bidInput;
// Auction Data
private HashMap<Integer, AuctionItem> items;
private AuctionItem selectedItem;
public BidsGUI(AuctionClient c) {
client = c;
buildPage();
}
public void buildPage() {
allResultsPanel = new JPanel();
listingsTable = new JTable();
refreshButton = new JButton("Refresh");
tableScrollPane = new JScrollPane();
selectedName = new JLabel("");
selectedPrice = new JLabel("");
bidButton = new JButton("Place Bid");
bidInput = new JTextField("");
detailsPanel = new JPanel();
nameTag = new JLabel("Name");
name = new JLabel("");
currentBidTag = new JLabel("Starting Price");
currentBid = new JLabel("");
myBidTag = new JLabel("Reserve Price");
myBid = new JLabel("");
conditionTag = new JLabel("Condition");
condition = new JLabel("");
descriptionTag = new JLabel("Description");
description = new JLabel("");
statusTag = new JLabel("Status");
status = new JLabel("");
bidButton.setEnabled(false);
refreshButton.addActionListener(this);
bidButton.addActionListener(this);
bidInput.getDocument().addDocumentListener(new DocumentListener(){
public void removeUpdate(DocumentEvent e) {
if (bidInput.getText().equals("") || selectedItem == null)
bidButton.setEnabled(false);
}
public void insertUpdate(DocumentEvent e) {
if (!bidInput.getText().equals("") && selectedItem != null)
bidButton.setEnabled(true);
}
public void changedUpdate(DocumentEvent e) {
}
});
// Specifies Add Listing Box Layout
detailsPanel.setBorder(BorderFactory.createTitledBorder("Add Item"));
GroupLayout detailsPanelLayout = new GroupLayout(detailsPanel);
detailsPanel.setLayout(detailsPanelLayout);
detailsPanelLayout.setHorizontalGroup(
detailsPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(GroupLayout.Alignment.TRAILING, detailsPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(detailsPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(detailsPanelLayout.createSequentialGroup()
.addGroup(detailsPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(descriptionTag, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE)
.addGroup(detailsPanelLayout.createSequentialGroup()
.addComponent(myBidTag, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(myBid, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE))
.addGroup(detailsPanelLayout.createSequentialGroup()
.addComponent(currentBidTag, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(currentBid, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE))
.addGroup(detailsPanelLayout.createSequentialGroup()
.addComponent(nameTag, GroupLayout.PREFERRED_SIZE, 111, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(name, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE))
.addGroup(detailsPanelLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
.addComponent(description, GroupLayout.PREFERRED_SIZE, 219, GroupLayout.PREFERRED_SIZE)
.addGroup(detailsPanelLayout.createSequentialGroup()
.addComponent(conditionTag, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(condition, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE)))
.addGroup(detailsPanelLayout.createSequentialGroup()
.addComponent(statusTag, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(status, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE)))
.addGap(0, 3, Short.MAX_VALUE)))
.addContainerGap())
);
detailsPanelLayout.setVerticalGroup(
detailsPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(detailsPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(detailsPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(nameTag)
.addComponent(name))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(detailsPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(currentBidTag)
.addComponent(currentBid))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(detailsPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(myBidTag)
.addComponent(myBid))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(detailsPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(conditionTag)
.addComponent(condition))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(descriptionTag)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(description, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
.addGap(16, 16, 16)
.addGroup(detailsPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(statusTag)
.addComponent(status))
.addContainerGap())
);
// Specifies Results Box Layout
allResultsPanel.setBorder(BorderFactory.createTitledBorder("All Results"));
GroupLayout allResultsPanelLayout = new GroupLayout(allResultsPanel);
allResultsPanel.setLayout(allResultsPanelLayout);
allResultsPanelLayout.setHorizontalGroup(
allResultsPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(allResultsPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(allResultsPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(tableScrollPane, GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
.addComponent(refreshButton, GroupLayout.Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 101, GroupLayout.PREFERRED_SIZE)
.addGroup(GroupLayout.Alignment.TRAILING, allResultsPanelLayout.createSequentialGroup()
.addComponent(selectedName, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(selectedPrice, GroupLayout.PREFERRED_SIZE, 73, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 53, Short.MAX_VALUE)
.addComponent(bidInput, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(bidButton)))
.addContainerGap())
);
allResultsPanelLayout.setVerticalGroup(
allResultsPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(GroupLayout.Alignment.TRAILING, allResultsPanelLayout.createSequentialGroup()
.addComponent(refreshButton)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tableScrollPane, GroupLayout.PREFERRED_SIZE, 372, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(allResultsPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(bidButton)
.addComponent(bidInput, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(selectedName)
.addComponent(selectedPrice))
.addContainerGap())
);
// Orders both panels in parent container
GroupLayout thisLayout = new GroupLayout(this);
this.setLayout(thisLayout);
thisLayout.setHorizontalGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(thisLayout
.createSequentialGroup().addContainerGap()
.addComponent(detailsPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(allResultsPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap()));
thisLayout
.setVerticalGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(GroupLayout.Alignment.TRAILING, thisLayout.createSequentialGroup().addContainerGap()
.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
.addComponent(detailsPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(allResultsPanel, GroupLayout.DEFAULT_SIZE,
GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap()));
}
// Returns page title
public String getTitle() {
return title;
}
// Updates table with listings from auction server
public void getListings() throws RemoteException, InvalidKeySpecException {
items = client.remoteGetUserBids();
Object[][] listings = new Object[items.keySet().size()][5];
int i = 0;
for (Integer key : items.keySet())
{
AuctionItem item = items.get(key);
listings[i][0] = item.getId();
listings[i][1] = item.getTitle();
listings[i][2] = String.format("£%.2f", item.getPrice());
listings[i][3] = item.getCondition();
listings[i][4] = item.getDescription();
i++;
}
listingsTable.setModel(new DefaultTableModel(listings,
new String[] { "Item ID", "Name", "Current Bid", "Condition", "Description" }) {
private static final long serialVersionUID = 1L;
Class[] types = new Class[] { Integer.class, String.class, String.class,
String.class, String.class };
boolean[] canEdit = new boolean[] { false, false, false, false, false };
public Class getColumnClass(int columnIndex) {
return types[columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit[columnIndex];
}
});
listingsTable.getColumnModel().getColumn(0).setPreferredWidth(40);
listingsTable.getColumnModel().getColumn(1).setPreferredWidth(100);
listingsTable.getColumnModel().getColumn(2).setPreferredWidth(60);
listingsTable.getColumnModel().getColumn(3).setPreferredWidth(46);
tableScrollPane.setViewportView(listingsTable);
listingsTable.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
rowSelect(evt);
}
});
selectedItem = null;
bidInput.setText("");
selectedName.setText("");
selectedPrice.setText("");
if(selectedItem != null)
selectedItem = items.get(selectedItem.getId());
}
public void showResults() {
name.setText(selectedItem.getTitle());
currentBid.setText(String.format("£%.2f", selectedItem.getPrice()));
myBid.setText(String.format("£%.2f", selectedItem.getBidders().get(client.getId().getId())));
condition.setText(selectedItem.getCondition());
description.setText("<html><p style=\"width:180px\">" + selectedItem.getDescription() + "</p></html>");
if(selectedItem.isActive()) {
status.setText("Active");
} else {
if(!selectedItem.isActive() && selectedItem.getHighestBidder().getId() == client.getId().getId())
status.setText("Successful");
else
status.setText("Unsuccessful");
}
}
//-----------------------------------------------Event Listeners-----------------------------------------------------------------------
// Used for button clicks
@Override
public void actionPerformed(ActionEvent e) {
try {
// Refresh Button
if (e.getSource() == refreshButton) {
getListings();;
// Bid Button
} else if (e.getSource() == bidButton) {
int itemid = selectedItem.getId();
float bid = Float.parseFloat(bidInput.getText());
float currentbid = selectedItem.getPrice();
if(bid > currentbid)
client.remotePlaceBid(itemid, bid);
else
JOptionPane.showMessageDialog(null, "Bid must be greater than current price");
}
} catch (RemoteException | InvalidKeySpecException e1) {
e1.printStackTrace();
}
}
// Waits for table rows to be selected
private void rowSelect(MouseEvent evt) {
JTable source = (JTable)evt.getSource();
int row = source.rowAtPoint( evt.getPoint() );
int itemid = Integer.parseInt(source.getModel().getValueAt(row, 0) + "");
selectedItem = items.get(itemid);
selectedName.setText(selectedItem.getTitle());
selectedPrice.setText(String.format("£%.2f", selectedItem.getPrice()));
showResults();
}
}