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);

问题是当我移动鼠标快于它会跳过选择一些行。

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

谢谢

4

1 回答 1

0

您可以从 dragStart xy 中选择所有行,直到您当前的 x y。只有一件事,你需要行高来计算行。

于 2012-05-18T09:54:45.213 回答