source: vlo/branches/to-wicket-1.6/vlo_web_app/src/main/java/eu/clarin/cmdi/vlo/pages/FacetBoxPanel.java @ 4240

Last change on this file since 4240 was 4240, checked in by twagoo, 10 years ago

Replaced calls getting session (either on component or statically) and casting to VloSession? with new convenience method VloSession?.get()

File size: 5.1 KB
Line 
1package eu.clarin.cmdi.vlo.pages;
2
3import eu.clarin.cmdi.vlo.VloSession;
4import java.util.ArrayList;
5import java.util.HashSet;
6import java.util.List;
7import java.util.Set;
8import org.apache.solr.client.solrj.response.FacetField;
9import org.apache.solr.client.solrj.response.FacetField.Count;
10import org.apache.wicket.AttributeModifier;
11import org.apache.wicket.markup.html.WebMarkupContainer;
12import org.apache.wicket.markup.html.basic.Label;
13import org.apache.wicket.markup.html.link.BookmarkablePageLink;
14import org.apache.wicket.markup.html.list.ListItem;
15import org.apache.wicket.markup.html.list.ListView;
16import org.apache.wicket.model.IModel;
17import org.apache.wicket.model.Model;
18import org.apache.wicket.request.mapper.parameter.PageParameters;
19
20public class FacetBoxPanel extends BasePanel {
21    private static final Set<String> IGNORABLE_VALUES = new HashSet<String>();
22    static {
23        IGNORABLE_VALUES.add("unknown");
24        IGNORABLE_VALUES.add("unspecified");
25    }
26    private static final int MAX_NR_OF_FACET_VALUES = 10;
27    private static final long serialVersionUID = 1L;
28    private Label label;
29    private FacetHeaderPanel facetHeaderPanel;
30    private FacetModel facetModel;
31    private int maxNrOfFacetValues;
32   
33    public FacetBoxPanel(String id, IModel<FacetField> model, String tooltipText) {
34        super(id, model);
35        setOutputMarkupId(true);
36        setMaxNrOfFacetValues(MAX_NR_OF_FACET_VALUES);
37        add(new AttributeModifier("title", tooltipText));
38       
39    }
40     
41    @SuppressWarnings({"serial"})
42    public FacetBoxPanel create(final SearchPageQuery query) {
43        final FacetField facetField = (FacetField) getDefaultModelObject();
44        facetModel = new FacetModel(facetField);
45        facetModel.setSelectedValue(query.getSelectedValue(facetField));
46        label = new Label("headerLabel", facetField.getName());
47        add(label);
48        facetHeaderPanel = new FacetHeaderPanel("facetHeaderPanel", new Model<FacetModel>(facetModel), query);
49        if (facetModel.isSelected()) {
50            add(facetHeaderPanel);
51        } else {
52            add(new WebMarkupContainer("facetHeaderPanel"));
53        }
54        final boolean showMore = facetField.getValueCount() > maxNrOfFacetValues + 1;
55        List<Count> values = getFacetListForBox(facetField, showMore);
56        ListView<Count> facetList = new ListView<Count>("facetList", values) {
57            @Override
58            protected void populateItem(ListItem<Count> item) {
59                item.add(new FacetLinkPanel("facetLinks", item.getModel(), query));
60            }
61
62            @Override
63            public boolean isVisible() {
64                return !facetModel.isSelected();
65            }
66        };
67        add(facetList);
68        PageParameters facetParameters = new PageParameters ();
69        facetParameters.add(ShowAllFacetValuesPage.SELECTED_FACET_PARAM, facetField.getName());
70        facetParameters.add(ShowAllFacetValuesPage.FACET_MIN_OCCURS, "1");
71        facetParameters.mergeWith(VloSession.get().getVloSessionPageParameters());
72
73        add(new BookmarkablePageLink("showMore", ShowAllFacetValuesPage.class, facetParameters) {
74            @Override
75            public boolean isVisible() {
76                return !facetModel.isSelected() && showMore;
77            }
78        });
79        return this;
80    }
81
82    List<Count> getFacetListForBox(final FacetField facetField, final boolean showMore) {
83        List<Count> allValues = facetField.getValues();
84        List<Count> values = new ArrayList<Count>();
85        if (showMore) {
86            if (facetField.getValueCount() == maxNrOfFacetValues || facetField.getValueCount() == maxNrOfFacetValues + 1) { //Show all values, the "more" link can be used as the extra facet
87                values = allValues;
88            } else {// make a sublist
89                //IGNORABLE_VALUES (like "unknown") are move to the back of the list and should only be shown when you click "more...", unless the list is too small then whe can just show them.
90                List<Count> ignorables = new ArrayList<Count>();
91                for (int i = 0; values.size() < maxNrOfFacetValues && i < allValues.size(); i++) {
92                    Count count = allValues.get(i);
93                    if (!IGNORABLE_VALUES.contains(count.getName().toLowerCase())) {
94                        values.add(count);
95                    } else {
96                        ignorables.add(count);
97                    }
98                }
99                int stillToAdd = maxNrOfFacetValues - values.size();
100                for (int i = 0; i < stillToAdd && i < ignorables.size(); i++) {
101                    values.add(ignorables.get(i));
102
103                }
104            }
105        } else { //show all values
106            values = allValues;
107        }
108        return values;
109    }
110
111    public void replaceHeader(boolean isSelected, String selectedValue) {
112        facetModel.setSelectedValue(selectedValue);
113        if (isSelected) {
114            label.replaceWith(facetHeaderPanel);
115        } else {
116            facetHeaderPanel.replaceWith(label);
117        }
118
119    }
120
121    void setMaxNrOfFacetValues(int maxNrOfFacetValues) {
122        this.maxNrOfFacetValues = maxNrOfFacetValues;
123    }
124
125}
Note: See TracBrowser for help on using the repository browser.