net.java.truelicense.swing.LicenseWorkerPanel 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-2015 Schlichtherle IT Services.
* All rights reserved. Use is subject to license terms.
*/
package net.java.truelicense.swing;
import java.util.Locale;
import java.util.concurrent.ExecutionException;
import javax.swing.JOptionPane;
import static javax.swing.SwingUtilities.isEventDispatchThread;
import javax.swing.SwingWorker;
import net.java.truelicense.core.LicenseManagementException;
import net.java.truelicense.core.util.BaseMessage;
import net.java.truelicense.core.util.Message;
import net.java.truelicense.ui.LicenseWizardMessage;
/**
* An abstract {@code JPanel} for license consumer management.
*
* @author Christian Schlichtherle
*/
abstract class LicenseWorkerPanel extends LicensePanel {
static final Message EMPTY_MESSAGE = new BaseMessage() {
static final long serialVersionUID = 0L;
@Override public String toString(Locale locale) { return ""; }
};
LicenseWorkerPanel(LicenseWizard wizard) { super(wizard); }
abstract void setStatusMessage(Message message);
abstract Message successMessage();
abstract Message failureMessage(Throwable throwable);
static boolean isConsideredConfidential(Throwable ex) {
return ex instanceof LicenseManagementException &&
((LicenseManagementException) ex).isConsideredConfidential();
}
@SuppressWarnings("PackageVisibleInnerClass")
abstract class LicenseWorker extends SwingWorker {
@Override protected final void done() {
try {
get();
setStatusMessage(successMessage());
} catch (ExecutionException ex) {
showExceptionDialog(ex.getCause());
} catch (InterruptedException ex) {
showExceptionDialog(ex);
}
}
void showExceptionDialog(final Throwable ex) {
assert isEventDispatchThread();
JOptionPane.showMessageDialog(
LicenseWorkerPanel.this,
isConsideredConfidential(ex)
? failureMessage(ex).toString()
: ex.getLocalizedMessage(),
LicenseWizardMessage.failure_title.format().toString(),
JOptionPane.ERROR_MESSAGE);
}
}
}