From 8f00bd20c95d6949ba82e6626ceff2a22354ba9b Mon Sep 17 00:00:00 2001 From: Tres Finocchiaro Date: Fri, 13 Sep 2024 00:09:00 -0400 Subject: [PATCH] Minor refactor --- src/qz/auth/Certificate.java | 14 ++-- src/qz/ui/SiteManagerDialog.java | 2 +- src/qz/ui/component/CertificateTable.java | 87 ++++++++++++----------- 3 files changed, 52 insertions(+), 51 deletions(-) diff --git a/src/qz/auth/Certificate.java b/src/qz/auth/Certificate.java index 60cecc84b..be58e9bf3 100644 --- a/src/qz/auth/Certificate.java +++ b/src/qz/auth/Certificate.java @@ -63,10 +63,8 @@ public enum Algorithm { // Valid date range allows UI to only show "Expired" text for valid certificates private static final Instant UNKNOWN_MIN = LocalDateTime.MIN.toInstant(ZoneOffset.UTC); private static final Instant UNKNOWN_MAX = LocalDateTime.MAX.toInstant(ZoneOffset.UTC); - - // This `dateFormat` corresponds to the `dateParse` in CertificateTable. If this line is changed, change it there as well. - private static DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); - private static DateTimeFormatter dateParse = DateTimeFormatter.ofPattern("uuuu-MM-dd['T'][ ]HH:mm:ss[.n]['Z']"); //allow parsing of both ISO and custom formatted dates + public static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + public static final DateTimeFormatter DATE_PARSE = DateTimeFormatter.ofPattern("uuuu-MM-dd['T'][ ]HH:mm:ss[.n]['Z']"); //allow parsing of both ISO and custom formatted dates private X509Certificate theCertificate; private boolean sponsored; @@ -324,8 +322,8 @@ public static Certificate loadCertificate(HashMap data) { cert.organization = data.get("organization"); try { - cert.validFrom = Instant.from(LocalDateTime.from(dateParse.parse(data.get("validFrom"))).atZone(ZoneOffset.UTC)); - cert.validTo = Instant.from(LocalDateTime.from(dateParse.parse(data.get("validTo"))).atZone(ZoneOffset.UTC)); + cert.validFrom = Instant.from(LocalDateTime.from(DATE_PARSE.parse(data.get("validFrom"))).atZone(ZoneOffset.UTC)); + cert.validTo = Instant.from(LocalDateTime.from(DATE_PARSE.parse(data.get("validTo"))).atZone(ZoneOffset.UTC)); } catch(DateTimeException e) { cert.validFrom = UNKNOWN_MIN; @@ -421,7 +419,7 @@ public String getOrganization() { public String getValidFrom() { if (validFrom.isAfter(UNKNOWN_MIN)) { - return dateFormat.format(validFrom.atZone(ZoneOffset.UTC)); + return DATE_FORMAT.format(validFrom.atZone(ZoneOffset.UTC)); } else { return "Not Provided"; } @@ -429,7 +427,7 @@ public String getValidFrom() { public String getValidTo() { if (validTo.isBefore(UNKNOWN_MAX)) { - return dateFormat.format(validTo.atZone(ZoneOffset.UTC)); + return DATE_FORMAT.format(validTo.atZone(ZoneOffset.UTC)); } else { return "Not Provided"; } diff --git a/src/qz/ui/SiteManagerDialog.java b/src/qz/ui/SiteManagerDialog.java index a9235b2ce..6c3296d40 100644 --- a/src/qz/ui/SiteManagerDialog.java +++ b/src/qz/ui/SiteManagerDialog.java @@ -555,7 +555,7 @@ public ArrayList readCertificates(ArrayList dataMap = new HashMap<>(); + HashMap dataMap = new HashMap<>(); for(int i = 0; i < data.length; i++) { dataMap.put(Certificate.saveFields[i], data[i]); } diff --git a/src/qz/ui/component/CertificateTable.java b/src/qz/ui/component/CertificateTable.java index 292baa6fa..f58ecfb3a 100644 --- a/src/qz/ui/component/CertificateTable.java +++ b/src/qz/ui/component/CertificateTable.java @@ -11,11 +11,12 @@ import java.time.LocalDateTime; import java.time.ZoneOffset; import java.time.ZonedDateTime; -import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.util.TimeZone; import java.util.function.Function; +import static qz.auth.Certificate.*; + /** * Created by Tres on 2/22/2015. * Displays Certificate information in a JTable @@ -23,13 +24,10 @@ public class CertificateTable extends DisplayTable implements Themeable { private Certificate cert; - private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone("UTC"); - private static DateTimeFormatter dateParse = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); - + private static final TimeZone DEFAULT_TIME_ZONE = TimeZone.getTimeZone("UTC"); + private static final TimeZone ALTERNATE_TIME_ZONE = TimeZone.getDefault(); private Instant warn; private Instant now; - private boolean useLocalTimezoneValidTo = false; - private boolean useLocalTimezoneValidFrom = false; enum CertificateField { ORGANIZATION("Organization", (Certificate cert) -> cert.getOrganization()), @@ -41,21 +39,29 @@ enum CertificateField { String description; Function getter; + TimeZone timeZone = DEFAULT_TIME_ZONE; // Date fields only CertificateField(String description, Function getter) { this.description = description; this.getter = getter; } - public String getValue(Certificate cert, TimeZone timeZone) { + public String getValue(Certificate cert) { String certFieldValue = getter.apply(cert).toString(); switch(this) { case VALID_FROM: case VALID_TO: - if (certFieldValue.equals("Not Provided")) return certFieldValue; - ZonedDateTime utcTime = LocalDateTime.from(dateParse.parse(certFieldValue)).atZone(ZoneOffset.UTC); // Parse the date string as UTC (Z/GMT) - ZonedDateTime zonedTime = Instant.from(utcTime).atZone(timeZone.toZoneId()); // Shift to the new timezone - return dateParse.format(zonedTime) + " " + timeZone.getDisplayName(false, TimeZone.SHORT); // Append a short timezone name e.g. "EST" + if (!certFieldValue.equals("Not Provided")) { + try { + // Parse the date string as UTC (Z/GMT) + ZonedDateTime utcTime = LocalDateTime.from(DATE_PARSE.parse(certFieldValue)).atZone(ZoneOffset.UTC); + // Shift to the new timezone + ZonedDateTime zonedTime = Instant.from(utcTime).atZone(timeZone.toZoneId()); + // Append a short timezone name e.g. "EST" + return DATE_PARSE.format(zonedTime) + " " + timeZone.getDisplayName(false, TimeZone.SHORT); + } catch (Exception ignore) {} + } + // fallthrough default: return certFieldValue; } @@ -73,36 +79,44 @@ public String getDescription() { public static int size() { return values().length; } + + public void toggleTimeZone() { + switch(this) { + case VALID_TO: + case VALID_FROM: + this.timeZone = (timeZone == DEFAULT_TIME_ZONE? ALTERNATE_TIME_ZONE:DEFAULT_TIME_ZONE); + break; + default: + throw new UnsupportedOperationException("TimeZone is only supported for date fields"); + } + } } public CertificateTable(IconCache iconCache) { super(iconCache); setDefaultRenderer(Object.class, new CertificateTableCellRenderer()); addMouseListener(new MouseAdapter() { - int lastRow = -1; - int lastCol = -1; + Point loc = new Point(-1, -1); @Override public void mousePressed(MouseEvent e) { super.mousePressed(e); JTable target = (JTable)e.getSource(); - int row = target.getSelectedRow(); - int col = target.getSelectedColumn(); - // Only trigger after the cell is click AND highlighted. This make copying text easier. - if (row == lastRow && col == lastCol) { - CertificateField rowKey = (CertificateField)target.getValueAt(row, 0); - if (rowKey == CertificateField.VALID_TO) { - useLocalTimezoneValidTo = !useLocalTimezoneValidTo; - refreshComponents(); - changeSelection(row, col, false, false); - } else if (rowKey == CertificateField.VALID_FROM) { - useLocalTimezoneValidFrom = !useLocalTimezoneValidFrom; - refreshComponents(); - changeSelection(row, col, false, false); + int x = target.getSelectedColumn(); + int y = target.getSelectedRow(); + // Only trigger after the cell is click AND highlighted. + if (loc.distance(x, y) == 0) { + CertificateField rowKey = (CertificateField)target.getValueAt(y, 0); + switch(rowKey) { + case VALID_FROM: + case VALID_TO: + rowKey.toggleTimeZone(); + refreshComponents(); + changeSelection(y, x, false, false); + break; } } - lastRow = row; - lastCol = col; + loc.setLocation(x, y); } }); @@ -128,21 +142,10 @@ public void refreshComponents() { // First Column for(CertificateField field : CertificateField.values()) { - TimeZone timeZone = null; - switch(field){ - case TRUSTED: - if (!Certificate.isTrustBuiltIn()) continue; // Remove "Verified by" text; uncertain in strict mode - break; - case VALID_TO: - timeZone = useLocalTimezoneValidTo ? TimeZone.getDefault() : UTC_TIME_ZONE; - break; - case VALID_FROM: - timeZone = useLocalTimezoneValidFrom ? TimeZone.getDefault() : UTC_TIME_ZONE; - break; - default: - break; + if(field.equals(CertificateField.TRUSTED) && !Certificate.isTrustBuiltIn()) { + continue; // Remove "Verified by" text; uncertain in strict mode } - model.addRow(new Object[] {field, field.getValue(cert, timeZone)}); + model.addRow(new Object[] {field, field.getValue(cert)}); } repaint();