From 9366fdf0cb8bcc45bbf1af68ee3284049e56c228 Mon Sep 17 00:00:00 2001 From: jonwltn <86822083+jonwltn@users.noreply.github.com> Date: Wed, 23 Sep 2026 01:49:46 -0700 Subject: [PATCH 1/3] Update all the add/edit/remove panels to MiG Layout. Other updates: - Refactored to extend from a common panel. - Updated all table models to follow the same pattern --- .../crypto/x509/PolicyInformationUtil.java | 7 +- .../kse/gui/crypto/JAddEditRemovePanel.java | 378 +++++++++++++++++ .../AccessDescriptionsTableModel.java | 28 +- .../JAccessDescriptions.java | 363 ++-------------- .../CustomExtKeyUsageTableModel.java | 31 +- .../JCustomExtendedKeyUsage.java | 376 ++--------------- .../DistributionPointsTableCellRend.java | 3 + .../DistributionPointsTableModel.java | 44 +- .../JDistributionPoints.java | 348 ++------------- .../generalname/GeneralNamesTableModel.java | 55 +-- .../gui/crypto/generalname/JGeneralName.java | 1 + .../gui/crypto/generalname/JGeneralNames.java | 343 ++------------- .../GeneralSubtreesTableModel.java | 30 +- .../generalsubtree/JGeneralSubtrees.java | 365 ++-------------- .../DPolicyInformationChooser.java | 17 +- .../DPolicyQualifierInfoChooser.java | 13 +- .../policyinformation/JPolicyInformation.java | 397 ++--------------- .../JPolicyQualifierInfo.java | 398 ++---------------- .../crypto/policyinformation/JUserNotice.java | 1 + .../PolicyInformationTableCellRend.java | 13 +- .../PolicyInformationTableModel.java | 36 +- .../PolicyQualifierInfoTableCellRend.java | 13 +- .../PolicyQualifierInfoTableModel.java | 37 +- .../crypto/policymapping/JPolicyMappings.java | 373 +++------------- .../PolicyMappingsTableModel.java | 39 +- .../java/org/kse/gui/datetime/JDateTime.java | 1 + .../DAuthorityInformationAccess.java | 9 +- .../extensions/DCertificatePolicies.java | 11 +- .../extensions/DCrlDistributionPoints.java | 14 +- .../extensions/DCustomExtKeyUsage.java | 4 +- .../dialogs/extensions/DPolicyMappings.java | 5 +- .../extensions/DSubjectInformationAccess.java | 10 +- .../org/kse/gui/table/LoadableTableModel.java | 38 ++ 33 files changed, 852 insertions(+), 2949 deletions(-) create mode 100644 kse/src/main/java/org/kse/gui/crypto/JAddEditRemovePanel.java create mode 100644 kse/src/main/java/org/kse/gui/table/LoadableTableModel.java diff --git a/kse/src/main/java/org/kse/crypto/x509/PolicyInformationUtil.java b/kse/src/main/java/org/kse/crypto/x509/PolicyInformationUtil.java index 314629f74..17e42978d 100644 --- a/kse/src/main/java/org/kse/crypto/x509/PolicyInformationUtil.java +++ b/kse/src/main/java/org/kse/crypto/x509/PolicyInformationUtil.java @@ -22,7 +22,6 @@ import static org.kse.crypto.x509.CertificatePolicyQualifierType.PKIX_CPS_POINTER_QUALIFIER; import static org.kse.crypto.x509.CertificatePolicyQualifierType.PKIX_USER_NOTICE_QUALIFIER; -import java.io.IOException; import java.text.MessageFormat; import java.util.ResourceBundle; @@ -99,9 +98,8 @@ private PolicyInformationUtil() { * * @param policyInformation Policy information * @return String representation of policy information - * @throws IOException If policy information is invalid */ - public static String toString(PolicyInformation policyInformation) throws IOException { + public static String toString(PolicyInformation policyInformation) { StringBuilder sbPolicyInformation = new StringBuilder(); ASN1ObjectIdentifier policyIdentifier = policyInformation.getPolicyIdentifier(); @@ -139,9 +137,8 @@ public static String toString(PolicyInformation policyInformation) throws IOExce * * @param policyQualifierInfo Policy qualifier info * @return String representation of policy qualifier info - * @throws IOException If policy qualifier info is invalid */ - public static String toString(PolicyQualifierInfo policyQualifierInfo) throws IOException { + public static String toString(PolicyQualifierInfo policyQualifierInfo) { StringBuilder sbPolicyQualifier = new StringBuilder(); ASN1ObjectIdentifier policyQualifierId = policyQualifierInfo.getPolicyQualifierId(); diff --git a/kse/src/main/java/org/kse/gui/crypto/JAddEditRemovePanel.java b/kse/src/main/java/org/kse/gui/crypto/JAddEditRemovePanel.java new file mode 100644 index 000000000..87c5c49eb --- /dev/null +++ b/kse/src/main/java/org/kse/gui/crypto/JAddEditRemovePanel.java @@ -0,0 +1,378 @@ +/* + * Copyright 2004 - 2013 Wayne Grant + * 2013 - 2026 Kai Kramer + * + * This file is part of KeyStore Explorer. + * + * KeyStore Explorer is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * KeyStore Explorer is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with KeyStore Explorer. If not, see . + */ +package org.kse.gui.crypto; + +import java.awt.Dimension; +import java.awt.Insets; +import java.awt.Point; +import java.awt.Toolkit; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import javax.swing.ImageIcon; +import javax.swing.JButton; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTable; +import javax.swing.ListSelectionModel; +import javax.swing.ScrollPaneConstants; + +import org.kse.gui.CursorUtil; +import org.kse.gui.PlatformUtil; +import org.kse.gui.table.LoadableTableModel; +import org.kse.utilities.os.OperatingSystem; + +import net.miginfocom.swing.MigLayout; + +/** + * Abstract component with infrastructure to edit a set of items. + * + * @param the type of collection. + * @param the type of elements to be managed. + */ +public abstract class JAddEditRemovePanel, E> extends JPanel { + private static final long serialVersionUID = 1L; + + private JButton jbAdd; + private JButton jbEdit; + private JButton jbRemove; + private JScrollPane jspTable; + private JTable jtTable; + + private C items; + private boolean enabled = true; + + protected JAddEditRemovePanel() { + initComponents(); + } + + protected abstract String getAddResource(); + protected abstract String getEditResource(); + protected abstract String getRemoveResource(); + protected abstract String getBundleString(String suffix); + protected abstract C newCollection(); + protected abstract JTable newTable(); + protected abstract E getItem(E item); + + private void initComponents() { + jbAdd = new JButton( + new ImageIcon(Toolkit.getDefaultToolkit().createImage(getClass().getResource(getAddResource())))); + jbAdd.setMargin(new Insets(2, 2, 2, 2)); + jbAdd.setToolTipText(getBundleString("jbAdd.tooltip")); + jbAdd.setMnemonic(getBundleString("jbAdd.mnemonic").charAt(0)); + + jbAdd.addActionListener(evt -> { + try { + CursorUtil.setCursorBusy(JAddEditRemovePanel.this); + addPressed(); + } finally { + CursorUtil.setCursorFree(JAddEditRemovePanel.this); + } + }); + + jbEdit = new JButton( + new ImageIcon(Toolkit.getDefaultToolkit().createImage(getClass().getResource(getEditResource())))); + jbEdit.setMargin(new Insets(2, 2, 2, 2)); + jbEdit.setToolTipText(getBundleString("jbEdit.tooltip")); + jbEdit.setMnemonic(getBundleString("jbEdit.mnemonic").charAt(0)); + + jbEdit.setEnabled(false); + + jbEdit.addActionListener(evt -> { + try { + CursorUtil.setCursorBusy(JAddEditRemovePanel.this); + editPressed(); + } finally { + CursorUtil.setCursorFree(JAddEditRemovePanel.this); + } + }); + + jbRemove = new JButton( + new ImageIcon(Toolkit.getDefaultToolkit().createImage(getClass().getResource(getRemoveResource())))); + jbRemove.setMargin(new Insets(2, 2, 2, 2)); + jbRemove.setToolTipText(getBundleString("jbRemove.tooltip")); + jbRemove.setMnemonic(getBundleString("jbRemove.mnemonic").charAt(0)); + + jbRemove.setEnabled(false); + + jbRemove.addActionListener(evt -> { + try { + CursorUtil.setCursorBusy(JAddEditRemovePanel.this); + removePressed(); + } finally { + CursorUtil.setCursorFree(JAddEditRemovePanel.this); + } + }); + + jtTable = newTable(); + + ListSelectionModel selectionModel = jtTable.getSelectionModel(); + selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + selectionModel.addListSelectionListener(evt -> { + if (!evt.getValueIsAdjusting()) { + updateButtonControls(); + } + }); + + jtTable.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent evt) { + maybeEditAccessDescription(evt); + } + }); + + jtTable.addKeyListener(new KeyAdapter() { + boolean deleteLastPressed = false; + + @Override + public void keyPressed(KeyEvent evt) { + // Record delete pressed on non-Macs + if (!OperatingSystem.isMacOs()) { + deleteLastPressed = evt.getKeyCode() == KeyEvent.VK_DELETE; + } + } + + @Override + public void keyReleased(KeyEvent evt) { + // Delete on non-Mac if delete was pressed and is now released + if (!OperatingSystem.isMacOs() && deleteLastPressed && evt.getKeyCode() == KeyEvent.VK_DELETE) { + try { + CursorUtil.setCursorBusy(JAddEditRemovePanel.this); + deleteLastPressed = false; + removeSelectedItem(); + } finally { + CursorUtil.setCursorFree(JAddEditRemovePanel.this); + } + } + } + + @Override + public void keyTyped(KeyEvent evt) { + // Delete on Mac if backspace typed + if (OperatingSystem.isMacOs() && evt.getKeyChar() == 0x08) { + try { + CursorUtil.setCursorBusy(JAddEditRemovePanel.this); + removeSelectedItem(); + } finally { + CursorUtil.setCursorFree(JAddEditRemovePanel.this); + } + } + } + }); + + jspTable = PlatformUtil.createScrollPane(jtTable, + ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, + ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); + jspTable.getViewport().setBackground(jtTable.getBackground()); + + setPreferredSize(new Dimension(400, 150)); + + // layout + setLayout(new MigLayout("insets 0, fill", "[]", "[]")); + add(jspTable, "grow, push"); + add(jbAdd, "split 3, flowy"); + add(jbEdit); + add(jbRemove, "wrap rel"); + + populate(); + } + + /** + * Get the items. + * + * @return The collection of items. + */ + public C getItems() { + return items; + } + + /** + * Set items. + * + * @param items Items to set for the Add/Edit/Remove panel + */ + public void setItems(C items) { + this.items = items; + populate(); + } + + /** + * Sets whether or not the component is enabled. + * + * @param enabled True if this component should be enabled, false otherwise + */ + @Override + public void setEnabled(boolean enabled) { + this.enabled = enabled; + + updateButtonControls(); + } + + /** + * Set component's tooltip text. + * + * @param toolTipText Tooltip text + */ + @Override + public void setToolTipText(String toolTipText) { + super.setToolTipText(toolTipText); + jspTable.setToolTipText(toolTipText); + jtTable.setToolTipText(toolTipText); + } + + private void populate() { + if (items == null) { + items = newCollection(); + } + + reloadTable(); + selectFirstItemInTable(); + updateButtonControls(); + } + + private void addPressed() { + E newItem = getItem(null); + + if (newItem == null) { + return; + } + + items.add(newItem); + + populate(); + selectItemInTable(newItem); + } + + private void removePressed() { + removeSelectedItem(); + } + + protected void removeSelectedItem() { + int selectedRow = jtTable.getSelectedRow(); + + if (selectedRow != -1) { + Object item = jtTable.getValueAt(selectedRow, 0); + + items.remove(item); + + reloadTable(); + selectFirstItemInTable(); + updateButtonControls(); + } + } + + private void editPressed() { + editSelectedAccessDescription(); + } + + private void maybeEditAccessDescription(MouseEvent evt) { + if (evt.getClickCount() > 1) { + Point point = new Point(evt.getX(), evt.getY()); + int row = jtTable.rowAtPoint(point); + + if (row != -1) { + try { + CursorUtil.setCursorBusy(JAddEditRemovePanel.this); + jtTable.setRowSelectionInterval(row, row); + editSelectedAccessDescription(); + } finally { + CursorUtil.setCursorFree(JAddEditRemovePanel.this); + } + } + } + } + + private void updateButtonControls() { + if (!enabled) { + jbAdd.setEnabled(false); + jbEdit.setEnabled(false); + jbRemove.setEnabled(false); + } else { + jbAdd.setEnabled(true); + + int selectedRow = jtTable.getSelectedRow(); + + if (selectedRow == -1) { + jbEdit.setEnabled(false); + jbRemove.setEnabled(false); + } else { + jbEdit.setEnabled(true); + jbRemove.setEnabled(true); + } + } + } + + private void editSelectedAccessDescription() { + int selectedRow = jtTable.getSelectedRow(); + + if (selectedRow != -1) { + @SuppressWarnings("unchecked") + E item = (E) jtTable.getValueAt(selectedRow, 0); + + E newItem = getItem(item); + + if (newItem == null) { + return; + } + + items.remove(item); + items.add(newItem); + + populate(); + selectItemInTable(newItem); + } + } + + private void selectItemInTable(E item) { + for (int i = 0; i < jtTable.getRowCount(); i++) { + if (item.equals(jtTable.getValueAt(i, 0))) { + jtTable.changeSelection(i, 0, false, false); + return; + } + } + } + + @SuppressWarnings("unchecked") + private void reloadTable() { + List tableList; + if (items instanceof List) { + tableList = (List) items; + } else { + tableList = new ArrayList<>(items); + } + getItemsTableModel().load(tableList); + } + + private void selectFirstItemInTable() { + if (jtTable.getModel().getRowCount() > 0) { + jtTable.changeSelection(0, 0, false, false); + } + } + + @SuppressWarnings("unchecked") + private LoadableTableModel getItemsTableModel() { + return (LoadableTableModel) jtTable.getModel(); + } +} diff --git a/kse/src/main/java/org/kse/gui/crypto/accessdescription/AccessDescriptionsTableModel.java b/kse/src/main/java/org/kse/gui/crypto/accessdescription/AccessDescriptionsTableModel.java index 49de43a0d..50c6871dc 100644 --- a/kse/src/main/java/org/kse/gui/crypto/accessdescription/AccessDescriptionsTableModel.java +++ b/kse/src/main/java/org/kse/gui/crypto/accessdescription/AccessDescriptionsTableModel.java @@ -19,20 +19,21 @@ */ package org.kse.gui.crypto.accessdescription; -import java.util.Arrays; +import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.ResourceBundle; import org.bouncycastle.asn1.x509.AccessDescription; import org.kse.crypto.x509.GeneralNameUtil; +import org.kse.gui.table.LoadableTableModel; import org.kse.gui.table.ToolTipTableModel; import org.kse.utilities.oid.ObjectIdComparator; /** * The table model used to display access descriptions. */ -public class AccessDescriptionsTableModel extends ToolTipTableModel { +public class AccessDescriptionsTableModel extends ToolTipTableModel implements LoadableTableModel { private static final long serialVersionUID = 1L; private static ResourceBundle res = ResourceBundle.getBundle("org/kse/gui/crypto/accessdescription/resources"); private static ObjectIdComparator objectIdComparator = new ObjectIdComparator(); @@ -43,7 +44,7 @@ public class AccessDescriptionsTableModel extends ToolTipTableModel { }; private String[] columnNames; - private Object[][] data; + private List data; /** * Construct a new AccessDescriptionsTableModel. @@ -54,7 +55,7 @@ public AccessDescriptionsTableModel() { columnNames[0] = res.getString("AccessDescriptionsTableModel.AccessMethodColumn"); columnNames[1] = res.getString("AccessDescriptionsTableModel.AccessLocationColumn"); - data = new Object[0][0]; + data = new ArrayList<>(); } /** @@ -62,19 +63,10 @@ public AccessDescriptionsTableModel() { * * @param accessDescriptions The access descriptions */ + @Override public void load(List accessDescriptions) { - AccessDescription[] accessDescriptionsArray = accessDescriptions.toArray( - AccessDescription[]::new); - Arrays.sort(accessDescriptionsArray, new AccessDescriptionMethodComparator()); - - data = new Object[accessDescriptionsArray.length][2]; - - int i = 0; - for (AccessDescription accessDescription : accessDescriptionsArray) { - data[i][0] = accessDescription; - data[i][1] = accessDescription; - i++; - } + data = accessDescriptions; + data.sort(new AccessDescriptionMethodComparator()); fireTableDataChanged(); } @@ -96,7 +88,7 @@ public int getColumnCount() { */ @Override public int getRowCount() { - return data.length; + return data.size(); } /** @@ -119,7 +111,7 @@ public String getColumnName(int col) { */ @Override public Object getValueAt(int row, int col) { - return data[row][col]; + return data.get(row); } /** diff --git a/kse/src/main/java/org/kse/gui/crypto/accessdescription/JAccessDescriptions.java b/kse/src/main/java/org/kse/gui/crypto/accessdescription/JAccessDescriptions.java index 018030928..428713c7a 100644 --- a/kse/src/main/java/org/kse/gui/crypto/accessdescription/JAccessDescriptions.java +++ b/kse/src/main/java/org/kse/gui/crypto/accessdescription/JAccessDescriptions.java @@ -19,58 +19,30 @@ */ package org.kse.gui.crypto.accessdescription; -import java.awt.BorderLayout; import java.awt.Container; -import java.awt.Dimension; -import java.awt.Insets; -import java.awt.Point; -import java.awt.Toolkit; -import java.awt.event.KeyAdapter; -import java.awt.event.KeyEvent; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; import java.util.ArrayList; import java.util.List; import java.util.ResourceBundle; -import javax.swing.Box; -import javax.swing.BoxLayout; -import javax.swing.ImageIcon; -import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; -import javax.swing.JPanel; -import javax.swing.JScrollPane; import javax.swing.JTable; -import javax.swing.ListSelectionModel; -import javax.swing.ScrollPaneConstants; import javax.swing.table.TableColumn; import javax.swing.table.TableRowSorter; import org.bouncycastle.asn1.x509.AccessDescription; -import org.kse.gui.CursorUtil; -import org.kse.gui.PlatformUtil; +import org.kse.gui.crypto.JAddEditRemovePanel; import org.kse.gui.table.ToolTipTable; -import org.kse.utilities.os.OperatingSystem; /** * Component to edit a set of access descriptions. */ -public class JAccessDescriptions extends JPanel { +public class JAccessDescriptions extends JAddEditRemovePanel, AccessDescription> { private static final long serialVersionUID = 1L; private static ResourceBundle res = ResourceBundle.getBundle("org/kse/gui/crypto/accessdescription/resources"); - private JPanel jpAccessDescriptionButtons; - private JButton jbAdd; - private JButton jbEdit; - private JButton jbRemove; - private JScrollPane jspAccessDescriptions; - private JTable jtAccessDescriptions; - private String title; - private List accessDescriptions; - private boolean enabled = true; /** * Construct a JAccessDescriptions. @@ -79,71 +51,37 @@ public class JAccessDescriptions extends JPanel { */ public JAccessDescriptions(String title) { this.title = title; - initComponents(); } - private void initComponents() { - jbAdd = new JButton(new ImageIcon( - Toolkit.getDefaultToolkit().createImage(getClass().getResource("images/add_access_desc.png")))); - jbAdd.setMargin(new Insets(2, 2, 0, 0)); - jbAdd.setToolTipText(res.getString("JAccessDescriptions.jbAdd.tooltip")); - jbAdd.setMnemonic(res.getString("JAccessDescriptions.jbAdd.mnemonic").charAt(0)); - - jbAdd.addActionListener(evt -> { - try { - CursorUtil.setCursorBusy(JAccessDescriptions.this); - addPressed(); - } finally { - CursorUtil.setCursorFree(JAccessDescriptions.this); - } - }); - - jbEdit = new JButton(new ImageIcon( - Toolkit.getDefaultToolkit().createImage(getClass().getResource("images/edit_access_desc.png")))); - jbEdit.setMargin(new Insets(2, 2, 0, 0)); - jbEdit.setToolTipText(res.getString("JAccessDescriptions.jbEdit.tooltip")); - jbEdit.setMnemonic(res.getString("JAccessDescriptions.jbEdit.mnemonic").charAt(0)); - - jbEdit.setEnabled(false); - - jbEdit.addActionListener(evt -> { - try { - CursorUtil.setCursorBusy(JAccessDescriptions.this); - editPressed(); - } finally { - CursorUtil.setCursorFree(JAccessDescriptions.this); - } - }); + @Override + protected String getAddResource() { + return "images/add_access_desc.png"; + } - jbRemove = new JButton(new ImageIcon( - Toolkit.getDefaultToolkit().createImage(getClass().getResource("images/remove_access_desc.png")))); - jbRemove.setMargin(new Insets(2, 2, 0, 0)); - jbRemove.setToolTipText(res.getString("JAccessDescriptions.jbRemove.tooltip")); - jbRemove.setMnemonic(res.getString("JAccessDescriptions.jbRemove.mnemonic").charAt(0)); + @Override + protected String getEditResource() { + return "images/edit_access_desc.png"; + } - jbRemove.setEnabled(false); + @Override + protected String getRemoveResource() { + return "images/remove_access_desc.png"; + } - jbRemove.addActionListener(evt -> { - try { - CursorUtil.setCursorBusy(JAccessDescriptions.this); - removePressed(); - } finally { - CursorUtil.setCursorFree(JAccessDescriptions.this); - } - }); + @Override + protected String getBundleString(String suffix) { + return res.getString("JAccessDescriptions." + suffix); + } - jpAccessDescriptionButtons = new JPanel(); - jpAccessDescriptionButtons.setLayout(new BoxLayout(jpAccessDescriptionButtons, BoxLayout.Y_AXIS)); - jpAccessDescriptionButtons.add(Box.createVerticalGlue()); - jpAccessDescriptionButtons.add(jbAdd); - jpAccessDescriptionButtons.add(Box.createVerticalStrut(3)); - jpAccessDescriptionButtons.add(jbEdit); - jpAccessDescriptionButtons.add(Box.createVerticalStrut(3)); - jpAccessDescriptionButtons.add(jbRemove); - jpAccessDescriptionButtons.add(Box.createVerticalGlue()); + @Override + protected List newCollection() { + return new ArrayList<>(); + } + @Override + protected JTable newTable() { AccessDescriptionsTableModel accessDescriptionsTableModel = new AccessDescriptionsTableModel(); - jtAccessDescriptions = new ToolTipTable(accessDescriptionsTableModel); + JTable jtAccessDescriptions = new ToolTipTable(accessDescriptionsTableModel); TableRowSorter sorter = new TableRowSorter<>(accessDescriptionsTableModel); sorter.setComparator(0, new AccessDescriptionsTableModel.AccessDescriptionMethodComparator()); @@ -162,263 +100,24 @@ private void initComponents() { column.setCellRenderer(new AccessDescriptionsTableCellRend()); } - ListSelectionModel selectionModel = jtAccessDescriptions.getSelectionModel(); - selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); - selectionModel.addListSelectionListener(evt -> { - if (!evt.getValueIsAdjusting()) { - updateButtonControls(); - } - }); - - jtAccessDescriptions.addMouseListener(new MouseAdapter() { - @Override - public void mouseClicked(MouseEvent evt) { - maybeEditAccessDescription(evt); - } - }); - - jtAccessDescriptions.addKeyListener(new KeyAdapter() { - boolean deleteLastPressed = false; - - @Override - public void keyPressed(KeyEvent evt) { - // Record delete pressed on non-Macs - if (!OperatingSystem.isMacOs()) { - deleteLastPressed = evt.getKeyCode() == KeyEvent.VK_DELETE; - } - } - - @Override - public void keyReleased(KeyEvent evt) { - // Delete on non-Mac if delete was pressed and is now released - if (!OperatingSystem.isMacOs() && deleteLastPressed && evt.getKeyCode() == KeyEvent.VK_DELETE) { - try { - CursorUtil.setCursorBusy(JAccessDescriptions.this); - deleteLastPressed = false; - removeSelectedAccessDescription(); - } finally { - CursorUtil.setCursorFree(JAccessDescriptions.this); - } - } - } - - @Override - public void keyTyped(KeyEvent evt) { - // Delete on Mac if backspace typed - if (OperatingSystem.isMacOs() && evt.getKeyChar() == 0x08) { - try { - CursorUtil.setCursorBusy(JAccessDescriptions.this); - removeSelectedAccessDescription(); - } finally { - CursorUtil.setCursorFree(JAccessDescriptions.this); - } - } - } - }); - - jspAccessDescriptions = PlatformUtil.createScrollPane(jtAccessDescriptions, - ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, - ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); - jspAccessDescriptions.getViewport().setBackground(jtAccessDescriptions.getBackground()); - - this.setLayout(new BorderLayout(5, 5)); - setPreferredSize(new Dimension(400, 150)); - add(jspAccessDescriptions, BorderLayout.CENTER); - add(jpAccessDescriptionButtons, BorderLayout.EAST); - - populate(); + return jtAccessDescriptions; } - /** - * Get access descriptions. - * - * @return Access descriptions - */ - public List getAccessDescriptions() { - return accessDescriptions; - } - - /** - * Set access descriptions. - * - * @param accessDescriptions Access descriptions - */ - public void setAccessDescriptions(List accessDescriptions) { - this.accessDescriptions = accessDescriptions; - populate(); - } - - /** - * Sets whether or not the component is enabled. - * - * @param enabled True if this component should be enabled, false otherwise - */ - @Override - public void setEnabled(boolean enabled) { - this.enabled = enabled; - - updateButtonControls(); - } - - /** - * Set component's tooltip text. - * - * @param toolTipText Tooltip text - */ @Override - public void setToolTipText(String toolTipText) { - super.setToolTipText(toolTipText); - jspAccessDescriptions.setToolTipText(toolTipText); - jtAccessDescriptions.setToolTipText(toolTipText); - } - - private void populate() { - if (accessDescriptions == null) { - accessDescriptions = new ArrayList<>(); - } - - reloadAccessDescriptionsTable(); - selectFirstAccessDescriptionInTable(); - updateButtonControls(); - } + protected AccessDescription getItem(AccessDescription item) { + DAccessDescriptionChooser dAccessDescriptionChooser = null; - private void addPressed() { Container container = getTopLevelAncestor(); - DAccessDescriptionChooser dAccessDescriptionChooser = null; - if (container instanceof JDialog) { - dAccessDescriptionChooser = new DAccessDescriptionChooser((JDialog) container, title, null); + dAccessDescriptionChooser = new DAccessDescriptionChooser((JDialog) container, title, item); } else { - dAccessDescriptionChooser = new DAccessDescriptionChooser((JFrame) container, title, null); + dAccessDescriptionChooser = new DAccessDescriptionChooser((JFrame) container, title, item); } dAccessDescriptionChooser.setLocationRelativeTo(container); dAccessDescriptionChooser.setVisible(true); - AccessDescription newAccessDescription = dAccessDescriptionChooser.getAccessDescription(); - - if (newAccessDescription == null) { - return; - } - - accessDescriptions.add(newAccessDescription); - - populate(); - selectAccessDescriptionInTable(newAccessDescription); - } - - private void removePressed() { - removeSelectedAccessDescription(); - } - - private void removeSelectedAccessDescription() { - int selectedRow = jtAccessDescriptions.getSelectedRow(); - - if (selectedRow != -1) { - AccessDescription accessDescription = (AccessDescription) jtAccessDescriptions.getValueAt(selectedRow, 0); - - accessDescriptions.remove(accessDescription); - - reloadAccessDescriptionsTable(); - selectFirstAccessDescriptionInTable(); - updateButtonControls(); - } - } - - private void editPressed() { - editSelectedAccessDescription(); - } - - private void maybeEditAccessDescription(MouseEvent evt) { - if (evt.getClickCount() > 1) { - Point point = new Point(evt.getX(), evt.getY()); - int row = jtAccessDescriptions.rowAtPoint(point); - - if (row != -1) { - try { - CursorUtil.setCursorBusy(JAccessDescriptions.this); - jtAccessDescriptions.setRowSelectionInterval(row, row); - editSelectedAccessDescription(); - } finally { - CursorUtil.setCursorFree(JAccessDescriptions.this); - } - } - } - } - - private void updateButtonControls() { - if (!enabled) { - jbAdd.setEnabled(false); - jbEdit.setEnabled(false); - jbRemove.setEnabled(false); - } else { - jbAdd.setEnabled(true); - - int selectedRow = jtAccessDescriptions.getSelectedRow(); - - if (selectedRow == -1) { - jbEdit.setEnabled(false); - jbRemove.setEnabled(false); - } else { - jbEdit.setEnabled(true); - jbRemove.setEnabled(true); - } - } - } - - private void editSelectedAccessDescription() { - int selectedRow = jtAccessDescriptions.getSelectedRow(); - - if (selectedRow != -1) { - AccessDescription accessDescription = (AccessDescription) jtAccessDescriptions.getValueAt(selectedRow, 0); - - Container container = getTopLevelAncestor(); - - DAccessDescriptionChooser dAccessDescriptionChooser = null; - - if (container instanceof JDialog) { - dAccessDescriptionChooser = new DAccessDescriptionChooser((JDialog) container, title, - accessDescription); - } else { - dAccessDescriptionChooser = new DAccessDescriptionChooser((JFrame) container, title, accessDescription); - } - dAccessDescriptionChooser.setLocationRelativeTo(container); - dAccessDescriptionChooser.setVisible(true); - - AccessDescription newAccessDescription = dAccessDescriptionChooser.getAccessDescription(); - - if (newAccessDescription == null) { - return; - } - - accessDescriptions.remove(accessDescription); - accessDescriptions.add(newAccessDescription); - - populate(); - selectAccessDescriptionInTable(newAccessDescription); - } - } - - private void selectAccessDescriptionInTable(AccessDescription accessDescription) { - for (int i = 0; i < jtAccessDescriptions.getRowCount(); i++) { - if (accessDescription.equals(jtAccessDescriptions.getValueAt(i, 0))) { - jtAccessDescriptions.changeSelection(i, 0, false, false); - return; - } - } + return dAccessDescriptionChooser.getAccessDescription(); } - private void reloadAccessDescriptionsTable() { - getAccessDescriptionsTableModel().load(accessDescriptions); - } - - private void selectFirstAccessDescriptionInTable() { - if (getAccessDescriptionsTableModel().getRowCount() > 0) { - jtAccessDescriptions.changeSelection(0, 0, false, false); - } - } - - private AccessDescriptionsTableModel getAccessDescriptionsTableModel() { - return (AccessDescriptionsTableModel) jtAccessDescriptions.getModel(); - } } diff --git a/kse/src/main/java/org/kse/gui/crypto/customextkeyusage/CustomExtKeyUsageTableModel.java b/kse/src/main/java/org/kse/gui/crypto/customextkeyusage/CustomExtKeyUsageTableModel.java index 3f87d59f5..54745e776 100644 --- a/kse/src/main/java/org/kse/gui/crypto/customextkeyusage/CustomExtKeyUsageTableModel.java +++ b/kse/src/main/java/org/kse/gui/crypto/customextkeyusage/CustomExtKeyUsageTableModel.java @@ -19,18 +19,19 @@ */ package org.kse.gui.crypto.customextkeyusage; -import java.util.Arrays; +import java.util.ArrayList; import java.util.Comparator; +import java.util.List; import java.util.ResourceBundle; -import java.util.Set; import org.bouncycastle.asn1.ASN1ObjectIdentifier; +import org.kse.gui.table.LoadableTableModel; import org.kse.gui.table.ToolTipTableModel; /** * The table model used to display Custom Extended Key Usage oids. */ -public class CustomExtKeyUsageTableModel extends ToolTipTableModel { +public class CustomExtKeyUsageTableModel extends ToolTipTableModel implements LoadableTableModel { private static final long serialVersionUID = 1L; private static ResourceBundle res = ResourceBundle.getBundle("org/kse/gui/crypto/customextkeyusage/resources"); @@ -39,7 +40,7 @@ public class CustomExtKeyUsageTableModel extends ToolTipTableModel { }; private String[] columnNames; - private Object[][] data; + private List data; /** * Construct a new CustomExtKeyUsageTableModel. @@ -48,7 +49,7 @@ public CustomExtKeyUsageTableModel() { super(res, COLUMN_TOOL_TIPS); columnNames = new String[1]; columnNames[0] = res.getString("CustomExtKeyUsageTableTableModel.ObjectId"); - data = new Object[0][0]; + data = new ArrayList<>(); } /** @@ -56,18 +57,12 @@ public CustomExtKeyUsageTableModel() { * * @param objectIds The EKU oids */ - public void load(Set objectIds) { - ASN1ObjectIdentifier[] objectIdsArray = objectIds.toArray(ASN1ObjectIdentifier[]::new); - Arrays.sort(objectIdsArray, Comparator.comparing(ASN1ObjectIdentifier::getId, - Comparator.nullsFirst(String.CASE_INSENSITIVE_ORDER))); - - data = new Object[objectIdsArray.length][2]; + @Override + public void load(List objectIds) { - int i = 0; - for (ASN1ObjectIdentifier CustomExtKeyUsage : objectIdsArray) { - data[i][0] = CustomExtKeyUsage; - i++; - } + data = objectIds; + data.sort(Comparator.comparing(ASN1ObjectIdentifier::getId, + Comparator.nullsFirst(String.CASE_INSENSITIVE_ORDER))); fireTableDataChanged(); } @@ -89,7 +84,7 @@ public int getColumnCount() { */ @Override public int getRowCount() { - return data.length; + return data.size(); } /** @@ -112,7 +107,7 @@ public String getColumnName(int col) { */ @Override public Object getValueAt(int row, int col) { - return data[row][col]; + return data.get(row); } /** diff --git a/kse/src/main/java/org/kse/gui/crypto/customextkeyusage/JCustomExtendedKeyUsage.java b/kse/src/main/java/org/kse/gui/crypto/customextkeyusage/JCustomExtendedKeyUsage.java index e3c2afae1..e2f3d64b8 100644 --- a/kse/src/main/java/org/kse/gui/crypto/customextkeyusage/JCustomExtendedKeyUsage.java +++ b/kse/src/main/java/org/kse/gui/crypto/customextkeyusage/JCustomExtendedKeyUsage.java @@ -19,63 +19,34 @@ */ package org.kse.gui.crypto.customextkeyusage; -import java.awt.BorderLayout; import java.awt.Container; -import java.awt.Dimension; -import java.awt.Insets; -import java.awt.Point; -import java.awt.Toolkit; -import java.awt.event.KeyAdapter; -import java.awt.event.KeyEvent; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; import java.util.HashSet; import java.util.ResourceBundle; import java.util.Set; -import javax.swing.Box; -import javax.swing.BoxLayout; -import javax.swing.ImageIcon; -import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; -import javax.swing.JPanel; -import javax.swing.JScrollPane; import javax.swing.JTable; -import javax.swing.ListSelectionModel; -import javax.swing.ScrollPaneConstants; import javax.swing.table.TableColumn; import javax.swing.table.TableRowSorter; import org.bouncycastle.asn1.ASN1ObjectIdentifier; -import org.kse.gui.CursorUtil; -import org.kse.gui.PlatformUtil; +import org.kse.gui.crypto.JAddEditRemovePanel; import org.kse.gui.error.DError; import org.kse.gui.oid.DObjectIdChooser; import org.kse.gui.table.ToolTipTable; import org.kse.utilities.oid.InvalidObjectIdException; import org.kse.utilities.oid.ObjectIdComparator; -import org.kse.utilities.os.OperatingSystem; /** * Component to edit a set of custom extended key usages. */ -public class JCustomExtendedKeyUsage extends JPanel { +public class JCustomExtendedKeyUsage extends JAddEditRemovePanel, ASN1ObjectIdentifier> { private static final long serialVersionUID = 1L; private static ResourceBundle res = ResourceBundle.getBundle("org/kse/gui/crypto/customextkeyusage/resources"); - private JPanel jpCustomExtKeyUsageButtons; - private JButton jbAdd; - private JButton jbEdit; - private JButton jbRemove; - private JScrollPane jspCustomExtKeyUsage; - private JTable jtCustomExtKeyUsages; - private String title; - private Set objectIds; - - private boolean enabled = true; /** * Construct a JCustomExtKeyUsage. @@ -84,71 +55,37 @@ public class JCustomExtendedKeyUsage extends JPanel { */ public JCustomExtendedKeyUsage(String title) { this.title = title; - initComponents(); } - private void initComponents() { - jbAdd = new JButton(new ImageIcon( - Toolkit.getDefaultToolkit().createImage(getClass().getResource("images/add_custom_eku.png")))); - jbAdd.setMargin(new Insets(2, 2, 0, 0)); - jbAdd.setToolTipText(res.getString("JCustomExtKeyUsage.jbAdd.tooltip")); - jbAdd.setMnemonic(res.getString("JCustomExtKeyUsage.jbAdd.mnemonic").charAt(0)); - - jbAdd.addActionListener(evt -> { - try { - CursorUtil.setCursorBusy(JCustomExtendedKeyUsage.this); - addPressed(); - } finally { - CursorUtil.setCursorFree(JCustomExtendedKeyUsage.this); - } - }); - - jbEdit = new JButton(new ImageIcon( - Toolkit.getDefaultToolkit().createImage(getClass().getResource("images/edit_custom_eku.png")))); - jbEdit.setMargin(new Insets(2, 2, 0, 0)); - jbEdit.setToolTipText(res.getString("JCustomExtKeyUsage.jbEdit.tooltip")); - jbEdit.setMnemonic(res.getString("JCustomExtKeyUsage.jbEdit.mnemonic").charAt(0)); - - jbEdit.setEnabled(false); - - jbEdit.addActionListener(evt -> { - try { - CursorUtil.setCursorBusy(JCustomExtendedKeyUsage.this); - editPressed(); - } finally { - CursorUtil.setCursorFree(JCustomExtendedKeyUsage.this); - } - }); + @Override + protected String getAddResource() { + return "images/add_custom_eku.png"; + } - jbRemove = new JButton(new ImageIcon( - Toolkit.getDefaultToolkit().createImage(getClass().getResource("images/remove_custom_eku.png")))); - jbRemove.setMargin(new Insets(2, 2, 0, 0)); - jbRemove.setToolTipText(res.getString("JCustomExtKeyUsage.jbRemove.tooltip")); - jbRemove.setMnemonic(res.getString("JCustomExtKeyUsage.jbRemove.mnemonic").charAt(0)); + @Override + protected String getEditResource() { + return "images/edit_custom_eku.png"; + } - jbRemove.setEnabled(false); + @Override + protected String getRemoveResource() { + return "images/remove_custom_eku.png"; + } - jbRemove.addActionListener(evt -> { - try { - CursorUtil.setCursorBusy(JCustomExtendedKeyUsage.this); - removePressed(); - } finally { - CursorUtil.setCursorFree(JCustomExtendedKeyUsage.this); - } - }); + @Override + protected String getBundleString(String suffix) { + return res.getString("JCustomExtKeyUsage." + suffix); + } - jpCustomExtKeyUsageButtons = new JPanel(); - jpCustomExtKeyUsageButtons.setLayout(new BoxLayout(jpCustomExtKeyUsageButtons, BoxLayout.Y_AXIS)); - jpCustomExtKeyUsageButtons.add(Box.createVerticalGlue()); - jpCustomExtKeyUsageButtons.add(jbAdd); - jpCustomExtKeyUsageButtons.add(Box.createVerticalStrut(3)); - jpCustomExtKeyUsageButtons.add(jbEdit); - jpCustomExtKeyUsageButtons.add(Box.createVerticalStrut(3)); - jpCustomExtKeyUsageButtons.add(jbRemove); - jpCustomExtKeyUsageButtons.add(Box.createVerticalGlue()); + @Override + protected Set newCollection() { + return new HashSet<>(); + } + @Override + protected JTable newTable() { CustomExtKeyUsageTableModel customExtKeyUsageTableModel = new CustomExtKeyUsageTableModel(); - jtCustomExtKeyUsages = new ToolTipTable(customExtKeyUsageTableModel); + JTable jtCustomExtKeyUsages = new ToolTipTable(customExtKeyUsageTableModel); TableRowSorter sorter = new TableRowSorter<>(customExtKeyUsageTableModel); sorter.setComparator(0, new ObjectIdComparator()); @@ -166,149 +103,26 @@ private void initComponents() { column.setCellRenderer(new CustomExtKeyUsageTableCellRend()); } - ListSelectionModel selectionModel = jtCustomExtKeyUsages.getSelectionModel(); - selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); - selectionModel.addListSelectionListener(evt -> { - if (!evt.getValueIsAdjusting()) { - updateButtonControls(); - } - }); - - jtCustomExtKeyUsages.addMouseListener(new MouseAdapter() { - @Override - public void mouseClicked(MouseEvent evt) { - maybeEditCustomExtKeyUsage(evt); - } - }); - - jtCustomExtKeyUsages.addKeyListener(new KeyAdapter() { - boolean deleteLastPressed = false; - - @Override - public void keyPressed(KeyEvent evt) { - // Record delete pressed on non-Macs - if (!OperatingSystem.isMacOs()) { - deleteLastPressed = evt.getKeyCode() == KeyEvent.VK_DELETE; - } - } - - @Override - public void keyReleased(KeyEvent evt) { - // Delete on non-Mac if delete was pressed and is now released - if (!OperatingSystem.isMacOs() && deleteLastPressed && evt.getKeyCode() == KeyEvent.VK_DELETE) { - try { - CursorUtil.setCursorBusy(JCustomExtendedKeyUsage.this); - deleteLastPressed = false; - removeSelectedCustomExtKeyUsage(); - } finally { - CursorUtil.setCursorFree(JCustomExtendedKeyUsage.this); - } - } - } - - @Override - public void keyTyped(KeyEvent evt) { - // Delete on Mac if backspace typed - if (OperatingSystem.isMacOs() && evt.getKeyChar() == 0x08) { - try { - CursorUtil.setCursorBusy(JCustomExtendedKeyUsage.this); - removeSelectedCustomExtKeyUsage(); - } finally { - CursorUtil.setCursorFree(JCustomExtendedKeyUsage.this); - } - } - } - }); - - jspCustomExtKeyUsage = PlatformUtil.createScrollPane(jtCustomExtKeyUsages, - ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, - ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); - jspCustomExtKeyUsage.getViewport().setBackground(jtCustomExtKeyUsages.getBackground()); - - this.setLayout(new BorderLayout(5, 5)); - setPreferredSize(new Dimension(250, 150)); - add(jspCustomExtKeyUsage, BorderLayout.CENTER); - add(jpCustomExtKeyUsageButtons, BorderLayout.EAST); - - populate(); - } - - /** - * Get custom extended key usages. - * - * @return Custom extended key usages - */ - public Set getCustomExtKeyUsages() { - return objectIds; + return jtCustomExtKeyUsages; } - /** - * Set custom extended key usages. - * - * @param customExtKeyUsages custom extended key usages - */ - public void setCustomExtKeyUsages(Set customExtKeyUsages) { - this.objectIds = customExtKeyUsages; - populate(); - } - - /** - * Sets whether or not the component is enabled. - * - * @param enabled True if this component should be enabled, false otherwise - */ @Override - public void setEnabled(boolean enabled) { - this.enabled = enabled; - - updateButtonControls(); - } - - /** - * Set component's tooltip text. - * - * @param toolTipText Tooltip text - */ - @Override - public void setToolTipText(String toolTipText) { - super.setToolTipText(toolTipText); - jspCustomExtKeyUsage.setToolTipText(toolTipText); - jtCustomExtKeyUsages.setToolTipText(toolTipText); - } - - private void populate() { - if (objectIds == null) { - objectIds = new HashSet<>(); - } - - reloadCustomExtKeyUsageTable(); - selectFirstCustomExtKeyUsageInTable(); - updateButtonControls(); - } - - private void addPressed() { + protected ASN1ObjectIdentifier getItem(ASN1ObjectIdentifier item) { Container container = getTopLevelAncestor(); try { DObjectIdChooser dObjectIdChooser = null; - if (container instanceof JDialog) { - dObjectIdChooser = new DObjectIdChooser((JDialog) container, title, null); + dObjectIdChooser = new DObjectIdChooser((JDialog) container, title, item); } else { - dObjectIdChooser = new DObjectIdChooser((JFrame) container, title, null); + dObjectIdChooser = new DObjectIdChooser((JFrame) container, title, item); } + dObjectIdChooser.setLocationRelativeTo(container); dObjectIdChooser.setVisible(true); - ASN1ObjectIdentifier newObjectId = dObjectIdChooser.getObjectId(); - - if (newObjectId == null) { - return; - } + return dObjectIdChooser.getObjectId(); - objectIds.add(newObjectId); - populate(); - selectCustomExtKeyUsageInTable(newObjectId); } catch (InvalidObjectIdException ex) { DError dError = null; @@ -320,135 +134,9 @@ private void addPressed() { dError.setLocationRelativeTo(container); dError.setVisible(true); - } - } - - private void removePressed() { - removeSelectedCustomExtKeyUsage(); - } - - private void removeSelectedCustomExtKeyUsage() { - int selectedRow = jtCustomExtKeyUsages.getSelectedRow(); - - if (selectedRow != -1) { - ASN1ObjectIdentifier extKeyUsageOid = (ASN1ObjectIdentifier) jtCustomExtKeyUsages.getValueAt(selectedRow, - 0); - - objectIds.remove(extKeyUsageOid); - - reloadCustomExtKeyUsageTable(); - selectFirstCustomExtKeyUsageInTable(); - updateButtonControls(); - } - } - - private void editPressed() { - editSelectedCustomExtKeyUsage(); - } - - private void maybeEditCustomExtKeyUsage(MouseEvent evt) { - if (evt.getClickCount() > 1) { - Point point = new Point(evt.getX(), evt.getY()); - int row = jtCustomExtKeyUsages.rowAtPoint(point); - - if (row != -1) { - try { - CursorUtil.setCursorBusy(JCustomExtendedKeyUsage.this); - jtCustomExtKeyUsages.setRowSelectionInterval(row, row); - editSelectedCustomExtKeyUsage(); - } finally { - CursorUtil.setCursorFree(JCustomExtendedKeyUsage.this); - } - } - } - } - - private void updateButtonControls() { - if (!enabled) { - jbAdd.setEnabled(false); - jbEdit.setEnabled(false); - jbRemove.setEnabled(false); - } else { - jbAdd.setEnabled(true); - - int selectedRow = jtCustomExtKeyUsages.getSelectedRow(); - if (selectedRow == -1) { - jbEdit.setEnabled(false); - jbRemove.setEnabled(false); - } else { - jbEdit.setEnabled(true); - jbRemove.setEnabled(true); - } + return null; } } - private void editSelectedCustomExtKeyUsage() { - int selectedRow = jtCustomExtKeyUsages.getSelectedRow(); - - if (selectedRow != -1) { - ASN1ObjectIdentifier objectId = (ASN1ObjectIdentifier) jtCustomExtKeyUsages.getValueAt(selectedRow, 0); - - Container container = getTopLevelAncestor(); - - try { - DObjectIdChooser dObjectIdChooser = null; - if (container instanceof JDialog) { - dObjectIdChooser = new DObjectIdChooser((JDialog) container, title, objectId); - } else { - dObjectIdChooser = new DObjectIdChooser((JFrame) container, title, objectId); - } - - dObjectIdChooser.setLocationRelativeTo(container); - dObjectIdChooser.setVisible(true); - - ASN1ObjectIdentifier newObjectId = dObjectIdChooser.getObjectId(); - - if (newObjectId == null) { - return; - } - - objectIds.remove(objectId); - objectIds.add(newObjectId); - - populate(); - selectCustomExtKeyUsageInTable(newObjectId); - } catch (InvalidObjectIdException ex) { - DError dError = null; - - if (container instanceof JDialog) { - dError = new DError((JDialog) container, ex); - } else { - dError = new DError((JFrame) container, ex); - } - - dError.setLocationRelativeTo(container); - dError.setVisible(true); - } - } - } - - private void selectCustomExtKeyUsageInTable(ASN1ObjectIdentifier objectId) { - for (int i = 0; i < jtCustomExtKeyUsages.getRowCount(); i++) { - if (objectId.equals(jtCustomExtKeyUsages.getValueAt(i, 0))) { - jtCustomExtKeyUsages.changeSelection(i, 0, false, false); - return; - } - } - } - - private void reloadCustomExtKeyUsageTable() { - getCustomExtKeyUsageTableModel().load(objectIds); - } - - private void selectFirstCustomExtKeyUsageInTable() { - if (getCustomExtKeyUsageTableModel().getRowCount() > 0) { - jtCustomExtKeyUsages.changeSelection(0, 0, false, false); - } - } - - private CustomExtKeyUsageTableModel getCustomExtKeyUsageTableModel() { - return (CustomExtKeyUsageTableModel) jtCustomExtKeyUsages.getModel(); - } - } diff --git a/kse/src/main/java/org/kse/gui/crypto/distributionpoints/DistributionPointsTableCellRend.java b/kse/src/main/java/org/kse/gui/crypto/distributionpoints/DistributionPointsTableCellRend.java index befa1d224..51336e609 100644 --- a/kse/src/main/java/org/kse/gui/crypto/distributionpoints/DistributionPointsTableCellRend.java +++ b/kse/src/main/java/org/kse/gui/crypto/distributionpoints/DistributionPointsTableCellRend.java @@ -50,6 +50,9 @@ public Component getTableCellRendererComponent(JTable jtDistributionPoints, Obje JLabel cell = (JLabel) super.getTableCellRendererComponent(jtDistributionPoints, value, isSelected, hasFocus, row, col); + // If the rendering of the distribution point changes such that + // distribution point content is used for the rendering then + // be sure to update the comparator in DistributionPointsTableModel. String distributionPointStr = "Distribution Point " + (row + 1); cell.setText(distributionPointStr); cell.setToolTipText(distributionPointStr); diff --git a/kse/src/main/java/org/kse/gui/crypto/distributionpoints/DistributionPointsTableModel.java b/kse/src/main/java/org/kse/gui/crypto/distributionpoints/DistributionPointsTableModel.java index ba312662e..2474db8ec 100644 --- a/kse/src/main/java/org/kse/gui/crypto/distributionpoints/DistributionPointsTableModel.java +++ b/kse/src/main/java/org/kse/gui/crypto/distributionpoints/DistributionPointsTableModel.java @@ -21,20 +21,19 @@ package org.kse.gui.crypto.distributionpoints; import java.util.ArrayList; -import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.ResourceBundle; -import org.bouncycastle.asn1.x509.CRLDistPoint; import org.bouncycastle.asn1.x509.DistributionPoint; import org.bouncycastle.asn1.x509.GeneralName; +import org.kse.gui.table.LoadableTableModel; import org.kse.gui.table.ToolTipTableModel; /** * The table model used to display general names. */ -public class DistributionPointsTableModel extends ToolTipTableModel { +public class DistributionPointsTableModel extends ToolTipTableModel implements LoadableTableModel { private static final long serialVersionUID = 4224864830348756671L; @@ -57,9 +56,9 @@ public DistributionPointsTableModel() { data = new ArrayList<>(); } - public void load(CRLDistPoint cRLDistPoint) { - DistributionPoint[] distributionPointArray = cRLDistPoint.getDistributionPoints(); - data = new ArrayList<>(Arrays.asList(distributionPointArray)); + @Override + public void load(List data) { + this.data = data; data.sort(new DistributionPointComparator()); fireTableDataChanged(); } @@ -130,38 +129,13 @@ public boolean isCellEditable(int row, int col) { return false; } - /** - * Add a row - */ - public void addRow(DistributionPoint distributionPoint) { - data.add(distributionPoint); - data.sort(new DistributionPointComparator()); - fireTableDataChanged(); - } - - /** - * Remove a row - * - * @param row Row number - */ - public void removeRow(int row) { - data.remove(row); - fireTableDataChanged(); - } - - /** - * Returns the table data - * - * @return List of general names as table data - */ - public List getData() { - return data; - } - static class DistributionPointComparator implements Comparator { @Override public int compare(DistributionPoint arg0, DistributionPoint arg1) { - return 0;//fix + // The DistributionPointsTableCellRend does not use the distribution + // point content for display. Sorting the list is irrelevant. The table + // cell renderer must be changed before enabling sorting. + return 0; } } } diff --git a/kse/src/main/java/org/kse/gui/crypto/distributionpoints/JDistributionPoints.java b/kse/src/main/java/org/kse/gui/crypto/distributionpoints/JDistributionPoints.java index f4659992e..f60a88005 100644 --- a/kse/src/main/java/org/kse/gui/crypto/distributionpoints/JDistributionPoints.java +++ b/kse/src/main/java/org/kse/gui/crypto/distributionpoints/JDistributionPoints.java @@ -20,56 +20,30 @@ package org.kse.gui.crypto.distributionpoints; -import java.awt.BorderLayout; import java.awt.Container; -import java.awt.Dimension; -import java.awt.Insets; -import java.awt.Point; -import java.awt.Toolkit; -import java.awt.event.KeyAdapter; -import java.awt.event.KeyEvent; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; +import java.util.ArrayList; +import java.util.List; import java.util.ResourceBundle; -import javax.swing.Box; -import javax.swing.BoxLayout; -import javax.swing.ImageIcon; -import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; -import javax.swing.JPanel; -import javax.swing.JScrollPane; import javax.swing.JTable; -import javax.swing.ListSelectionModel; -import javax.swing.ScrollPaneConstants; import javax.swing.table.TableColumn; import javax.swing.table.TableRowSorter; -import org.bouncycastle.asn1.x509.CRLDistPoint; import org.bouncycastle.asn1.x509.DistributionPoint; -import org.kse.gui.CursorUtil; -import org.kse.gui.PlatformUtil; +import org.kse.gui.crypto.JAddEditRemovePanel; import org.kse.gui.table.ToolTipTable; -import org.kse.utilities.os.OperatingSystem; /** * Component to show the list distribution points. */ -public class JDistributionPoints extends JPanel { +public class JDistributionPoints extends JAddEditRemovePanel, DistributionPoint> { private static final long serialVersionUID = 1L; private static ResourceBundle res = ResourceBundle.getBundle("org/kse/gui/crypto/distributionpoints/resources"); - private JPanel jpDistributionPointsButtons; - private JButton jbAdd; - private JButton jbEdit; - private JButton jbRemove; - private JScrollPane jspDistributionPoints; - private JTable jtDistributionPoints; - private String title; - private boolean enabled = true; /** * Creates a new JDistributionPoints @@ -78,71 +52,37 @@ public class JDistributionPoints extends JPanel { */ public JDistributionPoints(String title) { this.title = title; - initComponents(); } - private void initComponents() { - jbAdd = new JButton(new ImageIcon( - Toolkit.getDefaultToolkit().createImage(getClass().getResource("images/add_distribution_nms.png")))); - jbAdd.setMargin(new Insets(2, 2, 0, 0)); - jbAdd.setToolTipText(res.getString("JDistributionPoints.jbAdd.tooltip")); - jbAdd.setMnemonic(res.getString("JDistributionPoints.jbAdd.mnemonic").charAt(0)); - - jbAdd.addActionListener(evt -> { - try { - CursorUtil.setCursorBusy(JDistributionPoints.this); - addPressed(); - } finally { - CursorUtil.setCursorFree(JDistributionPoints.this); - } - }); - - jbEdit = new JButton(new ImageIcon( - Toolkit.getDefaultToolkit().createImage(getClass().getResource("images/edit_distribution_nms.png")))); - jbEdit.setMargin(new Insets(2, 2, 0, 0)); - jbEdit.setToolTipText(res.getString("JDistributionPoints.jbEdit.tooltip")); - jbEdit.setMnemonic(res.getString("JDistributionPoints.jbEdit.mnemonic").charAt(0)); - - jbEdit.setEnabled(false); - - jbEdit.addActionListener(evt -> { - try { - CursorUtil.setCursorBusy(JDistributionPoints.this); - editPressed(); - } finally { - CursorUtil.setCursorFree(JDistributionPoints.this); - } - }); + @Override + protected String getAddResource() { + return "images/add_distribution_nms.png"; + } - jbRemove = new JButton(new ImageIcon( - Toolkit.getDefaultToolkit().createImage(getClass().getResource("images/remove_distribution_nms.png")))); - jbRemove.setMargin(new Insets(2, 2, 0, 0)); - jbRemove.setToolTipText(res.getString("JDistributionPoints.jbRemove.tooltip")); - jbRemove.setMnemonic(res.getString("JDistributionPoints.jbRemove.mnemonic").charAt(0)); + @Override + protected String getEditResource() { + return "images/edit_distribution_nms.png"; + } - jbRemove.setEnabled(false); + @Override + protected String getRemoveResource() { + return "images/remove_distribution_nms.png"; + } - jbRemove.addActionListener(evt -> { - try { - CursorUtil.setCursorBusy(JDistributionPoints.this); - removePressed(); - } finally { - CursorUtil.setCursorFree(JDistributionPoints.this); - } - }); + @Override + protected String getBundleString(String suffix) { + return res.getString("JDistributionPoints." + suffix); + } - jpDistributionPointsButtons = new JPanel(); - jpDistributionPointsButtons.setLayout(new BoxLayout(jpDistributionPointsButtons, BoxLayout.Y_AXIS)); - jpDistributionPointsButtons.add(Box.createVerticalGlue()); - jpDistributionPointsButtons.add(jbAdd); - jpDistributionPointsButtons.add(Box.createVerticalStrut(3)); - jpDistributionPointsButtons.add(jbEdit); - jpDistributionPointsButtons.add(Box.createVerticalStrut(3)); - jpDistributionPointsButtons.add(jbRemove); - jpDistributionPointsButtons.add(Box.createVerticalGlue()); + @Override + protected List newCollection() { + return new ArrayList<>(); + } + @Override + protected JTable newTable() { DistributionPointsTableModel distributionPointsTableModel = new DistributionPointsTableModel(); - jtDistributionPoints = new ToolTipTable(distributionPointsTableModel); + JTable jtDistributionPoints = new ToolTipTable(distributionPointsTableModel); TableRowSorter sorter = new TableRowSorter<>(distributionPointsTableModel); sorter.setComparator(0, new DistributionPointsTableModel.DistributionPointComparator()); @@ -160,249 +100,25 @@ private void initComponents() { column.setCellRenderer(new DistributionPointsTableCellRend()); } - ListSelectionModel selectionModel = jtDistributionPoints.getSelectionModel(); - selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); - selectionModel.addListSelectionListener(evt -> { - if (!evt.getValueIsAdjusting()) { - updateButtonControls(); - } - }); - - jtDistributionPoints.addMouseListener(new MouseAdapter() { - @Override - public void mouseClicked(MouseEvent evt) { - maybeEditGeneralName(evt); - } - }); - - jtDistributionPoints.addKeyListener(new KeyAdapter() { - boolean deleteLastPressed = false; - - @Override - public void keyPressed(KeyEvent evt) { - // Record delete pressed on non-Macs - if (!OperatingSystem.isMacOs()) { - deleteLastPressed = evt.getKeyCode() == KeyEvent.VK_DELETE; - } - } - - @Override - public void keyReleased(KeyEvent evt) { - // Delete on non-Mac if delete was pressed and is now released - if (!OperatingSystem.isMacOs() && deleteLastPressed && evt.getKeyCode() == KeyEvent.VK_DELETE) { - try { - CursorUtil.setCursorBusy(JDistributionPoints.this); - deleteLastPressed = false; - removeSelectedDistributionPoint(); - } finally { - CursorUtil.setCursorFree(JDistributionPoints.this); - } - } - } - - @Override - public void keyTyped(KeyEvent evt) { - // Delete on Mac if backspace typed - if (OperatingSystem.isMacOs() && evt.getKeyChar() == 0x08) { - try { - CursorUtil.setCursorBusy(JDistributionPoints.this); - removeSelectedDistributionPoint(); - } finally { - CursorUtil.setCursorFree(JDistributionPoints.this); - } - } - } - }); - - jspDistributionPoints = PlatformUtil.createScrollPane(jtDistributionPoints, - ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, - ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); - jspDistributionPoints.getViewport().setBackground(jtDistributionPoints.getBackground()); - - this.setLayout(new BorderLayout(5, 5)); - setPreferredSize(new Dimension(250, 150)); - add(jspDistributionPoints, BorderLayout.CENTER); - add(jpDistributionPointsButtons, BorderLayout.EAST); - - selectFirstGeneralNameInTable(); - updateButtonControls(); - } - - /** - * @return A CRLDistPoint populated from the table. - */ - public CRLDistPoint getCRLDistPoint() { - return new CRLDistPoint(getDistributionPointsTableModel().getData().toArray(DistributionPoint[]::new)); - } - - /** - * Populates the table with the distribution points in cRLDistPoint. - * - * @param cRLDistPoint The distribution points to load. - */ - public void setCRLDistPoint(CRLDistPoint cRLDistPoint) { - getDistributionPointsTableModel().load(cRLDistPoint); - } - - /** - * Sets whether or not the component is enabled. - * - * @param enabled True if this component should be enabled, false otherwise - */ - @Override - public void setEnabled(boolean enabled) { - this.enabled = enabled; - - updateButtonControls(); + return jtDistributionPoints; } - /** - * Set component's tooltip text. - * - * @param toolTipText Tooltip text - */ @Override - public void setToolTipText(String toolTipText) { - super.setToolTipText(toolTipText); - jspDistributionPoints.setToolTipText(toolTipText); - jtDistributionPoints.setToolTipText(toolTipText); - } - - private void addPressed() { + protected DistributionPoint getItem(DistributionPoint item) { Container container = getTopLevelAncestor(); DDistributionPointsChooser distributionPointsChooser = null; if (container instanceof JDialog) { - distributionPointsChooser = new DDistributionPointsChooser((JDialog) container, title, null); + distributionPointsChooser = new DDistributionPointsChooser((JDialog) container, title, item); } else { - distributionPointsChooser = new DDistributionPointsChooser((JFrame) container, title, null); + distributionPointsChooser = new DDistributionPointsChooser((JFrame) container, title, item); } distributionPointsChooser.setLocationRelativeTo(container); distributionPointsChooser.setVisible(true); - DistributionPoint newDistributionPoint = distributionPointsChooser.getDistributionPoint(); - - if (newDistributionPoint == null) { - return; - } - - getDistributionPointsTableModel().addRow(newDistributionPoint); - - selectDistributionPointInTable(newDistributionPoint); - updateButtonControls(); - } - - private void removePressed() { - removeSelectedDistributionPoint(); - } - - private void removeSelectedDistributionPoint() { - int selectedRow = jtDistributionPoints.getSelectedRow(); - - if (selectedRow != -1) { - ((DistributionPointsTableModel) jtDistributionPoints.getModel()).removeRow(selectedRow); - - selectFirstGeneralNameInTable(); - updateButtonControls(); - } - } - - private void editPressed() { - editSelectedGeneralName(); - } - - private void maybeEditGeneralName(MouseEvent evt) { - if (evt.getClickCount() > 1) { - Point point = new Point(evt.getX(), evt.getY()); - int row = jtDistributionPoints.rowAtPoint(point); - - if (row != -1) { - try { - CursorUtil.setCursorBusy(JDistributionPoints.this); - jtDistributionPoints.setRowSelectionInterval(row, row); - editSelectedGeneralName(); - } finally { - CursorUtil.setCursorFree(JDistributionPoints.this); - } - } - } - } - - private void updateButtonControls() { - if (!enabled) { - jbAdd.setEnabled(false); - jbEdit.setEnabled(false); - jbRemove.setEnabled(false); - } else { - jbAdd.setEnabled(true); - - int selectedRow = jtDistributionPoints.getSelectedRow(); - - if (selectedRow == -1) { - jbEdit.setEnabled(false); - jbRemove.setEnabled(false); - } else { - jbEdit.setEnabled(true); - jbRemove.setEnabled(true); - } - } - } - - private void editSelectedGeneralName() { - int selectedRow = jtDistributionPoints.getSelectedRow(); - - if (selectedRow != -1) { - - DistributionPoint distributionPoint = (DistributionPoint) jtDistributionPoints.getValueAt(selectedRow, 0); - - Container container = getTopLevelAncestor(); - - DDistributionPointsChooser distributionPointsChooser = null; - - if (container instanceof JDialog) { - distributionPointsChooser = new DDistributionPointsChooser((JDialog) container, title, - distributionPoint); - } else { - distributionPointsChooser = new DDistributionPointsChooser((JFrame) container, title, - distributionPoint); - } - - distributionPointsChooser.setLocationRelativeTo(container); - distributionPointsChooser.setVisible(true); - - DistributionPoint newDistributionPoint = distributionPointsChooser.getDistributionPoint(); - - if (newDistributionPoint == null) { - return; - } - - getDistributionPointsTableModel().removeRow(selectedRow); - getDistributionPointsTableModel().addRow(newDistributionPoint); - - selectDistributionPointInTable(newDistributionPoint); - updateButtonControls(); - } - } - - private void selectDistributionPointInTable(DistributionPoint newDistributionPoint) { - for (int i = 0; i < jtDistributionPoints.getRowCount(); i++) { - if (newDistributionPoint.equals(jtDistributionPoints.getValueAt(i, 0))) { - jtDistributionPoints.changeSelection(i, 0, false, false); - return; - } - } - } - - private void selectFirstGeneralNameInTable() { - if (getDistributionPointsTableModel().getRowCount() > 0) { - jtDistributionPoints.changeSelection(0, 0, false, false); - } - } - - private DistributionPointsTableModel getDistributionPointsTableModel() { - return (DistributionPointsTableModel) jtDistributionPoints.getModel(); + return distributionPointsChooser.getDistributionPoint(); } } diff --git a/kse/src/main/java/org/kse/gui/crypto/generalname/GeneralNamesTableModel.java b/kse/src/main/java/org/kse/gui/crypto/generalname/GeneralNamesTableModel.java index 49878a478..68c9d812a 100644 --- a/kse/src/main/java/org/kse/gui/crypto/generalname/GeneralNamesTableModel.java +++ b/kse/src/main/java/org/kse/gui/crypto/generalname/GeneralNamesTableModel.java @@ -20,20 +20,19 @@ package org.kse.gui.crypto.generalname; import java.util.ArrayList; -import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.ResourceBundle; import org.bouncycastle.asn1.x509.GeneralName; -import org.bouncycastle.asn1.x509.GeneralNames; import org.kse.crypto.x509.GeneralNameUtil; +import org.kse.gui.table.LoadableTableModel; import org.kse.gui.table.ToolTipTableModel; /** * The table model used to display general names. */ -public class GeneralNamesTableModel extends ToolTipTableModel { +public class GeneralNamesTableModel extends ToolTipTableModel implements LoadableTableModel { private static final long serialVersionUID = 4224864830348756671L; @@ -61,10 +60,11 @@ public GeneralNamesTableModel() { * * @param generalNames The general names */ - public void load(GeneralNames generalNames) { - GeneralName[] generalNamesArray = generalNames.getNames(); + @Override + public void load(List generalNames) { - data = new ArrayList<>(Arrays.asList(generalNamesArray)); + this.data = generalNames; + data.sort(new GeneralNameComparator()); fireTableDataChanged(); } @@ -135,49 +135,6 @@ public boolean isCellEditable(int row, int col) { return false; } - /** - * Add a row - * - * @param generalName General name - */ - public void addRow(GeneralName generalName) { - data.add(generalName); - fireTableDataChanged(); - } - - /** - * Remove a row - * - * @param row Row number - */ - public void removeRow(int row) { - data.remove(row); - fireTableDataChanged(); - } - - /** - * Sets the value in the cell at columnIndex and - * rowIndex to value. - * - * @param value the new value - * @param rowIndex the row whose value is to be changed - * @param columnIndex the column whose value is to be changed - */ - @Override - public void setValueAt(Object value, int rowIndex, int columnIndex) { - data.set(rowIndex, (GeneralName) value); - fireTableDataChanged(); - } - - /** - * Returns the table data - * - * @return List of general names as table data - */ - public List getData() { - return data; - } - static class GeneralNameComparator implements Comparator { @Override public int compare(GeneralName name1, GeneralName name2) { diff --git a/kse/src/main/java/org/kse/gui/crypto/generalname/JGeneralName.java b/kse/src/main/java/org/kse/gui/crypto/generalname/JGeneralName.java index 0ab17b8c1..0287f8f8a 100644 --- a/kse/src/main/java/org/kse/gui/crypto/generalname/JGeneralName.java +++ b/kse/src/main/java/org/kse/gui/crypto/generalname/JGeneralName.java @@ -61,6 +61,7 @@ public JGeneralName(String title) { initComponents(); } + // TODO JW - miglayout private void initComponents() { jtfGeneralName = new JTextField(40); jtfGeneralName.setEditable(false); diff --git a/kse/src/main/java/org/kse/gui/crypto/generalname/JGeneralNames.java b/kse/src/main/java/org/kse/gui/crypto/generalname/JGeneralNames.java index 551bb6d6f..44e10918f 100644 --- a/kse/src/main/java/org/kse/gui/crypto/generalname/JGeneralNames.java +++ b/kse/src/main/java/org/kse/gui/crypto/generalname/JGeneralNames.java @@ -19,57 +19,32 @@ */ package org.kse.gui.crypto.generalname; -import java.awt.BorderLayout; import java.awt.Container; -import java.awt.Dimension; -import java.awt.Insets; -import java.awt.Point; -import java.awt.Toolkit; -import java.awt.Window; -import java.awt.event.KeyAdapter; -import java.awt.event.KeyEvent; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; import java.util.ResourceBundle; -import javax.swing.Box; -import javax.swing.BoxLayout; -import javax.swing.ImageIcon; -import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; -import javax.swing.JPanel; -import javax.swing.JScrollPane; import javax.swing.JTable; -import javax.swing.ListSelectionModel; -import javax.swing.ScrollPaneConstants; import javax.swing.table.TableColumn; import javax.swing.table.TableRowSorter; import org.bouncycastle.asn1.x509.GeneralName; import org.bouncycastle.asn1.x509.GeneralNames; -import org.kse.gui.CursorUtil; -import org.kse.gui.PlatformUtil; +import org.kse.gui.crypto.JAddEditRemovePanel; import org.kse.gui.table.ToolTipTable; -import org.kse.utilities.os.OperatingSystem; /** * Component to edit a set of general names. */ -public class JGeneralNames extends JPanel { +public class JGeneralNames extends JAddEditRemovePanel, GeneralName> { private static final long serialVersionUID = 459512931464920941L; private static ResourceBundle res = ResourceBundle.getBundle("org/kse/gui/crypto/generalname/resources"); - private JPanel jpGeneralNameButtons; - private JButton jbAdd; - private JButton jbEdit; - private JButton jbRemove; - private JScrollPane jspGeneralNames; - private JTable jtGeneralNames; - private String title; - private boolean enabled = true; /** * Construct a JGeneralNames. @@ -78,71 +53,37 @@ public class JGeneralNames extends JPanel { */ public JGeneralNames(String title) { this.title = title; - initComponents(); } - private void initComponents() { - jbAdd = new JButton(new ImageIcon( - Toolkit.getDefaultToolkit().createImage(getClass().getResource("images/add_general_nms.png")))); - jbAdd.setMargin(new Insets(2, 2, 0, 0)); - jbAdd.setToolTipText(res.getString("JGeneralNames.jbAdd.tooltip")); - jbAdd.setMnemonic(res.getString("JGeneralNames.jbAdd.mnemonic").charAt(0)); - - jbAdd.addActionListener(evt -> { - try { - CursorUtil.setCursorBusy(JGeneralNames.this); - addPressed(); - } finally { - CursorUtil.setCursorFree(JGeneralNames.this); - } - }); - - jbEdit = new JButton(new ImageIcon( - Toolkit.getDefaultToolkit().createImage(getClass().getResource("images/edit_general_nms.png")))); - jbEdit.setMargin(new Insets(2, 2, 0, 0)); - jbEdit.setToolTipText(res.getString("JGeneralNames.jbEdit.tooltip")); - jbEdit.setMnemonic(res.getString("JGeneralNames.jbEdit.mnemonic").charAt(0)); - - jbEdit.setEnabled(false); - - jbEdit.addActionListener(evt -> { - try { - CursorUtil.setCursorBusy(JGeneralNames.this); - editPressed(); - } finally { - CursorUtil.setCursorFree(JGeneralNames.this); - } - }); + @Override + protected String getAddResource() { + return "images/add_general_nms.png"; + } - jbRemove = new JButton(new ImageIcon( - Toolkit.getDefaultToolkit().createImage(getClass().getResource("images/remove_general_nms.png")))); - jbRemove.setMargin(new Insets(2, 2, 0, 0)); - jbRemove.setToolTipText(res.getString("JGeneralNames.jbRemove.tooltip")); - jbRemove.setMnemonic(res.getString("JGeneralNames.jbRemove.mnemonic").charAt(0)); + @Override + protected String getEditResource() { + return "images/edit_general_nms.png"; + } - jbRemove.setEnabled(false); + @Override + protected String getRemoveResource() { + return "images/remove_general_nms.png"; + } - jbRemove.addActionListener(evt -> { - try { - CursorUtil.setCursorBusy(JGeneralNames.this); - removePressed(); - } finally { - CursorUtil.setCursorFree(JGeneralNames.this); - } - }); + @Override + protected String getBundleString(String suffix) { + return res.getString("JGeneralNames." + suffix); + } - jpGeneralNameButtons = new JPanel(); - jpGeneralNameButtons.setLayout(new BoxLayout(jpGeneralNameButtons, BoxLayout.Y_AXIS)); - jpGeneralNameButtons.add(Box.createVerticalGlue()); - jpGeneralNameButtons.add(jbAdd); - jpGeneralNameButtons.add(Box.createVerticalStrut(3)); - jpGeneralNameButtons.add(jbEdit); - jpGeneralNameButtons.add(Box.createVerticalStrut(3)); - jpGeneralNameButtons.add(jbRemove); - jpGeneralNameButtons.add(Box.createVerticalGlue()); + @Override + protected List newCollection() { + return new ArrayList<>(); + } + @Override + protected JTable newTable() { GeneralNamesTableModel generalNamesTableModel = new GeneralNamesTableModel(); - jtGeneralNames = new ToolTipTable(generalNamesTableModel); + JTable jtGeneralNames = new ToolTipTable(generalNamesTableModel); TableRowSorter sorter = new TableRowSorter<>(generalNamesTableModel); sorter.setComparator(0, new GeneralNamesTableModel.GeneralNameComparator()); @@ -160,72 +101,24 @@ private void initComponents() { column.setCellRenderer(new GeneralNamesTableCellRend()); } - ListSelectionModel selectionModel = jtGeneralNames.getSelectionModel(); - selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); - selectionModel.addListSelectionListener(evt -> { - if (!evt.getValueIsAdjusting()) { - updateButtonControls(); - } - }); - - jtGeneralNames.addMouseListener(new MouseAdapter() { - @Override - public void mouseClicked(MouseEvent evt) { - maybeEditGeneralName(evt); - } - }); - - jtGeneralNames.addKeyListener(new KeyAdapter() { - boolean deleteLastPressed = false; - - @Override - public void keyPressed(KeyEvent evt) { - // Record delete pressed on non-Macs - if (!OperatingSystem.isMacOs()) { - deleteLastPressed = evt.getKeyCode() == KeyEvent.VK_DELETE; - } - } - - @Override - public void keyReleased(KeyEvent evt) { - // Delete on non-Mac if delete was pressed and is now released - if (!OperatingSystem.isMacOs() && deleteLastPressed && evt.getKeyCode() == KeyEvent.VK_DELETE) { - try { - CursorUtil.setCursorBusy(JGeneralNames.this); - deleteLastPressed = false; - removeSelectedGeneralName(); - } finally { - CursorUtil.setCursorFree(JGeneralNames.this); - } - } - } + return jtGeneralNames; + } - @Override - public void keyTyped(KeyEvent evt) { - // Delete on Mac if backspace typed - if (OperatingSystem.isMacOs() && evt.getKeyChar() == 0x08) { - try { - CursorUtil.setCursorBusy(JGeneralNames.this); - removeSelectedGeneralName(); - } finally { - CursorUtil.setCursorFree(JGeneralNames.this); - } - } - } - }); + @Override + protected GeneralName getItem(GeneralName item) { + Container container = getTopLevelAncestor(); - jspGeneralNames = PlatformUtil.createScrollPane(jtGeneralNames, - ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, - ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); - jspGeneralNames.getViewport().setBackground(jtGeneralNames.getBackground()); + DGeneralNameChooser dGeneralNameChooser = null; - this.setLayout(new BorderLayout(5, 5)); - setPreferredSize(new Dimension(250, 150)); - add(jspGeneralNames, BorderLayout.CENTER); - add(jpGeneralNameButtons, BorderLayout.EAST); + if (container instanceof JDialog) { + dGeneralNameChooser = new DGeneralNameChooser((JDialog) container, title, item); + } else { + dGeneralNameChooser = new DGeneralNameChooser((JFrame) container, title, item); + } + dGeneralNameChooser.setLocationRelativeTo(container); + dGeneralNameChooser.setVisible(true); - selectFirstGeneralNameInTable(); - updateButtonControls(); + return dGeneralNameChooser.getGeneralName(); } /** @@ -234,7 +127,7 @@ public void keyTyped(KeyEvent evt) { * @return General names */ public GeneralNames getGeneralNames() { - return new GeneralNames(getGeneralNamesTableModel().getData().toArray(GeneralName[]::new)); + return new GeneralNames(getItems().toArray(GeneralName[]::new)); } /** @@ -243,154 +136,6 @@ public GeneralNames getGeneralNames() { * @param generalNames General names */ public void setGeneralNames(GeneralNames generalNames) { - getGeneralNamesTableModel().load(generalNames); - } - - /** - * Sets whether or not the component is enabled. - * - * @param enabled True if this component should be enabled, false otherwise - */ - @Override - public void setEnabled(boolean enabled) { - this.enabled = enabled; - - updateButtonControls(); - } - - /** - * Set component's tooltip text. - * - * @param toolTipText Tooltip text - */ - @Override - public void setToolTipText(String toolTipText) { - super.setToolTipText(toolTipText); - jspGeneralNames.setToolTipText(toolTipText); - jtGeneralNames.setToolTipText(toolTipText); - } - - private void addPressed() { - Container container = getTopLevelAncestor(); - - DGeneralNameChooser dGeneralNameChooser = null; - - if (container instanceof JDialog) { - dGeneralNameChooser = new DGeneralNameChooser((JDialog) container, title, null); - } else { - dGeneralNameChooser = new DGeneralNameChooser((JFrame) container, title, null); - } - dGeneralNameChooser.setLocationRelativeTo(container); - dGeneralNameChooser.setVisible(true); - - GeneralName newGeneralName = dGeneralNameChooser.getGeneralName(); - - if (newGeneralName == null) { - return; - } - - getGeneralNamesTableModel().addRow(newGeneralName); - - selectGeneralNameInTable(newGeneralName); - updateButtonControls(); - } - - private void removePressed() { - removeSelectedGeneralName(); - } - - private void removeSelectedGeneralName() { - int selectedRow = jtGeneralNames.getSelectedRow(); - - if (selectedRow != -1) { - ((GeneralNamesTableModel) jtGeneralNames.getModel()).removeRow(selectedRow); - - selectFirstGeneralNameInTable(); - updateButtonControls(); - } - } - - private void editPressed() { - editSelectedGeneralName(); - } - - private void maybeEditGeneralName(MouseEvent evt) { - if (evt.getClickCount() > 1) { - Point point = new Point(evt.getX(), evt.getY()); - int row = jtGeneralNames.rowAtPoint(point); - - if (row != -1) { - try { - CursorUtil.setCursorBusy(JGeneralNames.this); - jtGeneralNames.setRowSelectionInterval(row, row); - editSelectedGeneralName(); - } finally { - CursorUtil.setCursorFree(JGeneralNames.this); - } - } - } - } - - private void updateButtonControls() { - if (!enabled) { - jbAdd.setEnabled(false); - jbEdit.setEnabled(false); - jbRemove.setEnabled(false); - } else { - jbAdd.setEnabled(true); - - int selectedRow = jtGeneralNames.getSelectedRow(); - - if (selectedRow == -1) { - jbEdit.setEnabled(false); - jbRemove.setEnabled(false); - } else { - jbEdit.setEnabled(true); - jbRemove.setEnabled(true); - } - } - } - - private void editSelectedGeneralName() { - int selectedRow = jtGeneralNames.getSelectedRow(); - - if (selectedRow != -1) { - GeneralName generalName = (GeneralName) jtGeneralNames.getValueAt(selectedRow, 0); - - Container container = getTopLevelAncestor(); - - DGeneralNameChooser dGeneralNameChooser = new DGeneralNameChooser((Window) container, title, generalName); - dGeneralNameChooser.setLocationRelativeTo(container); - dGeneralNameChooser.setVisible(true); - - GeneralName newGeneralName = dGeneralNameChooser.getGeneralName(); - - if (newGeneralName == null) { - return; - } - - getGeneralNamesTableModel().setValueAt(newGeneralName, selectedRow, 0); - selectGeneralNameInTable(newGeneralName); - updateButtonControls(); - } - } - - private void selectGeneralNameInTable(GeneralName generalName) { - for (int i = 0; i < jtGeneralNames.getRowCount(); i++) { - if (generalName.equals(jtGeneralNames.getValueAt(i, 0))) { - jtGeneralNames.changeSelection(i, 0, false, false); - return; - } - } - } - - private void selectFirstGeneralNameInTable() { - if (getGeneralNamesTableModel().getRowCount() > 0) { - jtGeneralNames.changeSelection(0, 0, false, false); - } - } - - private GeneralNamesTableModel getGeneralNamesTableModel() { - return (GeneralNamesTableModel) jtGeneralNames.getModel(); + setItems(new ArrayList<>(Arrays.asList(generalNames.getNames()))); } } diff --git a/kse/src/main/java/org/kse/gui/crypto/generalsubtree/GeneralSubtreesTableModel.java b/kse/src/main/java/org/kse/gui/crypto/generalsubtree/GeneralSubtreesTableModel.java index bd0de4079..ba3924a4f 100644 --- a/kse/src/main/java/org/kse/gui/crypto/generalsubtree/GeneralSubtreesTableModel.java +++ b/kse/src/main/java/org/kse/gui/crypto/generalsubtree/GeneralSubtreesTableModel.java @@ -20,19 +20,20 @@ package org.kse.gui.crypto.generalsubtree; import java.math.BigInteger; +import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.ResourceBundle; import org.bouncycastle.asn1.x509.GeneralSubtree; import org.kse.crypto.x509.GeneralNameUtil; -import org.kse.crypto.x509.GeneralSubtrees; +import org.kse.gui.table.LoadableTableModel; import org.kse.gui.table.ToolTipTableModel; /** * The table model used to display general subtrees. */ -public class GeneralSubtreesTableModel extends ToolTipTableModel { +public class GeneralSubtreesTableModel extends ToolTipTableModel implements LoadableTableModel { private static final long serialVersionUID = 1L; private static ResourceBundle res = ResourceBundle.getBundle("org/kse/gui/crypto/generalsubtree/resources"); @@ -44,7 +45,7 @@ public class GeneralSubtreesTableModel extends ToolTipTableModel { }; private String[] columnNames; - private Object[][] data; + private List data; /** * Construct a new GeneralSubtreesTableModel. @@ -56,7 +57,7 @@ public GeneralSubtreesTableModel() { columnNames[1] = res.getString("GeneralSubtreesTableModel.MinimumColumn"); columnNames[2] = res.getString("GeneralSubtreesTableModel.MaximumColumn"); - data = new Object[0][0]; + data = new ArrayList<>(); } /** @@ -64,19 +65,10 @@ public GeneralSubtreesTableModel() { * * @param generalSubtrees The general subtrees */ - public void load(GeneralSubtrees generalSubtrees) { - List generalSubtreesList = generalSubtrees.getGeneralSubtrees(); - generalSubtreesList.sort(new GeneralSubtreeBaseComparator()); - - data = new Object[generalSubtreesList.size()][3]; - - int i = 0; - for (GeneralSubtree generalSubtree : generalSubtreesList) { - data[i][0] = generalSubtree; - data[i][1] = generalSubtree; - data[i][2] = generalSubtree; - i++; - } + @Override + public void load(List generalSubtrees) { + data = generalSubtrees; + data.sort(new GeneralSubtreeBaseComparator()); fireTableDataChanged(); } @@ -98,7 +90,7 @@ public int getColumnCount() { */ @Override public int getRowCount() { - return data.length; + return data.size(); } /** @@ -121,7 +113,7 @@ public String getColumnName(int col) { */ @Override public Object getValueAt(int row, int col) { - return data[row][col]; + return data.get(row); } /** diff --git a/kse/src/main/java/org/kse/gui/crypto/generalsubtree/JGeneralSubtrees.java b/kse/src/main/java/org/kse/gui/crypto/generalsubtree/JGeneralSubtrees.java index 853cb56d8..16c32d222 100644 --- a/kse/src/main/java/org/kse/gui/crypto/generalsubtree/JGeneralSubtrees.java +++ b/kse/src/main/java/org/kse/gui/crypto/generalsubtree/JGeneralSubtrees.java @@ -19,58 +19,31 @@ */ package org.kse.gui.crypto.generalsubtree; -import java.awt.BorderLayout; import java.awt.Container; -import java.awt.Dimension; -import java.awt.Insets; -import java.awt.Point; -import java.awt.Toolkit; -import java.awt.event.KeyAdapter; -import java.awt.event.KeyEvent; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; import java.util.ArrayList; +import java.util.List; import java.util.ResourceBundle; -import javax.swing.Box; -import javax.swing.BoxLayout; -import javax.swing.ImageIcon; -import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; -import javax.swing.JPanel; -import javax.swing.JScrollPane; import javax.swing.JTable; -import javax.swing.ListSelectionModel; -import javax.swing.ScrollPaneConstants; import javax.swing.table.TableColumn; import javax.swing.table.TableRowSorter; import org.bouncycastle.asn1.x509.GeneralSubtree; import org.kse.crypto.x509.GeneralSubtrees; -import org.kse.gui.CursorUtil; -import org.kse.gui.PlatformUtil; +import org.kse.gui.crypto.JAddEditRemovePanel; import org.kse.gui.table.ToolTipTable; -import org.kse.utilities.os.OperatingSystem; /** * Component to edit a set of general subtrees. */ -public class JGeneralSubtrees extends JPanel { +public class JGeneralSubtrees extends JAddEditRemovePanel, GeneralSubtree> { private static final long serialVersionUID = 1L; private static ResourceBundle res = ResourceBundle.getBundle("org/kse/gui/crypto/generalsubtree/resources"); - private JPanel jpGeneralSubtreeButtons; - private JButton jbAdd; - private JButton jbEdit; - private JButton jbRemove; - private JScrollPane jspGeneralSubtrees; - private JTable jtGeneralSubtrees; - private String title; - private GeneralSubtrees generalSubtrees; - private boolean enabled = true; /** * Construct a JGeneralSubtrees. @@ -79,71 +52,37 @@ public class JGeneralSubtrees extends JPanel { */ public JGeneralSubtrees(String title) { this.title = title; - initComponents(); } - private void initComponents() { - jbAdd = new JButton(new ImageIcon( - Toolkit.getDefaultToolkit().createImage(getClass().getResource("images/add_general_subtree.png")))); - jbAdd.setMargin(new Insets(2, 2, 0, 0)); - jbAdd.setToolTipText(res.getString("JGeneralSubtrees.jbAdd.tooltip")); - jbAdd.setMnemonic(res.getString("JGeneralSubtrees.jbAdd.mnemonic").charAt(0)); - - jbAdd.addActionListener(evt -> { - try { - CursorUtil.setCursorBusy(JGeneralSubtrees.this); - addPressed(); - } finally { - CursorUtil.setCursorFree(JGeneralSubtrees.this); - } - }); - - jbEdit = new JButton(new ImageIcon( - Toolkit.getDefaultToolkit().createImage(getClass().getResource("images/edit_general_subtree.png")))); - jbEdit.setMargin(new Insets(2, 2, 0, 0)); - jbEdit.setToolTipText(res.getString("JGeneralSubtrees.jbEdit.tooltip")); - jbEdit.setMnemonic(res.getString("JGeneralSubtrees.jbEdit.mnemonic").charAt(0)); - - jbEdit.setEnabled(false); - - jbEdit.addActionListener(evt -> { - try { - CursorUtil.setCursorBusy(JGeneralSubtrees.this); - editPressed(); - } finally { - CursorUtil.setCursorFree(JGeneralSubtrees.this); - } - }); + @Override + protected String getAddResource() { + return "images/add_general_subtree.png"; + } - jbRemove = new JButton(new ImageIcon( - Toolkit.getDefaultToolkit().createImage(getClass().getResource("images/remove_general_subtree.png")))); - jbRemove.setMargin(new Insets(2, 2, 0, 0)); - jbRemove.setToolTipText(res.getString("JGeneralSubtrees.jbRemove.tooltip")); - jbRemove.setMnemonic(res.getString("JGeneralSubtrees.jbRemove.mnemonic").charAt(0)); + @Override + protected String getEditResource() { + return "images/edit_general_subtree.png"; + } - jbRemove.setEnabled(false); + @Override + protected String getRemoveResource() { + return "images/remove_general_subtree.png"; + } - jbRemove.addActionListener(evt -> { - try { - CursorUtil.setCursorBusy(JGeneralSubtrees.this); - removePressed(); - } finally { - CursorUtil.setCursorFree(JGeneralSubtrees.this); - } - }); + @Override + protected String getBundleString(String suffix) { + return res.getString("JGeneralSubtrees." + suffix); + } - jpGeneralSubtreeButtons = new JPanel(); - jpGeneralSubtreeButtons.setLayout(new BoxLayout(jpGeneralSubtreeButtons, BoxLayout.Y_AXIS)); - jpGeneralSubtreeButtons.add(Box.createVerticalGlue()); - jpGeneralSubtreeButtons.add(jbAdd); - jpGeneralSubtreeButtons.add(Box.createVerticalStrut(3)); - jpGeneralSubtreeButtons.add(jbEdit); - jpGeneralSubtreeButtons.add(Box.createVerticalStrut(3)); - jpGeneralSubtreeButtons.add(jbRemove); - jpGeneralSubtreeButtons.add(Box.createVerticalGlue()); + @Override + protected List newCollection() { + return new ArrayList<>(); + } + @Override + protected JTable newTable() { GeneralSubtreesTableModel generalSubtreesTableModel = new GeneralSubtreesTableModel(); - jtGeneralSubtrees = new ToolTipTable(generalSubtreesTableModel); + JTable jtGeneralSubtrees = new ToolTipTable(generalSubtreesTableModel); TableRowSorter sorter = new TableRowSorter<>(generalSubtreesTableModel); sorter.setComparator(0, new GeneralSubtreesTableModel.GeneralSubtreeBaseComparator()); @@ -163,71 +102,24 @@ private void initComponents() { column.setCellRenderer(new GeneralSubtreesTableCellRend()); } - ListSelectionModel selectionModel = jtGeneralSubtrees.getSelectionModel(); - selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); - selectionModel.addListSelectionListener(evt -> { - if (!evt.getValueIsAdjusting()) { - updateButtonControls(); - } - }); - - jtGeneralSubtrees.addMouseListener(new MouseAdapter() { - @Override - public void mouseClicked(MouseEvent evt) { - maybeEditGeneralSubtree(evt); - } - }); - - jtGeneralSubtrees.addKeyListener(new KeyAdapter() { - boolean deleteLastPressed = false; - - @Override - public void keyPressed(KeyEvent evt) { - // Record delete pressed on non-Macs - if (!OperatingSystem.isMacOs()) { - deleteLastPressed = evt.getKeyCode() == KeyEvent.VK_DELETE; - } - } - - @Override - public void keyReleased(KeyEvent evt) { - // Delete on non-Mac if delete was pressed and is now released - if (!OperatingSystem.isMacOs() && deleteLastPressed && evt.getKeyCode() == KeyEvent.VK_DELETE) { - try { - CursorUtil.setCursorBusy(JGeneralSubtrees.this); - deleteLastPressed = false; - removeSelectedGeneralSubtree(); - } finally { - CursorUtil.setCursorFree(JGeneralSubtrees.this); - } - } - } + return jtGeneralSubtrees; + } - @Override - public void keyTyped(KeyEvent evt) { - // Delete on Mac if backspace typed - if (OperatingSystem.isMacOs() && evt.getKeyChar() == 0x08) { - try { - CursorUtil.setCursorBusy(JGeneralSubtrees.this); - removeSelectedGeneralSubtree(); - } finally { - CursorUtil.setCursorFree(JGeneralSubtrees.this); - } - } - } - }); + @Override + protected GeneralSubtree getItem(GeneralSubtree item) { + Container container = getTopLevelAncestor(); - jspGeneralSubtrees = PlatformUtil.createScrollPane(jtGeneralSubtrees, - ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, - ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); - jspGeneralSubtrees.getViewport().setBackground(jtGeneralSubtrees.getBackground()); + DGeneralSubtreeChooser dGeneralSubtreeChooser = null; - this.setLayout(new BorderLayout(5, 5)); - setPreferredSize(new Dimension(400, 150)); - add(jspGeneralSubtrees, BorderLayout.CENTER); - add(jpGeneralSubtreeButtons, BorderLayout.EAST); + if (container instanceof JDialog) { + dGeneralSubtreeChooser = new DGeneralSubtreeChooser((JDialog) container, title, item); + } else { + dGeneralSubtreeChooser = new DGeneralSubtreeChooser((JFrame) container, title, item); + } + dGeneralSubtreeChooser.setLocationRelativeTo(container); + dGeneralSubtreeChooser.setVisible(true); - populate(); + return dGeneralSubtreeChooser.getGeneralSubtree(); } /** @@ -236,7 +128,7 @@ public void keyTyped(KeyEvent evt) { * @return General subtrees */ public GeneralSubtrees getGeneralSubtrees() { - return generalSubtrees; + return new GeneralSubtrees(getItems()); } /** @@ -245,179 +137,6 @@ public GeneralSubtrees getGeneralSubtrees() { * @param generalSubtrees General subtrees */ public void setGeneralSubtrees(GeneralSubtrees generalSubtrees) { - this.generalSubtrees = generalSubtrees; - populate(); - } - - /** - * Sets whether or not the component is enabled. - * - * @param enabled True if this component should be enabled, false otherwise - */ - @Override - public void setEnabled(boolean enabled) { - this.enabled = enabled; - updateButtonControls(); - } - - /** - * Set component's tooltip text. - * - * @param toolTipText Tooltip text - */ - @Override - public void setToolTipText(String toolTipText) { - super.setToolTipText(toolTipText); - jspGeneralSubtrees.setToolTipText(toolTipText); - jtGeneralSubtrees.setToolTipText(toolTipText); - } - - private void populate() { - if (generalSubtrees == null) { - generalSubtrees = new GeneralSubtrees(new ArrayList<>()); - } - - reloadGeneralSubtreesTable(); - selectFirstGeneralSubtreeInTable(); - updateButtonControls(); - } - - private void addPressed() { - Container container = getTopLevelAncestor(); - - DGeneralSubtreeChooser dGeneralSubtreeChooser = null; - - if (container instanceof JDialog) { - dGeneralSubtreeChooser = new DGeneralSubtreeChooser((JDialog) container, title, null); - } else { - dGeneralSubtreeChooser = new DGeneralSubtreeChooser((JFrame) container, title, null); - } - dGeneralSubtreeChooser.setLocationRelativeTo(container); - dGeneralSubtreeChooser.setVisible(true); - - GeneralSubtree newGeneralSubtree = dGeneralSubtreeChooser.getGeneralSubtree(); - - if (newGeneralSubtree == null) { - return; - } - - generalSubtrees.getGeneralSubtrees().add(newGeneralSubtree); - - populate(); - selectGeneralSubtreeInTable(newGeneralSubtree); - } - - private void removePressed() { - removeSelectedGeneralSubtree(); - } - - private void removeSelectedGeneralSubtree() { - int selectedRow = jtGeneralSubtrees.getSelectedRow(); - - if (selectedRow != -1) { - GeneralSubtree generalSubtree = (GeneralSubtree) jtGeneralSubtrees.getValueAt(selectedRow, 0); - - generalSubtrees.getGeneralSubtrees().remove(generalSubtree); - - reloadGeneralSubtreesTable(); - selectFirstGeneralSubtreeInTable(); - updateButtonControls(); - } - } - - private void editPressed() { - editSelectedGeneralSubtree(); - } - - private void maybeEditGeneralSubtree(MouseEvent evt) { - if (evt.getClickCount() > 1) { - Point point = new Point(evt.getX(), evt.getY()); - int row = jtGeneralSubtrees.rowAtPoint(point); - - if (row != -1) { - try { - CursorUtil.setCursorBusy(JGeneralSubtrees.this); - jtGeneralSubtrees.setRowSelectionInterval(row, row); - editSelectedGeneralSubtree(); - } finally { - CursorUtil.setCursorFree(JGeneralSubtrees.this); - } - } - } - } - - private void updateButtonControls() { - if (!enabled) { - jbAdd.setEnabled(false); - jbEdit.setEnabled(false); - jbRemove.setEnabled(false); - } else { - jbAdd.setEnabled(true); - - int selectedRow = jtGeneralSubtrees.getSelectedRow(); - - if (selectedRow == -1) { - jbEdit.setEnabled(false); - jbRemove.setEnabled(false); - } else { - jbEdit.setEnabled(true); - jbRemove.setEnabled(true); - } - } - } - - private void editSelectedGeneralSubtree() { - int selectedRow = jtGeneralSubtrees.getSelectedRow(); - - if (selectedRow != -1) { - GeneralSubtree generalSubtree = (GeneralSubtree) jtGeneralSubtrees.getValueAt(selectedRow, 0); - - Container container = getTopLevelAncestor(); - - DGeneralSubtreeChooser dGeneralSubtreeChooser = null; - - if (container instanceof JDialog) { - dGeneralSubtreeChooser = new DGeneralSubtreeChooser((JDialog) container, title, generalSubtree); - } else { - dGeneralSubtreeChooser = new DGeneralSubtreeChooser((JFrame) container, title, generalSubtree); - } - dGeneralSubtreeChooser.setLocationRelativeTo(container); - dGeneralSubtreeChooser.setVisible(true); - - GeneralSubtree newGeneralSubtree = dGeneralSubtreeChooser.getGeneralSubtree(); - - if (newGeneralSubtree == null) { - return; - } - - generalSubtrees.getGeneralSubtrees().remove(generalSubtree); - generalSubtrees.getGeneralSubtrees().add(newGeneralSubtree); - - populate(); - selectGeneralSubtreeInTable(newGeneralSubtree); - } - } - - private void selectGeneralSubtreeInTable(GeneralSubtree generalSubtree) { - for (int i = 0; i < jtGeneralSubtrees.getRowCount(); i++) { - if (generalSubtree.equals(jtGeneralSubtrees.getValueAt(i, 0))) { - jtGeneralSubtrees.changeSelection(i, 0, false, false); - return; - } - } - } - - private void reloadGeneralSubtreesTable() { - getGeneralSubtreesTableModel().load(generalSubtrees); - } - - private void selectFirstGeneralSubtreeInTable() { - if (getGeneralSubtreesTableModel().getRowCount() > 0) { - jtGeneralSubtrees.changeSelection(0, 0, false, false); - } - } - - private GeneralSubtreesTableModel getGeneralSubtreesTableModel() { - return (GeneralSubtreesTableModel) jtGeneralSubtrees.getModel(); + setItems(new ArrayList<>(generalSubtrees.getGeneralSubtrees())); } } diff --git a/kse/src/main/java/org/kse/gui/crypto/policyinformation/DPolicyInformationChooser.java b/kse/src/main/java/org/kse/gui/crypto/policyinformation/DPolicyInformationChooser.java index f37c735e4..f48c949bc 100644 --- a/kse/src/main/java/org/kse/gui/crypto/policyinformation/DPolicyInformationChooser.java +++ b/kse/src/main/java/org/kse/gui/crypto/policyinformation/DPolicyInformationChooser.java @@ -24,7 +24,6 @@ import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; -import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.ResourceBundle; @@ -79,10 +78,8 @@ public class DPolicyInformationChooser extends JEscDialog { * @param parent The parent frame * @param title The dialog title * @param policyInformation Policy information - * @throws IOException If policy information could not be decoded */ - public DPolicyInformationChooser(JFrame parent, String title, PolicyInformation policyInformation) - throws IOException { + public DPolicyInformationChooser(JFrame parent, String title, PolicyInformation policyInformation) { super(parent, title, ModalityType.DOCUMENT_MODAL); initComponents(policyInformation); } @@ -93,15 +90,13 @@ public DPolicyInformationChooser(JFrame parent, String title, PolicyInformation * @param parent The parent dialog * @param title The dialog title * @param policyInformation Policy information - * @throws IOException If policy information could not be decoded */ - public DPolicyInformationChooser(JDialog parent, String title, PolicyInformation policyInformation) - throws IOException { + public DPolicyInformationChooser(JDialog parent, String title, PolicyInformation policyInformation) { super(parent, title, Dialog.ModalityType.DOCUMENT_MODAL); initComponents(policyInformation); } - private void initComponents(PolicyInformation policyInformation) throws IOException { + private void initComponents(PolicyInformation policyInformation) { jlPolicyIdentifier = new JLabel(res.getString("DPolicyInformationChooser.jlPolicyIdentifier.text")); joiPolicyIdentifier = new JObjectId(res.getString("DPolicyInformationChooser.PolicyIdentifier.Text")); @@ -149,7 +144,7 @@ public void actionPerformed(ActionEvent evt) { pack(); } - private void populate(PolicyInformation policyInformation) throws IOException { + private void populate(PolicyInformation policyInformation) { if (policyInformation != null) { joiPolicyIdentifier.setObjectId(policyInformation.getPolicyIdentifier()); @@ -164,7 +159,7 @@ private void populate(PolicyInformation policyInformation) throws IOException { policyQualifierInfo.add(policyQualInfo); } - jpqPolicyQualifiers.setPolicyQualifierInfo(policyQualifierInfo); + jpqPolicyQualifiers.setItems(policyQualifierInfo); } } } @@ -188,7 +183,7 @@ private void okPressed() { return; } - List policyQualifierInfo = jpqPolicyQualifiers.getPolicyQualifierInfo(); + List policyQualifierInfo = jpqPolicyQualifiers.getItems(); if (!policyQualifierInfo.isEmpty()) { ASN1EncodableVector policyQualifiersVec = new ASN1EncodableVector(); diff --git a/kse/src/main/java/org/kse/gui/crypto/policyinformation/DPolicyQualifierInfoChooser.java b/kse/src/main/java/org/kse/gui/crypto/policyinformation/DPolicyQualifierInfoChooser.java index 827ceb280..a752c8d7f 100644 --- a/kse/src/main/java/org/kse/gui/crypto/policyinformation/DPolicyQualifierInfoChooser.java +++ b/kse/src/main/java/org/kse/gui/crypto/policyinformation/DPolicyQualifierInfoChooser.java @@ -26,7 +26,6 @@ import java.awt.Dialog; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; -import java.io.IOException; import java.util.ResourceBundle; import javax.swing.AbstractAction; @@ -83,10 +82,8 @@ public class DPolicyQualifierInfoChooser extends JEscDialog { * @param parent The parent frame * @param title The dialog title * @param policyQualifierInfo Policy qualifier info - * @throws IOException If policy qualifier info could not be decoded */ - public DPolicyQualifierInfoChooser(JFrame parent, String title, PolicyQualifierInfo policyQualifierInfo) - throws IOException { + public DPolicyQualifierInfoChooser(JFrame parent, String title, PolicyQualifierInfo policyQualifierInfo) { super(parent, title, ModalityType.DOCUMENT_MODAL); initComponents(policyQualifierInfo); } @@ -97,15 +94,13 @@ public DPolicyQualifierInfoChooser(JFrame parent, String title, PolicyQualifierI * @param parent The parent dialog * @param title The dialog title * @param policyQualifierInfo Policy qualifier info - * @throws IOException If policy qualifier info could not be decoded */ - public DPolicyQualifierInfoChooser(JDialog parent, String title, PolicyQualifierInfo policyQualifierInfo) - throws IOException { + public DPolicyQualifierInfoChooser(JDialog parent, String title, PolicyQualifierInfo policyQualifierInfo) { super(parent, title, Dialog.ModalityType.DOCUMENT_MODAL); initComponents(policyQualifierInfo); } - private void initComponents(PolicyQualifierInfo policyQualifierInfo) throws IOException { + private void initComponents(PolicyQualifierInfo policyQualifierInfo) { jlPolicyQualifierInfoType = new JLabel( res.getString("DPolicyQualifierInfoChooser.jlPolicyQualifierInfoType.text")); @@ -183,7 +178,7 @@ private void policyQualifierInfoTypeChanged() { pack(); } - private void populate(PolicyQualifierInfo policyQualifierInfo) throws IOException { + private void populate(PolicyQualifierInfo policyQualifierInfo) { if (policyQualifierInfo == null) { jrbCps.setSelected(true); } else { diff --git a/kse/src/main/java/org/kse/gui/crypto/policyinformation/JPolicyInformation.java b/kse/src/main/java/org/kse/gui/crypto/policyinformation/JPolicyInformation.java index 7fc2e0c07..38475de71 100644 --- a/kse/src/main/java/org/kse/gui/crypto/policyinformation/JPolicyInformation.java +++ b/kse/src/main/java/org/kse/gui/crypto/policyinformation/JPolicyInformation.java @@ -19,60 +19,30 @@ */ package org.kse.gui.crypto.policyinformation; -import java.awt.BorderLayout; import java.awt.Container; -import java.awt.Dimension; -import java.awt.Insets; -import java.awt.Point; -import java.awt.Toolkit; -import java.awt.event.KeyAdapter; -import java.awt.event.KeyEvent; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; -import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.ResourceBundle; -import javax.swing.Box; -import javax.swing.BoxLayout; -import javax.swing.ImageIcon; -import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; -import javax.swing.JPanel; -import javax.swing.JScrollPane; import javax.swing.JTable; -import javax.swing.ListSelectionModel; -import javax.swing.ScrollPaneConstants; import javax.swing.table.TableColumn; import javax.swing.table.TableRowSorter; import org.bouncycastle.asn1.x509.PolicyInformation; -import org.kse.gui.CursorUtil; -import org.kse.gui.PlatformUtil; -import org.kse.gui.error.DError; +import org.kse.gui.crypto.JAddEditRemovePanel; import org.kse.gui.table.ToolTipTable; -import org.kse.utilities.os.OperatingSystem; /** * Component to edit a set of policy information. */ -public class JPolicyInformation extends JPanel { +public class JPolicyInformation extends JAddEditRemovePanel, PolicyInformation> { private static final long serialVersionUID = 1L; private static ResourceBundle res = ResourceBundle.getBundle("org/kse/gui/crypto/policyinformation/resources"); - private JPanel jpPolicyInformationButtons; - private JButton jbAdd; - private JButton jbEdit; - private JButton jbRemove; - private JScrollPane jspPolicyInformation; - private JTable jtPolicyInformation; - private String title; - private List policyInformation; - private boolean enabled = true; /** * Construct a JPolicyInformation. @@ -81,71 +51,37 @@ public class JPolicyInformation extends JPanel { */ public JPolicyInformation(String title) { this.title = title; - initComponents(); } - private void initComponents() { - jbAdd = new JButton(new ImageIcon( - Toolkit.getDefaultToolkit().createImage(getClass().getResource("images/add_policy_info.png")))); - jbAdd.setMargin(new Insets(2, 2, 0, 0)); - jbAdd.setToolTipText(res.getString("JPolicyInformation.jbAdd.tooltip")); - jbAdd.setMnemonic(res.getString("JPolicyInformation.jbAdd.mnemonic").charAt(0)); - - jbAdd.addActionListener(evt -> { - try { - CursorUtil.setCursorBusy(JPolicyInformation.this); - addPressed(); - } finally { - CursorUtil.setCursorFree(JPolicyInformation.this); - } - }); - - jbEdit = new JButton(new ImageIcon( - Toolkit.getDefaultToolkit().createImage(getClass().getResource("images/edit_policy_info.png")))); - jbEdit.setMargin(new Insets(2, 2, 0, 0)); - jbEdit.setToolTipText(res.getString("JPolicyInformation.jbEdit.tooltip")); - jbEdit.setMnemonic(res.getString("JPolicyInformation.jbEdit.mnemonic").charAt(0)); - - jbEdit.setEnabled(false); - - jbEdit.addActionListener(evt -> { - try { - CursorUtil.setCursorBusy(JPolicyInformation.this); - editPressed(); - } finally { - CursorUtil.setCursorFree(JPolicyInformation.this); - } - }); + @Override + protected String getAddResource() { + return "images/add_policy_info.png"; + } - jbRemove = new JButton(new ImageIcon( - Toolkit.getDefaultToolkit().createImage(getClass().getResource("images/remove_policy_info.png")))); - jbRemove.setMargin(new Insets(2, 2, 0, 0)); - jbRemove.setToolTipText(res.getString("JPolicyInformation.jbRemove.tooltip")); - jbRemove.setMnemonic(res.getString("JPolicyInformation.jbRemove.mnemonic").charAt(0)); + @Override + protected String getEditResource() { + return "images/edit_policy_info.png"; + } - jbRemove.setEnabled(false); + @Override + protected String getRemoveResource() { + return "images/remove_policy_info.png"; + } - jbRemove.addActionListener(evt -> { - try { - CursorUtil.setCursorBusy(JPolicyInformation.this); - removePressed(); - } finally { - CursorUtil.setCursorFree(JPolicyInformation.this); - } - }); + @Override + protected String getBundleString(String suffix) { + return res.getString("JPolicyInformation." + suffix); + } - jpPolicyInformationButtons = new JPanel(); - jpPolicyInformationButtons.setLayout(new BoxLayout(jpPolicyInformationButtons, BoxLayout.Y_AXIS)); - jpPolicyInformationButtons.add(Box.createVerticalGlue()); - jpPolicyInformationButtons.add(jbAdd); - jpPolicyInformationButtons.add(Box.createVerticalStrut(3)); - jpPolicyInformationButtons.add(jbEdit); - jpPolicyInformationButtons.add(Box.createVerticalStrut(3)); - jpPolicyInformationButtons.add(jbRemove); - jpPolicyInformationButtons.add(Box.createVerticalGlue()); + @Override + protected List newCollection() { + return new ArrayList<>(); + } + @Override + protected JTable newTable() { PolicyInformationTableModel policyInformationTableModel = new PolicyInformationTableModel(); - jtPolicyInformation = new ToolTipTable(policyInformationTableModel); + JTable jtPolicyInformation = new ToolTipTable(policyInformationTableModel); TableRowSorter sorter = new TableRowSorter<>(policyInformationTableModel); sorter.setComparator(0, new PolicyInformationTableModel.PolicyInformationComparator()); @@ -163,288 +99,23 @@ private void initComponents() { column.setCellRenderer(new PolicyInformationTableCellRend()); } - ListSelectionModel selectionModel = jtPolicyInformation.getSelectionModel(); - selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); - selectionModel.addListSelectionListener(evt -> { - if (!evt.getValueIsAdjusting()) { - updateButtonControls(); - } - }); - - jtPolicyInformation.addMouseListener(new MouseAdapter() { - @Override - public void mouseClicked(MouseEvent evt) { - maybeEditPolicyInformation(evt); - } - }); - - jtPolicyInformation.addKeyListener(new KeyAdapter() { - boolean deleteLastPressed = false; - - @Override - public void keyPressed(KeyEvent evt) { - // Record delete pressed on non-Macs - if (!OperatingSystem.isMacOs()) { - deleteLastPressed = evt.getKeyCode() == KeyEvent.VK_DELETE; - } - } - - @Override - public void keyReleased(KeyEvent evt) { - // Delete on non-Mac if delete was pressed and is now released - if (!OperatingSystem.isMacOs() && deleteLastPressed && evt.getKeyCode() == KeyEvent.VK_DELETE) { - try { - CursorUtil.setCursorBusy(JPolicyInformation.this); - deleteLastPressed = false; - removeSelectedPolicyInformation(); - } finally { - CursorUtil.setCursorFree(JPolicyInformation.this); - } - } - } - - @Override - public void keyTyped(KeyEvent evt) { - // Delete on Mac if backspace typed - if (OperatingSystem.isMacOs() && evt.getKeyChar() == 0x08) { - try { - CursorUtil.setCursorBusy(JPolicyInformation.this); - removeSelectedPolicyInformation(); - } finally { - CursorUtil.setCursorFree(JPolicyInformation.this); - } - } - } - }); - - jspPolicyInformation = PlatformUtil.createScrollPane(jtPolicyInformation, - ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, - ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); - jspPolicyInformation.getViewport().setBackground(jtPolicyInformation.getBackground()); - - this.setLayout(new BorderLayout(5, 5)); - setPreferredSize(new Dimension(250, 150)); - add(jspPolicyInformation, BorderLayout.CENTER); - add(jpPolicyInformationButtons, BorderLayout.EAST); - - populate(); + return jtPolicyInformation; } - /** - * Get policy information. - * - * @return Policy information - */ - public List getPolicyInformation() { - return policyInformation; - } - - /** - * Set policy information. - * - * @param policyInformation Policy information - */ - public void setPolicyInformation(List policyInformation) { - this.policyInformation = policyInformation; - populate(); - } - - /** - * Sets whether or not the component is enabled. - * - * @param enabled True if this component should be enabled, false otherwise - */ @Override - public void setEnabled(boolean enabled) { - this.enabled = enabled; - - updateButtonControls(); - } - - /** - * Set component's tooltip text. - * - * @param toolTipText Tooltip text - */ - @Override - public void setToolTipText(String toolTipText) { - super.setToolTipText(toolTipText); - jspPolicyInformation.setToolTipText(toolTipText); - jtPolicyInformation.setToolTipText(toolTipText); - } - - private void populate() { - if (policyInformation == null) { - policyInformation = new ArrayList<>(); - } - - reloadPolicyInformationTable(); - selectFirstPolicyInformationInTable(); - updateButtonControls(); - } - - private void addPressed() { + protected PolicyInformation getItem(PolicyInformation item) { Container container = getTopLevelAncestor(); - try { - DPolicyInformationChooser dPolicyInformationChooser = null; - - if (container instanceof JDialog) { - dPolicyInformationChooser = new DPolicyInformationChooser((JDialog) container, title, null); - } else { - dPolicyInformationChooser = new DPolicyInformationChooser((JFrame) container, title, null); - } - dPolicyInformationChooser.setLocationRelativeTo(container); - dPolicyInformationChooser.setVisible(true); - - PolicyInformation newPolicyInfo = dPolicyInformationChooser.getPolicyInformation(); - - if (newPolicyInfo == null) { - return; - } - - policyInformation.add(newPolicyInfo); - - populate(); - selectPolicyInformationInTable(newPolicyInfo); - } catch (IOException ex) { - DError dError = null; - - if (container instanceof JDialog) { - dError = new DError((JDialog) container, ex); - } else { - dError = new DError((JFrame) container, ex); - } - - dError.setLocationRelativeTo(container); - dError.setVisible(true); - } - } - - private void removePressed() { - removeSelectedPolicyInformation(); - } - - private void removeSelectedPolicyInformation() { - int selectedRow = jtPolicyInformation.getSelectedRow(); - - if (selectedRow != -1) { - PolicyInformation policyInfo = (PolicyInformation) jtPolicyInformation.getValueAt(selectedRow, 0); - - policyInformation.remove(policyInfo); - - reloadPolicyInformationTable(); - selectFirstPolicyInformationInTable(); - updateButtonControls(); - } - } - - private void editPressed() { - editSelectedPolicyInformation(); - } - - private void maybeEditPolicyInformation(MouseEvent evt) { - if (evt.getClickCount() > 1) { - Point point = new Point(evt.getX(), evt.getY()); - int row = jtPolicyInformation.rowAtPoint(point); - - if (row != -1) { - try { - CursorUtil.setCursorBusy(JPolicyInformation.this); - jtPolicyInformation.setRowSelectionInterval(row, row); - editSelectedPolicyInformation(); - } finally { - CursorUtil.setCursorFree(JPolicyInformation.this); - } - } - } - } + DPolicyInformationChooser dPolicyInformationChooser = null; - private void updateButtonControls() { - if (!enabled) { - jbAdd.setEnabled(false); - jbEdit.setEnabled(false); - jbRemove.setEnabled(false); + if (container instanceof JDialog) { + dPolicyInformationChooser = new DPolicyInformationChooser((JDialog) container, title, item); } else { - jbAdd.setEnabled(true); - - int selectedRow = jtPolicyInformation.getSelectedRow(); - - if (selectedRow == -1) { - jbEdit.setEnabled(false); - jbRemove.setEnabled(false); - } else { - jbEdit.setEnabled(true); - jbRemove.setEnabled(true); - } - } - } - - private void editSelectedPolicyInformation() { - int selectedRow = jtPolicyInformation.getSelectedRow(); - - if (selectedRow != -1) { - PolicyInformation policyInfo = (PolicyInformation) jtPolicyInformation.getValueAt(selectedRow, 0); - - Container container = getTopLevelAncestor(); - - try { - DPolicyInformationChooser dPolicyNameChooser = null; - - if (container instanceof JDialog) { - dPolicyNameChooser = new DPolicyInformationChooser((JDialog) container, title, policyInfo); - } else { - dPolicyNameChooser = new DPolicyInformationChooser((JFrame) container, title, policyInfo); - } - dPolicyNameChooser.setLocationRelativeTo(container); - dPolicyNameChooser.setVisible(true); - - PolicyInformation newPolicyInfo = dPolicyNameChooser.getPolicyInformation(); - - if (newPolicyInfo == null) { - return; - } - - policyInformation.remove(policyInfo); - policyInformation.add(newPolicyInfo); - - populate(); - selectPolicyInformationInTable(newPolicyInfo); - } catch (IOException ex) { - DError dError = null; - - if (container instanceof JDialog) { - dError = new DError((JDialog) container, ex); - } else { - dError = new DError((JFrame) container, ex); - } - - dError.setLocationRelativeTo(container); - dError.setVisible(true); - } - } - } - - private void selectPolicyInformationInTable(PolicyInformation policyInfo) { - for (int i = 0; i < jtPolicyInformation.getRowCount(); i++) { - if (policyInfo.equals(jtPolicyInformation.getValueAt(i, 0))) { - jtPolicyInformation.changeSelection(i, 0, false, false); - return; - } + dPolicyInformationChooser = new DPolicyInformationChooser((JFrame) container, title, item); } - } - - private void reloadPolicyInformationTable() { - getPolicyInformationTableModel().load(policyInformation); - } - - private void selectFirstPolicyInformationInTable() { - if (getPolicyInformationTableModel().getRowCount() > 0) { - jtPolicyInformation.changeSelection(0, 0, false, false); - } - } + dPolicyInformationChooser.setLocationRelativeTo(container); + dPolicyInformationChooser.setVisible(true); - private PolicyInformationTableModel getPolicyInformationTableModel() { - return (PolicyInformationTableModel) jtPolicyInformation.getModel(); + return dPolicyInformationChooser.getPolicyInformation(); } } diff --git a/kse/src/main/java/org/kse/gui/crypto/policyinformation/JPolicyQualifierInfo.java b/kse/src/main/java/org/kse/gui/crypto/policyinformation/JPolicyQualifierInfo.java index 3e8c62da1..59f7727ef 100644 --- a/kse/src/main/java/org/kse/gui/crypto/policyinformation/JPolicyQualifierInfo.java +++ b/kse/src/main/java/org/kse/gui/crypto/policyinformation/JPolicyQualifierInfo.java @@ -19,60 +19,30 @@ */ package org.kse.gui.crypto.policyinformation; -import java.awt.BorderLayout; import java.awt.Container; -import java.awt.Dimension; -import java.awt.Insets; -import java.awt.Point; -import java.awt.Toolkit; -import java.awt.event.KeyAdapter; -import java.awt.event.KeyEvent; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; -import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.ResourceBundle; -import javax.swing.Box; -import javax.swing.BoxLayout; -import javax.swing.ImageIcon; -import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; -import javax.swing.JPanel; -import javax.swing.JScrollPane; import javax.swing.JTable; -import javax.swing.ListSelectionModel; -import javax.swing.ScrollPaneConstants; import javax.swing.table.TableColumn; import javax.swing.table.TableRowSorter; import org.bouncycastle.asn1.x509.PolicyQualifierInfo; -import org.kse.gui.CursorUtil; -import org.kse.gui.PlatformUtil; -import org.kse.gui.error.DError; +import org.kse.gui.crypto.JAddEditRemovePanel; import org.kse.gui.table.ToolTipTable; -import org.kse.utilities.os.OperatingSystem; /** * Component to edit a set of policy qualifier info. */ -public class JPolicyQualifierInfo extends JPanel { +public class JPolicyQualifierInfo extends JAddEditRemovePanel, PolicyQualifierInfo> { private static final long serialVersionUID = 1L; private static ResourceBundle res = ResourceBundle.getBundle("org/kse/gui/crypto/policyinformation/resources"); - private JPanel jpPolicyQualifierInfoButtons; - private JButton jbAdd; - private JButton jbEdit; - private JButton jbRemove; - private JScrollPane jspPolicyQualifierInfo; - private JTable jtPolicyQualifierInfo; - private String title; - private List policyQualifierInfo; - private boolean enabled = true; /** * Construct a JPolicyQualifierInfo. @@ -81,71 +51,37 @@ public class JPolicyQualifierInfo extends JPanel { */ public JPolicyQualifierInfo(String title) { this.title = title; - initComponents(); } - private void initComponents() { - jbAdd = new JButton(new ImageIcon(Toolkit.getDefaultToolkit().createImage( - getClass().getResource("images/add_policy_qualifier_info.png")))); - jbAdd.setMargin(new Insets(2, 2, 0, 0)); - jbAdd.setToolTipText(res.getString("JPolicyQualifierInfo.jbAdd.tooltip")); - jbAdd.setMnemonic(res.getString("JPolicyQualifierInfo.jbAdd.mnemonic").charAt(0)); - - jbAdd.addActionListener(evt -> { - try { - CursorUtil.setCursorBusy(JPolicyQualifierInfo.this); - addPressed(); - } finally { - CursorUtil.setCursorFree(JPolicyQualifierInfo.this); - } - }); - - jbEdit = new JButton(new ImageIcon(Toolkit.getDefaultToolkit().createImage( - getClass().getResource("images/edit_policy_qualifier_info.png")))); - jbEdit.setMargin(new Insets(2, 2, 0, 0)); - jbEdit.setToolTipText(res.getString("JPolicyQualifierInfo.jbEdit.tooltip")); - jbEdit.setMnemonic(res.getString("JPolicyQualifierInfo.jbEdit.mnemonic").charAt(0)); - - jbEdit.setEnabled(false); - - jbEdit.addActionListener(evt -> { - try { - CursorUtil.setCursorBusy(JPolicyQualifierInfo.this); - editPressed(); - } finally { - CursorUtil.setCursorFree(JPolicyQualifierInfo.this); - } - }); + @Override + protected String getAddResource() { + return "images/add_policy_qualifier_info.png"; + } - jbRemove = new JButton(new ImageIcon(Toolkit.getDefaultToolkit().createImage( - getClass().getResource("images/remove_policy_qualifier_info.png")))); - jbRemove.setMargin(new Insets(2, 2, 0, 0)); - jbRemove.setToolTipText(res.getString("JPolicyQualifierInfo.jbRemove.tooltip")); - jbRemove.setMnemonic(res.getString("JPolicyQualifierInfo.jbRemove.mnemonic").charAt(0)); + @Override + protected String getEditResource() { + return "images/edit_policy_qualifier_info.png"; + } - jbRemove.setEnabled(false); + @Override + protected String getRemoveResource() { + return "images/remove_policy_qualifier_info.png"; + } - jbRemove.addActionListener(evt -> { - try { - CursorUtil.setCursorBusy(JPolicyQualifierInfo.this); - removePressed(); - } finally { - CursorUtil.setCursorFree(JPolicyQualifierInfo.this); - } - }); + @Override + protected String getBundleString(String suffix) { + return res.getString("JPolicyQualifierInfo." + suffix); + } - jpPolicyQualifierInfoButtons = new JPanel(); - jpPolicyQualifierInfoButtons.setLayout(new BoxLayout(jpPolicyQualifierInfoButtons, BoxLayout.Y_AXIS)); - jpPolicyQualifierInfoButtons.add(Box.createVerticalGlue()); - jpPolicyQualifierInfoButtons.add(jbAdd); - jpPolicyQualifierInfoButtons.add(Box.createVerticalStrut(3)); - jpPolicyQualifierInfoButtons.add(jbEdit); - jpPolicyQualifierInfoButtons.add(Box.createVerticalStrut(3)); - jpPolicyQualifierInfoButtons.add(jbRemove); - jpPolicyQualifierInfoButtons.add(Box.createVerticalGlue()); + @Override + protected List newCollection() { + return new ArrayList<>(); + } + @Override + protected JTable newTable() { PolicyQualifierInfoTableModel policyQualifierInfoTableModel = new PolicyQualifierInfoTableModel(); - jtPolicyQualifierInfo = new ToolTipTable(policyQualifierInfoTableModel); + JTable jtPolicyQualifierInfo = new ToolTipTable(policyQualifierInfoTableModel); TableRowSorter sorter = new TableRowSorter<>(policyQualifierInfoTableModel); sorter.setComparator(0, new PolicyQualifierInfoTableModel.PolicyQualifierInfoComparator()); @@ -163,289 +99,23 @@ private void initComponents() { column.setCellRenderer(new PolicyQualifierInfoTableCellRend()); } - ListSelectionModel selectionModel = jtPolicyQualifierInfo.getSelectionModel(); - selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); - selectionModel.addListSelectionListener(evt -> { - if (!evt.getValueIsAdjusting()) { - updateButtonControls(); - } - }); - - jtPolicyQualifierInfo.addMouseListener(new MouseAdapter() { - @Override - public void mouseClicked(MouseEvent evt) { - maybeEditPolicyQualifierInfo(evt); - } - }); - - jtPolicyQualifierInfo.addKeyListener(new KeyAdapter() { - boolean deleteLastPressed = false; - - @Override - public void keyPressed(KeyEvent evt) { - // Record delete pressed on non-Macs - if (!OperatingSystem.isMacOs()) { - deleteLastPressed = evt.getKeyCode() == KeyEvent.VK_DELETE; - } - } - - @Override - public void keyReleased(KeyEvent evt) { - // Delete on non-Mac if delete was pressed and is now released - if (!OperatingSystem.isMacOs() && deleteLastPressed && evt.getKeyCode() == KeyEvent.VK_DELETE) { - try { - CursorUtil.setCursorBusy(JPolicyQualifierInfo.this); - deleteLastPressed = false; - removeSelectedPolicyQualifierInfo(); - } finally { - CursorUtil.setCursorFree(JPolicyQualifierInfo.this); - } - } - } - - @Override - public void keyTyped(KeyEvent evt) { - // Delete on Mac if backspace typed - if (OperatingSystem.isMacOs() && evt.getKeyChar() == 0x08) { - try { - CursorUtil.setCursorBusy(JPolicyQualifierInfo.this); - removeSelectedPolicyQualifierInfo(); - } finally { - CursorUtil.setCursorFree(JPolicyQualifierInfo.this); - } - } - } - }); - - jspPolicyQualifierInfo = PlatformUtil.createScrollPane(jtPolicyQualifierInfo, - ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, - ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); - jspPolicyQualifierInfo.getViewport().setBackground(jtPolicyQualifierInfo.getBackground()); - - this.setLayout(new BorderLayout(5, 5)); - setPreferredSize(new Dimension(400, 150)); - add(jspPolicyQualifierInfo, BorderLayout.CENTER); - add(jpPolicyQualifierInfoButtons, BorderLayout.EAST); - - populate(); + return jtPolicyQualifierInfo; } - /** - * Get policy qualifier info. - * - * @return Policy qualifier info - */ - public List getPolicyQualifierInfo() { - return policyQualifierInfo; - } - - /** - * Set policy qualifier info. - * - * @param policyQualifierInfo Policy qualifier info - */ - public void setPolicyQualifierInfo(List policyQualifierInfo) { - this.policyQualifierInfo = policyQualifierInfo; - populate(); - } - - /** - * Sets whether or not the component is enabled. - * - * @param enabled True if this component should be enabled, false otherwise - */ - @Override - public void setEnabled(boolean enabled) { - this.enabled = enabled; - updateButtonControls(); - } - - /** - * Set component's tooltip text. - * - * @param toolTipText Tooltip text - */ @Override - public void setToolTipText(String toolTipText) { - super.setToolTipText(toolTipText); - jspPolicyQualifierInfo.setToolTipText(toolTipText); - jtPolicyQualifierInfo.setToolTipText(toolTipText); - } - - private void populate() { - if (policyQualifierInfo == null) { - policyQualifierInfo = new ArrayList<>(); - } - - reloadPolicyQualifierInfoTable(); - selectFirstPolicyQualifierInfoInTable(); - updateButtonControls(); - } - - private void addPressed() { + protected PolicyQualifierInfo getItem(PolicyQualifierInfo item) { Container container = getTopLevelAncestor(); - try { - DPolicyQualifierInfoChooser dPolicyQualifierInfoChooser = null; - - if (container instanceof JDialog) { - dPolicyQualifierInfoChooser = new DPolicyQualifierInfoChooser((JDialog) container, title, null); - } else { - dPolicyQualifierInfoChooser = new DPolicyQualifierInfoChooser((JFrame) container, title, null); - } - dPolicyQualifierInfoChooser.setLocationRelativeTo(container); - dPolicyQualifierInfoChooser.setVisible(true); - - PolicyQualifierInfo newPolicyQualifierInfo = dPolicyQualifierInfoChooser.getPolicyQualifierInfo(); - - if (newPolicyQualifierInfo == null) { - return; - } - - policyQualifierInfo.add(newPolicyQualifierInfo); - - populate(); - selectPolicyQualifierInfoInTable(newPolicyQualifierInfo); - } catch (IOException ex) { - DError dError = null; - - if (container instanceof JDialog) { - dError = new DError((JDialog) container, ex); - } else { - dError = new DError((JFrame) container, ex); - } - - dError.setLocationRelativeTo(container); - dError.setVisible(true); - } - } - - private void removePressed() { - removeSelectedPolicyQualifierInfo(); - } - - private void removeSelectedPolicyQualifierInfo() { - int selectedRow = jtPolicyQualifierInfo.getSelectedRow(); - - if (selectedRow != -1) { - PolicyQualifierInfo policyQualInfo = (PolicyQualifierInfo) jtPolicyQualifierInfo.getValueAt(selectedRow, 0); - - policyQualifierInfo.remove(policyQualInfo); - - reloadPolicyQualifierInfoTable(); - selectFirstPolicyQualifierInfoInTable(); - updateButtonControls(); - } - } - - private void editPressed() { - editSelectedPolicQualifier(); - } - - private void maybeEditPolicyQualifierInfo(MouseEvent evt) { - if (evt.getClickCount() > 1) { - Point point = new Point(evt.getX(), evt.getY()); - int row = jtPolicyQualifierInfo.rowAtPoint(point); - - if (row != -1) { - try { - CursorUtil.setCursorBusy(JPolicyQualifierInfo.this); - jtPolicyQualifierInfo.setRowSelectionInterval(row, row); - editSelectedPolicQualifier(); - } finally { - CursorUtil.setCursorFree(JPolicyQualifierInfo.this); - } - } - } - } + DPolicyQualifierInfoChooser dPolicyQualifierInfoChooser = null; - private void updateButtonControls() { - if (!enabled) { - jbAdd.setEnabled(false); - jbEdit.setEnabled(false); - jbRemove.setEnabled(false); + if (container instanceof JDialog) { + dPolicyQualifierInfoChooser = new DPolicyQualifierInfoChooser((JDialog) container, title, item); } else { - jbAdd.setEnabled(true); - - int selectedRow = jtPolicyQualifierInfo.getSelectedRow(); - - if (selectedRow == -1) { - jbEdit.setEnabled(false); - jbRemove.setEnabled(false); - } else { - jbEdit.setEnabled(true); - jbRemove.setEnabled(true); - } - } - } - - private void editSelectedPolicQualifier() { - int selectedRow = jtPolicyQualifierInfo.getSelectedRow(); - - if (selectedRow != -1) { - PolicyQualifierInfo policyQualInfo = (PolicyQualifierInfo) jtPolicyQualifierInfo.getValueAt(selectedRow, 0); - - Container container = getTopLevelAncestor(); - - try { - DPolicyQualifierInfoChooser dPolicyQualifierInfoChooser = null; - - if (container instanceof JDialog) { - dPolicyQualifierInfoChooser = new DPolicyQualifierInfoChooser((JDialog) container, title, - policyQualInfo); - } else { - dPolicyQualifierInfoChooser = new DPolicyQualifierInfoChooser((JFrame) container, title, - policyQualInfo); - } - dPolicyQualifierInfoChooser.setLocationRelativeTo(container); - dPolicyQualifierInfoChooser.setVisible(true); - - PolicyQualifierInfo newPolicyQualifierInfo = dPolicyQualifierInfoChooser.getPolicyQualifierInfo(); - - if (newPolicyQualifierInfo == null) { - return; - } - - policyQualifierInfo.remove(policyQualInfo); - policyQualifierInfo.add(newPolicyQualifierInfo); - - populate(); - selectPolicyQualifierInfoInTable(newPolicyQualifierInfo); - } catch (IOException ex) { - DError dError = null; - - if (container instanceof JDialog) { - dError = new DError((JDialog) container, ex); - } else { - dError = new DError((JFrame) container, ex); - } - - dError.setLocationRelativeTo(container); - dError.setVisible(true); - } - } - } - - private void selectPolicyQualifierInfoInTable(PolicyQualifierInfo policyQualInfo) { - for (int i = 0; i < jtPolicyQualifierInfo.getRowCount(); i++) { - if (policyQualInfo.equals(jtPolicyQualifierInfo.getValueAt(i, 0))) { - jtPolicyQualifierInfo.changeSelection(i, 0, false, false); - return; - } + dPolicyQualifierInfoChooser = new DPolicyQualifierInfoChooser((JFrame) container, title, item); } - } - - private void reloadPolicyQualifierInfoTable() { - getPolicyQualifierInfoTableModel().load(policyQualifierInfo); - } - - private void selectFirstPolicyQualifierInfoInTable() { - if (getPolicyQualifierInfoTableModel().getRowCount() > 0) { - jtPolicyQualifierInfo.changeSelection(0, 0, false, false); - } - } + dPolicyQualifierInfoChooser.setLocationRelativeTo(container); + dPolicyQualifierInfoChooser.setVisible(true); - private PolicyQualifierInfoTableModel getPolicyQualifierInfoTableModel() { - return (PolicyQualifierInfoTableModel) jtPolicyQualifierInfo.getModel(); + return dPolicyQualifierInfoChooser.getPolicyQualifierInfo(); } } diff --git a/kse/src/main/java/org/kse/gui/crypto/policyinformation/JUserNotice.java b/kse/src/main/java/org/kse/gui/crypto/policyinformation/JUserNotice.java index e880757d4..782e84f06 100644 --- a/kse/src/main/java/org/kse/gui/crypto/policyinformation/JUserNotice.java +++ b/kse/src/main/java/org/kse/gui/crypto/policyinformation/JUserNotice.java @@ -61,6 +61,7 @@ public JUserNotice(String title) { initComponents(); } + // TODO JW - miglayout private void initComponents() { jtfUserNotice = new JTextField(20); jtfUserNotice.setEditable(false); diff --git a/kse/src/main/java/org/kse/gui/crypto/policyinformation/PolicyInformationTableCellRend.java b/kse/src/main/java/org/kse/gui/crypto/policyinformation/PolicyInformationTableCellRend.java index 03a9bbc07..8d5300bb4 100644 --- a/kse/src/main/java/org/kse/gui/crypto/policyinformation/PolicyInformationTableCellRend.java +++ b/kse/src/main/java/org/kse/gui/crypto/policyinformation/PolicyInformationTableCellRend.java @@ -20,7 +20,6 @@ package org.kse.gui.crypto.policyinformation; import java.awt.Component; -import java.io.IOException; import javax.swing.JLabel; import javax.swing.JTable; @@ -55,15 +54,9 @@ public Component getTableCellRendererComponent(JTable jtPolicyInformation, Objec PolicyInformation policyInformation = (PolicyInformation) value; - try { - String policyInformationStr = PolicyInformationUtil.toString(policyInformation); - cell.setText(policyInformationStr); - cell.setToolTipText(policyInformationStr); - } catch (IOException ex) { - throw new RuntimeException(ex); // We build this data so should not - // happen - } - + String policyInformationStr = PolicyInformationUtil.toString(policyInformation); + cell.setText(policyInformationStr); + cell.setToolTipText(policyInformationStr); cell.setHorizontalAlignment(LEFT); cell.setBorder(new EmptyBorder(0, 5, 0, 5)); diff --git a/kse/src/main/java/org/kse/gui/crypto/policyinformation/PolicyInformationTableModel.java b/kse/src/main/java/org/kse/gui/crypto/policyinformation/PolicyInformationTableModel.java index ce7d338b5..ee3d444ec 100644 --- a/kse/src/main/java/org/kse/gui/crypto/policyinformation/PolicyInformationTableModel.java +++ b/kse/src/main/java/org/kse/gui/crypto/policyinformation/PolicyInformationTableModel.java @@ -19,20 +19,20 @@ */ package org.kse.gui.crypto.policyinformation; -import java.io.IOException; -import java.util.Arrays; +import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.ResourceBundle; import org.bouncycastle.asn1.x509.PolicyInformation; import org.kse.crypto.x509.PolicyInformationUtil; +import org.kse.gui.table.LoadableTableModel; import org.kse.gui.table.ToolTipTableModel; /** * The table model used to display policy information. */ -public class PolicyInformationTableModel extends ToolTipTableModel { +public class PolicyInformationTableModel extends ToolTipTableModel implements LoadableTableModel { private static final long serialVersionUID = 1L; private static ResourceBundle res = ResourceBundle.getBundle("org/kse/gui/crypto/policyinformation/resources"); @@ -42,7 +42,7 @@ public class PolicyInformationTableModel extends ToolTipTableModel { }; private String[] columnNames; - private Object[][] data; + private List data; /** * Construct a new PolicyInformationTableModel. @@ -52,7 +52,7 @@ public PolicyInformationTableModel() { columnNames = new String[1]; columnNames[0] = res.getString("PolicyInformationTableModel.PolicyInformationColumn"); - data = new Object[0][0]; + data = new ArrayList<>(); } /** @@ -60,18 +60,11 @@ public PolicyInformationTableModel() { * * @param policyInformation The policy information */ + @Override public void load(List policyInformation) { - PolicyInformation[] policyInformationArray = policyInformation.toArray( - PolicyInformation[]::new); - Arrays.sort(policyInformationArray, new PolicyInformationComparator()); - - data = new Object[policyInformation.size()][1]; - int i = 0; - for (PolicyInformation policyInfo : policyInformationArray) { - data[i][0] = policyInfo; - i++; - } + data = policyInformation; + data.sort(new PolicyInformationComparator()); fireTableDataChanged(); } @@ -93,7 +86,7 @@ public int getColumnCount() { */ @Override public int getRowCount() { - return data.length; + return data.size(); } /** @@ -116,7 +109,7 @@ public String getColumnName(int col) { */ @Override public Object getValueAt(int row, int col) { - return data[row][col]; + return data.get(row); } /** @@ -145,13 +138,8 @@ public boolean isCellEditable(int row, int col) { static class PolicyInformationComparator implements Comparator { @Override public int compare(PolicyInformation policyInformation1, PolicyInformation policyInformation2) { - try { - return PolicyInformationUtil.toString(policyInformation1) - .compareToIgnoreCase(PolicyInformationUtil.toString(policyInformation2)); - } catch (IOException ex) { - throw new RuntimeException(ex); // We build this data so should - // not happen - } + return PolicyInformationUtil.toString(policyInformation1) + .compareToIgnoreCase(PolicyInformationUtil.toString(policyInformation2)); } } } diff --git a/kse/src/main/java/org/kse/gui/crypto/policyinformation/PolicyQualifierInfoTableCellRend.java b/kse/src/main/java/org/kse/gui/crypto/policyinformation/PolicyQualifierInfoTableCellRend.java index 7d722fb4b..43782006a 100644 --- a/kse/src/main/java/org/kse/gui/crypto/policyinformation/PolicyQualifierInfoTableCellRend.java +++ b/kse/src/main/java/org/kse/gui/crypto/policyinformation/PolicyQualifierInfoTableCellRend.java @@ -20,7 +20,6 @@ package org.kse.gui.crypto.policyinformation; import java.awt.Component; -import java.io.IOException; import javax.swing.JLabel; import javax.swing.JTable; @@ -55,15 +54,9 @@ public Component getTableCellRendererComponent(JTable jtPolicyQualifierInfo, Obj PolicyQualifierInfo policyQualifierInfo = (PolicyQualifierInfo) value; - try { - String policyQualifierInfoStr = PolicyInformationUtil.toString(policyQualifierInfo); - cell.setText(policyQualifierInfoStr); - cell.setToolTipText(policyQualifierInfoStr); - } catch (IOException ex) { - throw new RuntimeException(ex); // We build this data so should not - // happen - } - + String policyQualifierInfoStr = PolicyInformationUtil.toString(policyQualifierInfo); + cell.setText(policyQualifierInfoStr); + cell.setToolTipText(policyQualifierInfoStr); cell.setHorizontalAlignment(LEFT); cell.setBorder(new EmptyBorder(0, 5, 0, 5)); diff --git a/kse/src/main/java/org/kse/gui/crypto/policyinformation/PolicyQualifierInfoTableModel.java b/kse/src/main/java/org/kse/gui/crypto/policyinformation/PolicyQualifierInfoTableModel.java index 6b7d65a21..638c5d6f3 100644 --- a/kse/src/main/java/org/kse/gui/crypto/policyinformation/PolicyQualifierInfoTableModel.java +++ b/kse/src/main/java/org/kse/gui/crypto/policyinformation/PolicyQualifierInfoTableModel.java @@ -19,20 +19,20 @@ */ package org.kse.gui.crypto.policyinformation; -import java.io.IOException; -import java.util.Arrays; +import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.ResourceBundle; import org.bouncycastle.asn1.x509.PolicyQualifierInfo; import org.kse.crypto.x509.PolicyInformationUtil; +import org.kse.gui.table.LoadableTableModel; import org.kse.gui.table.ToolTipTableModel; /** * The table model used to display policy qualifier info. */ -public class PolicyQualifierInfoTableModel extends ToolTipTableModel { +public class PolicyQualifierInfoTableModel extends ToolTipTableModel implements LoadableTableModel { private static final long serialVersionUID = 1L; private static ResourceBundle res = ResourceBundle.getBundle("org/kse/gui/crypto/policyinformation/resources"); @@ -42,7 +42,7 @@ public class PolicyQualifierInfoTableModel extends ToolTipTableModel { }; private String[] columnNames; - private Object[][] data; + private List data; /** * Construct a new PolicyQualifierInfoTableModel. @@ -52,7 +52,7 @@ public PolicyQualifierInfoTableModel() { columnNames = new String[1]; columnNames[0] = res.getString("PolicyQualifierInfoTableModel.PolicyQualifierInfoColumn"); - data = new Object[0][0]; + data = new ArrayList<>(); } /** @@ -60,18 +60,10 @@ public PolicyQualifierInfoTableModel() { * * @param policyQualifierInfo The policy qualifier info */ + @Override public void load(List policyQualifierInfo) { - PolicyQualifierInfo[] policyQualifierInfoArray = policyQualifierInfo.toArray( - PolicyQualifierInfo[]::new); - Arrays.sort(policyQualifierInfoArray, new PolicyQualifierInfoComparator()); - - data = new Object[policyQualifierInfoArray.length][1]; - - int i = 0; - for (PolicyQualifierInfo policyQualInfo : policyQualifierInfoArray) { - data[i][0] = policyQualInfo; - i++; - } + data = policyQualifierInfo; + data.sort(new PolicyQualifierInfoComparator()); fireTableDataChanged(); } @@ -93,7 +85,7 @@ public int getColumnCount() { */ @Override public int getRowCount() { - return data.length; + return data.size(); } /** @@ -116,7 +108,7 @@ public String getColumnName(int col) { */ @Override public Object getValueAt(int row, int col) { - return data[row][col]; + return data.get(row); } /** @@ -145,13 +137,8 @@ public boolean isCellEditable(int row, int col) { static class PolicyQualifierInfoComparator implements Comparator { @Override public int compare(PolicyQualifierInfo policyQualifierInfo1, PolicyQualifierInfo policyQualifierInfo2) { - try { - return PolicyInformationUtil.toString(policyQualifierInfo1) - .compareToIgnoreCase(PolicyInformationUtil.toString(policyQualifierInfo2)); - } catch (IOException ex) { - throw new RuntimeException(ex); // We build this data so should - // not happen - } + return PolicyInformationUtil.toString(policyQualifierInfo1) + .compareToIgnoreCase(PolicyInformationUtil.toString(policyQualifierInfo2)); } } } diff --git a/kse/src/main/java/org/kse/gui/crypto/policymapping/JPolicyMappings.java b/kse/src/main/java/org/kse/gui/crypto/policymapping/JPolicyMappings.java index f1938da80..5c9979404 100644 --- a/kse/src/main/java/org/kse/gui/crypto/policymapping/JPolicyMappings.java +++ b/kse/src/main/java/org/kse/gui/crypto/policymapping/JPolicyMappings.java @@ -19,59 +19,34 @@ */ package org.kse.gui.crypto.policymapping; -import java.awt.BorderLayout; import java.awt.Container; -import java.awt.Dimension; -import java.awt.Insets; -import java.awt.Point; -import java.awt.Toolkit; -import java.awt.event.KeyAdapter; -import java.awt.event.KeyEvent; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; +import java.util.ArrayList; +import java.util.List; import java.util.ResourceBundle; -import javax.swing.Box; -import javax.swing.BoxLayout; -import javax.swing.ImageIcon; -import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; -import javax.swing.JPanel; -import javax.swing.JScrollPane; import javax.swing.JTable; -import javax.swing.ListSelectionModel; -import javax.swing.ScrollPaneConstants; import javax.swing.table.TableColumn; import javax.swing.table.TableRowSorter; -import org.bouncycastle.asn1.x509.CertPolicyId; +import org.bouncycastle.asn1.ASN1Encodable; +import org.bouncycastle.asn1.ASN1Sequence; import org.bouncycastle.asn1.x509.PolicyMappings; import org.kse.crypto.x509.PolicyMapping; import org.kse.crypto.x509.PolicyMappingsUtil; -import org.kse.gui.CursorUtil; -import org.kse.gui.PlatformUtil; +import org.kse.gui.crypto.JAddEditRemovePanel; import org.kse.gui.table.ToolTipTable; -import org.kse.utilities.os.OperatingSystem; /** * Component to edit a set of policy mappings. */ -public class JPolicyMappings extends JPanel { +public class JPolicyMappings extends JAddEditRemovePanel, PolicyMapping> { private static final long serialVersionUID = 1L; private static ResourceBundle res = ResourceBundle.getBundle("org/kse/gui/crypto/policymapping/resources"); - private JPanel jpPolicyMappingButtons; - private JButton jbAdd; - private JButton jbEdit; - private JButton jbRemove; - private JScrollPane jspPolicyMappings; - private JTable jtPolicyMappings; - private String title; - private PolicyMappings policyMappings; - private boolean enabled = true; /** * Construct a JPolicyMappings. @@ -80,71 +55,37 @@ public class JPolicyMappings extends JPanel { */ public JPolicyMappings(String title) { this.title = title; - initComponents(); } - private void initComponents() { - jbAdd = new JButton(new ImageIcon( - Toolkit.getDefaultToolkit().createImage(getClass().getResource("images/add_policy_map.png")))); - jbAdd.setMargin(new Insets(2, 2, 0, 0)); - jbAdd.setToolTipText(res.getString("JPolicyMappings.jbAdd.tooltip")); - jbAdd.setMnemonic(res.getString("JPolicyMappings.jbAdd.mnemonic").charAt(0)); - - jbAdd.addActionListener(evt -> { - try { - CursorUtil.setCursorBusy(JPolicyMappings.this); - addPressed(); - } finally { - CursorUtil.setCursorFree(JPolicyMappings.this); - } - }); - - jbEdit = new JButton(new ImageIcon( - Toolkit.getDefaultToolkit().createImage(getClass().getResource("images/edit_policy_map.png")))); - jbEdit.setMargin(new Insets(2, 2, 0, 0)); - jbEdit.setToolTipText(res.getString("JPolicyMappings.jbEdit.tooltip")); - jbEdit.setMnemonic(res.getString("JPolicyMappings.jbEdit.mnemonic").charAt(0)); - - jbEdit.setEnabled(false); - - jbEdit.addActionListener(evt -> { - try { - CursorUtil.setCursorBusy(JPolicyMappings.this); - editPressed(); - } finally { - CursorUtil.setCursorFree(JPolicyMappings.this); - } - }); + @Override + protected String getAddResource() { + return "images/add_policy_map.png"; + } - jbRemove = new JButton(new ImageIcon( - Toolkit.getDefaultToolkit().createImage(getClass().getResource("images/remove_policy_map.png")))); - jbRemove.setMargin(new Insets(2, 2, 0, 0)); - jbRemove.setToolTipText(res.getString("JPolicyMappings.jbRemove.tooltip")); - jbRemove.setMnemonic(res.getString("JPolicyMappings.jbRemove.mnemonic").charAt(0)); + @Override + protected String getEditResource() { + return "images/edit_policy_map.png"; + } - jbRemove.setEnabled(false); + @Override + protected String getRemoveResource() { + return "images/remove_policy_map.png"; + } - jbRemove.addActionListener(evt -> { - try { - CursorUtil.setCursorBusy(JPolicyMappings.this); - removePressed(); - } finally { - CursorUtil.setCursorFree(JPolicyMappings.this); - } - }); + @Override + protected String getBundleString(String suffix) { + return res.getString("JPolicyMappings." + suffix); + } - jpPolicyMappingButtons = new JPanel(); - jpPolicyMappingButtons.setLayout(new BoxLayout(jpPolicyMappingButtons, BoxLayout.Y_AXIS)); - jpPolicyMappingButtons.add(Box.createVerticalGlue()); - jpPolicyMappingButtons.add(jbAdd); - jpPolicyMappingButtons.add(Box.createVerticalStrut(3)); - jpPolicyMappingButtons.add(jbEdit); - jpPolicyMappingButtons.add(Box.createVerticalStrut(3)); - jpPolicyMappingButtons.add(jbRemove); - jpPolicyMappingButtons.add(Box.createVerticalGlue()); + @Override + protected List newCollection() { + return new ArrayList<>(); + } + @Override + protected JTable newTable() { PolicyMappingsTableModel policyMappingsTableModel = new PolicyMappingsTableModel(); - jtPolicyMappings = new ToolTipTable(policyMappingsTableModel); + JTable jtPolicyMappings = new ToolTipTable(policyMappingsTableModel); TableRowSorter sorter = new TableRowSorter<>(policyMappingsTableModel); sorter.setComparator(0, new PolicyMappingsTableModel.IssuerDomainPolicyComparator()); @@ -163,71 +104,24 @@ private void initComponents() { column.setCellRenderer(new PolicyMappingsTableCellRend()); } - ListSelectionModel selectionModel = jtPolicyMappings.getSelectionModel(); - selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); - selectionModel.addListSelectionListener(evt -> { - if (!evt.getValueIsAdjusting()) { - updateButtonControls(); - } - }); - - jtPolicyMappings.addMouseListener(new MouseAdapter() { - @Override - public void mouseClicked(MouseEvent evt) { - maybeEditPolicyMapping(evt); - } - }); - - jtPolicyMappings.addKeyListener(new KeyAdapter() { - boolean deleteLastPressed = false; - - @Override - public void keyPressed(KeyEvent evt) { - // Record delete pressed on non-Macs - if (!OperatingSystem.isMacOs()) { - deleteLastPressed = evt.getKeyCode() == KeyEvent.VK_DELETE; - } - } - - @Override - public void keyReleased(KeyEvent evt) { - // Delete on non-Mac if delete was pressed and is now released - if (!OperatingSystem.isMacOs() && deleteLastPressed && evt.getKeyCode() == KeyEvent.VK_DELETE) { - try { - CursorUtil.setCursorBusy(JPolicyMappings.this); - deleteLastPressed = false; - removeSelectedPolicyMapping(); - } finally { - CursorUtil.setCursorFree(JPolicyMappings.this); - } - } - } + return jtPolicyMappings; + } - @Override - public void keyTyped(KeyEvent evt) { - // Delete on Mac if backspace typed - if (OperatingSystem.isMacOs() && evt.getKeyChar() == 0x08) { - try { - CursorUtil.setCursorBusy(JPolicyMappings.this); - removeSelectedPolicyMapping(); - } finally { - CursorUtil.setCursorFree(JPolicyMappings.this); - } - } - } - }); + @Override + protected PolicyMapping getItem(PolicyMapping item) { + Container container = getTopLevelAncestor(); - jspPolicyMappings = PlatformUtil.createScrollPane(jtPolicyMappings, - ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, - ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); - jspPolicyMappings.getViewport().setBackground(jtPolicyMappings.getBackground()); + DPolicyMappingChooser dPolicyMappingChooser = null; - this.setLayout(new BorderLayout(5, 5)); - setPreferredSize(new Dimension(400, 150)); - add(jspPolicyMappings, BorderLayout.CENTER); - add(jpPolicyMappingButtons, BorderLayout.EAST); + if (container instanceof JDialog) { + dPolicyMappingChooser = new DPolicyMappingChooser((JDialog) container, title, item); + } else { + dPolicyMappingChooser = new DPolicyMappingChooser((JFrame) container, title, item); + } + dPolicyMappingChooser.setLocationRelativeTo(container); + dPolicyMappingChooser.setVisible(true); - populate(); + return dPolicyMappingChooser.getPolicyMapping(); } /** @@ -236,7 +130,7 @@ public void keyTyped(KeyEvent evt) { * @return Policy mappings */ public PolicyMappings getPolicyMappings() { - return policyMappings; + return PolicyMappingsUtil.createFromList(getItems()); } /** @@ -245,181 +139,16 @@ public PolicyMappings getPolicyMappings() { * @param policyMappings Policy mappings */ public void setPolicyMappings(PolicyMappings policyMappings) { - this.policyMappings = policyMappings; - populate(); - } - - /** - * Sets whether or not the component is enabled. - * - * @param enabled True if this component should be enabled, false otherwise - */ - @Override - public void setEnabled(boolean enabled) { - this.enabled = enabled; - - updateButtonControls(); - } - - /** - * Set component's tooltip text. - * - * @param toolTipText Tooltip text - */ - @Override - public void setToolTipText(String toolTipText) { - super.setToolTipText(toolTipText); - jspPolicyMappings.setToolTipText(toolTipText); - jtPolicyMappings.setToolTipText(toolTipText); - } - - private void populate() { - if (policyMappings == null) { - policyMappings = new PolicyMappings(new CertPolicyId[0], new CertPolicyId[0]); - } - - reloadPolicyMappingsTable(); - selectFirstPolicyMappingInTable(); - updateButtonControls(); - } - - private void addPressed() { - Container container = getTopLevelAncestor(); - - DPolicyMappingChooser dPolicyMappingChooser = null; - - if (container instanceof JDialog) { - dPolicyMappingChooser = new DPolicyMappingChooser((JDialog) container, title, null); - } else { - dPolicyMappingChooser = new DPolicyMappingChooser((JFrame) container, title, null); - } - dPolicyMappingChooser.setLocationRelativeTo(container); - dPolicyMappingChooser.setVisible(true); - - PolicyMapping newPolicyMapping = dPolicyMappingChooser.getPolicyMapping(); - - if (newPolicyMapping == null) { - return; - } - - policyMappings = PolicyMappingsUtil.add(newPolicyMapping, policyMappings); - - populate(); - selectPolicyMappingInTable(newPolicyMapping); - } - - private void removePressed() { - removeSelectedPolicyMapping(); - } - - private void removeSelectedPolicyMapping() { - int selectedRow = jtPolicyMappings.getSelectedRow(); - - if (selectedRow != -1) { - PolicyMapping policyMapping = (PolicyMapping) jtPolicyMappings.getValueAt(selectedRow, 0); - - policyMappings = PolicyMappingsUtil.remove(policyMapping, policyMappings); - - reloadPolicyMappingsTable(); - selectFirstPolicyMappingInTable(); - updateButtonControls(); - } - } - - private void editPressed() { - editSelectedPolicyMapping(); - } - - private void maybeEditPolicyMapping(MouseEvent evt) { - if (evt.getClickCount() > 1) { - Point point = new Point(evt.getX(), evt.getY()); - int row = jtPolicyMappings.rowAtPoint(point); - - if (row != -1) { - try { - CursorUtil.setCursorBusy(JPolicyMappings.this); - jtPolicyMappings.setRowSelectionInterval(row, row); - editSelectedPolicyMapping(); - } finally { - CursorUtil.setCursorFree(JPolicyMappings.this); - } - } - } - } - - private void updateButtonControls() { - if (!enabled) { - jbAdd.setEnabled(false); - jbEdit.setEnabled(false); - jbRemove.setEnabled(false); - } else { - jbAdd.setEnabled(true); + ASN1Sequence policyMappingsSeq = (ASN1Sequence) policyMappings.toASN1Primitive(); - int selectedRow = jtPolicyMappings.getSelectedRow(); - - if (selectedRow == -1) { - jbEdit.setEnabled(false); - jbRemove.setEnabled(false); - } else { - jbEdit.setEnabled(true); - jbRemove.setEnabled(true); - } + // convert and sort + ASN1Encodable[] asn1EncArray = policyMappingsSeq.toArray(); + List policyMappingsArray = new ArrayList<>(); + for (int i = 0; i < asn1EncArray.length; i++) { + policyMappingsArray.add(PolicyMapping.getInstance(asn1EncArray[i])); } - } - - private void editSelectedPolicyMapping() { - int selectedRow = jtPolicyMappings.getSelectedRow(); - - if (selectedRow != -1) { - PolicyMapping policyMapping = (PolicyMapping) jtPolicyMappings.getValueAt(selectedRow, 0); - - Container container = getTopLevelAncestor(); - - DPolicyMappingChooser dPolicyMappingChooser = null; - - if (container instanceof JDialog) { - dPolicyMappingChooser = new DPolicyMappingChooser((JDialog) container, title, policyMapping); - } else { - dPolicyMappingChooser = new DPolicyMappingChooser((JFrame) container, title, policyMapping); - } - dPolicyMappingChooser.setLocationRelativeTo(container); - dPolicyMappingChooser.setVisible(true); - - PolicyMapping newPolicyMapping = dPolicyMappingChooser.getPolicyMapping(); - - if (newPolicyMapping == null) { - return; - } - - policyMappings = PolicyMappingsUtil.remove(policyMapping, policyMappings); - policyMappings = PolicyMappingsUtil.add(newPolicyMapping, policyMappings); - - populate(); - selectPolicyMappingInTable(newPolicyMapping); - } - } - - private void selectPolicyMappingInTable(PolicyMapping policyMapping) { - for (int i = 0; i < jtPolicyMappings.getRowCount(); i++) { - if (policyMapping.equals(jtPolicyMappings.getValueAt(i, 0))) { - jtPolicyMappings.changeSelection(i, 0, false, false); - return; - } - } - } - - private void reloadPolicyMappingsTable() { - getPolicyMappingsTableModel().load(policyMappings); - } - - private void selectFirstPolicyMappingInTable() { - if (getPolicyMappingsTableModel().getRowCount() > 0) { - jtPolicyMappings.changeSelection(0, 0, false, false); - } - } - private PolicyMappingsTableModel getPolicyMappingsTableModel() { - return (PolicyMappingsTableModel) jtPolicyMappings.getModel(); + setItems(policyMappingsArray); } } diff --git a/kse/src/main/java/org/kse/gui/crypto/policymapping/PolicyMappingsTableModel.java b/kse/src/main/java/org/kse/gui/crypto/policymapping/PolicyMappingsTableModel.java index b6ff12078..c261ee63a 100644 --- a/kse/src/main/java/org/kse/gui/crypto/policymapping/PolicyMappingsTableModel.java +++ b/kse/src/main/java/org/kse/gui/crypto/policymapping/PolicyMappingsTableModel.java @@ -19,21 +19,21 @@ */ package org.kse.gui.crypto.policymapping; -import java.util.Arrays; +import java.util.ArrayList; import java.util.Comparator; +import java.util.List; import java.util.ResourceBundle; -import org.bouncycastle.asn1.ASN1Encodable; -import org.bouncycastle.asn1.ASN1Sequence; import org.bouncycastle.asn1.x509.PolicyMappings; import org.kse.crypto.x509.PolicyMapping; +import org.kse.gui.table.LoadableTableModel; import org.kse.gui.table.ToolTipTableModel; import org.kse.utilities.oid.ObjectIdComparator; /** * The table model used to display policy mappings. */ -public class PolicyMappingsTableModel extends ToolTipTableModel { +public class PolicyMappingsTableModel extends ToolTipTableModel implements LoadableTableModel { private static final long serialVersionUID = 1L; private static ResourceBundle res = ResourceBundle.getBundle("org/kse/gui/crypto/policymapping/resources"); private static ObjectIdComparator objectIdComparator = new ObjectIdComparator(); @@ -44,7 +44,7 @@ public class PolicyMappingsTableModel extends ToolTipTableModel { }; private String[] columnNames; - private Object[][] data; + private List data; /** * Construct a new PolicyMappingsTableModel. @@ -55,7 +55,7 @@ public PolicyMappingsTableModel() { columnNames[0] = res.getString("PolicyMappingsTableModel.IssuerDomainPolicyColumn"); columnNames[1] = res.getString("PolicyMappingsTableModel.SubjectDomainPolicyColumn"); - data = new Object[0][0]; + data = new ArrayList<>(); } /** @@ -63,26 +63,11 @@ public PolicyMappingsTableModel() { * * @param policyMappings The policy mappings */ - public void load(PolicyMappings policyMappings) { - - ASN1Sequence policyMappingsSeq = (ASN1Sequence) policyMappings.toASN1Primitive(); - - // convert and sort - ASN1Encodable[] asn1EncArray = policyMappingsSeq.toArray(); - PolicyMapping[] policyMappingsArray = new PolicyMapping[asn1EncArray.length]; - for (int i = 0; i < asn1EncArray.length; i++) { - policyMappingsArray[i] = PolicyMapping.getInstance(asn1EncArray[i]); - } - Arrays.sort(policyMappingsArray, new IssuerDomainPolicyComparator()); - - data = new Object[policyMappingsArray.length][2]; + @Override + public void load(List policyMappings) { - int i = 0; - for (PolicyMapping policyMapping : policyMappingsArray) { - data[i][0] = policyMapping; - data[i][1] = policyMapping; - i++; - } + data = policyMappings; + data.sort(new IssuerDomainPolicyComparator()); fireTableDataChanged(); } @@ -104,7 +89,7 @@ public int getColumnCount() { */ @Override public int getRowCount() { - return data.length; + return data.size(); } /** @@ -127,7 +112,7 @@ public String getColumnName(int col) { */ @Override public Object getValueAt(int row, int col) { - return data[row][col]; + return data.get(row); } /** diff --git a/kse/src/main/java/org/kse/gui/datetime/JDateTime.java b/kse/src/main/java/org/kse/gui/datetime/JDateTime.java index b0c79b706..73c1e1055 100644 --- a/kse/src/main/java/org/kse/gui/datetime/JDateTime.java +++ b/kse/src/main/java/org/kse/gui/datetime/JDateTime.java @@ -73,6 +73,7 @@ public JDateTime(String title, boolean showClearButton) { initComponents(); } + // TODO JW - miglayout private void initComponents() { jtfDateTime = new JTextField(18); jtfDateTime.setEditable(false); diff --git a/kse/src/main/java/org/kse/gui/dialogs/extensions/DAuthorityInformationAccess.java b/kse/src/main/java/org/kse/gui/dialogs/extensions/DAuthorityInformationAccess.java index d9aadc708..02ff4677b 100644 --- a/kse/src/main/java/org/kse/gui/dialogs/extensions/DAuthorityInformationAccess.java +++ b/kse/src/main/java/org/kse/gui/dialogs/extensions/DAuthorityInformationAccess.java @@ -83,9 +83,8 @@ public DAuthorityInformationAccess(JDialog parent) { * * @param parent The parent dialog * @param value Authority Information Access DER-encoded - * @throws IOException If value could not be decoded */ - public DAuthorityInformationAccess(JDialog parent, byte[] value) throws IOException { + public DAuthorityInformationAccess(JDialog parent, byte[] value) { super(parent); setTitle(res.getString("DAuthorityInformationAccess.Title")); initComponents(); @@ -136,17 +135,17 @@ public void windowClosing(WindowEvent evt) { pack(); } - private void prepopulateWithValue(byte[] value) throws IOException { + private void prepopulateWithValue(byte[] value) { AuthorityInformationAccess authorityInformationAccess = AuthorityInformationAccess.getInstance(value); List accessDescriptionList = new ArrayList<>( Arrays.asList(authorityInformationAccess.getAccessDescriptions())); - jadAccessDescriptions.setAccessDescriptions(accessDescriptionList); + jadAccessDescriptions.setItems(accessDescriptionList); } private void okPressed() { - List accessDescriptions = jadAccessDescriptions.getAccessDescriptions(); + List accessDescriptions = jadAccessDescriptions.getItems(); if (accessDescriptions.isEmpty()) { JOptionPane.showMessageDialog(this, res.getString("DAuthorityInformationAccess.ValueReq.message"), diff --git a/kse/src/main/java/org/kse/gui/dialogs/extensions/DCertificatePolicies.java b/kse/src/main/java/org/kse/gui/dialogs/extensions/DCertificatePolicies.java index 732889cf7..c162ccda0 100644 --- a/kse/src/main/java/org/kse/gui/dialogs/extensions/DCertificatePolicies.java +++ b/kse/src/main/java/org/kse/gui/dialogs/extensions/DCertificatePolicies.java @@ -83,9 +83,8 @@ public DCertificatePolicies(JDialog parent) { * * @param parent The parent dialog * @param value Certificate Policies DER-encoded - * @throws IOException If value could not be decoded */ - public DCertificatePolicies(JDialog parent, byte[] value) throws IOException { + public DCertificatePolicies(JDialog parent, byte[] value) { super(parent); setTitle(res.getString("DCertificatePolicies.Title")); initComponents(); @@ -136,17 +135,17 @@ public void windowClosing(WindowEvent evt) { pack(); } - private void prepopulateWithValue(byte[] value) throws IOException { + private void prepopulateWithValue(byte[] value) { CertificatePolicies certificatePolicies = CertificatePolicies.getInstance(value); - List accessDescriptionList = new ArrayList<>( + List policyInformation = new ArrayList<>( Arrays.asList(certificatePolicies.getPolicyInformation())); - jpiCertificatePolicies.setPolicyInformation(accessDescriptionList); + jpiCertificatePolicies.setItems(policyInformation); } private void okPressed() { - List policyInformation = jpiCertificatePolicies.getPolicyInformation(); + List policyInformation = jpiCertificatePolicies.getItems(); if (policyInformation.isEmpty()) { JOptionPane.showMessageDialog(this, res.getString("DCertificatePolicies.ValueReq.message"), getTitle(), diff --git a/kse/src/main/java/org/kse/gui/dialogs/extensions/DCrlDistributionPoints.java b/kse/src/main/java/org/kse/gui/dialogs/extensions/DCrlDistributionPoints.java index a2a495007..748e542c6 100644 --- a/kse/src/main/java/org/kse/gui/dialogs/extensions/DCrlDistributionPoints.java +++ b/kse/src/main/java/org/kse/gui/dialogs/extensions/DCrlDistributionPoints.java @@ -27,6 +27,8 @@ import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; import java.util.ResourceBundle; import javax.swing.AbstractAction; @@ -41,6 +43,7 @@ import org.bouncycastle.asn1.ASN1Encoding; import org.bouncycastle.asn1.x509.CRLDistPoint; +import org.bouncycastle.asn1.x509.DistributionPoint; import org.kse.crypto.x509.X509ExtensionType; import org.kse.gui.PlatformUtil; import org.kse.gui.crypto.distributionpoints.JDistributionPoints; @@ -84,9 +87,8 @@ public DCrlDistributionPoints(JDialog parent) { * * @param parent The parent dialog * @param value CRL distribution points DER-encoded - * @throws IOException If value could not be decoded */ - public DCrlDistributionPoints(JDialog parent, byte[] value) throws IOException { + public DCrlDistributionPoints(JDialog parent, byte[] value) { super(parent); setTitle(res.getString("DCrlDistributionPoints.Title")); initComponents(); @@ -140,17 +142,19 @@ public void windowClosing(WindowEvent evt) { pack(); } - private void prepopulateWithValue(byte[] value) throws IOException { + private void prepopulateWithValue(byte[] value) { CRLDistPoint cRLDistPoint = CRLDistPoint.getInstance(value); if (cRLDistPoint != null) { - jdpDistributionPoints.setCRLDistPoint(cRLDistPoint); + DistributionPoint[] distributionPointArray = cRLDistPoint.getDistributionPoints(); + jdpDistributionPoints.setItems(new ArrayList<>(Arrays.asList(distributionPointArray))); } } private void okPressed() { - CRLDistPoint cRLDistPoint = jdpDistributionPoints.getCRLDistPoint(); + CRLDistPoint cRLDistPoint = new CRLDistPoint( + jdpDistributionPoints.getItems().toArray(DistributionPoint[]::new)); if (cRLDistPoint.getDistributionPoints().length == 0) { JOptionPane.showMessageDialog(this, res.getString("DCrlDistributionPoints.ValueReq.message"), getTitle(), JOptionPane.WARNING_MESSAGE); diff --git a/kse/src/main/java/org/kse/gui/dialogs/extensions/DCustomExtKeyUsage.java b/kse/src/main/java/org/kse/gui/dialogs/extensions/DCustomExtKeyUsage.java index bb4cdbb8b..785fe1731 100644 --- a/kse/src/main/java/org/kse/gui/dialogs/extensions/DCustomExtKeyUsage.java +++ b/kse/src/main/java/org/kse/gui/dialogs/extensions/DCustomExtKeyUsage.java @@ -122,11 +122,11 @@ public void windowClosing(WindowEvent evt) { private void prepopulateWithOidList(Set customExtKeyUsageOids) { Set oids = new HashSet<>(customExtKeyUsageOids); - jCustomExtendedKeyUsage.setCustomExtKeyUsages(oids); + jCustomExtendedKeyUsage.setItems(oids); } private void okPressed() { - Set objectIds = jCustomExtendedKeyUsage.getCustomExtKeyUsages(); + Set objectIds = jCustomExtendedKeyUsage.getItems(); if (objectIds.isEmpty()) { JOptionPane.showMessageDialog(this, res.getString("DCustomExtendedKeyUsage.ValueReq.message"), getTitle(), JOptionPane.WARNING_MESSAGE); diff --git a/kse/src/main/java/org/kse/gui/dialogs/extensions/DPolicyMappings.java b/kse/src/main/java/org/kse/gui/dialogs/extensions/DPolicyMappings.java index 5ba337762..2862f6d85 100644 --- a/kse/src/main/java/org/kse/gui/dialogs/extensions/DPolicyMappings.java +++ b/kse/src/main/java/org/kse/gui/dialogs/extensions/DPolicyMappings.java @@ -78,9 +78,8 @@ public DPolicyMappings(JDialog parent) { * * @param parent The parent dialog * @param value Policy Mappings DER-encoded - * @throws IOException If value could not be decoded */ - public DPolicyMappings(JDialog parent, byte[] value) throws IOException { + public DPolicyMappings(JDialog parent, byte[] value) { super(parent); setTitle(res.getString("DPolicyMappings.Title")); initComponents(); @@ -130,7 +129,7 @@ public void windowClosing(WindowEvent evt) { pack(); } - private void prepopulateWithValue(byte[] value) throws IOException { + private void prepopulateWithValue(byte[] value) { PolicyMappings policyMappings = PolicyMappings.getInstance(value); jpmPolicyMappings.setPolicyMappings(policyMappings); diff --git a/kse/src/main/java/org/kse/gui/dialogs/extensions/DSubjectInformationAccess.java b/kse/src/main/java/org/kse/gui/dialogs/extensions/DSubjectInformationAccess.java index c2a1a8025..21ff10769 100644 --- a/kse/src/main/java/org/kse/gui/dialogs/extensions/DSubjectInformationAccess.java +++ b/kse/src/main/java/org/kse/gui/dialogs/extensions/DSubjectInformationAccess.java @@ -25,6 +25,7 @@ import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.IOException; +import java.util.ArrayList; import java.util.List; import java.util.ResourceBundle; @@ -79,9 +80,8 @@ public DSubjectInformationAccess(JDialog parent) { * * @param parent The parent dialog * @param value Subject Information Access DER-encoded - * @throws IOException If value could not be decoded */ - public DSubjectInformationAccess(JDialog parent, byte[] value) throws IOException { + public DSubjectInformationAccess(JDialog parent, byte[] value) { super(parent); setTitle(res.getString("DSubjectInformationAccess.Title")); initComponents(); @@ -132,14 +132,14 @@ public void windowClosing(WindowEvent evt) { pack(); } - private void prepopulateWithValue(byte[] value) throws IOException { + private void prepopulateWithValue(byte[] value) { SubjectInfoAccess subjectInformationAccess = SubjectInfoAccess.getInstance(value); - jadAccessDescriptions.setAccessDescriptions(subjectInformationAccess.getAccessDescriptionList()); + jadAccessDescriptions.setItems(new ArrayList<>(subjectInformationAccess.getAccessDescriptionList())); } private void okPressed() { - List accessDescriptions = jadAccessDescriptions.getAccessDescriptions(); + List accessDescriptions = jadAccessDescriptions.getItems(); if (accessDescriptions.isEmpty()) { JOptionPane.showMessageDialog(this, res.getString("DSubjectInformationAccess.ValueReq.message"), getTitle(), diff --git a/kse/src/main/java/org/kse/gui/table/LoadableTableModel.java b/kse/src/main/java/org/kse/gui/table/LoadableTableModel.java new file mode 100644 index 000000000..5f695ee18 --- /dev/null +++ b/kse/src/main/java/org/kse/gui/table/LoadableTableModel.java @@ -0,0 +1,38 @@ +/* + * Copyright 2004 - 2013 Wayne Grant + * 2013 - 2026 Kai Kramer + * + * This file is part of KeyStore Explorer. + * + * KeyStore Explorer is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * KeyStore Explorer is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with KeyStore Explorer. If not, see . + */ + +package org.kse.gui.table; + +import java.util.List; + +/** + * A table model that can be loaded using a List. + * + * @param the type of elements to be loaded. + */ +public interface LoadableTableModel { + + /** + * Loads the table model with the items. + * + * @param items The list of items to load. + */ + void load(List items); +} From 742ca970196c2b4c42073672da8ce6896d2ef269 Mon Sep 17 00:00:00 2001 From: jonwltn <86822083+jonwltn@users.noreply.github.com> Date: Wed, 23 Sep 2026 14:09:07 -0700 Subject: [PATCH 2/3] Migrated JGeneralName, JDateTime, JUserNotice to MiG layout. --- .../gui/crypto/generalname/JGeneralName.java | 35 ++-------- .../crypto/policyinformation/JUserNotice.java | 35 ++-------- .../java/org/kse/gui/datetime/JDateTime.java | 69 ++++++------------- 3 files changed, 33 insertions(+), 106 deletions(-) diff --git a/kse/src/main/java/org/kse/gui/crypto/generalname/JGeneralName.java b/kse/src/main/java/org/kse/gui/crypto/generalname/JGeneralName.java index 0287f8f8a..b5f144b65 100644 --- a/kse/src/main/java/org/kse/gui/crypto/generalname/JGeneralName.java +++ b/kse/src/main/java/org/kse/gui/crypto/generalname/JGeneralName.java @@ -20,9 +20,6 @@ package org.kse.gui.crypto.generalname; import java.awt.Container; -import java.awt.GridBagConstraints; -import java.awt.GridBagLayout; -import java.awt.Insets; import java.util.ResourceBundle; import javax.swing.ImageIcon; @@ -36,6 +33,8 @@ import org.kse.crypto.x509.GeneralNameUtil; import org.kse.gui.CursorUtil; +import net.miginfocom.swing.MigLayout; + /** * Component to edit a general name. */ @@ -61,18 +60,10 @@ public JGeneralName(String title) { initComponents(); } - // TODO JW - miglayout private void initComponents() { jtfGeneralName = new JTextField(40); jtfGeneralName.setEditable(false); - GridBagConstraints gbc_jtfGeneralName = new GridBagConstraints(); - gbc_jtfGeneralName.gridwidth = 1; - gbc_jtfGeneralName.gridheight = 1; - gbc_jtfGeneralName.gridx = 0; - gbc_jtfGeneralName.gridy = 0; - gbc_jtfGeneralName.insets = new Insets(0, 0, 0, 5); - ImageIcon editIcon = new ImageIcon(getClass().getResource("images/edit_general_nm.png")); jbEditGeneralName = new JButton(editIcon); jbEditGeneralName.setToolTipText(res.getString("JGeneralName.jbEditGeneralName.tooltip")); @@ -85,13 +76,6 @@ private void initComponents() { } }); - GridBagConstraints gbc_jbEditGeneralName = new GridBagConstraints(); - gbc_jbEditGeneralName.gridwidth = 1; - gbc_jbEditGeneralName.gridheight = 1; - gbc_jbEditGeneralName.gridx = 1; - gbc_jbEditGeneralName.gridy = 0; - gbc_jbEditGeneralName.insets = new Insets(0, 0, 0, 5); - ImageIcon clearIcon = new ImageIcon(getClass().getResource("images/clear_general_nm.png")); jbClearGeneralName = new JButton(clearIcon); jbClearGeneralName.setToolTipText(res.getString("JGeneralName.jbClearGeneralName.tooltip")); @@ -104,17 +88,10 @@ private void initComponents() { } }); - GridBagConstraints gbc_jbClearGeneralName = new GridBagConstraints(); - gbc_jbClearGeneralName.gridwidth = 1; - gbc_jbClearGeneralName.gridheight = 1; - gbc_jbClearGeneralName.gridx = 2; - gbc_jbClearGeneralName.gridy = 0; - gbc_jbClearGeneralName.insets = new Insets(0, 0, 0, 0); - - setLayout(new GridBagLayout()); - add(jtfGeneralName, gbc_jtfGeneralName); - add(jbEditGeneralName, gbc_jbEditGeneralName); - add(jbClearGeneralName, gbc_jbClearGeneralName); + setLayout(new MigLayout("insets 0, fill", "[]", "[]")); + add(jtfGeneralName, "growx, pushx"); + add(jbEditGeneralName, ""); + add(jbClearGeneralName, ""); populate(); } diff --git a/kse/src/main/java/org/kse/gui/crypto/policyinformation/JUserNotice.java b/kse/src/main/java/org/kse/gui/crypto/policyinformation/JUserNotice.java index 782e84f06..c5d692a98 100644 --- a/kse/src/main/java/org/kse/gui/crypto/policyinformation/JUserNotice.java +++ b/kse/src/main/java/org/kse/gui/crypto/policyinformation/JUserNotice.java @@ -20,9 +20,6 @@ package org.kse.gui.crypto.policyinformation; import java.awt.Container; -import java.awt.GridBagConstraints; -import java.awt.GridBagLayout; -import java.awt.Insets; import java.util.ResourceBundle; import javax.swing.ImageIcon; @@ -36,6 +33,8 @@ import org.kse.crypto.x509.PolicyInformationUtil; import org.kse.gui.CursorUtil; +import net.miginfocom.swing.MigLayout; + /** * Component to edit a user notice. */ @@ -61,18 +60,10 @@ public JUserNotice(String title) { initComponents(); } - // TODO JW - miglayout private void initComponents() { jtfUserNotice = new JTextField(20); jtfUserNotice.setEditable(false); - GridBagConstraints gbc_jtfUserNotice = new GridBagConstraints(); - gbc_jtfUserNotice.gridwidth = 1; - gbc_jtfUserNotice.gridheight = 1; - gbc_jtfUserNotice.gridx = 0; - gbc_jtfUserNotice.gridy = 0; - gbc_jtfUserNotice.insets = new Insets(0, 0, 0, 5); - ImageIcon editIcon = new ImageIcon(getClass().getResource("images/edit_user_notice.png")); jbEditUserNotice = new JButton(editIcon); jbEditUserNotice.setToolTipText(res.getString("JUserNotice.jbEditUserNotice.tooltip")); @@ -85,13 +76,6 @@ private void initComponents() { } }); - GridBagConstraints gbc_jbEditUserNotice = new GridBagConstraints(); - gbc_jbEditUserNotice.gridwidth = 1; - gbc_jbEditUserNotice.gridheight = 1; - gbc_jbEditUserNotice.gridx = 1; - gbc_jbEditUserNotice.gridy = 0; - gbc_jbEditUserNotice.insets = new Insets(0, 0, 0, 5); - ImageIcon clearIcon = new ImageIcon(getClass().getResource("images/clear_user_notice.png")); jbClearUserNotice = new JButton(clearIcon); jbClearUserNotice.setToolTipText(res.getString("JUserNotice.jbClearUserNotice.tooltip")); @@ -104,17 +88,10 @@ private void initComponents() { } }); - GridBagConstraints gbc_jbClearUserNotice = new GridBagConstraints(); - gbc_jbClearUserNotice.gridwidth = 1; - gbc_jbClearUserNotice.gridheight = 1; - gbc_jbClearUserNotice.gridx = 2; - gbc_jbClearUserNotice.gridy = 0; - gbc_jbClearUserNotice.insets = new Insets(0, 0, 0, 0); - - setLayout(new GridBagLayout()); - add(jtfUserNotice, gbc_jtfUserNotice); - add(jbEditUserNotice, gbc_jbEditUserNotice); - add(jbClearUserNotice, gbc_jbClearUserNotice); + setLayout(new MigLayout("insets 0, fill", "[]", "[]")); + add(jtfUserNotice, "growx, pushx"); + add(jbEditUserNotice, ""); + add(jbClearUserNotice, ""); populate(); } diff --git a/kse/src/main/java/org/kse/gui/datetime/JDateTime.java b/kse/src/main/java/org/kse/gui/datetime/JDateTime.java index 73c1e1055..2ed71aa7c 100644 --- a/kse/src/main/java/org/kse/gui/datetime/JDateTime.java +++ b/kse/src/main/java/org/kse/gui/datetime/JDateTime.java @@ -20,9 +20,6 @@ package org.kse.gui.datetime; import java.awt.Container; -import java.awt.GridBagConstraints; -import java.awt.GridBagLayout; -import java.awt.Insets; import java.util.Date; import java.util.ResourceBundle; @@ -36,6 +33,8 @@ import org.kse.gui.CursorUtil; import org.kse.utilities.StringUtils; +import net.miginfocom.swing.MigLayout; + /** * Component to edit a date/time value. */ @@ -73,18 +72,10 @@ public JDateTime(String title, boolean showClearButton) { initComponents(); } - // TODO JW - miglayout private void initComponents() { jtfDateTime = new JTextField(18); jtfDateTime.setEditable(false); - GridBagConstraints gbc_jtfDateTime = new GridBagConstraints(); - gbc_jtfDateTime.gridwidth = 1; - gbc_jtfDateTime.gridheight = 1; - gbc_jtfDateTime.gridx = 0; - gbc_jtfDateTime.gridy = 0; - gbc_jtfDateTime.insets = new Insets(0, 0, 0, 5); - ImageIcon editIcon = new ImageIcon(getClass().getResource("images/edit_date_time.png")); jbEditDateTime = new JButton(editIcon); jbEditDateTime.setToolTipText(res.getString("JDateTime.jbEditDateTime.tooltip")); @@ -97,39 +88,23 @@ private void initComponents() { } }); - GridBagConstraints gbc_jbEditDateTime = new GridBagConstraints(); - gbc_jbEditDateTime.gridwidth = 1; - gbc_jbEditDateTime.gridheight = 1; - gbc_jbEditDateTime.gridx = 1; - gbc_jbEditDateTime.gridy = 0; - gbc_jbEditDateTime.insets = new Insets(0, 0, 0, 5); - - setLayout(new GridBagLayout()); - add(jtfDateTime, gbc_jtfDateTime); - add(jbEditDateTime, gbc_jbEditDateTime); - - if (showClearButton) { - ImageIcon clearIcon = new ImageIcon(getClass().getResource("images/clear_date_time.png")); - jbClearDateTime = new JButton(clearIcon); - jbClearDateTime.setToolTipText(res.getString("JDateTime.jbClearDateTime.tooltip")); - jbClearDateTime.addActionListener(evt -> { - try { - CursorUtil.setCursorBusy(JDateTime.this); - clearDateTime(); - } finally { - CursorUtil.setCursorFree(JDateTime.this); - } - }); - - GridBagConstraints gbc_jbClearDateTime = new GridBagConstraints(); - gbc_jbClearDateTime.gridwidth = 1; - gbc_jbClearDateTime.gridheight = 1; - gbc_jbClearDateTime.gridx = 2; - gbc_jbClearDateTime.gridy = 0; - gbc_jbClearDateTime.insets = new Insets(0, 0, 0, 0); - - add(jbClearDateTime, gbc_jbClearDateTime); - } + ImageIcon clearIcon = new ImageIcon(getClass().getResource("images/clear_date_time.png")); + jbClearDateTime = new JButton(clearIcon); + jbClearDateTime.setToolTipText(res.getString("JDateTime.jbClearDateTime.tooltip")); + jbClearDateTime.addActionListener(evt -> { + try { + CursorUtil.setCursorBusy(JDateTime.this); + clearDateTime(); + } finally { + CursorUtil.setCursorFree(JDateTime.this); + } + }); + jbClearDateTime.setVisible(showClearButton); + + setLayout(new MigLayout("insets 0, fill", "[]", "[]")); + add(jtfDateTime, "growx, pushx"); + add(jbEditDateTime, ""); + add(jbClearDateTime, "hidemode 3"); populate(); } @@ -200,13 +175,11 @@ private void editDateTime() { if (container instanceof JDialog) { dDateTimeChooser = new DDateTimeChooser((JDialog) container, title, date); - dDateTimeChooser.setLocationRelativeTo(container); - dDateTimeChooser.setVisible(true); } else { dDateTimeChooser = new DDateTimeChooser((JFrame) container, title, date); - dDateTimeChooser.setLocationRelativeTo(container); - dDateTimeChooser.setVisible(true); } + dDateTimeChooser.setLocationRelativeTo(container); + dDateTimeChooser.setVisible(true); Date newDate = dDateTimeChooser.getDate(); From 623ed0f38e84151c69459334e3525268f91fdaf5 Mon Sep 17 00:00:00 2001 From: jonwltn <86822083+jonwltn@users.noreply.github.com> Date: Thu, 24 Sep 2026 01:41:38 -0700 Subject: [PATCH 3/3] Migrated JObjectId and DistinguishedNameChooser to MiG layout. --- .../java/org/kse/gui/dialogs/DGetAlias.java | 3 -- .../dnchooser/DistinguishedNameChooser.java | 28 +++++++++++++-- .../org/kse/gui/dnchooser/RdnPanelList.java | 30 ++++++++++++++-- .../main/java/org/kse/gui/oid/JObjectId.java | 34 ++++--------------- 4 files changed, 60 insertions(+), 35 deletions(-) diff --git a/kse/src/main/java/org/kse/gui/dialogs/DGetAlias.java b/kse/src/main/java/org/kse/gui/dialogs/DGetAlias.java index 7ec42ccff..8f16c08f7 100644 --- a/kse/src/main/java/org/kse/gui/dialogs/DGetAlias.java +++ b/kse/src/main/java/org/kse/gui/dialogs/DGetAlias.java @@ -19,7 +19,6 @@ */ package org.kse.gui.dialogs; -import java.awt.BorderLayout; import java.awt.Container; import java.awt.Dialog; import java.awt.event.ActionEvent; @@ -99,8 +98,6 @@ public String getAlias() { } private void initComponents(final String alias) { - getContentPane().setLayout(new BorderLayout()); - jlAlias = new JLabel(res.getString("DGetAlias.jlAlias.text")); jtfAlias = new JTextField(15); diff --git a/kse/src/main/java/org/kse/gui/dnchooser/DistinguishedNameChooser.java b/kse/src/main/java/org/kse/gui/dnchooser/DistinguishedNameChooser.java index cc08eba2b..e8922a19b 100644 --- a/kse/src/main/java/org/kse/gui/dnchooser/DistinguishedNameChooser.java +++ b/kse/src/main/java/org/kse/gui/dnchooser/DistinguishedNameChooser.java @@ -38,6 +38,8 @@ import org.bouncycastle.asn1.x500.X500Name; import org.kse.crypto.x509.KseX500NameStyle; +import net.miginfocom.swing.MigLayout; + /** * Flexible DN chooser/viewer component. */ @@ -50,6 +52,13 @@ public class DistinguishedNameChooser extends JPanel { private String defaultName; private RdnPanelList listPanel; + /** + * Constructs a new DistinguishedNameChooser panel. + * + * @param dn The distinguished name to display or edit. Use null for the default panel. + * @param editable Boolean to indicate if the panel should be editable. + * @param defaultDN The default DN to use when dn is null. Use null for the default panel. + */ public DistinguishedNameChooser(X500Name dn, boolean editable, String defaultDN) { this.editable = editable; if (dn == null || dn.getRDNs().length == 0) { @@ -73,10 +82,14 @@ private void init() { jScrollPane.setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS); jScrollPane.setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_NEVER); - setLayout(new BorderLayout()); - add(jScrollPane, BorderLayout.CENTER); + setLayout(new MigLayout("insets 0, fill", "[]", "[]")); + add(jScrollPane, "grow, push"); } + /** + * + * @return The distinguished name without any empty RDNs. + */ public X500Name getDN() { boolean noEmptyRdns = true; List rdns = listPanel.getRdns(noEmptyRdns); @@ -84,16 +97,27 @@ public X500Name getDN() { return new X500Name(rdns.toArray(RDN[]::new)); } + /** + * + * @return The distinguished name including empty RDNs. + */ public X500Name getDNWithEmptyRdns() { List rdns = listPanel.getRdns(false); Collections.reverse(rdns); return new X500Name(rdns.toArray(RDN[]::new)); } + /** + * + * @return The first RDN text field on the panel. + */ public JTextField getFirstTextField() { return listPanel.getFirstTextField(); } + /** + * Resets the panel to default. + */ public void reset() { this.currentName = new X500Name(defaultName); removeAll(); diff --git a/kse/src/main/java/org/kse/gui/dnchooser/RdnPanelList.java b/kse/src/main/java/org/kse/gui/dnchooser/RdnPanelList.java index fe973dd7b..fffafb689 100644 --- a/kse/src/main/java/org/kse/gui/dnchooser/RdnPanelList.java +++ b/kse/src/main/java/org/kse/gui/dnchooser/RdnPanelList.java @@ -54,16 +54,22 @@ public class RdnPanelList extends JPanel { private static final String[] comboBoxEntries = OidDisplayNameMapping.getDisplayNames(); + /** + * Constructs a new RdnPanelList panel. + * + * @param x500Name The X.500 name to display or edit. + * @param editable Boolean to indicate if the name should be editable. + */ public RdnPanelList(X500Name x500Name, boolean editable) { - setLayout(new MigLayout("insets dialog, flowy", "[right]", "[]rel[]")); + setLayout(new MigLayout("insets 0, flowy", "[right]", "[]rel[]")); setFocusCycleRoot(true); // we have to reverse RDN order for dialog List rdnsAsList = Arrays.asList(x500Name.getRDNs()); Collections.reverse(rdnsAsList); + this.editable = editable; for (RDN rdn : rdnsAsList) { - this.editable = editable; for (AttributeTypeAndValue atav : rdn.getTypesAndValues()) { String type = OidDisplayNameMapping.getDisplayNameForOid(atav.getType().getId()); String value = atav.getValue().toString(); @@ -113,6 +119,11 @@ public Component getFirstComponent(Container focusCycleRoot) { } } + /** + * Clones an RdnPanel for adding to the list. + * + * @param entry The RdnPanel entry to clone. + */ public void cloneEntry(RdnPanel entry) { Object selected = entry.getComboBox().getSelectedItem(); RdnPanel clone = new RdnPanel(new JComboBox<>(comboBoxEntries), selected.toString(), "", this, editable); @@ -157,6 +168,11 @@ private void addItemAfter(RdnPanel entryToAdd, RdnPanel afterThisEntry) { refresh(); } + /** + * Removes a RdnPanel from the list. + * + * @param entry The RdnPanel to remove. + */ public void removeItem(RdnPanel entry) { entries.remove(entry); remove(entry); @@ -164,6 +180,12 @@ public void removeItem(RdnPanel entry) { refresh(); } + /** + * Gets the RDNs. + * + * @param noEmptyRdns True, to strip out any empty RDNs. False, to include empty RDNs. + * @return The list of RDNs. + */ public List getRdns(boolean noEmptyRdns) { List rdns = new ArrayList<>(); for (RdnPanel rdnPanel : entries) { @@ -177,6 +199,10 @@ public List getRdns(boolean noEmptyRdns) { return rdns; } + /** + * + * @return The first text field in the list. + */ public JTextField getFirstTextField() { RdnPanel rdnPanel = entries.get(0); if (rdnPanel != null) { diff --git a/kse/src/main/java/org/kse/gui/oid/JObjectId.java b/kse/src/main/java/org/kse/gui/oid/JObjectId.java index 217b64061..1f98d23a3 100644 --- a/kse/src/main/java/org/kse/gui/oid/JObjectId.java +++ b/kse/src/main/java/org/kse/gui/oid/JObjectId.java @@ -20,9 +20,6 @@ package org.kse.gui.oid; import java.awt.Container; -import java.awt.GridBagConstraints; -import java.awt.GridBagLayout; -import java.awt.Insets; import java.util.ResourceBundle; import javax.swing.ImageIcon; @@ -38,6 +35,8 @@ import org.kse.utilities.oid.InvalidObjectIdException; import org.kse.utilities.oid.ObjectIdUtil; +import net.miginfocom.swing.MigLayout; + /** * Component to edit an object identifier. */ @@ -67,13 +66,6 @@ private void initComponents() { jtfObjectId = new JTextField(25); jtfObjectId.setEditable(false); - GridBagConstraints gbc_jtfObjectId = new GridBagConstraints(); - gbc_jtfObjectId.gridwidth = 1; - gbc_jtfObjectId.gridheight = 1; - gbc_jtfObjectId.gridx = 0; - gbc_jtfObjectId.gridy = 0; - gbc_jtfObjectId.insets = new Insets(0, 0, 0, 5); - ImageIcon editIcon = new ImageIcon(getClass().getResource("images/edit_object_id.png")); jbEditObjectId = new JButton(editIcon); jbEditObjectId.setToolTipText(res.getString("JObjectId.jbEditObjectId.tooltip")); @@ -86,13 +78,6 @@ private void initComponents() { } }); - GridBagConstraints gbc_jbEditObjectId = new GridBagConstraints(); - gbc_jbEditObjectId.gridwidth = 1; - gbc_jbEditObjectId.gridheight = 1; - gbc_jbEditObjectId.gridx = 1; - gbc_jbEditObjectId.gridy = 0; - gbc_jbEditObjectId.insets = new Insets(0, 0, 0, 5); - ImageIcon clearIcon = new ImageIcon(getClass().getResource("images/clear_object_id.png")); jbClearObjectId = new JButton(clearIcon); jbClearObjectId.setToolTipText(res.getString("JObjectId.jbClearObjectId.tooltip")); @@ -105,17 +90,10 @@ private void initComponents() { } }); - GridBagConstraints gbc_jbClearObjectId = new GridBagConstraints(); - gbc_jbClearObjectId.gridwidth = 1; - gbc_jbClearObjectId.gridheight = 1; - gbc_jbClearObjectId.gridx = 2; - gbc_jbClearObjectId.gridy = 0; - gbc_jbClearObjectId.insets = new Insets(0, 0, 0, 0); - - setLayout(new GridBagLayout()); - add(jtfObjectId, gbc_jtfObjectId); - add(jbEditObjectId, gbc_jbEditObjectId); - add(jbClearObjectId, gbc_jbClearObjectId); + setLayout(new MigLayout("insets 0, fill", "[]", "[]")); + add(jtfObjectId, "growx, pushx"); + add(jbEditObjectId, ""); + add(jbClearObjectId, ""); populate(); }