package de.nplusc.izc.tools.baseTools;

//import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader; //Woher kam enn der Scheissdreck???
//import expectj.*;
//import java.util.Map;
//import java.util.logging.Level;
//import java.util.logging.Logger;
//import org.apache.commons.io.*;
import de.nplusc.izc.tools.IOtools.FileTK;
import java.awt.Dimension;
import java.io.*;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.prefs.Preferences;
import javax.swing.*;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.Executor;

/**
 *
 * @author LH
 */
public class Tools
{

    public static boolean execCrashed = false;
    private static boolean ContState;
    private static JDialog d = new JDialog();
    //private boolean ProcessReturn;
    private static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
    private static int logNumber;

    protected Tools()
    {
        //disabled Constructor
    }

    public static String padLeft(final int count, final String base, final char filler)
    {
        if (base == null)
        {
            throw new NullPointerException();
        }
        if (base.length() >= count)
        {
            return base;
        }
        char[] prefix = new char[count - base.length()];
        Arrays.fill(prefix, filler);
        return new StringBuilder(count).append(prefix).append(base).toString();
    }

    public static void wait(int time)
    {

        try
        {
            Thread.sleep(time);
        }
        catch (InterruptedException ex)
        {
            ex.printStackTrace();
        }
    }

    public static String getSHAString(String s)
    {
        try
        {
            MessageDigest sha = MessageDigest.getInstance("SHA");
            @SuppressWarnings("StringBufferMayBeStringBuilder")
            StringBuffer t = new StringBuffer();
            byte[] digest = sha.digest(s.getBytes());
            for (byte b : digest)
            {
                Tools.DebugHelperPrint(String.format("%02x", b), true, "Toolkit.enableFinerDebug");
                int val = ((int) b) & 0xff;
                if (val < 16)
                {
                    t.append("0");
                }
                t.append(Integer.toHexString(val));
            }
            //System.out.println();
            return t.toString();
        }
        catch (Exception ex)
        {
            return null;
        }
    }

    public static int[] lettersToInt(String ip)
    {
        ip = ip.toLowerCase();
        char[] it = ip.toCharArray();
        int[] ri = new int[it.length];
        for (int i = 0; i < ri.length; i++)
        {
            ri[i] = (int) it[i];
        }
        return ri;
    }

    public static int addIntsTogether(int[] i)
    {
        int t = 1;
        for (int X : i)
        {
            t *= X;
        }
        return t;
    }

    public static int addIntsTogether(HashMap<String, String> m, boolean ints)
    {
        //boolean ints = !noints;
        String[] mk;
        Object[] mk2 = m.keySet().toArray();
        mk = new String[mk2.length];
        for (int i = 0; i < mk.length; i++)
        {
            String s = "";
            //s.toString();
            mk[i] = mk2[i].toString();
        }
        int t = 1;
        for (String s : mk)
        {
            if (ints == true)
            {
                t *= Integer.valueOf(m.get(s));
            }
            else
            {
                int t2 = 0;
                int[] itmp = lettersToInt(m.get(s));
                for (int i : itmp)
                {
                    t2 += i;
                }
                t += t2;
            }
        }
        return t;
    }

    public static String stringAdd(String s1, String s2)
    {
        char[] ca1 = s1.toLowerCase().toCharArray();
        char[] ca2 = s2.toLowerCase().toCharArray();
        int[] ci1 = new int[ca1.length];
        int[] ci2 = new int[ca2.length];

        String t;
        for (int i = 0; i < ca1.length; i++)
        {
            try
            {
                ci1[i] = Integer.valueOf(Character.toString(ca1[i]));
            }
            catch (Exception ex)
            {
                t = Character.toString(ca1[i]);
                ci1[i] = ALPHABET.indexOf(t) + 1;
            }
        }
        for (int i = 0; i < ca2.length; i++)
        {
            try
            {
                ci2[i] = Integer.valueOf(Character.toString(ca2[i]));
            }
            catch (Exception ex)
            {
                t = Character.toString(ca2[i]);
                ci2[i] = ALPHABET.indexOf(t) + 1;
            }
        }
        int l = ci1.length;
        if (ci1.length < ci2.length)
        {
            l = ci2.length;
        }
        int[] op = new int[l];
        int i1, i2, i3;
        for (int i = 0; i < l; i++)
        {
            try
            {
                i1 = ci1[i];
            }
            catch (Exception ex)
            {
                i1 = 0;
            }
            try
            {
                i2 = ci2[i];
            }
            catch (Exception ex)
            {
                i2 = 0;
            }
            i3 = i1 + i1;
            if (i3 > 26)
            {
                i3 -= 26;
            }
            op[i] = i3;
        }

        return backString(op);
    }

