package gui;
import hibernateSwingApi.component.CComboBox;
import hibernateSwingApi.component.CTextArea;
import hibernateSwingApi.component.CTextField;
import hibernateSwingApi.hibernate.HibernateManager;
import hibernateSwingApi.utils.Utils;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.MaskFormatter;
import pojos.Personel;
import pojos.PersonelUnvan;
import service.AutoClosingMessageDialog;
import service.PSAUtil;
import com.toedter.calendar.JDateChooser;
import com.toedter.calendar.JTextFieldDateEditor;
public class PSAPersonelGuncellemeEkrani extends JFrame {
private static final long serialVersionUID = 1L;
private static PSAPersonelGuncellemeEkrani instance;
private JPanel cPanelPersonel;
private JPanel cPanelPersonelEkleme;
private CTextField cTextFieldPersonelTelNo;
private CTextArea cTextAreaPersonelAdres;
private JButton JButtonPersonelGuncelle;
private JButton JButtonPersonelGeri;
private CComboBox<PersonelUnvan> cComboBoxPersonelUnvan;
private JSpinner cSpinnerPersonelPrim;
private JLabel cLabelPersonelBilgiAd;
private JLabel cLabelPersonelBilgiSoyadi;
private JLabel cLabelPersonelAd;
private JLabel cLabelPersonelGuncelleme;
private JLabel cLabelPersonelSoyad;
private JLabel cLabelPersonelTelNo;
private JLabel cLabelPersonelAdres;
private JLabel cLabelPersonelUnvan;
private JLabel cLabelPersonelPrim;
private JLabel cLabelPersonelMaas;
private JLabel cLabelPersonelBTarih;
private JLabel cLabelPersonelIcon;
private JScrollPane cScrollPanePersonelAdres;
private JDateChooser jDateChooserPersonelTarih;
private CTextField cTextFieldPersonelMaasTamKisim;
private JLabel cLabelVirgul;
private CTextField cTextFieldPersonelMaasKurusKisim;
private JLabel cLabelTL;
private Personel personel;
private static final String NO_CONTROL = "(___) ___ __ __";
private static final String DEFAULT_COMBOBOX_VALUE = "Seçiniz";
public static PSAPersonelGuncellemeEkrani getInstance(Personel personel) {
if (personel != null)
instance = new PSAPersonelGuncellemeEkrani(personel);
return instance;
}
public PSAPersonelGuncellemeEkrani(Personel personel1) {
personel = personel1;
initialize();
}
private void initialize() {
setTitle("PSA MERKEZ PERSONEL BİLGİ GÜNCELLEME EKRANI");
setBounds(0, 0, 510, 617);
setResizable(false);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setIconImage(PSAUtil.getImageIconViaRelativePath("picture/psaIcon.png")
.getImage());
cPanelPersonel = new JPanel();
cPanelPersonelEkleme = new JPanel();
cPanelPersonelEkleme.setBackground(UIManager.getColor("Button.shadow"));
cLabelPersonelBilgiAd = new JLabel();
cLabelPersonelBilgiAd.setBounds(226, 132, 186, 20);
cLabelPersonelBilgiSoyadi = new JLabel();
cLabelPersonelBilgiSoyadi.setBounds(226, 160, 186, 20);
JButtonPersonelGuncelle = new JButton();
JButtonPersonelGuncelle.setBounds(89, 425, 323, 40);
JButtonPersonelGuncelle.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/kullaniciEkle.png"));
JButtonPersonelGuncelle.setText("PERSONEL BİLGİLERİ GÜNCELLE");
JButtonPersonelGuncelle.setFont(new Font("Tahoma", Font.BOLD, 15));
MaskFormatter mf = null;
try {
mf = new MaskFormatter("(###) ### ## ##");
mf.setPlaceholderCharacter('_');
} catch (ParseException e) {
e.printStackTrace();
}
cTextFieldPersonelTelNo = new CTextField(mf);
cTextFieldPersonelTelNo.setBounds(226, 188, 186, 20);
cComboBoxPersonelUnvan = new CComboBox<PersonelUnvan>(
PersonelUnvan.class);
cComboBoxPersonelUnvan.setBounds(226, 279, 186, 20);
SpinnerModel spinnerModel = new SpinnerNumberModel(0, 0, 99, 1);
cSpinnerPersonelPrim = new JSpinner();
cSpinnerPersonelPrim.setBounds(226, 307, 83, 20);
cSpinnerPersonelPrim.setModel(spinnerModel);
cLabelPersonelGuncelleme = new JLabel();
cLabelPersonelGuncelleme.setFont(new Font("Tahoma", Font.BOLD, 16));
cLabelPersonelGuncelleme
.setText("PERSONEL B\u0130LG\u0130S\u0130 G\u00DCNCELLEME");
cLabelPersonelAd = new JLabel();
cLabelPersonelAd.setBounds(89, 132, 127, 20);
cLabelPersonelAd.setFont(new Font("Tahoma", Font.BOLD, 14));
cLabelPersonelAd.setText("*Ad\u0131");
cLabelPersonelSoyad = new JLabel();
cLabelPersonelSoyad.setBounds(89, 160, 127, 20);
cLabelPersonelSoyad.setText("*Soyad\u0131");
cLabelPersonelSoyad.setFont(new Font("Tahoma", Font.BOLD, 14));
cLabelPersonelTelNo = new JLabel();
cLabelPersonelTelNo.setBounds(89, 188, 127, 20);
cLabelPersonelTelNo.setText("*Tel No.");
cLabelPersonelTelNo.setFont(new Font("Tahoma", Font.BOLD, 14));
cLabelPersonelAdres = new JLabel();
cLabelPersonelAdres.setBounds(89, 219, 127, 20);
cLabelPersonelAdres.setText("Adresi");
cLabelPersonelAdres.setFont(new Font("Tahoma", Font.BOLD, 14));
cLabelPersonelUnvan = new JLabel();
cLabelPersonelUnvan.setBounds(89, 279, 127, 20);
cLabelPersonelUnvan.setText("\u00DCnvan\u0131");
cLabelPersonelUnvan.setFont(new Font("Tahoma", Font.BOLD, 14));
cLabelPersonelPrim = new JLabel();
cLabelPersonelPrim.setBounds(89, 307, 127, 20);
cLabelPersonelPrim.setText("Prim Miktar\u0131 ( % )");
cLabelPersonelPrim.setFont(new Font("Tahoma", Font.BOLD, 14));
cLabelPersonelMaas = new JLabel();
cLabelPersonelMaas.setBounds(89, 335, 127, 20);
cLabelPersonelMaas.setText("Maa\u015F Miktar\u0131");
cLabelPersonelMaas.setFont(new Font("Tahoma", Font.BOLD, 14));
cLabelPersonelBTarih = new JLabel();
cLabelPersonelBTarih.setBounds(89, 363, 127, 20);
cLabelPersonelBTarih.setText("Ba\u015Flama Tarihi");
cLabelPersonelBTarih.setFont(new Font("Tahoma", Font.BOLD, 14));
cLabelPersonelIcon = new JLabel();
cLabelPersonelIcon.setBounds(194, 11, 96, 115);
cLabelPersonelIcon.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/personelIcon.png"));
JButtonPersonelGeri = new JButton();
JButtonPersonelGeri.setBounds(89, 476, 323, 40);
JButtonPersonelGeri.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/geriButonu.png"));
JButtonPersonelGeri.setText("GER\u0130 D\u00D6N");
JButtonPersonelGeri.setFont(new Font("Tahoma", Font.BOLD, 15));
cScrollPanePersonelAdres = new JScrollPane();
cScrollPanePersonelAdres.setBounds(226, 219, 186, 51);
cTextAreaPersonelAdres = new CTextArea(100);
cTextAreaPersonelAdres.setLineWrap(true);
cScrollPanePersonelAdres.setViewportView(cTextAreaPersonelAdres);
jDateChooserPersonelTarih = new JDateChooser();
jDateChooserPersonelTarih.setBounds(226, 363, 186, 20);
((JTextFieldDateEditor) jDateChooserPersonelTarih.getDateEditor())
.setEditable(false);
cTextFieldPersonelMaasTamKisim = new CTextField(7);
cTextFieldPersonelMaasTamKisim.setAsOnlyNumberTextField();
cTextFieldPersonelMaasTamKisim.setText("0");
cTextFieldPersonelMaasTamKisim
.setHorizontalAlignment(SwingConstants.RIGHT);
cTextFieldPersonelMaasTamKisim.setBounds(226, 335, 111, 20);
cPanelPersonelEkleme.add(cTextFieldPersonelMaasTamKisim);
cLabelVirgul = new JLabel();
cLabelVirgul.setText(",");
cLabelVirgul.setFont(new Font("Tahoma", Font.PLAIN, 15));
cLabelVirgul.setBounds(341, 335, 5, 20);
cPanelPersonelEkleme.add(cLabelVirgul);
cTextFieldPersonelMaasKurusKisim = new CTextField(2);
cTextFieldPersonelMaasKurusKisim.setAsOnlyNumberTextField();
cTextFieldPersonelMaasKurusKisim.setText("00");
cTextFieldPersonelMaasKurusKisim.setBounds(347, 335, 44, 20);
cPanelPersonelEkleme.add(cTextFieldPersonelMaasKurusKisim);
cLabelTL = new JLabel();
cLabelTL.setText("TL");
cLabelTL.setFont(new Font("Tahoma", Font.PLAIN, 15));
cLabelTL.setBounds(395, 335, 17, 20);
cPanelPersonelEkleme.add(cLabelTL);
GroupLayout groupLayout = new GroupLayout(getContentPane());
groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(
Alignment.LEADING).addGroup(
groupLayout
.createSequentialGroup()
.addGap(10)
.addComponent(cPanelPersonel, GroupLayout.DEFAULT_SIZE,
485, Short.MAX_VALUE).addGap(9)));
groupLayout.setVerticalGroup(groupLayout.createParallelGroup(
Alignment.LEADING).addGroup(
groupLayout
.createSequentialGroup()
.addGap(11)
.addComponent(cPanelPersonel, GroupLayout.DEFAULT_SIZE,
567, Short.MAX_VALUE).addGap(11)));
GroupLayout gl_cPanelPersonel = new GroupLayout(cPanelPersonel);
gl_cPanelPersonel.setHorizontalGroup(gl_cPanelPersonel
.createParallelGroup(Alignment.LEADING)
.addGroup(
gl_cPanelPersonel
.createSequentialGroup()
.addComponent(cLabelPersonelGuncelleme,
GroupLayout.DEFAULT_SIZE, 275,
Short.MAX_VALUE).addGap(210))
.addComponent(cPanelPersonelEkleme, GroupLayout.DEFAULT_SIZE,
485, Short.MAX_VALUE));
gl_cPanelPersonel.setVerticalGroup(gl_cPanelPersonel
.createParallelGroup(Alignment.LEADING).addGroup(
gl_cPanelPersonel
.createSequentialGroup()
.addComponent(cLabelPersonelGuncelleme,
GroupLayout.DEFAULT_SIZE, 20,
Short.MAX_VALUE)
.addGap(6)
.addComponent(cPanelPersonelEkleme,
GroupLayout.DEFAULT_SIZE, 541,
Short.MAX_VALUE)));
cPanelPersonelEkleme.setLayout(null);
cPanelPersonelEkleme.add(cLabelPersonelIcon);
cPanelPersonelEkleme.add(cLabelPersonelAd);
cPanelPersonelEkleme.add(cLabelPersonelBilgiAd);
cPanelPersonelEkleme.add(cLabelPersonelSoyad);
cPanelPersonelEkleme.add(cLabelPersonelBilgiSoyadi);
cPanelPersonelEkleme.add(cLabelPersonelTelNo);
cPanelPersonelEkleme.add(cTextFieldPersonelTelNo);
cPanelPersonelEkleme.add(cLabelPersonelPrim);
cPanelPersonelEkleme.add(cSpinnerPersonelPrim);
cPanelPersonelEkleme.add(cLabelPersonelMaas);
cPanelPersonelEkleme.add(cLabelPersonelBTarih);
cPanelPersonelEkleme.add(jDateChooserPersonelTarih);
cPanelPersonelEkleme.add(JButtonPersonelGuncelle);
cPanelPersonelEkleme.add(JButtonPersonelGeri);
cPanelPersonelEkleme.add(cLabelPersonelAdres);
cPanelPersonelEkleme.add(cScrollPanePersonelAdres);
cPanelPersonelEkleme.add(cLabelPersonelUnvan);
cPanelPersonelEkleme.add(cComboBoxPersonelUnvan);
cPanelPersonel.setLayout(gl_cPanelPersonel);
getContentPane().setLayout(groupLayout);
initListeners();
personelUnvanDoldur();
personelBilgileriDoldur(personel);
}
private void personelUnvanDoldur() {
List<?> personelListesi = HibernateManager.getInstance().get(
PersonelUnvan.class, false);
PersonelUnvan personelUnvan = new PersonelUnvan();
personelUnvan.setUnvan(DEFAULT_COMBOBOX_VALUE);
cComboBoxPersonelUnvan.setInitialObject(personelUnvan);
cComboBoxPersonelUnvan.setObjects(personelListesi);
}
private void personelBilgileriDoldur(Personel personel) {
cLabelPersonelBilgiAd.setText(personel.getAd());
cLabelPersonelBilgiSoyadi.setText(personel.getSoyad());
cTextFieldPersonelTelNo.setValue(personel.getTelNo());
cTextAreaPersonelAdres.setText(personel.getAdres());
if (cComboBoxPersonelUnvan.getItemCount() > 0) {
String personelUnvan = personel.getUnvan();
cComboBoxPersonelUnvan
.setSelectedIndex(personelUnvanIndex(personelUnvan));
}
cTextFieldPersonelMaasTamKisim.setText(maasTamKisim(String
.valueOf(personel.getMaas())));
cTextFieldPersonelMaasKurusKisim.setText(maasKurusKisim(String
.valueOf(personel.getMaas())));
cSpinnerPersonelPrim.setValue(personel.getPrimOrani());
jDateChooserPersonelTarih.setDate(personel.getBaslamaTarihi());
}
private int personelUnvanIndex(String unvan) {
for (int i = 0; i < cComboBoxPersonelUnvan.getItemCount(); i++) {
String personelUnvan = cComboBoxPersonelUnvan.getItemAt(i)
.toString();
if (unvan.equals(personelUnvan)) {
return i;
}
}
return -1;
}
private void initListeners() {
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
anaEkranSetEnable();
}
});
JButtonPersonelGuncelle.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
personelGuncelleClicked();
}
});
JButtonPersonelGeri.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
anaEkranSetEnable();
}
});
((JSpinner.DefaultEditor) cSpinnerPersonelPrim.getEditor())
.getTextField().addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
((JSpinner.DefaultEditor) cSpinnerPersonelPrim
.getEditor()).getTextField().setEditable(true);
((JSpinner.DefaultEditor) cSpinnerPersonelPrim
.getEditor()).getTextField().selectAll();
}
});
cSpinnerPersonelPrim.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
((JSpinner.DefaultEditor) cSpinnerPersonelPrim.getEditor())
.getTextField().setEditable(false);
}
});
((JSpinner.DefaultEditor) cSpinnerPersonelPrim.getEditor())
.getTextField().addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
char keyChar = e.getKeyChar();
if ((keyChar >= '0' && keyChar <= '9')
|| keyChar == '.') {
return;
}
e.consume();
}
});
cTextFieldPersonelMaasKurusKisim.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
if (cTextFieldPersonelMaasKurusKisim.getText().equals("")) {
cTextFieldPersonelMaasKurusKisim.setText("00");
}
if (cTextFieldPersonelMaasKurusKisim.getText().length() == 1) {
String kurusKisim = cTextFieldPersonelMaasKurusKisim
.getText() + "0";
cTextFieldPersonelMaasKurusKisim.setText(kurusKisim);
}
}
});
cTextFieldPersonelMaasTamKisim.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
if (cTextFieldPersonelMaasTamKisim.getText().equals("")) {
cTextFieldPersonelMaasTamKisim.setText("0");
}
}
});
cTextAreaPersonelAdres.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent arg0) {
if (cTextAreaPersonelAdres.getText().equals("-")) {
cTextAreaPersonelAdres.setText("");
}
}
@Override
public void focusLost(FocusEvent e) {
if (personel.getAdres().equals("-")) {
if (cTextAreaPersonelAdres.getText().equals("")) {
cTextAreaPersonelAdres.setText("-");
}
}
}
});
((JSpinner.DefaultEditor) cSpinnerPersonelPrim.getEditor())
.getTextField().addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent arg0) {
if (primPercentControl()) {
AutoClosingMessageDialog
.showAutoClosingMessageDialog(
"Personel primi %99'u geçemez.", 2);
cSpinnerPersonelPrim.setValue(99);
return;
}
}
@Override
public void focusGained(FocusEvent arg0) {
}
});
cTextFieldPersonelTelNo.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent arg0) {
if (arg0.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
if (cTextFieldPersonelTelNo.getText().equals(NO_CONTROL)) {
cTextFieldPersonelTelNo.setValue(null);
}
}
}
});
}
protected boolean primPercentControl() {
String as = ((JSpinner.DefaultEditor) cSpinnerPersonelPrim.getEditor())
.getTextField().getText();
if (Double.parseDouble(as) > 99) {
return true;
}
return false;
}
protected void anaEkranSetEnable() {
PSAPersonelEkrani.getInstance(1).setEnabled(true);
instance.dispose();
}
protected void personelGuncelleClicked() {
if (primPercentControl()) {
AutoClosingMessageDialog.showAutoClosingMessageDialog(
"Personel primi %99'u geçemez.", 2);
cSpinnerPersonelPrim.setValue(99);
return;
}
if (!userControl()) {
Utils.showMessage("Lütfen zorunlu(*) alanları doldurunuz.");
return;
}
if (!compareUserInfo(personel)) {
Utils.showMessage("Personel ile ilgili güncellenecek bilgi bulunmamaktadır.");
return;
}
String personelTelNo = String.valueOf(cTextFieldPersonelTelNo
.getValue());
String personelAdres = cTextAreaPersonelAdres.getText().trim();
personel.setTelNo(personelTelNo);
if (personelAdres.length() > 0) {
personel.setAdres(personelAdres);
} else {
personel.setAdres("-");
}
if (!cComboBoxPersonelUnvan.getSelectedItem().toString()
.equals(DEFAULT_COMBOBOX_VALUE)) {
personel.setUnvan(cComboBoxPersonelUnvan.getSelectedItem()
.toString());
} else {
personel.setUnvan("-");
}
personel.setPrimOrani(Integer.parseInt(cSpinnerPersonelPrim.getValue()
.toString()));
personel.setMaas(Double.parseDouble(cTextFieldPersonelMaasTamKisim
.getText() + "." + cTextFieldPersonelMaasKurusKisim.getText()));
personel.setBaslamaTarihi(jDateChooserPersonelTarih.getDate());
boolean control = HibernateManager.getInstance().update(personel);
if (control) {
AutoClosingMessageDialog.showAutoClosingMessageDialog(
"Personel ile ilgili bilgi güncellenmiştir.", 2);
anaEkranSetEnable();
}
}
protected boolean compareUserInfo(Personel personel) {
boolean control = true;
String personelTarih = new SimpleDateFormat("dd.MMM.yyyy")
.format(personel.getBaslamaTarihi());
String jDateChooserTarih = new SimpleDateFormat("dd.MMM.yyyy")
.format(jDateChooserPersonelTarih.getDate());
Object telNo = cTextFieldPersonelTelNo.getValue();
if (telNo == null) {
telNo = "-";
}
if (personel.getTelNo().equals(String.valueOf(telNo))
&& personel.getAdres().equals(cTextAreaPersonelAdres.getText())
&& personel.getPrimOrani() == Integer
.parseInt(cSpinnerPersonelPrim.getValue().toString())
&& personel.getMaas() == (Double
.parseDouble(cTextFieldPersonelMaasTamKisim.getText()
+ "."
+ cTextFieldPersonelMaasKurusKisim.getText()))
&& personelTarih.equals(jDateChooserTarih)) {
if (!cComboBoxPersonelUnvan.getSelectedItem().toString()
.equals(DEFAULT_COMBOBOX_VALUE)) {
if (personel.getUnvan().equals(
cComboBoxPersonelUnvan.getSelectedItem().toString())) {
control = false;
}
} else {
if (personel.getUnvan().equals("-")) {
control = false;
}
}
}
return control;
}
private boolean userControl() {
boolean control = true;
if (String.valueOf(cTextFieldPersonelTelNo.getValue()).length() < 10) {
control = false;
}
if (String.valueOf(cTextFieldPersonelTelNo.getValue()).equals("")) {
control = false;
}
return control;
}
private static String maasTamKisim(String fiyatTam) {
String[] parts = fiyatTam.split("\\.");
fiyatTam = parts[0];
return fiyatTam;
}
private static String maasKurusKisim(String fiyatTam) {
String[] parts = fiyatTam.split("\\.");
if (parts.length < 2) {
fiyatTam = "00";
} else {
fiyatTam = parts[1];
if (fiyatTam.length() < 2) {
fiyatTam = fiyatTam + "0";
}
}
return fiyatTam;
}
}