ref #6190 removing svn property place holder in first line of code - java files
[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.URI;
13 import java.net.URISyntaxException;
14 import java.util.Arrays;
15 import java.util.List;
16 import java.util.Map;
17
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.widgets.Label;
20
21 import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteController;
22 import eu.etaxonomy.cdm.common.UriUtils;
23 import eu.etaxonomy.cdm.ext.geo.IEditGeoService;
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.taxeditor.model.FeatureNodeContainer;
29 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
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 * @version 1.0
44 */
45 public class FeatureDistributionDetailElement extends AbstractCdmDetailElement<FeatureNodeContainer> {
46
47 private BrowserElement image;
48
49 /**
50 * <p>Constructor for FeatureDistributionDetailElement.</p>
51 *
52 * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
53 * @param formElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
54 */
55 public FeatureDistributionDetailElement(CdmFormFactory formFactory,
56 ICdmFormElement formElement) {
57 super(formFactory, formElement);
58 }
59
60
61 /* (non-Javadoc)
62 * @see eu.etaxonomy.taxeditor.section.AbstractCdmDetailElement#createControls(eu.etaxonomy.taxeditor.forms.ICdmFormElement, eu.etaxonomy.cdm.model.common.AnnotatableEntity, int)
63 */
64 /** {@inheritDoc} */
65 @Override
66 protected void createControls(ICdmFormElement formElement, FeatureNodeContainer entity,
67 int style) {
68 String serviceUriString = PreferencesUtil.getPreferenceStore().getString(IPreferenceKeys.EDIT_MAP_SERVICE_ACCES_POINT);
69 String message = null;
70 try {
71 URI serviceUri = new URI(serviceUriString);
72 if(UriUtils.isServiceAvailable(serviceUri, 500)){
73 image = formFactory.createBrowserElement(formElement, null, style);
74 // FIXME : This is a temporary workaround which ic
75 // waiting for #5357 to be fixed
76 if(!isUnsavedDistribution(getEntity())) {
77 String mapUriString = getMapUriString(getEntity());
78 image.setImageUriString(mapUriString);
79 message = mapUriString;
80 }
81 formElement.getLayoutComposite().layout();
82
83 return;
84 } else {
85 message = String.format("The service is not available: %s", serviceUriString);
86 }
87 } catch (URISyntaxException e) {
88 message = String.format("The URI has problems: %s", serviceUriString);
89 }
90
91 Label label = formFactory.createLabel(getLayoutComposite(), message, SWT.WRAP);
92 addControl(label);
93 label.setLayoutData(LayoutConstants.FILL(2, 1));
94 }
95
96
97 /* (non-Javadoc)
98 * @see eu.etaxonomy.taxeditor.section.AbstractCdmDetailElement#handleEvent(java.lang.Object)
99 */
100 /** {@inheritDoc} */
101 @Override
102 public void handleEvent(Object eventSource) {
103 //
104 }
105
106 /**
107 * @return
108 * @throws URISyntaxException
109 */
110 private String getMapUriString(FeatureNodeContainer container){
111 String accessPoint = PreferencesUtil.getMapServiceAccessPoint();
112 Map<PresenceAbsenceTerm, java.awt.Color> presenceAbsenceTermColors = null;
113 // FIXME due to a bug in the rest map service we have to ensure that width will always be an even number
114 // image.calculateWidth() % 2 == 1 ? image.calculateWidth() + 1 :
115 int width = image.calculateWidth();
116 IEditGeoService editGeoService;
117 List<Language> languages = Arrays.asList(new Language[]{CdmStore.getDefaultLanguage()});
118 editGeoService = ((CdmApplicationRemoteController)CdmStore.getCurrentApplicationConfiguration()).getEditGeoService();
119
120 String parameter = editGeoService.getDistributionServiceRequestParameterString(
121 getTaxonDescriptions(),
122 false,
123 false,
124 null,
125 presenceAbsenceTermColors,
126 languages);
127
128 String mapUriString = String.format("%s?%s&ms=1000&bbox=-180,-90,180,90&l=earth", accessPoint, parameter);
129 return mapUriString;
130 }
131
132 /**
133 * @return
134 */
135 private List<TaxonDescription> getTaxonDescriptions() {
136 return Arrays.asList(((TaxonDescription) getEntity().getDescription()).getTaxon().getDescriptions().toArray(new TaxonDescription[0]));
137 }
138
139 private boolean isUnsavedDistribution(FeatureNodeContainer container) {
140 for(DescriptionElementBase dist : container.getDescriptionElements()) {
141 if(dist.getId() == 0) {
142 return true;
143 }
144 }
145 return false;
146 }
147
148 }