1 | package eu.clarin.cmdi.virtualcollectionregistry.gui.pages; |
---|
2 | |
---|
3 | import eu.clarin.cmdi.virtualcollectionregistry.gui.HandleLinkModel; |
---|
4 | import eu.clarin.cmdi.virtualcollectionregistry.model.Resource; |
---|
5 | import org.apache.wicket.AttributeModifier; |
---|
6 | import org.apache.wicket.markup.html.basic.Label; |
---|
7 | import org.apache.wicket.markup.html.link.ExternalLink; |
---|
8 | import org.apache.wicket.markup.html.panel.Panel; |
---|
9 | import org.apache.wicket.model.AbstractReadOnlyModel; |
---|
10 | import org.apache.wicket.model.IModel; |
---|
11 | import org.apache.wicket.model.PropertyModel; |
---|
12 | import org.apache.wicket.util.string.Strings; |
---|
13 | |
---|
14 | /** |
---|
15 | * |
---|
16 | * @author twagoo |
---|
17 | */ |
---|
18 | public class ReferenceLinkPanel extends Panel { |
---|
19 | |
---|
20 | public ReferenceLinkPanel(String id, IModel<Resource> model) { |
---|
21 | super(id, model); |
---|
22 | |
---|
23 | // Shared rerence model |
---|
24 | final PropertyModel<String> refModel = new PropertyModel<>(model, "ref"); |
---|
25 | |
---|
26 | // Wrapper for link model that detects handles |
---|
27 | final HandleLinkModel linkModel = new HandleLinkModel(refModel); |
---|
28 | final ExternalLink link = new ExternalLink("reference", linkModel); |
---|
29 | link.add(new AttributeModifier("title", true, refModel)); |
---|
30 | |
---|
31 | // Set label on link |
---|
32 | final ReferenceLabelModel labelModel = new ReferenceLabelModel(model); |
---|
33 | link.add(new Label("referenceLabel", labelModel)); |
---|
34 | |
---|
35 | add(link); |
---|
36 | |
---|
37 | final PropertyModel<String> descriptionModel = new PropertyModel<>(model, "description"); |
---|
38 | add(new Label("description", descriptionModel) { |
---|
39 | |
---|
40 | @Override |
---|
41 | protected void onConfigure() { |
---|
42 | super.onConfigure(); |
---|
43 | setVisible(descriptionModel.getObject() != null); |
---|
44 | } |
---|
45 | |
---|
46 | }); |
---|
47 | |
---|
48 | } |
---|
49 | |
---|
50 | /** |
---|
51 | * String model that returns the label of a {@link Resource} if it is |
---|
52 | * available (not null or empty), otherwise the reference URI |
---|
53 | */ |
---|
54 | private static class ReferenceLabelModel extends AbstractReadOnlyModel<String> { |
---|
55 | |
---|
56 | private final IModel<Resource> resourceModel; |
---|
57 | |
---|
58 | public ReferenceLabelModel(IModel<Resource> resourceModel) { |
---|
59 | this.resourceModel = resourceModel; |
---|
60 | } |
---|
61 | |
---|
62 | @Override |
---|
63 | public String getObject() { |
---|
64 | final Resource resource = resourceModel.getObject(); |
---|
65 | if (resource == null) { |
---|
66 | return null; |
---|
67 | } else { |
---|
68 | if (!Strings.isEmpty(resource.getLabel())) { |
---|
69 | return resource.getLabel(); |
---|
70 | } |
---|
71 | return resource.getRef(); |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | @Override |
---|
76 | public void detach() { |
---|
77 | resourceModel.detach(); |
---|
78 | } |
---|
79 | |
---|
80 | } |
---|
81 | |
---|
82 | } |
---|