}
// Where are we now?
// We use dragPos instead of e.getPoint() so that the gem drops where the drag ghost shows it.
Point where = dragPos;
ExtendedUndoableEditSupport undoableEditSupport = tableTop.getUndoableEditSupport();
// Now do whatever based on the drag mode
if (dragMode == TableTopDragMode.CTRLDRAGGING) {
// Ctrl Drag is used as a clone function
// We want to group this stuff as one single action
undoableEditSupport.beginUpdate();
undoableEditSupport.setEditName(GemCutter.getResourceString("UndoText_CopyDrag"));
DisplayedGemSelection displayedGemSelection = new DisplayedGemSelection(dragList, gemCutter);
Rectangle rect = dragList[0].getBounds();
for (int i = 1; i < dragList.length; i++) {
rect.add(dragList[i].getBounds());
}
int x = where.x - (pressedAt.x - rect.x);
int y = where.y - (pressedAt.y - rect.y);
tableTop.doPasteUserAction(displayedGemSelection, new Point(x, y));
undoableEditSupport.endUpdate();
} else if (dragMode == TableTopDragMode.GEMDRAGGING) {
// Increment the update level for the edit undo. This will aggregate the gem translations.
undoableEditSupport.beginUpdate();
if (dragList.length > 0) {
undoableEditSupport.setEditName(dragList.length > 1 ? GemCutter.getResourceString("UndoText_MoveGems") :
GemCutter.getResourceString("UndoText_MoveGem"));
}
// Do the move for each selected gem
for (final DisplayedGem displayedGem : dragList) {
Point newGemLocation = displayedGem.getLocation();
newGemLocation.translate(where.x - pressedAt.x, where.y - pressedAt.y);
// Perform the translation
tableTop.doChangeGemLocationUserAction(displayedGem, newGemLocation);
}
// Decrement the update level. This will post the edit if the level is zero.
undoableEditSupport.endUpdate();
} else if (dragMode == TableTopDragMode.CONNECTING || dragMode == TableTopDragMode.DISCONNECTING) {
// see if we can connect anything
DisplayedPart partUnder = tableTop.getGemPartUnder(where);
boolean connected = false;
if (partUnder != null) {
Connection newConnection = null;
if (connectionDragAnchorPart instanceof DisplayedPartOutput && partUnder instanceof DisplayedPartInput) {
newConnection = tableTop.handleConnectGemPartsGesture(connectionDragAnchorPart.getPartConnectable(),
((DisplayedPartInput)partUnder).getPartInput());
} else if (partUnder instanceof DisplayedPartConnectable && connectionDragAnchorPart instanceof DisplayedPartInput) {
newConnection = tableTop.handleConnectGemPartsGesture(((DisplayedPartConnectable)partUnder).getPartConnectable(),
((DisplayedPartInput)connectionDragAnchorPart).getPartInput());
}
connected = (newConnection != null);
}
// Undo any autoburns if we didn't connect anything
if (!connected && connectionDragAnchorPart instanceof DisplayedPartOutput) {
DisplayedGem burnGem = connectionDragAnchorPart.getDisplayedGem();
tableTop.getBurnManager().doUnburnAutomaticallyBurnedInputsUserAction(burnGem.getGem());
}
if (!connected && dragMode == TableTopDragMode.CONNECTING) {
// Don't post the edit if connecting and nothing happened.
undoableEditSupport.endUpdateNoPost();
} else if (connected && dragMode == TableTopDragMode.DISCONNECTING && disconnectedDisplayedPart == partUnder){
// Also don't post the edit if all we did was reconnect a part that we disconnected.
undoableEditSupport.endUpdateNoPost();
} else {
if (dragMode == TableTopDragMode.CONNECTING) {
undoableEditSupport.setEditName(GemCutter.getResourceString("UndoText_ConnectGems"));
} else {
undoableEditSupport.setEditName(GemCutter.getResourceString("UndoText_DisconnectGems"));
}
// Decrement the update level, possibly triggering the edit to be posted.
undoableEditSupport.endUpdate();
}
} else if (dragMode == TableTopDragMode.SELECTING && pressedAt != null) {
// calculate the bounds of the select area