View Javadoc

1   // %988591988:hoplugins.teamAnalyzer.ui%
2   package hoplugins.teamAnalyzer.ui;
3   
4   import hoplugins.Commons;
5   
6   import hoplugins.commons.utils.PluginProperty;
7   
8   import hoplugins.teamAnalyzer.SystemManager;
9   import hoplugins.teamAnalyzer.comparator.RosterPlayerComparator;
10  import hoplugins.teamAnalyzer.manager.PlayerDataManager;
11  import hoplugins.teamAnalyzer.manager.ReportManager;
12  import hoplugins.teamAnalyzer.ui.model.UiRosterTableModel;
13  import hoplugins.teamAnalyzer.ui.renderer.PlayerPositionTableCellRenderer;
14  import hoplugins.teamAnalyzer.vo.PlayerInfo;
15  import hoplugins.teamAnalyzer.vo.RosterPlayerData;
16  import hoplugins.teamAnalyzer.vo.RosterRoleData;
17  import hoplugins.teamAnalyzer.vo.SpotLineup;
18  import hoplugins.teamAnalyzer.vo.TeamLineup;
19  
20  import java.awt.BorderLayout;
21  
22  import java.text.DecimalFormat;
23  
24  import java.util.ArrayList;
25  import java.util.Arrays;
26  import java.util.Collections;
27  import java.util.HashMap;
28  import java.util.Iterator;
29  import java.util.List;
30  import java.util.Map;
31  import java.util.Vector;
32  
33  import javax.swing.JPanel;
34  import javax.swing.JScrollPane;
35  import javax.swing.JTable;
36  import javax.swing.ListSelectionModel;
37  
38  
39  /***
40   * TODO Missing Class Documentation
41   *
42   * @author TODO Author Name
43   */
44  public class RosterPanel extends JPanel {
45      //~ Instance fields ----------------------------------------------------------------------------
46  
47      private JTable table;
48      private List oldPlayersInfo = new ArrayList();
49      private Map players = new HashMap();
50      private UiRosterTableModel tableModel;
51      private String[] columns = {
52                                     Commons.getModel().getResource().getProperty("Name"),
53                                     PluginProperty.getString("Role"),
54                                     PluginProperty.getString("Position"),
55                                     PluginProperty.getString("Secondary"),
56                                     PluginProperty.getString("GamesCode"),
57                                     Commons.getModel().getResource().getProperty("Alter"),
58                                     Commons.getModel().getResource().getProperty("Form"),
59                                     PluginProperty.getString("EXPCode"),
60                                     PluginProperty.getString("TSI"),
61                                     PluginProperty.getString("SpecialEvent"),
62                                     PluginProperty.getString("Max"), PluginProperty.getString("Avg"),
63                                     PluginProperty.getString("Min"), "Status", "PlayerId"
64                                 };
65      private boolean reloading = false;
66  
67      //~ Constructors -------------------------------------------------------------------------------
68  
69      /***
70       * Creates a new RosterPanel object.
71       */
72      public RosterPanel() {
73          jbInit();
74      }
75  
76      //~ Methods ------------------------------------------------------------------------------------
77  
78      /***
79       * TODO Missing Method Documentation
80       *
81       * @param playerId TODO Missing Method Parameter Documentation
82       *
83       * @return TODO Missing Return Method Documentation
84       */
85      public PlayerInfo getPrevious(int playerId) {
86          for (Iterator iter = oldPlayersInfo.iterator(); iter.hasNext();) {
87              PlayerInfo element = (PlayerInfo) iter.next();
88  
89              if (element.getPlayerId() == playerId) {
90                  return element;
91              }
92          }
93  
94          return new PlayerInfo();
95      }
96  
97      /***
98       * TODO Missing Method Documentation
99       */
100     public void reload() {
101         reloading = true;
102         players = new HashMap();
103         oldPlayersInfo = new ArrayList();
104 
105         DecimalFormat df = new DecimalFormat("#0.0");
106 
107         for (int i = 1; i <= SystemManager.getFilter().getNumber(); i++) {
108             TeamLineup lineup = null;
109 
110             try {
111                 lineup = ReportManager.getLineup(i);
112             } catch (RuntimeException e) {
113             }
114 
115             if (lineup != null) {
116                 for (int spot = 1; spot < 12; spot++) {
117                     SpotLineup spotLineup = lineup.getSpotLineup(spot);
118 
119                     if ((spotLineup != null) && (spotLineup.getPlayerId() > 0)) {
120                         RosterPlayerData data = getPlayerData(spotLineup);
121 
122                         data.addMatch(spotLineup);
123                     }
124                 }
125             }
126         }
127 
128         // Empty model
129         while (tableModel.getRowCount() > 0) {
130             tableModel.removeRow(0);
131         }
132 
133         List l = new ArrayList(players.values());
134 
135         Collections.sort(l, new RosterPlayerComparator());
136 
137         for (Iterator iter = l.iterator(); iter.hasNext();) {
138             RosterPlayerData element = (RosterPlayerData) iter.next();
139 
140             if (element.getId() > 0) {
141                 PlayerInfo info = PlayerDataManager.getPlayerInfo(element.getId());
142 
143                 if (info.getPlayerId() == 0) {
144                     info.setPlayerId(element.getId());
145                     info.setTeamId(SystemManager.getActiveTeamId());
146                 }
147 
148                 info.setName(element.getName());
149 
150                 Vector rowData = new Vector();
151                 RosterRoleData mainRole = element.getMainRole();
152 
153                 rowData.add(element.getName());
154 
155                 rowData.add(new Integer(mainRole.getPos()));
156                 rowData.add(new Integer(mainRole.getPos()));
157 
158                 StringBuffer sec = new StringBuffer();
159 
160                 for (Iterator iterator = element.getSecondaryRoles().iterator();
161                      iterator.hasNext();) {
162                     if (sec.toString().length() > 0) {
163                         sec.append(" - ");
164                     }
165 
166                     RosterRoleData role = (RosterRoleData) iterator.next();
167 
168                     sec.append(Commons.getModel().getHelper().getNameForPosition((byte) role.getPos()));
169                     sec.append("(" + role.getApp() + ") ");
170                     sec.append(df.format(role.getMax()) + "/" + df.format(role.getAvg()) + "/"
171                                + df.format(role.getMin()));
172                 }
173 
174                 rowData.add(sec.toString());
175                 rowData.add("" + mainRole.getApp());
176                 rowData.add("" + info.getAge());
177                 rowData.add("" + info.getForm());
178                 rowData.add("" + info.getExperience());
179                 rowData.add("" + info.getTSI());
180                 rowData.add("" + info.getSpecialEvent());
181 
182                 rowData.add(df.format(mainRole.getMax()));
183                 rowData.add(df.format(mainRole.getAvg()));
184                 rowData.add(df.format(mainRole.getMin()));
185                 rowData.add("" + info.getStatus());
186                 rowData.add("" + element.getId());
187 
188                 tableModel.addRow(rowData);
189                 oldPlayersInfo.add(PlayerDataManager.getPreviousPlayerInfo(element.getId()));
190             }
191         }
192 
193         setColumnWidth(0, 130);
194         setColumnWidth(1, 130);
195         setColumnInvisible(2);
196         setColumnWidth(3, 130);
197         setColumnWidth(4, 25);
198         setColumnWidth(5, 25);
199         setColumnWidth(6, 30);
200         setColumnWidth(7, 25);
201         setColumnWidth(8, 40);
202         setColumnWidth(9, 25);
203         setColumnWidth(10, 25);
204         setColumnWidth(11, 25);
205         setColumnWidth(12, 25);
206         setColumnInvisible(13);
207         setColumnInvisible(14);
208 
209         table.getColumnModel().getColumn(1).setCellRenderer(new PlayerPositionTableCellRenderer());
210 
211         reloading = false;
212     }
213 
214     /***
215      * TODO Missing Method Documentation
216      *
217      * @param col TODO Missing Method Parameter Documentation
218      */
219     private void setColumnInvisible(int col) {
220         table.getTableHeader().getColumnModel().getColumn(col).setWidth(0);
221         table.getTableHeader().getColumnModel().getColumn(col).setPreferredWidth(0);
222         table.getTableHeader().getColumnModel().getColumn(col).setMaxWidth(0);
223         table.getTableHeader().getColumnModel().getColumn(col).setMinWidth(0);
224     }
225 
226     /***
227      * TODO Missing Method Documentation
228      *
229      * @param col TODO Missing Method Parameter Documentation
230      * @param width TODO Missing Method Parameter Documentation
231      */
232     private void setColumnWidth(int col, int width) {
233         table.getTableHeader().getColumnModel().getColumn(col).setWidth(width);
234         table.getTableHeader().getColumnModel().getColumn(col).setPreferredWidth(width);
235         table.getTableHeader().getColumnModel().getColumn(col).setMinWidth(0);
236     }
237 
238     /***
239      * TODO Missing Method Documentation
240      *
241      * @param spot TODO Missing Method Parameter Documentation
242      *
243      * @return TODO Missing Return Method Documentation
244      */
245     private RosterPlayerData getPlayerData(SpotLineup spot) {
246         RosterPlayerData data = (RosterPlayerData) players.get("" + spot.getPlayerId());
247 
248         if (data == null) {
249             data = new RosterPlayerData();
250             data.setId(spot.getPlayerId());
251             data.setName(spot.getName());
252             players.put("" + spot.getPlayerId(), data);
253         }
254 
255         return data;
256     }
257 
258     /***
259      * TODO Missing Method Documentation
260      */
261     private void jbInit() {
262         Vector data = new Vector();
263 
264         tableModel = new UiRosterTableModel(data, new Vector(Arrays.asList(columns)));
265         table = new JTable(tableModel);
266 
267         // Set up tool tips for column headers.
268         table.getTableHeader().setToolTipText(PluginProperty.getString("RecapPanel.Tooltip")); //$NON-NLS-1$
269         table.getTableHeader().setReorderingAllowed(false);
270 
271         table.setDefaultRenderer(Object.class, new RosterTableRenderer());
272 
273         table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
274 
275         setLayout(new BorderLayout());
276 
277         JScrollPane scrollPane = new JScrollPane(table);
278 
279         scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
280         scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
281         add(scrollPane);
282     }
283 }