Quantcast
Channel: SmartClient Forums
Viewing all articles
Browse latest Browse all 4756

Hilites do not match gird data

$
0
0
Hilites do not match gird data

All I want to do is color the entire row, based on one field.
I reworked your Simulated Stock Quotes Example in the EE showcase to use hilites.

First a button to launch a window
Code:

IButton button = new IButton("launch window");       
button.addClickHandler(new ClickHandler()
{
        int count = 1;
  @Override
        public void onClick(ClickEvent clickEvent)
        {
                new GuiWidgetLab("window" + count++).show();
        }
});

Click the button -> launch window 0 -> The servlet turns on updates for the next 90 seconds -> the updates go into the datasource -> the data updates -> the hilites update.
wait for the updates to end.
Click the button -> launch window 1 -> The servlet turns on updates for the next 90 seconds -> the updates go into the datasource -> the data updates -> the hilites update in all windows ( in window 1 they are correct. Window 0 they are not.)
wait for the updates to end.
Click the button -> launch window 2 -> The servlet turns on updates for the next 90 seconds -> the updates go into the datasource -> the data updates -> the hilites update in all windows ( in window 2 they are correct. Window 0 and window 1, they are not. )

The end state of the hilites for windows 0 and 1, is normally wrong (green rows with negitive change values, or red rows with positive), and windoww 1 doesn't match window 0.

The latest window, the hilite state is correct.


Code:

/*
 * Isomorphic SmartGWT web presentation layer
 * Copyright (c) 2011 Isomorphic Software, Inc.
 *
 * OWNERSHIP NOTICE
 * Isomorphic Software owns and reserves all rights not expressly granted in this source code,
 * including all intellectual property rights to the structure, sequence, and format of this code
 * and to all designs, interfaces, algorithms, schema, protocols, and inventions expressed herein.
 *
 *  If you have any questions, please email <sourcecode@isomorphic.com>.
 *
 *  This entire comment must accompany any portion of Isomorphic Software source code that is
 *  copied or moved from this file.
 */