    public static String backString(int[] ia)
    {
        int l = ia.length, i1;
        String[] r1 = new String[l];
        String rl = "";
        for (int i = 0; i < l; i++)
        {
            i1 = ia[i] - 1;
            if (i1 == -1)
            {
                i1++;
            }
            r1[i] = Character.toString(ALPHABET.charAt(i1));
        }
        for (String s : r1)
        {
            rl += s;
        }



        return rl;
    }

    public static String stringFold(String s, int l, int rec)//halbiert Strings und addiert die Hälften
    {
        int l1 = s.length() / 2;//,l2;
        String sh1, sh2, so1;
        //int l2=l1+1-1;//Neuen Wet erzeugen!!
        //l2=l1*2
        if ((l1 * 2) == s.length() - 1)
        {
            l1++;
        }
        sh1 = s.substring(0, l1++);
        sh2 = s.substring(l1++);
        so1 = stringAdd(sh1, sh2);
        if (so1.length() > l && rec > 0)
        {
            so1 = stringFold(so1, l, rec--);
        }

        return so1;
    }

    public static String stringFold(String s, int l)
    {
        return stringFold(s, l, 30);
    }

    public static boolean dlg(boolean mode, String MSGLine1, String MSGLine2)
    {
        return dlg(mode, MSGLine1, MSGLine2, "Fortfahren", "Abbrechen");
    }

    public static boolean dlg(boolean mode, String MSGLine1, String MSGLine2, String btnLeft, String btnRight)
    {
        JPanel p = new JPanel();
        javax.swing.JButton btnYes = new javax.swing.JButton();
        javax.swing.JButton btnNo = new javax.swing.JButton();
        JLabel lblMSG = new JLabel();
        JLabel lblMSG2 = new JLabel();



        d.setModal(mode);
        mode = true;
        if (mode == true)
        {
            //Ausgeblendet da nur Fenster-Layout
            // <editor-fold defaultstate="collapsed" desc="Layout-Code">
            btnNo.setText(btnRight);
            btnNo.addActionListener(new java.awt.event.ActionListener()
            {
                public void actionPerformed(java.awt.event.ActionEvent evt)
                {
                    btnNoActionPerformed(evt);
                }
            });

            btnYes.setText(btnLeft);
            btnYes.addActionListener(new java.awt.event.ActionListener()
            {
                public void actionPerformed(java.awt.event.ActionEvent evt)
                {
                    btnYesActionPerformed(evt);
                }
            });

            lblMSG.setText(MSGLine1);

            lblMSG2.setText(MSGLine2);

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(p);
            p.setLayout(layout);
            layout.setHorizontalGroup(
                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                    .addGap(36, 36, 36)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblMSG, javax.swing.GroupLayout.PREFERRED_SIZE, 258, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                    .addComponent(btnYes)
                    .addGap(18, 18, 18)
                    .addComponent(btnNo))
                    .addGroup(layout.createSequentialGroup()
                    .addGap(40, 40, 40)
                    .addComponent(lblMSG2)))
                    .addContainerGap(63, Short.MAX_VALUE)));
            layout.setVerticalGroup(
                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(lblMSG, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(lblMSG2)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnYes)
                    .addComponent(btnNo))
                    .addContainerGap(71, Short.MAX_VALUE)));
            // </editor-fold>

            //Auskommentierter Fail!
            // <editor-fold defaultstate="collapsed" desc="FailTry">
