#encoding: UTF-8
#    This file is part of Rmldonkey.
#    Rmldonkey 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.
#    Rmldonkey 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 Rmldonkey.  If not, see <http://www.gnu.org/licenses/>.
#    Copyright 2009-2014, Manuel Ramos Caro manuelramoscaro@yahoo.es

require 'bigdecimal'

import java.awt.Component
import java.awt.Color
import java.awt.BorderLayout
import java.awt.Font
import java.awt.Window
import java.awt.Image
import java.awt.Toolkit
import java.awt.Dimension
import java.awt.GridLayout
import java.awt.AlphaComposite

import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.awt.event.WindowEvent
import java.awt.event.WindowListener
import java.awt.event.MouseAdapter
import java.awt.event.ComponentListener
import java.awt.event.MouseEvent
import java.awt.event.MouseListener
import java.awt.event.MouseMotionListener

import javax.swing.BorderFactory
import javax.swing.BoxLayout
import javax.swing.ImageIcon
import javax.swing.JFrame
import javax.swing.JLabel
import javax.swing.JPanel
import javax.swing.Timer
import javax.swing.JComponent
import javax.swing.UIManager
import javax.swing.event.ChangeEvent
import javax.swing.event.ChangeListener
import javax.swing.event.ListSelectionListener
import javax.swing.JOptionPane
import javax.swing.JScrollPane
import javax.swing.JTable
import javax.swing.JButton
import javax.swing.AbstractCellEditor
import javax.swing.JEditorPane
import javax.swing.JTextPane
import javax.swing.SwingUtilities

import javax.swing.table.DefaultTableModel
import javax.swing.table.AbstractTableModel
import javax.swing.table.TableCellEditor
import javax.swing.table.TableCellRenderer

import javax.swing.event.HyperlinkEvent
import javax.swing.event.HyperlinkListener
import javax.swing.text.html.HTMLEditorKit
import java.io.IOException
import java.net.URI
import java.net.URISyntaxException
import java.net.URL

import java.awt.image.BufferedImage

import org.jfree.chart.ChartFactory
import org.jfree.chart.JFreeChart
import org.jfree.chart.plot.PlotOrientation
import org.jfree.data.xy.XYDataset
import org.jfree.data.xy.XYSeries
import org.jfree.data.xy.XYSeriesCollection
import org.jfree.chart.StandardChartTheme

import org.jfree.data.time.Millisecond
import org.jfree.data.time.TimeSeries
import org.jfree.data.time.TimeSeriesCollection

import java.beans.PropertyChangeEvent
import java.beans.PropertyChangeListener
import javax.swing.SwingWorker

import java.lang.System

class Modelo < DefaultTableModel
  
  def actualiza(datos,columnas)
     matrix = java.util.Vector.new
     columns = java.util.Vector.new
     datos.each do |valor|   
          submatrix = java.util.Vector.new
           i = 0
           valor.each do |subvalor|
               if i == 1 || i == 3
                 escaped = UnicodeEscape.new(subvalor)
                 subvalor = escaped.return_unicode_escape
               end #if i == 1
               submatrix.add_element(subvalor)
               #debug puts "subvalor: " + subvalor.to_s
               i += 1
            end
            matrix.add_element(submatrix)
            #debug puts "submatrix: " + submatrix.to_s
       end       
      
      columnas.each do |valor|
           columns.add_element(valor)  
      end
 
     self.setDataVector(matrix,columns)
  end
  
  def isCellEditable(row,column)
     return false
  end
  
end

class RowListener 
include ListSelectionListener 
  def initialize(tabla)
     @tabla = tabla
     @id = nil
     @previous = nil
  end #def initialize(tabla)
  def valueChanged(event) 
      viewRow = event.getLastIndex
      if event.getValueIsAdjusting && @tabla.getSelectedRow >= 0
         viewRow = @tabla.getSelectedRow
      end
      if $option == 1
         $option = 11
         $id =  @tabla.getValueAt(viewRow,0).to_s[2..5].to_i
         if @tabla.getValueAt(viewRow,7).to_s =~ /Paused/ 
            $paused = true
         else
            $paused = false
         end #if @tabla.getValueAt(viewRow,8).to_s =~ /Paused/ 
         $size = @tabla.getValueAt(viewRow,4).to_s
         $downloaded = @tabla.getValueAt(viewRow,3).to_s
         $priority = @tabla.getValueAt(viewRow,8).to_s 
      end #if $option == 1
      if $option == 21
            if $flag_download_confirm && $id_download_confirm != @tabla.getValueAt(viewRow,0).to_s[2..5].to_i
               $flag_download_confirm = false
               $id_download_confirm = @tabla.getValueAt(viewRow,0).to_s[2..5].to_i
               @id =  @tabla.getValueAt(viewRow,0).to_s[2..5].to_i
   			   message = JLabel.new
			   message.setText("<html> Download the file?<BR>" + @tabla.getValueAt(viewRow,3).to_s.squeeze(" ").strip + "... </html>")
			   message.setFont(Font.new case RbConfig::CONFIG['host_os'] when /mswin/; "Arial Unicode MS" when /darwin/; "AppleGothic, Arial" end, Font::PLAIN, 12)

               really_download = JOptionPane.showConfirmDialog $content_pane ,message , "Confirmation...",  JOptionPane::YES_NO_OPTION, JOptionPane::QUESTION_MESSAGE
               if  really_download == javax.swing.JOptionPane::YES_OPTION
                   $flag_download_confirm = true
				   $conexion.ctver_descarga(@id.to_s)
               else
                   $flag_download_confirm = true
               end #really_exit == javax.swing.JOptionPane::YES_OPTION
             end  
      end #if $option == 21
      if $option == 41
         text_client_details = String.new 
         capture = false
         
         $conexion.cver_valores_estadistiva_msg.each do | piece |
             if piece.to_s =~ /#{@tabla.getValueAt(viewRow,0).to_s}/ && piece.to_s =~ /Uptime/
                capture = true
                text_client_details += "<table style=\"font-family: Arial; margin-left: 60px;\"><tbody>"
             end #if piece =~ /#{@tabla.getValueAt(viewRow,0).to_s}/
             

             if capture
                if piece.to_s =~ /Uptime/ || piece.to_s =~ /Total/ || piece.to_s =~ /Client Brand/
                   text_client_details += "<tr><td><b><p>"
                   text_client_details += piece.to_s.chomp.gsub("\|","</p></b></td><td><b><p>")
                   text_client_details += "</p></b></td></tr>"
                else
                   text_client_details += "<tr><td>"
                   text_client_details += piece.to_s.chomp.gsub("\|","</td><td>") 
                   text_client_details += "</td></tr>"
                end #if piece.to_s =~ /Uptime/
                if piece.to_s =~ /Total/ && !(piece.to_s =~ /Uptime/)
                   capture = false
                   text_client_details += "</tbody></table>"
                end #if piece =~ /Total/  
             end #if capture
             

         end #$conexion.cver_valores_estadistiva_msg.to_s.each do | piece |
         
         $lista_stats_client_details = JPanel.new
         $lista_stats_client_details.setOpaque false
         
         $lista_stats_client_details.setForeground(Color.white)
         $lista_stats_client_details.setBackground(Color.black)

         $client_details_string = JLabel.new
         text_client_details = "<html><head><style type=\"text/css\">p {color:#659EC7;} </style></head><body>" + text_client_details + "</body></html>"
         $client_details_string.setText(text_client_details)
         $client_details_string.setBounds 0,168,$content_pane.getWidth,$content_pane.getHeight - 168
         $client_details_string.setOpaque false
         $lista_stats_client_details.add($client_details_string)
         
         $lista_stats_client_details_w_scroll = JScrollPane.new($lista_stats_client_details)
         $lista_stats_client_details_w_scroll.getViewport.setOpaque false
         
         
         $option = 42

      end #if $option == 41
      
      if $option == 5
         if $conexion.ctsecciones_grupos(@tabla.getValueAt(viewRow,0).to_s,$conexion.cver_valores_secciones_msg)
                    
            if $conexion.ctopciones

               op_details = $conexion.cver_valores_opciones_msg             
               y = 0
               $initial = 0
               $final = 0
               
               $conexion.cver_valores_secciones_num_msg.split('iii').each do |pos|
                  if y == 1
                     $initial = pos.to_i 
                  else
                     $final   = pos.to_i
                  end #if y == 1  
                  y += 1
               end #$conexion.cver_valores_secciones_num_msg.split('iii').each do |pos|
               
               if $final == 0
                  $final = op_details.length
               end #if $final == 0
               
               $op_details = op_details[$initial..$final]
               
               i = 0
               op_list = Array.new
               $op_details.each do |line|
                    y = 0
                    line.split('=').each do |piece|
                       if y == 1
                         @value = piece
                       end #if y == 1
                       y+=1
                    end #line.split('=').each do |piece|
               
                    y = 0
                    line[4..line.length - 4].split('[').each do |piece|
                       if y == 1
                          z = 0
                          piece.split(']').each do |piece2|
                          if z == 0
                             @op_name = piece2
                          end #if z == 0
                          z += 1
                          end #piece.split(']').each do |piece2|
                       end #if y == 1
                       y += 1
                    end #line[4..line.length - 4].split('[').each do |piece|
                    @op_name = @op_name.strip
                    @value = @value.strip               
                    op_list[i] = ["  " + @op_name.capitalize + " = " + @value]
                    i += 1  
               end #$op_details.each do |line|

               $op_details = op_list
               
               if event.getValueIsAdjusting != true &&  @tabla.getSelectedRow >= 0
                  $option = 51
               end #if event.getValueIsAdjusting != true   
            else
               JOptionPane.showMessageDialog  $content_pane, $conexion.conectatelnet_error_msg.to_s, "Error", JOptionPane::ERROR_MESSAGE
            end #if $conexion.ctopciones  
            
         else
            JOptionPane.showMessageDialog  $content_pane, $conexion.conectatelnet_error_msg.to_s, "Error", JOptionPane::ERROR_MESSAGE
         end #if $conexion.ctsecciones_grupos(line,$conexion.cver_valores_secciones_msg)
      end #if $option == 5     
  end #def valueChanged(event) 
end #class RowListener 

class RowListener_options 
include ListSelectionListener 
  def initialize(tabla)
     @tabla = tabla
     @id = nil
     @previous = nil
  end #def initialize(tabla)
  def valueChanged(event) 
      if event.getValueIsAdjusting && @tabla.getSelectedRow >= 0
         viewRow = @tabla.getSelectedRow
      end
      
      if $option == 51 
         if event.getValueIsAdjusting && 
            @tabla.getValueAt(viewRow,0).to_s =~ /=/ &&
            @tabla.getSelectedRow >= 0
               
               conf_value = @tabla.getValueAt(viewRow,0).to_s 
               i = 0
               conf_value.split('=').each do |piece|
                   if i == 0
                      @op_name = piece
                   else    
                      @value = piece
                   end #if i == 1
                   i+=1
               end #deft_value.split('=').each do |piece|
               
               @op_name = @op_name.strip
               @value = @value.strip
               
               option_change = JOptionPane.showInputDialog  $content_pane, @op_name, "Modify the value...", JOptionPane::INFORMATION_MESSAGE, nil, nil , @value
               if option_change != nil && $safe_cancel
                  if $conexion.ctestblece_opcion(@op_name.downcase,option_change)
                     #JOptionPane.showMessageDialog $content_pane, "Change Performed...", "Information", JOptionPane::INFORMATION_MESSAGE
                     $option = 5
                     $pulsado5 = true
                  else
                     JOptionPane.showMessageDialog  $content_pane, $conexion.conectatelnet_error_msg.to_s, "Error", JOptionPane::ERROR_MESSAGE
                  end #if conexion.ctestblece_opcion(@op_name,option_change)  
               else
                     $safe_cancel = false
                     $option = 5
                     $pulsado5 = true
               end #if option_change != nil
               
         end #event.getValueIsAdjusting && @tabla.getValueAt(viewRow,0).to_s =~ /=/ && @tabla.getSelectedRow >= 0
      end #if $option == 51
  end #def valueChanged(event) 
