
//import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader; //Woher kam enn der Scheissdreck???
import java.security.MessageDigest;
import java.util.HashMap;
import javax.swing.*;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author LH
 */

public class ToolsSet 
{
    private static boolean ContState;
    static JDialog d = new JDialog();
    public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
    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)
            {
                //System.out.printf("%02x", b);
                int val = ((int) b) & 0xff;
                if (val < 16) t.append("0");
                t.append(Integer.toHexString(val));
            }
            //System.out.println();
            return t.toString();
        }
        catch (Exception e)
        {
            return null;
        }
    }
    
    public static int[] lettersToInt(String ip)
    {
        ip = ip.toLowerCase();
        char[]it = ip.toCharArray();
        int[] r = new int[it.length];
        for (int i = 0;i<r.length;i++) 
        {
            r[i] = (int) it[i];
        }
        return r;
    }
    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 e)
            {
            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 e)
            {
            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 e)
            {
                i1=0;
            }
            try
            {
                i2 = ci2[i];
            }
            catch (Exception e)
            {
                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 r = "";
        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) {
            r +=s;
        }
        
        
        
        return r;
    }
    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)
    {
	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(true);
	if(mode==true)
        {
	    //Ausgeblendet da nur Fenster-Layout
	    // <editor-fold defaultstate="collapsed" desc="Layout-Code">
       btnNo.setText("Abbrechen");
        btnNo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNoActionPerformed(evt);
            }
        });

        btnYes.setText("Fortfahren");
        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 );
        }
	else
	{
	    //TODO working.dialog
	    return true;
	}
	return ContState;
    }
        private static void btnYesActionPerformed(java.awt.event.ActionEvent evt)
    {
	ContState = true;
	hideDialog();
    }
    private static void btnNoActionPerformed(java.awt.event.ActionEvent evt)
    {
	ContState = false;
	hideDialog();
    }
    public static void hideDialog()
    {
    d.setVisible(false);
    }
}