Search in sources :

Example 16 with Context

use of org.gwtproject.cell.client.Cell.Context in project gwtproject by treblereel.

the class ColumnTest method testOnBrowserEventWithFieldUpdater.

public void testOnBrowserEventWithFieldUpdater() {
    final Element theElem = Document.get().createDivElement();
    final NativeEvent theEvent = Document.get().createClickEvent(0, 0, 0, 0, 0, false, false, false, false);
    final MockEditableCell cell = new MockEditableCell() {

        @Override
        public void onBrowserEvent(Context context, Element parent, String value, NativeEvent event, ValueUpdater<String> valueUpdater) {
            assertEquals(theElem, parent);
            assertEquals("test", value);
            assertEquals("oldViewData", getViewData("test"));
            assertEquals(theEvent, event);
            assertNotNull(valueUpdater);
            setViewData("test", "newViewData");
            valueUpdater.update("newValue");
        }
    };
    final Column<String, String> column = new IdentityColumn<String>(cell);
    final MockFieldUpdater<String, String> fieldUpdater = new MockFieldUpdater<String, String>() {

        @Override
        public void update(int index, String object, String value) {
            // The new view data should already be set.
            assertEquals("newViewData", cell.getViewData("test"));
            super.update(index, object, value);
        }
    };
    column.setFieldUpdater(fieldUpdater);
    cell.setViewData("test", "oldViewData");
    Context context = new Context(3, 0, null);
    column.onBrowserEvent(context, theElem, "test", theEvent);
    fieldUpdater.assertUpdateCalled(true);
    fieldUpdater.assertIndex(3);
    fieldUpdater.assertObject("test");
    fieldUpdater.assertValue("newValue");
}
Also used : Context(org.gwtproject.cell.client.Cell.Context) ValueUpdater(org.gwtproject.cell.client.ValueUpdater) Element(org.gwtproject.dom.client.Element) NativeEvent(org.gwtproject.dom.client.NativeEvent)

Example 17 with Context

use of org.gwtproject.cell.client.Cell.Context in project gwtproject by treblereel.

the class DefaultCellTableBuilder method buildRowImpl.

@Override
public void buildRowImpl(T rowValue, int absRowIndex) {
    // Calculate the row styles.
    SelectionModel<? super T> selectionModel = cellTable.getSelectionModel();
    boolean isSelected = (selectionModel == null || rowValue == null) ? false : selectionModel.isSelected(rowValue);
    boolean isEven = absRowIndex % 2 == 0;
    StringBuilder trClasses = new StringBuilder(isEven ? evenRowStyle : oddRowStyle);
    if (isSelected) {
        trClasses.append(selectedRowStyle);
    }
    // Add custom row styles.
    RowStyles<T> rowStyles = cellTable.getRowStyles();
    if (rowStyles != null) {
        String extraRowStyles = rowStyles.getStyleNames(rowValue, absRowIndex);
        if (extraRowStyles != null) {
            trClasses.append(" ").append(extraRowStyles);
        }
    }
    // Build the row.
    TableRowBuilder tr = startRow(rowValue);
    tr.className(trClasses.toString());
    // Build the columns.
    int columnCount = cellTable.getColumnCount();
    for (int curColumn = 0; curColumn < columnCount; curColumn++) {
        Column<T, ?> column = cellTable.getColumn(curColumn);
        // Create the cell styles.
        StringBuilder tdClasses = new StringBuilder(cellStyle);
        tdClasses.append(isEven ? evenCellStyle : oddCellStyle);
        if (curColumn == 0) {
            tdClasses.append(firstColumnStyle);
        }
        if (isSelected) {
            tdClasses.append(selectedCellStyle);
        }
        // The first and last column could be the same column.
        if (curColumn == columnCount - 1) {
            tdClasses.append(lastColumnStyle);
        }
        // Add class names specific to the cell.
        Context context = new Context(absRowIndex, curColumn, cellTable.getValueKey(rowValue));
        String cellStyles = column.getCellStyleNames(context, rowValue);
        if (cellStyles != null) {
            tdClasses.append(" " + cellStyles);
        }
        // Build the cell.
        HorizontalAlignmentConstant hAlign = column.getHorizontalAlignment();
        VerticalAlignmentConstant vAlign = column.getVerticalAlignment();
        TableCellBuilder td = tr.startTD();
        td.className(tdClasses.toString());
        if (hAlign != null) {
            td.align(hAlign.getTextAlignString());
        }
        if (vAlign != null) {
            td.vAlign(vAlign.getVerticalAlignString());
        }
        addCellAttributes(td);
        // Add the inner div.
        DivBuilder div = td.startDiv();
        div.style().outlineStyle(OutlineStyle.NONE).endStyle();
        // Render the cell into the div.
        renderCell(div, context, column, rowValue);
        // End the cell.
        div.endDiv();
        td.endTD();
    }
    // End the row.
    tr.endTR();
}
Also used : Context(org.gwtproject.cell.client.Cell.Context) VerticalAlignmentConstant(org.gwtproject.user.client.ui.HasVerticalAlignment.VerticalAlignmentConstant) DivBuilder(org.gwtproject.dom.builder.shared.DivBuilder) TableRowBuilder(org.gwtproject.dom.builder.shared.TableRowBuilder) TableCellBuilder(org.gwtproject.dom.builder.shared.TableCellBuilder) HorizontalAlignmentConstant(org.gwtproject.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant)

