com.pari.license.ui.LicenseGenDialog.java Source code

Java tutorial

Introduction

Here is the source code for com.pari.license.ui.LicenseGenDialog.java

Source

/**
 * Copyright (c) 2005 - 2007 Pari Networks, Inc.  All Rights Reserved.
 *
 * This software is the proprietary information of Pari Networks, Inc.
 *
 */

package com.pari.license.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import com.jidesoft.dialog.StandardDialog;
import com.jidesoft.swing.AutoResizingTextArea;
import com.jidesoft.swing.CheckBoxList;
import com.jidesoft.swing.JideSwingUtilities;
import com.jidesoft.swing.SearchableUtils;
import com.pari.client.controls.ControlFactory;
import com.pari.client.controls.NumericTextField;
import com.pari.client.controls.TextFieldMenu;
import com.pari.client.utils.FileChooserUtils;
import com.pari.client.utils.MessageUtils;
import com.pari.client.utils.UIStyleConstants;
import com.parinetworks.system.lic.LicenseGenerator;

public final class LicenseGenDialog extends StandardDialog {
    private JTextArea seedTextArea = null;
    private JCheckBox evaluationCheckBox = null;
    private JCheckBox allowDeviceBeyondLicenseCheck = null;
    private JTextField evaluationPeriod = null;
    private JTextField maxDevices = null;
    private CheckBoxList licensedComponentsList = null;

    public LicenseGenDialog(JFrame frame) {
        super(frame);
        setTitle("Pari License Generator");
        super.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    }

    @Override
    public javax.swing.JComponent createBannerPanel() {
        return null;
    }

    @Override
    public com.jidesoft.dialog.ButtonPanel createButtonPanel() {
        com.jidesoft.dialog.ButtonPanel buttonPanel = new com.jidesoft.dialog.ButtonPanel();
        JButton cancelButton = new JButton();
        cancelButton.setName(CLOSE);
        buttonPanel.addButton(cancelButton, com.jidesoft.dialog.ButtonPanel.CANCEL_BUTTON);

        cancelButton.setAction(new AbstractAction(UIManager.getString("OptionPane.cancelButtonText")) {
            @Override
            public void actionPerformed(ActionEvent e) {
                setDialogResult(RESULT_CANCELLED);
                setVisible(false);
                dispose();
            }
        });
        cancelButton.setPreferredSize(new Dimension(80, 25));

        JButton okButton = new JButton();
        okButton.setName(OK);
        buttonPanel.addButton(okButton, com.jidesoft.dialog.ButtonPanel.AFFIRMATIVE_BUTTON);

        okButton.setAction(new AbstractAction("Generate License") {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (generateLicense()) {
                    setDialogResult(RESULT_AFFIRMED);
                    setVisible(false);
                    dispose();
                }
            }
        });
        okButton.setPreferredSize(new Dimension(80, 25));

