Untitled

 avatar
unknown
plain_text
3 years ago
6.4 kB
5
Indexable
package cl.ufro.gui;

import javax.swing.JPanel;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;

import cl.ufro.bd.Usuario;
import cl.ufro.service.PublicacionService;
import cl.ufro.solicitud.SolicitudPublicacion;
import utils.Foto;
import utils.Imagen;
import utils.ManagerSession;
import utils.PanelImagen;
import utils.Serializar;

import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.BoundedRangeModel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.awt.Color;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.LineBorder;
import javax.swing.JTextArea;
import java.awt.Font;

public class BuscoPanel extends Panel {
	private JTextField txtAsunto = new JTextField("");
	private JTextField txtFecha = new JTextField("");
	private PanelImagen panelImagen;

	Usuario usuario = new Usuario();
	private String[] categorias = {"Electrónicos", "Ropa", "Accesorios", "Documentos"};
	private String[] rangoHorario = {"00:00-02:00","02:00-04:00","04:00-06:00",
							 "06:00-08:00","08:00-10:00","10:00-12:00",
							 "12:00-14:00","14:00-16:00","16:00-18:00",
							 "18:00-20:00","20:00-22:00","22:00-00:00"};
	
	private JComboBox cbxCategoria = new JComboBox(categorias);
	private JComboBox cbxHora = new JComboBox(rangoHorario);
	private JTextArea txtDescripcion = new JTextArea("");
	
	public BuscoPanel(Principal principal) {
		super(principal);
		setBackground(Color.BLUE);
		build();
	}

	@Override
	public void init() {
		setVisible(true);
		usuario = (Usuario) ManagerSession.findObject("usuario");
		
		File defaultObject = new File("GenericObject.png");
		BufferedImage img =Imagen.readColorImage(defaultObject.getAbsolutePath());
		img = Imagen.scale(img, 200, 200);
		byte[] imgBytes = Serializar.BufferedImageToBytes(img);
		System.out.println(imgBytes.length);
		BufferedImage aux = Serializar.BytesToBufferedImage(imgBytes);
		panelImagen.setImage(aux);	
		panelImagen.setNombre(defaultObject.getName());
		txtAsunto.setText("");
		txtFecha.setText("");
		txtDescripcion.setText("");
	}
	
	@Override
	public void build() {
		
		panelImagen = new PanelImagen();
		panelImagen.setBorder(new LineBorder(new Color(0, 0, 0)));
		panelImagen.setSize(200, 200);
		panelImagen.setLocation(100, 69);
		add(panelImagen);
		
		JLabel lblCategora = new JLabel("Categoría");
		lblCategora.setForeground(Color.WHITE);
		lblCategora.setBounds(35, 362, 112, 17);
		add(lblCategora);
		
		JLabel lblAsunto = new JLabel("Asunto");
		lblAsunto.setForeground(Color.WHITE);
		lblAsunto.setBounds(35, 331, 112, 17);
		add(lblAsunto);
		
		JLabel lblFechaEnQue = new JLabel("<html>Fecha en que se perdió el objeto</html>");
		lblFechaEnQue.setForeground(Color.WHITE);
		lblFechaEnQue.setBounds(35, 401, 123, 46);
		add(lblFechaEnQue);
		
		JLabel lblRangoHorarioEn = new JLabel("<html>Rango horario en que se perdió</html>");
		lblRangoHorarioEn.setForeground(Color.WHITE);
		lblRangoHorarioEn.setBounds(35, 445, 123, 51);
		add(lblRangoHorarioEn);
		
		txtAsunto = new JTextField();
		txtAsunto.setBounds(176, 329, 173, 21);
		add(txtAsunto);
		txtAsunto.setColumns(10);
		
		JButton btnPublicar = new JButton("Publicar");
		btnPublicar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				PublicacionService service = new PublicacionService();
				SolicitudPublicacion solicitud = new SolicitudPublicacion();
				solicitud.setCorreoCreador(usuario.getCorreo());
				solicitud.setDescripcion(txtDescripcion.getText());
				solicitud.setIdCriterio(1);
				solicitud.setIdCategoria(cbxCategoria.getSelectedIndex());
				solicitud.setIdHora(cbxHora.getSelectedIndex());
				solicitud.setIdPublicacion(0);
				solicitud.setPublicacionActiva(true);
				solicitud.setRutaImagen("");
		
				BufferedImage aux = panelImagen.getImage();
				Foto foto = new Foto();
				foto.data = Serializar.BufferedImageToBytes(aux);
				foto.largo = foto.data.length;
				foto.nombre = panelImagen.getNombre();
				solicitud.imagen = foto;
				service.crearPublicacion(solicitud);
				principal.verHomePanel();
				
			}
		});
		
		JButton btnCargarImagen = new JButton("Cargar Imagen");
		
		btnCargarImagen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser file = new JFileChooser("./");
				file.showOpenDialog(null);
				File arch = file.getSelectedFile();
				if(arch!=null){
					BufferedImage img =Imagen.readColorImage(arch.getAbsolutePath());
					img = Imagen.scale(img, 200, 200);
					byte[] imgBytes = Serializar.BufferedImageToBytes(img);
					System.out.println(imgBytes.length);
					BufferedImage aux = Serializar.BytesToBufferedImage(imgBytes);
					panelImagen.setImage(aux);	
					panelImagen.setNombre(arch.getName());
				}
			}
		});
		btnCargarImagen.setBounds(100, 281, 200, 25);
		add(btnCargarImagen);
		btnPublicar.setBounds(248, 610, 101, 27);
		add(btnPublicar);
		
		JButton btnVolver = new JButton("Volver");
		btnVolver.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				principal.verHomePanel();
			}
		});
		btnVolver.setBounds(35, 610, 101, 27);
		add(btnVolver);
		
		txtFecha = new JTextField();
		txtFecha.setBounds(176, 417, 173, 19);
		add(txtFecha);
		txtFecha.setColumns(10);
		
		JLabel lblDescripcin = new JLabel("Descripción");
		lblDescripcin.setForeground(Color.WHITE);
		lblDescripcin.setBounds(35, 537, 91, 15);
		add(lblDescripcin);

		cbxCategoria.setBounds(176, 357, 173, 26);
		add(cbxCategoria);
				
		cbxHora.setBounds(176, 459, 173, 26);
		add(cbxHora);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane.setBounds(176, 497, 173, 89);
		add(scrollPane);
		scrollPane.setViewportView(txtDescripcion);
		txtDescripcion.setWrapStyleWord(true);
		txtDescripcion.setLineWrap(true);
		
		JLabel lblCrearPublicacin = new JLabel("Crear publicación");
		lblCrearPublicacin.setFont(new Font("Dialog", Font.BOLD, 18));
		lblCrearPublicacin.setForeground(new Color(255, 255, 255));
		lblCrearPublicacin.setBounds(35, 12, 200, 46);
		add(lblCrearPublicacin);
	}
}
Editor is loading...