Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.uberfire.ext.widgets.common.client.tables;
  
  import  com.github.gwtbootstrap.client.ui.DataGrid;
  import  com.google.gwt.cell.client.AbstractCell;
  import  com.google.gwt.cell.client.Cell.Context;
  import  com.google.gwt.dom.client.DivElement;
  import  com.google.gwt.dom.client.Document;
  import  com.google.gwt.dom.client.Element;
  import  com.google.gwt.dom.client.NativeEvent;
 import  com.google.gwt.dom.client.SpanElement;
 import  com.google.gwt.dom.client.Style;
 import  com.google.gwt.dom.client.Style.Cursor;
 import  com.google.gwt.dom.client.Style.Position;
 import  com.google.gwt.event.shared.HandlerRegistration;
 import  com.google.gwt.safehtml.shared.SafeHtmlBuilder;
 import  com.google.gwt.safehtml.shared.SafeHtmlUtils;
 import  com.google.gwt.user.cellview.client.Column;
 import  com.google.gwt.user.cellview.client.Header;
 import  com.google.gwt.user.client.Event;
 import  com.google.gwt.user.client.Event.NativePreviewEvent;
 import  com.google.gwt.user.client.Event.NativePreviewHandler;
 import  org.uberfire.commons.validation.PortablePreconditions;
 
 import static com.google.gwt.dom.client.Style.Unit.*;
 import java.util.List;

A column header that supports resizing and moving See https://github.com/gchatelet/GwtResizableDraggableColumns/blob/master/src/fr/mikrosimage/gwt/client/ResizableHeader.java

