apply template to existing components

do not mess up selection on right click
popup menu improvements
This commit is contained in:
bancika
2013-04-29 10:08:36 +00:00
parent 1714ddb444
commit 91deb1d7da
4 changed files with 87 additions and 13 deletions
@@ -12,6 +12,10 @@ public interface ITemplateProcessor {
List<Template> getTemplatesFor(String categoryName, String componentTypeName); List<Template> getTemplatesFor(String categoryName, String componentTypeName);
List<Template> getTemplatesForSelection();
void applyTemplateToSelection(Template template);
void deleteTemplate(String categoryName, String componentTypeName, String templateName); void deleteTemplate(String categoryName, String componentTypeName, String templateName);
public class TemplateAlreadyExistsException extends Exception { public class TemplateAlreadyExistsException extends Exception {
@@ -233,16 +233,8 @@ public class InstantiationManager {
component.setControlPoint(controlPoint, j); component.setControlPoint(controlPoint, j);
} }
} }
// Make the component into shape from the template
if (template != null && template.getPoints() != null loadComponentShapeFromTemplate(component, template);
&& template.getPoints().size() >= component
.getControlPointCount()) {
for (int i = 0; i < component.getControlPointCount(); i++) {
Point p = new Point(component.getControlPoint(0));
p.translate(template.getPoints().get(i).x, template.getPoints().get(i).y);
component.setControlPoint(p, i);
}
}
fillWithDefaultProperties(component, template); fillWithDefaultProperties(component, template);
@@ -340,6 +332,27 @@ public class InstantiationManager {
} }
} }
/**
* Uses stored control points from the template to shape component.
*
* @param component
* @param template
*/
public void loadComponentShapeFromTemplate(IDIYComponent<?> component,
Template template) {
if (template != null
&& template.getPoints() != null
&& template.getPoints().size() >= component
.getControlPointCount()) {
for (int i = 0; i < component.getControlPointCount(); i++) {
Point p = new Point(component.getControlPoint(0));
p.translate(template.getPoints().get(i).x, template.getPoints()
.get(i).y);
component.setControlPoint(p, i);
}
}
}
public void tryToRotateComponentSlot() { public void tryToRotateComponentSlot() {
if (this.componentSlot == null) { if (this.componentSlot == null) {
LOG.debug("Component slot is empty, cannot rotate"); LOG.debug("Component slot is empty, cannot rotate");
@@ -591,7 +591,10 @@ public class Presenter implements IPlugInPort {
} }
} else { } else {
// Otherwise just select that one component. // Otherwise just select that one component.
if (button == BUTTON1
|| !selectedComponents.contains(component)) {
newSelection.clear(); newSelection.clear();
}
newSelection newSelection
.addAll(findAllGroupedComponents(component)); .addAll(findAllGroupedComponents(component));
} }
@@ -1007,8 +1010,8 @@ public class Presenter implements IPlugInPort {
int dx = (scaledPoint.x - previousDragPoint.x); int dx = (scaledPoint.x - previousDragPoint.x);
int dy = (scaledPoint.y - previousDragPoint.y); int dy = (scaledPoint.y - previousDragPoint.y);
Point actualD = moveComponents(this.controlPointMap, dx, Point actualD = moveComponents(this.controlPointMap, dx, dy,
dy, isSnapToGrid()); isSnapToGrid());
if (actualD == null) if (actualD == null)
return true; return true;
@@ -2021,6 +2024,60 @@ public class Presenter implements IPlugInPort {
return null; return null;
} }
@Override
public List<Template> getTemplatesForSelection() {
if (this.selectedComponents.isEmpty()) {
throw new RuntimeException("No components selected");
}
ComponentType selectedType = ComponentProcessor
.getInstance()
.extractComponentTypeFrom(
(Class<? extends IDIYComponent<?>>) this.selectedComponents
.get(0).getClass());
for (int i = 1; i < this.selectedComponents.size(); i++) {
ComponentType newType = ComponentProcessor
.getInstance()
.extractComponentTypeFrom(
(Class<? extends IDIYComponent<?>>) this.selectedComponents
.get(i).getClass());
if (newType.getInstanceClass() != selectedType.getInstanceClass()) {
throw new RuntimeException(
"Template can be applied on multiple components of the same type only");
}
}
return getTemplatesFor(selectedType.getCategory(), selectedType
.getName());
}
@Override
public void applyTemplateToSelection(Template template) {
LOG.debug(String.format("applyTemplateToSelection(%s)", template
.getName()));
Project oldProject = currentProject.clone();
for (IDIYComponent<?> component : this.selectedComponents) {
try {
drawingManager.invalidateComponent(component);
this.instantiationManager.loadComponentShapeFromTemplate(
component, template);
this.instantiationManager.fillWithDefaultProperties(component,
template);
} catch (Exception e) {
LOG.warn("Could not apply templates to " + component.getName(),
e);
}
}
// Notify the listeners.
if (!oldProject.equals(currentProject)) {
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED,
oldProject, currentProject.clone(), "Edit Selection");
projectFileManager.notifyFileChange();
}
messageDispatcher.dispatchMessage(EventType.REPAINT);
}
@Override @Override
public void deleteTemplate(String categoryName, String componentTypeName, public void deleteTemplate(String categoryName, String componentTypeName,
String templateName) { String templateName) {