Java tutorial
/* * 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 agendapoo.View; import agendapoo.Control.ControlUsuario; import agendapoo.Exceptions.InvalidTimeRangeException; import agendapoo.Exceptions.TimeInterferenceException; import agendapoo.Model.TipoAtividade; import java.io.IOException; import java.sql.SQLException; import java.time.format.DateTimeParseException; import java.util.ArrayList; import java.util.List; import javax.swing.JOptionPane; import org.apache.commons.mail.EmailException; import agendapoo.Model.Usuario; import java.time.LocalDate; import java.time.LocalTime; import java.time.Period; import javax.swing.ImageIcon; /** * * @author kieckegard */ public class FrmCadastroAtividade extends javax.swing.JFrame { private final List<String> convidados; private final Usuario loggedUser; private final FrmUsuario usuarioMain; public FrmCadastroAtividade(Usuario loggedUser, FrmUsuario janela) { initComponents(); setFrameIcon(); this.setResizable(false); convidados = new ArrayList<>(); this.loggedUser = loggedUser; usuarioMain = janela; } private void setFrameIcon() { ImageIcon icon = new ImageIcon("src//agendapoo//imgs//file_add.png"); this.setIconImage(icon.getImage()); } private void cleanFields() { textData.setText(""); textDescricao.setText(""); textLocal.setText(""); textTimeFinal.setText(""); textTimeFinal.setText(""); comboTipoAtividade.setSelectedIndex(0); } private boolean isOldTime(LocalDate data, LocalTime end) { Period p = Period.between(data, LocalDate.now()); if (p.getDays() > 0) return true; else if (p.getDays() == 0) { return end.isBefore(LocalTime.now()); } return false; } private boolean isThereEmptyFields() { return textDescricao.getText().equals("") || textLocal.getText().equals("") || textTimeInicio.getText().equals("") || textTimeFinal.getText().equals(""); } private void cadastraAtividade(String descricao, String local, String data, String horaInicio, String horaFinal, String tipoAtividade) throws DateTimeParseException, SQLException, IOException, EmailException, InvalidTimeRangeException, TimeInterferenceException, ClassNotFoundException { TipoAtividade tipo = TipoAtividade.valueOf(tipoAtividade); ControlUsuario control = new ControlUsuario(); control.cadastraAtividade(descricao, local, data, horaInicio, horaFinal, convidados, tipo, loggedUser); } private void cadastrarAtividade(Usuario loggedUser) { ControlUsuario control = new ControlUsuario(); try { cadastraAtividade(textDescricao.getText(), textLocal.getText(), textData.getText(), textTimeInicio.getText(), textTimeFinal.getText(), comboTipoAtividade.getSelectedItem().toString()); JOptionPane.showMessageDialog(null, "Atividade cadastrada com sucesso! :)"); usuarioMain.loadAtividades(); this.dispose(); } catch (SQLException | IOException | InvalidTimeRangeException | TimeInterferenceException | ClassNotFoundException ex) { JOptionPane.showMessageDialog(this, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); } catch (DateTimeParseException dtpe) { JOptionPane.showMessageDialog(this, "Por favor, insira uma hora vlida!", "Hora invlida.", JOptionPane.ERROR_MESSAGE); } catch (EmailException ex) { JOptionPane.showMessageDialog(this, "No foi possvel enviar e-mail para os convidados cadastrados," + "por favor, verifique sua conexo com a internet e tente novamente.", "Erro ao enviar e-mail", JOptionPane.ERROR_MESSAGE); } } //All generated Swing code @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jPanelMain = new javax.swing.JPanel(); jPanel1 = new javax.swing.JPanel(); jLabel2 = new javax.swing.JLabel(); jLabel1 = new javax.swing.JLabel(); jPanel2 = new javax.swing.JPanel(); jLabel3 = new javax.swing.JLabel(); textDescricao = new javax.swing.JTextField(); jLabel4 = new javax.swing.JLabel(); textLocal = new javax.swing.JTextField(); jLabel5 = new javax.swing.JLabel(); jLabel6 = new javax.swing.JLabel(); textTimeInicio = new javax.swing.JFormattedTextField(); jLabel7 = new javax.swing.JLabel(); jLabel8 = new javax.swing.JLabel(); jLabel9 = new javax.swing.JLabel(); textTimeFinal = new javax.swing.JFormattedTextField(); comboTipoAtividade = new javax.swing.JComboBox(); jLabel10 = new javax.swing.JLabel(); btnAddConvidados = new javax.swing.JButton(); jLabel12 = new javax.swing.JLabel(); textData = new datechooser.beans.DateChooserCombo(); btnCadastrar = new javax.swing.JButton(); btnCancel = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setTitle("Nova atividade"); setResizable(false); jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); jLabel2.setFont(new java.awt.Font("Yu Gothic Light", 0, 36)); // NOI18N jLabel2.setText("Cadastro de"); jLabel1.setFont(new java.awt.Font("Yu Gothic Light", 0, 36)); // NOI18N jLabel1.setForeground(new java.awt.Color(165, 195, 220)); jLabel1.setText("Atividades"); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addGap(42, 42, 42).addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jLabel1) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2).addComponent(jLabel1)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Dados", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Yu Gothic Light", 0, 14))); // NOI18N jLabel3.setText("Descrio:"); textDescricao.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { textDescricaoActionPerformed(evt); } }); jLabel4.setText("Local:"); jLabel5.setText("Data:"); jLabel6.setText("Hora:"); try { textTimeInicio.setFormatterFactory( new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##:##"))); } catch (java.text.ParseException ex) { ex.printStackTrace(); } jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/agendapoo/imgs/file_add (1).png"))); // NOI18N jLabel8.setText("Tipo:"); jLabel9.setText("At:"); try { textTimeFinal.setFormatterFactory( new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##:##"))); } catch (java.text.ParseException ex) { ex.printStackTrace(); } comboTipoAtividade.setModel( new javax.swing.DefaultComboBoxModel(new String[] { "PESSOAL", "PROFISSIONAL", "ACADEMICO" })); comboTipoAtividade.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { comboTipoAtividadeActionPerformed(evt); } }); jLabel10.setText("Convidados:"); btnAddConvidados .setIcon(new javax.swing.ImageIcon(getClass().getResource("/agendapoo/imgs/friend_icon.png"))); // NOI18N btnAddConvidados.setText("Adicionar Convidados?"); btnAddConvidados.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnAddConvidadosActionPerformed(evt); } }); jLabel12.setIcon(new javax.swing.ImageIcon(getClass().getResource("/agendapoo/imgs/clock.png"))); // NOI18N textData.setFormat(2); textData.setFieldFont(new java.awt.Font("Tahoma", java.awt.Font.PLAIN, 12)); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addGap(21, 21, 21).addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel10) .addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(btnAddConvidados, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(comboTipoAtividade, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jLabel8)).addGap(0, 0, Short.MAX_VALUE)) .addGroup(jPanel2Layout.createSequentialGroup().addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(textLocal, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(textDescricao, javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addComponent(jLabel6) .addGap(44, 44, 44).addComponent(jLabel9).addGap(99, 99, 99))) .addGroup(jPanel2Layout.createSequentialGroup() .addComponent(textTimeInicio, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(14, 14, 14) .addComponent(textTimeFinal, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(jLabel12)) .addComponent(textData, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 61, Short.MAX_VALUE) .addComponent(jLabel7).addGap(27, 27, 27))))); jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addContainerGap() .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(textDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addComponent(textLocal, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(textData, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addGap(11, 11, 11) .addGroup(jPanel2Layout .createParallelGroup( javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout .createParallelGroup( javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(textTimeInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(textTimeFinal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(jPanel2Layout.createSequentialGroup().addGap(24, 24, 24) .addComponent(jLabel12)))) .addComponent(jLabel7)) .addGap(0, 0, 0) .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(comboTipoAtividade, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnAddConvidados, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 30, Short.MAX_VALUE))); btnCadastrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/agendapoo/imgs/002_10.png"))); // NOI18N btnCadastrar.setText("Cadastrar Atividade"); btnCadastrar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnCadastrarActionPerformed(evt); } }); btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/agendapoo/imgs/delete.png"))); // NOI18N btnCancel.setText("Cancelar"); btnCancel.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnCancelActionPerformed(evt); } }); javax.swing.GroupLayout jPanelMainLayout = new javax.swing.GroupLayout(jPanelMain); jPanelMain.setLayout(jPanelMainLayout); jPanelMainLayout.setHorizontalGroup(jPanelMainLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanelMainLayout.createSequentialGroup().addContainerGap() .addGroup(jPanelMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanelMainLayout.createSequentialGroup().addGroup(jPanelMainLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addGroup(jPanelMainLayout.createSequentialGroup().addGap(8, 8, 8) .addComponent(btnCadastrar) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(0, 1, Short.MAX_VALUE)) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap())); jPanelMainLayout.setVerticalGroup(jPanelMainLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanelMainLayout.createSequentialGroup().addContainerGap() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE) .addGroup(jPanelMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnCadastrar, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap())); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanelMain, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent( jPanelMain, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)); pack(); }// </editor-fold>//GEN-END:initComponents private void textDescricaoActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_textDescricaoActionPerformed {//GEN-HEADEREND:event_textDescricaoActionPerformed // TODO add your handling code here: }//GEN-LAST:event_textDescricaoActionPerformed private void comboTipoAtividadeActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_comboTipoAtividadeActionPerformed {//GEN-HEADEREND:event_comboTipoAtividadeActionPerformed // TODO add your handling code here: }//GEN-LAST:event_comboTipoAtividadeActionPerformed private void btnAddConvidadosActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnAddConvidadosActionPerformed {//GEN-HEADEREND:event_btnAddConvidadosActionPerformed FrmConvidados fc = new FrmConvidados(convidados); fc.setVisible(true); }//GEN-LAST:event_btnAddConvidadosActionPerformed private void btnCancelActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnCancelActionPerformed {//GEN-HEADEREND:event_btnCancelActionPerformed this.dispose(); }//GEN-LAST:event_btnCancelActionPerformed private void btnCadastrarActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnCadastrarActionPerformed {//GEN-HEADEREND:event_btnCadastrarActionPerformed if (!isThereEmptyFields()) { if (convidados.isEmpty()) { int i = JOptionPane.showConfirmDialog(this, "Parece que voc no adicionou nenhum convidado...\nVoc deseja prosseguir?"); switch (i) { case JOptionPane.OK_OPTION: cadastrarAtividade(loggedUser); break; } } else cadastrarAtividade(loggedUser); } else JOptionPane.showMessageDialog(this, "Por favor, preencha todos os campos!", "Campos vzios", JOptionPane.ERROR_MESSAGE); }//GEN-LAST:event_btnCadastrarActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnAddConvidados; private javax.swing.JButton btnCadastrar; private javax.swing.JButton btnCancel; private javax.swing.JComboBox comboTipoAtividade; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel10; private javax.swing.JLabel jLabel12; 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.JPanel jPanel2; private javax.swing.JPanel jPanelMain; private datechooser.beans.DateChooserCombo textData; private javax.swing.JTextField textDescricao; private javax.swing.JTextField textLocal; private javax.swing.JFormattedTextField textTimeFinal; private javax.swing.JFormattedTextField textTimeInicio; // End of variables declaration//GEN-END:variables }