diff --git a/diylc/diylc-core/src/org/diylc/common/IVariantProcessor.java b/diylc/diylc-core/src/org/diylc/common/IVariantProcessor.java index b481b849..21aa4a4c 100644 --- a/diylc/diylc-core/src/org/diylc/common/IVariantProcessor.java +++ b/diylc/diylc-core/src/org/diylc/common/IVariantProcessor.java @@ -21,11 +21,10 @@ */ package org.diylc.common; -import java.io.FileNotFoundException; import java.io.IOException; -import java.util.List; +import java.util.List; -import org.diylc.core.Template; +import org.diylc.core.Template; public interface IVariantProcessor { diff --git a/diylc/diylc-core/src/org/diylc/presenter/Presenter.java b/diylc/diylc-core/src/org/diylc/presenter/Presenter.java index ccc145c5..036332c1 100644 --- a/diylc/diylc-core/src/org/diylc/presenter/Presenter.java +++ b/diylc/diylc-core/src/org/diylc/presenter/Presenter.java @@ -191,6 +191,7 @@ public class Presenter implements IPlugInPort { // lockedLayers = EnumSet.noneOf(ComponentLayer.class); // visibleLayers = EnumSet.allOf(ComponentLayer.class); + upgradeVariants(); } public void installPlugin(IPlugIn plugIn) { @@ -2633,4 +2634,47 @@ public class Presenter implements IPlugInPort { return pkg.getBlocks().size(); } + + private static boolean upgradedVariants = false; + + @SuppressWarnings("unchecked") + private synchronized void upgradeVariants() { + if (upgradedVariants) + return; + + upgradedVariants = true; + + LOG.info("Checking if variants need to be updated"); + Map> lookupMap = new TreeMap>(String.CASE_INSENSITIVE_ORDER); + Map> variantMap = + (Map>) ConfigurationManager.getInstance().readObject(TEMPLATES_KEY, null); + + if (variantMap == null) + return; + + Map typeMap = new TreeMap(String.CASE_INSENSITIVE_ORDER); + + Map> componentTypes = getComponentTypes(); + for (Map.Entry> entry : componentTypes.entrySet()) + for (ComponentType type : entry.getValue()) { + typeMap.put(type.getInstanceClass().getCanonicalName(), type); + typeMap.put(type.getCategory() + "." + type.getName(), type); + if (type.getCategory().contains("Electro-Mechanical")) + typeMap.put(type.getCategory().replace("Electro-Mechanical", "Electromechanical") + "." + type.getName(), type); + } + + Map> newVariantMap = new HashMap>(); + + lookupMap.putAll(variantMap); + + for (Map.Entry> entry : variantMap.entrySet()) { + if (typeMap.containsKey(entry.getKey())) { + newVariantMap.put(typeMap.get(entry.getKey()).getInstanceClass().getCanonicalName(), entry.getValue()); // great, nothing to upgrade + } else { + LOG.warn("Could not upgrade variants for: " + entry.getKey()); + } + } + + ConfigurationManager.getInstance().writeValue(TEMPLATES_KEY, newVariantMap); + } } diff --git a/diylc/diylc-swing/lib/swing-framework.jar b/diylc/diylc-swing/lib/swing-framework.jar index e3c7b6a5..6cf5eef1 100644 Binary files a/diylc/diylc-swing/lib/swing-framework.jar and b/diylc/diylc-swing/lib/swing-framework.jar differ diff --git a/diylc/diylc-swing/src/org/diylc/swing/ActionFactory.java b/diylc/diylc-swing/src/org/diylc/swing/ActionFactory.java index 4495b622..ad034d44 100644 --- a/diylc/diylc-swing/src/org/diylc/swing/ActionFactory.java +++ b/diylc/diylc-swing/src/org/diylc/swing/ActionFactory.java @@ -36,6 +36,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.TreeMap; import javax.swing.AbstractAction; import javax.swing.JOptionPane; @@ -697,7 +698,7 @@ public class ActionFactory { private ISwingUI swingUI; private IPlugInPort plugInPort; - private Map typeMap = new HashMap(); + private Map typeMap = new TreeMap(String.CASE_INSENSITIVE_ORDER); public ExportVariantsAction(ISwingUI swingUI, IPlugInPort plugInPort) { super(); @@ -707,52 +708,69 @@ public class ActionFactory { Map> componentTypes = plugInPort.getComponentTypes(); for (Map.Entry> entry : componentTypes.entrySet()) - for (ComponentType type : entry.getValue()) + for (ComponentType type : entry.getValue()) { typeMap.put(type.getInstanceClass().getCanonicalName(), type); + } } @SuppressWarnings("unchecked") @Override public void actionPerformed(ActionEvent e) { LOG.info("ExportVariantsAction triggered"); - Map> variantMap = - (Map>) ConfigurationManager.getInstance().readObject(IPlugInPort.TEMPLATES_KEY, null); - if (variantMap == null || variantMap.isEmpty()) { - swingUI.showMessage("No variants found to export.", "Error", IView.ERROR_MESSAGE); + + Map> selectedVariants; + + try { + Map> variantMap = + (Map>) ConfigurationManager.getInstance().readObject(IPlugInPort.TEMPLATES_KEY, null); + if (variantMap == null || variantMap.isEmpty()) { + swingUI.showMessage("No variants found to export.", "Error", IView.ERROR_MESSAGE); + return; + } + + List types = new ArrayList(); + for (String className : variantMap.keySet()) { + ComponentType type = typeMap.get(className); + if (type != null) + types.add(type); + else + LOG.warn("Could not find type for: " + className); + } + + Collections.sort(types, new Comparator() { + + @Override + public int compare(ComponentType o1, ComponentType o2) { + return o1.toString().compareToIgnoreCase(o2.toString()); + }}); + + CheckBoxListDialog dialog = new CheckBoxListDialog(swingUI.getOwnerFrame(), "Export Variants", types.toArray()); + + dialog.setVisible(true); + + if (dialog.getSelectedButtonCaption() != "OK") + return; + + Object[] selected = dialog.getSelectedOptions(); + + if (selected.length == 0) { + swingUI.showMessage("No variants selected for export.", "Error", IView.ERROR_MESSAGE); + return; + } + + selectedVariants = new HashMap>(); + for (Object key : selected) { + ComponentType type = (ComponentType) key; + String clazz = type.getInstanceClass().getCanonicalName(); + List