Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.uberfire.ext.widgets.common.client.tables;
  
  import  com.google.gwt.cell.client.AbstractCell;
  import  com.google.gwt.cell.client.Cell;
  import  com.google.gwt.cell.client.Cell.Context;
  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.Style.Cursor;
 import  com.google.gwt.dom.client.Style.Position;
 import  com.google.gwt.dom.client.Style.Unit;
 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.AbstractCellTable;
 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;
 
 public class ResizableHeader<T> extends Header<String> {
 
     private Column<T, ?> column = null;
     private AbstractCellTable<T> cellTable;
     private String title = "";
     private static final int width = 20;
 
     public ResizableHeaderString title,
                             AbstractCellTable<T> cellTable,
                             Column<T, ?> column ) {
         supernew HeaderCell() );
         this. = title;
         this. = cellTable;
         this. = column;
     }
 
     @Override
     public String getValue() {
         return ;
     }
 
     @Override
     public void onBrowserEvent( Context context,
                                 Element target,
                                 NativeEvent event ) {
         String eventType = event.getType();
         if ( eventType.equals"mousemove" ) ) {
             new ColumnResizeHelper<T>( target );
         } else {
             return;
         }
     }
 
     private void setCursor( Element element,
                             Cursor cursor ) {
         element.getStyle().setCursor( cursor );
     }
 
     class ColumnResizeHelper<E> implements NativePreviewHandler {
 
         private HandlerRegistration handler;
         private AbstractCellTable<E> table;
         private Column<E, ?> col;
         private Element el;
         private boolean mousedown;
         private Element measuringElement;
 
         public ColumnResizeHelper( AbstractCellTable<E> table,
                                    Column<E, ?> col,
                                    Element el ) {
             this. = el;
             this. = table;
             this. = col;
              = Event.addNativePreviewHandler( this );
         }
 
         @SuppressWarnings("unchecked")
         @Override
         public void onPreviewNativeEvent( NativePreviewEvent event ) {
             NativeEvent nativeEvent = event.getNativeEvent();
             nativeEvent.preventDefault();
             nativeEvent.stopPropagation();
 
             String eventType = nativeEvent.getType();
             int clientX = nativeEvent.getClientX();
             if ( eventType.equals"mousemove" ) &&  ) {
                 int absoluteLeft = .getAbsoluteLeft();
                 int newWidth = clientX - absoluteLeft;
                 newWidth = newWidth <  ?  : newWidth;
                 .setColumnWidth( newWidth + "px" );
                 return;
             }
 
             if ( eventType.equals"mousemove" ) || eventType.equals"mousedown" ) ) {
                 Element eventTargetEl = nativeEvent.getEventTarget().cast();
                 int absoluteLeft = eventTargetEl.getAbsoluteLeft();
                 int offsetWidth = eventTargetEl.getOffsetWidth();
                 if ( clientX > absoluteLeft + offsetWidth -  ) {
                    if ( eventType.equals"mousedown" ) ) {
                         = true;
                    } else {
                        setCursor, Cursor.COL_RESIZE );
                    }
                } else {
                    removeHandler();
                    return;
                }
            } else if ( eventType.equals"mouseup" ) ) {
                 = false;
            } else if ( eventType.equals"mouseout" ) && ! ) {
                removeHandler();
                return;
            }
            if ( eventType.equals"dblclick" ) ) {
                // Get column
                nativeEvent.preventDefault();
                nativeEvent.stopPropagation();
                double max = 0;
                startMeasuring();
                for ( E t : .getVisibleItems() ) {
                    Object value = .getValue( t );
                    SafeHtmlBuilder sb = new SafeHtmlBuilder();
                    Cell<Objectcell = (Cell<Object>) .getCell();
                    cell.render( nullvaluesb );
                    max = Math.maxmeasureTextsb.toSafeHtml().asString() ), max );
                }
                finishMeasuring();
                .setColumnWidth( , ( max +  ) + "px" );
                removeHandler();
            }
        }
        private void removeHandler() {
            .removeHandler();
            setCursor, Cursor.DEFAULT );
        }
        private void startMeasuring() {
            Document document = Document.get();
             = document.createElement( "div" );
            .getStyle().setPosition( Position.ABSOLUTE );
            .getStyle().setLeft( -1000, Unit.PX );
            .getStyle().setTop( -1000, Unit.PX );
            document.getBody().appendChild(  );
        }
        private double measureTextString text ) {
            .setInnerHTML( text );
            return .getOffsetWidth();
        }
        private void finishMeasuring() {
            Document.get().getBody().removeChild(  );
        }
    }
    static class HeaderCell extends AbstractCell<String> {
        public HeaderCell() {
            super"click""mousedown""mousemove""dblclick" );
        }
        @Override
        public void render( Context context,
                            String value,
                            SafeHtmlBuilder sb ) {
            sb.append( SafeHtmlUtils.fromString( value ) );
        }
    }
New to GrepCode? Check out our FAQ X