Changeset 4540 for vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/wicket/components/FacetsPanel.java
- Timestamp:
- 02/20/14 16:09:24 (10 years ago)
- 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 17 17 package eu.clarin.cmdi.vlo.wicket.components; 18 18 19 import eu.clarin.cmdi.vlo.pojo.FacetSelection; 19 20 import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection; 20 21 import eu.clarin.cmdi.vlo.service.FacetFieldsService; 21 22 import eu.clarin.cmdi.vlo.wicket.provider.FacetFieldsDataProvider; 22 23 import java.util.Collection; 24 import java.util.HashSet; 23 25 import org.apache.solr.client.solrj.response.FacetField; 24 26 import org.apache.wicket.ajax.AjaxRequestTarget; … … 27 29 import org.apache.wicket.markup.repeater.data.DataView; 28 30 import org.apache.wicket.model.IModel; 31 import org.apache.wicket.model.Model; 29 32 import org.apache.wicket.spring.injection.annot.SpringBean; 30 33 31 34 /** 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. 33 42 * 34 43 * @author twagoo … … 38 47 @SpringBean 39 48 private FacetFieldsService facetFieldsService; 49 private final IModel<QueryFacetsSelection> model; 40 50 41 public FacetsPanel(final String id, finalIModel<QueryFacetsSelection> model) {51 public FacetsPanel(final String id, IModel<QueryFacetsSelection> model) { 42 52 super(id, model); 53 this.model = model; 43 54 44 55 add(new DataView<FacetField>("facets", new FacetFieldsDataProvider(facetFieldsService, model)) { … … 46 57 @Override 47 58 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); 65 60 } 66 61 }); 67 62 } 68 63 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 } 69 118 }
Note: See TracChangeset
for help on using the changeset viewer.