FileDocCategorySizeDatePackage
PolymorphicJList.javaAPI DocExample13968Mon Jan 09 11:01:58 GMT 2006None

PolymorphicJList

public class PolymorphicJList extends JList

Fields Summary
static Color
listForeground
static Color
listBackground
static Color
listSelectionForeground
static Color
listSelectionBackground
ImageIcon
fileIcon
ImageIcon
textFileIcon
ImageIcon
directoryIcon
ImageIcon
imageFileIcon
ImageIcon
pngFileIcon
ImageIcon
gifFileIcon
ImageIcon
jpegFileIcon
JComponent
fileCellPrototype
JComponent
textCellPrototype
JComponent
imageCellPrototype
JComponent
directoryCellPrototype
JLabel
fileNameLabel
JLabel
textNameLabel
JLabel
directoryNameLabel
JLabel
imageNameLabel
JLabel
fileSizeLabel
JLabel
textSizeLabel
JLabel
textWordCountLabel
JLabel
directoryCountLabel
JLabel
imageSizeLabel
JLabel
imageIconLabel
Constructors Summary
public PolymorphicJList(File dir)

        super();
        buildPrototypeCells();
        setCellRenderer (new PolyRenderer());
        setModel (new DefaultListModel());
        if (! dir.isDirectory())
            dir = new File (dir.getParent());
        buildModelFromDir (dir);
    
Methods Summary
private voidaddWithGridBag(java.awt.Component comp, java.awt.Container cont, int x, int y, int width, int height, int anchor, int fill, int weightx, int weighty)

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = width;
        gbc.gridheight = height;
        gbc.anchor = anchor;
        gbc.fill = fill;
        gbc.weightx = weightx;
        gbc.weighty = weighty;
        cont.add (comp, gbc);
    
protected voidbuildIcons()

        String SEP = System.getProperty ("file.separator");
        fileIcon = new ImageIcon ("images" + SEP + "generic.gif");
        textFileIcon = new ImageIcon ("images" + SEP + "text.gif");
        directoryIcon = new ImageIcon ("images" + SEP + "folder.gif");
        imageFileIcon = new ImageIcon ("images" + SEP + "image.gif");
        pngFileIcon = new ImageIcon ("images" + SEP + "png.gif");
        gifFileIcon = new ImageIcon ("images" + SEP + "gif.gif");
        jpegFileIcon = new ImageIcon ("images" + SEP + "jpeg.gif");
    
protected voidbuildModelFromDir(java.io.File dir)

        File[] files = dir.listFiles();
        DefaultListModel mod = (DefaultListModel) getModel();
        for (int i=0; i<files.length; i++) {
            if (isTextFile (files[i]))
                mod.addElement (new TextFileItem (files[i]));
            else if (isImageFile (files [i]))
                mod.addElement (new ImageFileItem (files[i]));
            else if (files[i].isDirectory())
                mod.addElement (new DirectoryItem (files[i]));
            else
                mod.addElement (new FileItem (files[i]));
        }
    