Example 18 with Context

use of org.gwtproject.cell.client.Cell.Context in project gwtproject by treblereel.

the class CellList method resetFocusOnCell.

@Override
protected boolean resetFocusOnCell() {
    int row = getKeyboardSelectedRow();
    if (isRowWithinBounds(row)) {
        Element rowElem = getKeyboardSelectedElement();
        Element cellParent = getCellParent(rowElem);
        T value = getVisibleItem(row);
        Context context = new Context(row + getPageStart(), 0, getValueKey(value));
        return cell.resetFocus(context, cellParent, value);
    }
    return false;
}
Also used : Context(org.gwtproject.cell.client.Cell.Context) Element(org.gwtproject.dom.client.Element) DivElement(org.gwtproject.dom.client.DivElement)

Example 19 with Context

use of org.gwtproject.cell.client.Cell.Context in project gwtproject by treblereel.

the class CellList method onBrowserEvent2.

@Override
protected void onBrowserEvent2(Event event) {
    // Get the event target.
    EventTarget eventTarget = event.getEventTarget();
    if (!Element.is(eventTarget)) {
        return;
    }
    final Element target = event.getEventTarget().cast();
    // Forward the event to the cell.
    String idxString = "";
    Element cellTarget = target;
    while ((cellTarget != null) && ((idxString = cellTarget.getAttribute("__idx")).length() == 0)) {
        cellTarget = cellTarget.getParentElement();
    }
    if (idxString.length() > 0) {
        // Select the item if the cell does not consume events. Selection occurs
        // before firing the event to the cell in case the cell operates on the
        // currently selected item.
        String eventType = event.getType();
        boolean isClick = BrowserEvents.CLICK.equals(eventType);
        int idx = Integer.parseInt(idxString);
        int indexOnPage = idx - getPageStart();
        if (!isRowWithinBounds(indexOnPage)) {
            // If the event causes us to page, then the index will be out of bounds.
            return;
        }
        // Get the cell parent before doing selection in case the list is redrawn.
        boolean isSelectionHandled = cell.handlesSelection() || HasKeyboardSelectionPolicy.KeyboardSelectionPolicy.BOUND_TO_SELECTION == getKeyboardSelectionPolicy();
        Element cellParent = getCellParent(cellTarget);
        T value = getVisibleItem(indexOnPage);
        Context context = new Context(idx, 0, getValueKey(value));
        CellPreviewEvent<T> previewEvent = CellPreviewEvent.fire(this, event, this, context, value, cellIsEditing, isSelectionHandled);
        // Fire the event to the cell if the list has not been refreshed.
        if (!previewEvent.isCanceled()) {
            fireEventToCell(context, event, cellParent, value);
        }
    }
}
Also used : Context(org.gwtproject.cell.client.Cell.Context) Element(org.gwtproject.dom.client.Element) DivElement(org.gwtproject.dom.client.DivElement) EventTarget(org.gwtproject.dom.client.EventTarget)

