org.eurekastreams.web.client.ui.pages.search.SearchResultItemRenderer.java Source code

Java tutorial

Introduction

Here is the source code for org.eurekastreams.web.client.ui.pages.search.SearchResultItemRenderer.java

Source

/*
 * Copyright (c) 2009-2011 Lockheed Martin Corporation
 *
 * 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.eurekastreams.web.client.ui.pages.search;

import java.util.HashMap;
import java.util.Map;

import org.eurekastreams.commons.search.modelview.ModelView;
import org.eurekastreams.server.search.modelview.DomainGroupModelView;
import org.eurekastreams.server.search.modelview.PersonModelView;
import org.eurekastreams.web.client.ui.common.pagedlist.GroupRenderer;
import org.eurekastreams.web.client.ui.common.pagedlist.ItemRenderer;
import org.eurekastreams.web.client.ui.common.pagedlist.PersonRenderer;
import org.eurekastreams.web.client.ui.pages.master.StaticResourceBundle;

import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;

/**
 * Renders an employee.
 */
public class SearchResultItemRenderer implements ItemRenderer<ModelView> {
    /**
     * Renders a person.
     */
    private final ItemRenderer<PersonModelView> personRenderer = new PersonRenderer(true);

    /**
     * Renders a group.
     */
    private final ItemRenderer<DomainGroupModelView> groupRenderer = new GroupRenderer();

    /**
     * Readable metadata keys.
     */
    private final Map<String, String> humanReadableMetadataKeys = new HashMap<String, String>();

    /**
     * Constructor.
     *
     */
    public SearchResultItemRenderer() {
        humanReadableMetadataKeys.put("jobs", "Jobs");
        humanReadableMetadataKeys.put("background", "Skills and Specialties");
        humanReadableMetadataKeys.put("capabilities", "Capabilities");
        humanReadableMetadataKeys.put("education", "Education");
        humanReadableMetadataKeys.put("biography", "Biography");
        humanReadableMetadataKeys.put("overview", "Overview");
    }

    /**
     * @param result
     *            the result to render.
     *
     * @return the result as a widget.
     */
    public Panel render(final ModelView result) {
        Panel resultWidget;

        if (result instanceof PersonModelView) {
            resultWidget = (Panel) personRenderer.render((PersonModelView) result);
        } else if (result instanceof DomainGroupModelView) {
            resultWidget = (Panel) groupRenderer.render((DomainGroupModelView) result);
        } else {
            resultWidget = new FlowPanel();
            resultWidget.add(new Label("Unknown Result Type"));
        }

        Object[] resultArr = result.getFieldMatch().getMatchedFieldKeys().toArray();

        FlowPanel matchedWidget = new FlowPanel();

        if (resultArr.length > 0) {
            Label resultsLbl = new Label("Matches found in: ");
            matchedWidget.add(resultsLbl);
            matchedWidget.addStyleName(StaticResourceBundle.INSTANCE.coreCss().searchMetadata());
            resultWidget.add(matchedWidget);
        }

        for (int i = 0; i < resultArr.length; i++) {
            Label keyLabel = new Label(humanReadableMetadataKeys.get(resultArr[i]));

            if (i + 1 < resultArr.length) {
                keyLabel.setText(keyLabel.getText() + ", ");
            }

            keyLabel.addStyleName(StaticResourceBundle.INSTANCE.coreCss().searchMetadataKey());

            matchedWidget.add(keyLabel);
        }

        return resultWidget;
    }
}