package gui;
import hibernateSwingApi.component.TablePanel;
import hibernateSwingApi.hibernate.HibernateManager;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import pojos.Firma;
import service.AutoClosingMessageDialog;
import service.PSAUtil;
public class PSAFirmaEkrani extends JFrame {
private static final long serialVersionUID = 1L;
private static PSAFirmaEkrani instance;
private JPopupMenu pMenu;
private JMenuItem menuItem;
private JMenuItem menuItem2;
private JButton JButtonFirmaEkle;
private JButton JButtonFirmaBilgiGuncelle;
private JButton JButtonFirmaSil;
private TablePanel cTablePanelFirma;
private JLabel cLabelFirmaListesi;
private Firma firma;
public static PSAFirmaEkrani getInstance(int state) {
if (state == 0) {
instance = new PSAFirmaEkrani();
}
return instance;
}
public PSAFirmaEkrani() {
initialize();
}
private void initialize() {
setTitle("PSA MERKEZ FİRMA EKRANI");
setBounds(0, 0, 1250, 485);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setIconImage(PSAUtil.getImageIconViaRelativePath("picture/psaIcon.png")
.getImage());
pMenu = new JPopupMenu();
menuItem = new JMenuItem("Sil");
pMenu.add(menuItem);
menuItem2 = new JMenuItem("Güncelle");
pMenu.add(menuItem2);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[] { 30, 951, 300, 15, 0 };
gridBagLayout.rowHeights = new int[] { 0, 50, 130, 130, 130, 15, 0 };
gridBagLayout.columnWeights = new double[] { 0.0, 1.0, 1.0, 0.0,
Double.MIN_VALUE };
gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 1.0, 1.0, 1.0, 0.0,
Double.MIN_VALUE };
getContentPane().setLayout(gridBagLayout);
cLabelFirmaListesi = new JLabel();
cLabelFirmaListesi.setText("F\u0130RMA L\u0130STES\u0130");
cLabelFirmaListesi.setFont(new Font("Tahoma", Font.BOLD, 16));
GridBagConstraints gbc_cLabelFirmaListesi = new GridBagConstraints();
gbc_cLabelFirmaListesi.fill = GridBagConstraints.BOTH;
gbc_cLabelFirmaListesi.insets = new Insets(0, 0, 5, 5);
gbc_cLabelFirmaListesi.gridx = 1;
gbc_cLabelFirmaListesi.gridy = 1;
getContentPane().add(cLabelFirmaListesi, gbc_cLabelFirmaListesi);
cTablePanelFirma = new TablePanel(null, Firma.class, true, true);
cTablePanelFirma.getTable().setAutoResizeMode(
JTable.AUTO_RESIZE_ALL_COLUMNS);
cTablePanelFirma.setBorder(new LineBorder(new Color(0, 0, 0), 2));
GridBagConstraints gbc_cTablePanelFirma = new GridBagConstraints();
gbc_cTablePanelFirma.fill = GridBagConstraints.BOTH;
gbc_cTablePanelFirma.insets = new Insets(0, 0, 5, 5);
gbc_cTablePanelFirma.gridheight = 3;
gbc_cTablePanelFirma.gridx = 1;
gbc_cTablePanelFirma.gridy = 2;
getContentPane().add(cTablePanelFirma, gbc_cTablePanelFirma);
JButtonFirmaEkle = new JButton();
JButtonFirmaEkle.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/ekle.png"));
JButtonFirmaEkle.setText("F\u0130RMA EKLE");
JButtonFirmaEkle.setFont(new Font("Tahoma", Font.BOLD, 14));
GridBagConstraints gbc_JButtonFirmaEkle = new GridBagConstraints();
gbc_JButtonFirmaEkle.fill = GridBagConstraints.BOTH;
gbc_JButtonFirmaEkle.insets = new Insets(0, 0, 5, 5);
gbc_JButtonFirmaEkle.gridx = 2;
gbc_JButtonFirmaEkle.gridy = 2;
getContentPane().add(JButtonFirmaEkle, gbc_JButtonFirmaEkle);
JButtonFirmaBilgiGuncelle = new JButton();
JButtonFirmaBilgiGuncelle.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/kullaniciGuncelle.png"));
JButtonFirmaBilgiGuncelle
.setText("F\u0130RMA B\u0130LG\u0130S\u0130 G\u00DCNCELLE");
JButtonFirmaBilgiGuncelle.setFont(new Font("Tahoma", Font.BOLD, 14));
JButtonFirmaBilgiGuncelle.setEnabled(false);
GridBagConstraints gbc_JButtonFirmaBilgiGuncelle = new GridBagConstraints();
gbc_JButtonFirmaBilgiGuncelle.fill = GridBagConstraints.BOTH;
gbc_JButtonFirmaBilgiGuncelle.insets = new Insets(0, 0, 5, 5);
gbc_JButtonFirmaBilgiGuncelle.gridx = 2;
gbc_JButtonFirmaBilgiGuncelle.gridy = 3;
getContentPane().add(JButtonFirmaBilgiGuncelle,
gbc_JButtonFirmaBilgiGuncelle);
JButtonFirmaSil = new JButton();
JButtonFirmaSil.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/silIcon.png"));
JButtonFirmaSil.setText("F\u0130RMA S\u0130L");
JButtonFirmaSil.setFont(new Font("Tahoma", Font.BOLD, 14));
JButtonFirmaSil.setEnabled(false);
GridBagConstraints gbc_JButtonFirmaSil = new GridBagConstraints();
gbc_JButtonFirmaSil.insets = new Insets(0, 0, 5, 5);
gbc_JButtonFirmaSil.fill = GridBagConstraints.BOTH;
gbc_JButtonFirmaSil.gridx = 2;
gbc_JButtonFirmaSil.gridy = 4;
getContentPane().add(JButtonFirmaSil, gbc_JButtonFirmaSil);
initListeners();
refreshTable();
}
@SuppressWarnings("unchecked")
private void refreshTable() {
List<Firma> firmaListesi = (List<Firma>) HibernateManager.getInstance()
.get(Firma.class, false);
cTablePanelFirma.setObjects(firmaListesi);
}
private void initListeners() {
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
anaEkranSetEnable();
}
});
cTablePanelFirma.getTable().getSelectionModel()
.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting())
return;
firma = (Firma) cTablePanelFirma.getSelectedObject();
if (firma == null) {
JButtonFirmaBilgiGuncelle.setEnabled(false);
JButtonFirmaSil.setEnabled(false);
} else {
JButtonFirmaBilgiGuncelle.setEnabled(true);
JButtonFirmaSil.setEnabled(true);
}
}
});
cTablePanelFirma.getTable().addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
firmaTablePopupMenu(e);
}
}
@Override
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {
firmaTablePopupMenu(e);
}
}
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
firmaGuncelleClicked();
}
}
});
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
firmaSilClicked();
}
});
menuItem2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
firmaGuncelleClicked();
}
});
JButtonFirmaEkle.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
firmaEkleClicked();
}
});
JButtonFirmaBilgiGuncelle.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
firmaGuncelleClicked();
}
});
JButtonFirmaSil.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
firmaSilClicked();
}
});
}
protected void firmaEkleClicked() {
PSAFirmaEklemeEkrani.getInstance(1).setVisible(true);
setEnabled(false);
}
protected void anaEkranSetEnable() {
PSAAnaEkran.getInstance(null).setEnabled(true);
instance.dispose();
}
protected void firmaTablePopupMenu(MouseEvent e) {
Point p = e.getPoint();
int rowNumber = cTablePanelFirma.getTable().rowAtPoint(p);
ListSelectionModel model = cTablePanelFirma.getTable()
.getSelectionModel();
model.setSelectionInterval(rowNumber, rowNumber);
pMenu.show(e.getComponent(), e.getX(), e.getY());
}
protected void firmaGuncelleClicked() {
PSAFirmaGuncellemeEkrani.getInstance(firma).setVisible(true);
setEnabled(false);
}
protected void firmaSilClicked() {
int selectedOption = JOptionPane.showOptionDialog(null,
"Firmayı silmek istediğinizden emin misiniz?",
"SİSTEM UYARISI", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE, null, new String[] { "EVET",
"HAYIR" }, "default");
if (selectedOption == JOptionPane.OK_OPTION) {
boolean delete = HibernateManager.getInstance().delete(firma);
if (!delete) {
return;
}
cTablePanelFirma.getTable().clearSelection();
AutoClosingMessageDialog.showAutoClosingMessageDialog(
"Firma sistemden başarı ile silinmiştir.", 2);
}
}
}