Example 20 with Context

use of org.gwtproject.cell.client.Cell.Context in project gwtproject by treblereel.

the class CellTreeNodeView method fireEventToCell.

/**
 * Fire an event to the {@link AbstractCell}.
 *
 * @param event the native event
 */
@SuppressWarnings("unchecked")
protected void fireEventToCell(NativeEvent event) {
    if (parentNodeInfo == null) {
        return;
    }
    Cell<T> parentCell = parentNodeInfo.getCell();
    String eventType = event.getType();
    Element cellParent = getCellParent();
    Object key = getValueKey();
    Context context = new Context(getIndex(), 0, key);
    boolean cellWasEditing = parentCell.isEditing(context, cellParent, value);
    // Update selection.
    boolean isSelectionHandled = parentCell.handlesSelection() || KeyboardSelectionPolicy.BOUND_TO_SELECTION == tree.getKeyboardSelectionPolicy();
    HasData<T> display = (HasData<T>) parentNode.listView;
    CellPreviewEvent<T> previewEvent = CellPreviewEvent.fire(display, event, display, context, value, cellWasEditing, isSelectionHandled);
    // Forward the event to the cell.
    if (previewEvent.isCanceled() || !cellParent.isOrHasChild(Element.as(event.getEventTarget()))) {
        return;
    }
    Set<String> consumedEvents = parentCell.getConsumedEvents();
    if (consumedEvents != null && consumedEvents.contains(eventType)) {
        parentCell.onBrowserEvent(context, cellParent, value, event, parentNodeInfo.getValueUpdater());
        tree.cellIsEditing = parentCell.isEditing(context, cellParent, value);
        if (cellWasEditing && !tree.cellIsEditing) {
            CellBasedWidgetImpl.get().resetFocus(new Scheduler.ScheduledCommand() {

                @Override
                public void execute() {
                    tree.setFocus(true);
                }
            });
        }
    }
}
Also used : Context(org.gwtproject.cell.client.Cell.Context) Scheduler(org.gwtproject.core.client.Scheduler) Element(org.gwtproject.dom.client.Element) AnchorElement(org.gwtproject.dom.client.AnchorElement) UIObject(org.gwtproject.user.client.ui.UIObject) HasData(org.gwtproject.view.client.HasData)

Aggregations

Context (org.gwtproject.cell.client.Cell.Context)33 Element (org.gwtproject.dom.client.Element)14 SafeHtmlBuilder (org.gwtproject.safehtml.shared.SafeHtmlBuilder)13 NativeEvent (org.gwtproject.dom.client.NativeEvent)9 ViewData (org.gwtproject.cell.client.EditTextCell.ViewData)3 ValueUpdater (org.gwtproject.cell.client.ValueUpdater)3 TableCellBuilder (org.gwtproject.dom.builder.shared.TableCellBuilder)2 DivElement (org.gwtproject.dom.client.DivElement)2 EventTarget (org.gwtproject.dom.client.EventTarget)2 ImageElement (org.gwtproject.dom.client.ImageElement)2 TableCellElement (org.gwtproject.dom.client.TableCellElement)2 TableElement (org.gwtproject.dom.client.TableElement)2 TableRowElement (org.gwtproject.dom.client.TableRowElement)2 TableSectionElement (org.gwtproject.dom.client.TableSectionElement)2 HorizontalAlignmentConstant (org.gwtproject.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant)2 VerticalAlignmentConstant (org.gwtproject.user.client.ui.HasVerticalAlignment.VerticalAlignmentConstant)2 HasCell (org.gwtproject.cell.client.HasCell)1 TextCell (org.gwtproject.cell.client.TextCell)1 Scheduler (org.gwtproject.core.client.Scheduler)1 DivBuilder (org.gwtproject.dom.builder.shared.DivBuilder)1