All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.uberfire.ext.widgets.common.client.tables.SortableHeader Maven / Gradle / Ivy

There is a newer version: 7.74.1.Final
Show newest version
/*
 * Copyright 2015 JBoss, by Red Hat, 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 org.uberfire.ext.widgets.common.client.tables;

import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.ClickableTextCell;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.Header;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import org.uberfire.ext.widgets.common.client.resources.CommonResources;

/**
 * Based on GWT bikeshed example.
 */
public class SortableHeader extends Header {

    private static final String DOWN_ARROW = makeImage(CommonResources.INSTANCE.images().downArrow());
    private static final String SMALL_DOWN_ARROW = makeImage(CommonResources.INSTANCE.images().smallDownArrow());
    private static final String UP_ARROW = makeImage(CommonResources.INSTANCE.images().upArrow());
    private static final String SMALL_UP_ARROW = makeImage(CommonResources.INSTANCE.images().smallUpArrow());
    private final SortableHeaderGroup sortableHeaderGroup;
    private final Column column;
    private String text;
    private SortDirection sortDirection = SortDirection.NONE;
    private int sortIndex = -1;
    public SortableHeader(SortableHeaderGroup sortableHeaderGroup,
                          String text,
                          Column column) {
        super(new ClickableTextCell());
        this.sortableHeaderGroup = sortableHeaderGroup;
        this.text = text;
        this.column = column;
        setUpdater(new ValueUpdater() {
            public void update(String s) {
                SortableHeader.this.sortableHeaderGroup.headerClicked(SortableHeader.this);
            }
        });
    }

    private static String makeImage(ImageResource resource) {
        AbstractImagePrototype prototype = AbstractImagePrototype.create(resource);
        return prototype.getHTML();
    }

    /**
     * @return the header label
     */
    public String getValue() {
        return text;
    }

    /**
     * Set the Column header
     * @param text
     */
    public void setValue(String text) {
        this.text = text;
    }

    public Column getColumn() {
        return column;
    }

    public SortDirection getSortDirection() {
        return sortDirection;
    }

    public void setSortDirection(SortDirection sortDirection) {
        this.sortDirection = sortDirection;
    }

    public int getSortIndex() {
        return sortIndex;
    }

    public void setSortIndex(int sortIndex) {
        this.sortIndex = sortIndex;
    }

    @Override
    public void render(Cell.Context context,
                       SafeHtmlBuilder sb) {
        sb.appendHtmlConstant("
"); sb.appendHtmlConstant(""); sb.appendEscaped(text); sb.appendHtmlConstant(""); // sb.appendHtmlConstant("
"); switch (sortDirection) { case NONE: // nothing break; case ASCENDING: sb.appendHtmlConstant(sortIndex == 0 ? UP_ARROW : SMALL_UP_ARROW); break; case DESCENDING: sb.appendHtmlConstant(sortIndex == 0 ? DOWN_ARROW : SMALL_DOWN_ARROW); break; default: throw new IllegalArgumentException("Unknown sortDirection (" + sortDirection + ")."); } sb.appendHtmlConstant("
"); // sb.appendHtmlConstant("
"); // sb.appendHtmlConstant("
"); } }



© 2015 - 2024 Weber Informatics LLC | Privacy Policy