Parameters:
<T>
 
 public abstract class ResizableMovableHeader<T> extends Header<String> {
 
     private static final Style.Cursor MOVE_CURSOR = Cursor.MOVE;
     private static final String MOVE_COLOR = "gray";
     private static final int MOVE_HANDLE_WIDTH = 32;
 
     private static final Style.Cursor RESIZE_CURSOR = Cursor.COL_RESIZE;
     private static final String RESIZE_COLOR = "gray";
     private static final int RESIZE_HANDLE_WIDTH = 8;
 
     private static final double GHOST_OPACITY = .3;
 
     private static final int MINIMUM_COLUMN_WIDTH = 30;
 
     private final Document document = Document.get();
 
     private final String title;
     private final DataGrid<T> table;
     private final ColumnPicker columnPicker;
     private final Column<T, ?> column;
 
     private final Element tableElement;
     private HeaderHelper current;
     
 
     public ResizableMovableHeaderfinal String title,
                                    final DataGrid<T> table
                                    final ColumnPicker columnPicker,
                                    final Column<T, ?> column ) {
         supernew HeaderCell() );      
         this. = PortablePreconditions.checkNotNull( "title",
                                                          title );
         this. = PortablePreconditions.checkNotNull( "table",
                                                          table );
         this. = PortablePreconditions.checkNotNull( "columnPicker",
                                                                 columnPicker );
         this. = PortablePreconditions.checkNotNull( "column",
                                                           column );
         this. = table.getElement();
     }
 
     @Override
     public String getValue() {
         return ;
     }
 
     @Override
     public void onBrowserEventfinal Context context,
                                 final Element target,
                                 final NativeEvent event ) {
         if (  == null ) {
              = new HeaderHelpertarget,
                                         event );
         }
     }
 
     interface IDragCallback {
 
         void dragFinished();
     }
 
     private static NativeEvent getEventAndPreventPropagationfinal NativePreviewEvent event ) {
         final NativeEvent nativeEvent = event.getNativeEvent();
         nativeEvent.preventDefault();
         nativeEvent.stopPropagation();
         return nativeEvent;
    }
    private static void setLinefinal Style style,
                                 final int width,
                                 final int top,
                                 final int height,
                                 final String color ) {
        style.setPosition( Position.ABSOLUTE );
        style.setTop( top,
                      PX );
        style.setHeight( height,
                         PX );
        style.setWidth( width,
                        PX );
        style.setBackgroundColor( color );
        style.setZIndex( . );
    }
    private class HeaderHelper implements NativePreviewHandler,
                                          IDragCallback {
        private final HandlerRegistration handler = Event.addNativePreviewHandler( this );
        private final Element source;
        private final Element handles;
        private final Element moveHandle;
        private final Element resizeHandle;
        private boolean dragging;
        public HeaderHelperfinal Element target,
                             final NativeEvent event ) {
            event.preventDefault();
            event.stopPropagation();
            this. = target;
            this. = .createDivElement();
            final int leftBound = target.getOffsetLeft() + target.getOffsetWidth();
            this. = createSpanElement( MOVE_CURSOR,
                                                 leftBound -  - ,
                                                  );
            this. = createSpanElement( RESIZE_CURSOR,
                                                   leftBound - ,
                                                    );
            .appendChild(  );
            .appendChild(  );
            .appendChild(  );
        }
        private SpanElement createSpanElementfinal Cursor cursor,
                                               final double left,
                                               final double width ) {
            final SpanElement span = .createSpanElement();
            span.setAttribute( "title",
                                );
            final Style style = span.getStyle();
            style.setCursor( cursor );
            style.setPosition( Position.ABSOLUTE );
            style.setBottom( 0,
                             PX );
            style.setHeight( .getOffsetHeight(),
                             PX );
            style.setTop( .getOffsetTop(),
                          PX );
            style.setWidth( width,
                            PX );
            style.setLeft( left,
                           PX );
            return span;
        }
        @Override
        public void onPreviewNativeEventfinal NativePreviewEvent event ) {
            final NativeEvent natEvent = event.getNativeEvent();
            final Element element = natEvent.getEventTarget().cast();
            final String eventType = natEvent.getType();
            if ( !( element ==  || element ==  ) ) {
                if ( "mousedown".equalseventType ) ) {
                    //No need to do anything, the event will be passed on to the column sort handler
                } else if ( ! && "mouseover".equalseventType ) ) {
                    cleanUp();
                }
                return;
            }
            final NativeEvent nativeEvent = getEventAndPreventPropagationevent );
            if ( "mousedown".equalseventType ) ) {
                if ( element ==  ) {
                    .removeFromParent();
                    new ColumnResizeHelperthis,
                                            ,
                                            nativeEvent );
                } else {
                    new ColumnMoverHelperthis,
                                           ,
                                           nativeEvent );
                }
                 = true;
            }
        }
        private void cleanUp() {
            .removeHandler();
            .removeFromParent();
             = null;
        }
        public void dragFinished() {
             = false;
            cleanUp();
        }
    }
    private class ColumnResizeHelper implements NativePreviewHandler {
        private final HandlerRegistration handler = Event.addNativePreviewHandler( this );
        private final DivElement resizeLine = .createDivElement();
        private final Style resizeLineStyle = .getStyle();
        private final Element header;
        private final IDragCallback dragCallback;
        private ColumnResizeHelperfinal IDragCallback dragCallback,
                                    final Element header,
                                    final NativeEvent event ) {
            this. = dragCallback;
            this. = header;
            setLine,
                     2,
                     0,
                     getTableBodyHeight(),
                      );
            moveLineevent.getClientX() );
            .appendChild(  );
        }
        @Override
        public void onPreviewNativeEventfinal NativePreviewEvent event ) {
            final NativeEvent nativeEvent = getEventAndPreventPropagationevent );
            final int clientX = nativeEvent.getClientX();
            final String eventType = nativeEvent.getType();
            if ( "mousemove".equalseventType ) ) {
                moveLineclientX );
            } else if ( "mouseup".equalseventType ) ) {
                .removeHandler();
                .removeFromParent();
                .dragFinished();
                columnResized( Math.maxclientX - .getAbsoluteLeft(),
                                          ) );
            }
        }
        private void moveLinefinal int clientX ) {
            final int xPos = clientX - .getAbsoluteLeft();
            .setLeft( xPos,
                                     PX );
        }
    }
    private class ColumnMoverHelper implements NativePreviewHandler {
        private static final int ghostLineWidth = 4;
        private final HandlerRegistration handler = Event.addNativePreviewHandler( this );
        private final DivElement ghostLine = .createDivElement();
        private final Style ghostLineStyle = .getStyle();
        private final DivElement ghostColumn = .createDivElement();
        private final Style ghostColumnStyle = .getStyle();
        private final int columnWidth;
        private final int[] columnXPositions;
        private final IDragCallback dragCallback;
        private int fromIndex = -1;
        private int toIndex;
        private ColumnMoverHelperfinal IDragCallback dragCallback,
                                   final Element target,
                                   final NativeEvent event ) {
            final int clientX = event.getClientX();
            final Element tr = target.getParentElement();
            final int columns = tr.getChildCount();
            this. = dragCallback;
            this. = target.getOffsetWidth();
            this. = new intcolumns + 1 ];
            this.[ 0 ] = tr.getAbsoluteLeft();
            for ( int i = 0; i < columns; ++i ) {
                final int xPos = i ] + ( (Element) tr.getChild( i ) ).getOffsetWidth();
                if ( xPos > clientX &&  == -1 ) {
                     = i;
                }
                i + 1 ] = xPos;
            }
             = ;
            final int bodyHeight = getTableBodyHeight();
            setLine,
                     ,
                     0,
                     bodyHeight,
                      );
            setLine,
                     ,
                     0,
                     bodyHeight,
                      );
            .setOpacity(  );
            moveColumnclientX );
            .appendChild(  );
            .appendChild(  );
        }
        @Override
        public void onPreviewNativeEventfinal NativePreviewEvent event ) {
            final NativeEvent nativeEvent = getEventAndPreventPropagationevent );
            final String eventType = nativeEvent.getType();
            if ( "mousemove".equalseventType ) ) {
                moveColumnnativeEvent.getClientX() );
            } else if ( "mouseup".equalseventType ) ) {
                .removeHandler();
                .removeFromParent();
                .removeFromParent();
                if (  !=  ) {
                    columnMoved,
                                  );
                }
                .dragFinished();
            }
        }
        private void moveColumnfinal int clientX ) {
            final int pointer = clientX -  / 2;
            .setLeft( pointer - .getAbsoluteLeft(),
                                      PX );
            for ( int i = 0; i < . - 1; ++i ) {
                if ( clientX < i + 1 ] ) {
                    final int adjustedIndex = i >  ? i + 1 : i;
                    int lineXPos = adjustedIndex ] - .getAbsoluteLeft();
                    if ( adjustedIndex == . - 1 ) {
                        lineXPos -= ;
                    } else if ( adjustedIndex > 0 ) {
                        lineXPos -=  / 2;
                    }
                    .setLeft( lineXPos,
                                            PX );
                     = i;
                    break;
                }
            }
        }
    }
    private static class HeaderCell extends AbstractCell<String> {
        public HeaderCell() {
            super"mousemove" );
        }
        @Override
        public void renderfinal Context context,
                            final String value,
                            final SafeHtmlBuilder sb ) {
            sb.append( SafeHtmlUtils.fromString( value ) );
        }
    }
    protected void columnResizedfinal int newWidth ) {
        .setColumnWidth( ,
                              newWidth + "px" );
        for(ColumnChangedHandler handler : ){
          handler.afterColumnChanged();
        }
    }
    protected void columnMovedfinal int fromIndex,
                                final int beforeIndex ) {
        .columnMovedfromIndex,
                                  beforeIndex );
        .removeColumn( fromIndex );
        .insertColumn( beforeIndex,
                            ,
                            this );
        for(ColumnChangedHandler handler : ){
          handler.afterColumnChanged();
        }
    }
    protected abstract int getTableBodyHeight();
    
    public void addColumnChangedHandler(ColumnChangedHandler handler){
      if(handler != null){
        .add(handler);
      }
    }
  
};

New to GrepCode? Check out our FAQ X