mirror of
https://github.com/bancika/diy-layout-creator
synced 2026-11-10 17:22:09 +10:00
import/export variants and building blocks
This commit is contained in:
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
package org.diylc.common;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
import org.diylc.core.IDIYComponent;
|
||||
|
||||
public class BuildingBlockPackage {
|
||||
|
||||
private Map<String, Collection<IDIYComponent<?>>> block;
|
||||
private String owner;
|
||||
|
||||
public BuildingBlockPackage(Map<String, Collection<IDIYComponent<?>>> blocks, String owner) {
|
||||
super();
|
||||
this.block = blocks;
|
||||
this.owner = owner;
|
||||
}
|
||||
|
||||
public Map<String, Collection<IDIYComponent<?>>> getBlocks() {
|
||||
return block;
|
||||
}
|
||||
|
||||
public String getOwner() {
|
||||
return owner;
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
|
||||
@@ -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<Template> getVariantsFor(String categoryName, String componentTypeName);
|
||||
List<Template> getVariantsFor(ComponentType type);
|
||||
|
||||
List<Template> 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 {
|
||||
|
||||
|
||||
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
package org.diylc.common;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.diylc.core.Template;
|
||||
|
||||
public class VariantPackage {
|
||||
|
||||
private Map<String, List<Template>> variants;
|
||||
private String owner;
|
||||
|
||||
public VariantPackage(Map<String, List<Template>> variants, String owner) {
|
||||
super();
|
||||
this.variants = variants;
|
||||
this.owner = owner;
|
||||
}
|
||||
|
||||
public Map<String, List<Template>> getVariants() {
|
||||
return variants;
|
||||
}
|
||||
|
||||
public String getOwner() {
|
||||
return owner;
|
||||
}
|
||||
}
|
||||
@@ -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<Template> templates = getVariantsFor(componentType.getCategory(), componentType.getName());
|
||||
String defaultTemplate = getDefaultVariant(componentType);
|
||||
List<Template> 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, List<Template>>();
|
||||
}
|
||||
String key = type.getCategory() + "." + type.getName();
|
||||
String key = type.getInstanceClass().getCanonicalName();
|
||||
List<Template> variants = variantMap.get(key);
|
||||
if (variants == null) {
|
||||
variants = new ArrayList<Template>();
|
||||
@@ -2293,7 +2295,7 @@ public class Presenter implements IPlugInPort {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public List<Template> getVariantsFor(String categoryName, String componentTypeName) {
|
||||
public List<Template> getVariantsFor(ComponentType type) {
|
||||
Map<String, List<Template>> lookupMap = new TreeMap<String, List<Template>>(String.CASE_INSENSITIVE_ORDER);
|
||||
|
||||
Map<String, List<Template>> 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<Template> variants = new ArrayList<Template>();
|
||||
if (variantMap != null) {
|
||||
List<Template> userVariants = variantMap.get(categoryName + "." + componentTypeName);
|
||||
List<Template> 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<Template> defaultVariants = defaultVariantMap.get(categoryName + "." + componentTypeName);
|
||||
List<Template> 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<String, List<Template>> templateMap =
|
||||
(Map<String, List<Template>>) ConfigurationManager.getInstance().readObject(TEMPLATES_KEY, null);
|
||||
if (templateMap != null) {
|
||||
List<Template> 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<Template> templates = templateMap.get(key1);
|
||||
if (templates != null) {
|
||||
Iterator<Template> 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<Template> 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<String, String> defaultTemplateMap =
|
||||
(Map<String, String>) ConfigurationManager.getInstance().readObject(DEFAULT_TEMPLATES_KEY, null);
|
||||
if (defaultTemplateMap == null)
|
||||
defaultTemplateMap = new HashMap<String, String>();
|
||||
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<String, String> defaultTemplateMap =
|
||||
(Map<String, String>) 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<IDIYComponent<?>> 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<String, List<Template>> variantMap =
|
||||
(Map<String, List<Template>>) ConfigurationManager.getInstance().readObject(TEMPLATES_KEY, null);
|
||||
if (variantMap == null) {
|
||||
variantMap = new HashMap<String, List<Template>>();
|
||||
}
|
||||
|
||||
for (Map.Entry<String, List<Template>> entry : pkg.getVariants().entrySet()) {
|
||||
List<Template> templates;
|
||||
templates = variantMap.get(entry.getKey());
|
||||
if (templates == null)
|
||||
{
|
||||
templates = new ArrayList<Template>();
|
||||
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<String, Collection<IDIYComponent<?>>> blocks =
|
||||
(Map<String, Collection<IDIYComponent<?>>>) ConfigurationManager.getInstance().readObject(BLOCKS_KEY, null);
|
||||
if (blocks == null) {
|
||||
blocks = new HashMap<String, Collection<IDIYComponent<?>>>();
|
||||
}
|
||||
|
||||
for (Map.Entry<String, Collection<IDIYComponent<?>>> 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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user