building blocks

This commit is contained in:
bancika
2017-01-12 15:50:54 +01:00
parent c64c9fb5b2
commit 0555d4b86e
18 changed files with 290 additions and 63 deletions
@@ -0,0 +1,38 @@
package org.diylc.common;
import java.util.Collection;
import org.diylc.core.IDIYComponent;
public class BuildingBlock {
private String name;
private Collection<IDIYComponent<?>> components;
public BuildingBlock(String name, Collection<IDIYComponent<?>> components) {
super();
this.name = name;
this.components = components;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Collection<IDIYComponent<?>> getComponents() {
return components;
}
public void setComponents(Collection<IDIYComponent<?>> components) {
this.components = components;
}
@Override
public String toString() {
return name;
}
}
@@ -0,0 +1,24 @@
package org.diylc.common;
public interface IBlockProcessor {
public static final String BLOCKS_KEY = "buildingBlocks";
void saveSelectionAsBlock(String blockName);
void loadBlock(String blockName) throws InvalidBlockException;
void deleteBlock(String blockName);
public class BlockAlreadyExistsException extends Exception {
private static final long serialVersionUID = 1L;
}
public class InvalidBlockException extends Exception {
private static final long serialVersionUID = 1L;
}
}
@@ -5,6 +5,7 @@ import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Point2D;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -29,7 +30,7 @@ import org.diylc.core.Theme;
* @see IMessageListener
* @see EventType
*/
public interface IPlugInPort extends ISelectionProcessor, IMouseProcessor, IKeyProcessor, ITemplateProcessor {
public interface IPlugInPort extends ISelectionProcessor, IMouseProcessor, IKeyProcessor, ITemplateProcessor, IBlockProcessor {
public static final String ANTI_ALIASING_KEY = "antiAliasing";
public static final String HI_QUALITY_RENDER_KEY = "hiQualityRender";
@@ -178,7 +179,7 @@ public interface IPlugInPort extends ISelectionProcessor, IMouseProcessor, IKeyP
*
* @param components
*/
void pasteComponents(List<IDIYComponent<?>> components);
void pasteComponents(Collection<IDIYComponent<?>> components);
/**
* Deletes all the selected components from the project.
@@ -4,6 +4,7 @@ import java.awt.Point;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -110,7 +111,7 @@ public class InstantiationManager {
return changeMade;
}
public void pasteComponents(List<IDIYComponent<?>> components, Point scaledPoint, boolean snapToGrid, Size gridSpacing) {
public void pasteComponents(Collection<IDIYComponent<?>> components, Point scaledPoint, boolean snapToGrid, Size gridSpacing) {
// Adjust location of components so they are centered under the mouse
// cursor
int minX = Integer.MAX_VALUE;
@@ -1202,7 +1202,7 @@ public class Presenter implements IPlugInPort {
if (view.showConfirmDialog("Selection contains components that cannot be mirrored. Do you want to exclude them?",
"Mirror Selection", IView.YES_NO_OPTION, IView.QUESTION_MESSAGE) != IView.YES_OPTION)
return;
if (changesCircuit)
if (view.showConfirmDialog("Mirroring operation will change the circuit. Do you want to continue?",
"Mirror Selection", IView.YES_NO_OPTION, IView.QUESTION_MESSAGE) != IView.YES_OPTION)
@@ -1296,7 +1296,7 @@ public class Presenter implements IPlugInPort {
}
@Override
public void pasteComponents(List<IDIYComponent<?>> components) {
public void pasteComponents(Collection<IDIYComponent<?>> components) {
LOG.info(String.format("pasteComponents(%s)", components));
instantiationManager.pasteComponents(components, this.previousScaledPoint, isSnapToGrid(),
currentProject.getGridSpacing());
@@ -1969,7 +1969,7 @@ public class Presenter implements IPlugInPort {
for (IDIYComponent<?> component : this.selectedComponents) {
try {
drawingManager.invalidateComponent(component);
// this.instantiationManager.loadComponentShapeFromTemplate(component, template);
// this.instantiationManager.loadComponentShapeFromTemplate(component, template);
this.instantiationManager.fillWithDefaultProperties(component, template);
} catch (Exception e) {
LOG.warn("Could not apply templates to " + component.getName(), e);
@@ -2050,4 +2050,47 @@ public class Presenter implements IPlugInPort {
return point == null ? null : new Point((int) (point.x / drawingManager.getZoomLevel()),
(int) (point.y / drawingManager.getZoomLevel()));
}
@SuppressWarnings("unchecked")
@Override
public void saveSelectionAsBlock(String blockName) {
Map<String, Collection<IDIYComponent<?>>> blocks =
(Map<String, Collection<IDIYComponent<?>>>) ConfigurationManager.getInstance().readObject(BLOCKS_KEY, null);
if (blocks == null)
blocks = new HashMap<String, Collection<IDIYComponent<?>>>();
blocks.put(blockName, this.selectedComponents);
ConfigurationManager.getInstance().writeValue(BLOCKS_KEY, blocks);
}
@SuppressWarnings("unchecked")
@Override
public void loadBlock(String blockName) throws InvalidBlockException {
Map<String, Collection<IDIYComponent<?>>> blocks =
(Map<String, Collection<IDIYComponent<?>>>) ConfigurationManager.getInstance().readObject(BLOCKS_KEY, null);
if (blocks != null) {
Collection<IDIYComponent<?>> components = blocks.get(blockName);
if (components == null)
throw new InvalidBlockException();
List<IDIYComponent<?>> clones = new ArrayList<IDIYComponent<?>>();
for (IDIYComponent<?> c : components)
try {
clones.add(c.clone());
} catch (CloneNotSupportedException e) {
LOG.error("Could not clone component: " + c);
}
pasteComponents(clones);
} else
throw new InvalidBlockException();
}
@SuppressWarnings("unchecked")
@Override
public void deleteBlock(String blockName) {
Map<String, Collection<IDIYComponent<?>>> blocks =
(Map<String, Collection<IDIYComponent<?>>>) ConfigurationManager.getInstance().readObject(BLOCKS_KEY, null);
if (blocks != null) {
blocks.remove(blockName);
ConfigurationManager.getInstance().writeValue(BLOCKS_KEY, blocks);
}
}
}