diff --git a/diylc/diylc-core/src/org/diylc/common/BuildingBlockPackage.java b/diylc/diylc-core/src/org/diylc/common/BuildingBlockPackage.java
new file mode 100644
index 00000000..1131dd2e
--- /dev/null
+++ b/diylc/diylc-core/src/org/diylc/common/BuildingBlockPackage.java
@@ -0,0 +1,47 @@
+/*
+
+ DIY Layout Creator (DIYLC).
+ Copyright (c) 2009-2018 held jointly by the individual authors.
+
+ This file is part of DIYLC.
+
+ DIYLC 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.
+
+ DIYLC 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 DIYLC. If not, see .
+
+*/
+package org.diylc.common;
+
+import java.util.Collection;
+import java.util.Map;
+
+import org.diylc.core.IDIYComponent;
+
+public class BuildingBlockPackage {
+
+ private Map>> block;
+ private String owner;
+
+ public BuildingBlockPackage(Map>> blocks, String owner) {
+ super();
+ this.block = blocks;
+ this.owner = owner;
+ }
+
+ public Map>> getBlocks() {
+ return block;
+ }
+
+ public String getOwner() {
+ return owner;
+ }
+}
diff --git a/diylc/diylc-core/src/org/diylc/common/IBlockProcessor.java b/diylc/diylc-core/src/org/diylc/common/IBlockProcessor.java
index 139915f4..b9f93fae 100644
--- a/diylc/diylc-core/src/org/diylc/common/IBlockProcessor.java
+++ b/diylc/diylc-core/src/org/diylc/common/IBlockProcessor.java
@@ -20,6 +20,8 @@
*/
package org.diylc.common;
+
+import java.io.IOException;
public interface IBlockProcessor {
@@ -29,7 +31,9 @@ public interface IBlockProcessor {
void loadBlock(String blockName) throws InvalidBlockException;
- void deleteBlock(String blockName);
+ void deleteBlock(String blockName);
+
+ int importBlocks(String fileName) throws IOException;
public class BlockAlreadyExistsException extends Exception {
diff --git a/diylc/diylc-core/src/org/diylc/common/IVariantProcessor.java b/diylc/diylc-core/src/org/diylc/common/IVariantProcessor.java
index d0cce3d4..b481b849 100644
--- a/diylc/diylc-core/src/org/diylc/common/IVariantProcessor.java
+++ b/diylc/diylc-core/src/org/diylc/common/IVariantProcessor.java
@@ -21,8 +21,10 @@
*/
package org.diylc.common;
+import java.io.FileNotFoundException;
+import java.io.IOException;
import java.util.List;
-
+
import org.diylc.core.Template;
public interface IVariantProcessor {
@@ -32,17 +34,19 @@ public interface IVariantProcessor {
void saveSelectedComponentAsVariant(String variantName);
- List getVariantsFor(String categoryName, String componentTypeName);
+ List getVariantsFor(ComponentType type);
List getVariantsForSelection();
void applyVariantToSelection(Template variant);
- void deleteVariant(String categoryName, String componentTypeName, String variantName);
+ void deleteVariant(ComponentType type, String variantName);
- void setDefaultVariant(String categoryName, String componentTypeName, String variantName);
+ void setDefaultVariant(ComponentType type, String variantName);
- String getDefaultVariant(String categoryName, String componentTypeName);
+ int importVariants(String fileName) throws IOException;
+
+ String getDefaultVariant(ComponentType type);
public class VariantAlreadyExistsException extends Exception {
diff --git a/diylc/diylc-core/src/org/diylc/common/VariantPackage.java b/diylc/diylc-core/src/org/diylc/common/VariantPackage.java
new file mode 100644
index 00000000..32879c60
--- /dev/null
+++ b/diylc/diylc-core/src/org/diylc/common/VariantPackage.java
@@ -0,0 +1,47 @@
+/*
+
+ DIY Layout Creator (DIYLC).
+ Copyright (c) 2009-2018 held jointly by the individual authors.
+
+ This file is part of DIYLC.
+
+ DIYLC 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.
+
+ DIYLC 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 DIYLC. If not, see .
+
+*/
+package org.diylc.common;
+
+import java.util.List;
+import java.util.Map;
+
+import org.diylc.core.Template;
+
+public class VariantPackage {
+
+ private Map> variants;
+ private String owner;
+
+ public VariantPackage(Map> variants, String owner) {
+ super();
+ this.variants = variants;
+ this.owner = owner;
+ }
+
+ public Map> getVariants() {
+ return variants;
+ }
+
+ public String getOwner() {
+ return owner;
+ }
+}
diff --git a/diylc/diylc-core/src/org/diylc/presenter/Presenter.java b/diylc/diylc-core/src/org/diylc/presenter/Presenter.java
index 776a45e8..912f7451 100644
--- a/diylc/diylc-core/src/org/diylc/presenter/Presenter.java
+++ b/diylc/diylc-core/src/org/diylc/presenter/Presenter.java
@@ -54,6 +54,7 @@ import org.diylc.appframework.miscutils.Utils;
import org.diylc.appframework.simplemq.MessageDispatcher;
import org.diylc.appframework.update.Version;
import org.diylc.appframework.update.VersionNumber;
+import org.diylc.common.BuildingBlockPackage;
import org.diylc.common.ComponentType;
import org.diylc.common.DrawOption;
import org.diylc.common.EventType;
@@ -63,6 +64,7 @@ import org.diylc.common.IKeyProcessor;
import org.diylc.common.IPlugIn;
import org.diylc.common.IPlugInPort;
import org.diylc.common.PropertyWrapper;
+import org.diylc.common.VariantPackage;
import org.diylc.core.ExpansionMode;
import org.diylc.core.IDIYComponent;
import org.diylc.core.IView;
@@ -2157,8 +2159,8 @@ public class Presenter implements IPlugInPort {
// try to find a default template if none is provided
if (componentType != null && template == null) {
- String defaultTemplate = getDefaultVariant(componentType.getCategory(), componentType.getName());
- List templates = getVariantsFor(componentType.getCategory(), componentType.getName());
+ String defaultTemplate = getDefaultVariant(componentType);
+ List templates = getVariantsFor(componentType);
if (templates != null && defaultTemplate != null)
for (Template t : templates) {
if (t.getName().equals(defaultTemplate)) {
@@ -2205,7 +2207,7 @@ public class Presenter implements IPlugInPort {
if (variantMap == null) {
variantMap = new HashMap>();
}
- String key = type.getCategory() + "." + type.getName();
+ String key = type.getInstanceClass().getCanonicalName();
List variants = variantMap.get(key);
if (variants == null) {
variants = new ArrayList();
@@ -2293,7 +2295,7 @@ public class Presenter implements IPlugInPort {
@SuppressWarnings("unchecked")
@Override
- public List getVariantsFor(String categoryName, String componentTypeName) {
+ public List getVariantsFor(ComponentType type) {
Map> lookupMap = new TreeMap>(String.CASE_INSENSITIVE_ORDER);
Map> variantMap =
@@ -2301,14 +2303,24 @@ public class Presenter implements IPlugInPort {
if (variantMap != null)
lookupMap.putAll(variantMap);
+ // try by class name and then by old category.type format
+ String key1 = type.getInstanceClass().getCanonicalName();
+ String key2 = type.getCategory() + "." + type.getName();
+
List variants = new ArrayList();
if (variantMap != null) {
- List userVariants = variantMap.get(categoryName + "." + componentTypeName);
+ List userVariants = variantMap.get(key1);
+ if (userVariants != null && !userVariants.isEmpty())
+ variants.addAll(userVariants);
+ userVariants = variantMap.get(key2);
if (userVariants != null && !userVariants.isEmpty())
variants.addAll(userVariants);
}
if (defaultVariantMap != null) {
- List defaultVariants = defaultVariantMap.get(categoryName + "." + componentTypeName);
+ List defaultVariants = defaultVariantMap.get(key1);
+ if (defaultVariants != null && !defaultVariants.isEmpty())
+ variants.addAll(defaultVariants);
+ defaultVariants = defaultVariantMap.get(key2);
if (defaultVariants != null && !defaultVariants.isEmpty())
variants.addAll(defaultVariants);
}
@@ -2339,7 +2351,7 @@ public class Presenter implements IPlugInPort {
else if (selectedType.getInstanceClass() != type.getInstanceClass())
return null;
}
- return getVariantsFor(selectedType.getCategory(), selectedType.getName());
+ return getVariantsFor(selectedType);
}
@Override
@@ -2370,12 +2382,26 @@ public class Presenter implements IPlugInPort {
@SuppressWarnings("unchecked")
@Override
- public void deleteVariant(String categoryName, String componentTypeName, String templateName) {
- LOG.debug(String.format("deleteTemplate(%s, %s, %s)", categoryName, componentTypeName, templateName));
+ public void deleteVariant(ComponentType type, String templateName) {
+ LOG.debug(String.format("deleteTemplate(%s, %s)", type, templateName));
Map> templateMap =
(Map>) ConfigurationManager.getInstance().readObject(TEMPLATES_KEY, null);
if (templateMap != null) {
- List templates = templateMap.get(categoryName + "." + componentTypeName);
+ // try by class name and then by old category.type format
+ String key1 = type.getInstanceClass().getCanonicalName();
+ String key2 = type.getCategory() + "." + type.getName();
+
+ List templates = templateMap.get(key1);
+ if (templates != null) {
+ Iterator i = templates.iterator();
+ while (i.hasNext()) {
+ Template t = i.next();
+ if (t.getName().equalsIgnoreCase(templateName)) {
+ i.remove();
+ }
+ }
+ }
+ templates = templateMap.get(key2);
if (templates != null) {
Iterator i = templates.iterator();
while (i.hasNext()) {
@@ -2391,27 +2417,44 @@ public class Presenter implements IPlugInPort {
@SuppressWarnings("unchecked")
@Override
- public void setDefaultVariant(String categoryName, String componentTypeName, String templateName) {
- LOG.debug(String.format("setTemplateDefault(%s, %s, %s)", categoryName, componentTypeName, templateName));
+ public void setDefaultVariant(ComponentType type, String templateName) {
+ LOG.debug(String.format("setTemplateDefault(%s, %s)", type, templateName));
Map defaultTemplateMap =
(Map) ConfigurationManager.getInstance().readObject(DEFAULT_TEMPLATES_KEY, null);
if (defaultTemplateMap == null)
defaultTemplateMap = new HashMap();
- if (templateName.equals(defaultTemplateMap.get(categoryName + "." + componentTypeName)))
- defaultTemplateMap.remove(categoryName + "." + componentTypeName);
- else
- defaultTemplateMap.put(categoryName + "." + componentTypeName, templateName);
+
+ // try by class name and then by old category.type format
+ String key1 = type.getInstanceClass().getCanonicalName();
+ String key2 = type.getCategory() + "." + type.getName();
+
+ if (templateName.equals(defaultTemplateMap.get(key1)) || templateName.equals(defaultTemplateMap.get(key2))) {
+ defaultTemplateMap.remove(key1);
+ defaultTemplateMap.remove(key2);
+ }
+ else {
+ // get rid of legacy key
+ defaultTemplateMap.remove(key2);
+ defaultTemplateMap.put(key1, templateName);
+ }
ConfigurationManager.getInstance().writeValue(DEFAULT_TEMPLATES_KEY, defaultTemplateMap);
}
@SuppressWarnings("unchecked")
@Override
- public String getDefaultVariant(String categoryName, String componentTypeName) {
+ public String getDefaultVariant(ComponentType type) {
Map defaultTemplateMap =
(Map) ConfigurationManager.getInstance().readObject(DEFAULT_TEMPLATES_KEY, null);
if (defaultTemplateMap == null)
return null;
- return defaultTemplateMap.get(categoryName + "." + componentTypeName);
+
+ String key1 = type.getInstanceClass().getCanonicalName();
+ String key2 = type.getCategory() + "." + type.getName();
+
+ if (defaultTemplateMap.containsKey(key1))
+ return defaultTemplateMap.get(key1);
+
+ return defaultTemplateMap.get(key2);
}
private Set> getLockedComponents() {
@@ -2519,4 +2562,75 @@ public class Presenter implements IPlugInPort {
return extraSpace;
}
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public int importVariants(String fileName) throws IOException {
+ LOG.debug(String.format("importVariants(%s)", fileName));
+ BufferedInputStream in = new BufferedInputStream(new FileInputStream(fileName));
+ XStream xStream = new XStream(new DomDriver());
+
+ VariantPackage pkg = (VariantPackage) xStream.fromXML(in);
+
+ in.close();
+
+ if (pkg == null || pkg.getVariants().isEmpty())
+ return 0;
+
+ Map> variantMap =
+ (Map>) ConfigurationManager.getInstance().readObject(TEMPLATES_KEY, null);
+ if (variantMap == null) {
+ variantMap = new HashMap>();
+ }
+
+ for (Map.Entry> entry : pkg.getVariants().entrySet()) {
+ List templates;
+ templates = variantMap.get(entry.getKey());
+ if (templates == null)
+ {
+ templates = new ArrayList();
+ variantMap.put(entry.getKey(), templates);
+ }
+ for (Template t : entry.getValue()) {
+ templates.add(new Template(t.getName() + " by " + pkg.getOwner(), t.getValues(), t.getPoints()));
+ }
+ }
+
+ ConfigurationManager.getInstance().writeValue(TEMPLATES_KEY, variantMap);
+
+ LOG.info(String.format("Loaded variants for %d components", pkg.getVariants().size()));
+
+ return pkg.getVariants().size();
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public int importBlocks(String fileName) throws IOException {
+ LOG.debug(String.format("importBlocks(%s)", fileName));
+ BufferedInputStream in = new BufferedInputStream(new FileInputStream(fileName));
+ XStream xStream = new XStream(new DomDriver());
+
+ BuildingBlockPackage pkg = (BuildingBlockPackage) xStream.fromXML(in);
+
+ in.close();
+
+ if (pkg == null || pkg.getBlocks().isEmpty())
+ return 0;
+
+ Map>> blocks =
+ (Map>>) ConfigurationManager.getInstance().readObject(BLOCKS_KEY, null);
+ if (blocks == null) {
+ blocks = new HashMap>>();
+ }
+
+ for (Map.Entry>> entry : pkg.getBlocks().entrySet()) {
+ blocks.put(entry.getKey() + " by " + pkg.getOwner(), entry.getValue());
+ }
+
+ ConfigurationManager.getInstance().writeValue(BLOCKS_KEY, blocks);
+
+ LOG.info(String.format("Loaded building blocks for %d components", pkg.getBlocks().size()));
+
+ return pkg.getBlocks().size();
+ }
}
diff --git a/diylc/diylc-swing/DIYLCStarter.launch b/diylc/diylc-swing/DIYLCStarter.launch
index 38c8ab51..aff8a7cb 100644
--- a/diylc/diylc-swing/DIYLCStarter.launch
+++ b/diylc/diylc-swing/DIYLCStarter.launch
@@ -11,5 +11,5 @@
-
+
diff --git a/diylc/diylc-swing/lib/diylc-core.jar b/diylc/diylc-swing/lib/diylc-core.jar
index 31ef4eb5..985a3c07 100644
Binary files a/diylc/diylc-swing/lib/diylc-core.jar and b/diylc/diylc-swing/lib/diylc-core.jar differ
diff --git a/diylc/diylc-swing/src/org/diylc/images/IconLoader.java b/diylc/diylc-swing/src/org/diylc/images/IconLoader.java
index 47ea675c..d79fc0f1 100644
--- a/diylc/diylc-swing/src/org/diylc/images/IconLoader.java
+++ b/diylc/diylc-swing/src/org/diylc/images/IconLoader.java
@@ -62,7 +62,8 @@ public enum IconLoader {
"pin_green.png"), CoffeebeanEdit("coffeebean_edit.png"), ApplicationEdit("application_edit.png"), ComponentAdd(
"component_add.png"), History("history.png"), DocumentPlain("document_plain.png"), FitToSize("fit_to_size.png"), DocumentsGear(
"documents_gear.png"), SplashResistor("splash_resistor.png"), SplashCeramic("splash_ceramic.png"), SplashElectrolytic(
- "splash_electrolytic.png"), SplashFilm("splash_film.png"), Splash("splash.png"), Help("help2.png"), ScrollInformation("scroll_information.png");
+ "splash_electrolytic.png"), SplashFilm("splash_film.png"), Splash("splash.png"), Help("help2.png"), ScrollInformation("scroll_information.png"),
+ Node("node.png");
protected String name;
diff --git a/diylc/diylc-swing/src/org/diylc/images/node.png b/diylc/diylc-swing/src/org/diylc/images/node.png
new file mode 100644
index 00000000..476fa601
Binary files /dev/null and b/diylc/diylc-swing/src/org/diylc/images/node.png differ
diff --git a/diylc/diylc-swing/src/org/diylc/swing/ActionFactory.java b/diylc/diylc-swing/src/org/diylc/swing/ActionFactory.java
index 700ac8b4..9df3699e 100644
--- a/diylc/diylc-swing/src/org/diylc/swing/ActionFactory.java
+++ b/diylc/diylc-swing/src/org/diylc/swing/ActionFactory.java
@@ -23,10 +23,14 @@ import java.awt.datatransfer.ClipboardOwner;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.print.PrinterException;
+import java.io.BufferedOutputStream;
import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
+import java.util.Map;
import java.util.Set;
import javax.swing.AbstractAction;
@@ -35,14 +39,17 @@ import javax.swing.KeyStroke;
import org.apache.log4j.Logger;
import org.diylc.appframework.miscutils.ConfigurationManager;
+import org.diylc.common.BuildingBlockPackage;
import org.diylc.common.IComponentTransformer;
import org.diylc.common.IPlugInPort;
import org.diylc.common.ITask;
import org.diylc.common.PropertyWrapper;
+import org.diylc.common.VariantPackage;
import org.diylc.core.ExpansionMode;
import org.diylc.core.IDIYComponent;
import org.diylc.core.IView;
import org.diylc.core.Project;
+import org.diylc.core.Template;
import org.diylc.core.Theme;
import org.diylc.core.measures.Nudge;
import org.diylc.core.measures.Size;
@@ -60,6 +67,9 @@ import org.diylc.swingframework.IDrawingProvider;
import org.diylc.swingframework.export.DrawingExporter;
import org.diylc.utils.BomEntry;
+import com.thoughtworks.xstream.XStream;
+import com.thoughtworks.xstream.io.xml.DomDriver;
+
public class ActionFactory {
private static final Logger LOG = Logger.getLogger(ActionFactory.class);
@@ -112,6 +122,22 @@ public class ActionFactory {
public PrintAction createPrintAction(IDrawingProvider drawingProvider, int keyModifiers) {
return new PrintAction(drawingProvider, keyModifiers);
}
+
+ public ExportVariantsAction createExportVariantsAction(ISwingUI swingUI) {
+ return new ExportVariantsAction(swingUI);
+ }
+
+ public ImportVariantsAction createImportVariantsAction(ISwingUI swingUI, IPlugInPort plugInPort) {
+ return new ImportVariantsAction(swingUI, plugInPort);
+ }
+
+ public ExportBlocksAction createExportBlocksAction(ISwingUI swingUI) {
+ return new ExportBlocksAction(swingUI);
+ }
+
+ public ImportBlocksAction createImportBlocksAction(ISwingUI swingUI, IPlugInPort plugInPort) {
+ return new ImportBlocksAction(swingUI, plugInPort);
+ }
public ExitAction createExitAction(IPlugInPort plugInPort) {
return new ExitAction(plugInPort);
@@ -653,11 +679,239 @@ public class ActionFactory {
try {
DrawingExporter.getInstance().print(this.drawingProvider);
} catch (PrinterException e1) {
- // TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
+
+ public static class ExportVariantsAction extends AbstractAction {
+
+ private static final long serialVersionUID = 1L;
+
+ private ISwingUI swingUI;
+
+ public ExportVariantsAction(ISwingUI swingUI) {
+ super();
+ this.swingUI = swingUI;
+ putValue(AbstractAction.NAME, "Export Variants");
+ }
+
+ @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);
+ return;
+ }
+
+ final VariantPackage variantPkg = new VariantPackage(variantMap, System.getProperty("user.name"));
+
+ File initialFile =
+ new File(variantPkg.getOwner() == null ? "variants.xml" : ("variants by "
+ + variantPkg.getOwner().toLowerCase() + ".xml"));
+
+ final File file =
+ DialogFactory.getInstance().showSaveDialog(swingUI.getOwnerFrame(), FileFilterEnum.XML.getFilter(),
+ initialFile, FileFilterEnum.XML.getExtensions()[0], null);
+
+ if (file != null) {
+ swingUI.executeBackgroundTask(new ITask() {
+
+ @Override
+ public Void doInBackground() throws Exception {
+ LOG.debug("Exporting variants to " + file.getAbsolutePath());
+
+ try {
+ BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
+ XStream xStream = new XStream(new DomDriver());
+ xStream.toXML(variantPkg, out);
+ out.close();
+ LOG.info("Exported variants succesfully");
+ } catch (IOException e) {
+ LOG.error("Could not export variants", e);
+ }
+
+ return null;
+ }
+
+ @Override
+ public void complete(Void result) {
+ swingUI.showMessage("Variants exported to \"" + file.getName() + "\".", "Success",
+ ISwingUI.INFORMATION_MESSAGE);
+ }
+
+ @Override
+ public void failed(Exception e) {
+ swingUI.showMessage("Could not export variants: " + e.getMessage(), "Error", ISwingUI.ERROR_MESSAGE);
+ }
+ }, true);
+ }
+ }
+ }
+
+ public static class ImportVariantsAction extends AbstractAction {
+
+ private static final long serialVersionUID = 1L;
+
+ private ISwingUI swingUI;
+ private IPlugInPort plugInPort;
+
+ public ImportVariantsAction(ISwingUI swingUI, IPlugInPort plugInPort) {
+ super();
+ this.swingUI = swingUI;
+ this.plugInPort = plugInPort;
+ putValue(AbstractAction.NAME, "Import Variants");
+ }
+
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ LOG.info("ImportVariantsAction triggered");
+
+
+ final File file =
+ DialogFactory.getInstance().showOpenDialog(FileFilterEnum.XML.getFilter(),
+ null, FileFilterEnum.XML.getExtensions()[0], null, swingUI.getOwnerFrame());
+
+ if (file != null) {
+ swingUI.executeBackgroundTask(new ITask() {
+
+ @Override
+ public Integer doInBackground() throws Exception {
+ return plugInPort.importVariants(file.getAbsolutePath());
+ }
+
+ @Override
+ public void complete(Integer result) {
+ swingUI.showMessage(result + " variant(s) imported from \"" + file.getName() + "\".", "Success",
+ ISwingUI.INFORMATION_MESSAGE);
+ }
+
+ @Override
+ public void failed(Exception e) {
+ swingUI.showMessage("Could not import variants: " + e.getMessage(), "Error", ISwingUI.ERROR_MESSAGE);
+ }
+ }, true);
+ }
+ }
+ }
+
+ public static class ExportBlocksAction extends AbstractAction {
+
+ private static final long serialVersionUID = 1L;
+
+ private ISwingUI swingUI;
+
+ public ExportBlocksAction(ISwingUI swingUI) {
+ super();
+ this.swingUI = swingUI;
+ putValue(AbstractAction.NAME, "Export Building Blocks");
+ // putValue(AbstractAction.SMALL_ICON, IconLoader.Print.getIcon());
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ LOG.info("ExportBuildingBlocksAction triggered");
+ Map>> blocks =
+ (Map>>) ConfigurationManager.getInstance().readObject(IPlugInPort.BLOCKS_KEY, null);
+ if (blocks == null || blocks.isEmpty()) {
+ swingUI.showMessage("No building blocks found to export.", "Error", IView.ERROR_MESSAGE);
+ return;
+ }
+
+ final BuildingBlockPackage variantPkg = new BuildingBlockPackage(blocks, System.getProperty("user.name"));
+
+ File initialFile =
+ new File(variantPkg.getOwner() == null ? "building blocks.xml" : ("building blocks by "
+ + variantPkg.getOwner().toLowerCase() + ".xml"));
+
+ final File file =
+ DialogFactory.getInstance().showSaveDialog(swingUI.getOwnerFrame(), FileFilterEnum.XML.getFilter(),
+ initialFile, FileFilterEnum.XML.getExtensions()[0], null);
+
+ if (file != null) {
+ swingUI.executeBackgroundTask(new ITask() {
+
+ @Override
+ public Void doInBackground() throws Exception {
+ LOG.debug("Exporting variants to " + file.getAbsolutePath());
+
+ try {
+ BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
+ XStream xStream = new XStream(new DomDriver());
+ xStream.toXML(variantPkg, out);
+ out.close();
+ LOG.info("Exported building blocks succesfully");
+ } catch (IOException e) {
+ LOG.error("Could not export building blocks", e);
+ }
+
+ return null;
+ }
+
+ @Override
+ public void complete(Void result) {
+ swingUI.showMessage("Building blocks exported to \"" + file.getName() + "\".", "Success",
+ ISwingUI.INFORMATION_MESSAGE);
+ }
+
+ @Override
+ public void failed(Exception e) {
+ swingUI.showMessage("Could not export building blocks: " + e.getMessage(), "Error", ISwingUI.ERROR_MESSAGE);
+ }
+ }, true);
+ }
+ }
+ }
+
+ public static class ImportBlocksAction extends AbstractAction {
+
+ private static final long serialVersionUID = 1L;
+
+ private ISwingUI swingUI;
+ private IPlugInPort plugInPort;
+
+ public ImportBlocksAction(ISwingUI swingUI, IPlugInPort plugInPort) {
+ super();
+ this.swingUI = swingUI;
+ this.plugInPort = plugInPort;
+ putValue(AbstractAction.NAME, "Import Building Blocks");
+ }
+
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ LOG.info("ImportBlocksAction triggered");
+
+
+ final File file =
+ DialogFactory.getInstance().showOpenDialog(FileFilterEnum.XML.getFilter(),
+ null, FileFilterEnum.XML.getExtensions()[0], null, swingUI.getOwnerFrame());
+
+ if (file != null) {
+ swingUI.executeBackgroundTask(new ITask() {
+
+ @Override
+ public Integer doInBackground() throws Exception {
+ return plugInPort.importBlocks(file.getAbsolutePath());
+ }
+
+ @Override
+ public void complete(Integer result) {
+ swingUI.showMessage(result + " building block(s) imported from \"" + file.getName() + "\".", "Success",
+ ISwingUI.INFORMATION_MESSAGE);
+ }
+
+ @Override
+ public void failed(Exception e) {
+ swingUI.showMessage("Could not import building blocks: " + e.getMessage(), "Error", ISwingUI.ERROR_MESSAGE);
+ }
+ }, true);
+ }
+ }
+ }
public static class ExitAction extends AbstractAction {
diff --git a/diylc/diylc-swing/src/org/diylc/swing/plugins/file/FileFilterEnum.java b/diylc/diylc-swing/src/org/diylc/swing/plugins/file/FileFilterEnum.java
index d9c32d4e..8117f0eb 100644
--- a/diylc/diylc-swing/src/org/diylc/swing/plugins/file/FileFilterEnum.java
+++ b/diylc/diylc-swing/src/org/diylc/swing/plugins/file/FileFilterEnum.java
@@ -29,7 +29,7 @@ public enum FileFilterEnum {
PNG("PNG Images (*.png)", "png"), PDF("PDF Files (*.pdf)", "pdf"), DIY("DIY Project Files (*.diy)", "diy"), EXCEL(
"Excel Workbooks (*.xls)", "xls"), CSV("Comma Separated Files (*.csv)", "csv"), HTML("HTML Files (*.html)",
- "html"), IMAGES("Image Files (*.png, *.jpg, *.gif)", "png", "jpg", "gif");
+ "html"), IMAGES("Image Files (*.png, *.jpg, *.gif)", "png", "jpg", "gif"), XML("XML Files (*.xml)", "xml");
FileFilter filter;
String[] extensions;
diff --git a/diylc/diylc-swing/src/org/diylc/swing/plugins/file/FileMenuPlugin.java b/diylc/diylc-swing/src/org/diylc/swing/plugins/file/FileMenuPlugin.java
index 4480d4cc..eae1b77d 100644
--- a/diylc/diylc-swing/src/org/diylc/swing/plugins/file/FileMenuPlugin.java
+++ b/diylc/diylc-swing/src/org/diylc/swing/plugins/file/FileMenuPlugin.java
@@ -46,7 +46,8 @@ public class FileMenuPlugin implements IPlugIn, IDynamicSubmenuHandler {
private static final Logger LOG = Logger.getLogger(FileMenuPlugin.class);
private static final String FILE_TITLE = "File";
- private static final String TRACE_MASK_TITLE = "Trace Mask";
+ private static final String TRACE_MASK_TITLE = "Trace Mask";
+ private static final String INTEGRATION_TITLE = "Integration";
private ProjectDrawingProvider drawingProvider;
private TraceMaskDrawingProvider traceMaskDrawingProvider;
@@ -84,7 +85,14 @@ public class FileMenuPlugin implements IPlugIn, IDynamicSubmenuHandler {
swingUI.injectMenuAction(
actionFactory.createPrintAction(traceMaskDrawingProvider, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()
| KeyEvent.SHIFT_DOWN_MASK), TRACE_MASK_TITLE);
- swingUI.injectMenuAction(actionFactory.createBomAction(plugInPort), FILE_TITLE);
+ swingUI.injectMenuAction(actionFactory.createBomAction(plugInPort), FILE_TITLE);
+ swingUI.injectMenuAction(null, FILE_TITLE);
+ swingUI.injectSubmenu(INTEGRATION_TITLE, IconLoader.Node.getIcon(), FILE_TITLE);
+ swingUI.injectMenuAction(actionFactory.createImportBlocksAction(swingUI, plugInPort), INTEGRATION_TITLE);
+ swingUI.injectMenuAction(actionFactory.createExportBlocksAction(swingUI), INTEGRATION_TITLE);
+ swingUI.injectMenuAction(null, INTEGRATION_TITLE);
+ swingUI.injectMenuAction(actionFactory.createImportVariantsAction(swingUI, plugInPort), INTEGRATION_TITLE);
+ swingUI.injectMenuAction(actionFactory.createExportVariantsAction(swingUI), INTEGRATION_TITLE);
swingUI.injectMenuAction(null, FILE_TITLE);
swingUI.injectMenuAction(actionFactory.createExitAction(plugInPort), FILE_TITLE);
}
diff --git a/diylc/diylc-swing/src/org/diylc/swing/plugins/toolbox/ComponentButtonFactory.java b/diylc/diylc-swing/src/org/diylc/swing/plugins/toolbox/ComponentButtonFactory.java
index 8e0e28b4..dace3c1b 100644
--- a/diylc/diylc-swing/src/org/diylc/swing/plugins/toolbox/ComponentButtonFactory.java
+++ b/diylc/diylc-swing/src/org/diylc/swing/plugins/toolbox/ComponentButtonFactory.java
@@ -126,7 +126,7 @@ public class ComponentButtonFactory {
}
});
- String defaultVariant = plugInPort.getDefaultVariant(componentType.getCategory(), componentType.getName());
+ String defaultVariant = plugInPort.getDefaultVariant(componentType);
JLabel label = new JLabel(variant.getName().equals(defaultVariant) ? IconLoader.PinGreen.getIcon() : IconLoader.PinGrey.getIcon());
label.setToolTipText(variant.getName().equals(defaultVariant) ? "Remove default variant" : "Set default variant");
@@ -140,7 +140,7 @@ public class ComponentButtonFactory {
JPopupMenu m = (JPopupMenu) c;
m.setVisible(false);
}
- plugInPort.setDefaultVariant(componentType.getCategory(), componentType.getName(), variant.getName());
+ plugInPort.setDefaultVariant(componentType, variant.getName());
e.consume();
}
});
@@ -167,7 +167,7 @@ public class ComponentButtonFactory {
if (result != JOptionPane.YES_OPTION) {
return;
}
- plugInPort.deleteVariant(componentType.getCategory(), componentType.getName(), variant.getName());
+ plugInPort.deleteVariant(componentType, variant.getName());
e.consume();
}
});
diff --git a/diylc/diylc-swing/src/org/diylc/swing/plugins/toolbox/ComponentTabbedPane.java b/diylc/diylc-swing/src/org/diylc/swing/plugins/toolbox/ComponentTabbedPane.java
index 20bd96f4..9ac39644 100644
--- a/diylc/diylc-swing/src/org/diylc/swing/plugins/toolbox/ComponentTabbedPane.java
+++ b/diylc/diylc-swing/src/org/diylc/swing/plugins/toolbox/ComponentTabbedPane.java
@@ -257,7 +257,7 @@ class ComponentTabbedPane extends JTabbedPane {
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
variantPopup.removeAll();
- List variants = plugInPort.getVariantsFor(componentType.getCategory(), componentType.getName());
+ List variants = plugInPort.getVariantsFor(componentType);
if (variants == null || variants.isEmpty()) {
JMenuItem item = new JMenuItem("");
item.setEnabled(false);
diff --git a/diylc/diylc-swing/src/org/diylc/swing/plugins/tree/TreePanel.java b/diylc/diylc-swing/src/org/diylc/swing/plugins/tree/TreePanel.java
index 8e72dc1e..ab5fe433 100644
--- a/diylc/diylc-swing/src/org/diylc/swing/plugins/tree/TreePanel.java
+++ b/diylc/diylc-swing/src/org/diylc/swing/plugins/tree/TreePanel.java
@@ -463,7 +463,7 @@ public class TreePanel extends JPanel {
popup.add(shortcutSubmenu);
popup.add(new JSeparator());
- List templates = plugInPort.getVariantsFor(componentType.getCategory(), componentType.getName());
+ List templates = plugInPort.getVariantsFor(componentType);
if (templates == null || templates.isEmpty()) {
JMenuItem item = new JMenuItem("");
item.setEnabled(false);
@@ -650,7 +650,7 @@ public class TreePanel extends JPanel {
}
if (payload.getComponentType() != null) {
- List variants = plugInPort.getVariantsFor(payload.getComponentType().getCategory(), payload.getComponentType().getName());
+ List variants = plugInPort.getVariantsFor(payload.getComponentType());
if (variants != null && !variants.isEmpty()) {
variantsHtml = " [+"
+ variants.size() + "]";
diff --git a/diylc/diylc-swing/update.xml b/diylc/diylc-swing/update.xml
index a78fa6f9..2adeddcf 100644
--- a/diylc/diylc-swing/update.xml
+++ b/diylc/diylc-swing/update.xml
@@ -2397,6 +2397,10 @@
NEW_FEATURE
Added extra work space around the layout that can be used to store additional components and helpers, but does not get exported to a file. Configurable from the menu
+
+
+ NEW_FEATURE
+ Ability to export and import building blocks and variants from the File menu
NEW_FEATURE