package gui;
import hibernateSwingApi.component.TablePanel;
import hibernateSwingApi.hibernate.HibernateManager;
import hibernateSwingApi.utils.Utils;
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 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.Kullanici;
import service.AutoClosingMessageDialog;
import service.PSAUtil;
public class PSAKullaniciEkrani extends JFrame {
private static final long serialVersionUID = 1L;
private static PSAKullaniciEkrani instance;
private JPopupMenu pMenu;
private JMenuItem menuItem;
private JMenuItem menuItem2;
private JButton JButtonKullaniciEkle;
private JButton JButtonKullaniciBilgiGuncelle;
private JButton JButtonKullaniciSil;
private TablePanel cTablePanelKullanici;
private JLabel cLabelKullanici;
private Kullanici kullanici;
public static PSAKullaniciEkrani getInstance(int state) {
if (state == 0)
instance = new PSAKullaniciEkrani();
return instance;
}
public PSAKullaniciEkrani() {
initialize();
}
private void initialize() {
setTitle("PSA MERKEZ KULLANICI 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, 880, 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);
cLabelKullanici = new JLabel();
cLabelKullanici.setText("KULLANICI L\u0130STES\u0130");
cLabelKullanici.setFont(new Font("Tahoma", Font.BOLD, 16));
GridBagConstraints gbc_cLabelKullanici = new GridBagConstraints();
gbc_cLabelKullanici.fill = GridBagConstraints.BOTH;
gbc_cLabelKullanici.insets = new Insets(0, 0, 5, 5);
gbc_cLabelKullanici.gridx = 1;
gbc_cLabelKullanici.gridy = 1;
getContentPane().add(cLabelKullanici, gbc_cLabelKullanici);
cTablePanelKullanici = new TablePanel(null, Kullanici.class, true, true);
cTablePanelKullanici.getTable().setAutoResizeMode(
JTable.AUTO_RESIZE_ALL_COLUMNS);
cTablePanelKullanici.setBorder(new LineBorder(new Color(0, 0, 0), 2));
GridBagConstraints gbc_cTablePanelKullanici = new GridBagConstraints();
gbc_cTablePanelKullanici.fill = GridBagConstraints.BOTH;
gbc_cTablePanelKullanici.insets = new Insets(0, 0, 5, 5);
gbc_cTablePanelKullanici.gridheight = 3;
gbc_cTablePanelKullanici.gridx = 1;
gbc_cTablePanelKullanici.gridy = 2;
getContentPane().add(cTablePanelKullanici, gbc_cTablePanelKullanici);
JButtonKullaniciEkle = new JButton();
JButtonKullaniciEkle.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/kullaniciEkle.png"));
JButtonKullaniciEkle.setText("KULLANICI EKLE");
JButtonKullaniciEkle.setFont(new Font("Tahoma", Font.BOLD, 14));
GridBagConstraints gbc_JButtonKullaniciEkle = new GridBagConstraints();
gbc_JButtonKullaniciEkle.fill = GridBagConstraints.BOTH;
gbc_JButtonKullaniciEkle.insets = new Insets(0, 0, 5, 5);
gbc_JButtonKullaniciEkle.gridx = 2;
gbc_JButtonKullaniciEkle.gridy = 2;
getContentPane().add(JButtonKullaniciEkle, gbc_JButtonKullaniciEkle);
JButtonKullaniciBilgiGuncelle = new JButton();
JButtonKullaniciBilgiGuncelle.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/kullaniciGuncelle.png"));
JButtonKullaniciBilgiGuncelle
.setText("KULLANICI B\u0130LG\u0130S\u0130 G\u00DCNCELLE");
JButtonKullaniciBilgiGuncelle
.setFont(new Font("Tahoma", Font.BOLD, 12));
JButtonKullaniciBilgiGuncelle.setEnabled(false);
GridBagConstraints gbc_JButtonKullaniciBilgiGuncelle = new GridBagConstraints();
gbc_JButtonKullaniciBilgiGuncelle.fill = GridBagConstraints.BOTH;
gbc_JButtonKullaniciBilgiGuncelle.insets = new Insets(0, 0, 5, 5);
gbc_JButtonKullaniciBilgiGuncelle.gridx = 2;
gbc_JButtonKullaniciBilgiGuncelle.gridy = 3;
getContentPane().add(JButtonKullaniciBilgiGuncelle,
gbc_JButtonKullaniciBilgiGuncelle);
JButtonKullaniciSil = new JButton();
JButtonKullaniciSil.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/kullaniciKaldir.png"));
JButtonKullaniciSil.setText("KULLANICI S\u0130L");
JButtonKullaniciSil.setFont(new Font("Tahoma", Font.BOLD, 14));
JButtonKullaniciSil.setEnabled(false);
GridBagConstraints gbc_JButtonKullaniciSil = new GridBagConstraints();
gbc_JButtonKullaniciSil.insets = new Insets(0, 0, 5, 5);
gbc_JButtonKullaniciSil.fill = GridBagConstraints.BOTH;
gbc_JButtonKullaniciSil.gridx = 2;
gbc_JButtonKullaniciSil.gridy = 4;
getContentPane().add(JButtonKullaniciSil, gbc_JButtonKullaniciSil);
initListeners();
refreshTable();
}
private void refreshTable() {
cTablePanelKullanici.refresh();
}
private void initListeners() {
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
anaEkranSetEnable();
}
});
JButtonKullaniciEkle.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
kullaniciEkleClicked();
}
});
cTablePanelKullanici.getTable().addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
kullaniciTablePopupMenu(e);
}
}
@Override
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {
kullaniciTablePopupMenu(e);
}
}
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
kullaniciGuncelleClicked();
}
}
});
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
kullaniciSilClicked();
}
});
menuItem2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
kullaniciGuncelleClicked();
}
});
cTablePanelKullanici.getTable().getSelectionModel()
.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting())
return;
kullanici = (Kullanici) cTablePanelKullanici
.getSelectedObject();
if (kullanici == null) {
JButtonKullaniciBilgiGuncelle.setEnabled(false);
JButtonKullaniciSil.setEnabled(false);
} else {
JButtonKullaniciBilgiGuncelle.setEnabled(true);
JButtonKullaniciSil.setEnabled(true);
}
}
});
JButtonKullaniciBilgiGuncelle.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
kullaniciGuncelleClicked();
}
});
JButtonKullaniciSil.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
kullaniciSilClicked();
}
});
}
protected void kullaniciEkleClicked() {
PSAKullaniciEklemeEkrani.getInstance().setVisible(true);
setEnabled(false);
}
protected void anaEkranSetEnable() {
PSAAnaEkran.getInstance(null).setEnabled(true);
instance.dispose();
}
protected void kullaniciTablePopupMenu(MouseEvent e) {
if (e.isPopupTrigger()) {
Point p = e.getPoint();
int rowNumber = cTablePanelKullanici.getTable().rowAtPoint(p);
ListSelectionModel model = cTablePanelKullanici.getTable()
.getSelectionModel();
model.setSelectionInterval(rowNumber, rowNumber);
pMenu.show(e.getComponent(), e.getX(), e.getY());
}
}
protected void kullaniciGuncelleClicked() {
PSAKullaniciGuncellemeEkrani.getInstance(kullanici).setVisible(true);
setEnabled(false);
}
protected void kullaniciSilClicked() {
if (kullanici.isAdmin()) {
Utils.showMessage("Ana kullanıcı sistemden silinemez.");
return;
}
int selectedOption = JOptionPane.showOptionDialog(null,
"Kullanıcıyı 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(kullanici);
if (check) {
AutoClosingMessageDialog.showAutoClosingMessageDialog(
"Kullanıcı sistemden başarı ile silinmiştir.", 2);
}
cTablePanelKullanici.getTable().clearSelection();
}
}
}