Vistas.PanelFoto.java Source code

Java tutorial

Introduction

Here is the source code for Vistas.PanelFoto.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 Vistas;

import com.mongodb.BasicDBObject;
import java.awt.Color;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author inftel18
 */
public class PanelFoto extends javax.swing.JPanel implements VistaFoto {

    /**
     * Creates new form JPanelFotos
     *
     * @throws java.io.IOException
     */
    public PanelFoto() throws IOException {
        initComponents();

        //jPanel1.setOpaque(false);
        jPanel1.setBackground(new Color(255, 255, 255, 215));
    }

    /**
     * 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() {

        Foto = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        mapa = new javax.swing.JButton();
        Nombre = new javax.swing.JLabel();
        Marca = new javax.swing.JLabel();
        Modelo = new javax.swing.JLabel();
        Flash = new javax.swing.JLabel();
        Photoshop = new javax.swing.JLabel();
        Fecha = new javax.swing.JLabel();
        pos = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jButton4 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();

        setPreferredSize(new java.awt.Dimension(580, 380));
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        Foto.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagen/imageNotFound-2.jpg"))); // NOI18N
        Foto.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(255, 255, 255), 1, true));
        Foto.setPreferredSize(new java.awt.Dimension(250, 200));
        add(Foto, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 50, -1, -1));

        jLabel2.setText("Nombre :");
        add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(325, 70, -1, -1));

        jLabel3.setText("Marca :");
        add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(325, 100, -1, -1));

        jLabel4.setText("Modelo :");
        add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(325, 130, -1, -1));

        jLabel5.setText("Flash :");
        add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(325, 160, -1, -1));

        jLabel6.setText("Photoshop :");
        add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(325, 190, -1, -1));

        jLabel7.setText("Fecha :");
        add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(325, 220, -1, -1));

        jLabel8.setFont(new java.awt.Font("Lucida Grande", 1, 18)); // NOI18N
        jLabel8.setForeground(new java.awt.Color(255, 255, 255));
        jLabel8.setText("Foto");
        add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 340, -1, -1));

        mapa.setText("Ver Mapa");
        add(mapa, new org.netbeans.lib.awtextra.AbsoluteConstraints(316, 260, 120, 50));

        Nombre.setText("-");
        Nombre.setMaximumSize(new java.awt.Dimension(50, 16));
        Nombre.setMinimumSize(new java.awt.Dimension(50, 16));
        Nombre.setPreferredSize(new java.awt.Dimension(150, 16));
        add(Nombre, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 70, -1, -1));

        Marca.setText("-");
        Marca.setPreferredSize(new java.awt.Dimension(150, 16));
        add(Marca, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 100, -1, -1));

        Modelo.setText("-");
        Modelo.setPreferredSize(new java.awt.Dimension(150, 16));
        add(Modelo, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 130, -1, -1));

        Flash.setText("-");
        Flash.setMaximumSize(new java.awt.Dimension(150, 16));
        Flash.setMinimumSize(new java.awt.Dimension(150, 16));
        Flash.setPreferredSize(new java.awt.Dimension(150, 16));
        add(Flash, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 160, -1, -1));

        Photoshop.setText("-");
        Photoshop.setMaximumSize(new java.awt.Dimension(150, 16));
        Photoshop.setMinimumSize(new java.awt.Dimension(150, 16));
        Photoshop.setPreferredSize(new java.awt.Dimension(150, 16));
        add(Photoshop, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 190, -1, -1));

        Fecha.setText("-");
        Fecha.setMaximumSize(new java.awt.Dimension(150, 16));
        Fecha.setMinimumSize(new java.awt.Dimension(150, 16));
        Fecha.setPreferredSize(new java.awt.Dimension(150, 16));
        add(Fecha, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 220, -1, -1));

        pos.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        pos.setForeground(new java.awt.Color(255, 255, 255));
        pos.setText("- / -");
        add(pos, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 340, -1, -1));

        jPanel1.setBackground(new java.awt.Color(250, 250, 250));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jButton4.setText("Abrir Foto");
        jPanel1.add(jButton4, new org.netbeans.lib.awtextra.AbsoluteConstraints(157, 210, 120, 50));

        jButton2.setText(">");
        jPanel1.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 280, 59, 49));

        jButton1.setText("<");
        jPanel1.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 280, 56, 49));

        add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 50, 290, 340));

        jLabel9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagen/fondo.png"))); // NOI18N
        add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, -1));
    }// </editor-fold>//GEN-END:initComponents

    @Override
    public void rellenarCampos(BasicDBObject obj, int posi, int l) {
        if (obj.getString("nombre") == null) {
            Nombre.setText("-");
        } else {
            Nombre.setText(obj.getString("nombre"));
        }
        if (obj.getString("marca") == null) {
            Marca.setText("-");
        } else {
            Marca.setText(obj.getString("marca"));
        }
        if (obj.getString("modelo") == null) {
            Modelo.setText("-");
        } else {
            Modelo.setText(obj.getString("modelo"));
        }
        if (obj.getString("Flash") == null) {
            Flash.setText("-");
        } else if (obj.getBoolean("Flash")) {
            Flash.setText("Si");
        } else {
            Flash.setText("No");
        }
        if (obj.getString("Photoshop") == null) {
            Photoshop.setText("-");
        } else if (obj.getBoolean("Photoshop")) {
            Photoshop.setText("Si");
        } else {
            Photoshop.setText("No");
        }
        if (obj.getString("fecha") == null) {
            Fecha.setText("-");
        } else {
            DateFormat dateFormatHora = new SimpleDateFormat("HH:mm");
            DateFormat dateFormatFecha = new SimpleDateFormat("dd/MM/yyyy");
            Date fechaTime = obj.getDate("fecha");
            String fecha = dateFormatFecha.format(fechaTime);
            String hora = dateFormatHora.format(fechaTime);
            Fecha.setText(fecha + " - " + hora);
        }

        BufferedImage originalImage = null;
        if (obj.getString("ruta") == null) {
            Foto.setText("-");
        } else {
            try {
                File f = new File(obj.getString("ruta") + "\\" + obj.getString("nombre"));
                if (f.isFile()) {
                    originalImage = ImageIO.read(f);
                    Image scaledImage = originalImage.getScaledInstance(250, 200, Image.SCALE_SMOOTH);
                    ImageIcon icon = new ImageIcon(scaledImage);
                    Foto.setIcon(icon);
                } else {
                    Foto.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagen/imageNotFound-2.jpg")));
                }
            } catch (IOException ex) {
                Logger.getLogger(PanelFoto.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        pos.setText((posi + 1) + "/" + (l));

    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel Fecha;
    private javax.swing.JLabel Flash;
    private javax.swing.JLabel Foto;
    private javax.swing.JLabel Marca;
    private javax.swing.JLabel Modelo;
    private javax.swing.JLabel Nombre;
    private javax.swing.JLabel Photoshop;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton mapa;
    private javax.swing.JLabel pos;
    // End of variables declaration//GEN-END:variables

    @Override
    public void controlador(ActionListener ctr) {
        jButton1.addActionListener(ctr);
        jButton1.setActionCommand(BotonMenos);
        jButton2.addActionListener(ctr);
        jButton2.setActionCommand(BotonMas);
        mapa.addActionListener(ctr);
        mapa.setActionCommand(BotonMapa);
        jButton4.addActionListener(ctr);
        jButton4.setActionCommand(BotonFoto);
    }

    @Override
    public void mensajeError(String message) {
        JOptionPane.showMessageDialog(this, message, "Error", JOptionPane.ERROR_MESSAGE);
    }
}