package net.jalbum.slideshow; import se.datadosen.explorer.ImageCache; import se.datadosen.jalbum.AlbumObject; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Image; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.IOException; import java.util.List; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.OverlayLayout; import se.datadosen.jalbum.Flag; import se.datadosen.jalbum.Icons; import se.datadosen.jalbum.JAlbumContext; import se.datadosen.util.Platform; import se.datadosen.util.VersionNumber; /** * A fullscreen slide show for JAlbum */ class BasicSlideShow extends JFrame implements SlideShow { BorderLayout borderLayout1 = new BorderLayout(); java.util.List albumObjects; int currentIndex; Image previousImage; Image currentImage; Image nextImage; JLabel imageLabel = new JLabel(); JPanel statusPanel = new JPanel(new FlowLayout(FlowLayout.LEADING)); JLabel[] stars = new JLabel[5]; JLabel flagLabel = new JLabel(); Dimension screenSize = getGraphicsConfiguration().getBounds().getSize(); ImageCache slideCache = new ImageCache(4, screenSize); private Player player; BasicSlideShow(List albumObjects, AlbumObject start) { super(JAlbumContext.getInstance().getFrame().getGraphicsConfiguration()); try { this.albumObjects = albumObjects; jbInit(); if (!(Platform.isMac() && new VersionNumber(System.getProperty("java.specification.version")).compareTo(new VersionNumber("1.7")) >= 0)) { getGraphicsConfiguration().getDevice().setFullScreenWindow(this); } for (int i = 0; i < albumObjects.size(); i++) { AlbumObject ao = (AlbumObject) albumObjects.get(i); if (ao == start) { currentIndex = i; showImage(i); return; } } showImage(0); } catch (Exception ex) { ex.printStackTrace(); } } void showImage(final int index) { try { if (player != null) { player.reset(); } // currentIndex = index; AlbumObject ao = albumObjects.get(index); ImageIcon icon = new ImageIcon(slideCache.getImage(ao)); currentImage = icon.getImage(); imageLabel.setIcon(icon); showRating(ao.getRating()); showFlag(ao.getFlag()); if (index < (albumObjects.size() - 1)) { slideCache.preload((AlbumObject) albumObjects.get(index + 1)); } } catch (IOException ex) { ex.printStackTrace(System.err); } } // Navigation actions void forward() { if (currentIndex < (albumObjects.size() - 1)) { showImage(++currentIndex); } else { if (imageLabel.getIcon() == null) { quit(); } imageLabel.setIcon(null); showRating(0); showFlag(Flag.NoFlag); } } void backward() { if (currentIndex > 0) { showImage(--currentIndex); } } void first() { currentIndex = 0; showImage(currentIndex); } void last() { currentIndex = albumObjects.size() - 1; showImage(currentIndex); } private void quit() { if (player != null) { player.interrupt(); player = null; } getGraphicsConfiguration().getDevice().setFullScreenWindow(null); setVisible(false); slideCache.dispose(); dispose(); } private void togglePlay() { if (player != null) { player.interrupt(); player = null; } else { player = new Player(); player.start(); } } void jbInit() throws Exception { setUndecorated(true); // Java 1.4 Now it looks like a JWindow, but // keyboard input works Container c = this.getContentPane(); c.setBackground(Color.black); addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_ESCAPE: quit(); break; case KeyEvent.VK_RIGHT: case KeyEvent.VK_DOWN: case KeyEvent.VK_PAGE_DOWN: case KeyEvent.VK_ENTER: forward(); break; case KeyEvent.VK_LEFT: case KeyEvent.VK_UP: case KeyEvent.VK_PAGE_UP: case KeyEvent.VK_BACK_SPACE: backward(); break; case KeyEvent.VK_HOME: first(); break; case KeyEvent.VK_END: last(); break; case KeyEvent.VK_SPACE: togglePlay(); break; case KeyEvent.VK_0: case KeyEvent.VK_1: case KeyEvent.VK_2: case KeyEvent.VK_3: case KeyEvent.VK_4: case KeyEvent.VK_5: case KeyEvent.VK_NUMPAD0: case KeyEvent.VK_NUMPAD1: case KeyEvent.VK_NUMPAD2: case KeyEvent.VK_NUMPAD3: case KeyEvent.VK_NUMPAD4: case KeyEvent.VK_NUMPAD5: setRating(e.getKeyChar() - '0'); break; case KeyEvent.VK_6: case KeyEvent.VK_NUMPAD6: setFlag(Flag.Select); break; case KeyEvent.VK_7: case KeyEvent.VK_NUMPAD7: setFlag(Flag.Second); break; case KeyEvent.VK_8: case KeyEvent.VK_NUMPAD8: setFlag(Flag.Review); break; case KeyEvent.VK_9: case KeyEvent.VK_NUMPAD9: setFlag(Flag.Approved); break; case KeyEvent.VK_H: statusPanel.setVisible(!statusPanel.isVisible()); break; } } }); addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON1) { forward(); } else if (e.getButton() == MouseEvent.BUTTON3) { backward(); } } }); imageLabel.setAlignmentX(0.5f); imageLabel.setAlignmentY(0.5f); OverlayLayout ol = new OverlayLayout((c)); c.setLayout(ol); statusPanel.setOpaque(false); for (int i = 0; i < stars.length; i++) { stars[i] = new JLabel(Icons.getIcon("big.star")); statusPanel.add(stars[i]); stars[i].setVisible(false); } statusPanel.add(flagLabel); c.add(statusPanel); c.add(imageLabel); setSize(screenSize); } private void setRating(int rating) { AlbumObject ao = albumObjects.get(currentIndex); ao.setRating(rating); showRating(rating); } private void setFlag(Flag flag) { AlbumObject ao = albumObjects.get(currentIndex); ao.setFlag(flag); showFlag(flag); } private void showRating(int rating) { for (int i = 0; i < rating; i++) { stars[i].setVisible(true); } for (int i = rating; i < stars.length; i++) { stars[i].setVisible(false); } } private void showFlag(Flag flag) { if (flag == Flag.NoFlag) { flagLabel.setVisible(false); } else { flagLabel.setIcon(flag.getIcon(48, false)); flagLabel.setToolTipText(flag.toString()); flagLabel.setVisible(true); } } public void start() { this.setVisible(true); } class Player extends Thread { private boolean resetFlag = false; public Player() { super("player"); setDaemon(true); } public void reset() { resetFlag = true; interrupt(); } @Override public void run() { synchronized (this) { while (true) { try { wait(5000); if (currentIndex < (albumObjects.size() - 1)) { showImage(++currentIndex); } else { player = null; break; } } catch (InterruptedException ex) { if (resetFlag) { resetFlag = false; } else { break; } } } } System.out.println("Player quitting"); } }; }