This commit is contained in:
bancika
2012-02-08 04:48:08 +00:00
parent 3d3a6e27d2
commit 19cfca3df8
2 changed files with 44 additions and 22 deletions
@@ -4,6 +4,7 @@ import java.awt.Cursor;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Point; import java.awt.Point;
import java.awt.dnd.DnDConstants;
import java.awt.geom.Point2D; import java.awt.geom.Point2D;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@@ -45,6 +46,9 @@ public interface IPlugInPort {
public static final String THEME_KEY = "theme"; public static final String THEME_KEY = "theme";
public static final String RECENT_COMPONENTS_KEY = "recentComponents"; public static final String RECENT_COMPONENTS_KEY = "recentComponents";
public static final int DND_TOGGLE_STICKY = 0x1;
public static final int DND_TOGGLE_SNAP = 0x40000000;
/** /**
* Returns size of the canvas that takes project dimensions into account as * Returns size of the canvas that takes project dimensions into account as
* well as zoom level. Each dimension is calculated as the product of the * well as zoom level. Each dimension is calculated as the product of the
@@ -212,11 +216,9 @@ public interface IPlugInPort {
* Note: point coordinates are scaled for zoom factor. * Note: point coordinates are scaled for zoom factor.
* *
* @param point * @param point
* @param ctrlDown * @param dragAction
* @param shiftDown
* @param altDown
*/ */
void dragStarted(Point point, boolean ctrlDown, boolean shiftDown, boolean altDown); void dragStarted(Point point, int dragAction);
/** /**
* Checks if it's possible to drop over the specified point. * Checks if it's possible to drop over the specified point.
@@ -228,6 +230,13 @@ public interface IPlugInPort {
*/ */
boolean dragOver(Point point); boolean dragOver(Point point);
/**
* Changes the current drag action during the dragging.
*
* @param dragAction
*/
void dragActionChanged(int dragAction);
/** /**
* Notification that drag has been ended in the specified point. * Notification that drag has been ended in the specified point.
* *
@@ -5,6 +5,7 @@ import java.awt.Dimension;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Point; import java.awt.Point;
import java.awt.Rectangle; import java.awt.Rectangle;
import java.awt.dnd.DnDConstants;
import java.awt.geom.Area; import java.awt.geom.Area;
import java.awt.geom.Point2D; import java.awt.geom.Point2D;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
@@ -93,6 +94,7 @@ public class Presenter implements IPlugInPort {
// Previous mouse location, not scaled for zoom factor. // Previous mouse location, not scaled for zoom factor.
private Point previousDragPoint = null; private Point previousDragPoint = null;
private Project preDragProject = null; private Project preDragProject = null;
private int dragAction;
public Presenter(IView view) { public Presenter(IView view) {
super(); super();
@@ -366,8 +368,7 @@ public class Presenter implements IPlugInPort {
switch (componentTypeSlot.getCreationMethod()) { switch (componentTypeSlot.getCreationMethod()) {
case SINGLE_CLICK: case SINGLE_CLICK:
try { try {
if (ConfigurationManager.getInstance().readBoolean( if (isSnapToGrid()) {
IPlugInPort.SNAP_TO_GRID_KEY, true)) {
CalcUtils.snapPointToGrid(scaledPoint, currentProject.getGridSpacing()); CalcUtils.snapPointToGrid(scaledPoint, currentProject.getGridSpacing());
} }
IDIYComponent<?> component = instantiationManager.instantiateComponent( IDIYComponent<?> component = instantiationManager.instantiateComponent(
@@ -401,8 +402,7 @@ public class Presenter implements IPlugInPort {
case POINT_BY_POINT: case POINT_BY_POINT:
// First click is just to set the controlPointSlot and // First click is just to set the controlPointSlot and
// componentSlot. // componentSlot.
if (ConfigurationManager.getInstance().readBoolean( if (isSnapToGrid()) {
IPlugInPort.SNAP_TO_GRID_KEY, true)) {
CalcUtils.snapPointToGrid(scaledPoint, currentProject.getGridSpacing()); CalcUtils.snapPointToGrid(scaledPoint, currentProject.getGridSpacing());
} }
if (instantiationManager.getComponentSlot() == null) { if (instantiationManager.getComponentSlot() == null) {
@@ -517,7 +517,7 @@ public class Presenter implements IPlugInPort {
Map<IDIYComponent<?>, Set<Integer>> components = new HashMap<IDIYComponent<?>, Set<Integer>>(); Map<IDIYComponent<?>, Set<Integer>> components = new HashMap<IDIYComponent<?>, Set<Integer>>();
Point scaledPoint = scalePoint(point); Point scaledPoint = scalePoint(point);
if (instantiationManager.getComponentTypeSlot() != null) { if (instantiationManager.getComponentTypeSlot() != null) {
if (ConfigurationManager.getInstance().readBoolean(IPlugInPort.SNAP_TO_GRID_KEY, true)) { if (isSnapToGrid()) {
CalcUtils.snapPointToGrid(scaledPoint, currentProject.getGridSpacing()); CalcUtils.snapPointToGrid(scaledPoint, currentProject.getGridSpacing());
} }
boolean refresh = false; boolean refresh = false;
@@ -526,8 +526,7 @@ public class Presenter implements IPlugInPort {
refresh = instantiationManager.updatePointByPoint(scaledPoint); refresh = instantiationManager.updatePointByPoint(scaledPoint);
break; break;
case SINGLE_CLICK: case SINGLE_CLICK:
refresh = instantiationManager.updateSingleClick(scaledPoint, ConfigurationManager refresh = instantiationManager.updateSingleClick(scaledPoint, isSnapToGrid(),
.getInstance().readBoolean(IPlugInPort.SNAP_TO_GRID_KEY, true),
currentProject.getGridSpacing()); currentProject.getGridSpacing());
break; break;
} }
@@ -601,21 +600,22 @@ public class Presenter implements IPlugInPort {
} }
@Override @Override
public void dragStarted(Point point, boolean ctrlDown, boolean shiftDown, boolean altDown) { public void dragStarted(Point point, int dragAction) {
LOG.debug(String.format("dragStarted(%s)", point)); LOG.debug(String.format("dragStarted(%s, %s)", point, dragAction));
if (instantiationManager.getComponentTypeSlot() != null) { if (instantiationManager.getComponentTypeSlot() != null) {
LOG.debug("Cannot start drag because a new component is being created."); LOG.debug("Cannot start drag because a new component is being created.");
mouseClicked(point, ctrlDown, shiftDown, altDown, 1); mouseClicked(point, dragAction == DnDConstants.ACTION_COPY, dragAction == DnDConstants.ACTION_LINK, dragAction == DnDConstants.ACTION_MOVE, 1);
return; return;
} }
dragInProgress = true; this.dragInProgress = true;
this.dragAction = dragAction;
preDragProject = cloner.deepClone(currentProject); preDragProject = cloner.deepClone(currentProject);
Point scaledPoint = scalePoint(point); Point scaledPoint = scalePoint(point);
previousDragPoint = scaledPoint; this.previousDragPoint = scaledPoint;
List<IDIYComponent<?>> components = findComponentsAt(scaledPoint); List<IDIYComponent<?>> components = findComponentsAt(scaledPoint);
if (!controlPointMap.isEmpty()) { if (!this.controlPointMap.isEmpty()) {
// If we're dragging control points reset selection. // If we're dragging control points reset selection.
updateSelection(new ArrayList<IDIYComponent<?>>(controlPointMap.keySet())); updateSelection(new ArrayList<IDIYComponent<?>>(this.controlPointMap.keySet()));
// messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED, // messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED,
// selectedComponents); // selectedComponents);
// messageDispatcher.dispatchMessage(EventType.SELECTION_SIZE_CHANGED, // messageDispatcher.dispatchMessage(EventType.SELECTION_SIZE_CHANGED,
@@ -651,13 +651,13 @@ public class Presenter implements IPlugInPort {
for (int i = 0; i < c.getControlPointCount(); i++) { for (int i = 0; i < c.getControlPointCount(); i++) {
pointIndices.add(i); pointIndices.add(i);
} }
controlPointMap.put(c, pointIndices); this.controlPointMap.put(c, pointIndices);
} }
} }
// Expand control points to include all stuck components. // Expand control points to include all stuck components.
boolean sticky = ConfigurationManager.getInstance().readBoolean( boolean sticky = ConfigurationManager.getInstance().readBoolean(
IPlugInPort.STICKY_POINTS_KEY, true); IPlugInPort.STICKY_POINTS_KEY, true);
if (ctrlDown) { if (this.dragAction == IPlugInPort.DND_TOGGLE_STICKY) {
sticky = !sticky; sticky = !sticky;
} }
if (sticky) { if (sticky) {
@@ -666,6 +666,12 @@ public class Presenter implements IPlugInPort {
} }
} }
@Override
public void dragActionChanged(int dragAction) {
LOG.debug("dragActionChanged(" + dragAction + ")");
this.dragAction = dragAction;
}
/** /**
* Finds any components that are stuck to one of the components already in * Finds any components that are stuck to one of the components already in
* the map. * the map.
@@ -741,6 +747,14 @@ public class Presenter implements IPlugInPort {
} }
} }
private boolean isSnapToGrid() {
boolean snapToGrid = ConfigurationManager.getInstance().readBoolean(
IPlugInPort.SNAP_TO_GRID_KEY, true);
if (this.dragAction == IPlugInPort.DND_TOGGLE_SNAP)
snapToGrid = !snapToGrid;
return snapToGrid;
}
@Override @Override
public boolean dragOver(Point point) { public boolean dragOver(Point point) {
if (point == null) { if (point == null) {
@@ -773,8 +787,7 @@ public class Presenter implements IPlugInPort {
isFirst = false; isFirst = false;
Point testPoint = new Point(controlPoints[index]); Point testPoint = new Point(controlPoints[index]);
testPoint.translate(dx, dy); testPoint.translate(dx, dy);
if (ConfigurationManager.getInstance().readBoolean( if (isSnapToGrid()) {
IPlugInPort.SNAP_TO_GRID_KEY, true)) {
CalcUtils.snapPointToGrid(testPoint, currentProject CalcUtils.snapPointToGrid(testPoint, currentProject
.getGridSpacing()); .getGridSpacing());
} }