Apply datasheet model to existing components

This commit is contained in:
Bane Stojkovic
2022-11-06 15:29:11 +01:00
parent 9d9de0bb4d
commit c297459ff2
19 changed files with 1708 additions and 1464 deletions
@@ -37,7 +37,14 @@ public interface ISelectionProcessor {
*
* @return
*/
Collection<IDIYComponent<?>> getSelectedComponents();
Collection<IDIYComponent<?>> getSelectedComponents();
/**
* Returns selected component types
*
* @return
*/
Collection<ComponentType> getSelectedComponentTypes();
/**
* Updates the selection with the specified list of component. Also, updates control point map
@@ -121,5 +128,13 @@ public interface ISelectionProcessor {
*
* @param criteria
*/
void selectMatching(String criteria);
void selectMatching(String criteria);
/**
* Attempts to apply the specified model to all selected components.
*
* @param model
*/
void applyModelToSelection(String[] model);
}
@@ -0,0 +1,27 @@
/*
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.core;
public interface IDatasheetSupport {
void applyModel(String[] model);
}
@@ -60,7 +60,7 @@ public class InstantiationManager {
private ComponentType componentTypeSlot;
private Template template;
private String[] parameters;
private String[] model;
private List<IDIYComponent<?>> componentSlot;
private Point2D firstControlPoint;
private Point2D potentialControlPoint;
@@ -82,8 +82,8 @@ public class InstantiationManager {
return template;
}
public String[] getParameters() {
return parameters;
public String[] getModel() {
return model;
}
public List<IDIYComponent<?>> getComponentSlot() {
@@ -102,12 +102,12 @@ public class InstantiationManager {
this.potentialControlPoint = potentialControlPoint;
}
public void setComponentTypeSlot(ComponentType componentTypeSlot, Template template, String[] parameters,
public void setComponentTypeSlot(ComponentType componentTypeSlot, Template template, String[] model,
Project currentProject, boolean forceInstatiate)
throws Exception {
this.componentTypeSlot = componentTypeSlot;
this.template = template;
this.parameters = parameters;
this.model = model;
if (componentTypeSlot == null) {
this.componentSlot = null;
} else {
@@ -277,11 +277,11 @@ public class InstantiationManager {
// Instantiate the component.
IDIYComponent<?> component;
if (parameters == null) {
if (model == null) {
component = componentType.getInstanceClass().newInstance();
} else {
Constructor<? extends IDIYComponent<?>> constructor = componentType.getInstanceClass().getConstructor(String[].class);
component = constructor.newInstance((Object)parameters);
component = constructor.newInstance((Object)model);
}
component.createdIn(currentProject);
@@ -45,6 +45,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;
import org.diylc.appframework.miscutils.IConfigurationManager;
@@ -67,6 +68,7 @@ import org.diylc.common.IProjectEditor;
import org.diylc.common.PropertyWrapper;
import org.diylc.core.ExpansionMode;
import org.diylc.core.IDIYComponent;
import org.diylc.core.IDatasheetSupport;
import org.diylc.core.IView;
import org.diylc.core.Project;
import org.diylc.core.Template;
@@ -605,7 +607,7 @@ public class Presenter implements IPlugInPort {
// Keep the reference to component type for later.
ComponentType componentTypeSlot = instantiationManager.getComponentTypeSlot();
Template template = instantiationManager.getTemplate();
String[] parameters = instantiationManager.getParameters();
String[] model = instantiationManager.getModel();
Project oldProject = currentProject.clone();
switch (componentTypeSlot.getCreationMethod()) {
case SINGLE_CLICK:
@@ -640,7 +642,7 @@ public class Presenter implements IPlugInPort {
editSelection();
}
if (configManager.readBoolean(IPlugInPort.CONTINUOUS_CREATION_KEY, false)) {
setNewComponentTypeSlot(componentTypeSlot, template, parameters, false);
setNewComponentTypeSlot(componentTypeSlot, template, model, false);
} else {
setNewComponentTypeSlot(null, null, null, false);
}
@@ -664,7 +666,7 @@ public class Presenter implements IPlugInPort {
} else {
// On the second click, add the component to the
// project.
addPendingComponentsToProject(scaledPoint, componentTypeSlot, template, parameters);
addPendingComponentsToProject(scaledPoint, componentTypeSlot, template, model);
}
break;
default:
@@ -2409,6 +2411,28 @@ public class Presenter implements IPlugInPort {
drawingManager.fireZoomChanged();
}
}
@Override
public void applyModelToSelection(String[] model) {
Project oldProject = currentProject.clone();
try {
selectedComponents.forEach(component -> {
((IDatasheetSupport) component).applyModel(model);
});
} catch (Exception e) {
LOG.warn("Could not apply datasheet to component", e);
view.showMessage(APPLY_ERROR, ERROR, IView.ERROR_MESSAGE);
} finally {
// Notify the listeners.
if (!oldProject.equals(currentProject)) {
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, oldProject,
currentProject.clone(), "Edit Project");
drawingManager.clearContinuityArea();
projectFileManager.notifyFileChange();
}
drawingManager.fireZoomChanged();
}
}
@Override
public ComponentType getNewComponentTypeSlot() {
@@ -2416,7 +2440,7 @@ public class Presenter implements IPlugInPort {
}
@Override
public void setNewComponentTypeSlot(ComponentType componentType, Template template, String[] parameters, boolean forceInstatiate) {
public void setNewComponentTypeSlot(ComponentType componentType, Template template, String[] model, boolean forceInstatiate) {
LOG.info(String.format("setNewComponentSlot(%s)", componentType == null ? null : componentType.getName()));
// record a test step if needed
@@ -2453,7 +2477,7 @@ public class Presenter implements IPlugInPort {
}
try {
instantiationManager.setComponentTypeSlot(componentType, template, parameters, currentProject, forceInstatiate);
instantiationManager.setComponentTypeSlot(componentType, template, model, currentProject, forceInstatiate);
if (forceInstatiate)
updateSelection(instantiationManager.getComponentSlot());
@@ -2599,6 +2623,16 @@ public class Presenter implements IPlugInPort {
}
return getVariantsFor(selectedType);
}
@SuppressWarnings("unchecked")
@Override
public Collection<ComponentType> getSelectedComponentTypes() {
return this.getSelectedComponents().stream()
.map(component -> ComponentProcessor.getInstance().extractComponentTypeFrom(
(Class<? extends IDIYComponent<?>>) component.getClass()))
.distinct()
.collect(Collectors.toList());
}
@Override
public void applyVariantToSelection(Template template) {