1 19 20 package org.netbeans.modules.form; 21 22 import java.awt.*; 23 import java.awt.event.*; 24 import java.awt.geom.*; 25 import javax.swing.*; 26 import javax.swing.undo.*; 27 import java.util.*; 28 import java.util.List ; 29 30 import org.netbeans.modules.form.layoutsupport.*; 31 32 36 37 class ComponentDragger 38 { 39 private FormDesigner formDesigner; 40 private HandleLayer handleLayer; 41 private RADVisualComponent[] selectedComponents; 42 private Rectangle[] originalBounds; private Point hotspot; private Point mousePosition; 45 private int resizeType; 46 47 private RADVisualContainer targetMetaContainer; 48 49 private Container targetContainer; 50 private Container targetContainerDel; 51 52 static Stroke dashedStroke1 = new BasicStroke((float) 2.0, 53 BasicStroke.CAP_SQUARE, 54 BasicStroke.JOIN_MITER, 55 (float) 10.0, 56 new float[] { (float) 1.0, (float) 4.0 }, 57 0); 58 59 static Stroke dashedStroke2 = new BasicStroke( 60 (float) 2.0, 61 BasicStroke.CAP_SQUARE, 62 BasicStroke.JOIN_MITER, 63 (float) 10.0, 64 new float[] { (float) 2.0, (float) 8.0 }, 65 0); 66 67 68 ComponentDragger(FormDesigner formDesigner, 70 HandleLayer handleLayer, 71 RADVisualComponent[] selectedComponents, 72 Rectangle[] originalBounds, 73 Point hotspot, 74 RADVisualContainer fixedTargetMetaContainer) 75 { 76 this.formDesigner = formDesigner; 77 this.handleLayer = handleLayer; 78 this.selectedComponents = selectedComponents; 79 this.originalBounds = originalBounds; 80 this.hotspot = hotspot; 81 this.mousePosition = hotspot; 82 this.resizeType = 0; 83 84 if (fixedTargetMetaContainer != null) { 85 targetMetaContainer = fixedTargetMetaContainer; 86 } 87 } 88 89 ComponentDragger(FormDesigner formDesigner, 91 HandleLayer handleLayer, 92 RADVisualComponent[] selectedComponents, 93 Rectangle[] originalBounds, 94 Point hotspot, 95 int resizeType) 96 { 97 this.formDesigner = formDesigner; 98 this.handleLayer = handleLayer; 99 this.selectedComponents = selectedComponents; 100 this.originalBounds = originalBounds; 101 this.hotspot = hotspot; 102 this.mousePosition = hotspot; 103 this.resizeType = resizeType; 104 } 105 106 void drag(Point p, RADVisualContainer target) { 107 targetMetaContainer = target; 108 mousePosition = p; 109 } 110 111 void paintDragFeedback(Graphics2D g) { 112 Stroke oldStroke = g.getStroke(); 113 g.setStroke(dashedStroke1); 114 115 Color oldColor = g.getColor(); 116 g.setColor(FormLoaderSettings.getInstance().getSelectionBorderColor()); 117 118 List constraints = new ArrayList(selectedComponents.length); 119 List indices = new ArrayList(selectedComponents.length); 120 121 boolean constraintsOK = computeConstraints(mousePosition, 122 constraints, indices); 123 124 Point contPos = null; 125 LayoutSupportManager layoutSupport = null; 126 if (constraintsOK) { 127 contPos = SwingUtilities.convertPoint(targetContainerDel, 0, 0, handleLayer); 128 layoutSupport = targetMetaContainer.getLayoutSupport(); 129 if (resizeType == 0) 130 paintTargetContainerFeedback(g, targetContainerDel); 131 } 132 133 for (int i = 0; i < selectedComponents.length; i++) { 134 RADVisualComponent metacomp = selectedComponents[i]; 135 boolean drawn = false; 136 137 if (constraintsOK) { 138 Component comp = (Component) formDesigner.getComponent(metacomp); 139 LayoutConstraints constr = (LayoutConstraints) constraints.get(i); 140 int index = ((Integer )indices.get(i)).intValue(); 141 142 if (constr != null || index >= 0) { 143 g.translate(contPos.x, contPos.y); 144 drawn = layoutSupport.paintDragFeedback( 145 targetContainer, targetContainerDel, 146 comp, constr, index, g); 147 g.translate(- contPos.x, - contPos.y); 148 } 149 } 151 152 if (!drawn) 153 paintDragFeedback(g, metacomp); 154 } 155 156 g.setColor(oldColor); 157 g.setStroke(oldStroke); 158 } 159 160 void dropComponents(Point point, RADVisualContainer target) { 161 List constraints = null; List indices = null; 164 targetMetaContainer = target; 165 if (targetMetaContainer != null) { 166 constraints = new ArrayList(selectedComponents.length); 167 indices = new ArrayList(selectedComponents.length); 168 computeConstraints(point, constraints, indices); 169 } 170 if (targetMetaContainer == null) { 171 constraints = indices = null; 175 } 176 177 FormModel formModel = formDesigner.getFormModel(); 178 179 LayoutSupportManager layoutSupport = null; 181 RADVisualComponent[] originalComponents = null; 183 LayoutConstraints[] originalConstraints = null; 184 List movedFromOutside = null; 186 List movedWithinTarget = null; 188 189 if (targetMetaContainer != null) { layoutSupport = targetMetaContainer.getLayoutSupport(); 191 originalComponents = targetMetaContainer.getSubComponents(); 192 originalConstraints = 193 new LayoutConstraints[originalComponents.length]; 194 195 adjustIndices(indices); 198 199 for (int i=0; i < selectedComponents.length; i++) { 201 if (selectedComponents[i].getParentContainer() 202 != targetMetaContainer) 203 { 204 if (movedFromOutside == null) 205 movedFromOutside = new ArrayList(selectedComponents.length); 206 movedFromOutside.add(new Integer (i)); } 208 else { 209 if (movedWithinTarget == null) 210 movedWithinTarget = new ArrayList(selectedComponents.length); 211 movedWithinTarget.add(selectedComponents[i]); 212 } 213 } 214 215 if (movedFromOutside != null && movedFromOutside.size() > 0) { 218 int count = movedFromOutside.size(); 219 RADVisualComponent[] newComps = new RADVisualComponent[count]; 220 LayoutConstraints[] newConstr = new LayoutConstraints[count]; 221 222 for (int i=0; i < count; i++) { 223 int index = ((Integer )movedFromOutside.get(i)).intValue(); 224 newComps[i] = selectedComponents[index]; 225 newConstr[i] = (LayoutConstraints) constraints.get(index); 226 } 227 228 int j = ((Integer )movedFromOutside.get(0)).intValue(); 230 int jj = ((Integer )indices.get(j)).intValue(); 232 233 try { 234 layoutSupport.acceptNewComponents(newComps, newConstr, jj); 235 } 236 catch (RuntimeException ex) { 237 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex); 239 return; 240 } 241 242 for (int i=0; i < count; i++) { 244 int index = ((Integer )movedFromOutside.get(i)).intValue(); 245 constraints.set(index, newConstr[i]); 246 } 247 248 movedFromOutside.clear(); } 250 } 251 252 int n = selectedComponents.length 254 + (originalComponents != null ? originalComponents.length : 0); 255 List newComponents = new ArrayList(n); 256 List newConstraints = new ArrayList(n); 257 int newI = 0; 258 259 for (int i=0; i < n; i++) { 261 newComponents.add(null); 262 newConstraints.add(null); 263 } 264 265 if (targetMetaContainer != null) { 266 for (int i=0; i < selectedComponents.length; i++) { 268 int index = ((Integer )indices.get(i)).intValue(); 269 if (index >= 0 && index < n && checkTarget(selectedComponents[i])) { 270 while (newComponents.get(index) != null) if (++index == n) 272 index = 0; 273 274 newComponents.set(index, selectedComponents[i]); 275 } 276 } 277 278 for (int i=0; i < originalComponents.length; i++) { 280 RADVisualComponent metacomp = originalComponents[i]; 281 originalConstraints[i] = layoutSupport.getConstraints(metacomp); 282 283 int index = newComponents.indexOf(metacomp); 284 if (index < 0) { while (newComponents.get(newI) != null) newI++; 287 288 newComponents.set(newI, metacomp); 289 newConstraints.set(newI, originalConstraints[i]); 290 } 291 } 292 } 293 294 for (int i=0; i < selectedComponents.length; i++) { 296 RADVisualComponent metacomp = selectedComponents[i]; 297 int index = newComponents.indexOf(metacomp); 298 if (index >= 0) { newConstraints.set(index, constraints != null ? 300 constraints.get(i) : null); 301 } 302 else if (checkTarget(metacomp)) { while (newComponents.get(newI) != null) newI++; 305 306 newComponents.set(newI, metacomp); 307 newConstraints.set(newI, constraints != null ? 308 constraints.get(i) : null); 309 } 310 } 311 313 for (int i=0; i < n; i++) { 315 RADVisualComponent metacomp = (RADVisualComponent) newComponents.get(i); 316 if (metacomp != null) { 317 RADVisualContainer parentCont = metacomp.getParentContainer(); 318 if (parentCont != targetMetaContainer) { 319 if (movedFromOutside == null) 320 movedFromOutside = new ArrayList(selectedComponents.length); 321 movedFromOutside.add(metacomp); 322 323 formModel.removeComponent(metacomp, false); 324 } 325 } 326 else { newComponents.remove(i); 328 newConstraints.remove(i); 329 i--; 330 n--; 331 } 332 } 333 334 if (n == 0) 335 return; 337 boolean undoRedoOn = formModel.isUndoRedoRecording(); 340 if (undoRedoOn) 341 formModel.setUndoRedoRecording(false); 342 343 RADVisualComponent[] newCompsArray = new RADVisualComponent[n]; 345 LayoutConstraints[] newConstrArray = new LayoutConstraints[n]; 346 347 if (targetMetaContainer != null) { for (int i=0; i < n; i++) { 349 newCompsArray[i] = (RADVisualComponent) newComponents.get(i); 350 newConstrArray[i] = (LayoutConstraints) newConstraints.get(i); 351 } 352 353 layoutSupport.removeAll(); 355 356 targetMetaContainer.initSubComponents(newCompsArray); 358 layoutSupport.addComponents(newCompsArray, newConstrArray, 0); 359 } 360 else { ComponentContainer othersMetaCont = formModel.getModelContainer(); 362 for (int i=0; i < n; i++) { 363 newCompsArray[i] = (RADVisualComponent) newComponents.get(i); 364 othersMetaCont.add(newCompsArray[i]); 365 } 366 } 367 368 RADVisualComponent[] compsMovedFromOutside; 370 if (movedFromOutside != null) { 371 n = movedFromOutside.size(); 372 compsMovedFromOutside = new RADVisualComponent[n]; 373 for (int i=0; i < n; i++) { 374 compsMovedFromOutside[i] = (RADVisualComponent) 375 movedFromOutside.get(i); 376 formModel.fireComponentAdded(compsMovedFromOutside[i], false); 377 } 378 } 379 else { 380 compsMovedFromOutside = new RADVisualComponent[0]; 381 formModel.fireComponentsReordered(targetMetaContainer, new int[0]); 382 } 383 384 RADVisualComponent[] compsMovedWithinTarget; 386 if (movedWithinTarget != null) { 387 n = movedWithinTarget.size(); 388 compsMovedWithinTarget = new RADVisualComponent[n]; 389 for (int i=0; i < n; i++) { 390 compsMovedWithinTarget[i] = (RADVisualComponent) 391 movedWithinTarget.get(i); 392 formModel.fireComponentLayoutChanged(compsMovedWithinTarget[i], 393 null, null, null); 394 } 395 } 396 else compsMovedWithinTarget = new RADVisualComponent[0]; 397 398 if (undoRedoOn) { 400 DropUndoableEdit dropUndo = new DropUndoableEdit(); 401 dropUndo.formModel = formModel; 402 dropUndo.targetContainer = targetMetaContainer; 403 dropUndo.targetComponentsBeforeMove = originalComponents; 404 dropUndo.targetConstraintsBeforeMove = originalConstraints; 405 dropUndo.targetComponentsAfterMove = newCompsArray; 406 dropUndo.targetConstraintsAfterMove = newConstrArray; 407 dropUndo.componentsMovedFromOutside = compsMovedFromOutside; 408 dropUndo.componentsMovedWithinTarget = compsMovedWithinTarget; 409 410 formModel.addUndoableEdit(dropUndo); 411 } 412 413 if (undoRedoOn) formModel.setUndoRedoRecording(true); 416 417 SwingUtilities.invokeLater(new Runnable () { 420 public void run() { 421 formDesigner.setSelectedComponents(selectedComponents); 422 } 423 }); 424 } 425 426 428 private boolean computeConstraints(Point p, List constraints, List indices) { 429 if (selectedComponents == null || selectedComponents.length == 0) 430 return false; 431 432 if (targetMetaContainer == null) 433 return false; 435 RADVisualContainer fixTargetContainer = null; 436 do { 437 if (fixTargetContainer != null) { 438 targetMetaContainer = fixTargetContainer; 439 fixTargetContainer = null; 440 } 441 442 LayoutSupportManager layoutSupport = targetMetaContainer.getLayoutSupport(); 443 446 targetContainer = (Container) formDesigner.getComponent(targetMetaContainer); 447 if (targetContainer == null) 448 return false; 450 targetContainerDel = targetMetaContainer.getContainerDelegate(targetContainer); 451 if (targetContainerDel == null) 452 return false; 454 Point posInCont = SwingUtilities.convertPoint(handleLayer, p, targetContainerDel); 455 456 for (int i = 0; i < selectedComponents.length; i++) { 457 LayoutConstraints constr = null; 458 int index = -1; 459 460 RADVisualComponent metacomp = selectedComponents[i]; 461 Component comp = (Component) formDesigner.getComponent(metacomp); 462 463 if (comp != null) { 464 if (!checkTarget(metacomp)) { 465 fixTargetContainer = metacomp.getParentContainer(); 466 constraints.clear(); 467 indices.clear(); 468 if (fixTargetContainer == null) 469 return false; break; 471 } 472 473 if (resizeType == 0) { Point posInComp = new Point(hotspot.x - originalBounds[i].x, 475 hotspot.y - originalBounds[i].y); 476 index = layoutSupport.getNewIndex( 477 targetContainer, targetContainerDel, 478 comp, metacomp.getComponentIndex(), 479 posInCont, posInComp); 480 constr = layoutSupport.getNewConstraints( 481 targetContainer, targetContainerDel, 482 comp, metacomp.getComponentIndex(), 483 posInCont, posInComp); 484 485 if (constr == null && index >= 0) { 486 LayoutSupportManager.storeConstraints(metacomp); 488 constr = layoutSupport.getStoredConstraints(metacomp); 489 } 490 } 491 else { int up = 0, down = 0, left = 0, right = 0; 493 494 if ((resizeType & LayoutSupportManager.RESIZE_DOWN) != 0) 495 down = p.y - hotspot.y; 496 else if ((resizeType & LayoutSupportManager.RESIZE_UP) != 0) 497 up = hotspot.y - p.y; 498 if ((resizeType & LayoutSupportManager.RESIZE_RIGHT) != 0) 499 right = p.x - hotspot.x; 500 else if ((resizeType & LayoutSupportManager.RESIZE_LEFT) != 0) 501 left = hotspot.x - p.x; 502 503 Insets sizeChanges = new Insets(up, left, down, right); 504 constr = layoutSupport.getResizedConstraints( 505 targetContainer, targetContainerDel, 506 comp, metacomp.getComponentIndex(), 507 handleLayer.convertRectangleToComponent(new Rectangle(originalBounds[i]), targetContainerDel), 508 sizeChanges, posInCont); 509 } 510 } 511 512 constraints.add(constr); 513 indices.add(new Integer (index)); 514 } 515 } 516 while (fixTargetContainer != null); 517 518 return true; 519 } 520 521 525 private boolean checkTarget(RADVisualComponent metacomp) { 526 if (!(metacomp instanceof RADVisualContainer)) 527 return true; 528 529 RADComponent targetCont = targetMetaContainer; 530 while (targetCont != null) { 531 if (targetCont == metacomp) 532 return false; 533 targetCont = targetCont.getParentComponent(); 534 } 535 536 return true; 537 } 538 539 542 private void adjustIndices(List indices) { 543 int index; 544 int correction; 545 int prevIndex = -1; 546 int prevCorrection = 0; 547 548 for (int i=0; i < indices.size(); i++) { 549 index = ((Integer )indices.get(i)).intValue(); 550 if (index >= 0) { 551 if (index == prevIndex) { 552 correction = prevCorrection; 553 } 554 else { 555 correction = 0; 556 RADVisualComponent[] targetComps = 557 targetMetaContainer.getSubComponents(); 558 for (int j=0; j < index; j++) { 559 RADVisualComponent tComp = targetComps[j]; 560 boolean isSelected = false; 561 for (int k=0; k < selectedComponents.length; k++) 562 if (tComp == selectedComponents[k]) { 563 isSelected = true; 564 break; 565 } 566 567 if (isSelected) 568 correction++; 569 } 570 prevIndex = index; 571 prevCorrection = correction; 572 } 573 574 if (correction != 0) { 575 index -= correction; 576 indices.set(i, new Integer (index)); 577 } 578 } 579 } 580 } 581 582 private void paintDragFeedback(Graphics2D g, RADVisualComponent metacomp) { 583 Object comp = formDesigner.getComponent(metacomp); 584 if (!(comp instanceof Component) || !((Component)comp).isShowing()) 585 return; 586 587 Component component = (Component) comp; 588 589 Rectangle rect = component.getBounds(); 590 rect = SwingUtilities.convertRectangle(component.getParent(), 591 rect, 592 handleLayer); 593 594 rect.translate(mousePosition.x - hotspot.x, 595 mousePosition.y - hotspot.y); 596 597 g.draw(new Rectangle2D.Double(rect.x, rect.y, rect.width, rect.height)); 598 599 if (metacomp instanceof RADVisualContainer) { 600 RADVisualComponent[] children = 601 ((RADVisualContainer)metacomp).getSubComponents(); 602 for (int i = 0; i < children.length; i++) { 603 paintDragFeedback(g, children[i]); 604 } 605 } 606 } 607 608 private void paintTargetContainerFeedback(Graphics2D g, Container cont) { 609 Stroke oldStroke = g.getStroke(); 610 g.setStroke(dashedStroke2); 611 612 Color oldColor = g.getColor(); 613 g.setColor(FormLoaderSettings.getInstance().getDragBorderColor()); 614 615 Rectangle rect = new Rectangle(new Point(0,0), cont.getSize()); 616 rect = SwingUtilities.convertRectangle(cont, 617 rect, 618 handleLayer); 619 g.draw(new Rectangle2D.Double(rect.x, rect.y, rect.width, rect.height)); 620 g.setColor(oldColor); 621 g.setStroke(oldStroke); 622 } 623 624 626 private static class DropUndoableEdit extends AbstractUndoableEdit { 627 628 FormModel formModel; 629 RADVisualContainer targetContainer; 630 RADVisualComponent[] targetComponentsBeforeMove; 631 LayoutConstraints[] targetConstraintsBeforeMove; 632 RADVisualComponent[] targetComponentsAfterMove; 633 LayoutConstraints[] targetConstraintsAfterMove; 634 RADVisualComponent[] componentsMovedFromOutside; 635 RADVisualComponent[] componentsMovedWithinTarget; 636 637 public void undo() throws CannotUndoException { 638 super.undo(); 639 640 boolean undoRedoOn = formModel.isUndoRedoRecording(); 642 if (undoRedoOn) 643 formModel.setUndoRedoRecording(false); 644 645 for (int i=0; i < componentsMovedFromOutside.length; i++) 646 formModel.removeComponentImpl( 647 componentsMovedFromOutside[i], false); 648 649 if (targetContainer != null) { 650 LayoutSupportManager layoutSupport = 651 targetContainer.getLayoutSupport(); 652 layoutSupport.removeAll(); 653 targetContainer.initSubComponents(targetComponentsBeforeMove); 654 layoutSupport.addComponents(targetComponentsBeforeMove, 655 targetConstraintsBeforeMove, 656 0); 657 658 for (int i=0; i < componentsMovedWithinTarget.length; i++) 659 formModel.fireComponentLayoutChanged( 660 componentsMovedWithinTarget[i], null, null, null); 661 } 662 663 if (undoRedoOn) formModel.setUndoRedoRecording(true); 665 } 666 667 public void redo() throws CannotRedoException { 668 super.redo(); 669 670 boolean undoRedoOn = formModel.isUndoRedoRecording(); 672 if (undoRedoOn) 673 formModel.setUndoRedoRecording(false); 674 675 if (targetContainer != null) { 676 LayoutSupportManager layoutSupport = 677 targetContainer.getLayoutSupport(); 678 layoutSupport.removeAll(); 679 targetContainer.initSubComponents(targetComponentsAfterMove); 680 layoutSupport.addComponents(targetComponentsAfterMove, 681 targetConstraintsAfterMove, 682 0); 683 } 684 else { 685 ComponentContainer othersMetaCont = formModel.getModelContainer(); 686 for (int i=0; i < targetComponentsAfterMove.length; i++) 687 othersMetaCont.add(targetComponentsAfterMove[i]); 688 } 689 690 for (int i=0; i < componentsMovedFromOutside.length; i++) 691 formModel.fireComponentAdded(componentsMovedFromOutside[i], 692 false); 693 694 for (int i=0; i < componentsMovedWithinTarget.length; i++) 695 formModel.fireComponentLayoutChanged( 696 componentsMovedWithinTarget[i], null, null, null); 697 698 if (undoRedoOn) formModel.setUndoRedoRecording(true); 700 } 701 702 public String getUndoPresentationName() { 703 return ""; } 705 public String getRedoPresentationName() { 706 return ""; } 708 } 709 } 710 | Popular Tags |