1
2 package hoplugins.transfers.ui;
3
4 import hoplugins.Commons;
5
6 import hoplugins.commons.ui.DefaultTableSorter;
7 import hoplugins.commons.ui.info.clearthought.layout.TableLayout;
8 import hoplugins.commons.utils.PluginProperty;
9
10 import hoplugins.transfers.dao.BookmarkDAO;
11 import hoplugins.transfers.dao.TransfersDAO;
12 import hoplugins.transfers.ui.model.TeamBookmarksTableModel;
13 import hoplugins.transfers.vo.Bookmark;
14 import hoplugins.transfers.vo.TransferTotals;
15
16 import java.awt.BorderLayout;
17 import java.awt.Dimension;
18
19 import java.util.List;
20 import java.util.Vector;
21
22 import javax.swing.ButtonGroup;
23 import javax.swing.ButtonModel;
24 import javax.swing.JLabel;
25 import javax.swing.JPanel;
26 import javax.swing.JRadioButton;
27 import javax.swing.JScrollPane;
28 import javax.swing.JSeparator;
29 import javax.swing.JSpinner;
30 import javax.swing.JTable;
31 import javax.swing.ListSelectionModel;
32 import javax.swing.SpinnerNumberModel;
33 import javax.swing.SwingConstants;
34 import javax.swing.event.ChangeEvent;
35 import javax.swing.event.ChangeListener;
36 import javax.swing.event.ListSelectionEvent;
37 import javax.swing.event.ListSelectionListener;
38 import javax.swing.table.TableModel;
39
40
41 /***
42 * Pane to show bookmarks.
43 *
44 * @author <a href=mailto:nethyperon@users.sourceforge.net>Boy van der Werf</a>
45 */
46 public class TeamBookmarkPane extends JPanel implements ListSelectionListener {
47
48
49 private ButtonModel spinSeason;
50 private JLabel amountTransfers = new JLabel("", SwingConstants.RIGHT);
51 private JLabel amountTransfersIn = new JLabel("", SwingConstants.RIGHT);
52 private JLabel amountTransfersOut = new JLabel("", SwingConstants.RIGHT);
53 private JSpinner spinner = new JSpinner();
54 private JTable bookmarkTable;
55 private List bookmarks;
56 private List transfers;
57 private TeamTransfersPane transferPane;
58 private TotalsPanel pricePanel;
59 private TotalsPanel tsiPanel;
60 private int teamid = 240416;
61
62
63
64 /***
65 * Creates an instance of the TeamBookmarkPane
66 */
67 public TeamBookmarkPane() {
68 super(new BorderLayout());
69
70
71 final double[][] sizes = {
72 {300},
73 {
74 TableLayout.PREFERRED, 10, TableLayout.PREFERRED,
75 TableLayout.FILL, TableLayout.PREFERRED, TableLayout.PREFERRED,
76 TableLayout.PREFERRED
77 }
78 };
79 final JPanel sidePanel = Commons.getModel().getGUI().createImagePanel();
80 sidePanel.setLayout(new TableLayout(sizes));
81 sidePanel.setOpaque(false);
82
83 final JPanel filterPanel = Commons.getModel().getGUI().createImagePanel();
84 filterPanel.setLayout(new TableLayout(new double[][]{
85 {
86 10, TableLayout.PREFERRED, 50,
87 TableLayout.FILL, 10
88 },
89 {10, TableLayout.PREFERRED, TableLayout.PREFERRED}
90 }));
91
92 final JRadioButton rb1 = new JRadioButton(PluginProperty.getString("AllSeason"));
93 rb1.setFocusable(false);
94 rb1.setOpaque(false);
95
96 final JRadioButton rb2 = new JRadioButton(PluginProperty.getString("Season"));
97 spinSeason = rb2.getModel();
98 rb2.setFocusable(false);
99 rb2.setOpaque(false);
100
101 if (Commons.getModel().getBasics().getSeason() > 0) {
102 spinner.setModel(new SpinnerNumberModel(Commons.getModel().getBasics().getSeason(), 1,
103 Commons.getModel().getBasics().getSeason(), 1));
104 } else {
105 rb2.setEnabled(false);
106 spinner.setModel(new SpinnerNumberModel());
107 }
108
109 spinner.setFocusable(false);
110 spinner.setEnabled(false);
111 spinner.addChangeListener(new ChangeListener() {
112 public void stateChanged(ChangeEvent e) {
113 refresh();
114 }
115 });
116
117 rb1.setSelected(true);
118 rb1.addChangeListener(new ChangeListener() {
119 public void stateChanged(ChangeEvent e) {
120 spinner.setEnabled(false);
121 refresh();
122 }
123 });
124 rb2.addChangeListener(new ChangeListener() {
125 public void stateChanged(ChangeEvent e) {
126 spinner.setEnabled(true);
127 refresh();
128 }
129 });
130
131 filterPanel.add(rb1, "1, 1, 2, 1");
132 filterPanel.add(rb2, "1, 2");
133 filterPanel.add(spinner, "2, 2");
134
135 final ButtonGroup bg = new ButtonGroup();
136 bg.add(rb1);
137 bg.add(rb2);
138
139 final JPanel amountPanel = Commons.getModel().getGUI().createImagePanel();
140 amountPanel.setLayout(new TableLayout(new double[][]{
141 {.25, 75, 25, .5, 50, 25, .25},
142 {10, 20, 20}
143 }));
144
145 final JLabel amTrans = new JLabel(PluginProperty.getString("Transfers") + ":", SwingConstants.LEFT);
146 final JLabel amTransIn = new JLabel(PluginProperty.getString("In") + ":", SwingConstants.LEFT);
147 amTransIn.setIcon(Icon.IN);
148
149 final JLabel amTransOut = new JLabel(PluginProperty.getString("Out") + ":", SwingConstants.LEFT);
150 amTransOut.setIcon(Icon.OUT);
151 amountPanel.add(amTrans, "1, 1");
152 amountPanel.add(amountTransfers, "2, 1");
153 amountPanel.add(amTransIn, "4, 1");
154 amountPanel.add(amountTransfersIn, "5, 1");
155 amountPanel.add(amTransOut, "4, 2");
156 amountPanel.add(amountTransfersOut, "5, 2");
157
158 pricePanel = new TotalsPanel(PluginProperty.getString("Price"),
159 Commons.getModel().getXtraDaten().getCurrencyName());
160 tsiPanel = new TotalsPanel(PluginProperty.getString("TSI"));
161
162 final TableModel model = new TeamBookmarksTableModel(new Vector());
163 final DefaultTableSorter sorter = new DefaultTableSorter(model);
164 bookmarkTable = new JTable(sorter);
165 sorter.setTableHeader(bookmarkTable.getTableHeader());
166
167 final JScrollPane bookmarkPanel = new JScrollPane(bookmarkTable);
168 bookmarkPanel.setOpaque(false);
169
170 bookmarkTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
171 bookmarkTable.getSelectionModel().addListSelectionListener(this);
172 bookmarkTable.getColumnModel().getColumn(0).setPreferredWidth(50);
173 bookmarkTable.getColumnModel().getColumn(1).setPreferredWidth(150);
174
175 sidePanel.add(filterPanel, "0, 0");
176 sidePanel.add(new JSeparator(), "0, 2");
177 sidePanel.add(bookmarkPanel, "0, 3");
178 sidePanel.add(amountPanel, "0, 4");
179 sidePanel.add(pricePanel, "0, 5");
180 sidePanel.add(tsiPanel, "0, 6");
181 sidePanel.setMinimumSize(new Dimension(285, 360));
182
183
184 final JPanel mainPanel = Commons.getModel().getGUI().createImagePanel();
185 mainPanel.setLayout(new BorderLayout());
186 add(mainPanel, BorderLayout.CENTER);
187
188 transferPane = new TeamTransfersPane();
189 mainPanel.add(transferPane, BorderLayout.CENTER);
190 mainPanel.add(sidePanel, BorderLayout.WEST);
191
192 refresh();
193 }
194
195
196
197 /***
198 * Refreshes the information on the panel.
199 */
200 public final void refresh() {
201 if (this.teamid > 0) {
202 if (spinSeason.isSelected()) {
203 final SpinnerNumberModel model = (SpinnerNumberModel) this.spinner.getModel();
204 this.transfers = TransfersDAO.getTransfers(teamid, model.getNumber().intValue(),
205 true, true);
206 } else {
207 this.transfers = TransfersDAO.getTransfers(teamid, 0, true, true);
208 }
209 } else {
210 this.transfers = new Vector();
211 }
212
213 this.bookmarks = BookmarkDAO.getBookmarks(Bookmark.TEAM);
214
215 final DefaultTableSorter sorter = (DefaultTableSorter) bookmarkTable.getModel();
216 sorter.setTableModel(new TeamBookmarksTableModel(transfers));
217
218 bookmarkTable.getColumnModel().getColumn(0).setPreferredWidth(50);
219 bookmarkTable.getColumnModel().getColumn(1).setPreferredWidth(150);
220
221 final TransferTotals totals = TransferTotals.calculateTotals(transfers);
222 pricePanel.setValues(totals.getBuyPriceTotal(), totals.getBuyPriceAvg(),
223 totals.getSellPriceTotal(), totals.getSellPriceAvg());
224 amountTransfers.setText(Integer.toString(totals.getAmountSell() + totals.getAmountBuy()));
225 amountTransfersIn.setText(Integer.toString(totals.getAmountBuy()));
226 amountTransfersOut.setText(Integer.toString(totals.getAmountSell()));
227 tsiPanel.setValues(totals.getBuyTsiTotal(), totals.getBuyTsiAvg(),
228 totals.getSellTsiTotal(), totals.getSellTsiAvg());
229 pricePanel.revalidate();
230
231 transferPane.refresh(this.transfers);
232 }
233
234 /*** {@inheritDoc} */
235 public final void valueChanged(ListSelectionEvent e) {
236 if (!e.getValueIsAdjusting()) {
237 final DefaultTableSorter sorter = (DefaultTableSorter) bookmarkTable.getModel();
238 final int index = sorter.modelIndex(bookmarkTable.getSelectedRow());
239
240 if (index >= 0) {
241 final Bookmark bookmark = (Bookmark) this.bookmarks.get(index);
242 this.teamid = bookmark.getId();
243 } else {
244 this.teamid = 0;
245 }
246 }
247 }
248 }