4
4
import java .awt .Dimension ;
5
5
import java .awt .GridBagConstraints ;
6
6
import java .awt .GridBagLayout ;
7
+ import java .awt .Image ;
7
8
import java .awt .Insets ;
8
9
import java .awt .event .ActionEvent ;
9
10
import java .awt .event .ActionListener ;
10
11
import java .awt .event .KeyEvent ;
11
12
import java .awt .event .MouseWheelListener ;
13
+ import java .awt .image .BufferedImage ;
14
+ import java .beans .Beans ;
15
+ import java .io .File ;
16
+ import java .io .IOException ;
17
+ import java .util .List ;
12
18
19
+ import javax .imageio .ImageIO ;
13
20
import javax .swing .AbstractAction ;
21
+ import javax .swing .BorderFactory ;
22
+ import javax .swing .ImageIcon ;
14
23
import javax .swing .JComponent ;
15
24
import javax .swing .JLabel ;
16
25
import javax .swing .JPanel ;
17
26
import javax .swing .JScrollPane ;
18
27
import javax .swing .KeyStroke ;
19
28
import javax .swing .Timer ;
20
- import javax .swing .border .LineBorder ;
29
+
30
+ import org .slf4j .Logger ;
31
+ import org .slf4j .LoggerFactory ;
32
+
33
+ import se .lantz .gui .MainWindow ;
34
+ import se .lantz .model .carousel .CarouselPreviewModel ;
35
+ import se .lantz .model .data .GameDetails ;
21
36
22
37
public class CoverPanel extends JPanel
23
38
{
39
+ private static final Logger logger = LoggerFactory .getLogger (CarouselPreviewModel .class );
40
+
24
41
private JPanel panel ;
25
42
private JScrollPane scrollPane ;
26
43
@@ -30,11 +47,14 @@ public class CoverPanel extends JPanel
30
47
31
48
private ActionListener timerListener = e -> scrollFromTimer ();
32
49
33
- private Timer scrolingTimer = new Timer (10 , timerListener );
50
+ private Timer scrolingTimer = new Timer (7 , timerListener );
51
+ private CarouselPreviewModel model ;
52
+ private MainWindow mainWindow ;
34
53
35
- public CoverPanel ()
54
+ public CoverPanel (CarouselPreviewModel model , final MainWindow mainWindow )
36
55
{
37
- // setBorder(new LineBorder(new Color(0, 0, 0)));
56
+ this .model = model ;
57
+ this .mainWindow = mainWindow ;
38
58
GridBagLayout gridBagLayout = new GridBagLayout ();
39
59
gridBagLayout .rowWeights = new double [] { 1.0 };
40
60
gridBagLayout .columnWeights = new double [] { 1.0 };
@@ -47,7 +67,17 @@ public CoverPanel()
47
67
gbc_scrollPane .gridx = 0 ;
48
68
gbc_scrollPane .gridy = 0 ;
49
69
add (getScrollPane (), gbc_scrollPane );
50
- addCovers (100 );
70
+ addCovers (10 );
71
+
72
+ if (!Beans .isDesignTime ())
73
+ {
74
+ model .addPropertyChangeListener (CarouselPreviewModel .SELECTED_GAME , e -> {
75
+ reloadScreens ();
76
+ updateSelectedBorder ();
77
+ });
78
+ //trigger once at startup
79
+ reloadScreens ();
80
+ }
51
81
}
52
82
53
83
private JPanel getPanel ()
@@ -99,6 +129,18 @@ private void scrollFromTimer()
99
129
scrollingTimerIndex = 0 ;
100
130
scrolingTimer .stop ();
101
131
scrolingStopped = true ;
132
+ //Select the next game in the list
133
+ String gameId = "" ;
134
+
135
+ if (scrollDirectionRight )
136
+ {
137
+ gameId = model .getNextGameToSelectWhenScrollingRight ().getGameId ();
138
+ }
139
+ else
140
+ {
141
+ gameId = model .getNextGameToSelectWhenScrollingLeft ().getGameId ();
142
+ }
143
+ this .mainWindow .setSelectedGameInGameList (gameId );
102
144
}
103
145
}
104
146
@@ -125,12 +167,22 @@ private JScrollPane getScrollPane()
125
167
126
168
private void addCovers (int converCount )
127
169
{
128
-
129
170
for (int i = 0 ; i < converCount ; i ++)
130
171
{
131
172
GridBagConstraints gbc_label = new GridBagConstraints ();
132
173
gbc_label .fill = GridBagConstraints .BOTH ;
133
- gbc_label .insets = new Insets (5 , 22 , 5 , 23 );
174
+ if (i == 0 )
175
+ {
176
+ gbc_label .insets = new Insets (5 , 100 , 5 , 23 );
177
+ }
178
+ else if (i == converCount - 1 )
179
+ {
180
+ gbc_label .insets = new Insets (5 , 22 , 5 , 100 );
181
+ }
182
+ else
183
+ {
184
+ gbc_label .insets = new Insets (5 , 22 , 5 , 23 );
185
+ }
134
186
gbc_label .weighty = 1.0 ;
135
187
gbc_label .gridx = i ;
136
188
gbc_label .gridy = 0 ;
@@ -140,11 +192,12 @@ private void addCovers(int converCount)
140
192
141
193
private JLabel getLabel (int index )
142
194
{
143
- JLabel label = new JLabel ("Label" );
144
- label .setBorder (new LineBorder (new Color (0 , 0 , 0 )));
195
+ JLabel label = new JLabel ();
145
196
label .setPreferredSize (new Dimension (125 , 175 ));
146
- // label.setBackground(Color.red);
147
- // label.setOpaque(true);
197
+ if (index == 4 )
198
+ {
199
+ label .setBorder (BorderFactory .createLineBorder (Color .YELLOW , 5 ));
200
+ }
148
201
return label ;
149
202
}
150
203
@@ -157,4 +210,62 @@ private void scrollOneGame(boolean right)
157
210
scrolingTimer .start ();
158
211
}
159
212
}
213
+
214
+ private void reloadScreens ()
215
+ {
216
+ panel .setVisible (false );
217
+ //Remove all existing
218
+ panel .removeAll ();
219
+ addCovers (10 );
220
+ List <GameDetails > games = model .getGameDetails ();
221
+
222
+ for (int i = 0 ; i < games .size (); i ++)
223
+ {
224
+ GameDetails game = games .get (i );
225
+ loadScreen ((JLabel ) panel .getComponent (i ), game );
226
+ }
227
+ scrollToPosition ();
228
+ panel .setVisible (true );
229
+ }
230
+
231
+ private void updateSelectedBorder ()
232
+ {
233
+ for (int i = 0 ; i < panel .getComponentCount (); i ++)
234
+ {
235
+ ((JLabel ) panel .getComponent (i )).setBorder (null );
236
+ }
237
+
238
+ List <GameDetails > games = model .getGameDetails ();
239
+
240
+ for (int i = 0 ; i < games .size (); i ++)
241
+ {
242
+ if (games .indexOf (model .getSelectedGame ()) == i )
243
+ {
244
+ // logger.debug("Setting selected border to cover nr " + i);
245
+ ((JLabel ) panel .getComponent (i )).setBorder (BorderFactory .createLineBorder (Color .YELLOW , 5 ));
246
+ }
247
+ }
248
+ }
249
+
250
+ private void loadScreen (JLabel label , GameDetails game )
251
+ {
252
+ String filename = game .getCover ();
253
+ File imagefile = new File ("./covers/" + filename );
254
+ try
255
+ {
256
+ BufferedImage image = ImageIO .read (imagefile );
257
+ Image newImage = image .getScaledInstance (125 , 175 , Image .SCALE_SMOOTH );
258
+ label .setIcon (new ImageIcon (newImage ));
259
+ }
260
+ catch (IOException e )
261
+ {
262
+ (label ).setIcon (null );
263
+ }
264
+ }
265
+
266
+ public void scrollToPosition ()
267
+ {
268
+ //Scroll one game
269
+ scrollPane .getHorizontalScrollBar ().setValue (200 );
270
+ }
160
271
}
0 commit comments