package barcode.gui;
import gui.PSAAnaEkran;
import hibernateSwingApi.component.CComboBox;
import hibernateSwingApi.component.CTextField;
import hibernateSwingApi.hibernate.HibernateManager;
import hibernateSwingApi.utils.Utils;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.print.PrintService;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
import net.sourceforge.barbecue.Barcode;
import net.sourceforge.barbecue.BarcodeException;
import net.sourceforge.barbecue.BarcodeFactory;
import pojos.UrunStok;
import service.LabelTasarimPaneli;
import service.PSAUtil;
import barcode.pojos.Etiket;
import barcode.pojos.EtiketLabel;
import barcode.pojos.PrintProperty;
public class PSAEtiketBasimEkrani {
private JFrame frame;
private CComboBox<Etiket> cComboBoxEtiketSecim;
private JButton JButtonEtiketBas;
private JSpinner JSpinnerAdet;
private JLabel JLabelEtiketSeciniz;
private JButton JButtonGeriDon;
private JLabel JLabelAdet;
private JButton cButtonEtiketOnizleme;
private JPanel JPanelIcerik;
private List<EtiketLabel> labelList;
private List<CTextField> fieldList = new ArrayList<CTextField>();
private Etiket etiket;
private UrunStok urun;
private PrintProperty property;
private String barkod;
private String barkodUrun;
private JScrollPane scrollPane;
public UrunStok getUrun() {
return urun;
}
public void setUrun(UrunStok urun) {
this.urun = urun;
}
public JFrame getFrame() {
return frame;
}
public void setFrame(JFrame frame) {
this.frame = frame;
}
public String getBarkod() {
return barkod;
}
public void setBarkod(String barkod) {
this.barkod = barkod;
}
public PSAEtiketBasimEkrani(PrintProperty property) {
this.property = property;
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(0, 0, 515, 360);
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.setTitle("ETİKET YAZDIR");
frame.setResizable(false);
frame.setIconImage(PSAUtil.getImageIconViaRelativePath(
"picture/psaIcon.png").getImage());
cComboBoxEtiketSecim = new CComboBox<Etiket>(Etiket.class);
cComboBoxEtiketSecim.setBounds(26, 50, 260, 30);
JLabelEtiketSeciniz = new JLabel();
JLabelEtiketSeciniz.setBounds(26, 31, 271, 19);
JLabelEtiketSeciniz.setFont(new Font("Tahoma", Font.BOLD, 12));
JLabelEtiketSeciniz.setHorizontalAlignment(SwingConstants.CENTER);
JLabelEtiketSeciniz.setText("ETİKET SEÇİNİZ");
JButtonEtiketBas = new JButton();
JButtonEtiketBas.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/yazdir.png"));
JButtonEtiketBas.setBounds(292, 130, 207, 50);
JButtonEtiketBas.setFont(new Font("Tahoma", Font.BOLD, 14));
JButtonEtiketBas.setText("ETİKET YAZDIR");
JButtonEtiketBas.setEnabled(false);
JButtonGeriDon = new JButton();
JButtonGeriDon.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/geriButonu.png"));
JButtonGeriDon.setBounds(292, 252, 207, 50);
JButtonGeriDon.setText("GERİ DÖN");
JButtonGeriDon.setFont(new Font("Tahoma", Font.BOLD, 14));
JSpinnerAdet = new JSpinner();
JSpinnerAdet.setBounds(430, 86, 69, 33);
JSpinnerAdet.setModel(new SpinnerNumberModel(property.getSatir()
* property.getSutun(), new Integer(1), null, new Integer(
property.getSatir() * property.getSutun())));
JLabelAdet = new JLabel();
JLabelAdet.setFont(new Font("Tahoma", Font.BOLD, 12));
JLabelAdet.setBounds(296, 86, 116, 26);
JLabelAdet.setText("ETİKET ADEDİ");
cButtonEtiketOnizleme = new JButton();
cButtonEtiketOnizleme.setIcon(PSAUtil
.getImageIconViaRelativePath("picture/onizleme.png"));
cButtonEtiketOnizleme.setBounds(292, 191, 207, 50);
cButtonEtiketOnizleme.setText("ETİKET ÖNİZLEME");
cButtonEtiketOnizleme.setFont(new Font("Tahoma", Font.BOLD, 12));
cButtonEtiketOnizleme.setEnabled(false);
frame.getContentPane().setLayout(null);
frame.getContentPane().add(JLabelEtiketSeciniz);
frame.getContentPane().add(cComboBoxEtiketSecim);
frame.getContentPane().add(JLabelAdet);
frame.getContentPane().add(JSpinnerAdet);
frame.getContentPane().add(JButtonGeriDon);
frame.getContentPane().add(JButtonEtiketBas);
frame.getContentPane().add(cButtonEtiketOnizleme);
scrollPane = new JScrollPane();
scrollPane.setBounds(26, 86, 260, 216);
scrollPane.setBorder(new LineBorder(new Color(0, 0, 0), 2, false));
frame.getContentPane().add(scrollPane);
JPanelIcerik = new JPanel();
scrollPane.setViewportView(JPanelIcerik);
GridBagLayout gbl_JPanelIcerik = new GridBagLayout();
gbl_JPanelIcerik.columnWidths = new int[] { 99, 130 };
gbl_JPanelIcerik.columnWeights = new double[] { Double.MIN_VALUE };
JPanelIcerik.setLayout(gbl_JPanelIcerik);
cComboBoxEtiketSecim.refresh();
initListeners();
}
private void initListeners() {
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {
anaEkranSetEnable();
}
});
cButtonEtiketOnizleme.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
showPreview();
}
});
JButtonGeriDon.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
anaEkranSetEnable();
frame.dispose();
}
});
cComboBoxEtiketSecim.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent arg0) {
etiketSelected();
}
});
JButtonEtiketBas.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
printerHazirlaVeBas();
}
});
}
protected void anaEkranSetEnable() {
PSAAnaEkran.getInstance(null).setEnabled(true);
}
@SuppressWarnings("unchecked")
protected void etiketSelected() {
fieldList = new ArrayList<CTextField>();
UrunStok urunOlustur = new UrunStok();
if (urun != null) {
urunOlustur = urun;
}
JPanelIcerik.removeAll();
etiket = (Etiket) cComboBoxEtiketSecim.getSelectedItem();
labelList = (List<EtiketLabel>) HibernateManager.getInstance()
.getEntitysList(etiket, "labelList");
int k = 0;
for (int i = 0; i < labelList.size(); i++) {
JLabel label = new JLabel();
label.setText(labelList.get(i).getName());
GridBagConstraints gbc_label = new GridBagConstraints();
gbc_label.insets = new Insets(0, 0, 5, 0);
gbc_label.anchor = GridBagConstraints.WEST;
gbc_label.gridx = 0;
gbc_label.gridy = k;
CTextField field = new CTextField(30);
GridBagConstraints gbc_textField_1 = new GridBagConstraints();
gbc_label.insets = new Insets(0, 0, 5, 0);
gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
gbc_textField_1.gridx = 1;
gbc_textField_1.gridy = k;
String labelName = label.getText();
switch (labelName) {
case "sirketAdi":
field.setText(labelList.get(i).getText());
break;
case "bosLabel-1":
field.setText(labelList.get(i).getText());
break;
case "bosLabel-2":
field.setText(labelList.get(i).getText());
break;
case "bosLabel-3":
field.setText(labelList.get(i).getText());
break;
case "bosLabel-4":
field.setText(labelList.get(i).getText());
break;
case "kod":
field.setText(urunOlustur.getKod());
break;
case "sezon":
field.setText(urunOlustur.getSezon());
break;
case "renk":
field.setText(urunOlustur.getRenkAd());
break;
case "grup":
field.setText(urunOlustur.getGrup());
break;
case "model":
field.setText(urunOlustur.getModel());
break;
case "beden":
field.setText(urunOlustur.getBeden());
break;
case "cinsiyet":
field.setText(urunOlustur.getCinsiyet());
break;
case "marka":
field.setText(urunOlustur.getMarka());
break;
case "pesinSatisFiyati":
double a = urunOlustur.getPesinSatisFiyati();
String[] parts = (String.valueOf(a)).split("\\.");
String b = "";
if (parts[1].equals("0")) {
b = parts[0] + ".00";
} else {
if (parts[1].length() == 1) {
b = parts[0] + "." + parts[1] + "0";
} else {
b = parts[0] + "." + parts[1];
}
}
field.setText(b);
break;
case "taksitliSatisFiyati":
double c = urunOlustur.getTaksitliSatisFiyati();
String[] parts1 = (String.valueOf(c)).split("\\.");
String d = "";
if (parts1[1].equals("0")) {
d = parts1[0] + ".00";
} else {
if (parts1[1].length() == 1) {
d = parts1[0] + "." + parts1[1] + "0";
} else {
d = parts1[0] + "." + parts1[1];
}
}
field.setText(d);
break;
default:
break;
}
fieldList.add(field);
JPanelIcerik.add(label, gbc_label);
JPanelIcerik.add(field, gbc_textField_1);
JPanelIcerik.updateUI();
k++;
}
JButtonEtiketBas.setEnabled(true);
cButtonEtiketOnizleme.setEnabled(true);
}
protected void showPreview() {
JPanel panel = etiketPngHazirla();
if (panel == null) {
return;
}
JFrame framePreview = new JFrame("ETİKET ÖNİZLEME");
framePreview.setIconImage(PSAUtil.getImageIconViaRelativePath(
"picture/psaIcon.png").getImage());
framePreview.setBounds(100, 100, panel.getWidth() + 37,
panel.getHeight() + 86);
framePreview.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
framePreview.getContentPane().setLayout(null);
framePreview.setResizable(false);
panel.setBounds(10, 36, panel.getWidth(), panel.getHeight());
framePreview.getContentPane().add(panel);
JLabel etiketBoyut = new JLabel("Etiket Boyutu "
+ LabelTasarimPaneli.pxToMm(panel.getWidth()) + "*"
+ LabelTasarimPaneli.pxToMm(panel.getHeight()) + "mm");
etiketBoyut.setFont(new Font("Tahoma", Font.BOLD, 14));
etiketBoyut.setHorizontalAlignment(SwingConstants.CENTER);
etiketBoyut.setBounds(10, 11, panel.getWidth(), 14);
framePreview.getContentPane().add(etiketBoyut);
framePreview.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
frame.setEnabled(true);
}
});
frame.setEnabled(false);
framePreview.setVisible(true);
}
@SuppressWarnings("unchecked")
public JPanel etiketPngHazirla() {
int satir = property.getSatir();
int sutun = property.getSutun();
int gen = property.getGen();
int yuks = property.getYuks();
int sagbosluk = property.getSagbosluk();
int solbosluk = property.getSolbosluk();
if (urun != null) {
barkodUrun = String.valueOf(urun.getBarkod());
}
gen = gen - solbosluk - sagbosluk;
JPanel onizleme = new JPanel();
JPanel panel = null;
onizleme.setBackground(new Color(192, 192, 192));
onizleme.setBounds(sagbosluk + LabelTasarimPaneli.mmToPx(1),
LabelTasarimPaneli.mmToPx(2), gen, yuks);
onizleme.setLayout(null);
final int tekEtiketGen = gen / sutun;
final int tekEtiketYuks = yuks / satir;
int x = 0;
int y = 0;
for (int i = 0; i < satir; i++) {
x = 0;
for (int j = 0; j < sutun; j++) {
panel = new JPanel();
panel.setBorder(new LineBorder(new Color(255, 255, 255), 2,
true));
panel.setBackground(Color.WHITE);
panel.setBounds(x, y, tekEtiketGen, tekEtiketYuks);
panel.setLayout(null);
Rectangle barkodPanelBounds = etiket.getBounds();
if (barkodPanelBounds != null) {
Rectangle barkodBounds = etiket.getBarkod().getBounds();
Barcode barkod = null;
try {
if (barkodUrun != null) {
barkod = BarcodeFactory.createCode39(barkodUrun,
false);
} else {
barkod = BarcodeFactory.createCode39("123456789",
false);
}
} catch (BarcodeException e) {
e.printStackTrace();
}
barkod.setSize(barkodBounds.getSize());
barkod.setBarHeight(20);
barkod.setBarWidth(1);
barkod.setFont(new Font("Times New Roman", Font.BOLD, 13));
JPanel barkodPanel = new JPanel();
barkodPanel.setBackground(Color.white);
barkodPanel.setLayout(null);
barkodPanel.setBounds(barkodPanelBounds);
barkodPanel.add(barkod);
panel.add(barkodPanel);
}
labelList = (List<EtiketLabel>) HibernateManager.getInstance()
.getEntitysList(etiket, "labelList");
for (int k = 0; k < labelList.size(); k++) {
EtiketLabel label = labelList.get(k);
JLabel label1 = label.getLabel();
JLabel part1 = null;
JLabel smallFontPart2 = null;
label1.setText(fieldList.get(k).getText());
FontMetrics metrics = label1.getFontMetrics(label1
.getFont());
label1.setSize(metrics.stringWidth(label1.getText()) + 5,
metrics.getHeight());
if (label1.getToolTipText() != null) {
if (label1.getToolTipText().equals("kampanya")) {
final int width = label1.getWidth();
final int height = label1.getHeight();
final Color foreGround = label1.getForeground();
JLabel crossedLabel = new JLabel(label1.getText()) {
/**
*
*/
private static final long serialVersionUID = -5749785272368634216L;
@Override
public void paintComponent(Graphics g) {
setForeground(foreGround);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(foreGround);
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d
.setStroke(new BasicStroke(2,
BasicStroke.CAP_ROUND,
BasicStroke.JOIN_BEVEL));
g2d.drawLine(2, 2, width - 2, height - 2);
g2d.drawLine(2, height - 2, width - 2, 2);
super.paintComponent(g2d);
}
};
crossedLabel.setBounds(label1.getBounds());
crossedLabel.setFont(label1.getFont());
crossedLabel.setName(label1.getName());
crossedLabel.repaint();
label1 = crossedLabel;
}
}
if (label1.getName().equals("pesinSatisFiyati")
|| label1.getName().equals("taksitliSatisFiyati")) {
String[] parts = (String.valueOf(label1.getText()))
.split("\\.");
part1 = new JLabel();
smallFontPart2 = new JLabel();
part1.setFont(label1.getFont());
part1.setText(parts[0]);
metrics = part1.getFontMetrics(part1.getFont());
int labelWidth = LabelTasarimPaneli.pxToMm(metrics
.stringWidth(part1.getText())) + 1;
int mmWidth = LabelTasarimPaneli.mmToPx(labelWidth);
part1.setBounds(label1.getX(), label1.getY(), mmWidth,
label1.getHeight());
part1.setForeground(label1.getForeground());
part1.setBackground(label1.getBackground());
String family = label1.getFont().getFamily();
int size = label1.getFont().getSize();
int style = label1.getFont().getStyle();
smallFontPart2
.setFont(new Font(family, style, size / 2));
smallFontPart2.setText("." + parts[1]);
smallFontPart2.setForeground(label1.getForeground());
smallFontPart2.setBackground(label1.getBackground());
metrics = smallFontPart2.getFontMetrics(smallFontPart2
.getFont());
int labelWidth1 = LabelTasarimPaneli.pxToMm(metrics
.stringWidth(smallFontPart2.getText())) + 1;
int labelHeight1 = LabelTasarimPaneli.pxToMm(metrics
.getHeight() + 4);
int mmWidth1 = LabelTasarimPaneli.mmToPx(labelWidth1);
int mmHeight = LabelTasarimPaneli.mmToPx(labelHeight1);
smallFontPart2.setBounds(
part1.getX() + part1.getWidth(), part1.getY(),
mmWidth1, mmHeight);
try {
JLabel piJLabel = new JLabel();
InputStream is = PSAEtiketBasimEkrani.class
.getResourceAsStream("/AbakuTLSymSans-Regular.ttf");
Font abakuFont = Font.createFont(
Font.TRUETYPE_FONT, is).deriveFont(
label1.getFont().getStyle(),
label1.getFont().getSize());
piJLabel.setFont(abakuFont);
piJLabel.setBounds(smallFontPart2.getX()
+ smallFontPart2.getWidth() + 2,
label1.getY() - 1, label1.getHeight(),
label1.getHeight());
piJLabel.setForeground(label1.getForeground());
piJLabel.setBackground(label1.getBackground());
piJLabel.setText("¨");
panel.add(piJLabel);
} catch (IOException e) {
Utils.showErrorMessage("TL simge dosyası bulunamadı !");
} catch (FontFormatException e) {
e.printStackTrace();
}
}
if (part1 == null && smallFontPart2 == null) {
panel.add(label1);
} else {
panel.add(part1);
panel.add(smallFontPart2);
}
panel.updateUI();
}
onizleme.add(panel);
x += tekEtiketGen;
}
y += tekEtiketYuks + LabelTasarimPaneli.mmToPx(1);
}
for (int i = 0; i < fieldList.size(); i++) {
if (fieldList.get(i).getText().equals("-")
|| fieldList.get(i).getText().length() <= 0) {
String[] options = { "Devam Et", "İptal" };
if (JOptionPane.showOptionDialog(null,
"Etikette boş alan bulunmaktadır !", null,
JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE,
null, options, options[1]) != 0) {
onizleme = null;
}
break;
}
}
return onizleme;
}
public void printerHazirlaVeBas() {
printCard();
}
public void printCard() {
JPanel panel = etiketPngHazirla();
if (panel == null) {
return;
}
PrinterJob printJob = PrinterJob.getPrinterJob();
PrintService printService = getPrintService(property.getService());
PageFormat format = new PageFormat();
format.setOrientation(PageFormat.PORTRAIT);
Paper paper = format.getPaper();
paper.setImageableArea(0, 0, panel.getWidth(), panel.getHeight());
format.setPaper(paper);
printJob.setPrintable(new Printer(panel), format);
int printCount = (int) JSpinnerAdet.getValue() / property.getSutun();
for (int i = 0; i < printCount; i++) {
try {
printJob.setPrintService(printService);
printJob.print();
} catch (PrinterException e) {
e.printStackTrace();
}
}
}
private static PrintService getPrintService(String printerName) {
PrintService service = null;
PrintService[] services = PrinterJob.lookupPrintServices();
for (int index = 0; service == null && index < services.length; index++) {
if (services[index].getName().equalsIgnoreCase(printerName)) {
service = services[index];
}
}
return service;
}
public static class Printer implements Printable {
final JPanel comp;
public Printer(JPanel comp) {
this.comp = comp;
}
@Override
public int print(Graphics g, PageFormat format, int page_index)
throws PrinterException {
if (page_index > 0) {
return Printable.NO_SUCH_PAGE;
}
Dimension dim = comp.getSize();
double cHeight = dim.getHeight();
double cWidth = dim.getWidth();
double pHeight = format.getImageableHeight();
double pWidth = format.getImageableWidth();
double pXStart = format.getImageableX();
double pYStart = format.getImageableY();
double xRatio = pWidth / cWidth;
double yRatio = pHeight / cHeight;
comp.setFont(new Font("Times New Roman", Font.BOLD, 13));
Graphics2D g2 = (Graphics2D) g;
g2.translate(pXStart, pYStart);
g2.scale(xRatio, yRatio);
comp.paint(g2);
return Printable.PAGE_EXISTS;
}
}
}