package view; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.Image; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingConstants; import model.Category; import model.File; import model.FileEvent; import model.FileOperation; import model.Viewtype; /** * Browser showing files in Search result * */ @SuppressWarnings("serial") public class Browser extends javax.swing.JPanel { private ArrayList panels; private MainFrame mainFrame; /** * Creates a new Browser * * @param mainF * The MainFrame to pass events to */ public Browser(MainFrame mainF) { mainFrame = mainF; setLayout(new GridLayout(0, 3)); } /** * Removes all panels from the browser */ public void clear() { removeAll(); panels = new ArrayList(); } /** * Fill the browser with panels. The panels have an image preview or * standard image for the file depending on the filetype. The images are * placed on a button that opens the ModifyView for that file. Under the * button there's a Labes with teh title of the file. * * @param fileinp * File[] of files to fill with */ public void draw(File[] fileinp) { if (fileinp == null) return; try { ArrayList files = new ArrayList(); for (int i = 0; i < fileinp.length; i++) { files.add(fileinp[i]); } panels = new ArrayList(); for (int i = 0; i < files.size(); i++) { final File temp = files.get(i); JPanel tempPanel = new JPanel(new GridBagLayout()); JLabel tempLabel = new JLabel("", SwingConstants.CENTER); tempLabel.setPreferredSize(new Dimension(140, 15)); tempLabel.setText(temp.getTitle()); tempLabel.setFont(tempLabel.getFont().deriveFont((float) 12.0)); JButton tempButton = new JButton(); tempButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { ArrayList fileArray = new ArrayList(); fileArray.add(temp); mainFrame.passEvent(new FileEvent( Viewtype.ExtendedSearch, fileArray, temp, FileOperation.VIEW)); } }); ImageIcon thumb; if (temp.getCategory() == Category.IMAGE) { thumb = new ImageIcon(temp.getPath()); } else if (temp.getCategory() == Category.VIDEO) { thumb = new ImageIcon("files/logo/video.png"); } else if (temp.getCategory() == Category.TEXT) { thumb = new ImageIcon("files/logo/text1.png"); } else if (temp.getCategory() == Category.AUDIO) { thumb = new ImageIcon("files/logo/muzieknoot.png"); } else { thumb = new ImageIcon("files/logo/overig.png"); } thumb.setImage(thumb.getImage().getScaledInstance(140, -1, Image.SCALE_FAST)); tempButton.setIcon(thumb); tempButton.setPreferredSize(new Dimension(140, thumb .getIconHeight())); GridBagConstraints tc = new GridBagConstraints(); tc.fill = GridBagConstraints.NONE; tc.insets = new Insets(2, 8, 2, 8); tc.gridx = 0; tc.gridy = 0; tempPanel.add(tempButton, tc); tc.gridy++; tc.anchor = GridBagConstraints.SOUTH; tempPanel.add(tempLabel, tc); panels.add(tempPanel); } for (JPanel p : panels) { add(p); } } catch (Exception e) { e.printStackTrace(); } } }