package cse1030; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; /** * A controller for the image viewer application. * * @author CSE1030Z * */ public class ViewerController implements ActionListener { /** * String for the action command for opening files. */ public static final String OPEN = "OPEN"; /** * String for the action command for clearing all of the images. */ public static final String CLEAR = "CLEAR"; /** * String for the action command for showing the previous image. */ public static final String PREVIOUS = "PREVIOUS"; /** * String for the action command for showing the next image. */ public static final String NEXT = "NEXT"; private ViewerModel model; private ViewerView view; /** * Creates a controller with the given model and view. * * @param model a ViewerModel * @param view a ViewerView */ public ViewerController(ViewerModel model, ViewerView view) { this.model = model; this.view = view; } /** * Method that responds to action events emitted from the view controls. * * @param event an event to respond to * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) */ @Override public void actionPerformed(ActionEvent event) { String action = event.getActionCommand(); if (action.equals(OPEN)) { File[] files = this.view.getFilesToOpen(); this.model.openImages(files); this.view.setImage(this.model.getCurrentImage()); } else if (action.equals(CLEAR)) { this.model.clearImages(); this.view.setImage(this.model.getCurrentImage()); } else if (action.equals(PREVIOUS)) { this.view.setImage(this.model.getPreviousImage()); } else if (action.equals(NEXT)) { this.view.setImage(this.model.getNextImage()); } } }