View Javadoc

1   // %4038744218:hoplugins.teamAnalyzer.ui%
2   package hoplugins.teamAnalyzer.ui;
3   
4   import gui.UserParameter;
5   
6   import hoplugins.Commons;
7   
8   import hoplugins.commons.utils.HTCalendar;
9   import hoplugins.commons.utils.HTCalendarFactory;
10  import hoplugins.commons.utils.PluginProperty;
11  import hoplugins.commons.utils.RatingUtil;
12  import hoplugins.commons.vo.MatchRating;
13  
14  import hoplugins.teamAnalyzer.SystemManager;
15  import hoplugins.teamAnalyzer.manager.MatchPopulator;
16  import hoplugins.teamAnalyzer.manager.ReportManager;
17  import hoplugins.teamAnalyzer.ui.model.UiRecapTableModel;
18  import hoplugins.teamAnalyzer.util.MatchUtil;
19  import hoplugins.teamAnalyzer.vo.Match;
20  import hoplugins.teamAnalyzer.vo.MatchDetail;
21  import hoplugins.teamAnalyzer.vo.TeamLineup;
22  
23  import java.awt.BorderLayout;
24  
25  import java.text.DecimalFormat;
26  
27  import java.util.Arrays;
28  import java.util.Calendar;
29  import java.util.Iterator;
30  import java.util.List;
31  import java.util.Vector;
32  
33  import javax.swing.ImageIcon;
34  import javax.swing.JPanel;
35  import javax.swing.JScrollPane;
36  import javax.swing.JTable;
37  import javax.swing.ListSelectionModel;
38  import javax.swing.event.ListSelectionEvent;
39  import javax.swing.event.ListSelectionListener;
40  
41  
42  /***
43   * TODO Missing Class Documentation
44   *
45   * @author TODO Author Name
46   */
47  public class RecapPanel extends JPanel {
48      //~ Static fields/initializers -----------------------------------------------------------------
49  
50      /*** TODO Missing Parameter Documentation */
51      private static final String VALUE_NA = "---"; //$NON-NLS-1$
52  
53      /*** TODO Missing Parameter Documentation */
54      private static final String GOALS_SPACE = " - "; //$NON-NLS-1$
55  
56      //~ Instance fields ----------------------------------------------------------------------------
57  
58      private JTable table;
59      private RecapTableSorter sorter;
60      private UiRecapTableModel tableModel;
61      private String[] columns = {
62                                     PluginProperty.getString("RecapPanel.Game"), //$NON-NLS-1$
63      PluginProperty.getString("Type"), //$NON-NLS-1$
64      PluginProperty.getString("RecapPanel.Result"), //$NON-NLS-1$
65      PluginProperty.getString("Week"), //$NON-NLS-1$
66      Commons.getModel().getResource().getProperty("Season"), //$NON-NLS-1$
67      Commons.getModel().getResource().getProperty("MatchMittelfeld"), //$NON-NLS-1$
68      Commons.getModel().getResource().getProperty("rechteAbwehrseite"), //$NON-NLS-1$
69      Commons.getModel().getResource().getProperty("Abwehrzentrum"), //$NON-NLS-1$
70      Commons.getModel().getResource().getProperty("linkeAbwehrseite"), //$NON-NLS-1$
71      Commons.getModel().getResource().getProperty("rechteAngriffsseite"), //$NON-NLS-1$
72      Commons.getModel().getResource().getProperty("Angriffszentrum"), //$NON-NLS-1$
73      Commons.getModel().getResource().getProperty("linkeAngriffsseite"), //$NON-NLS-1$
74      PluginProperty.getString("RecapPanel.Stars"), //$NON-NLS-1$
75      Commons.getModel().getResource().getProperty("Gesamtstaerke"), //$NON-NLS-1$
76      PluginProperty.getString("Squad"), //$NON-NLS-1$
77      PluginProperty.getString("SmartSquad"), //$NON-NLS-1$		
78      "LoddarStats", //$NON-NLS-1$
79      PluginProperty.getString("RecapPanel.Tactic"), //$NON-NLS-1$
80      Commons.getModel().getResource().getProperty("Taktikstaerke"), //$NON-NLS-1$
81      PluginProperty.getString("RecapPanel.Formation"), //$NON-NLS-1$ 
82      "", //$NON-NLS-1$ 
83      "" //$NON-NLS-1$
84                                 };
85  
86      //~ Constructors -------------------------------------------------------------------------------
87  
88      /***
89       * Creates a new RecapPanel object.
90       */
91      public RecapPanel() {
92          jbInit();
93      }
94  
95      //~ Methods ------------------------------------------------------------------------------------
96  
97      /***
98       * TODO Missing Method Documentation
99       *
100      * @param lineup TODO Missing Method Parameter Documentation
101      */
102     public void reload(TeamLineup lineup) {
103         // Empty model
104         while (tableModel.getRowCount() > 0) {
105             tableModel.removeRow(0);
106         }
107 
108         // table.updateUI();
109         MatchRating averageRating = null;
110         double stars = 0;
111 
112         if (lineup != null) {
113             averageRating = lineup.getRating();
114             stars = lineup.getStars();
115         }
116 
117         List list = MatchPopulator.getAnalyzedMatch();
118         Vector rowData;
119 
120         if (list.size() > 1) {
121             rowData = new Vector();
122             rowData.add(PluginProperty.getString("RecapPanel.Average")); //$NON-NLS-1$
123             rowData.add(VALUE_NA);
124             rowData.add(VALUE_NA);
125             rowData.add(VALUE_NA);
126             rowData.add(VALUE_NA);
127             setRating(rowData, averageRating);
128 
129             DecimalFormat df = new DecimalFormat("###.#"); //$NON-NLS-1$
130 
131             rowData.add(df.format(stars));
132             rowData.add(df.format(averageRating.getHatStats()));
133             rowData.add(df.format(averageRating.getSquad()));
134             rowData.add(df.format(averageRating.getSquad() / stars));
135             rowData.add(VALUE_NA);
136             rowData.add(VALUE_NA);
137             rowData.add(VALUE_NA);
138             rowData.add(VALUE_NA);
139             rowData.add(""); //$NON-NLS-1$
140             rowData.add(""); //$NON-NLS-1$
141             tableModel.addRow(rowData);
142             table.getSelectionModel().setSelectionInterval(0, 0);
143         }
144 
145         for (Iterator iter = list.iterator(); iter.hasNext();) {
146             MatchDetail matchDetail = (MatchDetail) iter.next();
147 
148             rowData = new Vector();
149 
150             Match match = matchDetail.getMatchDetail();
151 
152             int matchType = match.getMatchType();
153             boolean isHomeMatch = MatchUtil.isHome(match);
154 
155             // Columns 0-2
156             if (isHomeMatch) {
157                 rowData.add(match.getAwayTeam());
158                 rowData.add(Commons.getModel().getHelper().getImageIcon4Spieltyp(matchType));
159                 rowData.add(match.getHomeGoals() + GOALS_SPACE + match.getAwayGoals());
160             } else {
161                 rowData.add("* " + match.getHomeTeam()); //$NON-NLS-1$
162                 rowData.add(Commons.getModel().getHelper().getImageIcon4Spieltyp(matchType));
163                 rowData.add(match.getAwayGoals() + GOALS_SPACE + match.getHomeGoals());
164             }
165 
166             // Columns 3 & 4
167             rowData.add(match.getWeek() + ""); //$NON-NLS-1$
168             rowData.add(match.getSeason() + ""); //$NON-NLS-1$
169 
170             // Columns 5-11
171             setRating(rowData, matchDetail.getRating());
172 
173             DecimalFormat df = new DecimalFormat("###.#"); //$NON-NLS-1$
174 
175             // Columns 12-15
176             rowData.add(df.format(matchDetail.getStars()));
177             rowData.add(df.format(matchDetail.getRating().getHatStats()));
178             rowData.add(df.format(matchDetail.getRating().getSquad()));
179             rowData.add(df.format(matchDetail.getRating().getSquad() / matchDetail.getStars()));
180 
181             DecimalFormat df2 = new DecimalFormat("###.##"); //$NON-NLS-1$
182 
183             // Columns 16-17
184             rowData.add(df2.format(matchDetail.getRating().getLoddarStats(matchDetail.getTacticCode(),
185                                                                           matchDetail
186                                                                           .getTacticLevel())));
187             rowData.add(Commons.getModel().getHelper().getNameForTaktik(matchDetail.getTacticCode()));
188 
189             // Column 18
190             if (matchDetail.getTacticCode() == 0) {
191                 rowData.add(VALUE_NA);
192             } else {
193                 rowData.add(Commons.getModel().getHelper().getNameForSkill(matchDetail
194                                                                            .getTacticLevel(), false));
195             }
196 
197             // Columns 19-21
198             rowData.add(matchDetail.getFormation());
199             rowData.add(new Integer(matchType));
200             rowData.add(new Boolean(isHomeMatch));
201 
202             tableModel.addRow(rowData);
203         }
204 
205         if (list.size() == 0) {
206             rowData = new Vector();
207             rowData.add(PluginProperty.getString("RecapPanel.NoMatch")); //$NON-NLS-1$
208             tableModel.addRow(rowData);
209         }
210 
211         setColumnWidth(0, 100);
212         setColumnWidth(1, 20);
213         setColumnWidth(2, 40);
214         setColumnWidth(3, 50);
215         setColumnWidth(4, 50);
216 
217         if (SystemManager.getConfig().isStars()) {
218             setColumnWidth(12, 50);
219         } else {
220             setColumnInvisible(12);
221         }
222 
223         if (SystemManager.getConfig().isTotalStrength()) {
224             setColumnWidth(13, 50);
225         } else {
226             setColumnInvisible(13);
227         }
228 
229         if (SystemManager.getConfig().isSquad()) {
230             setColumnWidth(14, 50);
231         } else {
232             setColumnInvisible(14);
233         }
234 
235         if (SystemManager.getConfig().isSmartSquad()) {
236             setColumnWidth(15, 50);
237         } else {
238             setColumnInvisible(15);
239         }
240 
241         if (SystemManager.getConfig().isLoddarStats()) {
242             setColumnWidth(16, 50);
243         } else {
244             setColumnInvisible(16);
245         }
246 
247         // Hide 'match type' and 'is home match?' columns.
248         setColumnInvisible(20);
249         setColumnInvisible(21);
250     }
251 
252     /***
253      * TODO Missing Method Documentation
254      *
255      * @param col TODO Missing Method Parameter Documentation
256      */
257     private void setColumnInvisible(int col) {
258         table.getTableHeader().getColumnModel().getColumn(col).setWidth(0);
259         table.getTableHeader().getColumnModel().getColumn(col).setPreferredWidth(0);
260         table.getTableHeader().getColumnModel().getColumn(col).setMaxWidth(0);
261         table.getTableHeader().getColumnModel().getColumn(col).setMinWidth(0);
262     }
263 
264     /***
265      * TODO Missing Method Documentation
266      *
267      * @param col TODO Missing Method Parameter Documentation
268      * @param width TODO Missing Method Parameter Documentation
269      */
270     private void setColumnWidth(int col, int width) {
271         table.getTableHeader().getColumnModel().getColumn(col).setWidth(width);
272         table.getTableHeader().getColumnModel().getColumn(col).setPreferredWidth(width);
273         table.getTableHeader().getColumnModel().getColumn(col).setMaxWidth(200);
274         table.getTableHeader().getColumnModel().getColumn(col).setMinWidth(0);
275     }
276 
277     /***
278      * TODO Missing Method Documentation
279      *
280      * @param row TODO Missing Method Parameter Documentation
281      * @param rating TODO Missing Method Parameter Documentation
282      */
283     private void setRating(Vector row, MatchRating rating) {
284         if (rating == null) {
285             for (int i = 0; i < 7; i++) {
286                 row.add(""); //$NON-NLS-1$
287             }
288 
289             return;
290         }
291 
292         row.add(getRating((int) rating.getMidfield()));
293         row.add(getRating((int) rating.getRightDefense()));
294         row.add(getRating((int) rating.getCentralDefense()));
295         row.add(getRating((int) rating.getLeftDefense()));
296         row.add(getRating((int) rating.getRightAttack()));
297         row.add(getRating((int) rating.getCentralAttack()));
298         row.add(getRating((int) rating.getLeftAttack()));
299     }
300 
301     /***
302      * TODO Missing Method Documentation
303      *
304      * @param rating TODO Missing Method Parameter Documentation
305      *
306      * @return TODO Missing Return Method Documentation
307      */
308     private String getRating(int rating) {
309         return RatingUtil.getRating(rating, SystemManager.getConfig().isNumericRating(),
310                                     SystemManager.getConfig().isDescriptionRating(),
311                                     Commons.getModel());
312     }
313 
314     /***
315      * TODO Missing Method Documentation
316      */
317     private void jbInit() {
318         Vector data = new Vector();
319 
320         tableModel = new UiRecapTableModel(data, new Vector(Arrays.asList(columns)));
321 
322         sorter = new RecapTableSorter(tableModel);
323         table = new JTable(sorter);
324         sorter.setTableHeader(table.getTableHeader());
325 
326         // Set up tool tips for column headers.
327         table.getTableHeader().setToolTipText(PluginProperty.getString("RecapPanel.Tooltip")); //$NON-NLS-1$
328 
329         table.setDefaultRenderer(Object.class, new RecapTableRenderer());
330         table.setDefaultRenderer(ImageIcon.class, new RecapTableRenderer());
331         table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
332 
333         ListSelectionModel rowSM = table.getSelectionModel();
334 
335         rowSM.addListSelectionListener(new ListSelectionListener() {
336                 public void valueChanged(ListSelectionEvent e) {
337                     if (e.getValueIsAdjusting()) {
338                         return;
339                     }
340 
341                     ListSelectionModel lsm = (ListSelectionModel) e.getSource();
342 
343                     if (!lsm.isSelectionEmpty()) {
344                         int selectedRow = sorter.modelIndex(lsm.getMinSelectionIndex());
345 
346                         if (selectedRow == 0) {
347                             TeamLineup lineup = ReportManager.getLineup();
348                             Calendar calendar = Calendar.getInstance();
349                             calendar.add(Calendar.HOUR, UserParameter.instance().TimeZoneDifference);
350 
351                             HTCalendar cal = HTCalendarFactory.createTrainingCalendar(Commons
352                                                                                       .getModel(),
353                                                                                       calendar
354                                                                                       .getTime());
355                             SystemManager.getPlugin().getMainPanel().reload(lineup,
356                                                                             cal.getHTWeek(),
357                                                                             cal.getHTSeason());
358                             SystemManager.getPlugin().getRatingPanel().reload(lineup);
359                         } else {
360                             TeamLineup lineup = ReportManager.getLineup(selectedRow);
361                             int week = Integer.parseInt("" + tableModel.getValueAt(selectedRow, 3));
362                             int season = Integer.parseInt(""
363                                                           + tableModel.getValueAt(selectedRow, 4));
364 
365                             SystemManager.getPlugin().getMainPanel().reload(lineup, week, season);
366                             SystemManager.getPlugin().getRatingPanel().reload(lineup);
367                         }
368                     }
369                 }
370             });
371         setLayout(new BorderLayout());
372 
373         JScrollPane scrollPane = new JScrollPane(table);
374 
375         scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
376         scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
377         add(scrollPane);
378     }
379 }