View Javadoc

1   // %3839090226:hoplugins.trainingExperience.ui%
2   package hoplugins.trainingExperience.ui;
3   
4   import hoplugins.Commons;
5   import hoplugins.TrainingExperience;
6   
7   import hoplugins.commons.utils.PluginProperty;
8   
9   import hoplugins.trainingExperience.ui.model.OutputTableModel;
10  import hoplugins.trainingExperience.ui.renderer.OutputTableRenderer;
11  
12  import plugins.IHOMiniModel;
13  
14  import java.awt.BorderLayout;
15  import java.awt.Dimension;
16  import java.awt.event.ActionEvent;
17  import java.awt.event.ActionListener;
18  
19  import javax.swing.JButton;
20  import javax.swing.JOptionPane;
21  import javax.swing.JPanel;
22  import javax.swing.JScrollPane;
23  import javax.swing.JTable;
24  import javax.swing.ListSelectionModel;
25  import javax.swing.table.TableColumn;
26  
27  
28  /***
29   * The Panel where the main training table is shown ("Training").
30   * 
31   * <p>
32   * TODO Costomize to show only players that received training?
33   * </p>
34   * 
35   * <p>
36   * TODO Maybe i want to test for players that haven't received trainings to preview effect of
37   * change of training.
38   * </p>
39   *
40   * @author <a href=mailto:draghetto@users.sourceforge.net>Massimiliano Amato</a>
41   */
42  public class OutputPanel extends JPanel {
43      //~ Instance fields ----------------------------------------------------------------------------
44  
45      private JPanel m_jpPanel;
46      private JTable m_jtOutputTable;
47      private OutputTableSorter sorter;
48  
49      //~ Constructors -------------------------------------------------------------------------------
50  
51      /***
52       * Creates a new OutputPanel object.
53       */
54      public OutputPanel() {
55          super();
56          jbInit();
57      }
58  
59      //~ Methods ------------------------------------------------------------------------------------
60  
61      /***
62       * update the panel with the new value
63       */
64      public void reload() {
65          OutputTableSorter otm = (OutputTableSorter) m_jtOutputTable.getModel();
66  
67          otm.fillWithData();
68      }
69  
70      /***
71       * Import a match from Hattrick
72       */
73      private void import_matches() {
74          IHOMiniModel model = Commons.getModel();
75          String input = JOptionPane.showInputDialog(PluginProperty.getString("GameID")); //$NON-NLS-1$
76  
77          try {
78              Integer matchID = new Integer(input);
79  
80              if (model.getHelper().isUserMatch(input)) {
81                  if (model.getHelper().downloadMatchData(matchID.intValue())) {
82                      model.getHelper().showMessage(null, PluginProperty.getString("MatchImported"), //$NON-NLS-1$
83                                                    PluginProperty.getString("ImportOK"), //$NON-NLS-1$
84                                                    1); //$NON-NLS-1$ //$NON-NLS-2$
85                      model.getGUI().doRefresh();
86                  }
87              } else {
88                  model.getHelper().showMessage(null, PluginProperty.getString("NotUserMatch"), //$NON-NLS-1$
89                                                PluginProperty.getString("ImportError"), 1); //$NON-NLS-1$ //$NON-NLS-2$
90              }
91          } catch (Exception e) {
92              model.getHelper().showMessage(null, PluginProperty.getString("MatchNotImported"), //$NON-NLS-1$
93                                            PluginProperty.getString("ImportError"), 1); //$NON-NLS-1$ //$NON-NLS-2$
94          }
95      }
96  
97      /***
98       * Initialize the object layout
99       */
100     private void jbInit() {
101         IHOMiniModel model = Commons.getModel();
102 
103         m_jpPanel = model.getGUI().createImagePanel();
104         m_jpPanel.setLayout(new BorderLayout());
105 
106         OutputTableModel outputTableModel = new OutputTableModel(model);
107 
108         sorter = new OutputTableSorter(outputTableModel);
109         m_jtOutputTable = new OutputTable(sorter);
110         m_jtOutputTable.getTableHeader().setReorderingAllowed(false);
111         sorter.setTableHeader(m_jtOutputTable.getTableHeader());
112         m_jtOutputTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
113         m_jtOutputTable.setDefaultRenderer(Object.class, new OutputTableRenderer());
114 
115         ListSelectionModel rowSM = m_jtOutputTable.getSelectionModel();
116 
117         rowSM.addListSelectionListener(new PlayerSelectionListener(m_jtOutputTable, 11));
118 
119         for (int i = 0; i < m_jtOutputTable.getColumnCount(); i++) {
120             TableColumn column = m_jtOutputTable.getColumnModel().getColumn(i);
121 
122             switch (i) {
123                 case 0:
124                     column.setPreferredWidth(150);
125                     break;
126 
127                 case 1:
128                     column.setPreferredWidth(43);
129                     break;
130 
131                 case 2:
132                     column.setPreferredWidth(100);
133                     break;
134 
135                 default:
136                     column.setPreferredWidth(70);
137             }
138         }
139 
140         // Hide column 11 (playerId)
141         m_jtOutputTable.getTableHeader().getColumnModel().getColumn(11).setPreferredWidth(0);
142         m_jtOutputTable.getTableHeader().getColumnModel().getColumn(11).setMinWidth(0);
143         m_jtOutputTable.getTableHeader().getColumnModel().getColumn(11).setMaxWidth(0);
144         m_jtOutputTable.setAutoResizeMode(0);
145         m_jtOutputTable.setPreferredScrollableViewportSize(new Dimension(500, 70));
146 
147         JScrollPane scrollPane = new JScrollPane(m_jtOutputTable);
148 
149         m_jpPanel.add(scrollPane, BorderLayout.CENTER);
150 
151         JPanel m_jpPanel2 = new JPanel();
152 
153         m_jpPanel2.setLayout(new BorderLayout());
154 
155         JButton p_JB_berechne = new JButton(PluginProperty.getString("Berechnen")); //$NON-NLS-1$
156 
157         p_JB_berechne.addActionListener(new ActionListener() {
158                 public void actionPerformed(ActionEvent arg0) {
159                     Commons.getModel().getTrainingsManager().recalcSubskills(true);
160                     reload();
161                     TrainingExperience.getTabbedPanel().getRecap().reload();
162                 }
163             });
164         m_jpPanel2.add(p_JB_berechne, BorderLayout.CENTER);
165 
166         JButton p_JB_import = new JButton(PluginProperty.getString("ImportMatch")); //$NON-NLS-1$
167 
168         p_JB_import.addActionListener(new ActionListener() {
169                 public void actionPerformed(ActionEvent arg0) {
170                     import_matches();
171                 }
172             });
173         m_jpPanel2.add(p_JB_import, BorderLayout.WEST);
174 
175         m_jpPanel.add(m_jpPanel2, BorderLayout.NORTH);
176         setLayout(new BorderLayout());
177         add(m_jpPanel, BorderLayout.CENTER);
178     }
179 }