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.util.Date;
import java.util.List;
import java.util.Random;
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 PSAPersonelEklemeEkrani extends JFrame {
private static final long serialVersionUID = 1L;
private static PSAPersonelEklemeEkrani instance;
private JPanel cPanelPersonel;
private JPanel cPanelPersonelEkleme;
private CTextField cTextFieldPersonelAd;
private CTextField cTextFieldPersonelSoyad;
private CTextField cTextFieldPersonelMaasTamKisim;
private CTextField cTextFieldPersonelTelNo;
private JButton JButtonPersonelEkle;
private JButton JButtonPersonelGeri;
private CComboBox<PersonelUnvan> cComboBoxPersonelUnvan;
private JSpinner cSpinnerPersonelPrim;
private CTextArea cTextAreaPersonelAdres;
private JLabel cLabelPersonelAd;
private JLabel cLabelPersonelEkleme;
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 JLabel cLabelTL;
private JScrollPane cScrollPanePersonelAdres;
private JDateChooser jDateChooserPersonelTarih;
private JButton JButtonTemizle;
private CTextField cTextFieldPersonelMaasKurusKisim;
private JLabel cLabelVirgul;
private static final String DEFAULT_COMBOBOX_VALUE = "Seçiniz";
public PSAPersonelEklemeEkrani() {
initialize();
}
public static PSAPersonelEklemeEkrani getInstance(int state) {
if (state == 0)
instance = new PSAPersonelEklemeEkrani();
return instance;
}
private void initialize() {
setTitle("PSA MERKEZ PERSONEL EKLEME 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"));
cTextFieldPersonelAd = new CTextField(20);
cTextFieldPersonelAd.setBounds(226, 132, 186, 20);
cTextFieldPersonelSoyad = new CTextField(20);
cTextFieldPersonelSoyad.setBounds(226, 160, 186, 20);
JButtonPersonelEkle = new JButton();
JButtonPersonelEkle.setBounds(89, 425, 323, 40);
JButtonPersonelEkle.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/kullaniciEkle.png"));
JButtonPersonelEkle.setText("PERSONEL EKLE");
JButtonPersonelEkle.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, 76, 20);
cSpinnerPersonelPrim.setModel(spinnerModel);
cTextFieldPersonelMaasTamKisim = new CTextField(7);
cTextFieldPersonelMaasTamKisim.setBounds(226, 335, 111, 20);
cTextFieldPersonelMaasTamKisim
.setHorizontalAlignment(SwingConstants.RIGHT);
cTextFieldPersonelMaasTamKisim.setText("0");
cTextFieldPersonelMaasTamKisim.setAsOnlyNumberTextField();
JButtonTemizle = new JButton();
JButtonTemizle.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/temizle.png"));
JButtonTemizle.setBounds(370, 11, 108, 27);
JButtonTemizle.setFont(new Font("Tahoma", Font.BOLD, 9));
JButtonTemizle.setText("TEM\u0130ZLE");
cTextFieldPersonelMaasKurusKisim = new CTextField(2);
cTextFieldPersonelMaasKurusKisim.setAsOnlyNumberTextField();
cTextFieldPersonelMaasKurusKisim.setBounds(347, 335, 44, 20);
cTextFieldPersonelMaasKurusKisim.setText("00");
cLabelVirgul = new JLabel();
cLabelVirgul.setBounds(341, 335, 5, 20);
cLabelVirgul.setText(",");
cLabelVirgul.setFont(new Font("Tahoma", Font.PLAIN, 15));
cLabelPersonelEkleme = new JLabel();
cLabelPersonelEkleme.setFont(new Font("Tahoma", Font.BOLD, 16));
cLabelPersonelEkleme.setText("PERSONEL EKLEME");
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, 116);
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("\u0130PTAL");
JButtonPersonelGeri.setFont(new Font("Tahoma", Font.BOLD, 15));
cLabelTL = new JLabel();
cLabelTL.setBounds(395, 335, 17, 20);
cLabelTL.setFont(new Font("Tahoma", Font.PLAIN, 15));
cLabelTL.setText("TL");
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);
jDateChooserPersonelTarih.setDate(new Date());
((JTextFieldDateEditor) jDateChooserPersonelTarih.getDateEditor())
.setEditable(false);
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(cLabelPersonelEkleme,
GroupLayout.DEFAULT_SIZE, 170,
Short.MAX_VALUE).addGap(315))
.addComponent(cPanelPersonelEkleme, GroupLayout.DEFAULT_SIZE,
485, Short.MAX_VALUE));
gl_cPanelPersonel.setVerticalGroup(gl_cPanelPersonel
.createParallelGroup(Alignment.LEADING).addGroup(
gl_cPanelPersonel
.createSequentialGroup()
.addComponent(cLabelPersonelEkleme,
GroupLayout.DEFAULT_SIZE, 20,
Short.MAX_VALUE)
.addGap(6)
.addComponent(cPanelPersonelEkleme,
GroupLayout.DEFAULT_SIZE, 541,
Short.MAX_VALUE)));
cPanelPersonelEkleme.setLayout(null);
cPanelPersonelEkleme.add(cTextFieldPersonelMaasKurusKisim);
cPanelPersonelEkleme.add(cLabelVirgul);
cPanelPersonelEkleme.add(cLabelPersonelIcon);
cPanelPersonelEkleme.add(JButtonTemizle);
cPanelPersonelEkleme.add(cLabelPersonelAd);
cPanelPersonelEkleme.add(cTextFieldPersonelAd);
cPanelPersonelEkleme.add(cLabelPersonelSoyad);
cPanelPersonelEkleme.add(cTextFieldPersonelSoyad);
cPanelPersonelEkleme.add(cLabelPersonelTelNo);
cPanelPersonelEkleme.add(cTextFieldPersonelTelNo);
cPanelPersonelEkleme.add(cLabelPersonelPrim);
cPanelPersonelEkleme.add(cSpinnerPersonelPrim);
cPanelPersonelEkleme.add(cLabelPersonelMaas);
cPanelPersonelEkleme.add(cTextFieldPersonelMaasTamKisim);
cPanelPersonelEkleme.add(cLabelTL);
cPanelPersonelEkleme.add(cLabelPersonelBTarih);
cPanelPersonelEkleme.add(jDateChooserPersonelTarih);
cPanelPersonelEkleme.add(JButtonPersonelEkle);
cPanelPersonelEkleme.add(JButtonPersonelGeri);
cPanelPersonelEkleme.add(cLabelPersonelAdres);
cPanelPersonelEkleme.add(cScrollPanePersonelAdres);
cPanelPersonelEkleme.add(cLabelPersonelUnvan);
cPanelPersonelEkleme.add(cComboBoxPersonelUnvan);
cPanelPersonel.setLayout(gl_cPanelPersonel);
getContentPane().setLayout(groupLayout);
initListeners();
personelUnvanDoldur();
}
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 initListeners() {
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
anaEkranSetEnable();
}
});
JButtonPersonelEkle.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
personelEkleClicked();
}
});
JButtonPersonelGeri.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
anaEkranSetEnable();
}
});
JButtonTemizle.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
temizleClicked();
}
});
cTextFieldPersonelMaasTamKisim.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent arg0) {
if (cTextFieldPersonelMaasTamKisim.getText().equals("0")) {
cTextFieldPersonelMaasTamKisim.setText("");
}
}
@Override
public void focusLost(FocusEvent e) {
if (cTextFieldPersonelMaasTamKisim.getText().equals("")) {
cTextFieldPersonelMaasTamKisim.setText("0");
}
}
});
cTextFieldPersonelMaasKurusKisim.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
if (cTextFieldPersonelMaasKurusKisim.getText().equals("00")) {
cTextFieldPersonelMaasKurusKisim.setText("");
}
}
@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);
}
}
});
((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().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) {
}
});
((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();
}
});
}
protected void anaEkranSetEnable() {
PSAPersonelEkrani.getInstance(1).setEnabled(true);
instance.dispose();
}
protected boolean primPercentControl() {
String as = ((JSpinner.DefaultEditor) cSpinnerPersonelPrim.getEditor())
.getTextField().getText();
if (Double.parseDouble(as) > 99) {
return true;
}
return false;
}
protected void personelEkleClicked() {
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;
}
Personel personel = new Personel();
personel.setKod(personelKodu());
personel.setAd(cTextFieldPersonelAd.getText().toUpperCase());
personel.setSoyad(cTextFieldPersonelSoyad.getText().toUpperCase());
personel.setTelNo(String.valueOf(cTextFieldPersonelTelNo.getValue()));
String personelAdres = cTextAreaPersonelAdres.getText().trim();
if (personelAdres.length() > 0) {
personel.setAdres(personelAdres);
}
if (!cComboBoxPersonelUnvan.getSelectedItem().toString()
.equals(DEFAULT_COMBOBOX_VALUE)) {
personel.setUnvan(cComboBoxPersonelUnvan.getSelectedItem()
.toString());
}
personel.setPrimOrani(Integer.parseInt(cSpinnerPersonelPrim.getValue()
.toString()));
personel.setMaas(Double.parseDouble(cTextFieldPersonelMaasTamKisim
.getText() + "." + cTextFieldPersonelMaasKurusKisim.getText()));
personel.setBaslamaTarihi(jDateChooserPersonelTarih.getDate());
boolean control = HibernateManager.getInstance().save(personel);
if (control == true) {
AutoClosingMessageDialog.showAutoClosingMessageDialog(
"Sisteme personel kaydınız başarı ile tamamlanmıştır.", 2);
anaEkranSetEnable();
}
}
protected void temizleClicked() {
cTextFieldPersonelAd.setText("");
cTextFieldPersonelSoyad.setText("");
cTextFieldPersonelTelNo.setValue(null);
if (cComboBoxPersonelUnvan.getItemCount() > 0) {
cComboBoxPersonelUnvan.setSelectedIndex(0);
}
cComboBoxPersonelUnvan.updateUI();
cTextAreaPersonelAdres.setText("");
cSpinnerPersonelPrim.setValue(0);
cTextFieldPersonelMaasTamKisim.setText("0");
cTextFieldPersonelMaasKurusKisim.setText("00");
}
private boolean userControl() {
boolean control = true;
if (String.valueOf(cTextFieldPersonelTelNo.getValue()).length() < 10) {
control = false;
return control;
}
if (cTextFieldPersonelAd.getText().trim().equals("")
|| cTextFieldPersonelSoyad.getText().trim().equals("")
|| String.valueOf(cTextFieldPersonelTelNo.getValue())
.equals("")) {
control = false;
}
return control;
}
private static int personelKodu() {
Random rdm = new Random();
return rdm.nextInt(1000);
}
}