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