/*javax.swing.JButton btnYes = new javax.swing.JButton();
             btnYes.addActionListener
             (
             new java.awt.event.ActionListener()
             {
             public void actionPerformed(java.awt.event.ActionEvent evt)
             {
             btnCTNActionPerformed(evt);
             }
             }
             );
             javax.swing.JButton btnNo = new javax.swing.JButton();
             btnNo.addActionListener
             (
             new java.awt.event.ActionListener()
             {
             public void actionPerformed(java.awt.event.ActionEvent evt)
             {
             btnSTPActionPerformed(evt);
             }
             }
             );
             btnYes.setText("Fortsetzen");
             btnNo.setText("Abbrechen");*/
            //d.add(new JButton());// </editor-fold>


            //p.add(btnYes);
            //p.add(btnNo);
            d.add(p);

            d.setSize(p.getPreferredSize());
            d.setVisible(true);

            //btnNo.setText(btnRight);
            //btnYes.setText(btnLeft);
        }
        else
        {
            //TODO working.dialog
            return true;
        }

        return ContState;
    }

    public static void log(final String logLine)
    {
        /*Thread t = new Thread(new Runnable() {

         //@SuppressWarnings("SleepWhileInLoop")//Hx
         public void run() {*/
        /*
         if(logLines>5000)
         {
         cmdScreen.setText("");
         System.err.println("log_flushed");
         logLines=0;
         }
         loglineG = logLine;
         //System.err.println("LOG()");
         cmdScreen.setText(cmdScreen.getText()+"\n"+loglineG);
         loglineG = "";
         logLines++;
         d.validate();
    
         /*}
         });
         t.start();*/
        Tools.DebugHelperPrint(logLine, true, "Toolkit.enableCoreDebug");
    }

    // <editor-fold defaultstate="collapsed" desc="Altlast">
    /*
     public static void jCmd() {
     javax.swing.JButton Eingabe;
     javax.swing.JButton jButton2;
     javax.swing.JPanel jPanel1;
     javax.swing.JScrollPane jScrollPane1;

     // End of variables declaration
     jPanel1 = new javax.swing.JPanel();
     jScrollPane1 = new javax.swing.JScrollPane();
     cmdScreen = new javax.swing.JTextArea();
     Eingabe = new javax.swing.JButton();
     jButton2 = new javax.swing.JButton();
     cmdline = new javax.swing.JTextField();
        
     Eingabe.addActionListener(new java.awt.event.ActionListener() {
     public void actionPerformed(java.awt.event.ActionEvent evt) {
     eingabeActionPerformed(evt);
     }
     });

     jButton2.addActionListener(new java.awt.event.ActionListener() {
     public void actionPerformed(java.awt.event.ActionEvent evt) {
     cmdQuitActionPerformed(evt);
     }
     });
        
     //setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

     cmdScreen.setColumns(20);
     cmdScreen.setLineWrap(true);
     cmdScreen.setRows(5);
     jScrollPane1.setViewportView(cmdScreen);

     Eingabe.setText("Eingabe");

     jButton2.setText("Beenden");

     javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
     jPanel1.setLayout(jPanel1Layout);
     jPanel1Layout.setHorizontalGroup(
     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
     .addGroup(jPanel1Layout.createSequentialGroup()
     .addContainerGap()
     .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
     .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 733, Short.MAX_VALUE)
     .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
     .addComponent(cmdline, javax.swing.GroupLayout.PREFERRED_SIZE, 493, javax.swing.GroupLayout.PREFERRED_SIZE)
     .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
     .addComponent(Eingabe, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
     .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
     .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)))
     .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
     );
     jPanel1Layout.setVerticalGroup(
     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
     .addGroup(jPanel1Layout.createSequentialGroup()
     .addContainerGap()
     .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 342, javax.swing.GroupLayout.PREFERRED_SIZE)
     .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
     .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
     .addComponent(Eingabe)
     .addComponent(cmdline, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
     .addComponent(jButton2))
     .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
     );
     d.setTitle("jCmd");
     d.setResizable(false);
     d.setModal(true);
     d.add(jPanel1);
     Dimension dms = jPanel1.getPreferredSize();
     dms.setSize(dms.getWidth()+5,dms.getHeight()+30);//HACK!!
     d.setSize(dms);
     cmdScreen.setEditable(false);
     //d.p
     d.setVisible( true );
        
     /*
     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
     getContentPane().setLayout(layout);
     layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
     .addGap(663, 663, 663)
     .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
     .addGap(1382, 1382, 1382))
     );
     layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
     .addGroup(layout.createSequentialGroup()
     .addGap(65, 65, 65)
     .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
     .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
     );* /

     //pack();
     }
    
    
    
     */
    //</editor-fold>
    public static void runSingleCmd(String... runnable)
    {
        runSingleCmd(true, runnable);
    }

    public static void runSingleCmd(boolean logg, String... runnable)
    {
        runSingleCmd(true, false, runnable);
    }

    public static void runSingleCmd(boolean logg, boolean legacy, String... runnable)
    {
        runSingleCmd(logg, legacy, false, runnable);
    }

    

    
    
    public static String runSingleCmd(boolean logg, boolean legacy, boolean sret, String... runnable)
    {
        return runSingleCmd(logg, legacy, sret, null, runnable);
    }

    public static String runSingleCmd(boolean logg, boolean legacy, boolean sret,JTextArea tx, String... runnable)
    {
    return runSingleCmd(logg, legacy, sret, false, tx, runnable);
    }
    public static String runSingleCmd(boolean logg, boolean legacy, boolean sret,boolean asArray,JTextArea tx, String... runnable)
    {
        return runSingleCmd(logg, legacy, sret, asArray, tx, null, runnable);
    }
    public static String runSingleCmd(boolean logg, boolean legacy, boolean sret,boolean asArray,JTextArea tx, PrintStream pw, String... runnable)
    {
        execCrashed = false;
                String cret = "";
        //String rn = runnable[0];
        //String[]temp = runnable[0].split("\\.");
        /*if("bat".equals(temp[temp.length-1]))
         {
            
         runnable[0] = "cmd.exe";
         runnable[1] = "/c";
         runnable[2] = "\"\""+rn+"\" \"";
         }*/
        //else
        //{
        //String[] r2 = runnable;
        //}
        String cmd = "";
        if(!asArray)
        {
            for (String string : runnable)
            {
                cmd += string + " ";
                cmd = cmd.trim();

            }
        }
        Tools.DebugHelperPrint(cmd, true, "Toolkit.enableFinerDebug");
        try
        {
            Executor e = null;
            CommandLine c = null;
            if (!legacy)
            {
                c = new CommandLine("cmd.exe");
                c.addArgument(cmd);
                e = new DefaultExecutor();
            }
            else
            {
                Process p = null;
                if(!asArray)
                    p=Runtime.getRuntime().exec(cmd);
                else
                    p=Runtime.getRuntime().exec(runnable);
                if(tx!=null)
                {
                    Scanner scanner = new Scanner(p.getInputStream());
                    Scanner scannerE = new Scanner(p.getErrorStream());
                    while ((scanner.hasNextLine()||scanner.hasNextLine()))
                    {
                        if(scanner.hasNextLine())
                        {
                            String l = scanner.nextLine();
                            tx.append(l+"\n");
                            if(pw!=null)
                                pw.println(l);
                        }
                        if(scannerE.hasNextLine())
                        {
                            String l = scannerE.nextLine();
                            tx.append(l+"\n");
                            if(pw!=null)
                                pw.println(l);
                        }
                    }
                }
                if (sret)
                {
                    cret = new BufferedReader(new InputStreamReader(p.getInputStream())).readLine();
                }
                int rv = p.waitFor();
                if(rv>0)
                {
                    execCrashed=true;
                }
            }
            //InputStream s = p.getInputStream();//TODO LogFile via CommonsExec
            //InputStream se = p.getErrorStream();//<<<
            //s.
            //BufferedWriter log = new BufferedWriter(new FileWriter("C:\\log"+logNumber+".txt"));
            // BufferedWriter err = new BufferedWriter(new FileWriter("C:\\err"+logNumber+".txt"));
            Tools.DebugHelperPrint(cmd, true, "Toolkit.enableCoreDebug");
            if (logg)
            {
                Tools.DebugHelperPrint("Logg", true, "Toolkit.enableFinerDebug");
                //IOUtils.copy(s,log);
                //IOUtils.copy(se,err);
                logNumber++;
            }
            // int i = p.waitFor();
            // log = new BufferedWriter(new FileWriter("C:\\log"+logNumber+".txt"));
            // err = new BufferedWriter(new FileWriter("C:\\err"+logNumber+".txt"));
            if (!legacy)
            {
                e.execute(c);
            }
            //if(logg)
            // {
            //     System.err.println("Logg");
            //     IOUtils.copy(s,log);
            //     IOUtils.copy(se,err);
            //      logNumber++;
            //  }
            // if(i>0)
            //{
            //    dlg(true,"Error in ",cmd);
            //   throw new Exception();//Löst SetupError aus
            //}
            // else
            //  {
            //SetupContinue
            //  }

            /*
             ProcessBuilder p = new ProcessBuilder(runnable);
             File f = new File(new File(rn).getParent());
             p.directory(f);
             Map<String,String> env = p.environment();
             Tools.cmdrunner(p,runnable,false);*/
            //return false;//Altlast
        }
        catch (Exception ex)
        {
            Tools.DebugHelperPrint("Error@"+cmd, true, "Toolkit.enableFinerDebug");
            Tools.DebugHelperPrint(ex.toString(), true, "Toolkit.enableFinerDebug");
            Tools.DebugHelperPrint(ex.getMessage(), true, "Toolkit.enableFinerDebug");
            //TODO:SetupError_Here
            //Logger.getLogger(Tools.class.getName()).log(Level.SEVERE, null, ex);
        }
        return cret;
    }
    
    
    // <editor-fold defaultstate="collapsed" desc="Altlast">
    /*
     public static int cmdrunner(final ProcessBuilder p, final String[] runnable)
     {
     return cmdrunner(p,runnable,true);
     }*/
    /*
     public static int cmdrunner(final ProcessBuilder p, final String[] runnable, final boolean bjCmd)
     {
     //try {
     Thread t = new Thread(new Runnable() {

     //@SuppressWarnings("SleepWhileInLoop")//Hx
     @SuppressWarnings("CallToThreadDumpStack")
     public void run() {

     //try {
     d = null;
     d = new JDialog();
                
     try {
     //e.
     //exp=null;
     //exp = new ExpectJ(5);
     String run = arraytools.ArrayKleben(runnable," ");
     //Spawn s = exp.spawn(run);
     try {
     e = p.start();
     //s.expectErr(".");
     } catch (Exception ex) {
     ex.printStackTrace();
     }
     output = new StreamGobbler(e.getInputStream(), ">>");
     error = new StreamGobbler(e.getErrorStream(), "!>");
     output.start();
     error.start();
                    
     if(bjCmd)
     {
     jCmd();
     }
     /* else
     {
     while(error.)
     {
                            
     }
     }*
     * /
     //int r = 1;
     } catch (Exception ex) {
     ex.printStackTrace();
     }



     //e.
     /*}
     catch (IOException ex) 
     {
     ex.printStackTrace();
     r=100;
     }*
     * /

     }
     });
        
     t.start();
     /*NOP* /
     r = 1;
     return r;
     /*} catch (IOException ex) {
     ex.printStackTrace();
     }
     return 0;* /
        
        
     }
    
    
    
     */
    //</editor-fold>
    private static void btnYesActionPerformed(java.awt.event.ActionEvent evt)
    {
        ContState = true;
        hideDialog();
        d = null;
        d = new JDialog();
    }

    // <editor-fold defaultstate="collapsed" desc="Altlast">
    /*
     private static void cmdQuitActionPerformed(java.awt.event.ActionEvent evt)
     {
        
     hideDialog();
     d=null;
     d = new JDialog();
     e.destroy();
     //error.
     }*/
    //@SuppressWarnings("CallToThreadDumpStack")
     /*   private static void eingabeActionPerformed(java.awt.event.ActionEvent evt)
     {
     String cmd = cmdline.getText()+"\n";
     //NOP;
     OutputStreamWriter osw = new OutputStreamWriter(e.getOutputStream());
     BufferedWriter bw = new BufferedWriter(osw);
     try {
     bw.write(cmd,0,cmd.length());
     log("I>"+cmd);
     //output.start();
     //error.start();
     } catch (IOException ex) {
     ex.printStackTrace();
     }
     d.validate();
     }*/
    //</editor-fold>
    private static void btnNoActionPerformed(java.awt.event.ActionEvent evt)
    {
        ContState = false;
        hideDialog();
        d = null;
        d = new JDialog();
    }

    public static void hideDialog()
    {
        d.setVisible(false);
    }

    
        public static String getRelativePath(String absolutePath, String basePath)//Initializer für Hack!
    {
       return FileTK.getRelativePath(absolutePath, basePath);
    }
    
    
    @SuppressWarnings("ManualArrayToCollectionCopy")//Quick and Dirty Hack
    public static Object[] truncateArray(Object[] toTrunc, int number, boolean fromBack)
    {
        //number++;//fucked up error!
        if (toTrunc.length - number < 1)//Error bei Zahlen <0
        {
            throw new IllegalArgumentException("Shortened Array to zero or lower!!!");
        }
        Object[] out = new Object[toTrunc.length - number];
        if (fromBack == true)//Hinten weg
        {
            for (int i = 0; i < toTrunc.length - number; i++)
            {
                out[i] = toTrunc[i];
            }
        }
        else
        {
            for (int i = 0; i > toTrunc.length - number; i++)
            {
                int j = i + number;
                out[i] = toTrunc[j];
            }
        }
        return out;
    }

    public static String getInputString(String title)
    {
        return getInputString(title, false);
    }

    public static String getInputString(String title, boolean txArea)
    {
        Object txfIP;//Hack!!!!
        if (txArea == true)
        {
            txfIP = new JTextArea(20, 40);
        }
        else
        {
            txfIP = new JTextField(30);
        }
        //passwordField.setEchoChar('#');
        JOptionPane.showMessageDialog(
                null,
                txfIP,
                title,
                JOptionPane.OK_OPTION);
        String ret = "";
        if (txArea == true)
        {
            ret = ((JTextArea) txfIP).getText();
        }
        else
        {
            ret = ((JTextField) txfIP).getText();
        }
        return ret;
    }

    public static void saveSerializedObject(Object[] o, String p)
    {
        try
        {
            FileOutputStream fos = new FileOutputStream(p);
            ObjectOutputStream os = new ObjectOutputStream(fos);
            os.writeObject(o);
        }
        catch (Exception e)
        {
            //NOP
        }
    }

    public static Object[] loadSerializedObject(String p)
    {
        try
        {
            FileInputStream fis = new FileInputStream(p);
            ObjectInputStream o = new ObjectInputStream(fis);
            return (Object[]) o.readObject();
        }
        catch (Exception e)
        {
            Tools.DebugHelperPrint("Loader.failure", true, "Toolkit.enableCoreDebug");
            e.printStackTrace();
            return null;
        }
    }
    private static JList lstItemz;
    private static String retval;

    public static String listSelectorPopup(String[] elemsToChoseFrom, String Question)
    {
        JPanel p = new JPanel();
        d.setModal(true);
        lstItemz = new javax.swing.JList();
        JLabel txtQuestion = new javax.swing.JLabel();
        JScrollPane jScrollPane1 = new javax.swing.JScrollPane();

        JButton btnCont = new javax.swing.JButton();
        JButton btnAbort = new javax.swing.JButton();

        //setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        txtQuestion.setText(Question);

        lstItemz.setModel(new javax.swing.DefaultListModel());
        jScrollPane1.setViewportView(lstItemz);

        btnCont.setText("Fertig");

        btnAbort.setText("Doch nicht");

        ((DefaultListModel) (lstItemz.getModel())).clear();
        //  String[] availUPIDs = dbc.querySIngleColumn("Select UPID20 from upidlst");

        for (String elem : elemsToChoseFrom)
        {
            ((DefaultListModel) (Tools.lstItemz.getModel())).addElement(elem);
        }


        retval = "NOP";

        btnCont.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                retval = (String) lstItemz.getSelectedValue();
                hideDialog();
                d = null;
                d = new JDialog();
            }
        });

        btnAbort.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                retval = "NOP";
                hideDialog();
                d = null;
                d = new JDialog();;
            }
        });



        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(p);
        p.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1)
                .addGroup(layout.createSequentialGroup()
                .addGap(47, 47, 47)
                .addComponent(txtQuestion, javax.swing.GroupLayout.PREFERRED_SIZE, 494, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(95, Short.MAX_VALUE))
                .addGroup(layout.createSequentialGroup()
                .addComponent(btnCont, javax.swing.GroupLayout.PREFERRED_SIZE, 327, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnAbort, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(txtQuestion)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 215, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(btnCont, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
                .addComponent(btnAbort, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))));
        d.add(p);
        Dimension d2 = p.getPreferredSize();
        d2 = new Dimension(d2.width, d2.height + 40);
        d.setSize(d2);
        d.setVisible(true);



        return retval;
    }

    public static String FileChooseDlg(javax.swing.JFrame frm)
    {
        return FileChooseDlg(frm,false,false,null);
    }
    
    public static String FileChooseDlg(javax.swing.JFrame frm,boolean restrict,boolean dirsOnly,String[] exts)
    {
        JFileChooser fc = new JFileChooser();
        String ret = "";
        //fc.
        if(!dirsOnly)
            fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        else
            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        if(restrict)
        {
            FIleLImiter f = new FIleLImiter();
            f.setExtensions(exts);
            fc.setFileFilter(f);
        }
        int returnVal = fc.showOpenDialog(frm);
        if (returnVal == JFileChooser.APPROVE_OPTION)
        {
            File file = fc.getSelectedFile();
            try
            {
                //This is where a real application would open the file.
                ret = file.getCanonicalPath();
            }
            catch (IOException ex)
            {
            }
        }
        return ret;
        //log.setCaretPosition(log.getDocument().getLength());
    }
    private static int retnumber = 0;

    public static int numberInputWIndow(int maxNbr, int[] blockednumbrs)
    {
        log("niw");
        k1 = new javax.swing.JButton();
        k2 = new javax.swing.JButton();
        k3 = new javax.swing.JButton();
        k4 = new javax.swing.JButton();
        k5 = new javax.swing.JButton();
        k6 = new javax.swing.JButton();
        k7 = new javax.swing.JButton();
        k8 = new javax.swing.JButton();
        k9 = new javax.swing.JButton();
        btnOk = new javax.swing.JButton();
        btnAbbr = new javax.swing.JButton();
        entrdNbr = new javax.swing.JTextField();
        k0 = new javax.swing.JButton();
        kC = new javax.swing.JButton();
        btnClear = new javax.swing.JButton();



        d = new JDialog();
        d.setModal(true);
        numEntered = 0;
        numMax = maxNbr;
        k0.setEnabled(true);
        k1.setEnabled(true);
        k2.setEnabled(true);
        k3.setEnabled(true);
        k4.setEnabled(true);
        k5.setEnabled(true);
        k6.setEnabled(true);
        k7.setEnabled(true);
        k8.setEnabled(true);
        k9.setEnabled(true);
        retnumber = 0;

        for (int i : blockednumbrs)
        {
            if (i == 0)
            {
                k0.setEnabled(false);
            }
            if (i == 1)
            {
                k1.setEnabled(false);
            }
            if (i == 2)
            {
                k2.setEnabled(false);
            }
            if (i == 3)
            {
                k3.setEnabled(false);
            }
            if (i == 4)
            {
                k4.setEnabled(false);
            }
            if (i == 5)
            {
                k5.setEnabled(false);
            }
            if (i == 6)
            {
                k6.setEnabled(false);
            }
            if (i == 7)
            {
                k7.setEnabled(false);
            }
            if (i == 8)
            {
                k8.setEnabled(false);
            }
            if (i == 9)
            {
                k9.setEnabled(false);
            }


        }





        k1.setText("1");
        k1.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                k1ActionPerformed(evt);
            }
        });

        k2.setText("2");
        k2.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                k2ActionPerformed(evt);
            }
        });

        k3.setText("3");
        k3.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                k3ActionPerformed(evt);
            }
        });

        k4.setText("4");
        k4.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                k4ActionPerformed(evt);
            }
        });

        k5.setText("5");
        k5.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                k5ActionPerformed(evt);
            }
        });

        k6.setText("6");
        k6.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                k6ActionPerformed(evt);
            }
        });

        k7.setText("7");
        k7.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                k7ActionPerformed(evt);
            }
        });

        k8.setText("8");
        k8.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                k8ActionPerformed(evt);
            }
        });

        k9.setText("9");
        k9.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                k9ActionPerformed(evt);
            }
        });

        btnOk.setText("OK");
        btnOk.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                btnOkActionPerformed(evt);
            }
        });

        btnAbbr.setText("ABBR");
        btnAbbr.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                btnAbbrActionPerformed(evt);
            }
        });

        k0.setText("0");
        k0.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                k0ActionPerformed(evt);
            }
        });

        kC.setText("<--");
        kC.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                kCActionPerformed(evt);
            }
        });

        btnClear.setText("Clr");
        btnClear.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                btnClearActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(d.getContentPane());
        d.getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addGroup(layout.createSequentialGroup()
                .addComponent(btnOk, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnAbbr, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(k1, javax.swing.GroupLayout.DEFAULT_SIZE, 51, Short.MAX_VALUE)
                .addComponent(k4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(k2, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE)
                .addComponent(k5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(k3, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE)
                .addComponent(k6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(k7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnClear, javax.swing.GroupLayout.DEFAULT_SIZE, 51, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(k0, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE)
                .addComponent(k8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(k9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(kC, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addComponent(entrdNbr))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addGap(7, 7, 7)
                .addComponent(entrdNbr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(k3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(k2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(k1, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(k6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(k5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(k4, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(k7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(k8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(k9, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(k0, javax.swing.GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE)
                .addComponent(kC, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnClear, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(btnAbbr, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(btnOk, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 8, Short.MAX_VALUE)));

        d.pack();
        d.setVisible(true);
        log("Scheißdreck");
        return retnumber;
    }// </editor-fold>

    private static void k1ActionPerformed(java.awt.event.ActionEvent evt)
    {
        if (numEntered >= numMax)
        {
            return;
        }
        entrdNbr.setText(entrdNbr.getText() + "1");
        numEntered++;
    }

    private static void k2ActionPerformed(java.awt.event.ActionEvent evt)
    {
        if (numEntered >= numMax)
        {
            return;
        }
        entrdNbr.setText(entrdNbr.getText() + "2");
        numEntered++;
    }

    private static void k3ActionPerformed(java.awt.event.ActionEvent evt)
    {
        if (numEntered >= numMax)
        {
            return;
        }
        entrdNbr.setText(entrdNbr.getText() + "3");
        numEntered++;
    }

    private static void k4ActionPerformed(java.awt.event.ActionEvent evt)
    {
        if (numEntered >= numMax)
        {
            return;
        }
        entrdNbr.setText(entrdNbr.getText() + "4");
        numEntered++;
    }

    private static void k5ActionPerformed(java.awt.event.ActionEvent evt)
    {
        if (numEntered >= numMax)
        {
            return;
        }
        entrdNbr.setText(entrdNbr.getText() + "5");
        numEntered++;
    }

    private static void k6ActionPerformed(java.awt.event.ActionEvent evt)
    {
        if (numEntered >= numMax)
        {
            return;
        }
        entrdNbr.setText(entrdNbr.getText() + "6");
        numEntered++;
    }

    private static void k7ActionPerformed(java.awt.event.ActionEvent evt)
    {
        if (numEntered >= numMax)
        {
            return;
        }
        entrdNbr.setText(entrdNbr.getText() + "7");
        numEntered++;
    }

    private static void k8ActionPerformed(java.awt.event.ActionEvent evt)
    {
        if (numEntered >= numMax)
        {
            return;
        }
        entrdNbr.setText(entrdNbr.getText() + "8");
        numEntered++;
    }

    private static void k9ActionPerformed(java.awt.event.ActionEvent evt)
    {
        if (numEntered >= numMax)
        {
            return;
        }
        entrdNbr.setText(entrdNbr.getText() + "9");
        numEntered++;
    }

    private static void btnClearActionPerformed(java.awt.event.ActionEvent evt)
    {
        numEntered = 0;
        entrdNbr.setText("");
    }

    private static void k0ActionPerformed(java.awt.event.ActionEvent evt)
    {
        if (numEntered >= numMax)
        {
            return;
        }
        entrdNbr.setText(entrdNbr.getText() + "0");
        numEntered++;
    }

    private static void kCActionPerformed(java.awt.event.ActionEvent evt)
    {
        retnumber = -1;
        hideDialog();
        d = null;
        d = new JDialog();
    }

    private static void btnOkActionPerformed(java.awt.event.ActionEvent evt)
    {
        retnumber = Integer.valueOf(entrdNbr.getText());
        hideDialog();
        d = null;
        d = new JDialog();
    }

    private static void btnAbbrActionPerformed(java.awt.event.ActionEvent evt)
    {
        //retnumber=-1;
        hideDialog();
        d = null;
        d = new JDialog();
    }

    public static String processEnvVarsinLine(String line)
    {
        Map<String, String> env = System.getenv();
        String linemod = line;
        int varstart = linemod.indexOf("%");
        int varend = linemod.indexOf("%", varstart + 1);
        String preVar = "";
        if (varstart == -1)
        {
            return line;
        }
        if (varstart > 0)
        {
            preVar = linemod.substring(0, varstart);
        }
        String postVar = linemod.substring(varend + 1);
        String var = linemod.substring(varstart + 1, varend);
        var = env.get(var.toUpperCase());
        line = preVar + var + postVar;
        return line;
    }
    public static String replaceHashmarkedVars(String line,String varname,String toReplace)
    {
        String linemod = line;
        int varstart = linemod.indexOf("#");
        int varend = linemod.indexOf("#", varstart + 1);
        String preVar = "";
        if (varstart == -1||varend == -1)
        {
            return line;
        }
        if (varstart > 0)
        {
            preVar = linemod.substring(0, varstart);
        }
        String postVar = linemod.substring(varend + 1);
        String var = linemod.substring(varstart + 1, varend);
        if(var.equalsIgnoreCase(varname))
        {
        var = toReplace;
        postVar=replaceHashmarkedVars(postVar, varname, toReplace);//falls sich noch wo anders eine versteckt
        }
        else
        {
            var="#"+var+"#";
            postVar=replaceHashmarkedVars(postVar, varname, toReplace);//falls sich noch wo anders eine versteckt
        }
        line = preVar + var + postVar;
        return line;
    }
    
    public static void DebugHelperPrint(String msg,boolean dbgonly,String debugEnablePreference)
    {
        if(dbgonly)
            if(!Boolean.valueOf(Preferences.userRoot().get(debugEnablePreference, "false")))
            return;
        System.err.println(msg);
    }
    
    public static String getFormatStrforThisAsMax(int nbr)      
    {
        int filesMax=nbr;
        String nfs = "%1";
        if (filesMax > 9)
        {
            nfs = "%2";
            if (filesMax > 99)
            {
                nfs = "%3";
                if (filesMax > 999)
                {
                    nfs = "%4";
                    if (filesMax > 9_999)
                    {
                        nfs = "%5";
                        if (filesMax > 99_999)
                        {
                            nfs = "%6";
                            if (filesMax > 999_999)
                            {
                                nfs = "%7";
                                if (filesMax > 9_999_999)
                                {
                                    nfs = "%8";
                                    if (filesMax > 99_999_999)
                                    {
                                        nfs = "%9";
                                        if (filesMax > 999_999_999)
                                        {
                                            nfs = "%10";
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }   
        return nfs+"d";
    }
    
    
    private static int numEntered = 0, numMax = 0;
    private static javax.swing.JButton btnAbbr;
    private static javax.swing.JButton btnClear;
    private static javax.swing.JButton btnOk;
    private static javax.swing.JTextField entrdNbr;
    private static javax.swing.JButton k0;
    private static javax.swing.JButton k1;
    private static javax.swing.JButton k2;
    private static javax.swing.JButton k3;
    private static javax.swing.JButton k4;
    private static javax.swing.JButton k5;
    private static javax.swing.JButton k6;
    private static javax.swing.JButton k7;
    private static javax.swing.JButton k8;
    private static javax.swing.JButton k9;
    private static javax.swing.JButton kC;
}