cz.cvut.kbe.crypthelper.ui.MainPanel.java Source code

Java tutorial

Introduction

Here is the source code for cz.cvut.kbe.crypthelper.ui.MainPanel.java

Source

/*
 * Software License Agreement (BSD License)
 * 
 * Copyright (c) 2012, Radek Jedk <jezdik.radek@gmail.com>
 * All rights reserved.
 * 
 * Redistribution and use of this software in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 * 
 *   Redistributions of source code must retain the above
 *   copyright notice, this list of conditions and the
 *   following disclaimer.
 * 
 *   Redistributions in binary form must reproduce the above
 *   copyright notice, this list of conditions and the
 *   following disclaimer in the documentation and/or other
 *   materials provided with the distribution.
 * 
 *   Neither the name of Radek Jedk <jezdik.radek@gmail.com> nor the names of its
 *   contributors may be used to endorse or promote products
 *   derived from this software without specific prior
 *   written permission by Radek Jedk <jezdik.radek@gmail.com>
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package cz.cvut.kbe.crypthelper.ui;

import cz.cvut.kbe.crypthelper.MainPanelController;
import cz.cvut.kbe.crypthelper.helper.Alphabets;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 *
 * @author Radek Jedk <jezdik.radek@gmail.com>
 */
public class MainPanel extends javax.swing.JPanel {

    private MainPanelController controller;

    private AlphabetEntryModel alphaEntryModel;

    private CharacterTableModel tableModel;

    /** Creates new form NewJPanel */
    public MainPanel(MainPanelController controller) {
        this.controller = controller;

        initComponents();

        buttonGroup.add(percentOption);
        buttonGroup.add(perCountOption);
        percentOption.setSelected(true);

        fillSelect();

        graphPanel.setLayout(new BorderLayout());

        offsetSlider.setVisible(false);
        bottomPanel.setVisible(false);

        coincidenceIndexLabel.setText("");
    }

    public void setChartData() {
        CategoryDataset dataset = getDataset();

        JFreeChart chart = ChartFactory.createLineChart("etnost znak", "Znak", "etnost (%)", dataset,
                PlotOrientation.VERTICAL, false, false, false);

        styleChart(chart);

        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setMaximumDrawHeight(800);
        chartPanel.setMaximumDrawHeight(600);
        graphPanel.removeAll();
        graphPanel.add(chartPanel, BorderLayout.CENTER);
        graphPanel.updateUI();

        bottomPanel.setVisible(true);
    }

    private CategoryDataset getDataset() {
        if (tableModel.size() == 0)
            return null;

        DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        for (int i = 0; i < tableModel.size(); i++) {
            double freq = tableModel.getFrequency(i);
            String str = tableModel.getCharacter(i) + "";

            if (alphaEntryModel != null) {
                str += alphaEntryModel.getCharacter(i);
            }
            dataset.addValue(freq, "etnost", str);
            if (alphaEntryModel != null) {
                dataset.addValue(alphaEntryModel.getFrequency(i), "etnost 2", str);
            }
        }
        return dataset;
    }

    private void styleChart(JFreeChart chart) {
        chart.getTitle().setPaint(Color.BLACK);
        chart.setBackgroundPaint(new Color(1f, 1f, 1f, 0f));

        CategoryPlot p = chart.getCategoryPlot();
        p.setRangeGridlinePaint(Color.red);
    }

