package controller; import model.Photo; import model.PhotoAlbumModel; import strategy.SortByDate; import strategy.SortByName; import strategy.SortBySize; import view.PhotoAlbumView; import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter; import java.io.File; import java.util.Date; /** * A controller that manages interactions between the photo album model and view. *
* This class handles user actions from the view, such as adding and deleting photos, * navigating through the album, and changing the sorting strategy. * It acts as an intermediary between the {@link PhotoAlbumModel} and {@link PhotoAlbumView}, * translating user interface events into model operations. * * @author Yuri Tatishchev * @version 0.1 2025-03-26 * @see PhotoAlbumModel * @see PhotoAlbumView * @see Photo */ public class PhotoAlbumController { private final PhotoAlbumModel model; private final PhotoAlbumView view; /** * Constructs a new photo album controller. * * @param model the photo album model to control * @param view the view to handle user interactions */ public PhotoAlbumController(PhotoAlbumModel model, PhotoAlbumView view) { this.model = model; this.view = view; } /** * Returns the photo album model being controlled. * * @return the photo album model */ public PhotoAlbumModel getModel() { return model; } /** * Handles the addition of a new photo to the album. * Opens a file chooser dialog for selecting an image file and * prompts for a photo name. */ public void handleAddPhoto() { JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileFilter(new FileNameExtensionFilter( "Image files", "jpg", "jpeg", "png", "gif" )); if (fileChooser.showOpenDialog(view) == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); String name = JOptionPane.showInputDialog(view, "Enter photo name:"); if (name != null && !name.trim().isEmpty()) { Photo photo = new Photo( name, file.getAbsolutePath(), new Date(), file.length() ); model.addPhoto(photo); } } } /** * Handles the deletion of a photo from the album. * Prompts for the name of the photo to delete. */ public void handleDeletePhoto() { String name = JOptionPane.showInputDialog(view, "Enter photo name to delete:"); if (name != null && !name.trim().isEmpty()) { model.deletePhoto(name); } } /** * Handles navigation to the next photo in the album. * Moves to the next photo if one exists. */ public void handleNext() { if (model.hasNext()) { model.next(); } } /** * Handles navigation to the previous photo in the album. * Moves to the previous photo if one exists. */ public void handlePrevious() { if (model.hasPrevious()) { model.previous(); } } /** * Handles changing the sorting strategy for photos in the album. * * @param strategy the sorting strategy to use: *