end #class RowListener_options 

class JTableModelSearch <  AbstractTableModel

   def initialize       
       @columnNames = ["Actions","Actual Searches..."]
       @columnTypes = [String.class, String.class] 
       @data = [["Actions","dato1"]]
   end #initialize
   
   def actualiza(datos)
     matrix = java.util.Vector.new
     datos.each do |valor|   
           submatrix = java.util.Vector.new
           valor.each do |subvalor|
               submatrix.add_element("Delete")
               escaped = UnicodeEscape.new(subvalor)
               subvalor = escaped.return_unicode_escape
               submatrix.add_element(subvalor)
           end #valor.each do |subvalor|
         matrix.add_element(submatrix)
      end #datos.each do |valor|   
      @data = matrix
   end #   def actualiza(datos)

   
   
   def getColumnCount
       return @columnNames.length
   end #def getColumnCount
   
   def getRowCount
       return @data.length
   end #def getRowCount
   
  def getColumnName(columnIndex)
     return @columnNames[columnIndex]
  end #getColumnName(columnIndex)

  def getColumnClass(columnIndex)
     return @columnTypes[columnIndex]
  end #getColumnClass(columnIndex)
  
  def isCellEditable(row, col)
    return true
  end #isCellEditable(row, col)
  
  
  
  def getValueAt(rowIndex,columnIndex)
     return @data[rowIndex][columnIndex]
  end #getValueAt(rowIndex,columnIndex)
end #class JTableModelSearch < AbstractTableModel

class JTableButtonRendererSearch 
include TableCellRenderer

  def getTableCellRendererComponent(table, value, isClicked, hasFocus,row, column)
    border = javax.swing.border.LineBorder.new(Color::BLACK, 1)
    button = JButton.new(value)
    button.setBorder(border)
    table.getColumnModel.getColumn(column).setMinWidth(100)
    table.getColumnModel.getColumn(column).setMaxWidth(100)
    table.getColumnModel.getColumn(column).setWidth(100)
    return button
  end # getTableCellRendererComponent
  
end #class JTableButtonRendererSearch

class JTableButtonMouseListenerSearch < MouseAdapter
  
   def getTable(table)
     @table = table
   end #def getTable(table)
   
   def mouseClicked(e)
        column = @table.getColumnModel().getColumnIndexAtX(e.getX())
        row    = e.getY()/@table.getRowHeight()

        if (row < @table.getRowCount() && row >= 0 && column < @table.getColumnCount() && column >= 0)
                value = @table.getValueAt(row, column)
                if (value.kind_of? JButton)     
                                value.doClick()
                end #if (value.kind_of? JButton)     
        end #if (row < @table.getRowCount() && row >= 0 && column < @table.getColumnCount() && column >= 0)
   end #def mouseClicked(e)
   
 
end #class JTableButtonMouseListenerSearch < MouseAdapter


class JTableButtonEditorSearch < AbstractCellEditor
  include TableCellEditor
  include ActionListener

  EDIT = "edit"

  
  def init
    @button = JButton.new
    @button.setActionCommand(EDIT)
    @button.addActionListener(self)
    @button.setBorderPainted(false)
  end #def init

  def actionPerformed(e)
    if EDIT == e.getActionCommand
       if @columna == 0
           $conexion.ctver_forget(@id[1..5])
           if $conexion.ctlista_busqueda
              cuerpo = Array.new
              i  = 0
              $conexion.ctlista_busqueda_retorno_msg.each do |valor|
                 y = 0
                 subcuerpo = Array.new
                 subcuerpo[y] = valor.to_s
                 cuerpo[i]  = subcuerpo
                 i  +=1
                 y +=1
              end #if $conexion.ctlista_busqueda

              $modelo_lbusq.actualiza(cuerpo)
              $tabla_lbusq.getColumnModel.getColumn(0).setPreferredWidth($content_pane.getWidth/6)
           else
              JOptionPane.showMessageDialog  $content_pane, $conexion.conectatelnet_error_msg.to_s, "Error", JOptionPane::ERROR_MESSAGE
           end  #if $conexion.cestado
       end #if @columna == 0
       if @columna == 1
            #debug puts "ETXO  :::  Fila: " + @fila.to_s + " Columna:  " + @columna.to_s
            #debug puts "identificacion: " + $id
            $option = 21
       end #if @columna == 1   
       fireEditingStopped
    end #if EDIT == e.getActionCommand
  end #def actionPerformed(e)

  def getCellEditorValue
    return " "
  end #def getCellEditorValue

  def getTableCellEditorComponent(table, value, isSelected, row, column)
    @id = table.getValueAt(row, 1)
    $id = @id[1..5]
    @fila = row
    @columna = column
    @value = value
    return @button
  end #getTableCellEditorComponent(table, value, isSelected, row, column)

end #class JTableButtonEditorSearch < AbstractCellEditor


class Canvas < JPanel
  def initialize 
      @details = "0"
  end #def initialize 
  def getDetails(details)
      @details = details
  end #def getDetails(details)

  def paintComponent g
     ### if we have the new chunks format does a translation to show status correctly
     if @details =~ /=/     
        @details_bis = String.new
        i = 0
        @details.split('=').each do |part0|
           if i == 0
              @tot = part0.to_i
           else
              y = 0
              part0.split('+').each do |part1|
                 case
                   when y == 0
                      @details_bis += "0" * ((part1.to_i*100)/@tot)
                   when y == 1
                      @details_bis += "1" * ((part1.to_i*100)/@tot)
                   when y == 2
                      @details_bis += "2" * ((part1.to_i*100)/@tot)
                   when y == 3
                      @details_bis += "3" * ((part1.to_i*100)/@tot)
                 end #case 
                 y += 1
              end #part0.split('+').each do |part1|
           end #if i == 1
           i +=1 
        end #@details.split("=") do |part0|
        @details = @details_bis.reverse
     end #if details =~ /=/       
      
     i = 0
     while i <= @details.length
        case @details[i..i]
           when "0"
             g.setColor Color::RED
           when "1"
             g.setColor Color::GREEN
           when "2"
             g.setColor Color::GREEN
           when "3"
               g.setColor Color::BLUE
        end #case @details[i..i]
           
        g.setComposite AlphaComposite.getInstance AlphaComposite::SRC_OVER,1
        g.fillRect 1 + (5 * i), 1, 2, 20
        i += 1
     end #while i <= @details.length
     
  end #def paintComponent g
    
end #class Canvas < JPanel

class UnicodeEscape 
  def initialize(string)
#debug       puts "previous string_ucode:" + string
       @string = '\\u'+ string.unpack("U*").map{|c|"%04x" %c}.join('\\u')
#debug       puts "transformed ucode string: " + @string.to_s
       @string_out = java.lang.StringBuilder.new
       i = 0
       @string.split('\u').each do |piece|   
            if i > 0   
#debug               puts "char " + i.to_s + " :" + piece  
               @char = java.lang.Character.new java.lang.Integer.parseInt(piece, 16).to_java(:char)
#debug               puts "char " + i.to_s + " :" + @char.to_s
               @string_out.append(@char)   
            end #if i > 0
            i += 1
       end #@string.split('\u').each do |piece|
       
#debug       puts "output string: " + @string_out.toString.to_s
  end #def initialize(string)
  def return_unicode_escape
       return @string_out.toString
  end #def return_unicode_escape
end #class UnicodeEscape


class JTableModelServer <  AbstractTableModel

   def initialize
       @columnNames = ["Type", "Id","Ip","Name","Users","Files","Status","Actions allowed"," ",""]
       @columnTypes = [String.class, String.class,String.class,String.class,String.class,String.class,String.class,String.class,String.class,String.class] 
       @data = [["Type", "Id","Ip","Name","Users","Files","Status","Actions1","Actions2",""]]
   end #initialize
   
   def actualiza(datos)
     matrix = java.util.Vector.new
     datos.each do |valor|   
           submatrix = java.util.Vector.new
           i = 0
           valor.each do |subvalor|
               escaped = UnicodeEscape.new(subvalor)
               subvalor = escaped.return_unicode_escape
               if i != 1  && i != 3 && i != 6 
                  subvalor = "  " + subvalor
               end #if i == 0
               submatrix.add_element(subvalor)
               i += 1
           end #valor.each do |subvalor|
           if submatrix[6] == "Connected" 
              submatrix.add_element("Disconnect")
           else
              submatrix.add_element("Connect")
           end
           #submatrix.add_element("Actions1")
           submatrix.add_element("Remove")
           submatrix.add_element("")
           matrix.add_element(submatrix)
     end #datos.each do |valor|   
     @data = matrix
   end #   def actualiza(datos)

   
   
   def getColumnCount
       return @columnNames.length
   end #def getColumnCount
   
   def getRowCount
       return @data.length
   end #def getRowCount
   
  def getColumnName(columnIndex)
     return @columnNames[columnIndex]
  end #getColumnName(columnIndex)

  def getColumnClass(columnIndex)
     return @columnTypes[columnIndex]
  end #getColumnClass(columnIndex)
  
  def isCellEditable(row, col)
    return true
  end #isCellEditable(row, col)
  
  
  
  def getValueAt(rowIndex,columnIndex)
     return @data[rowIndex][columnIndex]
  end #getValueAt(rowIndex,columnIndex)
end #class JTableModelServer < AbstractTableModel

class JTableButtonRendererServer 
include TableCellRenderer

  def getTableCellRendererComponent(table, value, isClicked, hasFocus,row, column)

    border = javax.swing.border.LineBorder.new(Color::BLACK, 1)
    button = JButton.new(value)
    button.setBorder(border)
    
    table.getColumnModel.getColumn(column).setMinWidth(100)
    table.getColumnModel.getColumn(column).setMaxWidth(100)
    table.getColumnModel.getColumn(column).setWidth(100)
    
    return button
  end # getTableCellRendererComponent
  
end #class JTableButtonRendererServer

class JTableButtonMouseListenerServer < MouseAdapter
  
   def getTable(table)
     @table = table
   end #def getTable(table)
   
   def mouseClicked(e)
        column = @table.getColumnModel().getColumnIndexAtX(e.getX())
        row    = e.getY()/@table.getRowHeight()

        if (row < @table.getRowCount() && row >= 0 && column < @table.getColumnCount() && column >= 0)
                value = @table.getValueAt(row, column)
                if (value.kind_of? JButton)     
                                value.doClick()
                end #if (value.kind_of? JButton)     
        end #if (row < @table.getRowCount() && row >= 0 && column < @table.getColumnCount() && column >= 0)
   end #def mouseClicked(e)
   
 
