1 31 32 package org.antlr.xjlib.appkit.gview.event; 33 34 import org.antlr.xjlib.appkit.gview.GView; 35 36 import java.awt.*; 37 import java.awt.event.InputEvent ; 38 import java.awt.event.MouseEvent ; 39 40 public class GEventDragSelection extends GAbstractEvent { 41 42 private boolean selecting = false; 43 private Point p1 = null; 44 private Point p2 = null; 45 46 public GEventDragSelection(GView view) { 47 super(view); 48 } 49 50 public void mousePressed(MouseEvent e, Point mousePosition) { 51 if(hasExclusiveValue(GEventManager.EXCLUSIVE_DRAG_VALUE)) 52 return; 53 54 if(e.getClickCount() != 1) 55 return; 56 57 int mask = InputEvent.BUTTON1_DOWN_MASK; 58 if((e.getModifiersEx() & mask) == mask && delegate.eventQueryElementAtPoint(mousePosition) == null) { 59 addExclusiveValue(GEventManager.EXCLUSIVE_DRAG_VALUE); 60 selecting = true; 61 delegate.eventSouldSelectAllElements(false); 62 p1 = mousePosition; 63 } 64 } 65 66 public void mouseReleased(MouseEvent e, Point mousePosition) { 67 removeExclusiveValue(GEventManager.EXCLUSIVE_DRAG_VALUE); 68 delegate.eventShouldRepaint(); 69 selecting = false; 70 p1 = null; 71 p2 = null; 72 } 73 74 public void mouseDragged(MouseEvent e, Point mousePosition) { 75 if(selecting == false) 76 return; 77 78 p2 = mousePosition; 79 delegate.eventSelectElementsInRect(p1.x, p1.y, p2.x-p1.x, p2.y-p1.y); 80 delegate.eventShouldRepaint(); 81 } 82 83 public void draw(Graphics g) { 84 if(selecting && p1 != null && p2 != null) { 85 Graphics2D g2d = (Graphics2D)g; 86 87 int x = Math.min(p1.x, p2.x); 88 int y = Math.min(p1.y, p2.y); 89 int dx = Math.abs(p2.x-p1.x); 90 int dy = Math.abs(p2.y-p1.y); 91 92 g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f )); 93 g.setColor(Color.gray); 94 g.fillRect(x, y, dx, dy); 95 g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f )); 96 g.setColor(Color.black); 97 g.drawRect(x, y, dx, dy); 98 } 99 } 100 } 101 | Popular Tags |