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