end #class JTableButtonMouseListenerServer < MouseAdapter


class JTableButtonEditorServer < AbstractCellEditor
  include TableCellEditor
  include ActionListener

  EDIT = "edit"

  
  def init
    @button = JButton.new
    @button.setActionCommand(EDIT)
    @button.addActionListener(self)
    @button.setBorderPainted(false)
  end #def init

  def actionPerformed(e)
    if EDIT == e.getActionCommand
       #if @columna == 0
       #    $conexion.ctver_forget(@id[1..5])
       #end #if @columna == 0
       #if @columna == 1
       #     #debug puts "ETXO  :::  Fila: " + @fila.to_s + " Columna:  " + @columna.to_s
       #     #debug puts "identificacion: " + $id
       #     $option = 21
       #end #if @columna == 1 
       
       if @columna != 8 
           if @status == "Connected"
              if $conexion.ctdesconecta_server($id.to_s)
              else
                 JOptionPane.showMessageDialog panel, $conexion.conectatelnet_error_msg.to_s, "Error", JOptionPane::ERROR_MESSAGE                    
              end #if $conexion.ctdesconecta_server($id.to_s)
           else
              if $conexion.ctconecta_server($id.to_s)
              else
                 JOptionPane.showMessageDialog panel, $conexion.conectatelnet_error_msg.to_s, "Error", JOptionPane::ERROR_MESSAGE                    
              end #if $conexion.ctconecta_server($id.to_s)
           end #if @status == "Connected"
       end #if @columna == 7
       if @columna == 8
          message = "Do you really want to remove  \"" + @server_name.to_s.squeeze(" ").strip + "\"?"
          really_remove = JOptionPane.showConfirmDialog $content_pane ,message , "Confirmation...",  JOptionPane::YES_NO_OPTION, JOptionPane::QUESTION_MESSAGE
          if really_remove == javax.swing.JOptionPane::YES_OPTION
              if $conexion.ctborra_server($id.to_s)
              else
                 JOptionPane.showMessageDialog panel, $conexion.conectatelnet_error_msg.to_s, "Error", JOptionPane::ERROR_MESSAGE                    
              end #if $conexion.ctborra_server($id.to_s)
          end #if really_remove == javax.swing.JOptionPane::YES_OPTION
       end #if @columna == 8
      
       fireEditingStopped
    end #if EDIT == e.getActionCommand
  end #def actionPerformed(e)

  def getCellEditorValue
    return " "
  end #def getCellEditorValue

  def getTableCellEditorComponent(table, value, isSelected, row, column)
    @id = table.getValueAt(row, 1)
    @status = table.getValueAt(row,6)
    @server_name = table.getValueAt(row,3)
    $id = @id
    @fila = row
    @columna = column
    @value = value
    return @button
  end #getTableCellEditorComponent(table, value, isSelected, row, column)

end #class JTableButtonEditorServer < AbstractCellEditor


class HTMLListener
  include HyperlinkListener
  def hyperlinkUpdate(e)
     if e.getEventType == HyperlinkEvent::EventType::ACTIVATED
           java.awt.Desktop.getDesktop.browse(URI.new(e.getURL.toString))
     end #if e.getEventType == HyperlinkEvent.EventType::ACTIVATED     
  end #def hyperlinkUpdate(e)
end #def HTMLListener 

class JTableModelUser <  AbstractTableModel

   def initialize
       @columnNames = ["User","Admin","Groups","Dgroup","Email","Commit dir","Max dls","Dls"," ","",""]
       @columnTypes = [String.class,String.class,String.class,String.class,String.class,String.class,String.class,String.class,String.class,String.class,String.class,String.class] 
       @data = [["User","Admin","Groups","Dgroup","Email","CommitDir","MaxDls","Dls","OP1","OP2","ESP"]]
   end #initialize
   
   def actualiza(datos)
     matrix = java.util.Vector.new
     datos.each do |valor|   
           submatrix = java.util.Vector.new
           i = 0
           valor.each do |subvalor|
               escaped = UnicodeEscape.new(subvalor)
               subvalor = escaped.return_unicode_escape
               submatrix.add_element(subvalor)
               i += 1
           end #valor.each do |subvalor|
           submatrix.add_element(" Change ")
           submatrix.add_element(" Delete ")
           submatrix.add_element(" ")
           matrix.add_element(submatrix)
     end #datos.each do |valor|   
     @data = matrix
   end #   def actualiza(datos)

   
   
   def getColumnCount
       return @columnNames.length
   end #def getColumnCount
   
   def getRowCount
       return @data.length
   end #def getRowCount
   
  def getColumnName(columnIndex)
     return @columnNames[columnIndex]
  end #getColumnName(columnIndex)

  def getColumnClass(columnIndex)
     return @columnTypes[columnIndex]
  end #getColumnClass(columnIndex)
  
  def isCellEditable(row, col)
    return true
  end #isCellEditable(row, col)
  
  
  
  def getValueAt(rowIndex,columnIndex)
     return @data[rowIndex][columnIndex]
  end #getValueAt(rowIndex,columnIndex)
end #class JTableModeUser < AbstractTableModel

class JTableButtonRendererUser 
include TableCellRenderer

  def getTableCellRendererComponent(table, value, isClicked, hasFocus,row, column)

    border = javax.swing.border.LineBorder.new(Color::BLACK, 1)
    button = JButton.new(value)
    button.setBorder(border)
    
    table.getColumnModel.getColumn(column).setMinWidth(100)
    table.getColumnModel.getColumn(column).setMaxWidth(100)
    table.getColumnModel.getColumn(column).setWidth(100)
    
    return button
  end # getTableCellRendererComponent
  
end #class JTableButtonRendererServer

class JTableButtonMouseListenerUser < MouseAdapter
  
   def getTable(table)
     @table = table
   end #def getTable(table)
   
   def mouseClicked(e)
        column = @table.getColumnModel().getColumnIndexAtX(e.getX())
        row    = e.getY()/@table.getRowHeight()

        if (row < @table.getRowCount() && row >= 0 && column < @table.getColumnCount() && column >= 0)
                value = @table.getValueAt(row, column)
                if (value.kind_of? JButton)     
                                value.doClick()
                end #if (value.kind_of? JButton)     
        end #if (row < @table.getRowCount() && row >= 0 && column < @table.getColumnCount() && column >= 0)
   end #def mouseClicked(e)
   
 
end #class JTableButtonMouseListenerUser < MouseAdapter


class JTableButtonEditorUser < AbstractCellEditor
  include TableCellEditor
  include ActionListener

  EDIT = "edit"

  def init
    @button = JButton.new
    @button.setActionCommand(EDIT)
    @button.addActionListener(self)
    @button.setBorderPainted(false)
  end #def init

  def actionPerformed(e)
    if EDIT == e.getActionCommand
       #if @columna == 0
       #    $conexion.ctver_forget(@id[1..5])
       #end #if @columna == 0
       #if @columna == 1
       puts "ETXO  :1:  Fila: " + @fila.to_s + " Columna:  " + @columna.to_s
       $pulsado52 = true
       if @columna == 8
          JFrameUu.new(@user,@admin,@group,@dgroup,@commit_dir,@mail,@max_dls,@dls)
       end #if @columna == 8
       if @columna == 9
          message = "Do you really want to delete  \"" + @user.to_s.squeeze(" ").strip + "\" user ?"
          really_remove = JOptionPane.showConfirmDialog $content_pane ,message , "Confirmation...",  JOptionPane::YES_NO_OPTION, JOptionPane::QUESTION_MESSAGE
          if really_remove == javax.swing.JOptionPane::YES_OPTION
                    if $conexion.ctuserdel(@user)
                    else
                       JOptionPane.showMessageDialog panel, $conexion.conectatelnet_error_msg.to_s, "Error", JOptionPane::ERROR_MESSAGE                    
                    end #if $conexion.ctuserdel(@user)
          end #if really_remove == javax.swing.JOptionPane::YES_OPTION
       end #if @columna == 9
       if @columna != 8 && @columna != 9
          JFrameVu.new(@user,@admin,@group,@dgroup,@commit_dir,@mail,@max_dls,@dls)
       end #if @columna != 8 && @columna != 9
       fireEditingStopped
    end #if EDIT == e.getActionCommand
  end #def actionPerformed(e)

  def getCellEditorValue
    return " "
  end #def getCellEditorValue

  def getTableCellEditorComponent(table, value, isSelected, row, column)
    @user = table.getValueAt(row, 0)
    @admin = table.getValueAt(row, 1)
    @group = table.getValueAt(row, 2)
    @dgroup = table.getValueAt(row, 3)
    @commit_dir = table.getValueAt(row, 4)
    @email = table.getValueAt(row, 5)
    @max_dls = table.getValueAt(row, 6)
    @dls = table.getValueAt(row, 7)
    @fila = row
    @columna = column
    @value = value
    return @button
  end #getTableCellEditorComponent(table, value, isSelected, row, column)

end #class JTableButtonEditorUser < AbstractCellEditor

class JTableModelGroup <  AbstractTableModel

   def initialize
       @columnNames = ["Group","Admin","Members","Downloads"," ","",""]
       @columnTypes = [String.class,String.class,String.class,String.class,String.class,String.class,String.class] 
       @data = [["Group","Admin","Members","Downloads","OP1","OP2","ESP"]]
   end #initialize
   
   def actualiza(datos)
     matrix = java.util.Vector.new
     datos.each do |valor|   
           submatrix = java.util.Vector.new
           i = 0
           valor.each do |subvalor|
               escaped = UnicodeEscape.new(subvalor)
               subvalor = escaped.return_unicode_escape
               submatrix.add_element(subvalor)
               i += 1
           end #valor.each do |subvalor|
           submatrix.add_element(" Change ")
           submatrix.add_element(" Delete ")
           submatrix.add_element(" ")
           matrix.add_element(submatrix)
     end #datos.each do |valor|   
     @data = matrix
   end #   def actualiza(datos)

   
   
   def getColumnCount
       return @columnNames.length
   end #def getColumnCount
   
   def getRowCount
       return @data.length
   end #def getRowCount
   
  def getColumnName(columnIndex)
     return @columnNames[columnIndex]
  end #getColumnName(columnIndex)

  def getColumnClass(columnIndex)
     return @columnTypes[columnIndex]
  end #getColumnClass(columnIndex)
  
  def isCellEditable(row, col)
    return true
  end #isCellEditable(row, col)
  
  
  
  def getValueAt(rowIndex,columnIndex)
     return @data[rowIndex][columnIndex]
  end #getValueAt(rowIndex,columnIndex)
end #class JTableModelGroup < AbstractTableModel

class JTableButtonRendererGroup 
include TableCellRenderer

  def getTableCellRendererComponent(table, value, isClicked, hasFocus,row, column)

    border = javax.swing.border.LineBorder.new(Color::BLACK, 1)
    button = JButton.new(value)
    button.setBorder(border)
    
    table.getColumnModel.getColumn(column).setMinWidth(100)
    table.getColumnModel.getColumn(column).setMaxWidth(100)
    table.getColumnModel.getColumn(column).setWidth(100)
    
    return button
  end # getTableCellRendererComponent
  
