cs151/hw3/src/controller/PhotoAlbumController.java

136 lines
3.9 KiB
Java

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.
* <p>
* 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:
* <ul>
* <li>0 for name-based sorting,
* <li>1 for date-based sorting,
* <li>2 for size-based sorting
* </ul>
*/
public void handleSort(int strategy) {
switch (strategy) {
case 0:
model.setSortingStrategy(new SortByName());
break;
case 1:
model.setSortingStrategy(new SortByDate());
break;
case 2:
model.setSortingStrategy(new SortBySize());
break;
}
}
}