Skip to content

Commit

Permalink
A few smaller code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
kaikramer committed Aug 31, 2023
1 parent 6d0e8b7 commit abf97c9
Show file tree
Hide file tree
Showing 4 changed files with 337 additions and 252 deletions.
103 changes: 51 additions & 52 deletions kse/src/org/kse/gui/actions/CompareCertificateAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,65 +19,64 @@

/**
* Action to show Compare Certificate dialog.
*
*/
public class CompareCertificateAction extends KeyStoreExplorerAction {

private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 1L;

/**
* Construct action.
*
* @param kseFrame KeyStore Explorer frame
*/
public CompareCertificateAction(KseFrame kseFrame) {
super(kseFrame);
putValue(LONG_DESCRIPTION, res.getString("CompareCertificateAction.statusbar"));
putValue(NAME, res.getString("CompareCertificateAction.text"));
putValue(SHORT_DESCRIPTION, res.getString("CompareCertificateAction.tooltip"));
putValue(SMALL_ICON, new ImageIcon(
Toolkit.getDefaultToolkit().createImage(getClass().getResource("images/exportkeypair.png"))));
}
/**
* Construct action.
*
* @param kseFrame KeyStore Explorer frame
*/
public CompareCertificateAction(KseFrame kseFrame) {
super(kseFrame);
putValue(LONG_DESCRIPTION, res.getString("CompareCertificateAction.statusbar"));
putValue(NAME, res.getString("CompareCertificateAction.text"));
putValue(SHORT_DESCRIPTION, res.getString("CompareCertificateAction.tooltip"));
putValue(SMALL_ICON, new ImageIcon(
Toolkit.getDefaultToolkit().createImage(getClass().getResource("images/exportkeypair.png"))));
}

@Override
protected void doAction() {
List<Certificate> listCertificate = getCertificates();
if (listCertificate != null && listCertificate.size() == 2) {
X509Certificate cert1 = (X509Certificate) listCertificate.get(0);
X509Certificate cert2 = (X509Certificate) listCertificate.get(1);
DCompareCertificates dialog = new DCompareCertificates(frame, cert1,cert2);
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
} else {
JOptionPane.showMessageDialog(frame, res.getString("CompareCertificateAction.onlytwo.message"),
res.getString("CompareCertificateAction.Title"), JOptionPane.WARNING_MESSAGE);
}
}
@Override
protected void doAction() {
List<Certificate> listCertificate = getCertificates();
if (listCertificate != null && listCertificate.size() == 2) {
X509Certificate cert1 = (X509Certificate) listCertificate.get(0);
X509Certificate cert2 = (X509Certificate) listCertificate.get(1);
DCompareCertificates dialog = new DCompareCertificates(frame, cert1, cert2);
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
} else {
JOptionPane.showMessageDialog(frame, res.getString("CompareCertificateAction.onlytwo.message"),
res.getString("CompareCertificateAction.Title"), JOptionPane.WARNING_MESSAGE);
}
}

private List<Certificate> getCertificates() {
KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
KeyStoreState currentState = history.getCurrentState();
private List<Certificate> getCertificates() {
KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
KeyStoreState currentState = history.getCurrentState();

String[] aliases = kseFrame.getSelectedEntryAliases();
String[] aliases = kseFrame.getSelectedEntryAliases();

if (aliases.length < 2) {
return null;
}
try {
List<Certificate> listCertificates = new ArrayList<>();
KeyStore keyStore = currentState.getKeyStore();
for (String alias : aliases) {
if (KeyStoreUtil.isTrustedCertificateEntry(alias, keyStore)
|| KeyStoreUtil.isKeyPairEntry(alias, keyStore)) {
Certificate certificate = keyStore.getCertificate(alias);
listCertificates.add(certificate);
}
}
return listCertificates;
} catch (Exception ex) {
DError.displayError(frame, ex);
return null;
}
}
if (aliases.length < 2) {
return null;
}
try {
List<Certificate> listCertificates = new ArrayList<>();
KeyStore keyStore = currentState.getKeyStore();
for (String alias : aliases) {
if (KeyStoreUtil.isTrustedCertificateEntry(alias, keyStore) ||
KeyStoreUtil.isKeyPairEntry(alias, keyStore)) {
Certificate certificate = keyStore.getCertificate(alias);
listCertificates.add(certificate);
}
}
return listCertificates;
} catch (Exception ex) {
DError.displayError(frame, ex);
return null;
}
}

}
5 changes: 4 additions & 1 deletion kse/src/org/kse/gui/datetime/DDateTimeChooser.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@
public class DDateTimeChooser extends JEscDialog {
private static final long serialVersionUID = 1L;

public static int MAX_YEAR = 2200;
public static int MIN_YEAR = 1900;

private static ResourceBundle res = ResourceBundle.getBundle("org/kse/gui/datetime/resources");

private static final String[] MONTH_NAMES = new String[] { res.getString("DDateTimeChooser.Month.January"),
Expand Down Expand Up @@ -173,7 +176,7 @@ private void initComponents(Date date) {
jcbMonth = new JComboBox<>(MONTH_NAMES);
jcbMonth.addItemListener(evt -> update());

jsYear = new JSpinner(new SpinnerNumberModel(calendar.get(Calendar.YEAR), 1900, 2200, 1));
jsYear = new JSpinner(new SpinnerNumberModel(calendar.get(Calendar.YEAR), MIN_YEAR, MAX_YEAR, 1));
jsYear.setEditor(new JSpinner.NumberEditor(jsYear, "0000"));
jsYear.addChangeListener(evt -> update());

Expand Down
Loading

0 comments on commit abf97c9

Please sign in to comment.