end #class JTableButtonRendererGroup

class JTableButtonMouseListenerGroup < MouseAdapter
  
   def getTable(table)
     @table = table
   end #def getTable(table)
   
   def mouseClicked(e)
        column = @table.getColumnModel().getColumnIndexAtX(e.getX())
        row    = e.getY()/@table.getRowHeight()

        if (row < @table.getRowCount() && row >= 0 && column < @table.getColumnCount() && column >= 0)
                value = @table.getValueAt(row, column)
                if (value.kind_of? JButton)     
                                value.doClick()
                end #if (value.kind_of? JButton)     
        end #if (row < @table.getRowCount() && row >= 0 && column < @table.getColumnCount() && column >= 0)
   end #def mouseClicked(e)
   
 
end #class JTableButtonMouseListenerGroup < MouseAdapter


class JTableButtonEditorGroup < AbstractCellEditor
  include TableCellEditor
  include ActionListener

  EDIT = "edit"

  
  def init
    @button = JButton.new
    @button.setActionCommand(EDIT)
    @button.addActionListener(self)
    @button.setBorderPainted(false)
  end #def init

  def actionPerformed(e)
    if EDIT == e.getActionCommand
#       puts "ETXO  :0:  Fila: " + @fila.to_s + " Columna:  " + @columna.to_s
       $pulsado52 = true
       if @columna == 4
           JFrameUg.new(@group,@admin,@members,@downloads)
       end #if @columna == 4
       if @columna == 5
          message = "Do you really want to delete  \"" + @group.to_s.squeeze(" ").strip + "\" group ?"
          really_remove = JOptionPane.showConfirmDialog $content_pane ,message , "Confirmation...",  JOptionPane::YES_NO_OPTION, JOptionPane::QUESTION_MESSAGE
          if really_remove == javax.swing.JOptionPane::YES_OPTION
               if $conexion.ctgroupdel(@group)
               else
                  JOptionPane.showMessageDialog panel, $conexion.conectatelnet_error_msg.to_s, "Error", JOptionPane::ERROR_MESSAGE                    
               end #if $conexion.ctgruopdel(@group)
          end #if really_remove == javax.swing.JOptionPane::YES_OPTION
       end #if @columna == 5
       if @columna != 4 && @columna != 5
          JFrameVg.new(@group,@admin,@members,@downloads)
       end #if @columna != 8 && @columna != 9

       fireEditingStopped
    end #if EDIT == e.getActionCommand
  end #def actionPerformed(e)

  def getCellEditorValue
    return " "
  end #def getCellEditorValue

  def getTableCellEditorComponent(table, value, isSelected, row, column)
    @group = table.getValueAt(row, 0)
    @admin = table.getValueAt(row, 1)
    @members = table.getValueAt(row, 2)
    @downloads = table.getValueAt(row, 3) 
    @fila = row
    @columna = column
    @value = value
    return @button
  end #getTableCellEditorComponent(table, value, isSelected, row, column)

end #class JTableButtonEditorGroup < AbstractCellEditor

class JFrameNug < JFrame

       def initialize
          super "New User / Group"
          self.initUI
      end #initialize

      def initUI
          panel = JPanel.new
          panel.setLayout nil
          self.getContentPane.add panel
          
          @call_type = javax.swing.JComboBox.new(["User", "Group"].to_java)
          @call_type.setBounds 100,40,100,20

          icono_fondo_principal  = ImageIcon.new java.awt.Toolkit::default_toolkit.get_image(java.lang.Object.new.java_class.resource("/fondo_00.png"))
          fondo_principal = JLabel.new icono_fondo_principal
          fondo_principal.setBounds(0,0,300,480)
          icono_fondo_principal = ImageIcon.new(icono_fondo_principal.getImage().getScaledInstance(300,480,java.awt.Image::SCALE_FAST))
          fondo_principal.setIcon(icono_fondo_principal)

          @field01 = javax.swing.JLabel.new
          @field01.setBounds 20,80,150,20
          @textfield01 = javax.swing.JTextField.new
          @textfield01.setBounds 130,80,150,25
          @field02 = javax.swing.JLabel.new
          @field02.setBounds 20,120,150,20
          @textfield02 = javax.swing.JPasswordField.new
          @textfield02.setBounds 130,120,150,25
          @group_type = javax.swing.JComboBox.new(["True", "False"].to_java)
          @group_type.setBounds 130,120,150,25
          @field03 = javax.swing.JLabel.new
          @field03.setBounds 20,160,150,20
          @textfield03 = javax.swing.JPasswordField.new
          @textfield03.setBounds 130,160,150,25
          @field04 = javax.swing.JLabel.new
          @field04.setBounds 20,200,150,20
          @textfield04 = javax.swing.JTextField.new
          @textfield04.setBounds 130,200,150,25
          @field05 = javax.swing.JLabel.new
          @field05.setBounds 20,240,150,20
          @textfield05 = javax.swing.JTextField.new
          @textfield05.setBounds 130,240,150,25
          @field06 = javax.swing.JLabel.new
          @field06.setBounds 20,280,150,20
          @textfield06 = javax.swing.JTextField.new
          @textfield06.setBounds 130,280,150,25
          @field07 = javax.swing.JLabel.new
          @field07.setBounds 20,320,150,20
          @textfield07 = javax.swing.JTextField.new
          @textfield07.setBounds 130,320,150,25
          
          @ok = javax.swing.JButton.new
          @ok.setText("Ok")
          @ok.setBounds 45, 380, 100,40
          @ok.add_action_listener do |e|
              puts "OK PULSADO"
              if @textfield01.getText.scan(/\w+/).length > 1 ||
                 @textfield02.getText.scan(/\w+/).length > 1 ||
                 @textfield03.getText.scan(/\w+/).length > 1 ||
                 @textfield04.getText.scan(/\w+/).length > 1 ||
                 @textfield05.getText.scan(/\w+/).length > 1 ||
                 @textfield06.getText.scan(/\w+/).length > 1 ||
                 @textfield07.getText.scan(/\w+/).length > 1 
                   JOptionPane.showMessageDialog panel, "New incorrect user or group only one value for each parameter allowed.", "Error", JOptionPane::ERROR_MESSAGE
                   self.dispose
              else     
                case  @call_type.getSelectedItem.to_s
                  when "User"
                   if $conexion.ctuseradd(@textfield01.getText,@textfield02.getText,@textfield03.getText ,@textfield04.getText ,@textfield05.getText ,@textfield06.getText ,@textfield07.getText)
                      self.dispose
                   else
                      JOptionPane.showMessageDialog panel, $conexion.conectatelnet_error_msg.to_s, "Error", JOptionPane::ERROR_MESSAGE
                      self.dispose
                   end #if $conexion.ctuseradd(@textfield01.getText,@textfield02.getText,@textfield03.getText ,@textfield04.getText ,@textfield05.getText ,@textfield06.getText ,@textfield07.getText) 
                  when "Group"
                   if $conexion.ctgroupadd(@textfield01.getText,@group_type.getSelectedItem.to_s) 
                     self.dispose
                   else
                      JOptionPane.showMessageDialog panel, $conexion.conectatelnet_error_msg.to_s, "Error", JOptionPane::ERROR_MESSAGE
                      self.dispose
                  end #if $conexion.ctgroupadd(@nuevo_grupo.text,@valor_adm)   
                end #case  @call_type.getSelectedItem.to_s
              end #if @textfield01.getText.scan(/\w+/).length > 1 ||...
          end #botonOk.add_action_listener do |e|
          
          @cancel = javax.swing.JButton.new 
          @cancel.setText("Cancel")
          @cancel.setBounds 155, 380, 100,40
          @cancel.add_action_listener do |e|
              puts "CANCEL PULSADO"
              self.dispose
          end #botonOk.add_action_listener do |e|
          
          panel.add @call_type
          panel.add @ok
          panel.add @cancel

          @call_type.add_action_listener do |e|
             @textfield01.setText("")
             @textfield02.setText("")
             @textfield03.setText("")
             @textfield04.setText("")
             @textfield05.setText("")
             @textfield06.setText("")
             @textfield07.setText("")
             panel.remove @field01
             panel.remove @textfield01
             panel.remove @field02
             panel.remove @textfield02
             panel.remove @group_type
             panel.remove @field03
             panel.remove @textfield03
             panel.remove @field04
             panel.remove @textfield04
             panel.remove @field05
             panel.remove @textfield05
             panel.remove @field06
             panel.remove @textfield06
             panel.remove @field07
             panel.remove @textfield07
             panel.remove fondo_principal
             
             case  @call_type.getSelectedItem.to_s
             when "User"
                panel.add @field01
                panel.add @textfield01
                panel.add @field02
                panel.add @textfield02
                panel.add @field03
                panel.add @textfield03
                panel.add @field04
                panel.add @textfield04
                panel.add @field05
                panel.add @textfield05
                panel.add @field06
                panel.add @textfield06
                panel.add @field07
                panel.add @textfield07
                panel.add fondo_principal
                
                @field01.setText("User *")
                @field02.setText("Password *")
                @field03.setText("Conf. Password *")
                @field04.setText("Group")
                @field05.setText("Commit Dir")
                @field06.setText("Mail")
                @field07.setText("Max Dls")

                
                self.revalidate()
                self.repaint()

             when "Group"
                panel.add @field01
                panel.add @textfield01
                panel.add @field02
                panel.add @group_type
                panel.add fondo_principal
                
                @field01.setText("Group *")
                @field02.setText("Administrator *")
                
                self.revalidate()
                self.repaint()
             end # case  call_type.getSelectedItem.to_s
          end #@choosed.add_action_listener do |e|
          
          @call_type.fireActionEvent
          
          self.setDefaultCloseOperation JFrame::DISPOSE_ON_CLOSE
          self.setSize java.awt.Dimension.new(300,480)
          self.setResizable(false)
          self.setLocationRelativeTo nil
          self.setVisible true
          self.setIconImage(java.awt.Toolkit::default_toolkit.get_image(java.lang.Object.new.java_class.resource("/01.png")))
      end #initUI
      

end #Nug < JFrameNug


