JScratch
Loading...
Searching...
No Matches
AssetPanel.java
Go to the documentation of this file.
1package com.jscratch.ui;
2
3import com.jscratch.WorkspaceManager;
4import javax.swing.*;
5import java.awt.*;
6import java.io.File;
7import java.nio.file.Files;
8import java.nio.file.StandardCopyOption;
9import java.util.List;
10
11public class AssetPanel extends JPanel {
12 private DefaultListModel<String> listModel = new DefaultListModel<>();
13 private JList<String> list = new JList<>(listModel);
14 private JLabel previewLabel = new JLabel("No Preview", SwingConstants.CENTER);
15 private String type;
16 private List<String> currentAssets;
17
18 public AssetPanel(String type, List<String> assets) {
19 this.type = type;
20 this.currentAssets = assets;
21 setLayout(new BorderLayout());
22
23 refreshList(assets);
24
25 JPanel leftPanel = new JPanel(new BorderLayout());
26 leftPanel.setPreferredSize(new Dimension(200, 0));
27
28 JPanel toolbar = new JPanel(new FlowLayout(FlowLayout.LEFT));
29 JButton addBtn = new JButton("Add " + type);
30 addBtn.addActionListener(e -> {
31 JFileChooser fc = new JFileChooser(WorkspaceManager.getAssetsDir());
32 if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
33 File file = fc.getSelectedFile();
34 try {
35 File dest = new File(WorkspaceManager.getAssetsDir(), file.getName());
36 Files.copy(file.toPath(), dest.toPath(), StandardCopyOption.REPLACE_EXISTING);
37 currentAssets.add(file.getName());
39 } catch (Exception ex) {
40 ex.printStackTrace();
41 JOptionPane.showMessageDialog(this, "Error copying asset: " + ex.getMessage());
42 }
43 }
44 });
45 toolbar.add(addBtn);
46
47 leftPanel.add(toolbar, BorderLayout.NORTH);
48 leftPanel.add(new JScrollPane(list), BorderLayout.CENTER);
49
50 previewLabel.setBorder(BorderFactory.createTitledBorder("Preview"));
51
52 list.addListSelectionListener(e -> {
53 if (!e.getValueIsAdjusting() && list.getSelectedIndex() != -1) {
54 updatePreview(list.getSelectedValue());
55 }
56 });
57
58 add(leftPanel, BorderLayout.WEST);
59 add(previewLabel, BorderLayout.CENTER);
60 }
61
62 public void refreshList(List<String> assets) {
63 this.currentAssets = assets;
64 listModel.clear();
65 for (String a : assets) listModel.addElement(a);
66 previewLabel.setIcon(null);
67 previewLabel.setText("No Preview");
68 }
69
70 private void updatePreview(String filename) {
71 File file = new File(WorkspaceManager.getAssetsDir(), filename);
72 if (!file.exists()) {
73 previewLabel.setText("File not found: " + filename);
74 return;
75 }
76
77 if (type.equals("Costume")) {
78 try {
79 ImageIcon icon = new ImageIcon(file.getAbsolutePath());
80 Image img = icon.getImage();
81 // Scale to fit while maintaining aspect ratio
82 int w = img.getWidth(null);
83 int h = img.getHeight(null);
84 double scale = Math.min(400.0 / w, 400.0 / h);
85 if (scale > 1.0) scale = 1.0;
86
87 Image scaled = img.getScaledInstance((int)(w * scale), (int)(h * scale), Image.SCALE_SMOOTH);
88 previewLabel.setIcon(new ImageIcon(scaled));
89 previewLabel.setText("");
90 } catch (Exception e) {
91 previewLabel.setIcon(null);
92 previewLabel.setText("Error loading image");
93 }
94 } else {
95 previewLabel.setText("Audio Asset: " + filename);
96 }
97 }
98}
DefaultListModel< String > listModel
void updatePreview(String filename)
void refreshList(List< String > assets)
List< String > currentAssets
AssetPanel(String type, List< String > assets)