package cse1030; import java.awt.Dimension; import java.io.File; import javax.swing.BoxLayout; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; /** * A view for the image viewer app. * * @author CSE1030Z * */ public class ViewerView extends JFrame { private static final long serialVersionUID = 5851493549391433289L; private JLabel img; private JFileChooser fc; /** * Create a view using the given model. * * @param m a model for the image viewer app */ public ViewerView(ViewerModel m) { super("Image Viewer"); ViewerController c = new ViewerController(m, this); img = new JLabel(""); JMenuBar bar = new JMenuBar(); this.setJMenuBar(bar); JMenu menu = new JMenu("File"); bar.add(menu); JMenuItem open = new JMenuItem("Open"); open.addActionListener(c); open.setActionCommand(ViewerController.OPEN); menu.add(open); JMenuItem clear = new JMenuItem("Clear"); clear.addActionListener(c); clear.setActionCommand(ViewerController.CLEAR); menu.add(clear); fc = new JFileChooser(); fc.setCurrentDirectory(new File("/cse/dept/www/course_archive/2012-13/W/1030/Z/labs/08/img")); fc.setMultiSelectionEnabled(true); JButton prev = new JButton("Previous"); prev.addActionListener(c); prev.setActionCommand(ViewerController.PREVIOUS); JButton next = new JButton("Next"); next.addActionListener(c); next.setActionCommand(ViewerController.NEXT); this.setMinimumSize(new Dimension(500, 500)); this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS)); this.add(prev); this.add(next); this.add(img); this.pack(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } /** * Gets an array of files to open from a file chooser dialog. * The returned array has length zero if the user has not selected * any files, or has chosen to cancel the file chooser dialog. * * @return an array of files to open */ public File[] getFilesToOpen() { File[] files = new File[0]; int returnVal = fc.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { files = fc.getSelectedFiles(); } return files; } /** * Sets the image shown by the view to the specified image. * * @param icon an image to show in the view */ public void setImage(ImageIcon icon) { if (this.img.getIcon() != icon) { this.img.setIcon(icon); this.pack(); } } }