        setDefaultCancelAction(cancelButton.getAction());
        buttonPanel.setBorder(UIStyleConstants.buttonPanelBorder);
        buttonPanel.setButtonOrder("HAC");
        return buttonPanel;
    }

    @Override
    public JComponent createContentPanel() {
        JPanel contentPanel = new JPanel();
        contentPanel.setLayout(new BorderLayout());
        contentPanel.setBackground(Color.white);

        seedTextArea = new AutoResizingTextArea(100, 100);
        seedTextArea.addMouseListener(new TextFieldMenu(seedTextArea));
        seedTextArea.setEditable(true);

        evaluationCheckBox = new JCheckBox("Evaluation License Only");
        evaluationCheckBox.setOpaque(false);
        evaluationCheckBox.setMargin(new Insets(0, 0, 0, 0));

        evaluationCheckBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                enableControls();
            }
        });

        allowDeviceBeyondLicenseCheck = new JCheckBox("Allow to add devices beyond Maximum Devices");
        allowDeviceBeyondLicenseCheck.setOpaque(false);
        allowDeviceBeyondLicenseCheck.setMargin(new Insets(0, 0, 0, 0));

        allowDeviceBeyondLicenseCheck.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
            }
        });

        JPanel optionsPane = new JPanel();
        optionsPane.setOpaque(false);

        String columnSpecs = "5dlu, pref, 5dlu, 175dlu, 5dlu, 30dlu";
        String rowSpecs = "pref, 4dlu, top:100dlu, 8dlu, pref, 4dlu, pref, 5dlu, pref, 5dlu, pref, 8dlu, pref, 6dlu, pref, 8dlu, pref";
        FormLayout formLayout = new FormLayout(columnSpecs, rowSpecs);
        DefaultFormBuilder builder = new DefaultFormBuilder(formLayout, optionsPane);
        builder.setDefaultDialogBorder();

        evaluationPeriod = new NumericTextField();
        evaluationPeriod.addMouseListener(new TextFieldMenu(evaluationPeriod));

        maxDevices = new NumericTextField();
        maxDevices.addMouseListener(new TextFieldMenu(maxDevices));

        JLabel seedLabel = ControlFactory.createLabelFor("License Seed", true, seedTextArea, 'S');
        JLabel evalPeriodLabel = ControlFactory.createLabelFor("Evaluation Period", false, evaluationPeriod, 'E');
        JLabel maxDevicesLabel = ControlFactory.createLabelFor("Maximum Devices", true, maxDevices, 'M');

        CellConstraints cc = new CellConstraints();

        int rowIndex = 1;
        builder.addSeparator("License Seed Information", cc.xyw(1, rowIndex, 6));

        rowIndex += 2;
        builder.add(seedLabel, cc.xy(2, rowIndex));
        builder.add(new JScrollPane(seedTextArea), cc.xyw(4, rowIndex, 3));

        rowIndex += 2;
        builder.addSeparator("License Details", cc.xyw(1, rowIndex, 6));

        rowIndex += 2;
        builder.add(evaluationCheckBox, cc.xyw(2, rowIndex, 4));

        rowIndex += 2;
        builder.add(evalPeriodLabel, cc.xy(2, rowIndex));
        builder.add(evaluationPeriod, cc.xy(4, rowIndex));
        builder.add(new JLabel("(days)"), cc.xy(6, rowIndex));

        rowIndex += 2;
        builder.add(maxDevicesLabel, cc.xy(2, rowIndex));
        builder.add(maxDevices, cc.xy(4, rowIndex));

        rowIndex += 2;

        builder.add(allowDeviceBeyondLicenseCheck, cc.xyw(2, rowIndex, 4));

        contentPanel.add(optionsPane, BorderLayout.CENTER);

        licensedComponentsList = new CheckBoxList(getAllComponentNames());
        licensedComponentsList.setPreferredSize(new Dimension(350, 175));
        SearchableUtils.installSearchable(licensedComponentsList);

        JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3),
                BorderFactory.createTitledBorder("Select Licensed Components")));

        panel.setOpaque(false);
        panel.setLayout(new BorderLayout());
        panel.add(new JScrollPane(licensedComponentsList), BorderLayout.CENTER);

        contentPanel.add(panel, BorderLayout.SOUTH);

        contentPanel.setBackground(Color.white);
        setResizable(true);
        enableControls();
        return contentPanel;
    }

    public String[] getAllComponentNames() {
        return new String[] { "TerminalGateway", "VersionControlSystem" };
    }

    private void enableControls() {
        boolean enable = evaluationCheckBox.isSelected();
        evaluationPeriod.setEnabled(enable);
    }

    public void cleanup() {
    }

    @Override
    public ComponentOrientation getComponentOrientation() {
        return super.getComponentOrientation();
    }

    @Override
    public void setVisible(boolean b) {
        if (b) {
            pack();
            JideSwingUtilities.globalCenterWindow(this);
        }
        super.setVisible(b);
    }

    private boolean generateLicense() {
        if (!validateData()) {
            return false;
        }

        String seedText = seedTextArea.getText().trim();
        boolean isEval = evaluationCheckBox.isSelected();
        int eval = getValidNumber(evaluationPeriod.getText().trim());
        int max = getValidNumber(maxDevices.getText().trim());

        File file = FileChooserUtils.displaySaveFileChooser("Select License File", null, evaluationCheckBox);
        if (file != null) {
            try {
                String componentStr = "";
                Object[] components = licensedComponentsList.getCheckBoxListSelectedValues();
                if ((components != null) && (components.length > 0)) {
                    for (Object comp : components) {
                        componentStr += (comp.toString() + ",");
                    }
                }

                LicenseGenerator licenseGen = new LicenseGenerator();
                boolean allowDevicesAfterMaxDeviceNo = allowDeviceBeyondLicenseCheck.isSelected();
                String licenseText = licenseGen.generateLicFile(seedText, isEval, eval, max,
                        allowDevicesAfterMaxDeviceNo, componentStr);
                FileWriter writer = new FileWriter(file);
                writer.write(licenseText);
                writer.flush();
                writer.close();
                MessageUtils.showInfoDialog(this, getTitle(), "Successfully generated the License.");
                return true;
            } catch (Exception ex) {
                MessageUtils.showErrorDialog(this, getTitle(), "Error while generating License File");
                return false;
            }
        }
        return false;
    }

    private boolean validateData() {
        String seedText = seedTextArea.getText().trim();
        if (seedText.length() == 0) {
            MessageUtils.showErrorDialog(this, getTitle(), "Please enter a valid License Seed");
            seedTextArea.requestFocus();
            return false;
        }

        boolean seedValid = false;
        try {
            LicenseGenerator licenseGen = new LicenseGenerator();
            seedValid = licenseGen.validateSeed(seedText);
        } catch (Exception ex) {
            System.out.println("Error while validating seeed");
            ex.printStackTrace();
            seedValid = false;
        }

        if (!seedValid) {
            MessageUtils.showErrorDialog(this, getTitle(),
                    "Invalid License Seed. Please enter a valid License Seed");
            seedTextArea.requestFocus();
            return false;
        }

        if (evaluationCheckBox.isSelected()) {
            String evalPeriodText = evaluationPeriod.getText().trim();
            if (evalPeriodText.length() == 0) {
                MessageUtils.showErrorDialog(this, getTitle(), "Please enter Evaluation Period in days");
                evaluationPeriod.requestFocus();
                return false;
            }

            if (getValidNumber(evalPeriodText) <= 0) {
                MessageUtils.showErrorDialog(this, getTitle(), "Please enter a valid Evaluation Period in days");
                evaluationPeriod.requestFocus();
                return false;
            }

        }

        String maxDevicesText = maxDevices.getText().trim();
        if (maxDevicesText.length() == 0) {
            MessageUtils.showErrorDialog(this, getTitle(), "Please enter Maximum number of Devices");
            maxDevices.requestFocus();
            return false;
        }

        if (getValidNumber(maxDevicesText) <= 0) {
            MessageUtils.showErrorDialog(this, getTitle(), "Please enter a valid Maximum number of Devices");
            maxDevices.requestFocus();
            return false;
        }

        return true;
    }

    private int getValidNumber(String text) {
        try {
            return Integer.valueOf(text);
        } catch (Exception ex) {
            return -1;
        }
    }
}