mirror of
https://github.com/bancika/diy-layout-creator
synced 2026-11-07 12:24:58 +10:00
Keep track of variant creation time and update when needed
This commit is contained in:
@@ -29,7 +29,7 @@ public interface IVariantProcessor {
|
||||
|
||||
public static final String TEMPLATES_KEY = "templates";
|
||||
public static final String DEFAULT_TEMPLATES_KEY = "defaultTemplates";
|
||||
public static final String DEFAULT_TEMPLATES_IMPORTED_KEY = "defaultTemplatesImported";
|
||||
public static final String DEFAULT_TEMPLATES_IMPORT_DATE_KEY = "defaultTemplatesImportDate";
|
||||
|
||||
void saveSelectedComponentAsVariant(String variantName);
|
||||
|
||||
|
||||
@@ -55,8 +55,7 @@ public class Angle {
|
||||
newValue += 360;
|
||||
while (newValue >= 360)
|
||||
newValue -= 360;
|
||||
valueCache.computeIfAbsent(newValue, (value) -> new Angle(value));
|
||||
return valueCache.get(newValue);
|
||||
return valueCache.computeIfAbsent(newValue, (value) -> new Angle(value));
|
||||
}
|
||||
|
||||
public Angle rotate(int direction) {
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
package org.diylc.core;
|
||||
|
||||
import java.awt.geom.Point2D;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -33,14 +34,20 @@ public class Template {
|
||||
@SuppressWarnings("unused")
|
||||
@Deprecated
|
||||
private boolean defaultFlag;
|
||||
private LocalDateTime createdOn;
|
||||
|
||||
public Template() {
|
||||
}
|
||||
|
||||
public Template(String name, Map<String, Object> values, List<Point2D> points) {
|
||||
this(name, values, points, LocalDateTime.now());
|
||||
}
|
||||
|
||||
public Template(String name, Map<String, Object> values, List<Point2D> points) {
|
||||
public Template(String name, Map<String, Object> values, List<Point2D> points, LocalDateTime createdOn) {
|
||||
this.name = name;
|
||||
this.values = values;
|
||||
this.points = points;
|
||||
this.points = points;
|
||||
this.createdOn = createdOn;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
@@ -65,45 +72,59 @@ public class Template {
|
||||
|
||||
public void setPoints(List<Point2D> points) {
|
||||
this.points = points;
|
||||
}
|
||||
|
||||
public LocalDateTime getCreatedOn() {
|
||||
return createdOn;
|
||||
}
|
||||
|
||||
public void setCreatedOn(LocalDateTime createdOn) {
|
||||
this.createdOn = createdOn;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
||||
result = prime * result + ((points == null) ? 0 : points.hashCode());
|
||||
result = prime * result + ((values == null) ? 0 : values.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Template other = (Template) obj;
|
||||
if (name == null) {
|
||||
if (other.name != null)
|
||||
return false;
|
||||
} else if (!name.equals(other.name))
|
||||
return false;
|
||||
if (points == null) {
|
||||
if (other.points != null)
|
||||
return false;
|
||||
} else if (!points.equals(other.points))
|
||||
return false;
|
||||
if (values == null) {
|
||||
if (other.values != null)
|
||||
return false;
|
||||
} else if (!values.equals(other.values))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((createdOn == null) ? 0 : createdOn.hashCode());
|
||||
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
||||
result = prime * result + ((points == null) ? 0 : points.hashCode());
|
||||
result = prime * result + ((values == null) ? 0 : values.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Template other = (Template) obj;
|
||||
if (createdOn == null) {
|
||||
if (other.createdOn != null)
|
||||
return false;
|
||||
} else if (!createdOn.equals(other.createdOn))
|
||||
return false;
|
||||
if (name == null) {
|
||||
if (other.name != null)
|
||||
return false;
|
||||
} else if (!name.equals(other.name))
|
||||
return false;
|
||||
if (points == null) {
|
||||
if (other.points != null)
|
||||
return false;
|
||||
} else if (!points.equals(other.points))
|
||||
return false;
|
||||
if (values == null) {
|
||||
if (other.values != null)
|
||||
return false;
|
||||
} else if (!values.equals(other.values))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name;
|
||||
|
||||
@@ -112,8 +112,6 @@ public @interface ComponentDescriptor {
|
||||
String keywordTag() default "";
|
||||
|
||||
/**
|
||||
* Only to be used for components that span in its eternity in the rectangle defined by the two outside control points.
|
||||
*
|
||||
* @return true if the component drawing should be cached
|
||||
*/
|
||||
boolean enableCache() default false;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
*
|
||||
l * DIY Layout Creator (DIYLC). Copyright (c) 2009-2018 held jointly by the individual authors.
|
||||
* l * DIY Layout Creator (DIYLC). Copyright (c) 2009-2018 held jointly by the individual authors.
|
||||
*
|
||||
* This file is part of DIYLC.
|
||||
*
|
||||
@@ -21,6 +21,7 @@ import java.io.BufferedInputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
@@ -28,7 +29,9 @@ import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeMap;
|
||||
import java.util.stream.Collectors;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.diylc.appframework.miscutils.IConfigurationManager;
|
||||
import org.diylc.common.ComponentType;
|
||||
@@ -39,11 +42,11 @@ import org.diylc.serialization.ProjectFileManager;
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
|
||||
public class VariantManager {
|
||||
|
||||
|
||||
private static final String VARIANTS_FILE_NAME = "variants.xml";
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(VariantManager.class);
|
||||
|
||||
|
||||
private IConfigurationManager<?> configManager;
|
||||
private XStream xStream;
|
||||
|
||||
@@ -56,43 +59,53 @@ public class VariantManager {
|
||||
@SuppressWarnings("unchecked")
|
||||
public void importDefaultVariants() {
|
||||
// import default templates from variants.xml file only if we didn't do it already
|
||||
if (!configManager.readBoolean(IPlugInPort.DEFAULT_TEMPLATES_IMPORTED_KEY, false)) {
|
||||
try {
|
||||
URL resource = Presenter.class.getResource(VARIANTS_FILE_NAME);
|
||||
if (resource != null) {
|
||||
BufferedInputStream in = new BufferedInputStream(resource.openStream());
|
||||
Map<String, List<Template>> defaults = (Map<String, List<Template>>) ProjectFileManager.xStreamSerializer.fromXML(in);
|
||||
in.close();
|
||||
|
||||
Map<String, List<Template>> variantMap =
|
||||
(Map<String, List<Template>>) configManager.readObject(IPlugInPort.TEMPLATES_KEY, null);
|
||||
if (variantMap == null)
|
||||
variantMap = new HashMap<String, List<Template>>();
|
||||
LocalDateTime importDate = (LocalDateTime) configManager
|
||||
.readObject(IPlugInPort.DEFAULT_TEMPLATES_IMPORT_DATE_KEY, LocalDateTime.MIN);
|
||||
|
||||
int importCount = 0;
|
||||
|
||||
try {
|
||||
URL resource = Presenter.class.getResource(VARIANTS_FILE_NAME);
|
||||
if (resource != null) {
|
||||
BufferedInputStream in = new BufferedInputStream(resource.openStream());
|
||||
Map<String, List<Template>> defaults =
|
||||
(Map<String, List<Template>>) ProjectFileManager.xStreamSerializer.fromXML(in);
|
||||
in.close();
|
||||
|
||||
Map<String, List<Template>> variantMap =
|
||||
(Map<String, List<Template>>) configManager.readObject(IPlugInPort.TEMPLATES_KEY, null);
|
||||
if (variantMap == null)
|
||||
variantMap = new HashMap<String, List<Template>>();
|
||||
|
||||
// merge default variants with user's
|
||||
for (Map.Entry<String, List<Template>> entry : defaults.entrySet()) {
|
||||
List<Template> existingVariants = variantMap.computeIfAbsent(entry.getKey(), key -> new ArrayList<Template>());
|
||||
Set<String> existingNames = existingVariants.stream()
|
||||
.map(x -> x.getName())
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
// merge default variants with user's
|
||||
for (Map.Entry<String, List<Template>> entry : defaults.entrySet()) {
|
||||
List<Template> templates = variantMap.getOrDefault(entry.getKey(), null);
|
||||
if (templates == null) {
|
||||
templates = new ArrayList<Template>();
|
||||
variantMap.put(entry.getKey(), templates);
|
||||
for (Template variant : entry.getValue()) {
|
||||
if ((variant.getCreatedOn() == null || importDate.compareTo(variant.getCreatedOn()) < 0)
|
||||
&& !existingNames.contains(variant.getName())) {
|
||||
existingVariants.add(variant);
|
||||
importCount++;
|
||||
}
|
||||
templates.addAll(entry.getValue());
|
||||
}
|
||||
|
||||
// update templates and a flag marking that we imported them
|
||||
configManager.writeValue(IPlugInPort.DEFAULT_TEMPLATES_IMPORTED_KEY, true);
|
||||
configManager.writeValue(IPlugInPort.TEMPLATES_KEY, variantMap);
|
||||
LOG.info(String.format("Imported default variants for %d components",
|
||||
defaults == null ? 0 : defaults.size()));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.error("Could not load default variants", e);
|
||||
|
||||
// update templates and a flag marking that we imported them
|
||||
configManager.writeValue(IPlugInPort.DEFAULT_TEMPLATES_IMPORT_DATE_KEY,
|
||||
LocalDateTime.now());
|
||||
configManager.writeValue(IPlugInPort.TEMPLATES_KEY, variantMap);
|
||||
LOG.info(String.format("Imported default %d variants", importCount));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.error("Could not load default variants", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static boolean upgradedVariants = false;
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public synchronized void upgradeVariants(Map<String, List<ComponentType>> componentTypes) {
|
||||
if (upgradedVariants)
|
||||
@@ -101,22 +114,24 @@ public class VariantManager {
|
||||
upgradedVariants = true;
|
||||
|
||||
LOG.info("Checking if variants need to be updated");
|
||||
Map<String, List<Template>> lookupMap = new TreeMap<String, List<Template>>(String.CASE_INSENSITIVE_ORDER);
|
||||
Map<String, List<Template>> lookupMap =
|
||||
new TreeMap<String, List<Template>>(String.CASE_INSENSITIVE_ORDER);
|
||||
Map<String, List<Template>> variantMap =
|
||||
(Map<String, List<Template>>) configManager.readObject(IPlugInPort.TEMPLATES_KEY, null);
|
||||
|
||||
if (variantMap == null)
|
||||
return;
|
||||
|
||||
Map<String, ComponentType> typeMap = new TreeMap<String, ComponentType>(String.CASE_INSENSITIVE_ORDER);
|
||||
Map<String, ComponentType> typeMap =
|
||||
new TreeMap<String, ComponentType>(String.CASE_INSENSITIVE_ORDER);
|
||||
|
||||
for (Map.Entry<String, List<ComponentType>> 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);
|
||||
typeMap.put(type.getCategory().replace("Electro-Mechanical", "Electromechanical") + "."
|
||||
+ type.getName(), type);
|
||||
}
|
||||
|
||||
Map<String, List<Template>> newVariantMap = new HashMap<String, List<Template>>();
|
||||
@@ -125,10 +140,11 @@ public class VariantManager {
|
||||
|
||||
for (Map.Entry<String, List<Template>> entry : variantMap.entrySet()) {
|
||||
if (typeMap.containsKey(entry.getKey())) {
|
||||
newVariantMap.put(typeMap.get(entry.getKey()).getInstanceClass().getCanonicalName(), entry.getValue()); // great,
|
||||
// nothing
|
||||
// to
|
||||
// upgrade
|
||||
newVariantMap.put(typeMap.get(entry.getKey()).getInstanceClass().getCanonicalName(),
|
||||
entry.getValue()); // great,
|
||||
// nothing
|
||||
// to
|
||||
// upgrade
|
||||
} else {
|
||||
LOG.warn("Could not upgrade variants for: " + entry.getKey());
|
||||
}
|
||||
@@ -136,10 +152,11 @@ public class VariantManager {
|
||||
|
||||
configManager.writeValue(IPlugInPort.TEMPLATES_KEY, newVariantMap);
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Template> getVariantsFor(ComponentType type) {
|
||||
Map<String, List<Template>> lookupMap = new TreeMap<String, List<Template>>(String.CASE_INSENSITIVE_ORDER);
|
||||
Map<String, List<Template>> lookupMap =
|
||||
new TreeMap<String, List<Template>>(String.CASE_INSENSITIVE_ORDER);
|
||||
|
||||
Map<String, List<Template>> variantMap =
|
||||
(Map<String, List<Template>>) configManager.readObject(IPlugInPort.TEMPLATES_KEY, null);
|
||||
@@ -168,12 +185,12 @@ public class VariantManager {
|
||||
});
|
||||
return variants;
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public int importVariants(String fileName) throws IOException {
|
||||
LOG.debug(String.format("importVariants(%s)", fileName));
|
||||
BufferedInputStream in = new BufferedInputStream(new FileInputStream(fileName));
|
||||
|
||||
BufferedInputStream in = new BufferedInputStream(new FileInputStream(fileName));
|
||||
|
||||
VariantPackage pkg = (VariantPackage) xStream.fromXML(in);
|
||||
|
||||
in.close();
|
||||
@@ -195,7 +212,8 @@ public class VariantManager {
|
||||
variantMap.put(entry.getKey(), templates);
|
||||
}
|
||||
for (Template t : entry.getValue()) {
|
||||
templates.add(new Template(t.getName() + " [" + pkg.getOwner() + "]", t.getValues(), t.getPoints()));
|
||||
templates.add(
|
||||
new Template(t.getName() + " [" + pkg.getOwner() + "]", t.getValues(), t.getPoints()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -205,7 +223,7 @@ public class VariantManager {
|
||||
|
||||
return pkg.getVariants().size();
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public String getDefaultVariant(ComponentType type) {
|
||||
Map<String, String> defaultTemplateMap =
|
||||
@@ -221,7 +239,7 @@ public class VariantManager {
|
||||
|
||||
return defaultTemplateMap.get(key2);
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void deleteVariant(ComponentType type, String templateName) {
|
||||
LOG.debug(String.format("deleteTemplate(%s, %s)", type, templateName));
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user