2012-05-18 224 views
1

我想選擇上拖動鼠標在網格行了,我有如下一段代碼:鼠標移動事件

我的鼠標事件監聽器是DragMultiSelectListener.java

import java.util.List; 

import com.myexample.client.ui.items.AbstractContentItem; 
import com.extjs.gxt.ui.client.event.BaseEvent; 
import com.extjs.gxt.ui.client.event.Events; 
import com.extjs.gxt.ui.client.event.GridEvent; 
import com.extjs.gxt.ui.client.event.Listener; 

public class DragMultiSelectListener implements Listener<BaseEvent> { 

    Presenter presenter = null; 

    /* multiple select on drag */ 
    private boolean mousePressed = false; 
    private boolean dragEnable = false; 
    private boolean selected = false; 
    private List<AbstractContentItem> currentSelected = null; 
    private int startIndex = -1; 
    private int endIndex = -1; 
    private int startX=0; 
    private int startY=0; 
    private int endX=0; 
    private int endY=0; 

    /** mouseMovedStarted used for preventing calling Grid onClick event for IE while using multiple select on DND **/ 
    private boolean mouseMovedStarted = false; 

    public DragMultiSelectListener(Presenter pPresenter) { 
     this.presenter = pPresenter; 
    } 

    @Override 
    public void handleEvent(BaseEvent be) { 
     if(be.getType() == Events.OnMouseDown){ 
      GridEvent ge = (GridEvent) be; 

      /* Multiple Row Select */ 
      mouseMovedStarted = false; 
      mousePressed = true; 
      startIndex = ge.getRowIndex(); 

      /* Deselect All */ 
      if(ge.getTarget().getClassName().equals("x-grid3-scroller")){ 
       if(ge.getTarget().getClientWidth() < ge.getTarget().getScrollWidth()){ 
        if(ge.getTarget().getClientHeight() < (ge.getClientY() - ge.getTarget().getAbsoluteTop())){ 
         mousePressed = false; 
        } 
        if(ge.getTarget().getClientWidth() < (ge.getClientX() - ge.getTarget().getAbsoluteLeft())){ 
         mousePressed = false; 
        } 
       } 
       presenter.deselectAllContentItem(); 
      } 

      /* Select Record */ 
      if(currentSelected != null && (!presenter.getSelectedContent().isEmpty())){ 
       selected = false; 
       for(AbstractContentItem item : currentSelected){ 
        AbstractContentItem clickedItem = (AbstractContentItem) ge.getGrid().getStore().getAt(ge.getRowIndex()); 
        if(clickedItem != null && item.getDTO().getId() == clickedItem.getDTO().getId()){ 
         selected = true; 
        } 
       } 
      }else{ 
       currentSelected = presenter.getSelectedContent(); 
       selected = false; 
      } 

      dragEnable = false; 

      if(ge.getTarget().getNodeName().equalsIgnoreCase("SPAM")){ 
       dragEnable = true; 
      } 

      if(selected){ 
       dragEnable = true; 
      } 

      /* draw box */ 
      if(mousePressed){ 
       startX = ge.getClientX(); 
       startY = ge.getClientY(); 
      } 

     }else if(be.getType() == Events.OnMouseMove){ 
      GridEvent ge = (GridEvent) be; 

      if(mousePressed && !dragEnable && (ge.getRowIndex() != -1)){ 

       if(startIndex == -1){ 
        startIndex = ge.getRowIndex(); 
       } 

       endIndex = ge.getRowIndex(); 
       presenter.deselectAllContentItem(); 
       if((startIndex - endIndex) >= 0){ 
        for(int i = endIndex; i<=startIndex; i++){ 
         presenter.selectContentItem(i, true); 
        } 
       }else{ 
        for(int i = startIndex; i<=endIndex; i++){ 
         presenter.selectContentItem(i, true); 
        } 
       } 

       /* Show selection box */ 
       endX = ge.getClientX(); 
       endY = ge.getClientY(); 



      }else if(mousePressed && !dragEnable){ 

       /* Show selection box */ 
       endX = ge.getClientX(); 
       endY = ge.getClientY(); 

       if(ge.getTarget().getClassName().equals("x-grid3-scroller")){ 
        if(startY > endY){ 
         presenter.deselectAllContentItem(); 
        } 
       } 

       displaySelectionBox(ge.getGrid().getAbsoluteLeft(), ge.getGrid().getAbsoluteTop()); 
      } 
      mouseMovedStarted = true; 

     }else if(be.getType() == Events.OnMouseUp){ 
      GridEvent ge = (GridEvent) be; 
      mousePressed = false; 
      currentSelected = presenter.getSelectedContent(); 

     } 
    } 

    public boolean isMouseMovedStarted() { 
     return mouseMovedStarted; 
    } 

    public boolean isDragEnable() { 
     return dragEnable; 
    } 

    public void setEndX(int endX) { 
     this.endX = endX; 
    } 

    public void setEndY(int endY) { 
     this.endY = endY; 
    } 

    public boolean isSelected() { 
     return selected; 
    } 

    public void setSelected(boolean selected) { 
     this.selected = selected; 
    } 

    public List<AbstractContentItem> getCurrentSelected() { 
     return currentSelected; 
    } 

    public void setCurrentSelected(List<AbstractContentItem> currentSelected) { 
     this.currentSelected = currentSelected; 
    } 

    public void setMousePressed(boolean mousePressed) { 
     this.mousePressed = mousePressed; 
    } 

} 

註冊電網事件由以下代碼

 dragMultiSelectListener = new DragMultiSelectListener(presenter); 
    grid.addListener(Events.OnMouseDown, dragMultiSelectListener); 
    grid.addListener(Events.OnMouseMove, dragMultiSelectListener); 
    grid.addListener(Events.OnMouseUp, dragMultiSelectListener); 

問題是,當我移動鼠標比它會跳過選擇一些行快。

我不明白,如何提高執行速度以防止跳過行。

感謝

回答

0

您可以選擇的dragstart x和y的所有行,直到你的當前x和y。只有一件事,你需要行高來計算行數。

+0

我該如何從x,y獲得Rowindex?y –

+1

@Alexey Ogarkov如果將來CSS的CSS會發生變化,有什麼好處?此解決方案可能無法在當時正常工作。 –