ref #10138: after parsing send event to update the details view
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / feature / FeatureDistributionDetailElement.java
index 30aa76de0429418b90d227a7a009f92036a9f705..26f4b62d6372e8d2f24875caf968cce9b3c6ce89 100644 (file)
@@ -1,4 +1,3 @@
-// $Id$
 /**
 * Copyright (C) 2007 EDIT
 * European Distributed Institute of Taxonomy
@@ -10,7 +9,6 @@
 
 package eu.etaxonomy.taxeditor.ui.section.feature;
 
-import java.net.URI;
 import java.net.URISyntaxException;
 import java.util.Arrays;
 import java.util.List;
@@ -19,12 +17,16 @@ import java.util.Map;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.Label;
 
+import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteController;
+import eu.etaxonomy.cdm.common.URI;
 import eu.etaxonomy.cdm.common.UriUtils;
+import eu.etaxonomy.cdm.ext.geo.IEditGeoService;
 import eu.etaxonomy.cdm.model.common.Language;
+import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
 import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
 import eu.etaxonomy.cdm.model.description.TaxonDescription;
+import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
 import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
-import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
 import eu.etaxonomy.taxeditor.store.CdmStore;
 import eu.etaxonomy.taxeditor.ui.element.BrowserElement;
@@ -38,7 +40,6 @@ import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
  *
  * @author n.hoffmann
  * @created Sep 23, 2010
- * @version 1.0
  */
 public class FeatureDistributionDetailElement extends AbstractCdmDetailElement<FeatureNodeContainer> {
 
@@ -55,24 +56,24 @@ public class FeatureDistributionDetailElement extends AbstractCdmDetailElement<F
                super(formFactory, formElement);
        }
 
-
-       /* (non-Javadoc)
-        * @see eu.etaxonomy.taxeditor.section.AbstractCdmDetailElement#createControls(eu.etaxonomy.taxeditor.forms.ICdmFormElement, eu.etaxonomy.cdm.model.common.AnnotatableEntity, int)
-        */
-       /** {@inheritDoc} */
        @Override
        protected void createControls(ICdmFormElement formElement, FeatureNodeContainer entity,
                        int style) {
-               String serviceUriString = PreferencesUtil.getPreferenceStore().getString(IPreferenceKeys.EDIT_MAP_SERVICE_ACCES_POINT);
+               String serviceUriString = PreferencesUtil.getStringValue(PreferencePredicate.EditMapServiceAccessPoint.getKey());
                String message = null;
                try {
                        URI serviceUri = new URI(serviceUriString);
                        if(UriUtils.isServiceAvailable(serviceUri, 500)){
                                image = formFactory.createBrowserElement(formElement, null, style);
-                               String mapUriString = getMapUriString(getEntity());
-                               image.setImageUriString(mapUriString);
+                               // FIXME : This is a temporary workaround which ic
+                               //         waiting for #5357 to be fixed
+                               if(!isUnsavedDistribution(getEntity())) {
+                                   String mapUriString = getMapUriString(getEntity());
+                                   image.setImageUriString(mapUriString);
+                                   message = mapUriString;
+                               }
                                formElement.getLayoutComposite().layout();
-                               message = mapUriString;
+
                                return;
                        } else {
                                message = String.format("The service is not available: %s", serviceUriString);
@@ -86,41 +87,71 @@ public class FeatureDistributionDetailElement extends AbstractCdmDetailElement<F
                label.setLayoutData(LayoutConstants.FILL(2, 1));
        }
 
-
-       /* (non-Javadoc)
-        * @see eu.etaxonomy.taxeditor.section.AbstractCdmDetailElement#handleEvent(java.lang.Object)
-        */
-       /** {@inheritDoc} */
        @Override
        public void handleEvent(Object eventSource) {
                //
        }
 
-       /**
-        * @return
-        * @throws URISyntaxException
-        */
        private String getMapUriString(FeatureNodeContainer container){
                String accessPoint = PreferencesUtil.getMapServiceAccessPoint();
                Map<PresenceAbsenceTerm, java.awt.Color> presenceAbsenceTermColors = null;
                // FIXME due to a bug in the rest map service we have to ensure that width will always be an even number
                // image.calculateWidth() % 2 == 1 ? image.calculateWidth() + 1 :
                int width = image.calculateWidth();
-
+                IEditGeoService editGeoService;
                List<Language> languages = Arrays.asList(new Language[]{CdmStore.getDefaultLanguage()});
+               editGeoService = ((CdmApplicationRemoteController)CdmStore.getCurrentApplicationConfiguration()).getEditGeoService();
 
-               String parameter = CdmStore.getGeoService().getDistributionServiceRequestParameterString(getTaxonDescriptions(),
-                false, false, null, presenceAbsenceTermColors, languages);
+               String parameter = editGeoService.getDistributionServiceRequestParameterString(
+                               getTaxonDescriptions(),
+                false,
+                false,
+                null,
+                presenceAbsenceTermColors,
+                languages);
 
                String mapUriString = String.format("%s?%s&ms=1000&bbox=-180,-90,180,90&l=earth", accessPoint, parameter);
                return mapUriString;
        }
 
-       /**
-        * @return
-        */
        private List<TaxonDescription> getTaxonDescriptions() {
                return Arrays.asList(((TaxonDescription) getEntity().getDescription()).getTaxon().getDescriptions().toArray(new TaxonDescription[0]));
        }
 
+       private boolean isUnsavedDistribution(FeatureNodeContainer container) {
+           for(DescriptionElementBase dist : container.getDescriptionElements()) {
+               if(dist.getId() == 0) {
+                   return true;
+               }
+           }
+           return false;
+       }
+
+       @Override
+       public void fillFields() {
+               String serviceUriString = PreferencesUtil.getStringValue(PreferencePredicate.EditMapServiceAccessPoint.getKey());
+               String message = null;
+               try {
+                       URI serviceUri = new URI(serviceUriString);
+                       if(UriUtils.isServiceAvailable(serviceUri, 500)){
+//                             image = formFactory.createBrowserElement(formElement, null, style);
+                               // FIXME : This is a temporary workaround which ic
+                               //         waiting for #5357 to be fixed
+                               if(!isUnsavedDistribution(getEntity())) {
+                                   String mapUriString = getMapUriString(getEntity());
+                                   image.setImageUriString(mapUriString);
+                                   message = mapUriString;
+                               }
+                               getParentElement().getLayoutComposite().layout();
+
+                               return;
+                       } else {
+                               message = String.format("The service is not available: %s", serviceUriString);
+                       }
+               } catch (URISyntaxException e) {
+                       message = String.format("The URI has problems: %s", serviceUriString);
+               }
+               
+       }
+
 }