From ae8839e7566e06877ab2d476f41efdf23f410335 Mon Sep 17 00:00:00 2001 From: Douglas Gillespie <50671166+douggillespie@users.noreply.github.com> Date: Wed, 20 Dec 2023 14:48:15 +0000 Subject: [PATCH] Fix settings import not all imports working. Now seems fixed. --- src/PamController/PamSettingManager.java | 2 +- src/PamController/UsedModuleInfo.java | 8 ++++++ .../settings/SettingsImport.java | 27 +++++++++++++------ .../settings/SettingsImportDialog.java | 8 +++--- .../settings/SettingsImportGroup.java | 4 ++- 5 files changed, 36 insertions(+), 13 deletions(-) diff --git a/src/PamController/PamSettingManager.java b/src/PamController/PamSettingManager.java index ddd15740..46bc0679 100644 --- a/src/PamController/PamSettingManager.java +++ b/src/PamController/PamSettingManager.java @@ -476,7 +476,7 @@ public class PamSettingManager { */ public PamSettings findSettingsOwner(String unitType, String unitName, String unitClassName) { for (PamSettings owner:owners) { - if (owner.getClass() != null) { + if (owner.getClass() != null && unitClassName != null) { if (owner.getClass().getName().equals(unitClassName) == false) { continue; } diff --git a/src/PamController/UsedModuleInfo.java b/src/PamController/UsedModuleInfo.java index 88756866..390ab1c0 100644 --- a/src/PamController/UsedModuleInfo.java +++ b/src/PamController/UsedModuleInfo.java @@ -58,4 +58,12 @@ public class UsedModuleInfo implements Serializable, ManagedParameters { return ps; } + /** + * Get the unit name of the module being imported. + * @return + */ + public String getUnitName() { + return unitName; + } + } diff --git a/src/PamController/settings/SettingsImport.java b/src/PamController/settings/SettingsImport.java index 0c52fb31..16e23f35 100644 --- a/src/PamController/settings/SettingsImport.java +++ b/src/PamController/settings/SettingsImport.java @@ -116,14 +116,16 @@ public class SettingsImport { */ private PamControlledUnit importReplace(SettingsImportGroup importGroup, String replaceModule) { PamControlledUnitSettings mainSet = importGroup.getMainSettings(); - PamControlledUnit unit = PamController.getInstance().findControlledUnit(mainSet.getUnitType(), replaceModule); + UsedModuleInfo importInfo = importGroup.getUsedModuleInfo(); + PamControlledUnit unit = PamController.getInstance().findControlledUnit(importInfo.getUnitType(), replaceModule); if (unit == null) { - System.out.println("Unable to find " + mainSet.getUnitType() + " " + mainSet.getUnitName() + " for settings replacement"); + System.out.println("Unable to find " + importInfo.getUnitType() + " " + importInfo.getUnitName() + " for settings replacement"); return null; } // check we can cast it to PamSettings - if (PamSettings.class.isAssignableFrom(unit.getClass())) { + if (PamSettings.class.isAssignableFrom(unit.getClass()) && mainSet != null) { try { + mainSet.setUnitName(replaceModule); ((PamSettings) unit).restoreSettings(mainSet); } catch (Exception e) { @@ -132,7 +134,7 @@ public class SettingsImport { System.err.println(e.getMessage()); } } - loadSubUnitSettings(importGroup, mainSet.getUnitName()); + loadSubUnitSettings(importGroup, replaceModule); return unit; } @@ -149,7 +151,15 @@ public class SettingsImport { } PamSettingManager setManager = PamSettingManager.getInstance(); for (PamControlledUnitSettings pamSettings:subSets) { - PamSettings owner = setManager.findSettingsOwner(pamSettings.getUnitType(), unitName, pamSettings.getOwnerClassName()); + /* + * class name in pamSettings is no longer correct, so cannot use pamSettings.getOwnerClassName(). + * but the classnames of all the sub modules are unknown (and will be different form the unit class name + * which can be got from importGroup.getPamModuleInfo().getClassName + * so will have to do this only on the unit type and name and hope for no conflicts (catch exception). + */ +// PamModuleInfo moduleInfo = importGroup.getPamModuleInfo(); +// String className = moduleInfo.getClassName(); + PamSettings owner = setManager.findSettingsOwner(pamSettings.getUnitType(), unitName, null); if (owner == null) { System.out.println(String.format("Cannot find settings owner for %s %s in current model", pamSettings.getUnitType(), unitName)); continue; @@ -168,7 +178,8 @@ public class SettingsImport { private PamControlledUnit importNew(SettingsImportGroup importGroup) { PamControlledUnitSettings mainSet = importGroup.getMainSettings(); - String moduleName = mainSet.getUnitName(); + UsedModuleInfo importInfo = importGroup.getUsedModuleInfo(); + String moduleName = importInfo.unitName; // check we've got a name that doesnt' exist and replace it if if does. // int startChar = 0; @@ -199,11 +210,11 @@ public class SettingsImport { PamControlledUnit unit = PamController.getInstance().addModule(PamController.getMainFrame(), moduleInfo); if (unit == null) { - System.out.println("Unable to find " + mainSet.getUnitType() + " " + mainSet.getUnitName() + " for settings replacement"); + System.out.println("Unable to find " + importInfo.getUnitType() + " " + importInfo.getUnitName() + " for settings replacement"); return null; } // check we can cast it to PamSettings - if (PamSettings.class.isAssignableFrom(unit.getClass())) { + if (PamSettings.class.isAssignableFrom(unit.getClass()) && mainSet != null) { try { mainSet.setUnitName(unit.getUnitName()); // need to force the unit name for some modules. ((PamSettings) unit).restoreSettings(mainSet); diff --git a/src/PamController/settings/SettingsImportDialog.java b/src/PamController/settings/SettingsImportDialog.java index 6f6621e6..4241a0cf 100644 --- a/src/PamController/settings/SettingsImportDialog.java +++ b/src/PamController/settings/SettingsImportDialog.java @@ -195,12 +195,14 @@ public class SettingsImportDialog extends PamDialog { @Override public Object getValueAt(int rowIndex, int columnIndex) { SettingsImportGroup set = groupedSettings.get(rowIndex); - PamControlledUnitSettings mainSet = set.getMainSettings(); +// PamControlledUnitSettings mainSet = set.getMainSettings(); switch (columnIndex) { case 0: - return mainSet.getUnitType(); + return set.getUsedModuleInfo().getUnitType(); +// return mainSet.getUnitType(); case 1: - return mainSet.getUnitName(); + return set.getUsedModuleInfo().unitName; +// return mainSet.getUnitName(); case 2: // return choiceBoxes[rowIndex].getSelectedItem().toString(); return set.getImportChoice().toString(); diff --git a/src/PamController/settings/SettingsImportGroup.java b/src/PamController/settings/SettingsImportGroup.java index a74a6885..98bf1e4f 100644 --- a/src/PamController/settings/SettingsImportGroup.java +++ b/src/PamController/settings/SettingsImportGroup.java @@ -82,8 +82,10 @@ public class SettingsImportGroup { try { ownerClass = Class.forName(usedModuleInfo.className); } catch (ClassNotFoundException e) { + + System.out.println("Unknown class in loaded settings: " + usedModuleInfo.className); // TODO Auto-generated catch block - e.printStackTrace(); +// e.printStackTrace(); } ArrayList existingModules = PamController.getInstance().findControlledUnits(ownerClass);