net.java.truelicense.swing.InstallPanel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of truelicense-swing Show documentation
Show all versions of truelicense-swing Show documentation
The TrueLicense Swing module provides a graphical user interface for
consuming license keys.
/*
* Copyright (C) 2005-2013 Schlichtherle IT Services.
* All rights reserved. Use is subject to license terms.
*/
package net.java.truelicense.swing;
import java.awt.Cursor;
import java.io.File;
import java.security.GeneralSecurityException;
import java.util.Locale;
import javax.swing.JFileChooser;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.filechooser.FileFilter;
import net.java.truelicense.core.LicenseValidationException;
import net.java.truelicense.core.io.FileStore;
import net.java.truelicense.core.util.Objects;
import net.java.truelicense.obfuscate.Obfuscate;
import static net.java.truelicense.swing.Messages.*;
import net.java.truelicense.swing.nexes.WizardPanelDescriptor;
/**
* @author Christian Schlichtherle
*/
public class InstallPanel extends javax.swing.JPanel {
private static final long serialVersionUID = 1L;
/**
* The suffix for files which hold license certificates.
*
* => ".lic" - must be lowercase!
*/
@Obfuscate
private static final String LICENSE_SUFFIX = ".lic";
@Obfuscate
private static final String FILE_FILTER_DESCRIPTION = "InstallPanel.fileFilter.description";
@Obfuscate
private static final String FILE_FILTER_SUFFIX = " (*.lic)";
private final ObservableLicenseConsumerManager ocm;
public InstallPanel(final ObservableLicenseConsumerManager ocm) {
this.ocm = Objects.requireNonNull(ocm);
initComponents();
fileField.getDocument().addDocumentListener(
new DocumentListener() {
@Override public void insertUpdate(DocumentEvent e) {
updateFileField();
}
@Override public void removeUpdate(DocumentEvent e) {
updateFileField();
}
@Override public void changedUpdate(DocumentEvent e) {
updateFileField();
}
});
}
private void updateFileField() {
final String fileName = fileField.getText();
installButton.setEnabled(null != fileName && new File(fileName).isFile());
}
@Override
public void setVisible(final boolean visible) {
if (visible) updateFileField();
super.setVisible(visible);
}
/**
* Returns a suitable file filter for the subject of this license manager.
* On Windows systems, the case of the suffix is ignored when browsing
* directories.
*
* @return A valid {@code FileFilter}.
*/
private FileFilter fileFilter() {
final String description = message(FILE_FILTER_DESCRIPTION,
ocm.subject()).toString();
final FileFilter fileFilter;
if (File.separatorChar == '\\') {
fileFilter = new FileFilter() {
@Override
public boolean accept(File f) {
return f.isDirectory()
|| f.getPath().toLowerCase(Locale.ROOT).endsWith(LICENSE_SUFFIX);
}
@Override
public String getDescription() {
return description + FILE_FILTER_SUFFIX;
}
};
} else {
fileFilter = new FileFilter() {
@Override
public boolean accept(File f) {
return f.isDirectory()
|| f.getPath().endsWith(LICENSE_SUFFIX);
}
@Override
public String getDescription() {
return description + FILE_FILTER_SUFFIX;
}
};
}
return fileFilter;
}
/**
* 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;
filechooser = new javax.swing.JFileChooser();
prompt = new javax.swing.JTextArea();
fileButton = new javax.swing.JButton();
installButton = new net.java.truelicense.swing.util.EnhancedButton();
success = new javax.swing.JTextArea();
filechooser.setFileFilter(fileFilter());
setLayout(new java.awt.GridBagLayout());
prompt.setEditable(false);
prompt.setLineWrap(true);
prompt.setText(message("InstallPanel.prompt.text").toString()); // NOI18N
prompt.setWrapStyleWord(true);
prompt.setBorder(null);
prompt.setOpaque(false);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.ipady = 5;
gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
gridBagConstraints.weightx = 1.0;
add(prompt, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
gridBagConstraints.weightx = 1.0;
add(fileField, gridBagConstraints);
fileButton.setText("...");
fileButton.setName("select"); // NOI18N
fileButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
fileButtonActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 1;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
add(fileButton, gridBagConstraints);
installButton.setText(message("InstallPanel.installButton.text").toString()); // NOI18N
installButton.setName("install"); // NOI18N
installButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
installButtonActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
add(installButton, gridBagConstraints);
success.setEditable(false);
success.setBorder(null);
success.setOpaque(false);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 3;
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.insets = new java.awt.Insets(15, 0, 0, 0);
add(success, gridBagConstraints);
}// //GEN-END:initComponents
private void installButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_installButtonActionPerformed
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
try {
ocm.install(new FileStore(new File(fileField.getText())));
installButton.setEnabled(false);
success.setText(message("InstallPanel.success.text").toString());
} catch (final GeneralSecurityException failure) {
Dialogs.showMessageDialog(
this,
failure instanceof LicenseValidationException
? failure.getLocalizedMessage()
: message("InstallPanel.failure.message").toString(), // don't show details!
message("InstallPanel.failure.title").toString(),
Dialogs.ERROR_MESSAGE);
} finally {
setCursor(null);
}
}//GEN-LAST:event_installButtonActionPerformed
private void fileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileButtonActionPerformed
if (filechooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION)
fileField.setText(filechooser.getSelectedFile().getPath());
}//GEN-LAST:event_fileButtonActionPerformed
/**
* Adds the given license consumer manager listener.
* It is safe to call this method from a listener implementation.
* The implementation uses a list, so adding the same listener multiple
* times will result in multiple notifications for each event.
*
* @param cml the license consumer manager listener to add.
*/
public void addLicenseConsumerManagerListener(
LicenseConsumerManagerListener cml) {
ocm.addLicenseConsumerManagerListener(cml);
}
/**
* Removes the given license consumer manager listener.
* It is safe to call this method from a listener implementation.
* Removing an listener which is not registered has no effect.
*
* @param cml the license consumer manager listener to remove.
*/
public void removeLicenseConsumerManagerListener(
LicenseConsumerManagerListener cml) {
ocm.removeLicenseConsumerManagerListener(cml);
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton fileButton;
private final javax.swing.JTextField fileField = new javax.swing.JTextField();
private javax.swing.JFileChooser filechooser;
private net.java.truelicense.swing.util.EnhancedButton installButton;
private javax.swing.JTextArea prompt;
private javax.swing.JTextArea success;
// End of variables declaration//GEN-END:variables
public static class Descriptor extends WizardPanelDescriptor {
public static final String IDENTIFIER = "INSTALL_PANEL"; // NOI18N
public Descriptor(ObservableLicenseConsumerManager ocm) {
this(new InstallPanel(ocm));
}
private Descriptor(final InstallPanel panel) {
super(IDENTIFIER, panel);
panel.addLicenseConsumerManagerListener(new BasicLicenseConsumerManagerListener() {
@Override public void installed(LicenseConsumerManagerEvent cme) {
getWizardModel().setNextButtonEnabled(Boolean.TRUE);
}
});
}
@Override public InstallPanel getPanel() {
return (InstallPanel) super.getPanel();
}
@Override
public String getBackPanelIdentifier() {
return WelcomePanel.Descriptor.IDENTIFIER;
}
@Override
public String getNextPanelIdentifier() {
return LicensePanel.Descriptor.IDENTIFIER;
}
@Override
@SuppressWarnings("AccessingNonPublicFieldOfAnotherObject")
public void aboutToDisplayPanel() {
getPanel().success.setText(null);
getWizardModel().setNextButtonEnabled(Boolean.FALSE);
}
}
}