74088ca798cea8d2eed1ef43c5302af8c4ecbf71
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / editor / descriptiontree / detailpage / DistributionMapDetailsSection.java
1 /**
2 *
3 */
4 package eu.etaxonomy.taxeditor.editor.descriptiontree.detailpage;
5
6 import java.awt.Color;
7 import java.net.URI;
8 import java.net.URISyntaxException;
9 import java.util.HashSet;
10 import java.util.Map;
11 import java.util.Set;
12
13 import org.apache.log4j.Logger;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.ControlAdapter;
16 import org.eclipse.swt.events.ControlEvent;
17 import org.eclipse.swt.widgets.Composite;
18
19 import eu.etaxonomy.cdm.ext.EditGeoServiceUtilities;
20 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
21 import eu.etaxonomy.cdm.model.description.Distribution;
22 import eu.etaxonomy.cdm.model.description.PresenceAbsenceTermBase;
23 import eu.etaxonomy.cdm.model.description.TaxonDescription;
24 import eu.etaxonomy.taxeditor.editor.descriptiontree.TaxonDescriptionFeature;
25 import eu.etaxonomy.taxeditor.forms.ImageComposite;
26
27 /**
28 * @author nho
29 *
30 */
31 public class DistributionMapDetailsSection extends AbstractDescriptionDetailSection {
32
33 private static final Logger logger = Logger.getLogger(DistributionMapDetailsSection.class);
34
35
36 private ImageComposite imageComposite;
37
38 /**
39 * @param parent
40 * @param page
41 * @param style
42 */
43 public DistributionMapDetailsSection(Composite parent,
44 AbstractDescriptionDetailsPage page, int style) {
45 super(parent, page, style);
46
47 setText("Distribution Overview"); //$NON-NLS-1$
48
49 imageComposite = toolkit.createImageComposite(getClient(), null, SWT.WRAP);
50 imageComposite.addControlListener(new ControlAdapter() {
51 /* (non-Javadoc)
52 * @see org.eclipse.swt.events.ControlAdapter#controlResized(org.eclipse.swt.events.ControlEvent)
53 */
54 @Override
55 public void controlResized(ControlEvent e) {
56 updateSection();
57 }
58 });
59 }
60
61 /* (non-Javadoc)
62 * @see eu.etaxonomy.taxeditor.editor.descriptiontree.detailpage.AbstractDescriptionDetailSection#updateSection()
63 */
64 @Override
65 public void updateSection() {
66 try {
67 imageComposite.setImageUri(this.getImageUri());
68 } catch (URISyntaxException e) {
69 logger.error("Error while building the uri for map webservice.", e);
70 }
71 }
72
73 /**
74 * @return
75 * @throws URISyntaxException
76 */
77 private URI getImageUri() throws URISyntaxException {
78 // FIXME make these hardcoded parameters configurable via preferences
79 String accessPoint = "http://edit.csic.es/v1/areas.php";
80 Map<PresenceAbsenceTermBase<?>,Color> presenceAbsenceTermColors = null;
81 // FIXME due to a bug in the rest map service we have to ensure that width will always be an even number
82 int width = imageComposite.calculateWidth() % 2 == 1 ? imageComposite.calculateWidth() + 1 : imageComposite.calculateWidth();
83
84 String bbox = "-180,-90,180,90";
85 String backLayer = null;
86
87 String parameter = EditGeoServiceUtilities.getEditGeoServiceUrlParameterString(getDistributions(),
88 presenceAbsenceTermColors, 0, 0, null, "tdwg4");
89
90 return new URI(accessPoint + "?" + parameter + "&bbox=" + bbox + "&ms=" + width );
91 }
92
93 /**
94 * @return
95 */
96 private Set<Distribution> getDistributions() {
97 HashSet<Distribution> distributions = new HashSet<Distribution>();
98 TaxonDescription description = ((TaxonDescriptionFeature) page.input).getDescription();
99 for (DescriptionElementBase element : description.getElements()) {
100 if (element instanceof Distribution) {
101 distributions.add((Distribution) element);
102 }
103 }
104 return distributions;
105 }
106
107 }