UI.ImageList.java Source code

Java tutorial

Introduction

Here is the source code for UI.ImageList.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package UI;

import Database.MediaBase;
import Holders.InfoHolder;
import UI.Library.StretchIcon;
import Util.Print;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
import java.awt.Component;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;

/**
 *
 * @author sasuke
 */
public class ImageList extends javax.swing.JFrame {

    /**
     * Creates new form ImageList
     */

    ArrayList<InfoHolder> holder;
    String columns[];

    public ImageList() {
        initComponents();
    }

    public ImageList(String folderName) {

        MediaBase base = new MediaBase();

        try {
            holder = base.queryGetImageList(folderName);
            base.close();
        } catch (SQLException ex) {
            Logger.getLogger(ImageList.class.getName()).log(Level.SEVERE, null, ex);
        }

        columns = new String[] { "", "File", "Folder Name", "Path" };

        initComponents();

        jTable1.setRowHeight(50);

        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {

            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                int row = jTable1.rowAtPoint(evt.getPoint());
                int col = jTable1.columnAtPoint(evt.getPoint());
                if (row >= 0 && col >= 0) {
                    try {
                        Desktop.getDesktop().open(new File(holder.get(row).Path));
                    } catch (IOException ex) {
                        Logger.getLogger(ImageList.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        });
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable() {
            public TableCellRenderer getCellRenderer(int row, int column) {
                return new PlusMinusCellRenderer();
            }
        };

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jTable1.setModel(new ImagesTableModel(holder, columns));
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 699, Short.MAX_VALUE));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 449,
                Short.MAX_VALUE));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ImageList.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ImageList.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ImageList.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ImageList.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ImageList().setVisible(true);
            }
        });
    }

    public class ImagesTableModel extends AbstractTableModel {

        private ArrayList<InfoHolder> holder;
        private String columns[];

        public ImagesTableModel(ArrayList<InfoHolder> holder, String columns[]) {
            this.holder = holder;
            this.columns = columns;
        }

        public int getColumnCount() {
            return columns.length;
        }

        public int getRowCount() {
            return holder.size();
        }

        public String getColumnName(int columnIndex) {
            return columns[columnIndex];
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            InfoHolder ah = holder.get(rowIndex);

            switch (columnIndex) {
            case 0:
                return ah.Path;
            case 1:
                return ah.File_Name;
            case 2:
                return ah.Folder_Name;
            case 3:
                return ah.Path;
            }

            return null;
        }
    }

    class PlusMinusCellRenderer extends JPanel implements TableCellRenderer {
        public Component getTableCellRendererComponent(final JTable table, Object value, boolean isSelected,
                boolean hasFocus, int row, int column) {

            switch (column) {
            case 0:
                File file = new File(value.toString());

                if (file.exists()) {
                    JLabel label = new JLabel();
                    StretchIcon icon = new StretchIcon(file.getPath());
                    label.setIcon(icon);
                    label.setPreferredSize(new Dimension(40, 40));

                    this.add(label);
                }
                break;
            default:

                JLabel label;
                if (value != null)
                    label = new JLabel(value.toString());
                else
                    label = new JLabel();

                this.add(label);
                break;
            }

            return this;
        }
    }

    private void print() {
        Document document = new Document(PageSize.A4.rotate());
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("jTable.pdf"));

            document.open();
            PdfContentByte cb = writer.getDirectContent();

            cb.saveState();
            Graphics2D g2 = cb.createGraphicsShapes(500, 500);

            Shape oldClip = g2.getClip();
            g2.clipRect(0, 0, 500, 500);

            jTable1.print(g2);
            g2.setClip(oldClip);

            g2.dispose();
            cb.restoreState();
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        document.close();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}