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

Last change on this file since 4199 was 4199, checked in by keeloo, 10 years ago
File size: 5.0 KB
Line 
1package eu.clarin.cmdi.vlo.pages;
2
3import eu.clarin.cmdi.vlo.VloPageParameters;
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;
18
19public class FacetBoxPanel extends BasePanel {
20    private static final Set<String> IGNORABLE_VALUES = new HashSet<String>();
21    static {
22        IGNORABLE_VALUES.add("unknown");
23        IGNORABLE_VALUES.add("unspecified");
24    }
25    private static final int MAX_NR_OF_FACET_VALUES = 10;
26    private static final long serialVersionUID = 1L;
27    private Label label;
28    private FacetHeaderPanel facetHeaderPanel;
29    private FacetModel facetModel;
30    private int maxNrOfFacetValues;
31   
32    public FacetBoxPanel(String id, IModel<FacetField> model, String tooltipText) {
33        super(id, model);
34        setOutputMarkupId(true);
35        setMaxNrOfFacetValues(MAX_NR_OF_FACET_VALUES);
36        add(new AttributeModifier("title", tooltipText));
37       
38    }
39     
40    @SuppressWarnings({"serial"})
41    public FacetBoxPanel create(final SearchPageQuery query) {
42        final FacetField facetField = (FacetField) getDefaultModelObject();
43        facetModel = new FacetModel(facetField);
44        facetModel.setSelectedValue(query.getSelectedValue(facetField));
45        label = new Label("headerLabel", facetField.getName());
46        add(label);
47        facetHeaderPanel = new FacetHeaderPanel("facetHeaderPanel", new Model<FacetModel>(facetModel), query);
48        if (facetModel.isSelected()) {
49            add(facetHeaderPanel);
50        } else {
51            add(new WebMarkupContainer("facetHeaderPanel"));
52        }
53        final boolean showMore = facetField.getValueCount() > maxNrOfFacetValues + 1;
54        List<Count> values = getFacetListForBox(facetField, showMore);
55        ListView<Count> facetList = new ListView<Count>("facetList", values) {
56            @Override
57            protected void populateItem(ListItem<Count> item) {
58                item.add(new FacetLinkPanel("facetLinks", item.getModel(), query));
59            }
60
61            @Override
62            public boolean isVisible() {
63                return !facetModel.isSelected();
64            }
65        };
66        add(facetList);
67        VloPageParameters facetParameters = new VloPageParameters ();
68               
69        facetParameters.add(ShowAllFacetValuesPage.SELECTED_FACET_PARAM, facetField.getName());
70        facetParameters.add(ShowAllFacetValuesPage.FACET_MIN_OCCURS, "1");
71        facetParameters.addToSession();
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.