litesoft
@ 948
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
/* * Copyright 2008 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package com.google.gwt.gen2.table.client; import com.google.gwt.core.client.*; import com.google.gwt.event.dom.client.*; import com.google.gwt.user.client.*; import com.google.gwt.user.client.ui.*; import com.google.gwt.user.client.ui.FlexTable; import com.google.gwt.user.client.ui.FlexTable.*; /** * An abstract representation of an editor used to edit the contents of a cell. * <p/> * <h3>CSS Style Rules</h3> * <dl> * <dt>.gwt-InlineCellEditor</dt> * <dd>applied to the entire widget</dd> * <dt>.gwt-InlineCellEditor .accept</dt> * <dd>applied to the accept image</dd> * <dt>.gwt-InlineCellEditor .cancel</dt> * <dd>applied to the cancel image</dd> * </dl> * * @param <ColType> the data type of the column */ public abstract class InlineCellEditor<ColType> extends PopupPanel implements CellEditor<ColType> { /** * An {@link ImageBundle} that provides images for {@link InlineCellEditor}. */ public static interface InlineCellEditorImages extends ImageBundle { /** * An image used to fill the available width. * * @return a prototype of this image */ AbstractImagePrototype cellEditorAccept(); /** * An image indicating that a column is sorted in ascending order. * * @return a prototype of this image */ AbstractImagePrototype cellEditorCancel(); } /** * <code>ClickDecoratorPanel</code> decorates any widget with the minimal * amount of machinery to receive clicks for delegation to the parent. */ private static final class ClickDecoratorPanel extends SimplePanel { public ClickDecoratorPanel( Widget child, ClickHandler delegate ) { setWidget( child ); addDomHandler( delegate, ClickEvent.getType() ); } } /** * Default style name. */ public static final String DEFAULT_STYLENAME = "gwt-InlineCellEditor"; /** * The click listener used to accept. */ private ClickHandler cancelHandler = new ClickHandler() { public void onClick( ClickEvent event ) { cancel(); } }; /** * The click listener used to accept. */ private ClickHandler acceptHandler = new ClickHandler() { public void onClick( ClickEvent event ) { accept(); } }; /** * The current {@link CellEditor.Callback}. */ private Callback<ColType> curCallback = null; /** * The current {@link CellEditor.CellEditInfo}. */ private CellEditInfo curCellEditInfo = null; /** * The main grid used for layout. */ private FlexTable layoutTable; /** * Construct a new {@link InlineCellEditor}. * * @param content the {@link Widget} used to edit */ protected InlineCellEditor( Widget content ) { this( content, GWT.<InlineCellEditorImages>create( InlineCellEditorImages.class ) ); } /** * Construct a new {@link InlineCellEditor} with the specified images. * * @param content the {@link Widget} used to edit * @param images the images to use for the accept/cancel buttons */ protected InlineCellEditor( Widget content, InlineCellEditorImages images ) { super( true, true ); setStylePrimaryName( DEFAULT_STYLENAME ); // Wrap contents in a table layoutTable = new FlexTable(); FlexCellFormatter formatter = layoutTable.getFlexCellFormatter(); layoutTable.setCellSpacing( 0 ); setWidget( layoutTable ); // Add a label setLabel( "" ); formatter.setColSpan( 0, 0, 3 ); // Add content widget layoutTable.setWidget( 1, 0, content ); // Add accept and cancel buttons setAcceptWidget( images.cellEditorAccept().createImage() ); setCancelWidget( images.cellEditorCancel().createImage() ); } public void editCell( CellEditInfo cellEditInfo, ColType cellValue, Callback<ColType> callback ) { // Save the current values curCallback = callback; curCellEditInfo = cellEditInfo; // Get the info about the cell HTMLTable table = curCellEditInfo.getTable(); int row = curCellEditInfo.getRowIndex(); int cell = curCellEditInfo.getCellIndex(); // Get the location of the cell Element cellElem = table.getCellFormatter().getElement( row, cell ); int top = DOM.getAbsoluteTop( cellElem ) + getOffsetTop(); int left = DOM.getAbsoluteLeft( cellElem ) + getOffsetLeft(); setPopupPosition( left, top ); // Set the current value setValue( cellValue ); // Show the editor show(); } /** * @return the label text */ public String getLabel() { return layoutTable.getHTML( 0, 0 ); } /** * Set the label for this cell editor. * * @param label the new label */ public void setLabel( String label ) { layoutTable.setHTML( 0, 0, label ); } /** * Accept the contents of the cell editor as the new cell value. */ protected void accept() { // Check if we are ready to accept if ( !onAccept() ) { return; } // Get the value before hiding the editor ColType cellValue = getValue(); // Hide the editor hide(); // Send the new cell value to the callback curCallback.onComplete( curCellEditInfo, cellValue ); curCallback = null; curCellEditInfo = null; } /** * Cancel the cell edit. */ protected void cancel() { // Fire the event if ( !onCancel() ) { return; } // Hide the popup hide(); // Call the callback if ( curCallback != null ) { curCallback.onCancel( curCellEditInfo ); curCellEditInfo = null; curCallback = null; } } /** * @return the Widget that is used to accept the current value. */ protected Widget getAcceptWidget() { ClickDecoratorPanel clickPanel = (ClickDecoratorPanel) layoutTable.getWidget( 1, 1 ); return clickPanel.getWidget(); } /** * @return the Widget that is used to cancel editing. */ protected Widget getCancelWidget() { ClickDecoratorPanel clickPanel = (ClickDecoratorPanel) layoutTable.getWidget( 1, 2 ); return clickPanel.getWidget(); } /** * @return the content widget */ protected Widget getContentWidget() { return layoutTable.getWidget( 1, 0 ); } /** * Get the additional number of pixels to offset this cell editor from the top * left corner of the cell. Override this method to shift the editor left or * right. * * @return the additional left offset in pixels */ protected int getOffsetLeft() { return 0; } /** * Get the additional number of pixels to offset this cell editor from the top * left corner of the cell. Override this method to shift the editor up or * down. * * @return the additional top offset in pixels */ protected int getOffsetTop() { return 0; } /** * Get the new cell value from the editor. * * @return the new cell value */ protected abstract ColType getValue(); /** * Called before an accept takes place. * * @return true to allow the accept, false to prevent it */ protected boolean onAccept() { return true; } /** * Called before a cancel takes place. * * @return true to allow the cancel, false to prevent it */ protected boolean onCancel() { return true; } /** * Set the Widget that is used to accept the current value. * * @param w the widget */ protected void setAcceptWidget( Widget w ) { ClickDecoratorPanel clickPanel = new ClickDecoratorPanel( w, acceptHandler ); clickPanel.setStyleName( "accept" ); layoutTable.setWidget( 1, 1, clickPanel ); } /** * Set the Widget that is used to cancel editing. * * @param w the widget */ protected void setCancelWidget( Widget w ) { ClickDecoratorPanel clickPanel = new ClickDecoratorPanel( w, cancelHandler ); clickPanel.setStyleName( "cancel" ); layoutTable.setWidget( 1, 2, clickPanel ); } /** * Set the cell value in the editor. * * @param cellValue the value in the cell */ protected abstract void setValue( ColType cellValue ); } |