class JFrameVu < JFrame

       def initialize(user,admin,group,dgroup,commit_dir,mail,max_dls,dls)
          @user = user
          @admin = admin
          @group = group
          @dgroup = dgroup
          @commit_dir = commit_dir
          @mail = mail
          @max_dls = max_dls
          @dls = dls
          super "View user details"
          self.initUI
      end #initialize
      
      
      def initUI
    
          panel = JPanel.new
          panel.setLayout nil
          self.getContentPane.add panel
          
          icono_fondo_principal  = ImageIcon.new java.awt.Toolkit::default_toolkit.get_image(java.lang.Object.new.java_class.resource("/fondo_00.png"))
          fondo_principal = JLabel.new icono_fondo_principal
          fondo_principal.setBounds(0,0,600,330)
          icono_fondo_principal = ImageIcon.new(icono_fondo_principal.getImage().getScaledInstance(600,330,java.awt.Image::SCALE_FAST))
          fondo_principal.setIcon(icono_fondo_principal)

          @field01 = javax.swing.JLabel.new
          @field01.setBounds 20,10,200,20
          @field01.setFont(Font.new case RbConfig::CONFIG['host_os'] when /mswin/; "Arial Unicode MS" when /darwin/; "AppleGothic, Arial" end, Font::BOLD, 12)
          @textfield01 = javax.swing.JLabel.new
          @textfield01.setBounds 210,10,390,20
          
          @field02 = javax.swing.JLabel.new
          @field02.setBounds 20,40,200,20
          @field02.setFont(Font.new case RbConfig::CONFIG['host_os'] when /mswin/; "Arial Unicode MS" when /darwin/; "AppleGothic, Arial" end, Font::BOLD, 12)
          @textfield02 = javax.swing.JLabel.new
          @textfield02.setBounds 210,40,390,20
          
          @field03 = javax.swing.JLabel.new
          @field03.setBounds 20,70,200,20
          @field03.setFont(Font.new case RbConfig::CONFIG['host_os'] when /mswin/; "Arial Unicode MS" when /darwin/; "AppleGothic, Arial" end, Font::BOLD, 12)
          @textfield03 = javax.swing.JLabel.new
          @textfield03.setBounds 210,70,390,20
          
          @field04 = javax.swing.JLabel.new
          @field04.setBounds 20,100,200,20
          @field04.setFont(Font.new case RbConfig::CONFIG['host_os'] when /mswin/; "Arial Unicode MS" when /darwin/; "AppleGothic, Arial" end, Font::BOLD, 12)
          @textfield04 = javax.swing.JLabel.new
          @textfield04.setBounds 210,100,390,20
          
          @field05 = javax.swing.JLabel.new
          @field05.setBounds 20,130,200,20
          @field05.setFont(Font.new case RbConfig::CONFIG['host_os'] when /mswin/; "Arial Unicode MS" when /darwin/; "AppleGothic, Arial" end, Font::BOLD, 12)
          @textfield05 = javax.swing.JLabel.new
          @textfield05.setBounds 210,130,390,20
          
          @field06 = javax.swing.JLabel.new
          @field06.setBounds 20,160,200,20
          @field06.setFont(Font.new case RbConfig::CONFIG['host_os'] when /mswin/; "Arial Unicode MS" when /darwin/; "AppleGothic, Arial" end, Font::BOLD, 12)
          @textfield06 = javax.swing.JLabel.new
          @textfield06.setBounds 210,160,390,20
          
          @field07 = javax.swing.JLabel.new
          @field07.setBounds 20,190,200,20
          @field07.setFont(Font.new case RbConfig::CONFIG['host_os'] when /mswin/; "Arial Unicode MS" when /darwin/; "AppleGothic, Arial" end, Font::BOLD, 12)
          @textfield07 = javax.swing.JLabel.new
          @textfield07.setBounds 210,190,390,20
          
          @field08 = javax.swing.JLabel.new
          @field08.setBounds 20,220,200,20
          @field08.setFont(Font.new case RbConfig::CONFIG['host_os'] when /mswin/; "Arial Unicode MS" when /darwin/; "AppleGothic, Arial" end, Font::BOLD, 12)
          @textfield08 = javax.swing.JLabel.new
          @textfield08.setBounds 210,220,390,20
          
          @close = javax.swing.JButton.new 
          @close.setText("Close")
          @close.setBounds 255, 260, 100,30
          @close.add_action_listener do |e|
              puts "CLOSE PULSADO"
              self.dispose
          end #@close.add_action_listener do |e|
          
          panel.add @close

          panel.add @field01
          panel.add @textfield01
          panel.add @field02
          panel.add @textfield02
          panel.add @field03
          panel.add @textfield03
          panel.add @field04
          panel.add @textfield04
          panel.add @field05
          panel.add @textfield05
          panel.add @field06
          panel.add @textfield06
          panel.add @field07
          panel.add @textfield07
          panel.add @field08
          panel.add @textfield08
          panel.add fondo_principal
                
          @field01.setText("User")
          @textfield01.setText(@user)
          @field02.setText("Admin Rigths")
          @textfield02.setText(@admin)
          @field03.setText("Groups")
          @textfield03.setText(@group[1..@group.length - 1])
          @field04.setText("Default Group")
          @textfield04.setText(@dgroup)
          @field05.setText("Commit Dir")
          @textfield05.setText(@commit_dir)
          @field06.setText("Mail")
          @textfield06.setText(@mail)
          @field07.setText("Max Allowed Downloads")
          @textfield07.setText(@max_dls)
          @field08.setText("Actuals Downloads")
          @textfield08.setText(@dls)
          #@user,@admin,@group,@dgroup,@commit_dir,@mail,@max_dls,@dls
             
          self.revalidate()
          self.repaint()
        
        
          self.setDefaultCloseOperation JFrame::DISPOSE_ON_CLOSE
          self.setSize java.awt.Dimension.new(600,330)
          self.setResizable(false)
          self.setLocationRelativeTo nil
          self.setVisible true
          self.setIconImage(java.awt.Toolkit::default_toolkit.get_image(java.lang.Object.new.java_class.resource("/01.png")))
      end #initUI
      

end #class JFrameVu < JFrame

class JFrameUu < JFrame

       def initialize(user,admin,group,dgroup,commit_dir,mail,max_dls,dls)
          @user = user
          @admin = admin
          @group = group
          @dgroup = dgroup
          @commit_dir = commit_dir
          @mail = mail
          @max_dls = max_dls
          @dls = dls
          super "Change user \"" + user.to_s + "\" details..."
          self.initUI
      end #initialize
      
      
      def initUI
    
          panel = JPanel.new
          panel.setLayout nil
          self.getContentPane.add panel
          
          icono_fondo_principal  = ImageIcon.new java.awt.Toolkit::default_toolkit.get_image(java.lang.Object.new.java_class.resource("/fondo_00.png"))
          fondo_principal = JLabel.new icono_fondo_principal
          fondo_principal.setBounds(0,0,600,300)
          icono_fondo_principal = ImageIcon.new(icono_fondo_principal.getImage().getScaledInstance(600,300,java.awt.Image::SCALE_FAST))
          fondo_principal.setIcon(icono_fondo_principal)
          
          @field03 = javax.swing.JLabel.new
          @field03.setBounds 20,20,200,20
          @textfield03 = javax.swing.JTextField.new
          @textfield03.setBounds 210,20,310,25
          
          @field04 = javax.swing.JLabel.new
          @field04.setBounds 20,60,200,20
          @textfield04 = javax.swing.JTextField.new
          @textfield04.setBounds 210,60,310,25
          
          @field05 = javax.swing.JLabel.new
          @field05.setBounds 20,100,200,20
          @textfield05 = javax.swing.JTextField.new
          @textfield05.setBounds 210,100,310,25
          
          @field06 = javax.swing.JLabel.new
          @field06.setBounds 20,140,200,20
          @textfield06 = javax.swing.JTextField.new
          @textfield06.setBounds 210,140,310,25
          
          @field07 = javax.swing.JLabel.new
          @field07.setBounds 20,180,200,20
          @textfield07 = javax.swing.JTextField.new
          @textfield07.setBounds 210,180,310,25

          @change = javax.swing.JButton.new 
          @change.setText("Change")
          @change.setBounds 200, 230, 100,30
          @change.add_action_listener do |e|
              if @group[1..@group.length - 1] != @textfield03.getText
                 new_groups = @textfield03.getText.scan(/\w+/) - @group[1..@group.length - 1].scan(/\w+/)
                 old_groups = @group[1..@group.length - 1].scan(/\w+/) - @textfield03.getText.scan(/\w+/)
                 if new_groups.length > 0
                    new_groups.each do |grupo|
                        if $conexion.ctusergroupadd(@user,grupo)
                        else
                           JOptionPane.showMessageDialog panel, $conexion.conectatelnet_error_msg.to_s, "Error", JOptionPane::ERROR_MESSAGE
                        end #if $conexion.ctusergroupadd(@user,grupo)
                    end #new_groups.each do |grupo|
                 end #if new_groups.length > 0
                 if old_groups.length > 0
                    old_groups.each do |grupo|
                        puts "grupo" + grupo.to_s
                        if $conexion.ctusergroupdel(@user,grupo)
                        else
                           JOptionPane.showMessageDialog panel, $conexion.conectatelnet_error_msg.to_s, "Error", JOptionPane::ERROR_MESSAGE
                        end #if $conexion.ctusergroupadd(@user,grupo)
                    end #new_groups.each do |grupo|
                 end #if old_groups.length > 0
              end #if @group[1..@group.length - 1] != @textfield03.getText
              if @textfield04.getText != @dgroup
                new_dgroups = @textfield04.getText.scan(/\w+/)
                if new_dgroups.length == 1
                   if $conexion.ctuserdgroup(@user,new_dgroups[0]) 
                   else
                      JOptionPane.showMessageDialog panel, $conexion.conectatelnet_error_msg.to_s, "Error", JOptionPane::ERROR_MESSAGE
                   end #if $conexion.ctuserdgroup(@user,new_dgroups[0]) 
                else
                   JOptionPane.showMessageDialog panel, "More that one Default Group isn't allowed", "Error", JOptionPane::ERROR_MESSAGE
                end #if new_dgroups.length == 1
              end #if @textfield04.getText != @dgroup
              if @textfield05.getText != @commit_dir
                 if $conexion.ctusercommit(@user,@textfield05.getText)
                 else
                    JOptionPane.showMessageDialog panel, $conexion.conectatelnet_error_msg.to_s, "Error", JOptionPane::ERROR_MESSAGE
                 end #if $conexion.ctusermail(@user,@textfield05.getText)
              end #if @textfield05.getText != @commit_dir
              if @textfield06.getText != @mail
                 if $conexion.ctusermail(@user,@textfield06.getText)
                 else
                    JOptionPane.showMessageDialog panel, $conexion.conectatelnet_error_msg.to_s, "Error", JOptionPane::ERROR_MESSAGE
                 end #if $conexion.ctusermail(@user,@textfield06.getText)
              end #if @textfield06.getText != @mail
              if @textfield07.getText != @max_dls
                 if $conexion.ctuserdls(@user,@textfield07.getText)
                 else
                    JOptionPane.showMessageDialog panel, $conexion.conectatelnet_error_msg.to_s, "Error", JOptionPane::ERROR_MESSAGE
                 end #if $conexion.ctusermail(@user,@textfield07.getText)
              end #if @textfield07.getText != @max_dls
              puts "CHANGE PULSADO"
              self.dispose
          end #@close.add_action_listener do |e|
          
          @close = javax.swing.JButton.new 
          @close.setText("Cancel")
          @close.setBounds 305, 230, 100,30
          @close.add_action_listener do |e|
              puts "CLOSE PULSADO"
              self.dispose
          end #@close.add_action_listener do |e|

          panel.add @change
          panel.add @close

          panel.add @field03
          panel.add @textfield03
          panel.add @field04
          panel.add @textfield04
          panel.add @field05
          panel.add @textfield05
          panel.add @field06
          panel.add @textfield06
          panel.add @field07
          panel.add @textfield07
          panel.add fondo_principal
          
          @field03.setText("Groups")
          @textfield03.setText(@group[1..@group.length - 1])
          @field04.setText("Default Group")
          @textfield04.setText(@dgroup)
          @field05.setText("Commit Dir")
          @textfield05.setText(@commit_dir)
          @field06.setText("Mail")
          @textfield06.setText(@mail)
          @field07.setText("Max Allowed Downloads")
          @textfield07.setText(@max_dls)
             
          self.revalidate()
          self.repaint()
        
        
          self.setDefaultCloseOperation JFrame::DISPOSE_ON_CLOSE
          self.setSize java.awt.Dimension.new(600,300)
          self.setResizable(false)
          self.setLocationRelativeTo nil
          self.setVisible true
          self.setIconImage(java.awt.Toolkit::default_toolkit.get_image(java.lang.Object.new.java_class.resource("/01.png")))
      end #initUI
      

