From 7a193d0f1651db15276af7b6a0f943c5f2f25298 Mon Sep 17 00:00:00 2001 From: Bengt Martensson Date: Wed, 22 Mar 2017 22:36:24 +0100 Subject: [PATCH] Implemented sanity check for remotes (most of #87), called by export. Resolves #192. --- .../harctoolbox/irscrutinizer/GuiMain.form | 16 ++++ .../harctoolbox/irscrutinizer/GuiMain.java | 55 +++++++++----- .../irscrutinizer/NamedIrSignal.java | 73 +++++++++++++++++-- 3 files changed, 116 insertions(+), 28 deletions(-) diff --git a/IrScrutinizer/src/main/java/org/harctoolbox/irscrutinizer/GuiMain.form b/IrScrutinizer/src/main/java/org/harctoolbox/irscrutinizer/GuiMain.form index 658b1668..a69186a9 100644 --- a/IrScrutinizer/src/main/java/org/harctoolbox/irscrutinizer/GuiMain.form +++ b/IrScrutinizer/src/main/java/org/harctoolbox/irscrutinizer/GuiMain.form @@ -356,6 +356,14 @@ + + + + + + + + @@ -645,6 +653,14 @@ + + + + + + + + diff --git a/IrScrutinizer/src/main/java/org/harctoolbox/irscrutinizer/GuiMain.java b/IrScrutinizer/src/main/java/org/harctoolbox/irscrutinizer/GuiMain.java index 9d518d7d..869b5d37 100644 --- a/IrScrutinizer/src/main/java/org/harctoolbox/irscrutinizer/GuiMain.java +++ b/IrScrutinizer/src/main/java/org/harctoolbox/irscrutinizer/GuiMain.java @@ -1078,34 +1078,16 @@ private void saveRawSignals(RemoteSetExporter exporter) { } private File saveCommands(NamedIrSignal.LearnedIrSignalTableModel tableModel, String title, RemoteSetExporter exporter) throws IrpMasterException, IOException, TransformerException { - Map commands = getCommands(tableModel); + Map commands = tableModel.getCommandsWithSanityCheck(guiUtils); if (commands == null) return null; + File file = saveCommands(commands, "IrScrutinizer " + tableModel.getType() + " table", title, exporter); if (file != null) tableModel.clearUnsavedChanges(); return file; } - private Map getCommands(NamedIrSignal.LearnedIrSignalTableModel tableModel) throws IrpMasterException { - ListduplicateNames = tableModel.getNonUniqueNames(); - if (!duplicateNames.isEmpty()) { - StringBuilder str = new StringBuilder("The following names are non-unique: "); - str.append(String.join(", ", duplicateNames)); - str.append(".\n").append("Only one signal per name will be preserved in the export."); - str.append("\n").append("Continue?"); - boolean answer = guiUtils.confirm(str.toString()); - if (!answer) - return null; - } - - if (tableModel.getRowCount() == 0) { - guiUtils.error("No " + tableModel.getType() + " signals present; export aborted."); - return null; - } - return tableModel.getCommands(true); - } - private File saveCommands(Map commands, String source, String title, RemoteSetExporter exporter) throws IrpMasterException, IOException, TransformerException { if (properties.getExportInquireDeviceData() && exporter.supportsMetaData()) { Remote.MetaData newMetaData = MetaDataDialog.inquireMetaData(metaData, this); @@ -1803,6 +1785,7 @@ private void initComponents() { debugTableRowMenuItem1 = new javax.swing.JMenuItem(); jSeparator14 = new javax.swing.JPopupMenu.Separator(); clearMenuItem1 = new javax.swing.JMenuItem(); + checkParametrizedSignalsMenuItem = new javax.swing.JMenuItem(); saveCookedMenuItem = new javax.swing.JMenuItem(); jSeparator19 = new javax.swing.JPopupMenu.Separator(); parametrizedCopyAllMenuItem = new javax.swing.JMenuItem(); @@ -1839,6 +1822,7 @@ private void initComponents() { debugTableRowMenuItem = new javax.swing.JMenuItem(); jSeparator12 = new javax.swing.JPopupMenu.Separator(); clearMenuItem = new javax.swing.JMenuItem(); + checkRawCommandsMenuItem = new javax.swing.JMenuItem(); saveRawMenuItem = new javax.swing.JMenuItem(); jSeparator28 = new javax.swing.JPopupMenu.Separator(); rawCopyAllMenuItem = new javax.swing.JMenuItem(); @@ -2563,6 +2547,14 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { }); parameterTablePopupMenu.add(clearMenuItem1); + checkParametrizedSignalsMenuItem.setText("Check commands"); + checkParametrizedSignalsMenuItem.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + checkParametrizedSignalsMenuItemActionPerformed(evt); + } + }); + parameterTablePopupMenu.add(checkParametrizedSignalsMenuItem); + saveCookedMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, 0)); saveCookedMenuItem.setMnemonic('E'); saveCookedMenuItem.setText("Export"); @@ -2819,6 +2811,14 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { }); rawTablePopupMenu.add(clearMenuItem); + checkRawCommandsMenuItem.setText("Check commands"); + checkRawCommandsMenuItem.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + checkRawCommandsMenuItemActionPerformed(evt); + } + }); + rawTablePopupMenu.add(checkRawCommandsMenuItem); + saveRawMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, 0)); saveRawMenuItem.setMnemonic('E'); saveRawMenuItem.setText("Export"); @@ -9040,6 +9040,19 @@ public void processSequence(ModulatedIrSequence sequence) { } }//GEN-LAST:event_continuousCaptureButtonActionPerformed + private void checkTable(NamedIrSignal.LearnedIrSignalTableModel tableModel) { + boolean success = tableModel.sanityCheck(guiUtils); + guiUtils.info(success ? "No problems found" : "Problems found"); + } + + private void checkParametrizedSignalsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkParametrizedSignalsMenuItemActionPerformed + checkTable(parameterTableModel); + }//GEN-LAST:event_checkParametrizedSignalsMenuItemActionPerformed + + private void checkRawCommandsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkRawCommandsMenuItemActionPerformed + checkTable(rawTableModel); + }//GEN-LAST:event_checkRawCommandsMenuItemActionPerformed + // // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JPopupMenu CCFCodePopupMenu; @@ -9083,6 +9096,8 @@ public void processSequence(ModulatedIrSequence sequence) { private org.harctoolbox.irscrutinizer.importer.FileImporterBean ccfFileImporterBean; private javax.swing.JPanel ccfImportPanel; private javax.swing.JRadioButtonMenuItem ccfRadioButtonMenuItem; + private javax.swing.JMenuItem checkParametrizedSignalsMenuItem; + private javax.swing.JMenuItem checkRawCommandsMenuItem; private javax.swing.JMenuItem checkUpToDateMenuItem; private javax.swing.JCheckBoxMenuItem cleanerCheckBoxMenuItem; private javax.swing.JMenuItem clearConsoleMenuItem; diff --git a/IrScrutinizer/src/main/java/org/harctoolbox/irscrutinizer/NamedIrSignal.java b/IrScrutinizer/src/main/java/org/harctoolbox/irscrutinizer/NamedIrSignal.java index 983e051d..f62e52d2 100644 --- a/IrScrutinizer/src/main/java/org/harctoolbox/irscrutinizer/NamedIrSignal.java +++ b/IrScrutinizer/src/main/java/org/harctoolbox/irscrutinizer/NamedIrSignal.java @@ -22,12 +22,15 @@ import java.util.Collection; import java.util.Date; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import javax.swing.table.DefaultTableColumnModel; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableColumn; +import org.harctoolbox.IrpMaster.IrSignal; import org.harctoolbox.IrpMaster.IrpMasterException; import org.harctoolbox.girr.Command; +import org.harctoolbox.guicomponents.GuiUtils; /** * @@ -228,27 +231,81 @@ protected LearnedIrSignalTableModel(AbstractColumnFunction columnFunc) { public abstract Command toCommand(int row) throws IrpMasterException; - public Map getCommands(boolean forgiveSillySignals) throws IrpMasterException { - Map commands = new LinkedHashMap<>(getRowCount() + 10); + public Map getCommands() { + Map commands = new LinkedHashMap<>(getRowCount()); for (int row = 0; row < getRowCount(); row++) { try { Command command = toCommand(row); - if (command != null) - commands.put(command.getName(), command); + commands.put(command.getName(), command); } catch (IrpMasterException ex) { - if (forgiveSillySignals) { String commandName = (String) getValueAt(row, columnsFunc.getPosName()); String commandComment = (String) getValueAt(row, columnsFunc.getPosComment()); System.err.println("Warning: Signal named " + commandName + " (" + commandComment + ") could not be rendered (" + ex.getMessage() + "); ignored."); - } else { - throw ex; - } } } return commands; } + public boolean sanityCheck(GuiUtils guiUtils) { + Map commands = getCommands(); + return sanityCheck(commands); + } + + + public Map getCommandsWithSanityCheck(GuiUtils guiUtils) { + Map commands = getCommands(); + boolean status = sanityCheck(commands); + return status || guiUtils.confirm("Some signals in export erroneous. Continue anyhow?") ? commands : null; + } + + private boolean checkNonUniqueNames() { + List duplicateNames = getNonUniqueNames(); + if (!duplicateNames.isEmpty()) { + StringBuilder str = new StringBuilder("The following names are non-unique: "); + str.append(String.join(", ", duplicateNames)); + str.append(".\n").append("Only one signal per name will be preserved in the export."); + System.err.println(str); + return false; + } + return true; + } + + private boolean sanityCheck(Map commands) { + if (commands.isEmpty()) { + System.err.println("No signals present."); + return false; + } + boolean success = true; + for (Command command : commands.values()) { + success = checkCommandSanity(command) && success; + } + + return checkNonUniqueNames() && success; + } + + // likely to be overridden + protected boolean checkName(String name) { + if (name.contains(" ")) { + System.err.println("Command named \"" + name + "\" contains space."); + return false; + } + return true; + } + + protected boolean checkCommandSanity(Command command) { + if (!checkName(command.getName())) + return false; + + try { + IrSignal irSignal = command.toIrSignal(); + return irSignal != null; + } catch (IrpMasterException ex) { + System.err.println(ex.getMessage()); + return false; + } + } + public synchronized void clearComment() { for (int row = 0; row < getRowCount(); row++) { NamedIrSignal nir = (NamedIrSignal) getValueAt(row, columnsFunc.getPosIrSignal());