hw3: PhotoAlbumView: add thumbnails, date, file size to list
This commit is contained in:
parent
528254c9bc
commit
b12cf9f6d2
@ -38,9 +38,21 @@ public class PhotoAlbumView extends JFrame implements PhotoAlbumModel.ModelChang
|
|||||||
deleteButton = new JButton("Delete Photo");
|
deleteButton = new JButton("Delete Photo");
|
||||||
previousButton = new JButton("Previous");
|
previousButton = new JButton("Previous");
|
||||||
nextButton = new JButton("Next");
|
nextButton = new JButton("Next");
|
||||||
|
// Disable navigation buttons by default
|
||||||
|
previousButton.setEnabled(false);
|
||||||
|
nextButton.setEnabled(false);
|
||||||
|
deleteButton.setEnabled(false);
|
||||||
|
|
||||||
String[] sortOptions = {"Sort by Name", "Sort by Date", "Sort by Size"};
|
String[] sortOptions = {"Sort by Name", "Sort by Date", "Sort by Size"};
|
||||||
sortingCombo = new JComboBox<>(sortOptions);
|
sortingCombo = new JComboBox<>(sortOptions);
|
||||||
|
// Select default sorting option
|
||||||
|
sortingCombo.setSelectedIndex(1);
|
||||||
|
|
||||||
|
listModel = new DefaultListModel<>();
|
||||||
|
photoList = new JList<>(listModel);
|
||||||
|
photoList.setEnabled(false);
|
||||||
|
photoList.setCellRenderer(new PhotoListCellRenderer());
|
||||||
|
photoList.setFixedCellHeight(60); // Accommodate thumbnails
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupLayout() {
|
private void setupLayout() {
|
||||||
@ -64,14 +76,6 @@ public class PhotoAlbumView extends JFrame implements PhotoAlbumModel.ModelChang
|
|||||||
controlPanel.add(deleteButton);
|
controlPanel.add(deleteButton);
|
||||||
controlPanel.add(sortingCombo);
|
controlPanel.add(sortingCombo);
|
||||||
add(controlPanel, BorderLayout.SOUTH);
|
add(controlPanel, BorderLayout.SOUTH);
|
||||||
|
|
||||||
// Disable navigation buttons by default
|
|
||||||
previousButton.setEnabled(false);
|
|
||||||
nextButton.setEnabled(false);
|
|
||||||
deleteButton.setEnabled(false);
|
|
||||||
|
|
||||||
// Select default sorting option
|
|
||||||
sortingCombo.setSelectedIndex(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setController(PhotoAlbumController controller) {
|
public void setController(PhotoAlbumController controller) {
|
||||||
@ -83,14 +87,6 @@ public class PhotoAlbumView extends JFrame implements PhotoAlbumModel.ModelChang
|
|||||||
nextButton.addActionListener(e -> controller.handleNext());
|
nextButton.addActionListener(e -> controller.handleNext());
|
||||||
previousButton.addActionListener(e -> controller.handlePrevious());
|
previousButton.addActionListener(e -> controller.handlePrevious());
|
||||||
sortingCombo.addActionListener(e -> controller.handleSort(sortingCombo.getSelectedIndex()));
|
sortingCombo.addActionListener(e -> controller.handleSort(sortingCombo.getSelectedIndex()));
|
||||||
photoList.addListSelectionListener(e -> {
|
|
||||||
if (!e.getValueIsAdjusting()) {
|
|
||||||
int index = photoList.getSelectedIndex();
|
|
||||||
if (index >= 0) {
|
|
||||||
controller.handlePhotoSelection(index);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -141,4 +137,68 @@ public class PhotoAlbumView extends JFrame implements PhotoAlbumModel.ModelChang
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class PhotoListCellRenderer extends JPanel implements ListCellRenderer<String> {
|
||||||
|
private final JLabel imageLabel = new JLabel();
|
||||||
|
private final JLabel textLabel = new JLabel();
|
||||||
|
private final JLabel detailsLabel = new JLabel();
|
||||||
|
|
||||||
|
public PhotoListCellRenderer() {
|
||||||
|
setLayout(new BorderLayout(5, 0));
|
||||||
|
|
||||||
|
// Left side - image
|
||||||
|
imageLabel.setPreferredSize(new Dimension(50, 50));
|
||||||
|
add(imageLabel, BorderLayout.WEST);
|
||||||
|
|
||||||
|
// Center - name and details
|
||||||
|
JPanel textPanel = new JPanel(new GridLayout(2, 1));
|
||||||
|
textPanel.setOpaque(false);
|
||||||
|
textLabel.setFont(textLabel.getFont().deriveFont(Font.BOLD));
|
||||||
|
detailsLabel.setForeground(Color.GRAY);
|
||||||
|
detailsLabel.setFont(detailsLabel.getFont().deriveFont(10.0f));
|
||||||
|
textPanel.add(textLabel);
|
||||||
|
textPanel.add(detailsLabel);
|
||||||
|
add(textPanel, BorderLayout.CENTER);
|
||||||
|
|
||||||
|
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
|
||||||
|
setOpaque(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Component getListCellRendererComponent(JList<? extends String> list,
|
||||||
|
String name, int index, boolean isSelected, boolean cellHasFocus) {
|
||||||
|
textLabel.setText(name);
|
||||||
|
|
||||||
|
Photo photo = model.getPhotos().get(index);
|
||||||
|
ImageIcon thumbnail = loadThumbnail(photo.filePath());
|
||||||
|
imageLabel.setIcon(thumbnail);
|
||||||
|
|
||||||
|
// Format date
|
||||||
|
String date = String.format("%tF", photo.dateAdded());
|
||||||
|
// Format file size
|
||||||
|
String size = formatFileSize(photo.fileSize());
|
||||||
|
detailsLabel.setText(date + " • " + size);
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String formatFileSize(long size) {
|
||||||
|
if (size < 1024 * 1024) {
|
||||||
|
return String.format("%.1f KB", size / 1024.0);
|
||||||
|
} else {
|
||||||
|
return String.format("%.1f MB", size / (1024.0 * 1024));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private ImageIcon loadThumbnail(String path) {
|
||||||
|
try {
|
||||||
|
ImageIcon icon = new ImageIcon(path);
|
||||||
|
Image img = icon.getImage();
|
||||||
|
Image scaledImg = img.getScaledInstance(50, 50, Image.SCALE_AREA_AVERAGING);
|
||||||
|
return new ImageIcon(scaledImg);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user