1    /*
2    Copyright 2000 by Ralph Hartley
3    This software is licenced under the terms of the
4    Gnu Public Licence
5    */
6    import java.awt.event.*;
7    import java.awt.*;
8    import java.io.*;
9    import javax.swing.*;
10   
11   public class Library extends LibraryItem {
12   
13     static int MINFILES = 10;
14     static int EXTRA = 2;
15   
16     public Library(File file,Icon myicon,LibraryUser user) {
17       super(file,user);
18       if (icon==null && iconsym==null) {
19         icon = myicon;
20         height = icon.getIconHeight();
21         width = icon.getIconWidth();
22       }
23     }
24   
25     public Library(File file,LibraryUser user) {
26       super(file,user);
27       if (icon==null && iconsym==null) {
28         icon = new ImageIcon(getClass().getResource("icons/folder.gif"));
29         height = icon.getIconHeight();
30         width = icon.getIconWidth();
31       }
32     }
33   
34     public void actionPerformed(ActionEvent e) {    
35       JFrame frame = new JFrame();
36   
37       frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
38   
39       File[] filelist = file.listFiles(new FileFilter() {
40           public boolean accept(File f) {
41             if (f.getName().endsWith(".smb")) return(true);
42             if (file.isDirectory()) return(true);
43             return(false);
44           }});
45   
46       int count = filelist.length;
47       if (count < MINFILES) count = MINFILES;
48       count += EXTRA;
49   
50       int cols = (int)Math.sqrt(count*2.0/3);
51       int rows = 1 + count/cols;
52       
53       frame.getContentPane().setLayout(new GridLayout(rows,cols));
54   
55       for (int i=0;i<cols*rows;i++) {
56         JButton slot = new JButton();
57         Icon icon;
58         if (i<filelist.length) {
59           icon = getItem(filelist[i],user);
60           slot.addActionListener((LibraryItem)icon);
61   //        slot.setText(((LibraryItem)icon).name);
62         }
63         else {
64           icon = new ImageIcon("icons/empty.gif");
65   //        slot.setText(((LibraryItem)icon).name);
66           slot.addActionListener(new LibraryAdder(slot,file,user));
67         }
68         slot.setIcon(icon);
69         frame.getContentPane().add(slot);
70       }
71       frame.setSize(frame.getPreferredSize());
72       frame.show();
73     }
74   }
75   
76   
77