end #class JFrameUu < JFrame

class JFrameVg < JFrame

       def initialize(group,admin,members,downloads)
          @group = group
          @admin = admin
          @members = members
          @downloads = downloads
          super "View group details"
          self.initUI
      end #initialize
      
      
      def initUI
    
          panel = JPanel.new
          panel.setLayout nil
          self.getContentPane.add panel
          
          icono_fondo_principal  = ImageIcon.new java.awt.Toolkit::default_toolkit.get_image(java.lang.Object.new.java_class.resource("/fondo_00.png"))
          fondo_principal = JLabel.new icono_fondo_principal
          fondo_principal.setBounds(0,0,600,230)
          icono_fondo_principal = ImageIcon.new(icono_fondo_principal.getImage().getScaledInstance(600,230,java.awt.Image::SCALE_FAST))
          fondo_principal.setIcon(icono_fondo_principal)

          @field01 = javax.swing.JLabel.new
          @field01.setBounds 20,10,200,20
          @field01.setFont(Font.new case RbConfig::CONFIG['host_os'] when /mswin/; "Arial Unicode MS" when /darwin/; "AppleGothic, Arial" end, Font::BOLD, 12)
          @textfield01 = javax.swing.JLabel.new
          @textfield01.setBounds 210,10,390,20
          
          @field02 = javax.swing.JLabel.new
          @field02.setBounds 20,40,200,20
          @field02.setFont(Font.new case RbConfig::CONFIG['host_os'] when /mswin/; "Arial Unicode MS" when /darwin/; "AppleGothic, Arial" end, Font::BOLD, 12)
          @textfield02 = javax.swing.JLabel.new
          @textfield02.setBounds 210,40,390,20
          
          @field03 = javax.swing.JLabel.new
          @field03.setBounds 20,70,200,20
          @field03.setFont(Font.new case RbConfig::CONFIG['host_os'] when /mswin/; "Arial Unicode MS" when /darwin/; "AppleGothic, Arial" end, Font::BOLD, 12)
          @textfield03 = javax.swing.JLabel.new
          @textfield03.setBounds 210,70,390,20
          
          @field04 = javax.swing.JLabel.new
          @field04.setBounds 20,100,200,20
          @field04.setFont(Font.new case RbConfig::CONFIG['host_os'] when /mswin/; "Arial Unicode MS" when /darwin/; "AppleGothic, Arial" end, Font::BOLD, 12)
          @textfield04 = javax.swing.JLabel.new
          @textfield04.setBounds 210,100,390,20
                    
          @close = javax.swing.JButton.new 
          @close.setText("Close")
          @close.setBounds 255, 160, 100,30
          @close.add_action_listener do |e|
              puts "CLOSE PULSADO"
              self.dispose
          end #@close.add_action_listener do |e|
          
          panel.add @close

          panel.add @field01
          panel.add @textfield01
          panel.add @field02
          panel.add @textfield02
          panel.add @field03
          panel.add @textfield03
          panel.add @field04
          panel.add @textfield04
          panel.add fondo_principal
                
          @field01.setText("Group")
          @textfield01.setText(@group)
          @field02.setText("Admin Rigths")
          @textfield02.setText(@admin)
          @field03.setText("Actual Members")
          @textfield03.setText(@members)
          @field04.setText("Actual Downloads")
          @textfield04.setText(@downloads)
             
          self.revalidate()
          self.repaint()
        
        
          self.setDefaultCloseOperation JFrame::DISPOSE_ON_CLOSE
          self.setSize java.awt.Dimension.new(600,230)
          self.setResizable(false)
          self.setLocationRelativeTo nil
          self.setVisible true
          self.setIconImage(java.awt.Toolkit::default_toolkit.get_image(java.lang.Object.new.java_class.resource("/01.png")))
      end #initUI
      

end #class JFrameVg < JFrame

class JFrameUg < JFrame

       def initialize(group,admin,members,downloads)
          @group = group
          @admin = admin
          @members = members
          @downloads = downloads
          super "Change group \"" + group.to_s + "\" admin rigths..."
          self.initUI
      end #initialize
      
      
      def initUI
    
          panel = JPanel.new
          panel.setLayout nil
          self.getContentPane.add panel
          
          icono_fondo_principal  = ImageIcon.new java.awt.Toolkit::default_toolkit.get_image(java.lang.Object.new.java_class.resource("/fondo_00.png"))
          fondo_principal = JLabel.new icono_fondo_principal
          fondo_principal.setBounds(0,0,350,180)
          icono_fondo_principal = ImageIcon.new(icono_fondo_principal.getImage().getScaledInstance(350,180,java.awt.Image::SCALE_FAST))
          fondo_principal.setIcon(icono_fondo_principal)

          @field02 = javax.swing.JLabel.new
          @field02.setBounds 70,40,120,20
          @group_type = javax.swing.JComboBox.new(["True", "False"].to_java)
          @group_type.setBounds 180,40,100,20
          
          @change = javax.swing.JButton.new 
          @change.setText("Change")
          @change.setBounds 70, 100, 100,30
          @change.add_action_listener do |e|
              puts "CHANGE PULSADO"
              puts "original"
              puts "cambio solicitado " + @group_type.getSelectedItem.to_s.downcase
              if @group_type.getSelectedItem.to_s.downcase != @admin
                 if $conexion.ctgroupadmin(@group,@group_type.getSelectedItem.to_s.downcase)
                 else
                    JOptionPane.showMessageDialog panel, $conexion.conectatelnet_error_msg.to_s, "Error", JOptionPane::ERROR_MESSAGE
                 end #if $conexion.ctgroupadmin(@group,@group_type.getSelectedItem.to_s.downcase)
              end #if @group_type.getSelectedItem.to_s != 
              self.dispose
          end #@close.add_action_listener do |e|

          @close = javax.swing.JButton.new 
          @close.setText("Cancel")
          @close.setBounds 175, 100, 100,30
          @close.add_action_listener do |e|
              puts "CANCEL PULSADO"
              self.dispose
          end #@close.add_action_listener do |e|
          
          panel.add @change
          panel.add @close

          panel.add @field02
          panel.add @group_type
          panel.add fondo_principal
                
          @field02.setText("Admin Rigths")
          if @admin =~ /true/
            @group_type.setSelectedIndex(0)
          else
            @group_type.setSelectedIndex(1)
          end #if @admin ~= /true/
             
          self.revalidate()
          self.repaint()
        
        
          self.setDefaultCloseOperation JFrame::DISPOSE_ON_CLOSE
          self.setSize java.awt.Dimension.new(350,180)
          self.setResizable(false)
          self.setLocationRelativeTo nil
          self.setVisible true
          self.setIconImage(java.awt.Toolkit::default_toolkit.get_image(java.lang.Object.new.java_class.resource("/01.png")))
      end #initUI
      

end #class JFrameVg < JFrame


class JTableModelShare <  AbstractTableModel

   def initialize
       @columnNames = ["Priority","Directory","Strategy","",""]
       @columnTypes = [String.class,String.class,String.class,String.class,String.class] 
       @data = [["Group","Admin","Members","OP1","ESP"]]
   end #initialize
   
   def actualiza(datos)
     matrix = java.util.Vector.new
     datos.each do |valor|   
           submatrix = java.util.Vector.new
           i = 0
           valor.each do |subvalor|
               escaped = UnicodeEscape.new(subvalor)
               subvalor = escaped.return_unicode_escape
               submatrix.add_element(subvalor)
               i += 1
           end #valor.each do |subvalor|
           submatrix.add_element(" Unshare ")
           submatrix.add_element(" ")
           matrix.add_element(submatrix)
     end #datos.each do |valor|   
     @data = matrix
   end #   def actualiza(datos)

   
   
   def getColumnCount
       return @columnNames.length
   end #def getColumnCount
   
   def getRowCount
       return @data.length
   end #def getRowCount
   
  def getColumnName(columnIndex)
     return @columnNames[columnIndex]
  end #getColumnName(columnIndex)

  def getColumnClass(columnIndex)
     return @columnTypes[columnIndex]
  end #getColumnClass(columnIndex)
  
  def isCellEditable(row, col)
    return true
  end #isCellEditable(row, col)
  
  
  
  def getValueAt(rowIndex,columnIndex)
     return @data[rowIndex][columnIndex]
  end #getValueAt(rowIndex,columnIndex)
end #class JTableModelShare < AbstractTableModel

class JTableButtonRendererShare
include TableCellRenderer

  def getTableCellRendererComponent(table, value, isClicked, hasFocus,row, column)

    border = javax.swing.border.LineBorder.new(Color::BLACK, 1)
    button = JButton.new(value)
    button.setBorder(border)
    
    table.getColumnModel.getColumn(column).setMinWidth(100)
    table.getColumnModel.getColumn(column).setMaxWidth(100)
    table.getColumnModel.getColumn(column).setWidth(100)
    
    return button
  end # getTableCellRendererComponent
  
end #class JTableButtonRendererShare

class JTableButtonMouseListenerShare < MouseAdapter
  
   def getTable(table)
     @table = table
   end #def getTable(table)
   
   def mouseClicked(e)
        column = @table.getColumnModel().getColumnIndexAtX(e.getX())
        row    = e.getY()/@table.getRowHeight()

        if (row < @table.getRowCount() && row >= 0 && column < @table.getColumnCount() && column >= 0)
                value = @table.getValueAt(row, column)
                if (value.kind_of? JButton)     
                                value.doClick()
                end #if (value.kind_of? JButton)     
        end #if (row < @table.getRowCount() && row >= 0 && column < @table.getColumnCount() && column >= 0)
   end #def mouseClicked(e)
   
 
end #class JTableButtonMouseListenerShare < MouseAdapter


class JTableButtonEditorShare < AbstractCellEditor
  include TableCellEditor
  include ActionListener

  EDIT = "edit"

  
  def init
    @button = JButton.new
    @button.setActionCommand(EDIT)
    @button.addActionListener(self)
    @button.setBorderPainted(false)
  end #def init

  def actionPerformed(e)
    if EDIT == e.getActionCommand
       $pulsado52 = true
       if @columna == 3
           message = "Do you really want to unshare  \"" + @dir.to_s.strip + "\" ?"
           really_unshare = JOptionPane.showConfirmDialog $content_pane ,message , "Confirmation...",  JOptionPane::YES_NO_OPTION, JOptionPane::QUESTION_MESSAGE
           if really_unshare == javax.swing.JOptionPane::YES_OPTION
               if $conexion.ctunshare(@dir)
               else
                  JOptionPane.showMessageDialog panel, $conexion.conectatelnet_error_msg.to_s, "Error", JOptionPane::ERROR_MESSAGE                    
               end #if $conexion.ctunshare(@dir)
           end #if really_unshare == javax.swing.JOptionPane::YES_OPTION

       end #if @columna == 3
       if @columna != 3
       end #if @columna == 3       #if @columna == 5

       fireEditingStopped
    end #if EDIT == e.getActionCommand
  end #def actionPerformed(e)

  def getCellEditorValue
    return " "
  end #def getCellEditorValue

  def getTableCellEditorComponent(table, value, isSelected, row, column)
    @dir = table.getValueAt(row, 1)
    @fila = row
    @columna = column
    @value = value
    return @button
  end #getTableCellEditorComponent(table, value, isSelected, row, column)

