1 | package geniusweb.exampleparties.humangui;
|
---|
2 |
|
---|
3 | import java.awt.BorderLayout;
|
---|
4 | import java.util.Arrays;
|
---|
5 | import java.util.Collections;
|
---|
6 | import java.util.HashSet;
|
---|
7 | import java.util.logging.Level;
|
---|
8 |
|
---|
9 | import javax.swing.JFrame;
|
---|
10 | import javax.swing.WindowConstants;
|
---|
11 |
|
---|
12 | import geniusweb.actions.Action;
|
---|
13 | import geniusweb.actions.Offer;
|
---|
14 | import geniusweb.inform.ActionDone;
|
---|
15 | import geniusweb.inform.Finished;
|
---|
16 | import geniusweb.inform.Inform;
|
---|
17 | import geniusweb.inform.Settings;
|
---|
18 | import geniusweb.inform.YourTurn;
|
---|
19 | import geniusweb.party.Capabilities;
|
---|
20 | import geniusweb.party.DefaultParty;
|
---|
21 | import geniusweb.party.Party;
|
---|
22 | import geniusweb.profile.Profile;
|
---|
23 | import geniusweb.profileconnection.ProfileConnectionFactory;
|
---|
24 | import geniusweb.profileconnection.ProfileInterface;
|
---|
25 | import tudelft.utilities.logging.Reporter;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * A {@link Party} that shows a GUI to the user showing the last received offer
|
---|
29 | * and allowing the user to enter his own bid, make an offer or accept the
|
---|
30 | * received offer. The GUI is shown on the window attached to the machine where
|
---|
31 | * the party runs, so this usually will be on the partyserver. Therefore the
|
---|
32 | * users usually will install a partyserver on their local machine to use this
|
---|
33 | * party.
|
---|
34 | */
|
---|
35 | public class HumanGui extends DefaultParty {
|
---|
36 |
|
---|
37 | private JFrame frame;
|
---|
38 | private BiddingInfo biddinginfo;
|
---|
39 |
|
---|
40 | public HumanGui() {
|
---|
41 | }
|
---|
42 |
|
---|
43 | public HumanGui(Reporter reporter) {
|
---|
44 | super(reporter); // for debugging
|
---|
45 | }
|
---|
46 |
|
---|
47 | @Override
|
---|
48 | public void notifyChange(Inform info) {
|
---|
49 | try {
|
---|
50 | if (info instanceof Settings) {
|
---|
51 | Settings settings = (Settings) info;
|
---|
52 | ProfileInterface profileint = ProfileConnectionFactory
|
---|
53 | .create(settings.getProfile().getURI(), reporter);
|
---|
54 | biddinginfo = new BiddingInfo(settings, getConnection(),
|
---|
55 | getReporter(), profileint);
|
---|
56 | initGUI();
|
---|
57 | } else if (info instanceof ActionDone) {
|
---|
58 | Action otheract = ((ActionDone) info).getAction();
|
---|
59 | if (otheract instanceof Offer) {
|
---|
60 | biddinginfo.receivedOffer((Offer) otheract);
|
---|
61 | }
|
---|
62 | } else if (info instanceof YourTurn) {
|
---|
63 | biddinginfo.setMyTurn(true);
|
---|
64 | } else if (info instanceof Finished) {
|
---|
65 | getReporter().log(Level.INFO, "Final ourcome:" + info);
|
---|
66 | if (frame != null)
|
---|
67 | frame.setVisible(false);
|
---|
68 | }
|
---|
69 | } catch (Exception e) {
|
---|
70 | throw new RuntimeException("Failed to handle info", e);
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | private void initGUI() {
|
---|
75 | frame = new JFrame("Negotiation for " + biddinginfo.getMyId());
|
---|
76 | frame.getContentPane().setLayout(new BorderLayout());
|
---|
77 | frame.add(new MyGUI(biddinginfo), BorderLayout.CENTER);
|
---|
78 | frame.pack();
|
---|
79 | // we could do frame.addWindowListener but that's a lot of code..
|
---|
80 | frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
|
---|
81 | frame.setVisible(true);
|
---|
82 | }
|
---|
83 |
|
---|
84 | @Override
|
---|
85 | public Capabilities getCapabilities() {
|
---|
86 | return new Capabilities(new HashSet<>(Arrays.asList("SAOP")),
|
---|
87 | Collections.singleton(Profile.class));
|
---|
88 | }
|
---|
89 |
|
---|
90 | @Override
|
---|
91 | public String getDescription() {
|
---|
92 | return "Offers a GUI to the human owning the partiesserver to manually make offers";
|
---|
93 | }
|
---|
94 |
|
---|
95 | }
|
---|