Merge branch 'hotfix/5.43.1'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / feature / FeatureDistributionDetailElement.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.taxeditor.ui.section.feature;
11
12 import java.net.URISyntaxException;
13 import java.util.Arrays;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.UUID;
17
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.widgets.Label;
20
21 import eu.etaxonomy.cdm.api.service.geo.IDistributionService;
22 import eu.etaxonomy.cdm.common.URI;
23 import eu.etaxonomy.cdm.common.UriUtils;
24 import eu.etaxonomy.cdm.model.common.Language;
25 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
26 import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
27 import eu.etaxonomy.cdm.model.description.TaxonDescription;
28 import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
29 import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
30 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
31 import eu.etaxonomy.taxeditor.store.CdmStore;
32 import eu.etaxonomy.taxeditor.ui.element.BrowserElement;
33 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
34 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
35 import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
36 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
37
38 /**
39 * <p>FeatureDistributionDetailElement class.</p>
40 *
41 * @author n.hoffmann
42 * @created Sep 23, 2010
43 */
44 public class FeatureDistributionDetailElement extends AbstractCdmDetailElement<FeatureNodeContainer> {
45
46 private BrowserElement image;
47
48 /**
49 * <p>Constructor for FeatureDistributionDetailElement.</p>
50 *
51 * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
52 * @param formElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
53 */
54 public FeatureDistributionDetailElement(CdmFormFactory formFactory,
55 ICdmFormElement formElement) {
56 super(formFactory, formElement);
57 }
58
59 @Override
60 protected void createControls(ICdmFormElement formElement, FeatureNodeContainer entity,
61 int style) {
62 String serviceUriString = PreferencesUtil.getStringValue(PreferencePredicate.EditMapServiceAccessPoint.getKey());
63 String message = null;
64 try {
65 URI serviceUri = new URI(serviceUriString);
66 if(UriUtils.isServiceAvailable(serviceUri, 500)){
67 image = formFactory.createBrowserElement(formElement, null, style);
68 // FIXME : This is a temporary workaround which ic
69 // waiting for #5357 to be fixed
70 if(!isUnsavedDistribution(getEntity())) {
71 String mapUriString = getMapUriString(getEntity());
72 image.setImageUriString(mapUriString);
73 message = mapUriString;
74 }
75 formElement.getLayoutComposite().layout();
76
77 return;
78 } else {
79 message = String.format("The service is not available: %s", serviceUriString);
80 }
81 } catch (URISyntaxException e) {
82 message = String.format("The URI has problems: %s", serviceUriString);
83 }
84
85 Label label = formFactory.createLabel(getLayoutComposite(), message, SWT.WRAP);
86 addControl(label);
87 label.setLayoutData(LayoutConstants.FILL(2, 1));
88 }
89
90 @Override
91 public void handleEvent(Object eventSource) {
92 //
93 }
94
95 private String getMapUriString(FeatureNodeContainer container){
96
97 String accessPoint = PreferencesUtil.getMapServiceAccessPoint();
98 Map<UUID, java.awt.Color> presenceAbsenceTermColors = null;
99 // FIXME due to a bug in the rest map service we have to ensure that width will always be an even number
100 // image.calculateWidth() % 2 == 1 ? image.calculateWidth() + 1 :
101 int width = image.calculateWidth();
102
103 List<Language> languages = Arrays.asList(new Language[]{CdmStore.getDefaultLanguage()});
104 IDistributionService distributionService = CdmStore.getCurrentApplicationConfiguration().getDistributionService();
105 boolean includeUnpublished = true;
106
107 String parameter = distributionService.getDistributionServiceRequestParameterString(
108 getTaxonDescriptions(),
109 false,
110 false,
111 null,
112 presenceAbsenceTermColors,
113 languages,
114 includeUnpublished);
115
116 String mapUriString = String.format("%s?%s&ms=1000&bbox=-180,-90,180,90&l=earth", accessPoint, parameter);
117 return mapUriString;
118 }
119
120 private List<TaxonDescription> getTaxonDescriptions() {
121 return Arrays.asList(((TaxonDescription) getEntity().getDescription()).getTaxon().getDescriptions().toArray(new TaxonDescription[0]));
122 }
123
124 private boolean isUnsavedDistribution(FeatureNodeContainer container) {
125 for(DescriptionElementBase dist : container.getDescriptionElements()) {
126 if(dist.getId() == 0) {
127 return true;
128 }
129 }
130 return false;
131 }
132
133 @Override
134 public void fillFields() {
135 String serviceUriString = PreferencesUtil.getStringValue(PreferencePredicate.EditMapServiceAccessPoint.getKey());
136 String message = null;
137 try {
138 URI serviceUri = new URI(serviceUriString);
139 if(UriUtils.isServiceAvailable(serviceUri, 500)){
140 // image = formFactory.createBrowserElement(formElement, null, style);
141 // FIXME : This is a temporary workaround which ic
142 // waiting for #5357 to be fixed
143 if(!isUnsavedDistribution(getEntity())) {
144 String mapUriString = getMapUriString(getEntity());
145 image.setImageUriString(mapUriString);
146 message = mapUriString;
147 }
148 getParentElement().getLayoutComposite().layout();
149
150 return;
151 } else {
152 message = String.format("The service is not available: %s", serviceUriString);
153 }
154 } catch (URISyntaxException e) {
155 message = String.format("The URI has problems: %s", serviceUriString);
156 }
157
158 }
159
160 }