end #class JTableButtonEditorShare < AbstractCellEditor

class JFrameNs < JFrame

       def initialize
          super "New Share"
          self.initUI
      end #initialize
      
      
      def initUI
    
          panel = JPanel.new
          panel.setLayout nil
          self.getContentPane.add panel
          
          icono_fondo_principal  = ImageIcon.new java.awt.Toolkit::default_toolkit.get_image(java.lang.Object.new.java_class.resource("/fondo_00.png"))
          fondo_principal = JLabel.new icono_fondo_principal
          fondo_principal.setBounds(0,0,450,230)
          icono_fondo_principal = ImageIcon.new(icono_fondo_principal.getImage().getScaledInstance(450,230,java.awt.Image::SCALE_FAST))
          fondo_principal.setIcon(icono_fondo_principal)

          @field01 = javax.swing.JLabel.new
          @field01.setBounds 70,40,120,20
          @textfield01 = javax.swing.JTextField.new
          @textfield01.setBounds 200,40,210,25
          
          @field02 = javax.swing.JLabel.new
          @field02.setBounds 70,80,120,20
          @textfield02 = javax.swing.JTextField.new
          @textfield02.setBounds 200,80,210,25
          
          @field03 = javax.swing.JLabel.new
          @field03.setBounds 70,120,120,20
          @textfield03 = javax.swing.JTextField.new
          @textfield03.setBounds 200,120,210,25
          
          @change = javax.swing.JButton.new 
          @change.setText("Accept")
          @change.setBounds 120, 160, 100,30
          @change.add_action_listener do |e|
              if $conexion.ctshare(@textfield01.getText,@textfield02.getText,@textfield03.getText)
              else
                 JOptionPane.showMessageDialog panel, $conexion.conectatelnet_error_msg.to_s, "Error", JOptionPane::ERROR_MESSAGE
              end #if conexion.ctshare(@textfield01.getText,@textfield02.getText,@textfield03.getText)
              self.dispose
          end #@close.add_action_listener do |e|

          @close = javax.swing.JButton.new 
          @close.setText("Cancel")
          @close.setBounds 225, 160, 100,30
          @close.add_action_listener do |e|
              self.dispose
          end #@close.add_action_listener do |e|
          
          panel.add @change
          panel.add @close

          panel.add @field01
          panel.add @field02
          panel.add @field03
          panel.add @textfield01
          panel.add @textfield02
          panel.add @textfield03

          panel.add fondo_principal
          
          @field01.setText("Priority   *")
          @field02.setText("Directory  *")
          @field03.setText("Strategy   *")      
                     
          self.revalidate()
          self.repaint()
        
        
          self.setDefaultCloseOperation JFrame::DISPOSE_ON_CLOSE
          self.setSize java.awt.Dimension.new(450,230)
          self.setResizable(false)
          self.setLocationRelativeTo nil
          self.setVisible true
          self.setIconImage(java.awt.Toolkit::default_toolkit.get_image(java.lang.Object.new.java_class.resource("/01.png")))
      end #initUI
      

end #class JFrameNs < JFrame

class JFrameNc < JFrame

       def initialize
          super "Console"
          self.initUI
      end #initialize
      
      
      def initUI
    
          panel = JPanel.new
          panel.setLayout nil
          self.getContentPane.add panel
          
          icono_fondo_principal   = ImageIcon.new java.awt.Toolkit::default_toolkit.get_image(java.lang.Object.new.java_class.resource("/fondo_00.png"))
          icono_fondo_secundario  = ImageIcon.new java.awt.Toolkit::default_toolkit.get_image(java.lang.Object.new.java_class.resource("/fondo_03.png"))
          fondo_principal = JLabel.new icono_fondo_principal
          fondo_secundario = JLabel.new icono_fondo_secundario
          fondo_principal.setBounds(0,0,700,600)
          fondo_secundario.setBounds(0,80,700,40)
          
          icono_fondo_principal = ImageIcon.new(icono_fondo_principal.getImage().getScaledInstance(700,600,java.awt.Image::SCALE_FAST))
          icono_fondo_secundario = ImageIcon.new(icono_fondo_principal.getImage().getScaledInstance(700,60,java.awt.Image::SCALE_FAST))
          fondo_principal.setIcon(icono_fondo_principal)
          fondo_secundario.setIcon(icono_fondo_secundario)

          @field01 = javax.swing.JLabel.new
          @field01.setBounds 20,40,80,20
          @textfield01 = javax.swing.JTextField.new
          @textfield01.setBounds 100,40,300,25
          
          @console_output_details_show = javax.swing.JLabel.new 
          @console_output_details_show_s = JScrollPane.new(@console_output_details_show)
          @console_output_details_show_s.setOpaque false
          @console_output_details_show.setHorizontalAlignment(javax.swing.SwingConstants::LEFT)
          @console_output_details_show.setVerticalAlignment(javax.swing.SwingConstants::TOP)
          @console_output_details_show_s.setBounds 0,120,700,455
          
          console_output_value_ww = String.new 
          sub_console_output_value_ww = String.new
          line_number = 0
          @change = javax.swing.JButton.new 
          @change.setText("Run")
          @change.setBounds 410, 37, 100,30
          @change.add_action_listener do |e|
             unless @textfield01.getText == "q"
               if $conexion.ctany(@textfield01.getText)
                  sub_console_output_value_ww = ""
                  line_number += 1
                  num_lines = 0
                  $conexion.cver_valores_any_msg.each do |line|
                      if line != nil
                         num_lines += 1
                         i = 0
                         while i <= line.length - 1 && line[i..i] ==  " "
                            line = line[i..line.length]
                            i += 1
                         end #while i <= line.length
                         line = line.gsub("&","&amp;")
                         line = line.gsub("<","&lt;")
                         line = line.gsub(">","&gt;")
                         sub_console_output_value_ww = sub_console_output_value_ww + "<BR>" + line
                       end #if line != nil
                  end #$conexion.cver_valores_any_msg.each do |line|
                  console_output_value_ww = sub_console_output_value_ww + console_output_value_ww
                  @console_output_details_show.setText("<html>" + console_output_value_ww + "</html>")
                  self.revalidate()
                  self.repaint()
               else
                   JOptionPane.showMessageDialog panel, $conexion.conectatelnet_error_msg.to_s, "Error", JOptionPane::ERROR_MESSAGE
               end #unless @textfield01.getText == "q"
             else  
               self.dispose
             end #if @textfield01.getText =~ /q/ 
          end #@change.add_action_listener do |e|
          
          @close = javax.swing.JButton.new 
          @close.setText("Exit")
          @close.setBounds 520, 37, 100,30
          @close.add_action_listener do |e|
              self.dispose
          end #@close.add_action_listener do |e|
          
          panel.add @change
          panel.add @close

          panel.add @field01
          panel.add @textfield01
          panel.add @console_output_details_show_s

          panel.add fondo_secundario
          panel.add fondo_principal
          
          @field01.setText("Command  ")
          @textfield01.setText("?")
             
          self.revalidate()
          self.repaint()
        
        
          self.setDefaultCloseOperation JFrame::DISPOSE_ON_CLOSE
          self.setSize java.awt.Dimension.new(710,610)
          self.setResizable(false)
          self.setLocationRelativeTo nil
          self.setVisible true
          self.setIconImage(java.awt.Toolkit::default_toolkit.get_image(java.lang.Object.new.java_class.resource("/01.png")))
      end #initUI
      

end #class JFrameNc < JFrame


class JFrameNse < JFrame

       def initialize
          super "Add a New Server"
          self.initUI
      end #initialize
      
      
      def initUI
    
          panel = JPanel.new
          panel.setLayout nil
          self.getContentPane.add panel
          
          icono_fondo_principal   = ImageIcon.new java.awt.Toolkit::default_toolkit.get_image(java.lang.Object.new.java_class.resource("/fondo_00.png"))
          
          fondo_principal = JLabel.new icono_fondo_principal
          
          fondo_principal.setBounds(0,0,350,160)
          
          
          icono_fondo_principal = ImageIcon.new(icono_fondo_principal.getImage().getScaledInstance(350,160,java.awt.Image::SCALE_FAST))
          
          fondo_principal.setIcon(icono_fondo_principal)
          

          @field01 = javax.swing.JLabel.new
          @field01.setBounds 20,20,80,20
          @textfield01 = javax.swing.JTextField.new
          @textfield01.setBounds 100,20,100,25

          @field02 = javax.swing.JLabel.new
          @field02.setBounds 20,60,80,20
          @textfield02 = javax.swing.JTextField.new
          @textfield02.setBounds 100,60,100,25
          
          
          @add_button = javax.swing.JButton.new 
          @add_button.setText(" ADD ")
          @add_button.setBounds 210, 20, 100, 65
          @add_button.add_action_listener do |e|
              if $conexion.ctany( "n " + @textfield01.getText + " " + @textfield02.getText)
                 puts $conexion.cver_valores_any_msg
              else
                 JOptionPane.showMessageDialog panel, $conexion.conectatelnet_error_msg.to_s, "Error", JOptionPane::ERROR_MESSAGE
              end #if $conexion.ctany(@textfield01.getText)
              self.dispose
          end #@add_button.add_action_listener do |e|
          
          @add_s_link = javax.swing.JButton.new
          @add_s_link.setText("Remote Server.met")
          @add_s_link.setBounds 20, 100,150, 25
          @add_s_link.add_action_listener do |e|
                self.dispose
                name = JOptionPane.showInputDialog $content_pane, "Your Server.met Link Here...", "Server.met Link", JOptionPane::INFORMATION_MESSAGE, nil, nil ,nil 
                #puts "selected link is " + name.to_s
                if name != nil
                    if $conexion.ctany("servers " + name)
                      JOptionPane.showMessageDialog $content_pane, $conexion.cver_valores_any_msg[1], "Results", JOptionPane::INFORMATION_MESSAGE
                    else
                      JOptionPane.showMessageDialog panel, $conexion.conectatelnet_error_msg.to_s, "Error", JOptionPane::ERROR_MESSAGE
                    end #if $conexion.ctany("servers " + name)
                end #if name != nil
          end #@add_s_link.add_action_listener do |e|
          
          @add_s_file = javax.swing.JButton.new
          @add_s_file.setText("Local Server.met")
          @add_s_file.setBounds 180, 100,150, 25
          @add_s_file.add_action_listener do |e|
                 self.dispose
                 unless @fc
                   javax.swing.UIManager.put("FileChooser.readOnly", true)
                   @fc = javax.swing.JFileChooser.new
                 end #unless @fc
                 returnVal = @fc.showOpenDialog($content_pane)
                 @fc.setFileSelectionMode javax.swing.JFileChooser::FILES_ONLY
                 if returnVal == javax.swing.JFileChooser::APPROVE_OPTION
                    file = @fc.getSelectedFile
                    #puts "selected file is " + file.getAbsolutePath 
                    if file != nil
                       if $conexion.ctany("servers " + file.getAbsolutePath)
                         JOptionPane.showMessageDialog $content_pane, $conexion.cver_valores_any_msg[1], "Results", JOptionPane::INFORMATION_MESSAGE
                       else
                         JOptionPane.showMessageDialog panel, $conexion.conectatelnet_error_msg.to_s, "Error", JOptionPane::ERROR_MESSAGE
                       end #if $conexion.ctany("servers " + file.getAbsolutePath)
                    end #if name != nil
                    
                 end #if returnVal == JFileChooser::APPROVE_OPTION
                 @fc.setSelectedFile(nil)
                 
          end #@add_s_link.add_action_listener do |e|

          
          panel.add @add_button
          panel.add @add_s_link
          panel.add @add_s_file

          panel.add @field01
          panel.add @textfield01
          panel.add @field02
          panel.add @textfield02


          panel.add fondo_principal
          
          @field01.setText("Server ip")
          @field02.setText("Server port")
             
          self.revalidate()
          self.repaint()
        
        
          self.setDefaultCloseOperation JFrame::DISPOSE_ON_CLOSE
          self.setSize java.awt.Dimension.new(350,160)
          self.setResizable(false)
          self.setLocationRelativeTo nil
          self.setVisible true
          self.setIconImage(java.awt.Toolkit::default_toolkit.get_image(java.lang.Object.new.java_class.resource("/01.png")))
      end #initUI
      