protected voidbuildPrototypeCells()

        buildIcons();
        fileCellPrototype = new JPanel();
        fileCellPrototype.setLayout (new GridBagLayout());
        addWithGridBag (new JLabel(fileIcon), fileCellPrototype,
                        0, 0, 1, 2,
                        GridBagConstraints.WEST, GridBagConstraints.BOTH, 0, 0);
        fileNameLabel = new JLabel();
        Font defaultLabelFont = fileNameLabel.getFont();
        Font nameFont = defaultLabelFont.deriveFont (Font.BOLD,
                                                     defaultLabelFont.getSize()+2);
        fileNameLabel.setFont (nameFont);
        addWithGridBag (fileNameLabel, fileCellPrototype,
                        1, 0, 1, 1,
                        GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, 1, 0);
        fileSizeLabel = new JLabel();
        addWithGridBag (fileSizeLabel, fileCellPrototype,
                        1, 1, 1, 1,
                        GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL, 1, 0);
        opacify (fileCellPrototype);
        // text file
        textCellPrototype = new JPanel();
        textCellPrototype.setLayout (new GridBagLayout());
        addWithGridBag (new JLabel(textFileIcon), textCellPrototype,
                        0, 0, 1, 2,
                        GridBagConstraints.WEST, GridBagConstraints.BOTH, 0, 0);
        textNameLabel = new JLabel();
        textNameLabel.setFont (nameFont);
        addWithGridBag (textNameLabel, textCellPrototype,
                        1, 0, 2, 1,
                        GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, 1, 0);
        textSizeLabel = new JLabel();
        textWordCountLabel = new JLabel();
        addWithGridBag (textSizeLabel, textCellPrototype,
                        1, 1, 1, 1,
                        GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, 0, 0);
        addWithGridBag (textWordCountLabel, textCellPrototype,
                        2, 1, 1, 1,
                        GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL, 1, 0);

        opacify (textCellPrototype);
        // directory
        directoryCellPrototype = new JPanel();
        directoryCellPrototype.setLayout (new GridBagLayout());
        addWithGridBag (new JLabel(directoryIcon), directoryCellPrototype,
                        0, 0, 1, 2,
                        GridBagConstraints.WEST, GridBagConstraints.BOTH, 0, 0);
        directoryNameLabel = new JLabel();
        directoryNameLabel.setFont (nameFont);
        addWithGridBag (directoryNameLabel, directoryCellPrototype,
                        1, 0, 1, 1,
                        GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, 1, 0);
        directoryCountLabel = new JLabel();
        addWithGridBag (directoryCountLabel, directoryCellPrototype,
                        1, 1, 1, 1,
                        GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL, 1, 0);
        opacify (directoryCellPrototype);
        // image 
        imageCellPrototype = new JPanel();
        imageCellPrototype.setLayout (new GridBagLayout());
        addWithGridBag (new JLabel(imageFileIcon), imageCellPrototype,
                        0, 0, 1, 2,
                        GridBagConstraints.WEST, GridBagConstraints.BOTH, 0, 0);
        imageNameLabel = new JLabel();
        imageNameLabel.setFont (nameFont);
        addWithGridBag (imageNameLabel, imageCellPrototype,
                        1, 0, 1, 1,
                        GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, 1, 0);
        imageSizeLabel = new JLabel();
        addWithGridBag (imageSizeLabel, imageCellPrototype,
                        1, 1, 1, 1,
                        GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL, 1, 0);
        imageIconLabel = new JLabel();
        addWithGridBag (imageIconLabel, imageCellPrototype,
                        2, 0, 1, 2,
                        GridBagConstraints.EAST, GridBagConstraints.VERTICAL, 0, 0);
        opacify (imageCellPrototype);
    
protected booleanisImageFile(java.io.File f)

        if (f.isDirectory())
            return false;
        String name = f.getName();
        return name.endsWith (".gif") || name.endsWith (".GIF") ||
            name.endsWith (".jpg") || name.endsWith (".JPG") ||
            name.endsWith (".jpeg") || name.endsWith (".JPEG") ||
            name.endsWith (".bmp") || name.endsWith (".BMP") ||
            name.endsWith (".png") || name.endsWith (".PNG");
    
protected booleanisTextFile(java.io.File f)

        if (f.isDirectory())
            return false;
        String name = f.getName();
        return name.endsWith (".txt") || name.endsWith (".html") ||
            name.endsWith (".xml") || name.endsWith (".xhtml") ||
            name.endsWith (".java") || name.endsWith (".c") ||
            name.endsWith (".cpp") || name.endsWith (".c++") ||
            name.endsWith (".m") || name.endsWith (".h");
    
public static voidmain(java.lang.String[] args)

        File dir = new File (".");
        if (args.length > 0)
            dir = new File (args[0]);
        JList list = new PolymorphicJList (dir);
        JScrollPane pain =
            new JScrollPane (list,
                             ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                             ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        JFrame frame = new JFrame ("PolymorphicJList");
        frame.getContentPane().add (pain);
        frame.pack();
        frame.setVisible(true);
    
private voidopacify(java.awt.Container prototype)

        Component[] comps = prototype.getComponents();
        for (int i=0; i<comps.length; i++) {
            if (comps[i] instanceof JComponent)
                ((JComponent)comps[i]).setOpaque(true);
        }