All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.java.truelicense.swing.DisplayPanel Maven / Gradle / Ivy

Go to download

The TrueLicense Swing module provides a graphical user interface for consuming license keys.

There is a newer version: 2.6.6
Show newest version
/*
 * Copyright (C) 2005-2015 Schlichtherle IT Services.
 * All rights reserved. Use is subject to license terms.
 */

package net.java.truelicense.swing;

import java.util.Date;
import javax.annotation.CheckForNull;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import net.java.truelicense.core.License;
import net.java.truelicense.core.LicenseValidationException;
import static net.java.truelicense.ui.LicenseWizardMessage.display_consumer;
import static net.java.truelicense.ui.LicenseWizardMessage.display_consumerFormat;
import static net.java.truelicense.ui.LicenseWizardMessage.display_dateTimeFormat;
import static net.java.truelicense.ui.LicenseWizardMessage.display_failure;
import static net.java.truelicense.ui.LicenseWizardMessage.display_holder;
import static net.java.truelicense.ui.LicenseWizardMessage.display_info;
import static net.java.truelicense.ui.LicenseWizardMessage.display_issued;
import static net.java.truelicense.ui.LicenseWizardMessage.display_issuer;
import static net.java.truelicense.ui.LicenseWizardMessage.display_notAfter;
import static net.java.truelicense.ui.LicenseWizardMessage.display_notBefore;
import static net.java.truelicense.ui.LicenseWizardMessage.display_subject;
import static net.java.truelicense.ui.LicenseWizardMessage.display_title;
import static net.java.truelicense.ui.LicenseWizardMessage.failure_title;

/**
 * @author Christian Schlichtherle
 */
final class DisplayPanel extends LicensePanel {

    private static final long serialVersionUID = 1L;

    private License license;

    DisplayPanel(final LicenseWizard wizard) {
        super(wizard);
        initComponents();
    }

    /** Updates the view with the contents of the license and verifies it. */
    @Override public void onAfterStateSwitch() {
        assert SwingUtilities.isEventDispatchThread();
        assert isVisible();
        try {
            license = manager().view();
            onLicenseChange();
            manager().verify();
        } catch (final Exception failure) {
            JOptionPane.showMessageDialog(
                    this,
                    failure instanceof LicenseValidationException
                        ? failure.getLocalizedMessage()
                        : format(display_failure),
                    format(failure_title),
                    JOptionPane.ERROR_MESSAGE);
        }
    }

    private void onLicenseChange() {
        if (null == license) return;
        subjectComponent.setText(nonNullOrEmptyString(license.getSubject()));
        holderComponent.setText(nonNullOrEmptyString(license.getHolder()));
        issuerComponent.setText(nonNullOrEmptyString(license.getIssuer()));
        issuedComponent.setText(format(license.getIssued()));
        notBeforeComponent.setText(format(license.getNotBefore()));
        notAfterComponent.setText(format(license.getNotAfter()));
        consumerComponent.setText(display_consumerFormat.format(
                subject(),
                license.getConsumerType(),
                license.getConsumerAmount()).toString());
        infoComponent.setText(nonNullOrEmptyString(license.getInfo()));
    }

    private static String nonNullOrEmptyString(@CheckForNull Object obj) {
        return null != obj ? obj.toString() : "";
    }

    private String format(@CheckForNull Date date) {
        return display_dateTimeFormat(subject(), date);
    }

    /** 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.
     */
    // //GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        javax.swing.JLabel subjectLabel = new javax.swing.JLabel();
        subjectComponent = new javax.swing.JTextArea();
        javax.swing.JLabel holderLabel = new javax.swing.JLabel();
        javax.swing.JScrollPane holderScrollPane = new javax.swing.JScrollPane();
        holderComponent = new javax.swing.JTextArea();
        javax.swing.JLabel issuerLabel = new javax.swing.JLabel();
        javax.swing.JScrollPane issuerScrollPane = new javax.swing.JScrollPane();
        issuerComponent = new javax.swing.JTextArea();
        javax.swing.JLabel issuedLabel = new javax.swing.JLabel();
        issuedComponent = new javax.swing.JTextArea();
        javax.swing.JLabel notBeforeLabel = new javax.swing.JLabel();
        notBeforeComponent = new javax.swing.JTextArea();
        javax.swing.JLabel notAfterLabel = new javax.swing.JLabel();
        notAfterComponent = new javax.swing.JTextArea();
        javax.swing.JLabel consumerLabel = new javax.swing.JLabel();
        consumerComponent = new javax.swing.JTextArea();
        javax.swing.JLabel infoLabel = new javax.swing.JLabel();
        javax.swing.JScrollPane infoScrollPane = new javax.swing.JScrollPane();
        infoComponent = new javax.swing.JTextArea();

        setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createTitledBorder(format(display_title)), javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10))); // NOI18N
        setName(DisplayPanel.class.getSimpleName());
        setLayout(new java.awt.GridBagLayout());

        subjectLabel.setLabelFor(subjectComponent);
        subjectLabel.setText(format(display_subject)); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.ipadx = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.BASELINE_TRAILING;
        add(subjectLabel, gridBagConstraints);

        subjectComponent.setEditable(false);
        subjectComponent.setFont(getFont());
        subjectComponent.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        subjectComponent.setName(display_subject.name());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.BASELINE_LEADING;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(subjectComponent, gridBagConstraints);

        holderLabel.setLabelFor(holderComponent);
        holderLabel.setText(format(display_holder)); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.ipadx = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.BASELINE_TRAILING;
        add(holderLabel, gridBagConstraints);

        holderScrollPane.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        holderScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        holderScrollPane.setPreferredSize(new java.awt.Dimension(300, 65));

        holderComponent.setEditable(false);
        holderComponent.setFont(getFont());
        holderComponent.setLineWrap(true);
        holderComponent.setWrapStyleWord(true);
        holderComponent.setBorder(null);
        holderComponent.setName(display_holder.name());
        holderScrollPane.setViewportView(holderComponent);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.BASELINE_LEADING;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(holderScrollPane, gridBagConstraints);

        issuerLabel.setLabelFor(issuerComponent);
        issuerLabel.setText(format(display_issuer)); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.ipadx = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.BASELINE_TRAILING;
        add(issuerLabel, gridBagConstraints);

        issuerScrollPane.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        issuerScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        issuerScrollPane.setPreferredSize(new java.awt.Dimension(300, 65));

        issuerComponent.setEditable(false);
        issuerComponent.setFont(getFont());
        issuerComponent.setLineWrap(true);
        issuerComponent.setWrapStyleWord(true);
        issuerComponent.setBorder(null);
        issuerComponent.setName(display_issuer.name());
        issuerScrollPane.setViewportView(issuerComponent);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.BASELINE_LEADING;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(issuerScrollPane, gridBagConstraints);

        issuedLabel.setLabelFor(issuedComponent);
        issuedLabel.setText(format(display_issued)); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.ipadx = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.BASELINE_TRAILING;
        add(issuedLabel, gridBagConstraints);

        issuedComponent.setEditable(false);
        issuedComponent.setFont(getFont());
        issuedComponent.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        issuedComponent.setName(display_issued.name());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.BASELINE_LEADING;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(issuedComponent, gridBagConstraints);

        notBeforeLabel.setLabelFor(notBeforeComponent);
        notBeforeLabel.setText(format(display_notBefore)); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.ipadx = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.BASELINE_TRAILING;
        add(notBeforeLabel, gridBagConstraints);

        notBeforeComponent.setEditable(false);
        notBeforeComponent.setFont(getFont());
        notBeforeComponent.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        notBeforeComponent.setName(display_notBefore.name());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.BASELINE_LEADING;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(notBeforeComponent, gridBagConstraints);

        notAfterLabel.setLabelFor(notAfterComponent);
        notAfterLabel.setText(format(display_notAfter)); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.ipadx = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.BASELINE_TRAILING;
        add(notAfterLabel, gridBagConstraints);

        notAfterComponent.setEditable(false);
        notAfterComponent.setFont(getFont());
        notAfterComponent.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        notAfterComponent.setName(display_notAfter.name());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.BASELINE_LEADING;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(notAfterComponent, gridBagConstraints);

        consumerLabel.setLabelFor(consumerComponent);
        consumerLabel.setText(format(display_consumer)); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.ipadx = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.BASELINE_TRAILING;
        add(consumerLabel, gridBagConstraints);

        consumerComponent.setEditable(false);
        consumerComponent.setFont(getFont());
        consumerComponent.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        consumerComponent.setName(display_consumer.name());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.BASELINE_LEADING;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(consumerComponent, gridBagConstraints);

        infoLabel.setLabelFor(infoComponent);
        infoLabel.setText(format(display_info)); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.ipadx = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.BASELINE_TRAILING;
        add(infoLabel, gridBagConstraints);

        infoScrollPane.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        infoScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        infoScrollPane.setPreferredSize(new java.awt.Dimension(300, 65));

        infoComponent.setEditable(false);
        infoComponent.setFont(getFont());
        infoComponent.setLineWrap(true);
        infoComponent.setWrapStyleWord(true);
        infoComponent.setBorder(null);
        infoComponent.setName(display_info.name());
        infoScrollPane.setViewportView(infoComponent);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.BASELINE_LEADING;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(infoScrollPane, gridBagConstraints);
    }// //GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea consumerComponent;
    private javax.swing.JTextArea holderComponent;
    private javax.swing.JTextArea infoComponent;
    private javax.swing.JTextArea issuedComponent;
    private javax.swing.JTextArea issuerComponent;
    private javax.swing.JTextArea notAfterComponent;
    private javax.swing.JTextArea notBeforeComponent;
    private javax.swing.JTextArea subjectComponent;
    // End of variables declaration//GEN-END:variables
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy