/*
 * Okno.java
 *
 * Created on Utorok, 2009, máj 19, 9:29
 */

package skuska;

import javax.swing.JOptionPane;
import java.io.*;


/**
 *
 * @author  ondrus00
 */
public class Okno extends javax.swing.JFrame {
    
    /** Creates new form Okno */
    public Okno() {
        initComponents();
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        txtBINinput = new javax.swing.JTextField();
        txtDECoutput = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtDECinput = new javax.swing.JTextField();
        txtBINoutput = new javax.swing.JTextField();
        btnB2D = new javax.swing.JButton();
        btnD2B = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jLabel1.setText("Bin2Oct");

        jLabel2.setText("Oct2Bin");

        btnB2D.setText("Convert B2D");
        btnB2D.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnB2DMouseClicked(evt);
            }
        });

        btnD2B.setText("Convert D2B");
        btnD2B.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnD2BMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(txtDECinput, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtBINinput, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE))
                        .addGap(54, 54, 54)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtBINoutput)
                            .addComponent(txtDECoutput, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 61, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btnB2D)
                            .addComponent(btnD2B))
                        .addGap(18, 18, 18))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addContainerGap(356, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtBINinput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtDECoutput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnB2D))
                .addGap(17, 17, 17)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtDECinput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtBINoutput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnD2B))
                .addContainerGap(39, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnD2BMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnD2BMouseClicked
        String hodnota=txtDECinput.getText();
        try{
        int IntHodnota=Integer.parseInt(hodnota);
        if(IntHodnota<8)
        {
            txtBINoutput.setText(Integer.toString(IntHodnota,2));
        }
        else txtBINoutput.setText("Max do 7!");
        
        }catch(Exception e)
        {
            txtBINoutput.setText("Zadajte octal hodnotu!");
        }
    }//GEN-LAST:event_btnD2BMouseClicked

    private void btnB2DMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnB2DMouseClicked
        if(IsBIN())
        {
            String hodnota=txtBINinput.getText();
            if(hodnota.length()<4)
            {
            int binary=Integer.parseInt(hodnota,2);
            txtDECoutput.setText(Integer.toString(binary,8));
            }
            else txtDECoutput.setText("Max 3 znaky!");
        }else txtDECoutput.setText("Nieje BIN hodnota!");
    }//GEN-LAST:event_btnB2DMouseClicked
    
    public boolean IsBIN()
    {
        String hodnota=txtBINinput.getText();
        boolean binarne=true;
        for(int i=0;i<hodnota.length();i++)
        {
            if(hodnota.charAt(i)=='1' || hodnota.charAt(i)=='0') {}
            else binarne=false;   
        }
        return binarne;
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Okno().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnB2D;
    private javax.swing.JButton btnD2B;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField txtBINinput;
    private javax.swing.JTextField txtBINoutput;
    private javax.swing.JTextField txtDECinput;
    private javax.swing.JTextField txtDECoutput;
    // End of variables declaration//GEN-END:variables
    
}
