package gui;
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.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.ParseException;
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.UIManager;
import javax.swing.WindowConstants;
import javax.swing.text.MaskFormatter;
import pojos.Firma;
import service.AutoClosingMessageDialog;
import service.PSAUtil;
import control.MailRegex;
public class PSAFirmaGuncellemeEkrani extends JFrame {
private static final long serialVersionUID = 1L;
private static PSAFirmaGuncellemeEkrani instance;
private JPanel cPanelFirma;
private JLabel cLabelFirmaGuncelleme;
private JPanel cPanelFirmaEkleme;
private JLabel cLabelFirmaAd;
private JLabel cLabelFirmaTelNo;
private JLabel cLabelFaksNo;
private JLabel cLabelFirmaIcon;
private JLabel cLabelFirmaAdres;
private JLabel cLabelFirmaMail;
private JLabel cLabelFirmaBilgiAd;
private CTextField cTextFieldFirmaTelNo;
private CTextField cTextFieldFaksNo;
private CTextField cTextFieldFirmaMail;
private JButton JButtonFirmaGuncelle;
private JButton JButtonFirmaGeri;
private JScrollPane cScrollPaneFirmaAdres;
private CTextArea cTextAreaFirmaAdres;
private Firma firma;
private static final String E_MAIL = "info@altisoft.com.tr";
private static final String KONTROL = "-";
private static final String NO_CONTROL = "(___) ___ __ __";
public static PSAFirmaGuncellemeEkrani getInstance(Firma firma) {
if (firma != null)
instance = new PSAFirmaGuncellemeEkrani(firma);
return instance;
}
public PSAFirmaGuncellemeEkrani(Firma firma1) {
firma = firma1;
initialize();
}
private void initialize() {
setTitle("PSA MERKEZ FİRMA BİLGİSİ GÜNCELLEME EKRANI");
setBounds(0, 0, 510, 567);
setResizable(false);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setIconImage(PSAUtil.getImageIconViaRelativePath("picture/psaIcon.png")
.getImage());
cPanelFirma = new JPanel();
cLabelFirmaGuncelleme = new JLabel();
cLabelFirmaGuncelleme.setText("F\u0130RMA G\u00DCNCELLEME");
cLabelFirmaGuncelleme.setFont(new Font("Tahoma", Font.BOLD, 16));
cPanelFirmaEkleme = new JPanel();
cPanelFirmaEkleme.setBackground(UIManager.getColor("Button.shadow"));
cLabelFirmaAd = new JLabel();
cLabelFirmaAd.setBounds(89, 132, 127, 20);
cLabelFirmaAd.setText("*Firma Ad\u0131");
cLabelFirmaAd.setFont(new Font("Tahoma", Font.BOLD, 14));
cLabelFirmaBilgiAd = new JLabel();
cLabelFirmaBilgiAd.setBounds(226, 132, 186, 21);
cLabelFirmaTelNo = new JLabel();
cLabelFirmaTelNo.setBounds(89, 160, 127, 21);
cLabelFirmaTelNo.setText("Tel No");
cLabelFirmaTelNo.setFont(new Font("Tahoma", Font.BOLD, 14));
MaskFormatter mf = null;
try {
mf = new MaskFormatter("(###) ### ## ##");
mf.setPlaceholderCharacter('_');
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cTextFieldFirmaTelNo = new CTextField(mf);
cTextFieldFirmaTelNo.setBounds(226, 160, 186, 20);
cTextFieldFirmaTelNo.setAsNumberTextField();
cLabelFaksNo = new JLabel();
cLabelFaksNo.setBounds(89, 188, 127, 20);
cLabelFaksNo.setText("Faks No");
cLabelFaksNo.setFont(new Font("Tahoma", Font.BOLD, 14));
cTextFieldFaksNo = new CTextField(mf);
cTextFieldFaksNo.setBounds(226, 188, 186, 21);
cTextFieldFaksNo.setAsNumberTextField();
cLabelFirmaAdres = new JLabel();
cLabelFirmaAdres.setBounds(89, 247, 127, 20);
cLabelFirmaAdres.setText("Adresi");
cLabelFirmaAdres.setFont(new Font("Tahoma", Font.BOLD, 14));
JButtonFirmaGuncelle = new JButton();
JButtonFirmaGuncelle.setBounds(89, 379, 323, 40);
JButtonFirmaGuncelle.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/ekle.png"));
JButtonFirmaGuncelle.setText("F\u0130RMA G\u00DCNCELLE");
JButtonFirmaGuncelle.setFont(new Font("Tahoma", Font.BOLD, 15));
cLabelFirmaIcon = new JLabel();
cLabelFirmaIcon.setBounds(194, 11, 96, 116);
cLabelFirmaIcon.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/firmaBilgileri.png"));
cLabelFirmaMail = new JLabel();
cLabelFirmaMail.setBounds(89, 219, 127, 20);
cLabelFirmaMail.setText("E-posta");
cLabelFirmaMail.setFont(new Font("Tahoma", Font.BOLD, 14));
cTextFieldFirmaMail = new CTextField(40);
cTextFieldFirmaMail.setBounds(226, 219, 186, 20);
cTextFieldFirmaMail.setHint(E_MAIL);
cScrollPaneFirmaAdres = new JScrollPane();
cScrollPaneFirmaAdres.setBounds(226, 247, 186, 81);
cTextAreaFirmaAdres = new CTextArea(100);
cTextAreaFirmaAdres.setLineWrap(true);
cScrollPaneFirmaAdres.setViewportView(cTextAreaFirmaAdres);
JButtonFirmaGeri = new JButton();
JButtonFirmaGeri.setBounds(89, 432, 323, 40);
JButtonFirmaGeri.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/geriButonu.png"));
JButtonFirmaGeri.setText("GER\u0130 D\u00D6N");
JButtonFirmaGeri.setFont(new Font("Tahoma", Font.BOLD, 15));
GroupLayout gl_cPanelFirma = new GroupLayout(cPanelFirma);
gl_cPanelFirma.setHorizontalGroup(gl_cPanelFirma
.createParallelGroup(Alignment.LEADING)
.addGroup(
gl_cPanelFirma
.createSequentialGroup()
.addComponent(cLabelFirmaGuncelleme,
GroupLayout.DEFAULT_SIZE, 213,
Short.MAX_VALUE).addGap(317))
.addComponent(cPanelFirmaEkleme, GroupLayout.DEFAULT_SIZE, 485,
Short.MAX_VALUE));
gl_cPanelFirma.setVerticalGroup(gl_cPanelFirma.createParallelGroup(
Alignment.LEADING)
.addGroup(
gl_cPanelFirma
.createSequentialGroup()
.addComponent(cLabelFirmaGuncelleme,
GroupLayout.DEFAULT_SIZE, 20,
Short.MAX_VALUE)
.addGap(6)
.addComponent(cPanelFirmaEkleme,
GroupLayout.DEFAULT_SIZE, 491,
Short.MAX_VALUE)));
cPanelFirmaEkleme.setLayout(null);
cPanelFirmaEkleme.add(cLabelFirmaIcon);
cPanelFirmaEkleme.add(cLabelFirmaAd);
cPanelFirmaEkleme.add(cLabelFirmaBilgiAd);
cPanelFirmaEkleme.add(cLabelFirmaTelNo);
cPanelFirmaEkleme.add(cTextFieldFirmaTelNo);
cPanelFirmaEkleme.add(cLabelFaksNo);
cPanelFirmaEkleme.add(cTextFieldFaksNo);
cPanelFirmaEkleme.add(JButtonFirmaGuncelle);
cPanelFirmaEkleme.add(JButtonFirmaGeri);
cPanelFirmaEkleme.add(cLabelFirmaAdres);
cPanelFirmaEkleme.add(cScrollPaneFirmaAdres);
cPanelFirmaEkleme.add(cLabelFirmaMail);
cPanelFirmaEkleme.add(cTextFieldFirmaMail);
cPanelFirma.setLayout(gl_cPanelFirma);
GroupLayout groupLayout = new GroupLayout(getContentPane());
groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(
Alignment.LEADING).addGroup(
groupLayout
.createSequentialGroup()
.addGap(10)
.addComponent(cPanelFirma, GroupLayout.DEFAULT_SIZE,
GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(9)));
groupLayout.setVerticalGroup(groupLayout.createParallelGroup(
Alignment.LEADING).addGroup(
groupLayout
.createSequentialGroup()
.addGap(11)
.addComponent(cPanelFirma, GroupLayout.DEFAULT_SIZE,
GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(11)));
getContentPane().setLayout(groupLayout);
initListeners();
firmaBilgileriDoldur(firma);
}
private void firmaBilgileriDoldur(Firma firma) {
cLabelFirmaBilgiAd.setText(firma.getFirmaAd());
if (!firma.getFirmaTelNo().equals(KONTROL)) {
cTextFieldFirmaTelNo.setValue(firma.getFirmaTelNo());
}
if (!firma.getFirmaFaksNo().equals(KONTROL)) {
cTextFieldFaksNo.setValue(firma.getFirmaFaksNo());
}
if (firma.getFirmaMail().equals(KONTROL)) {
cTextFieldFirmaMail.setText("");
} else {
cTextFieldFirmaMail.setText(firma.getFirmaMail());
}
cTextAreaFirmaAdres.setText(firma.getFirmaAdres());
}
private void initListeners() {
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
firmaEkraniSetEnable();
}
});
JButtonFirmaGuncelle.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
firmaGuncelleClicked(firma);
}
});
JButtonFirmaGeri.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
firmaEkraniSetEnable();
}
});
cTextAreaFirmaAdres.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent arg0) {
if (cTextAreaFirmaAdres.getText().equals(KONTROL)) {
cTextAreaFirmaAdres.setText("");
}
}
@Override
public void focusLost(FocusEvent e) {
if (firma.getFirmaAdres().equals(KONTROL)) {
if (cTextAreaFirmaAdres.getText().equals("")) {
cTextAreaFirmaAdres.setText(KONTROL);
}
}
}
});
cTextFieldFirmaTelNo.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent arg0) {
if (arg0.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
if (cTextFieldFirmaTelNo.getText().equals(NO_CONTROL)) {
cTextFieldFirmaTelNo.setValue(null);
}
}
}
});
cTextFieldFaksNo.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent arg0) {
if (arg0.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
if (cTextFieldFaksNo.getText().equals(NO_CONTROL)) {
cTextFieldFaksNo.setValue(null);
}
}
}
});
}
protected void firmaEkraniSetEnable() {
PSAFirmaEkrani.getInstance(1).setEnabled(true);
instance.dispose();
}
protected void firmaGuncelleClicked(Firma firma) {
if (!compareFirmaInfo(firma)) {
Utils.showMessage("Firma ile ilgi güncellenecek bilgi bulunmamaktadır.");
return;
}
String firmaMail = cTextFieldFirmaMail.getText().trim();
if (firmaMail.length() > 0 && !MailRegex.validate(firmaMail)) {
Utils.showMessage("E-posta adresinin doğruluğunu kontrol ediniz.");
return;
}
String firmaAdres = cTextAreaFirmaAdres.getText().trim();
Object firmaTelNo = cTextFieldFirmaTelNo.getValue();
Object firmaFaksNo = cTextFieldFaksNo.getValue();
if (firmaTelNo != null) {
firma.setFirmaTelNo(firmaTelNo.toString());
} else {
firma.setFirmaTelNo("-");
}
if (firmaFaksNo != null) {
firma.setFirmaFaksNo(firmaFaksNo.toString());
} else {
firma.setFirmaFaksNo("-");
}
if (firmaMail.length() > 0) {
firma.setFirmaMail(firmaMail);
} else {
firma.setFirmaMail("-");
}
if (firmaAdres.length() > 0) {
firma.setFirmaAdres(firmaAdres);
} else {
firma.setFirmaAdres("-");
}
boolean control = HibernateManager.getInstance().update(firma);
if (control) {
AutoClosingMessageDialog.showAutoClosingMessageDialog(
"Sistemdeki firma bilgisi güncellenmiştir.", 2);
firmaEkraniSetEnable();
}
}
private boolean compareFirmaInfo(Firma firma) {
String mail = null;
if (cTextFieldFirmaMail.getText().equals("")) {
mail = "-";
} else {
mail = cTextFieldFirmaMail.getText();
}
Object faksNo = cTextFieldFaksNo.getValue();
Object telNo = cTextFieldFirmaTelNo.getValue();
if (faksNo == null) {
faksNo = "-";
}
if (telNo == null) {
telNo = "-";
}
if (firma.getFirmaTelNo().equals(String.valueOf(telNo))
&& firma.getFirmaFaksNo().equals(String.valueOf(faksNo))
&& firma.getFirmaMail().equals(mail)
&& firma.getFirmaAdres().equals(cTextAreaFirmaAdres.getText())) {
return false;
}
return true;
}
}