From bfc6721974ebb4788c3f71ab1ef6a8d012ab9071 Mon Sep 17 00:00:00 2001 From: bancika Date: Sat, 4 Apr 2020 11:58:03 +0200 Subject: [PATCH] add default building blocks functionality --- .../src/org/diylc/common/IBlockProcessor.java | 3 +- .../src/org/diylc/presenter/Presenter.java | 61 ++- .../src/org/diylc/presenter/blocks.xml | 385 ++++++++++++++++++ .../src/org/diylc/presenter/update.xml | 6 +- diylc/diylc-swing/DIYLCStarter.launch | 2 +- 5 files changed, 453 insertions(+), 4 deletions(-) create mode 100644 diylc/diylc-core/src/org/diylc/presenter/blocks.xml diff --git a/diylc/diylc-core/src/org/diylc/common/IBlockProcessor.java b/diylc/diylc-core/src/org/diylc/common/IBlockProcessor.java index b9f93fae..85aa96a7 100644 --- a/diylc/diylc-core/src/org/diylc/common/IBlockProcessor.java +++ b/diylc/diylc-core/src/org/diylc/common/IBlockProcessor.java @@ -25,7 +25,8 @@ import java.io.IOException; public interface IBlockProcessor { - public static final String BLOCKS_KEY = "buildingBlocks"; + public static final String BLOCKS_KEY = "buildingBlocks"; + public static final String DEFAULT_BLOCKS_IMPORTED_KEY = "defaultBlocksImported"; void saveSelectionAsBlock(String blockName); diff --git a/diylc/diylc-core/src/org/diylc/presenter/Presenter.java b/diylc/diylc-core/src/org/diylc/presenter/Presenter.java index 145255f7..11af1888 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 { // visibleLayers = EnumSet.allOf(ComponentLayer.class); upgradeVariants(); importDefaultVariants(); + importDefaultBlocks(); } public void installPlugin(IPlugIn plugIn) { @@ -2457,6 +2458,40 @@ public class Presenter implements IPlugInPort { } } } + + @SuppressWarnings("unchecked") + private void importDefaultBlocks() { + // import default templates from variants.xml file only if we didn't do it already + if (!ConfigurationManager.getInstance().readBoolean(DEFAULT_BLOCKS_IMPORTED_KEY, false)) { + try { + URL resource = Presenter.class.getResource("blocks.xml"); + if (resource != null) { + BufferedInputStream in = new BufferedInputStream(resource.openStream()); + XStream xStream = new XStream(new DomDriver()); + Map>> defaults = (Map>>) xStream.fromXML(in); + in.close(); + + Map>> blocksMap = + (Map>>) ConfigurationManager.getInstance().readObject(BLOCKS_KEY, null); + if (blocksMap == null) + blocksMap = new HashMap>>(); + + // merge default blocks with user's + for (Map.Entry>> entry : defaults.entrySet()) { + if (!blocksMap.containsKey(entry.getKey())) + blocksMap.put(entry.getKey(), entry.getValue()); + } + + // update templates and a flag marking that we imported them + ConfigurationManager.getInstance().writeValue(DEFAULT_BLOCKS_IMPORTED_KEY, true); + ConfigurationManager.getInstance().writeValue(BLOCKS_KEY, blocksMap); + LOG.info(String.format("Imported %d default building blocks", defaults == null ? 0 : defaults.size())); + } + } catch (Exception e) { + LOG.error("Could not load default blocks", e); + } + } + } @SuppressWarnings("unchecked") @Override @@ -2667,7 +2702,31 @@ public class Presenter implements IPlugInPort { } }); blocks.put(blockName, blockComponents); - ConfigurationManager.getInstance().writeValue(BLOCKS_KEY, blocks); + + if (System.getProperty("org.diylc.WriteStaticBlocks", "false").equalsIgnoreCase("true")) { + Map>> defaultBlockMap = new HashMap>>(); + // unify default and user-variants + for (Map.Entry>> entry : blocks.entrySet()) { + if (defaultBlockMap.containsKey(entry.getKey())) { + defaultBlockMap.get(entry.getKey()).addAll(entry.getValue()); + } else { + defaultBlockMap.put(entry.getKey(), entry.getValue()); + } + } + try { + BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("blocks.xml")); + XStream xStream = new XStream(new DomDriver()); + xStream.toXML(defaultBlockMap, out); + out.close(); + // no more user variants + ConfigurationManager.getInstance().writeValue(BLOCKS_KEY, null); + LOG.info("Saved default blocks"); + } catch (IOException e) { + LOG.error("Could not save default blocks", e); + } + } else { + ConfigurationManager.getInstance().writeValue(BLOCKS_KEY, blocks); + } } @SuppressWarnings("unchecked") diff --git a/diylc/diylc-core/src/org/diylc/presenter/blocks.xml b/diylc/diylc-core/src/org/diylc/presenter/blocks.xml new file mode 100644 index 00000000..de6dde42 --- /dev/null +++ b/diylc/diylc-core/src/org/diylc/presenter/blocks.xml @@ -0,0 +1,385 @@ + + + TO-92 w/ 2 Leads + + + J1 + 127 + + + 580 + 720 + + + 580 + 740 + + + 1320 + 250 + + + + 255 + 255 + 255 + 255 + + + 0 + 0 + 0 + 255 + + + + 117 + 157 + 175 + 255 + + NAME + false + Directional + false + + + + J2 + 127 + + + 580 + 780 + + + 580 + 760 + + + -177 + 1259 + + + + 255 + 255 + 255 + 255 + + + 0 + 0 + 0 + 255 + + + + 117 + 157 + 175 + 255 + + NAME + false + Directional + false + + + + Q1 + 100 + + DEFAULT + + + 580 + 740 + + + 580 + 750 + + + 580 + 760 + + + + 128 + 128 + 128 + 255 + + + 89 + 89 + 89 + 255 + + + NAME + false + + 0.05 + in + + + + + + TO-92 w/ 3 Leads + + + J1 + 127 + + + 720 + 620 + + + 760 + 640 + + + 650 + -41 + + + + 255 + 255 + 255 + 255 + + + 0 + 0 + 0 + 255 + + + + 117 + 157 + 175 + 255 + + NAME + false + Directional + false + + + + J2 + 127 + + + 720 + 680 + + + 760 + 660 + + + -206 + 95 + + + + 255 + 255 + 255 + 255 + + + 0 + 0 + 0 + 255 + + + + 117 + 157 + 175 + 255 + + NAME + false + Directional + false + + + + J3 + 127 + + + 719 + 650 + + + 760 + 650 + + + 147 + -279 + + + + 255 + 255 + 255 + 255 + + + 0 + 0 + 0 + 255 + + + + 117 + 157 + 175 + 255 + + NAME + false + Directional + false + + + + Q1 + 100 + + DEFAULT + + + 760 + 640 + + + 760 + 650 + + + 760 + 660 + + + + 128 + 128 + 128 + 255 + + + 89 + 89 + 89 + 255 + + + NAME + false + + 0.05 + in + + + + + + TO-92 w/ 2 Leads + + + J1 + 127 + + + 580 + 720 + + + 580 + 740 + + + 1320 + 250 + + + + + + + NAME + false + Directional + false + + + + J2 + 127 + + + 580 + 780 + + + 580 + 760 + + + -177 + 1259 + + + + + + + NAME + false + Directional + false + + + + Q1 + 100 + + DEFAULT + + + 580 + 740 + + + 580 + 750 + + + 580 + 760 + + + + + + NAME + false + + + + + \ No newline at end of file diff --git a/diylc/diylc-core/src/org/diylc/presenter/update.xml b/diylc/diylc-core/src/org/diylc/presenter/update.xml index 31e2069f..e6749ed5 100644 --- a/diylc/diylc-core/src/org/diylc/presenter/update.xml +++ b/diylc/diylc-core/src/org/diylc/presenter/update.xml @@ -2849,10 +2849,14 @@ NEW_FEATURE Added "Ruler Inch Subdivision" config option that allows inch rulers to be subdivided either using base of 2 (1/4", 1/8, 1/16) or base of 10 (1/10) + + + NEW_FEATURE + Added few out-of-the-box building blocks that can be deleted if unwanted BUG_FIX - Fix the issue with theme configuration that causes some components to fail to render + Fix the issue with theme configuration that causes the app to fail to start and some components to fail to render https://github.com/bancika/diy-layout-creator/releases diff --git a/diylc/diylc-swing/DIYLCStarter.launch b/diylc/diylc-swing/DIYLCStarter.launch index 8833c28d..a56fd82d 100644 --- a/diylc/diylc-swing/DIYLCStarter.launch +++ b/diylc/diylc-swing/DIYLCStarter.launch @@ -11,5 +11,5 @@ - +