end #class JFrameNse < JFrame

class Idown
   def initialize
      unless @fc
         javax.swing.UIManager.put("FileChooser.readOnly", true)
         @fc = javax.swing.JFileChooser.new
      end #unless @fc
      returnVal = @fc.showOpenDialog($content_pane)
      @fc.setFileSelectionMode javax.swing.JFileChooser::FILES_ONLY
      if returnVal == javax.swing.JFileChooser::APPROVE_OPTION
         file = @fc.getSelectedFile
         #puts "selected file is " + file.getAbsolutePath 
         if file != nil
            if $conexion.ctany("dllinks " + file.getAbsolutePath)
               JOptionPane.showMessageDialog $content_pane, "Check downloads to see results...", "Results", JOptionPane::INFORMATION_MESSAGE
               puts "dllinks results" 
               $conexion.cver_valores_any_msg.each do |result|
                  puts result
               end #$conexion.cver_valores_any_msg.each do |result|
            else
               JOptionPane.showMessageDialog panel, $conexion.conectatelnet_error_msg.to_s, "Error", JOptionPane::ERROR_MESSAGE
            end #if $conexion.ctany("servers " + file.getAbsolutePath)

            puts "Import File choosed " + file.getAbsolutePath
         end #if name != nil
      end #if returnVal == JFileChooser::APPROVE_OPTION
      @fc.setSelectedFile(nil)
   end #def initialize  
end #class Idown

class Edown
   def initialize
      unless @fc
         javax.swing.UIManager.put("FileChooser.readOnly", false)
         @fc = javax.swing.JFileChooser.new
      end #unless @fc
      returnVal = @fc.showSaveDialog($content_pane)
      @fc.setFileSelectionMode javax.swing.JFileChooser::FILES_ONLY
      if returnVal == javax.swing.JFileChooser::APPROVE_OPTION
         file = @fc.getSelectedFile
         #puts "selected file is " + file.getAbsolutePath 
         if file != nil
            #puts "Export File choosed " + file.getAbsolutePath
            if File.exists?(file.getAbsolutePath)
               JOptionPane.showMessageDialog $content_pane, "File exists you must select another name." , "Warning", JOptionPane::WARNING_MESSAGE
            else
               $file_name = file.getAbsolutePath
               EProgressBar.new
               #save_links(file.getAbsolutePath,get_array_link)
            end #if File.exists?(file.getAbsolutePath)
         end #if name != nil
      end #if returnVal == JFileChooser::APPROVE_OPTION
      @fc.setSelectedFile(nil)
   end #def initialize              
   
end #class Edown


class EProgressBar < javax.swing.JFrame
include java.lang.Runnable

  def initialize
          super "Export links progress..."
          self.initUI
  end #def initialize
  
  def initUI
    @progressBar = javax.swing.JProgressBar.new(0, 100)
    @progressBar.setStringPainted(true)
    @progressBar.setBorder(javax.swing.BorderFactory.createEmptyBorder(20, 20, 20, 20))
    
    self.add @progressBar
    self.setIconImage(java.awt.Toolkit::default_toolkit.get_image(java.lang.Object.new.java_class.resource("/01.png")))
    self.revalidate()
    self.repaint()
    self.setDefaultCloseOperation javax.swing.JFrame::DISPOSE_ON_CLOSE
    self.setSize java.awt.Dimension.new(250,80)
    self.setResizable(false)
    self.setLocationRelativeTo nil
    self.setVisible true
    
    progress = 0
    @progressBar.setValue(0)
    f = File.new($file_name,"w")
   
    if $conexion.ctany("set max_name_len 500")
       else
           JOptionPane.showMessageDialog panel, $conexion.conectatelnet_error_msg.to_s, "Error", JOptionPane::ERROR_MESSAGE
    end #if $conexion.ctany("set max_name_len 500")
    
    if $conexion.ctestado 
       if $conexion.ctdescargas(" ")
          total = 0
          $conexion.ctdescargas_retorno_msg.each do |dla|
             if dla =~ /\[D/
                total += 1
             end #if dla =~ /\[D/
          end #$conexion.ctdescargas_retorno_msg.each do |dla|  
          $conexion.ctdescargas_retorno_msg.each do |dla|
             if dla =~ /\[D/
                id = $conexion.tratar_mensaje(dla.to_s)[0]
                id = id.gsub("\[","")
                id = id.gsub("D","")
                id = id.gsub("\]","")
                @name_s = $conexion.formatea_descarga($conexion.tratar_mensaje(dla.to_s),'File')
                if $conexion.ctdescargas(id.to_s)
                   i = 0
                   $conexion.ctdescargas_retorno_msg.each do |dla_d|
                      if  i == 3 ##urn
                          urn_s = dla_d.to_s
                          @urn_s = urn_s.gsub("urn:ed2k:","")
                      end #if  i == 3 ##urn
                      if  i == 4 ##size
                          size_s = dla_d.to_s
                          size_s = size_s.gsub(" ","")
                          @size_s = size_s.gsub("Total","")
                      end #if  i == 4 ##size
                      i += 1 
                   end #$conexion.ctdescargas_retorno_msg.each do |dla_d|
                        
                end #if $conexion.ctdescargas(id.to_s)

                #puts "ed2k://|file|" + @name_s + "|" + @size_s + "|" + @urn_s + "|/"
                f.write("ed2k://|file|" + @name_s + "|" + @size_s + "|" + @urn_s + "|/\n")     
                progress += 1   

                #debug
                #puts "progress" + progress.to_s
                #puts "total" + total.to_s
                #debug

                total_d = BigDecimal.new(total)

                #debug
                #puts (total_d/100).to_s('F')
                #puts "setProgress" + (progress / (a/100)).to_s('F')
                #debug

                @progressBar.setValue (progress / (total_d/100)).to_i

                self.update(self.getGraphics)                
                self.revalidate()
                self.repaint()
                

             end #if dla =~ /\[D/
          end #$conexion.ctdescargas_retorno_msg.each do |dla|
       else
          JOptionPane.showMessageDialog panel, $conexion.conectatelnet_error_msg.to_s, "Error", JOptionPane::ERROR_MESSAGE
       end #if $conexion.ctdescargas(" ") 
   
    else
       JOptionPane.showMessageDialog panel, $conexion.conectatelnet_error_msg.to_s, "Error", JOptionPane::ERROR_MESSAGE
    end #if $conexion.ctestado 
    
    if $conexion.ctany("set max_name_len 50")
    else
       JOptionPane.showMessageDialog panel, $conexion.conectatelnet_error_msg.to_s, "Error", JOptionPane::ERROR_MESSAGE
    end #if $conexion.ctany("set max_name_len 50")
    
    f.close    
    
    self.dispose
  end #def initUI
  
end #class ProgressBarDemo 

class STorrent
   def initialize
      unless @fc
         javax.swing.UIManager.put("FileChooser.readOnly", true)
         @fc = javax.swing.JFileChooser.new
      end #unless @fc
      returnVal = @fc.showOpenDialog($content_pane)
      @fc.setFileSelectionMode javax.swing.JFileChooser::FILES_ONLY
      if returnVal == javax.swing.JFileChooser::APPROVE_OPTION
         file = @fc.getSelectedFile
         puts "selected file is " + file.getAbsolutePath 
         if file != nil
            if $conexion.ctdownurl(file.getAbsolutePath)
               JOptionPane.showMessageDialog $content_pane, "Download has been added...", "Information", JOptionPane::INFORMATION_MESSAGE
            else
               JOptionPane.showMessageDialog $content_pane, $conexion.conectatelnet_error_msg.to_s, "Error", JOptionPane::ERROR_MESSAGE
            end #if $conexion.ctdownurl(file.getAbsolutePath)

         end #if name != nil
      end #if returnVal == JFileChooser::APPROVE_OPTION
      @fc.setSelectedFile(nil)
   end #def initialize  
end #class STorrent


class JFrameTorrent < JFrame

      def initialize
          self.initUI
      end #initialize
      
      
      def initUI

          @torrentPanelIcon = ImageIcon.new java.awt.Toolkit::default_toolkit.get_image(java.lang.Object.new.java_class.resource("/iconb.png"))
          buttonsb = [javax.swing.UIManager.getString("OptionPane.okButtonText", java.util.Locale.getDefault),javax.swing.UIManager.getString("OptionPane.cancelButtonText", java.util.Locale.getDefault),"Choose a file..."].to_java
          panel = javax.swing.JPanel.new
          panel.setLayout javax.swing.BoxLayout.new(panel, javax.swing.BoxLayout::Y_AXIS)
                             
          panel.add(javax.swing.JLabel.new(" "))
          panel.add(javax.swing.JLabel.new("Your Torrent url Here..."))
          text_f = javax.swing.JTextField.new 25
          text_f.setMaximumSize(java.awt.Dimension.new(java.lang.Integer::MAX_VALUE, text_f.getPreferredSize.height)) 
          panel.add(text_f)
      
          nameb = JOptionPane.showOptionDialog  $busquedas, panel, "Torrent Url", JOptionPane::YES_NO_CANCEL_OPTION,JOptionPane::PLAIN_MESSAGE, @torrentPanelIcon , buttonsb  ,nil   
      
         if nameb == JOptionPane::YES_OPTION
            $conexion.ctdownurl(text_f.getText)
            JOptionPane.showMessageDialog $content_pane, "Download has been added...", "Information", JOptionPane::INFORMATION_MESSAGE
         end #if nameb == JOptionPane::YES_OPTION
         
         if nameb == JOptionPane::CANCEL_OPTION
            STorrent.new
         end #if nameb == JOptionPane::YES_OPTION

         
      end #def initUI
      
end #class JFrameTorrent < JFrame
