Iteración 3(Versión sin idiomas)
[ISBets21MAUBRY] / eclipse-workspace / ISBets21MAUBRY / src / main / java / gui / HistorialApuestasGUI.java
1 package gui;
2
3 import java.awt.Color;
4 import java.awt.Component;
5 import java.awt.Font;
6 import java.awt.Rectangle;
7 import java.awt.SystemColor;
8 import java.awt.event.ActionEvent;
9 import java.awt.event.ActionListener;
10 import java.awt.print.PrinterException;
11 import java.util.Vector;
12
13 import javax.swing.ImageIcon;
14 import javax.swing.JButton;
15 import javax.swing.JFrame;
16 import javax.swing.JLabel;
17 import javax.swing.JPanel;
18 import javax.swing.JScrollPane;
19 import javax.swing.JSeparator;
20 import javax.swing.JTable;
21 import javax.swing.table.DefaultTableCellRenderer;
22 import javax.swing.table.DefaultTableModel;
23 import javax.swing.table.TableCellRenderer;
24 import javax.swing.table.TableColumn;
25
26 import businessLogic.BLFacade;
27 import domain.Bet;
28 import domain.RegularUser;
29
30 public class HistorialApuestasGUI extends JFrame {
31
32         private JScrollPane scrollPaneApuestas = new JScrollPane();
33         private JScrollPane scrollPaneApuestas2 = new JScrollPane();
34
35         private JTable tabla;
36         private JTable tabla2;
37
38         private String[] nombresColumnas = { "Fecha", "Evento", "Pregunta", "Apuesta a ", "Cant. apostada", "Cuota",
39                         "Estado" };
40         private String[] nombresColumnas2 = { "Fecha", "Evento", "Pregunta", "Apuesta a", "Estado", "Pron. ganador",
41                         "Cant. apostada", "Cuota", "Ganado (Beneficio)" };
42
43 //      private String[][] datosFilas = {
44 //
45 //                      { "Bryan", "Sanchez", "23", "Español", "170" }, { "Mauri", "Contreras", "22", "Peruano", "150" },
46 //                      { "Melisa", "Fernandez", "21", "Español", "150" }, { "Jorshua", "Dickensen", "23", "Peruano", "160" },
47 //                      { "Leire", "Cartagena", "20", "Español", "150" }, { "Kofi", "Darko", "21", "Africano", "160" },
48 //
49 //      };
50
51         private DefaultTableModel tableModelApuestas = new DefaultTableModel(null, nombresColumnas) {
52                 @Override
53                 public boolean isCellEditable(int row, int column) {
54                         return false;
55                 }
56         };
57
58         private DefaultTableModel tableModelApuestas2 = new DefaultTableModel(null, nombresColumnas2) {
59                 @Override
60                 public boolean isCellEditable(int row, int column) {
61                         return false;
62                 }
63         };
64 //      private String estadoApuesta = "";
65
66         private RegularUser userlog;
67
68         private static BLFacade facade = LoginGUI.getBusinessLogic();
69         private JLabel lblInfo;
70
71         public HistorialApuestasGUI(RegularUser ru) {
72                 getContentPane().setBackground(Color.WHITE);
73
74                 userlog = ru;
75                 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
76 //              setTitle(ResourceBundle.getBundle("Etiquetas").getString("MainGUI.btnHistorial.text")
77 //                              + "                                                                                                                                          " +  userlog.getUserName());
78                 setTitle("btnHistorial"
79                                 + "                                                                                                                                          "
80                                 + userlog.getUserName());
81
82                 getContentPane().setLayout(null);
83                 this.setSize(1388, 641);
84
85                 Vector<Bet> apuestasUsuario = facade.getApuestasByUser(userlog);
86
87                 /* TABLA 1 APUESTAS ABIERTAS */
88
89                 // Introducir datos de las dos tablas a los tablemodel, dependiendo de si la
90                 // apuesta esta cerrada o no
91                 for (Bet b : apuestasUsuario) {
92
93 //                      if (b.getForecast().getForecast().equals(b.getForecast().getQuestion().getResult())) {
94 //                              estadoApuesta = "Ganada";
95 //                      } else {
96 //                              estadoApuesta = "Perdida";
97 //                      }
98
99                         if (b.getForecast().getQuestion().getResult() == null && b.getEstadoApuesta().equals("Anulada") == false) { // PRIMERA
100                                                                                                                                                                                                                                                 // TABLA
101                                                                                                                                                                                                                                                 // APUESTAS
102                                                                                                                                                                                                                                                 // ABIERTAS
103                                 Vector<Object> row = new Vector<Object>();
104                                 row.add(b.getForecast().getQuestion().getEvent().getEventDate().toString().substring(0, 11));
105                                 row.add(b.getForecast().getQuestion().getEvent().getDescription());
106                                 row.add(b.getForecast().getQuestion().getQuestion());
107                                 row.add(b.getForecast().getForecast());
108                                 row.add(b.getAmount());
109                                 row.add(b.getForecast().getFee());
110                                 row.add(b.getEstadoApuesta());
111
112                                 tableModelApuestas.addRow(row);
113                         } else { // SEGUNDA TABLA APUESTAS CERRADAS
114                                 Vector<Object> row = new Vector<Object>();
115                                 row.add(b.getForecast().getQuestion().getEvent().getEventDate().toString().substring(0, 11));
116                                 row.add(b.getForecast().getQuestion().getEvent().getDescription());
117                                 row.add(b.getForecast().getQuestion().getQuestion());
118                                 row.add(b.getForecast().getForecast());
119                                 row.add(b.getEstadoApuesta());
120                                 row.add(b.getForecast().getQuestion().getResult());
121                                 row.add(b.getAmount());
122                                 row.add(b.getForecast().getFee());
123
124                                 if (b.getEstadoApuesta().equals("Perdida")) {
125                                         row.add("");
126
127                                 } else if (b.getEstadoApuesta().equals("Anulada")) {
128
129                                 } else {
130
131                                         if (Float.toString((b.getAmount() * b.getForecast().getFee() - b.getAmount())).equals("0.0")) {
132                                                 row.add(b.getAmount() * b.getForecast().getFee());
133
134                                         } else {
135                                                 row.add(b.getAmount() * b.getForecast().getFee() + " ("
136                                                                 + Float.toString((b.getAmount() * b.getForecast().getFee() - b.getAmount())) + ") ");
137
138                                         }
139                                 }
140                                 tableModelApuestas2.addRow(row);
141                         }
142
143                 }
144
145                 // OVERRIDE DE METODO PARA QUE SE AJUSTEN AUTOMATICAMENTE LAS COLUMNAS DE LA
146                 // TABLA SEGUN EL CONTENIDO
147                 tabla = new JTable(tableModelApuestas) {
148                         @Override
149                         public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
150                                 Component component = super.prepareRenderer(renderer, row, column);
151                                 int rendererWidth = component.getPreferredSize().width;
152                                 TableColumn tableColumn = getColumnModel().getColumn(column);
153                                 tableColumn.setPreferredWidth(
154                                                 Math.max(rendererWidth + getIntercellSpacing().width, tableColumn.getPreferredWidth()));
155                                 return component;
156                         }
157
158                 };
159
160                 // COLOR DE LAS FILAS DE LA TABLA GRIS Y BLANCO
161                 tabla.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
162                         @Override
163                         public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
164                                         boolean hasFocus, int row, int column) {
165                                 final Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row,
166                                                 column);
167
168                                 c.setBackground(row % 2 == 0 ? new Color(233, 233, 233) : Color.WHITE);
169
170                                 return c;
171                         }
172                 });
173
174                 scrollPaneApuestas.setBounds(new Rectangle(35, 148, 958, 119));
175                 scrollPaneApuestas.setViewportView(tabla);
176                 this.getContentPane().add(scrollPaneApuestas);
177
178                 /* TABLA 2 APUESTAS CERRADAS */
179
180                 // OVERRIDE DE METODO PARA QUE SE AJUSTEN AUTOMATICAMENTE LAS COLUMNAS DE LA
181                 // TABLA SEGUN EL CONTENIDO
182                 tabla2 = new JTable(tableModelApuestas2) {
183                         @Override
184                         public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
185                                 Component component = super.prepareRenderer(renderer, row, column);
186                                 int rendererWidth = component.getPreferredSize().width;
187                                 TableColumn tableColumn = getColumnModel().getColumn(column);
188                                 tableColumn.setPreferredWidth(
189                                                 Math.max(rendererWidth + getIntercellSpacing().width, tableColumn.getPreferredWidth()));
190                                 return component;
191                         }
192
193                 };
194
195                 // COLOR DE LAS FILAS DE LA TABLA GRIS Y BLANCO
196                 tabla2.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
197                         @Override
198                         public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
199                                         boolean hasFocus, int row, int column) {
200                                 final Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row,
201                                                 column);
202
203                                 c.setBackground(row % 2 == 0 ? new Color(233, 233, 233) : Color.WHITE);
204
205                                 return c;
206                         }
207                 });
208
209                 scrollPaneApuestas2.setBounds(new Rectangle(35, 347, 1089, 119));
210                 scrollPaneApuestas2.setViewportView(tabla2);
211
212                 getContentPane().add(scrollPaneApuestas2);
213
214                 /* BOTON ATRAS, BOTONES DESCARGAR TABLA Y LABELS */
215
216 //              JButton btnAtras = new JButton(ResourceBundle.getBundle("Etiquetas").getString("Close"));
217                 JButton btnAtras = new JButton("Close");
218                 btnAtras.setFont(new Font("Dialog", Font.BOLD, 14));
219                 btnAtras.setForeground(Color.WHITE);
220                 btnAtras.setBackground(SystemColor.controlShadow);
221
222                 btnAtras.addActionListener(new ActionListener() {
223                         @Override
224                         public void actionPerformed(ActionEvent arg0) {
225
226                                 JFrame a = new MainGUI(userlog);
227                                 a.setAlwaysOnTop(true);
228                                 a.setVisible(true);
229                                 dispose();
230
231                         }
232                 });
233                 btnAtras.setBounds(35, 537, 99, 26);
234                 getContentPane().add(btnAtras);
235
236                 JLabel lblApuestasAbiertas = new JLabel("Apuestas abiertas");
237                 lblApuestasAbiertas.setBounds(49, 120, 147, 16);
238                 getContentPane().add(lblApuestasAbiertas);
239
240                 JLabel lblApuestasCerradas = new JLabel("Apuestas cerradas");
241                 lblApuestasCerradas.setBounds(49, 314, 147, 16);
242                 getContentPane().add(lblApuestasCerradas);
243
244                 JButton btnDescargarTabla = new JButton("Descargar tabla");
245                 btnDescargarTabla.setFont(new Font("Dialog", Font.BOLD, 14));
246                 btnDescargarTabla.setForeground(Color.WHITE);
247                 btnDescargarTabla.setBackground(SystemColor.textHighlight);
248                 btnDescargarTabla.addActionListener(new ActionListener() {
249                         @Override
250                         public void actionPerformed(ActionEvent e) {
251
252                                 try {
253                                         System.out.println("IMPRIMIDA LA TABLA");
254                                         setAlwaysOnTop(false);
255                                         tabla.print();
256                                 } catch (PrinterException e1) {
257                                         e1.printStackTrace();
258                                 }
259                         }
260                 });
261                 btnDescargarTabla.setBounds(1003, 193, 159, 26);
262                 getContentPane().add(btnDescargarTabla);
263
264                 JButton btnDescargarTabla2 = new JButton("Descargar tabla");
265                 btnDescargarTabla2.setForeground(Color.WHITE);
266                 btnDescargarTabla2.setFont(new Font("Dialog", Font.BOLD, 14));
267                 btnDescargarTabla2.setBackground(SystemColor.textHighlight);
268                 btnDescargarTabla2.addActionListener(new ActionListener() {
269                         @Override
270                         public void actionPerformed(ActionEvent arg0) {
271                                 try {
272                                         System.out.println("IMPRIMIDA LA TABLA 2");
273                                         setAlwaysOnTop(false);
274                                         tabla2.print();
275                                 } catch (PrinterException e1) {
276                                         e1.printStackTrace();
277                                 }
278                         }
279                 });
280                 btnDescargarTabla2.setBounds(1136, 401, 165, 26);
281                 getContentPane().add(btnDescargarTabla2);
282
283                 lblInfo = new JLabel(
284                                 "Aquí podrás encontrar el historial de tus apuestas, tanto las que están en curso como las que están cerradas.");
285                 lblInfo.setBounds(100, 44, 748, 16);
286                 getContentPane().add(lblInfo);
287
288                 JSeparator separator = new JSeparator();
289                 separator.setBounds(94, 72, 631, 15);
290                 getContentPane().add(separator);
291
292                 ImageIcon icon = new ImageIcon("imagenes/info.png");
293                 JLabel lblNewLabel = new JLabel(icon);
294                 lblNewLabel.setBounds(68, 37, 30, 30);
295                 getContentPane().add(lblNewLabel);
296
297                 JPanel panel = new JPanel();
298                 panel.setBackground(new Color(0, 0, 128));
299                 panel.setBounds(895, 37, 141, 30);
300                 getContentPane().add(panel);
301
302 //              JLabel lblSaldo = new JLabel(ResourceBundle.getBundle("Etiquetas").getString("MainGUI.lblSaldo") //$NON-NLS-1$ //$NON-NLS-2$
303                 JLabel lblSaldo = new JLabel("Balance" //$NON-NLS-1$ //$NON-NLS-2$
304                                 + " " + userlog.getBalance() + "€");
305                 panel.add(lblSaldo);
306                 lblSaldo.setFont(new Font("Tahoma", Font.BOLD, 14));
307                 lblSaldo.setForeground(Color.WHITE);
308
309         }
310 }