library/src/library/AlbumFieldPattern.java

294 lines
13 KiB
Java

/*
* 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 library;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
*
* @author Agnieszka
*/
public class AlbumFieldPattern extends javax.swing.JFrame {
public boolean add = false, delete = false, edit = false, search = false, update = false;
private Database d = new Database();
/**
* Creates new form bookFieldPattern
*/
public AlbumFieldPattern() {
initComponents();
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public AlbumFieldPattern(boolean required, Album... a) {
initComponents();
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Validation verify;
if (required) {
verify = new Validation(true);
} else {
verify = new Validation();
}
albumTitle.setInputVerifier(verify);
musican.setInputVerifier(verify);
year.setInputVerifier(verify);
try {
if (a[0].getId() >= 0) {
fillField(a[0]);
update = true;
}
} catch (ArrayIndexOutOfBoundsException e) {
//when only one parameter is defined
}
}
public void fillField(Album a) {
idValue.setText(Integer.toString(a.getId()));
albumTitle.setText(a.getName());
musican.setText(a.getMusican());
year.setText(Integer.toString(a.getYear()));
Category.setSelectedItem(a.getCategory());
}
public void search(Album a, String... mode) {
try {
List<Album> albums = new LinkedList<>();
albums = d.selectAlbums(a);
Object[][] data = d.convertToTable(albums);
String[] columnNames = {
"ID",
"Tytuł",
"Muzyk/Zespół",
"Rok",
"Kategoria"};
ListResult table = new ListResult(data, columnNames, "album");
table.setVisible(true);
if (mode[0].equals("edit")) {
table.setEnabledButton("edit");
}
if (mode[0].equals("delete")) {
table.setEnabledButton("delete");
}
} catch (IndexOutOfBoundsException e) {
//nothing to do here
}
}
/**
* 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() {
Title = new javax.swing.JLabel();
albumTitle = new javax.swing.JTextField();
lmusican = new javax.swing.JLabel();
musican = new javax.swing.JTextField();
lyear = new javax.swing.JLabel();
year = new javax.swing.JTextField();
lcategory = new javax.swing.JLabel();
Category = new javax.swing.JComboBox<>();
mainlabel = new javax.swing.JLabel();
SaveChanges = new javax.swing.JButton();
idValue = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
Title.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
Title.setText("Tytuł:");
albumTitle.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
lmusican.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
lmusican.setText("Twórca:");
musican.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
lyear.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
lyear.setText("Rok:");
year.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
year.setName("year"); // NOI18N
lcategory.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
lcategory.setText("Gatunek:");
Category.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { " ", "Jazz", "Rap", "Rock", "Muzyka Klasyczna", "Reggae", "Pop", "Elektroniczna" }));
mainlabel.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
mainlabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
mainlabel.setText("Wypełnij formualrz:");
SaveChanges.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
SaveChanges.setText("OK");
SaveChanges.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SaveChangesActionPerformed(evt);
}
});
idValue.setText(".");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Title)
.addComponent(lmusican)
.addComponent(idValue, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(49, 49, 49)
.addComponent(mainlabel, javax.swing.GroupLayout.PREFERRED_SIZE, 289, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(lyear, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(musican, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(year, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(albumTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 362, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addComponent(lcategory, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(SaveChanges, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Category, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addContainerGap(26, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(mainlabel)
.addGap(18, 18, 18))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(idValue)
.addGap(7, 7, 7)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Title)
.addComponent(albumTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lmusican)
.addComponent(musican, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lyear)
.addComponent(year, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lcategory)
.addComponent(Category, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(SaveChanges)
.addContainerGap(14, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
public void ChangeOkButtonText(String s) {
this.SaveChanges.setText(s);
}
private void SaveChangesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveChangesActionPerformed
if (this.add) {
try {
Album a = new Album(0, albumTitle.getText(), musican.getText(), year.getText(), Category.getSelectedItem().toString());
a.insertToDB();
this.dispose();
} catch (SQLException e) {
e.getMessage();
}
} else if (this.edit) {
Album a = new Album(0, albumTitle.getText(), musican.getText(), year.getText(), Category.getSelectedItem().toString());
this.search(a, "edit");
} else if (this.delete) {
Album a = new Album(0, albumTitle.getText(), musican.getText(), year.getText(), Category.getSelectedItem().toString());
this.search(a, "delete");
} else if (this.search) {
Album a = new Album(0, albumTitle.getText(), musican.getText(), year.getText(), Category.getSelectedItem().toString());
this.search(a);
} else if (this.update) {
Album a = new Album(Integer.parseInt(idValue.getText()), albumTitle.getText(), musican.getText(), year.getText(), Category.getSelectedItem().toString());
try {
a.editInDB();
} catch (SQLException e) {
e.printStackTrace();
}
}
d.closeConnection();
dispose();
}//GEN-LAST:event_SaveChangesActionPerformed
/**
* @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(AlbumFieldPattern.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(AlbumFieldPattern.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(AlbumFieldPattern.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(AlbumFieldPattern.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new AlbumFieldPattern().setVisible(false);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JComboBox<String> Category;
private javax.swing.JButton SaveChanges;
private javax.swing.JLabel Title;
private javax.swing.JTextField albumTitle;
private javax.swing.JLabel idValue;
private javax.swing.JLabel lcategory;
private javax.swing.JLabel lmusican;
private javax.swing.JLabel lyear;
private javax.swing.JLabel mainlabel;
private javax.swing.JTextField musican;
private javax.swing.JTextField year;
// End of variables declaration//GEN-END:variables
}