    /** 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup = new javax.swing.ButtonGroup();
        alphaButtonGroup = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        inputText = new javax.swing.JTextArea();
        jPanel1 = new javax.swing.JPanel();
        bottomPanel = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        charTable = new javax.swing.JTable();
        graphPanel = new javax.swing.JPanel();
        offsetSlider = new javax.swing.JSlider();
        percentOption = new javax.swing.JRadioButton();
        perCountOption = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();
        alphabetSelect = new javax.swing.JComboBox();
        coincidenceIndexLabel = new javax.swing.JLabel();
        processButton = new javax.swing.JButton();

        inputText.setColumns(20);
        inputText.setFont(new java.awt.Font("Courier New", 0, 14)); // NOI18N
        inputText.setLineWrap(true);
        inputText.setRows(5);
        jScrollPane1.setViewportView(inputText);

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(102, 102, 102)));

        charTable.setAutoCreateRowSorter(true);
        charTable.setFont(new java.awt.Font("Courier New", 0, 14)); // NOI18N
        charTable.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][] { { null, null }, { null, null }, { null, null }, { null, null } },
                new String[] { "Znak", "etnost" }) {
            Class[] types = new Class[] { java.lang.String.class, java.lang.Integer.class };
            boolean[] canEdit = new boolean[] { false, false };

            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
        charTable.setIntercellSpacing(new java.awt.Dimension(1, 3));
        jScrollPane2.setViewportView(charTable);

        javax.swing.GroupLayout graphPanelLayout = new javax.swing.GroupLayout(graphPanel);
        graphPanel.setLayout(graphPanelLayout);
        graphPanelLayout.setHorizontalGroup(graphPanelLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 478, Short.MAX_VALUE));
        graphPanelLayout.setVerticalGroup(graphPanelLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 140, Short.MAX_VALUE));

        offsetSlider.setMajorTickSpacing(1);
        offsetSlider.setMaximum(25);
        offsetSlider.setMinorTickSpacing(1);
        offsetSlider.setPaintLabels(true);
        offsetSlider.setPaintTicks(true);
        offsetSlider.setToolTipText("Posunut abecedy");
        offsetSlider.setValue(0);

        percentOption.setText("v procentech");

        perCountOption.setText("v po?tu vskyt");

        jLabel1.setText("Abeceda:");

        alphabetSelect.setModel(
                new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout bottomPanelLayout = new javax.swing.GroupLayout(bottomPanel);
        bottomPanel.setLayout(bottomPanelLayout);
        bottomPanelLayout.setHorizontalGroup(bottomPanelLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(bottomPanelLayout.createSequentialGroup().addContainerGap().addGroup(bottomPanelLayout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(perCountOption)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 135,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(percentOption)).addGap(18, 18, 18)
                        .addGroup(bottomPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(graphPanel, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(offsetSlider, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(bottomPanelLayout.createSequentialGroup().addComponent(jLabel1)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(alphabetSelect, javax.swing.GroupLayout.PREFERRED_SIZE, 124,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(0, 0, Short.MAX_VALUE)))));
        bottomPanelLayout.setVerticalGroup(bottomPanelLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(bottomPanelLayout.createSequentialGroup().addComponent(percentOption)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(bottomPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(bottomPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel1).addComponent(alphabetSelect,
                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(perCountOption))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(bottomPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(bottomPanelLayout.createSequentialGroup()
                                        .addComponent(graphPanel, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(offsetSlider, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0,
                                        Short.MAX_VALUE))));

        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().addComponent(bottomPanel,
                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap()));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addComponent(bottomPanel,
                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap()));

        coincidenceIndexLabel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        coincidenceIndexLabel.setText("coincidenceIndexLabel");

        processButton.setText("Analyzovat");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 663, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup().addComponent(processButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(coincidenceIndexLabel))
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(5, 5, 5)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(processButton).addComponent(coincidenceIndexLabel))
                        .addGap(18, 18, 18).addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap()));
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup alphaButtonGroup;
    private javax.swing.JComboBox alphabetSelect;
    private javax.swing.JPanel bottomPanel;
    private javax.swing.ButtonGroup buttonGroup;
    private javax.swing.JTable charTable;
    private javax.swing.JLabel coincidenceIndexLabel;
    private javax.swing.JPanel graphPanel;
    private javax.swing.JTextArea inputText;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSlider offsetSlider;
    private javax.swing.JRadioButton perCountOption;
    private javax.swing.JRadioButton percentOption;
    private javax.swing.JButton processButton;
    // End of variables declaration//GEN-END:variables

    public MainPanelController getController() {
        return controller;
    }

    public JTextArea getInputText() {
        return inputText;
    }

    public JButton getProcessButton() {
        return processButton;
    }

    public JTable getCharTable() {
        return charTable;
    }

    public JRadioButton getPerCountOption() {
        return perCountOption;
    }

    public JRadioButton getPercentOption() {
        return percentOption;
    }

    public ButtonGroup getAlphaButtonGroup() {
        return alphaButtonGroup;
    }

    public JComboBox getAlphabetSelect() {
        return alphabetSelect;
    }

    public JSlider getOffsetSlider() {
        return offsetSlider;
    }

    public void setTableModel(CharacterTableModel tableModel) {
        this.tableModel = tableModel;
        charTable.setModel(tableModel);

        DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer() {

            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                    boolean hasFocus, int row, int column) {
                JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row,
                        column);
                label.setHorizontalAlignment(SwingConstants.CENTER);
                return label;
            }

        };

        for (int i = 0; i < charTable.getColumnCount(); i++) {
            charTable.getColumnModel().getColumn(i).setCellRenderer(dtcr);
        }
    }

    public void setOffset(int value) {
        if (alphaEntryModel != null) {
            tableModel.setOffset(value);
            setChartData();
        }
    }

    public void setIndexOfCoincidence(double index) {
        coincidenceIndexLabel.setText("Index koincidence: " + index);
    }

    public String getSubstitutedText() {
        return controller.getSubstituionText();
    }

    public void setAlphaEntryModel(AlphabetEntryModel alphaModel) {
        this.alphaEntryModel = alphaModel;
    }

    private void fillSelect() {
        alphabetSelect.removeAllItems();
        alphabetSelect.addItem("dn");

        String[] names = Alphabets.getNames();
        for (String name : names) {
            alphabetSelect.addItem(name);
        }
    }

}