import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.Timer;
import com.smartgwt.client.data.*;
import com.smartgwt.client.rpc.Messaging;
import com.smartgwt.client.rpc.MessagingCallback;
import com.smartgwt.client.rpc.RPCManager;
import com.smartgwt.client.rpc.RPCRequest;
import com.smartgwt.client.types.DSOperationType;
import com.smartgwt.client.types.OperatorId;
import com.smartgwt.client.util.JSOHelper;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class GuiWidgetLab extends Window
{
    private ListGrid stockQuotesGrid;

    public GuiWidgetLab(String title)
    {
        super();
        setTitle(title);
        setWidth("50%");
        setHeight("50%");
        final long startParameter = System.currentTimeMillis();

        // Grid and button below it
        setMembersMargin(10);
        setPadding(10);

        stockQuotesGrid = new ListGrid();
        stockQuotesGrid.setWidth(600);
        stockQuotesGrid.setHeight(300);
        stockQuotesGrid.setShowAllRecords(true);
        stockQuotesGrid.setDataSource(StockQuotesDS.getInstance());
        stockQuotesGrid.setAutoFetchData(true);
        stockQuotesGrid.enableHiliting();
        stockQuotesGrid.setCanEditHilites(false);
        stockQuotesGrid.setIncludeHilitesInSummaryFields(true);


        List<ListGridField> fieldList = new ArrayList<>(StockQuotesDS.getInstance().getFields().length);
        fieldList.add(new ListGridField("id"));
        fieldList.add(new ListGridField("name"));
        fieldList.add(new ListGridField("symbol"));
        fieldList.add(new ListGridField("lastValue"));
        fieldList.add(new ListGridField("changeValue"));
        fieldList.add(new ListGridField("openValue"));
        fieldList.add(new ListGridField("dayHighValue"));
        fieldList.add(new ListGridField("dayLowValue"));
        ListGridField lastUpdatedField = new ListGridField("lastUpdated");
        lastUpdatedField.setHidden(true);
        fieldList.add(lastUpdatedField);

        ListGridField[] fields = fieldList.toArray(new ListGridField[fieldList.size()]);
        stockQuotesGrid.setFields(fields);
        stockQuotesGrid.setHilites(buildHilites(fields));
        addItem(stockQuotesGrid);

        final Button generateUpdatesButton = new Button("Generate more updates");
        generateUpdatesButton.setWidth(200);
        generateUpdatesButton.addClickHandler(new ClickHandler()
        {
            @Override
            public void onClick(ClickEvent event)
            {
                generateUpdates(startParameter, generateUpdatesButton);
            }
        });
        addItem(generateUpdatesButton);
        // receive messages from the stockQuotes channel and update data grid
        Messaging.subscribe("stockQuotes" + startParameter, new MessagingCallback() {
            @Override
            public void execute(Object data) {
                updateStockQuotes(data);
            }
        });

        generateUpdates(startParameter, generateUpdatesButton);
    }

    /**
    * We get id and changeValue only from server - combine it with the record
    * in the grid to get the rest of the fields
    *
    * @param data
    */
    @SuppressWarnings("unchecked")
    private void updateStockQuotes(Object data) {
        List<List<?>> stockData = (List<List<?>>) JSOHelper
                .convertToJava((JavaScriptObject) data);
        List<Record> newStockData = new ArrayList<Record>();
        // prepare data for grid manually using received data from servlet
        // we receive only 'id' and 'change percent' data here
        for (List<?> recordData : stockData) {
            float change = ((Number) recordData.get(1)).floatValue();
            if (change != 0) {
                final Integer id = (Integer) recordData.get(0);
                Record record = stockQuotesGrid.getDataAsRecordList().find("id", id);
                float lastValue = record.getAttributeAsFloat("lastValue");
                float newChangeValue = change * lastValue / 100;
                float newLastValue = newChangeValue + lastValue;
                record.setAttribute("changeValue", newChangeValue);
                record.setAttribute("lastValue", newLastValue);
                record.setAttribute("dayHighValue",
                        Math.max(record.getAttributeAsFloat("dayHighValue"), newLastValue));
                record.setAttribute("dayLowValue",
                        Math.min(record.getAttributeAsFloat("dayLowValue"), newLastValue));
                record.setAttribute("lastUpdated", new Date());
                newStockData.add(record);

                new Timer()
                {
                    @Override
                    public void run()
                    {
                        Record record = stockQuotesGrid.getDataAsRecordList().find("id", id);
                        if(record.getAttribute("lastUpdated") != null)
                        {
                            Date lastUpdated = record.getAttributeAsDate("lastUpdated");
                            long delta = System.currentTimeMillis() - lastUpdated.getTime();
                            if(delta >= blinkPeriod)
                            {
                                record.setAttribute("lastUpdated", (Date) null);
                            }
                            DSResponse dsResponse = new DSResponse();
                            Record[] toUpdate = new Record[] {record};
                            dsResponse.setData(toUpdate);

                            DSRequest dsRequest = new DSRequest();
                            dsRequest.setOperationType(DSOperationType.UPDATE);
                            // broadcast the change - the grid will notice this automatically (and so would other
                            // components showing the same record)
                            StockQuotesDS.getInstance().updateCaches(dsResponse, dsRequest);

                        }
                    }
                }.schedule(blinkPeriod);
            }
        }

        DSResponse dsResponse = new DSResponse();
        dsResponse.setData((Record[]) newStockData.toArray(new Record[newStockData.size()]));

        DSRequest dsRequest = new DSRequest();
        dsRequest.setOperationType(DSOperationType.UPDATE);
        // broadcast the change - the grid will notice this automatically (and so would other
        // components showing the same record)
        StockQuotesDS.getInstance().updateCaches(dsResponse, dsRequest);
    }
    private int blinkPeriod = 2000;

    private void generateUpdates(final long startParameter, final Button generateUpdatesButton) {
        generateUpdatesButton.disable();
        RPCRequest request = new RPCRequest();
        // we tells servlet which channel it should use for sending data
        request.setActionURL("examples/StockQuotes/generate?sp=" + startParameter);
        RPCManager.sendRequest(request);
        // block button repeat click for 90 seconds - time while servlet
        // will send data to us
        new Timer() {
            public void run() {
                generateUpdatesButton.enable();
            }
        }.schedule(90000);
    }


    public static Hilite[] buildHilites(ListGridField... fields)
    {
        String [] names = new String[fields.length];
        int i = 0;
        for(ListGridField field: fields)
        {
            names[i] = field.getName();
            i++;
        }
        return buildHilites(names);
    }

    public static Hilite[] buildHilites(String... fields)
    {
        List<Hilite> lites= buildHilitesList(fields);
        return lites.toArray(new Hilite[lites.size()]);
    }

    public static List<Hilite> buildHilitesList(String... fields)
    {
        List<Hilite> lites= new ArrayList<>();
        lites.add(buildHilite("#000000", "#FF0000", fields, new Criterion("changeValue", OperatorId.LESS_THAN, 0f) ));
        lites.add(buildHilite("#000000", "#00FF00", fields, new Criterion("changeValue", OperatorId.GREATER_THAN, 0f)));
        lites.add(buildHilite("#000000", "#FFFFFF", fields, new Criterion("changeValue", OperatorId.EQUALS, 0f)));
        lites.add(buildHiliteNull("#000000", "#FFFFFF", fields, "lastUpdated"));
        return lites;
    }

    public static Hilite buildHilite(String textColor, String bkgrndColor, String[] hiliteFields, Criterion ... criterions)
    {
        Hilite build = new Hilite();
        build.setTextColor(textColor);
        build.setBackgroundColor(bkgrndColor);
        build.setFieldNames(hiliteFields);
        if(criterions == null || criterions.length <= 0 )
        {
            return build;
        }
        AdvancedCriteria criteria = new AdvancedCriteria(OperatorId.OR, criterions);
        build.setCriteria(criteria);
        return build;
    }
    public static Hilite buildHiliteNull(String textColor, String bkgrndColor, String[] hiliteFields, String criteriaField)
    {
        Hilite build = new Hilite();
        build.setTextColor(textColor);
        build.setBackgroundColor(bkgrndColor);
        build.setFieldNames(hiliteFields);

        List<Criterion> criterionList = new ArrayList<>(1);
            criterionList.add(new Criterion(criteriaField, OperatorId.IS_NULL ));
        AdvancedCriteria criteria = new AdvancedCriteria(OperatorId.OR, criterionList.toArray(new Criterion[criterionList.size()]));
        build.setCriteria(criteria);
        return build;
    }


}



1. SmartClient Version: v9.1p_2014-12-12/PowerEdition Deployment (built 2014-12-12)

2. FF 33.1.1
Chrome 39.0.2171.95m

3. - 5. n/a

6. see above

Viewing all articles
Browse latest Browse all 4756

Trending Articles