1
2 package hoplugins.teamAnalyzer.ui.component;
3
4 import hoplugins.Commons;
5
6 import hoplugins.commons.ui.NumberTextField;
7 import hoplugins.commons.utils.PluginProperty;
8
9 import hoplugins.teamAnalyzer.ht.HattrickManager;
10 import hoplugins.teamAnalyzer.manager.TeamManager;
11 import hoplugins.teamAnalyzer.ui.controller.FavoriteItemListener;
12 import hoplugins.teamAnalyzer.vo.Team;
13
14 import java.awt.BorderLayout;
15 import java.awt.event.ActionEvent;
16 import java.awt.event.ActionListener;
17
18 import javax.swing.JButton;
19 import javax.swing.JLabel;
20 import javax.swing.JMenuItem;
21 import javax.swing.JPanel;
22
23
24 /***
25 * A panel that allows the user to add a new favourite team
26 *
27 * @author <a href=mailto:draghetto@users.sourceforge.net>Massimiliano Amato</a>
28 */
29 public class AddPanel extends JPanel {
30
31
32 /*** The Favourite Menu itself */
33 FavouriteMenu menu;
34
35 /*** The add button */
36 JButton addButton = new JButton(Commons.getModel().getResource().getProperty("Hinzufuegen"));
37
38 /*** A status label */
39 JLabel status = new JLabel();
40
41 /*** The text field */
42 NumberTextField teamId = new NumberTextField(8);
43
44
45
46 /***
47 * Creates a new AddPanel object.
48 *
49 * @param me the favourite menu, for reference
50 */
51 public AddPanel(FavouriteMenu me) {
52 jbInit();
53 menu = me;
54 }
55
56 /***
57 * Constructs a new instance.
58 */
59 public AddPanel() {
60 }
61
62
63
64 /***
65 * Initializes the state of this instance.
66 */
67 private void jbInit() {
68 setLayout(new BorderLayout());
69
70
71 add(addButton, BorderLayout.CENTER);
72 add(teamId, BorderLayout.WEST);
73 add(status, BorderLayout.SOUTH);
74
75 addButton.addActionListener(new ActionListener() {
76 public void actionPerformed(ActionEvent e) {
77 Team team = new Team();
78
79 team.setTeamId(teamId.getValue());
80
81 if (TeamManager.isTeamInList(teamId.getValue())) {
82 status.setText(PluginProperty.getString("Favourite.InList"));
83 teamId.setText("");
84
85 return;
86 }
87
88 if (menu.dao.isFavourite(teamId.getValue())) {
89 status.setText(PluginProperty.getString("Favourite.Already"));
90 teamId.setText("");
91
92 return;
93 }
94
95 try {
96 String teamName = HattrickManager.downloadTeam(teamId.getValue());
97
98 team.setName(teamName);
99 } catch (Exception e1) {
100 status.setText(PluginProperty.getString("Favourite.Error"));
101 teamId.setText("");
102
103 return;
104 }
105
106 status.setText(team.getName() + " "
107 + Commons.getModel().getResource().getProperty("hinzugefuegt"));
108 menu.teams.add(team);
109
110 JMenuItem item = new JMenuItem(team.getName());
111
112 item.addActionListener(new FavoriteItemListener(team));
113 menu.items.add(item);
114 menu.add(item, 0);
115 menu.dao.addTeam(team);
116 menu.itemDelete.setVisible(true);
117 teamId.setText("");
118 }
119 });
120 }
121 }