1dccfcdcdbe6eca14383dc86744d062868aa9e2c
[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.HashSet;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Set;
20
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.widgets.Label;
23
24 import eu.etaxonomy.cdm.common.UriUtils;
25 import eu.etaxonomy.cdm.ext.geo.EditGeoServiceUtilities;
26 import eu.etaxonomy.cdm.model.common.Language;
27 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
28 import eu.etaxonomy.cdm.model.description.Distribution;
29 import eu.etaxonomy.cdm.model.description.PresenceAbsenceTermBase;
30 import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
31 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
32 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
33 import eu.etaxonomy.taxeditor.store.CdmStore;
34 import eu.etaxonomy.taxeditor.ui.forms.BrowserElement;
35 import eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory;
36 import eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement;
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.forms.CdmFormFactory} object.
54 * @param formElement a {@link eu.etaxonomy.taxeditor.ui.forms.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)){
74 image = formFactory.createBrowserElement(formElement, null, style);
75 String mapUriString = getMapUriString(getEntity());
76 image.setImageUriString(mapUriString);
77 formElement.getLayoutComposite().layout();
78 message = mapUriString;
79 return;
80 } else {
81 message = String.format("The service is not available: %s", serviceUriString);
82 }
83 } catch (URISyntaxException e) {
84 message = String.format("The URI has problems: %s", serviceUriString);
85 }
86
87 Label label = formFactory.createLabel(getLayoutComposite(), message, SWT.WRAP);
88 addControl(label);
89 label.setLayoutData(CdmFormFactory.FILL(2, 1));
90 }
91
92
93 /* (non-Javadoc)
94 * @see eu.etaxonomy.taxeditor.section.AbstractCdmDetailElement#handleEvent(java.lang.Object)
95 */
96 /** {@inheritDoc} */
97 @Override
98 public void handleEvent(Object eventSource) {
99 //
100 }
101
102 /**
103 * @return
104 * @throws URISyntaxException
105 */
106 private String getMapUriString(FeatureNodeContainer container){
107 String accessPoint = PreferencesUtil.getMapServiceAccessPoint();
108 Map<PresenceAbsenceTermBase<?>, java.awt.Color> presenceAbsenceTermColors = null;
109 // FIXME due to a bug in the rest map service we have to ensure that width will always be an even number
110 // image.calculateWidth() % 2 == 1 ? image.calculateWidth() + 1 :
111 int width = image.calculateWidth();
112
113 String bbox = "-180,-90,180,90";
114 String backLayer = null;
115
116 List<Language> languages = Arrays.asList(new Language[]{CdmStore.getDefaultLanguage()});
117
118 String parameter = EditGeoServiceUtilities.getDistributionServiceRequestParameterString(getDistributions(), presenceAbsenceTermColors, width, 0, bbox, backLayer, languages);
119
120
121 String mapUriString = accessPoint + "?" + parameter + "&ms=1000";
122 return mapUriString;
123 }
124
125 /**
126 * @return
127 */
128 private Set<Distribution> getDistributions() {
129 HashSet<Distribution> distributions = new HashSet<Distribution>();
130
131 for (DescriptionElementBase element : getEntity().getDescriptionElements()) {
132 if (element instanceof Distribution) {
133 distributions.add((Distribution) element);
134 }
135 }
136 return distributions;
137 }
138
139 }