1 31 32 package org.antlr.xjlib.appkit.gview.event; 33 34 import org.antlr.xjlib.appkit.gview.GView; 35 import org.antlr.xjlib.appkit.gview.base.Vector2D; 36 import org.antlr.xjlib.appkit.gview.object.GElement; 37 38 import java.awt.*; 39 import java.awt.event.InputEvent ; 40 import java.awt.event.MouseEvent ; 41 42 public class GEventDragElement extends GAbstractEvent { 43 44 public GElement dragElement = null; 45 46 protected boolean dragging = false; 47 protected Point p1 = null; 48 protected Point p2 = null; 49 50 protected Vector2D dragElementOffset = null; 51 52 public GEventDragElement(GView view) { 53 super(view); 54 } 55 56 public void mousePressed(MouseEvent e, Point mousePosition) { 57 if(hasExclusiveValue(GEventManager.EXCLUSIVE_DRAG_VALUE)) 58 return; 59 60 if(hasExclusiveValue(GEventManager.EXCLUSIVE_CREATE_LINK_VALUE)) 61 return; 62 63 if(e.getClickCount() != 1) 64 return; 65 66 if((e.getModifiersEx() & InputEvent.SHIFT_DOWN_MASK) == InputEvent.SHIFT_DOWN_MASK) 67 return; 68 69 dragElement = delegate.eventQueryElementAtPoint(mousePosition); 70 if(dragElement != null && delegate.eventIsSelectedElement(dragElement)) { 71 addExclusiveValue(GEventManager.EXCLUSIVE_DRAG_VALUE); 72 dragging = true; 73 p1 = mousePosition; 74 } else if(dragElement != null && dragElement.isDraggable()) { 75 addExclusiveValue(GEventManager.EXCLUSIVE_DRAG_VALUE); 76 dragElement.beginDrag(); 77 dragElementOffset = Vector2D.vector(mousePosition).sub(dragElement.getPosition()); 78 return; 79 } 80 81 dragElement = null; 82 } 83 84 public void mouseReleased(MouseEvent e, Point mousePosition) { 85 removeExclusiveValue(GEventManager.EXCLUSIVE_DRAG_VALUE); 86 87 view.hideAllMagnetics(); 88 89 dragElement = null; 90 dragging = false; 91 } 92 93 public void mouseDragged(MouseEvent e, Point mousePosition) { 94 if(dragElement != null) { 95 Vector2D mouse = Vector2D.vector(mousePosition); 96 Vector2D position = mouse.sub(dragElementOffset); 97 98 view.showAndAjustPositionToMagnetics(position); 99 dragElement.moveToPosition(position); 100 101 delegate.eventChangeDone(); 102 delegate.eventShouldRepaint(); 103 } else if(dragging) { 104 p2 = mousePosition; 105 delegate.eventMoveSelectedElements(p2.x-p1.x, p2.y-p1.y); 106 p1 = p2; 107 delegate.eventChangeDone(); 108 delegate.eventShouldRepaint(); 109 } 110 } 111 } 112 | Popular Tags |