package gui;
import hibernateSwingApi.component.CTable;
import hibernateSwingApi.hibernate.HibernateManager;
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.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import pojos.BankaPOSBilgileri;
import service.AutoClosingMessageDialog;
import service.PSAUtil;
public class PSAPOSBilgileriEkrani extends JFrame {
private static final long serialVersionUID = 1L;
private static PSAPOSBilgileriEkrani instance;
private JLabel jLabelBankalar;
private JButton jButtonBankaEkle;
private JButton jButtonBankaSil;
private JButton jButtonBankaDuzenle;
private JScrollPane scrollPane;
private CTable cTablePosBilgileri;
private BankaPOSBilgileri posBilgi;
private JPopupMenu pMenu;
private JMenuItem menuItem;
private JMenuItem menuItem2;
public static PSAPOSBilgileriEkrani getInstance(int state) {
if (state == 0)
instance = new PSAPOSBilgileriEkrani();
return instance;
}
public PSAPOSBilgileriEkrani() {
initialize();
}
private void initialize() {
setTitle("PSA MERKEZ POS BİLGİLERİ EKRANI");
setResizable(false);
setBounds(0, 0, 671, 303);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setIconImage(PSAUtil.getImageIconViaRelativePath("picture/psaIcon.png")
.getImage());
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[] { 0, 306, 150, 0, 0 };
gridBagLayout.rowHeights = new int[] { 0, 35, 60, 60, 60, 0, 0 };
gridBagLayout.columnWeights = new double[] { 1.0, 1.0, 1.0, 1.0,
Double.MIN_VALUE };
gridBagLayout.rowWeights = new double[] { 1.0, 0.0, 1.0, 1.0, 1.0, 1.0,
Double.MIN_VALUE };
getContentPane().setLayout(gridBagLayout);
jLabelBankalar = new JLabel("BANKALAR");
jLabelBankalar.setFont(new Font("Tahoma", Font.BOLD, 14));
jLabelBankalar.setHorizontalAlignment(SwingConstants.LEFT);
GridBagConstraints gbc_jLabelBankalar = new GridBagConstraints();
gbc_jLabelBankalar.fill = GridBagConstraints.BOTH;
gbc_jLabelBankalar.insets = new Insets(0, 0, 5, 5);
gbc_jLabelBankalar.gridx = 1;
gbc_jLabelBankalar.gridy = 1;
getContentPane().add(jLabelBankalar, gbc_jLabelBankalar);
scrollPane = new JScrollPane();
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
gbc_scrollPane.fill = GridBagConstraints.BOTH;
gbc_scrollPane.insets = new Insets(0, 0, 5, 5);
gbc_scrollPane.gridheight = 3;
gbc_scrollPane.gridx = 1;
gbc_scrollPane.gridy = 2;
getContentPane().add(scrollPane, gbc_scrollPane);
cTablePosBilgileri = new CTable((String) null, BankaPOSBilgileri.class,
true, true);
cTablePosBilgileri.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
scrollPane.setViewportView(cTablePosBilgileri);
jButtonBankaEkle = new JButton("YENİ BANKA TANIMLA");
jButtonBankaEkle.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/bankaEkle.png"));
jButtonBankaEkle.setFont(new Font("Tahoma", Font.BOLD, 14));
GridBagConstraints gbc_jButtonBankaEkle = new GridBagConstraints();
gbc_jButtonBankaEkle.fill = GridBagConstraints.BOTH;
gbc_jButtonBankaEkle.insets = new Insets(0, 0, 5, 5);
gbc_jButtonBankaEkle.gridx = 2;
gbc_jButtonBankaEkle.gridy = 2;
getContentPane().add(jButtonBankaEkle, gbc_jButtonBankaEkle);
jButtonBankaDuzenle = new JButton("BANKA BİLGİLERİ DÜZENLE");
jButtonBankaDuzenle.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/kullaniciGuncelle.png"));
jButtonBankaDuzenle.setFont(new Font("Tahoma", Font.BOLD, 14));
jButtonBankaDuzenle.setEnabled(false);
GridBagConstraints gbc_jButtonBankaDuzenle = new GridBagConstraints();
gbc_jButtonBankaDuzenle.fill = GridBagConstraints.BOTH;
gbc_jButtonBankaDuzenle.insets = new Insets(0, 0, 5, 5);
gbc_jButtonBankaDuzenle.gridx = 2;
gbc_jButtonBankaDuzenle.gridy = 3;
getContentPane().add(jButtonBankaDuzenle, gbc_jButtonBankaDuzenle);
jButtonBankaSil = new JButton("BANKA SİL");
jButtonBankaSil.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/silIcon.png"));
jButtonBankaSil.setFont(new Font("Tahoma", Font.BOLD, 14));
jButtonBankaSil.setEnabled(false);
GridBagConstraints gbc_jButtonBankaSil = new GridBagConstraints();
gbc_jButtonBankaSil.insets = new Insets(0, 0, 5, 5);
gbc_jButtonBankaSil.fill = GridBagConstraints.BOTH;
gbc_jButtonBankaSil.gridx = 2;
gbc_jButtonBankaSil.gridy = 4;
getContentPane().add(jButtonBankaSil, gbc_jButtonBankaSil);
pMenu = new JPopupMenu();
menuItem = new JMenuItem("Sil");
pMenu.add(menuItem);
menuItem2 = new JMenuItem("Güncelle");
pMenu.add(menuItem2);
initListeners();
refreshTable();
}
@SuppressWarnings("unchecked")
private void refreshTable() {
List<BankaPOSBilgileri> bankaListesi = (List<BankaPOSBilgileri>) HibernateManager
.getInstance().get(BankaPOSBilgileri.class, false);
cTablePosBilgileri.setObjects(bankaListesi);
}
private void initListeners() {
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
anaEkranSetEnable();
}
});
jButtonBankaEkle.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
bankaEkleDuzenleClicked(0);
}
});
jButtonBankaDuzenle.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
bankaEkleDuzenleClicked(1);
}
});
jButtonBankaSil.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
bankaSilClicked();
}
});
cTablePosBilgileri.getSelectionModel().addListSelectionListener(
new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting())
return;
posBilgi = (BankaPOSBilgileri) cTablePosBilgileri
.getSelectedObject();
if (posBilgi == null) {
jButtonBankaDuzenle.setEnabled(false);
jButtonBankaSil.setEnabled(false);
return;
}
jButtonBankaDuzenle.setEnabled(true);
jButtonBankaSil.setEnabled(true);
}
});
cTablePosBilgileri.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
bankaTablePopupMenu(e);
}
}
@Override
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {
bankaTablePopupMenu(e);
}
}
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
bankaEkleDuzenleClicked(1);
}
}
});
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
bankaSilClicked();
}
});
menuItem2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
bankaEkleDuzenleClicked(1);
}
});
}
protected void anaEkranSetEnable() {
PSAAnaEkran.getInstance(null).setEnabled(true);
instance.dispose();
}
protected void bankaSilClicked() {
int selectedOption = JOptionPane.showOptionDialog(null,
"Bankayı 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 check = HibernateManager.getInstance().delete(posBilgi);
if (check) {
AutoClosingMessageDialog.showAutoClosingMessageDialog(
"Banka sistemden silinmiştir.", 2);
}
}
}
protected void bankaTablePopupMenu(MouseEvent e) {
Point p = e.getPoint();
int rowNumber = cTablePosBilgileri.rowAtPoint(p);
ListSelectionModel model = cTablePosBilgileri.getSelectionModel();
model.setSelectionInterval(rowNumber, rowNumber);
pMenu.show(e.getComponent(), e.getX(), e.getY());
}
protected void bankaEkleDuzenleClicked(int state) {
PSABankaEkleDuzenleEkrani.getInstance(state, posBilgi).setVisible(true);
cTablePosBilgileri.clearSelection();
setEnabled(false);
}
}