gt:         m_jRefund.setMargin(new java.awt.Insets(8, 14, 8, 14));
context: //    Openbravo POS is a point of sales application designed for touch screens.
  //    Copyright (C) 2007-2009 Openbravo, S.L.
  //    http://www.openbravo.com/product/pos
  //
  //    This file is part of Openbravo POS.
  //
  //    Openbravo POS is free software: you can redistribute it and/or modify
  //    it under the terms of the GNU General Public License as published by
  //    the Free Software Foundation, either version 3 of the License, or
  //    (at your option) any later version.
  //
  //    Openbravo POS is distributed in the hope that it will be useful,
  //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  //    GNU General Public License for more details.
  //
  //    You should have received a copy of the GNU General Public License
  //    along with Openbravo POS.  If not, see <http://www.gnu.org/licenses/>.
  
  package com.openbravo.pos.sales;
  
  import com.openbravo.pos.ticket.TicketInfo;
  import com.openbravo.pos.ticket.TicketLineInfo;
  import java.awt.*;
  import java.util.ArrayList;
  import javax.swing.*;
  import com.openbravo.data.gui.MessageInf;
  import com.openbravo.pos.forms.AppView; 
  import com.openbravo.pos.forms.AppLocal; 
  import com.openbravo.pos.printer.*;
  import com.openbravo.basic.BasicException;
  import com.openbravo.data.gui.JMessageDialog;
  import com.openbravo.pos.customers.DataLogicCustomers;
  import com.openbravo.pos.scripting.ScriptEngine;
  import com.openbravo.pos.scripting.ScriptException;
  import com.openbravo.pos.scripting.ScriptFactory;
  import com.openbravo.pos.forms.DataLogicSystem;
  import com.openbravo.pos.panels.JTicketsFinder;
  import com.openbravo.pos.ticket.FindTicketsInfo;
  
  public class JTicketsBagTicket extends JTicketsBag {
      
      private DataLogicSystem m_dlSystem = null;
      protected DataLogicCustomers dlCustomers = null;
  
      private DeviceTicket m_TP;    
      private TicketParser m_TTP;    
      private TicketParser m_TTP2; 
      
      private TicketInfo m_ticket;
      private TicketInfo m_ticketCopy;
      
      private JTicketsBagTicketBag m_TicketsBagTicketBag;
      
      private JPanelTicketEdits m_panelticketedit;
  
      /** Creates new form JTicketsBagTicket */
      public JTicketsBagTicket(AppView app, JPanelTicketEdits panelticket) {
          
          super(app, panelticket);
          m_panelticketedit = panelticket; 
          m_dlSystem = (DataLogicSystem) m_App.getBean("com.openbravo.pos.forms.DataLogicSystem");
          dlCustomers = (DataLogicCustomers) m_App.getBean("com.openbravo.pos.customers.DataLogicCustomers");
          
          // Inicializo la impresora...
          m_TP = new DeviceTicket();
     
          // Inicializo el parser de documentos de ticket
          m_TTP = new TicketParser(m_TP, null, m_dlSystem); // para visualizar el ticket
          m_TTP2 = new TicketParser(m_App.getDeviceTicket(), app.getDeviceDrawer(), m_dlSystem); // para imprimir el ticket
          
          initComponents();
          
          m_TicketsBagTicketBag = new JTicketsBagTicketBag(this);
          
          m_jTicketEditor.addEditorKeys(m_jKeys);
          
          // Este deviceticket solo tiene una impresora, la de pantalla
          m_jPanelTicket.add(m_TP.getDevicePrinter("1").getPrinterComponent(), BorderLayout.CENTER);
      }
      
      public void activate() {
          
          // precondicion es que no tenemos ticket activado ni ticket en el panel
          
          m_ticket = null;
          m_ticketCopy = null;
          
          printTicket();        
          
          m_jTicketEditor.reset();
          m_jTicketEditor.activate();
          
          m_panelticketedit.setActiveTicket(null, null);
  
          jrbSales.setSelected(true);
          
          m_jEdit.setVisible(m_App.getAppUserView().getUser().hasPermission("sales.EditTicket"));
          m_jRefund.setVisible(m_App.getAppUserView().getUser().hasPermission("sales.RefundTicket"));
          m_jPrint.setVisible(m_App.getAppUserView().getUser().hasPermission("sales.PrintTicket"));
               
          // postcondicion es que tenemos ticket activado aqui y ticket en el panel
      }
      
      public boolean deactivate() {
          
          // precondicion es que tenemos ticket activado aqui y ticket en el panel        
          m_ticket = null;   
          m_ticketCopy = null;
          return true;       
          // postcondicion es que no tenemos ticket activado ni ticket en el panel
      }
      
      public void deleteTicket() {
          
          if (m_ticketCopy != null) {           
              // Para editar borramos el ticket anterior
              try {               
                  m_dlSales.deleteTicket(m_ticketCopy, m_App.getInventoryLocation());
              } catch (BasicException eData) {
                  MessageInf msg = new MessageInf(MessageInf.SGN_NOTICE, AppLocal.getIntString("message.nosaveticket"), eData);
                  msg.show(this);                
              }            
          }
          
          m_ticket = null;
          m_ticketCopy = null;
          resetToTicket(); 
      }    
          
      public void canceleditionTicket() {
          
          m_ticketCopy = null;
          resetToTicket();
      }    
      
      private void resetToTicket() {       
          printTicket();
          m_jTicketEditor.reset();
          m_jTicketEditor.activate();
          m_panelticketedit.setActiveTicket(null, null); 
      }
      
      protected JComponent getBagComponent() {
          return m_TicketsBagTicketBag;
      }
      
      protected JComponent getNullComponent() {
          return this;
      }
        
      private void readTicket(int iTicketid, int iTickettype) {
          
          try {
              TicketInfo ticket = (iTicketid==-1) 
                  ? m_dlSales.loadTicket(iTickettype,  m_jTicketEditor.getValueInteger())
                  : m_dlSales.loadTicket(iTickettype, iTicketid) ;
  
              if (ticket == null) {
                  MessageInf msg = new MessageInf(MessageInf.SGN_WARNING, AppLocal.getIntString("message.notexiststicket"));
                  msg.show(this);
              } else {
                  m_ticket = ticket;
                  m_ticketCopy = null; // se asigna al pulsar el boton de editar o devolver
                  printTicket();
              }
              
          } catch (BasicException e) {
              MessageInf msg = new MessageInf(MessageInf.SGN_WARNING, AppLocal.getIntString("message.cannotloadticket"), e);
              msg.show(this);
          }
          
          m_jTicketEditor.reset();
          m_jTicketEditor.activate();
      }
      
      private void printTicket() {
          
          // imprimo m_ticket
          
          try {
              m_jEdit.setEnabled(
                      m_ticket != null
                      && (m_ticket.getTicketType() == TicketInfo.RECEIPT_NORMAL || m_ticket.getTicketType() == TicketInfo.RECEIPT_REFUND)
                      && m_dlSales.isCashActive(m_ticket.getActiveCash()));
          } catch (BasicException e) {
              m_jEdit.setEnabled(false);
          }
          m_jRefund.setEnabled(m_ticket != null && m_ticket.getTicketType() == TicketInfo.RECEIPT_NORMAL);
          m_jPrint.setEnabled(m_ticket != null);
          
          // Este deviceticket solo tiene una impresora, la de pantalla
          m_TP.getDevicePrinter("1").reset();
          
          if (m_ticket == null) {
              m_jTicketId.setText(null);            
          } else {
              m_jTicketId.setText(m_ticket.getName());
              
              try {
                  ScriptEngine script = ScriptFactory.getScriptEngine(ScriptFactory.VELOCITY);
                  script.put("ticket", m_ticket);
                  m_TTP.printTicket(script.eval(m_dlSystem.getResourceAsXML("Printer.TicketPreview")).toString());
              } catch (ScriptException e) {
                  MessageInf msg = new MessageInf(MessageInf.SGN_WARNING, AppLocal.getIntString("message.cannotprintticket"), e);
                  msg.show(this);
              } catch (TicketPrinterException eTP) {
                  MessageInf msg = new MessageInf(MessageInf.SGN_WARNING, AppLocal.getIntString("message.cannotprintticket"), eTP);
                  msg.show(this);
              }
          }
      }
  
      /** This method is called from within the constructor to
       * initialize the form.
       * WARNING: Do NOT modify this code. The content of this method is
       * always regenerated by the Form Editor.
       */
      // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
      private void initComponents() {
          java.awt.GridBagConstraints gridBagConstraints;
  
          buttonGroup1 = new javax.swing.ButtonGroup();
          m_jOptions = new javax.swing.JPanel();
          m_jButtons = new javax.swing.JPanel();
          m_jTicketId = new javax.swing.JLabel();
          jButton2 = new javax.swing.JButton();
          m_jEdit = new javax.swing.JButton();
          m_jRefund = new javax.swing.JButton();
          m_jPrint = new javax.swing.JButton();
          jPanel2 = new javax.swing.JPanel();
          m_jPanelTicket = new javax.swing.JPanel();
          jPanel3 = new javax.swing.JPanel();
          jPanel4 = new javax.swing.JPanel();
          m_jKeys = new com.openbravo.editor.JEditorKeys();
          jPanel5 = new javax.swing.JPanel();
          jButton1 = new javax.swing.JButton();
          m_jTicketEditor = new com.openbravo.editor.JEditorIntegerPositive();
          jPanel1 = new javax.swing.JPanel();
          jrbSales = new javax.swing.JRadioButton();
          jrbRefunds = new javax.swing.JRadioButton();
  
          setLayout(new java.awt.BorderLayout());
  
          m_jOptions.setLayout(new java.awt.BorderLayout());
  
          m_jButtons.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));
  
          m_jTicketId.setBackground(java.awt.Color.white);
          m_jTicketId.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
          m_jTicketId.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createLineBorder(javax.swing.UIManager.getDefaults().getColor("Button.darkShadow")), javax.swing.BorderFactory.createEmptyBorder(1, 4, 1, 4)));
          m_jTicketId.setOpaque(true);
          m_jTicketId.setPreferredSize(new java.awt.Dimension(160, 25));
          m_jTicketId.setRequestFocusEnabled(false);
          m_jButtons.add(m_jTicketId);
  
          jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/search.png"))); // NOI18N
          jButton2.setText(AppLocal.getIntString("label.search")); // NOI18N
          jButton2.setFocusPainted(false);
          jButton2.setFocusable(false);
          jButton2.setMargin(new java.awt.Insets(8, 14, 8, 14));
          jButton2.setRequestFocusEnabled(false);
          jButton2.addActionListener(new java.awt.event.ActionListener() {
              public void actionPerformed(java.awt.event.ActionEvent evt) {
                  jButton2ActionPerformed(evt);
              }
          });
          m_jButtons.add(jButton2);
  
          m_jEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/edit.png"))); // NOI18N
          m_jEdit.setText(AppLocal.getIntString("button.edit")); // NOI18N
          m_jEdit.setFocusPainted(false);
          m_jEdit.setFocusable(false);
          m_jEdit.setMargin(new java.awt.Insets(8, 14, 8, 14));
          m_jEdit.setRequestFocusEnabled(false);
          m_jEdit.addActionListener(new java.awt.event.ActionListener() {
              public void actionPerformed(java.awt.event.ActionEvent evt) {
                  m_jEditActionPerformed(evt);
              }
          });
          m_jButtons.add(m_jEdit);
  
          m_jRefund.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/inbox.png"))); // NOI18N
          m_jRefund.setText(AppLocal.getIntString("button.refund")); // NOI18N
          m_jRefund.setFocusPainted(false);
          m_jRefund.setFocusable(false);
  