View Javadoc

1   // %1176270089:hoplugins.teamAnalyzer.ui.component%
2   package hoplugins.teamAnalyzer.ui.component;
3   
4   import hoplugins.Commons;
5   
6   import hoplugins.commons.utils.PluginProperty;
7   
8   import hoplugins.teamAnalyzer.vo.Team;
9   
10  import java.awt.BorderLayout;
11  import java.awt.event.ActionEvent;
12  import java.awt.event.ActionListener;
13  
14  import java.util.Iterator;
15  
16  import javax.swing.JButton;
17  import javax.swing.JComboBox;
18  import javax.swing.JLabel;
19  import javax.swing.JMenuItem;
20  import javax.swing.JPanel;
21  
22  
23  /***
24   * A panel that allows the user to remove a favourite team
25   *
26   * @author <a href=mailto:draghetto@users.sourceforge.net>Massimiliano Amato</a>
27   */
28  public class DeletePanel extends JPanel {
29      //~ Instance fields ----------------------------------------------------------------------------
30  
31      /*** The Favourite Menu itself */
32      FavouriteMenu menu;
33  
34      /*** The add button */
35      JButton addButton = new JButton(Commons.getModel().getResource().getProperty("Hinzufuegen"));
36  
37      /*** The delete button */
38      JButton deletebutton = new JButton(Commons.getModel().getResource().getProperty("loeschen"));
39  
40      /*** ComboBox with the list of favourite teams */
41      JComboBox teams = new JComboBox();
42  
43      /*** A status label */
44      JLabel status = new JLabel();
45  
46      //~ Constructors -------------------------------------------------------------------------------
47  
48      /***
49       * Constructs a new instance.
50       *
51       * @param me the favourite menu for reference
52       */
53      public DeletePanel(FavouriteMenu me) {
54          menu = me;
55          jbInit();
56      }
57  
58      //~ Methods ------------------------------------------------------------------------------------
59  
60      /***
61       * Methods that fill the combo with  the favourite teams
62       */
63      private void fillCombo() {
64          teams.removeAllItems();
65  
66          for (Iterator iter = menu.teams.iterator(); iter.hasNext();) {
67              Team element = (Team) iter.next();
68  
69              teams.addItem(element);
70          }
71  
72          teams.setEnabled(true);
73          deletebutton.setEnabled(true);
74  
75          if (teams.getItemCount() == 0) {
76              teams.setEnabled(false);
77              deletebutton.setEnabled(false);
78          }
79      }
80  
81      /***
82       * Initializes the state of this instance.
83       */
84      private void jbInit() {
85          setLayout(new BorderLayout());
86          add(deletebutton, BorderLayout.CENTER);
87          add(teams, BorderLayout.WEST);
88          add(status, BorderLayout.SOUTH);
89  
90          fillCombo();
91  
92          deletebutton.addActionListener(new ActionListener() {
93                  public void actionPerformed(ActionEvent e) {
94                      Team team = (Team) teams.getSelectedItem();
95  
96                      if (team == null) {
97                          status.setText(PluginProperty.getString("Favourite.SelectTeam"));
98  
99                          return;
100                     }
101 
102                     int pos = menu.teams.indexOf(team);
103 
104                     menu.teams.remove(pos);
105 
106                     JMenuItem item = (JMenuItem) menu.items.get(pos);
107 
108                     menu.remove(item);
109                     menu.items.remove(pos);
110                     menu.dao.removeTeam(team.getTeamId());
111                     fillCombo();
112 
113                     if (menu.teams.size() == 0) {
114                         menu.itemDelete.setVisible(false);
115                     }
116                 }
117             });
118     }
119 }