mirror of
https://github.com/bancika/diy-layout-creator
synced 2026-11-10 09:12:09 +10:00
refactored selection code
This commit is contained in:
+8
@@ -23,6 +23,14 @@ public class ComponentSelection extends ArrayList<IDIYComponent<?>> implements T
|
|||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
public static final DataFlavor listFlavor = new DataFlavor(List.class, "Java List");
|
public static final DataFlavor listFlavor = new DataFlavor(List.class, "Java List");
|
||||||
|
|
||||||
|
public static final ComponentSelection EMPTY_SELECTION = new ComponentSelection();
|
||||||
|
|
||||||
|
public static ComponentSelection of(IDIYComponent<?> component) {
|
||||||
|
ComponentSelection selection = new ComponentSelection();
|
||||||
|
selection.add(component);
|
||||||
|
return selection;
|
||||||
|
}
|
||||||
|
|
||||||
public ComponentSelection() {
|
public ComponentSelection() {
|
||||||
super();
|
super();
|
||||||
|
|||||||
+35
-36
@@ -178,7 +178,7 @@ public class Presenter implements IPlugInPort {
|
|||||||
public void loadProject(Project project, boolean freshStart) {
|
public void loadProject(Project project, boolean freshStart) {
|
||||||
LOG.info(String.format("loadProject(%s, %s)", project.getTitle(), freshStart));
|
LOG.info(String.format("loadProject(%s, %s)", project.getTitle(), freshStart));
|
||||||
this.currentProject = project;
|
this.currentProject = project;
|
||||||
selectedComponents.clear();
|
updateSelection(ComponentSelection.EMPTY_SELECTION);
|
||||||
messageDispatcher.dispatchMessage(EventType.PROJECT_LOADED, project, freshStart);
|
messageDispatcher.dispatchMessage(EventType.PROJECT_LOADED, project, freshStart);
|
||||||
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
||||||
}
|
}
|
||||||
@@ -350,10 +350,7 @@ public class Presenter implements IPlugInPort {
|
|||||||
componentTypeSlot, scaledPoint, currentProject);
|
componentTypeSlot, scaledPoint, currentProject);
|
||||||
addComponent(component, componentTypeSlot);
|
addComponent(component, componentTypeSlot);
|
||||||
// Select the new component
|
// Select the new component
|
||||||
selectedComponents.clear();
|
updateSelection(ComponentSelection.of(component));
|
||||||
selectedComponents.add(component);
|
|
||||||
|
|
||||||
fireSelectionChanged();
|
|
||||||
// messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED,
|
// messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED,
|
||||||
// selectedComponents);
|
// selectedComponents);
|
||||||
// messageDispatcher.dispatchMessage(EventType.SELECTION_SIZE_CHANGED,
|
// messageDispatcher.dispatchMessage(EventType.SELECTION_SIZE_CHANGED,
|
||||||
@@ -388,9 +385,7 @@ public class Presenter implements IPlugInPort {
|
|||||||
componentSlot.setControlPoint(scaledPoint, 1);
|
componentSlot.setControlPoint(scaledPoint, 1);
|
||||||
addComponent(componentSlot, componentTypeSlot);
|
addComponent(componentSlot, componentTypeSlot);
|
||||||
// Select the new component
|
// Select the new component
|
||||||
selectedComponents.clear();
|
updateSelection(ComponentSelection.of(componentSlot));
|
||||||
selectedComponents.add(componentSlot);
|
|
||||||
fireSelectionChanged();
|
|
||||||
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
||||||
setNewComponentTypeSlot(null);
|
setNewComponentTypeSlot(null);
|
||||||
}
|
}
|
||||||
@@ -405,27 +400,28 @@ public class Presenter implements IPlugInPort {
|
|||||||
projectFileManager.notifyFileChange();
|
projectFileManager.notifyFileChange();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
ComponentSelection newSelection = new ComponentSelection(selectedComponents);
|
||||||
List<IDIYComponent<?>> components = findComponentsAt(scaledPoint);
|
List<IDIYComponent<?>> components = findComponentsAt(scaledPoint);
|
||||||
// If there's nothing under mouse cursor deselect all.
|
// If there's nothing under mouse cursor deselect all.
|
||||||
if (components.isEmpty()) {
|
if (components.isEmpty()) {
|
||||||
selectedComponents.clear();
|
newSelection.clear();
|
||||||
} else {
|
} else {
|
||||||
IDIYComponent<?> component = components.get(components.size() - 1);
|
IDIYComponent<?> component = components.get(components.size() - 1);
|
||||||
// If ctrl is pressed just toggle the component under mouse
|
// If ctrl is pressed just toggle the component under mouse
|
||||||
// cursor.
|
// cursor.
|
||||||
if (ctrlDown) {
|
if (ctrlDown) {
|
||||||
if (selectedComponents.contains(component)) {
|
if (newSelection.contains(component)) {
|
||||||
selectedComponents.removeAll(findAllGroupedComponents(component));
|
newSelection.removeAll(findAllGroupedComponents(component));
|
||||||
} else {
|
} else {
|
||||||
selectedComponents.addAll(findAllGroupedComponents(component));
|
newSelection.addAll(findAllGroupedComponents(component));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Otherwise just select that one component.
|
// Otherwise just select that one component.
|
||||||
selectedComponents.clear();
|
newSelection.clear();
|
||||||
selectedComponents.addAll(findAllGroupedComponents(component));
|
newSelection.addAll(findAllGroupedComponents(component));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fireSelectionChanged();
|
updateSelection(newSelection);
|
||||||
// messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED,
|
// messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED,
|
||||||
// selectedComponents);
|
// selectedComponents);
|
||||||
// messageDispatcher.dispatchMessage(EventType.SELECTION_SIZE_CHANGED,
|
// messageDispatcher.dispatchMessage(EventType.SELECTION_SIZE_CHANGED,
|
||||||
@@ -506,8 +502,7 @@ public class Presenter implements IPlugInPort {
|
|||||||
@Override
|
@Override
|
||||||
public void selectAll() {
|
public void selectAll() {
|
||||||
LOG.info("selectAll()");
|
LOG.info("selectAll()");
|
||||||
this.selectedComponents = new ComponentSelection(currentProject.getComponents());
|
updateSelection(new ComponentSelection(currentProject.getComponents()));
|
||||||
fireSelectionChanged();
|
|
||||||
// messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED,
|
// messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED,
|
||||||
// selectedComponents);
|
// selectedComponents);
|
||||||
// messageDispatcher.dispatchMessage(EventType.SELECTION_SIZE_CHANGED,
|
// messageDispatcher.dispatchMessage(EventType.SELECTION_SIZE_CHANGED,
|
||||||
@@ -530,9 +525,7 @@ public class Presenter implements IPlugInPort {
|
|||||||
List<IDIYComponent<?>> components = findComponentsAt(scaledPoint);
|
List<IDIYComponent<?>> components = findComponentsAt(scaledPoint);
|
||||||
if (!controlPointMap.isEmpty()) {
|
if (!controlPointMap.isEmpty()) {
|
||||||
// If we're dragging control points reset selection.
|
// If we're dragging control points reset selection.
|
||||||
selectedComponents.clear();
|
updateSelection(new ComponentSelection(controlPointMap.keySet()));
|
||||||
selectedComponents.addAll(controlPointMap.keySet());
|
|
||||||
fireSelectionChanged();
|
|
||||||
// messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED,
|
// messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED,
|
||||||
// selectedComponents);
|
// selectedComponents);
|
||||||
// messageDispatcher.dispatchMessage(EventType.SELECTION_SIZE_CHANGED,
|
// messageDispatcher.dispatchMessage(EventType.SELECTION_SIZE_CHANGED,
|
||||||
@@ -540,8 +533,7 @@ public class Presenter implements IPlugInPort {
|
|||||||
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
||||||
} else if (components.isEmpty()) {
|
} else if (components.isEmpty()) {
|
||||||
// If there are no components are under the cursor, reset selection.
|
// If there are no components are under the cursor, reset selection.
|
||||||
selectedComponents.clear();
|
updateSelection(ComponentSelection.EMPTY_SELECTION);
|
||||||
fireSelectionChanged();
|
|
||||||
// messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED,
|
// messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED,
|
||||||
// selectedComponents);
|
// selectedComponents);
|
||||||
// messageDispatcher.dispatchMessage(EventType.SELECTION_SIZE_CHANGED,
|
// messageDispatcher.dispatchMessage(EventType.SELECTION_SIZE_CHANGED,
|
||||||
@@ -553,9 +545,7 @@ public class Presenter implements IPlugInPort {
|
|||||||
// If the component under the cursor is not already selected, make
|
// If the component under the cursor is not already selected, make
|
||||||
// it into the only selected component.
|
// it into the only selected component.
|
||||||
if (!selectedComponents.contains(component)) {
|
if (!selectedComponents.contains(component)) {
|
||||||
selectedComponents.clear();
|
updateSelection(new ComponentSelection(findAllGroupedComponents(component)));
|
||||||
selectedComponents.addAll(findAllGroupedComponents(component));
|
|
||||||
fireSelectionChanged();
|
|
||||||
// messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED,
|
// messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED,
|
||||||
// selectedComponents);
|
// selectedComponents);
|
||||||
// messageDispatcher.dispatchMessage(EventType.SELECTION_SIZE_CHANGED,
|
// messageDispatcher.dispatchMessage(EventType.SELECTION_SIZE_CHANGED,
|
||||||
@@ -733,14 +723,15 @@ public class Presenter implements IPlugInPort {
|
|||||||
if (scaledPoint != null) {
|
if (scaledPoint != null) {
|
||||||
this.selectionRect = Utils.createRectangle(scaledPoint, previousDragPoint);
|
this.selectionRect = Utils.createRectangle(scaledPoint, previousDragPoint);
|
||||||
}
|
}
|
||||||
selectedComponents.clear();
|
ComponentSelection newSelection = new ComponentSelection();
|
||||||
for (IDIYComponent<?> component : currentProject.getComponents()) {
|
for (IDIYComponent<?> component : currentProject.getComponents()) {
|
||||||
Area area = drawingManager.getComponentArea(component);
|
Area area = drawingManager.getComponentArea(component);
|
||||||
if ((area != null) && (selectionRect != null) && area.intersects(selectionRect)) {
|
if ((area != null) && (selectionRect != null) && area.intersects(selectionRect)) {
|
||||||
selectedComponents.addAll(findAllGroupedComponents(component));
|
newSelection.addAll(findAllGroupedComponents(component));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
selectionRect = null;
|
selectionRect = null;
|
||||||
|
updateSelection(newSelection);
|
||||||
// messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED,
|
// messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED,
|
||||||
// selectedComponents);
|
// selectedComponents);
|
||||||
// messageDispatcher.dispatchMessage(EventType.SELECTION_SIZE_CHANGED,
|
// messageDispatcher.dispatchMessage(EventType.SELECTION_SIZE_CHANGED,
|
||||||
@@ -753,8 +744,8 @@ public class Presenter implements IPlugInPort {
|
|||||||
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, preDragProject, cloner
|
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, preDragProject, cloner
|
||||||
.deepClone(currentProject), "Drag");
|
.deepClone(currentProject), "Drag");
|
||||||
projectFileManager.notifyFileChange();
|
projectFileManager.notifyFileChange();
|
||||||
|
fireSelectionSizeChanged();
|
||||||
}
|
}
|
||||||
fireSelectionChanged();
|
|
||||||
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
||||||
dragInProgress = false;
|
dragInProgress = false;
|
||||||
}
|
}
|
||||||
@@ -772,12 +763,10 @@ public class Presenter implements IPlugInPort {
|
|||||||
}
|
}
|
||||||
addComponent(component, componentTypeMap.get(component.getClass().getName()));
|
addComponent(component, componentTypeMap.get(component.getClass().getName()));
|
||||||
}
|
}
|
||||||
selectedComponents.clear();
|
|
||||||
selectedComponents.addAll(components);
|
|
||||||
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, oldProject, cloner
|
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, oldProject, cloner
|
||||||
.deepClone(currentProject), "Add");
|
.deepClone(currentProject), "Add");
|
||||||
projectFileManager.notifyFileChange();
|
projectFileManager.notifyFileChange();
|
||||||
fireSelectionChanged();
|
updateSelection(new ComponentSelection(components));
|
||||||
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -796,11 +785,10 @@ public class Presenter implements IPlugInPort {
|
|||||||
drawingManager.invalidateComponent(component);
|
drawingManager.invalidateComponent(component);
|
||||||
}
|
}
|
||||||
currentProject.getComponents().removeAll(selectedComponents);
|
currentProject.getComponents().removeAll(selectedComponents);
|
||||||
this.selectedComponents.clear();
|
|
||||||
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, oldProject, cloner
|
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, oldProject, cloner
|
||||||
.deepClone(currentProject), "Delete");
|
.deepClone(currentProject), "Delete");
|
||||||
projectFileManager.notifyFileChange();
|
projectFileManager.notifyFileChange();
|
||||||
fireSelectionChanged();
|
updateSelection(ComponentSelection.EMPTY_SELECTION);
|
||||||
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -861,7 +849,15 @@ public class Presenter implements IPlugInPort {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void fireSelectionChanged() {
|
/**
|
||||||
|
* Updates the selection with the specified {@link ComponentSelection}.
|
||||||
|
* Also, updates control point map with all components that are stuck to the
|
||||||
|
* newly selected components.
|
||||||
|
*
|
||||||
|
* @param newSelection
|
||||||
|
*/
|
||||||
|
private void updateSelection(ComponentSelection newSelection) {
|
||||||
|
this.selectedComponents = newSelection;
|
||||||
Map<IDIYComponent<?>, Set<Integer>> controlPointMap = new HashMap<IDIYComponent<?>, Set<Integer>>();
|
Map<IDIYComponent<?>, Set<Integer>> controlPointMap = new HashMap<IDIYComponent<?>, Set<Integer>>();
|
||||||
for (IDIYComponent<?> component : selectedComponents) {
|
for (IDIYComponent<?> component : selectedComponents) {
|
||||||
Set<Integer> indices = new HashSet<Integer>();
|
Set<Integer> indices = new HashSet<Integer>();
|
||||||
@@ -873,6 +869,10 @@ public class Presenter implements IPlugInPort {
|
|||||||
includeStuckComponents(controlPointMap);
|
includeStuckComponents(controlPointMap);
|
||||||
messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED, selectedComponents,
|
messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED, selectedComponents,
|
||||||
controlPointMap.keySet());
|
controlPointMap.keySet());
|
||||||
|
fireSelectionSizeChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void fireSelectionSizeChanged() {
|
||||||
messageDispatcher.dispatchMessage(EventType.SELECTION_SIZE_CHANGED,
|
messageDispatcher.dispatchMessage(EventType.SELECTION_SIZE_CHANGED,
|
||||||
calculateSelectionDimension());
|
calculateSelectionDimension());
|
||||||
}
|
}
|
||||||
@@ -1049,8 +1049,7 @@ public class Presenter implements IPlugInPort {
|
|||||||
: componentType.getName()));
|
: componentType.getName()));
|
||||||
try {
|
try {
|
||||||
instantiationManager.setComponentTypeSlot(componentType, currentProject);
|
instantiationManager.setComponentTypeSlot(componentType, currentProject);
|
||||||
selectedComponents.clear();
|
updateSelection(ComponentSelection.EMPTY_SELECTION);
|
||||||
fireSelectionChanged();
|
|
||||||
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
||||||
// messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED,
|
// messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED,
|
||||||
// selectedComponents);
|
// selectedComponents);
|
||||||
|
|||||||
Reference in New Issue
Block a user