Ignore:
Timestamp:
02/20/14 16:09:24 (10 years ago)
Author:
twagoo
Message:

Added a panel for facets with selected valeus that allow for deselection. FacetsPanel? adds either an instance of this or of the value select panel depending on whether an existing selection is present.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/wicket/components/FacetsPanel.java

    r4538 r4540  
    1717package eu.clarin.cmdi.vlo.wicket.components;
    1818
     19import eu.clarin.cmdi.vlo.pojo.FacetSelection;
    1920import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection;
    2021import eu.clarin.cmdi.vlo.service.FacetFieldsService;
    2122import eu.clarin.cmdi.vlo.wicket.provider.FacetFieldsDataProvider;
    2223import java.util.Collection;
     24import java.util.HashSet;
    2325import org.apache.solr.client.solrj.response.FacetField;
    2426import org.apache.wicket.ajax.AjaxRequestTarget;
     
    2729import org.apache.wicket.markup.repeater.data.DataView;
    2830import org.apache.wicket.model.IModel;
     31import org.apache.wicket.model.Model;
    2932import org.apache.wicket.spring.injection.annot.SpringBean;
    3033
    3134/**
    32  * A panel representing a group of facets
     35 * A panel representing a group of facets.
     36 *
     37 * For each facet present (retrieved from the injected
     38 * {@link FacetFieldsService}, a panel is added. This is either a
     39 * {@link FacetValuesPanel}, allowing for selection of facet values, or a
     40 * {@link SelectedFacetPanel} representing a facet with selected values,
     41 * allowing for deselection of these values.
    3342 *
    3443 * @author twagoo
     
    3847    @SpringBean
    3948    private FacetFieldsService facetFieldsService;
     49    private final IModel<QueryFacetsSelection> model;
    4050
    41     public FacetsPanel(final String id, final IModel<QueryFacetsSelection> model) {
     51    public FacetsPanel(final String id, IModel<QueryFacetsSelection> model) {
    4252        super(id, model);
     53        this.model = model;
    4354
    4455        add(new DataView<FacetField>("facets", new FacetFieldsDataProvider(facetFieldsService, model)) {
     
    4657            @Override
    4758            protected void populateItem(Item<FacetField> item) {
    48                 item.add(new FacetValuesPanel("facet", item.getModel()) {
    49 
    50                     @Override
    51                     public void onValuesSelected(String facet, Collection<String> value, AjaxRequestTarget target) {
    52                         // A value has been selected on this facet's panel,
    53                         // update the model!
    54                         model.getObject().selectValues(facet, value);
    55 
    56                         // Trigger updates to reflect new model state
    57                         modelChanged();
    58 
    59                         if (target != null) {
    60                             // reload entire page for now
    61                             target.add(getPage());
    62                         }
    63                     }
    64                 });
     59                createFacetPanel(item);
    6560            }
    6661        });
    6762    }
    6863
     64    private void createFacetPanel(Item<FacetField> item) {
     65        // Is there a selection for this facet?
     66        final IModel<FacetField> facetFieldModel = item.getModel();
     67        final String facetName = facetFieldModel.getObject().getName();
     68        final Collection<String> selectionValues = model.getObject().getSelectionValues(facetName);
     69
     70        // Show different panel, depending on selected values
     71        if (selectionValues == null || selectionValues.isEmpty()) {
     72            // No values selected, show value selection panel
     73            item.add(createFacetValuesPanel(facetFieldModel));
     74        } else {
     75            // Values selected, show selected values panel (with option to remove)
     76            item.add(createSelectedFacetPanel(facetName));
     77        }
     78    }
     79
     80    private FacetValuesPanel createFacetValuesPanel(final IModel<FacetField> facetFieldModel) {
     81        return new FacetValuesPanel("facet", facetFieldModel) {
     82
     83            @Override
     84            public void onValuesSelected(String facet, Collection<String> value, AjaxRequestTarget target) {
     85                // A value has been selected on this facet's panel,
     86                // update the model!
     87                model.getObject().selectValues(facet, value);
     88
     89                if (target != null) {
     90                    // reload entire page for now
     91                    target.add(getPage());
     92                }
     93            }
     94        };
     95    }
     96
     97    private SelectedFacetPanel createSelectedFacetPanel(String facetName) {
     98        final FacetSelection selection = new FacetSelection(facetName, model);
     99        return new SelectedFacetPanel("facet", new Model(selection)) {
     100
     101            @Override
     102            public void onValuesUnselected(String facet, Collection<String> valuesRemoved, AjaxRequestTarget target) {
     103                // Values have been removed, calculate remainder
     104                final Collection<String> currentSelection = model.getObject().getSelectionValues(facet);
     105                final Collection<String> newSelection = new HashSet<String>(currentSelection);
     106                newSelection.removeAll(valuesRemoved);
     107
     108                // Update model
     109                model.getObject().selectValues(facet, newSelection);
     110
     111                if (target != null) {
     112                    // reload entire page for now
     113                    target.add(getPage());
     114                }
     115            }
     116        };
     117    }
    69118}
Note: See TracChangeset for help on using the changeset viewer.