/**
   * Returns a mapgraphics layer by its class.
   *
   * @param theClass the class of the mapgraphics.
   * @return the layer or null, if none was found (not visible or not existing).
   */
  public static ILayer getMapgraphicLayerByClass(Class<?> theClass) {

    try {
      List<IResolve> mapgraphics =
          CatalogPlugin.getDefault().getLocalCatalog().find(MapGraphicService.SERVICE_URL, null);
      List<IResolve> members = mapgraphics.get(0).members(null);
      for (IResolve resolve : members) {
        if (resolve.canResolve(theClass)) {
          IGeoResource resolve2 = resolve.resolve(IGeoResource.class, null);
          String resName = resolve2.getInfo(new NullProgressMonitor()).getName();

          List<ILayer> mapLayers = ApplicationGIS.getActiveMap().getMapLayers();
          for (ILayer layer : mapLayers) {
            if (layer.getName().trim().equals(resName.trim())) {
              return layer;
            }
          }
        }
      }
    } catch (IOException e) {
      JGrassPlugin.log(
          "JGrassPlugin problem: eu.hydrologis.udig.catalog.utils#JGrassCatalogUtilities#getMapgraphicLayerByClass",
          e); //$NON-NLS-1$
      e.printStackTrace();
      return null;
    }
    return null;
  }
  private MemoryServiceImpl getMemoryService() {
    // Make sure the memory service is in the catalog
    MemoryServiceImpl service = null;

    try {
      List<? extends IResolve> members =
          CatalogPlugin.getDefault().getLocalCatalog().members(new NullProgressMonitor());
      for (IResolve resolve : members) {
        if (resolve instanceof MemoryServiceImpl) {
          if (URLUtils.urlEquals(resolve.getIdentifier(), MemoryServiceExtensionImpl.URL, true)) {
            service = (MemoryServiceImpl) resolve;
            break;
          }
        }
      }
    } catch (IOException e) {
      CatalogPlugin.log("Error finding services", e); // $NON-NLS-1$
    }

    if (service == null) {
      service = new MemoryServiceImpl(MemoryServiceExtensionImpl.URL);
      CatalogPlugin.getDefault().getLocalCatalog().add(service);
    }
    return service;
  }
  /**
   * Find a resource from its url and add it to the map
   *
   * @param url the resources url
   * @param progressMonitor
   * @param addPosition the position into which to put it, 0 is most lower
   * @param map the map to which to add the resource to
   */
  public static void addResourceFromUrlToMap(
      URL url, IProgressMonitor progressMonitor, int addPosition, IMap map) {

    synchronized (url) {
      ICatalog catalog = CatalogPlugin.getDefault().getLocalCatalog();
      // first search the local catalog.
      List<IResolve> matches = catalog.find(url, new SubProgressMonitor(progressMonitor, 2));

      for (IResolve resolve : matches) {
        List<IResolve> members = null;
        try {
          List<IGeoResource> geoResources = new ArrayList<IGeoResource>();
          members = resolve.members(progressMonitor);
          if (members.size() < 1 && resolve.canResolve(IGeoResource.class)) {
            // if it is a map, it has no members
            geoResources.add(resolve.resolve(IGeoResource.class, progressMonitor));
          } else if (members.get(0).canResolve(IGeoResource.class)) {
            for (IResolve tmp : members) {
              IGeoResource finalResolve = tmp.resolve(IGeoResource.class, progressMonitor);
              geoResources.add(finalResolve);
            }
          }
          List<? extends ILayer> addedLayers =
              ApplicationGIS.addLayersToMap(map, geoResources, addPosition);
          if (addedLayers.size() == 0) {
            System.out.println("strange");
          }
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
 /**
  * Remove a memory datastore from the catalog, since the update is not possible (Caused by:
  * java.lang.UnsupportedOperationException: Schema modification not supported)
  *
  * @param typeName the name of the type to remove, if it is there
  */
 public static synchronized void removeMemoryServiceByTypeName(String typeName) {
   MemoryServiceImpl service = null;
   try {
     List<? extends IResolve> members =
         CatalogPlugin.getDefault().getLocalCatalog().members(new NullProgressMonitor());
     for (IResolve resolve : members) {
       if (resolve instanceof MemoryServiceImpl) {
         if (URLUtils.urlEquals(resolve.getIdentifier(), MemoryServiceExtensionImpl.URL, true)) {
           service = (MemoryServiceImpl) resolve;
           break;
         }
       }
     }
     if (service == null) return;
     MemoryDataStore ds = service.resolve(MemoryDataStore.class, new NullProgressMonitor());
     if (Arrays.asList(ds.getTypeNames()).contains(typeName)) {
       CatalogPlugin.getDefault().getLocalCatalog().remove(service);
     }
   } catch (IOException e) {
     CatalogPlugin.log("Error finding services", e); // $NON-NLS-1$
   }
 }
  /**
   * Generate text from the resolve.getURI()
   *
   * <p>Note this name is only used as a first try, the ResolveLabelDecorator is expected to provide
   * a label based on Name or Title information.
   *
   * @param element
   * @return label based on IResolve.getIdentifier
   */
  public String getText(Object element) {
    if (element instanceof IResolve) {
      IResolve resolve = (IResolve) element;
      try {
        if (resolve instanceof IGeoResource) {
          IGeoResource resource = (IGeoResource) resolve;
          String title = resource.getTitle();
          // This provider should be non-blocking
          //                    if (title == null) {
          //                        IGeoResourceInfo info = resource.getInfo(new
          // NullProgressMonitor());
          //                        if(info != null) {
          //                        	title = info.getTitle();
          //                        }
          //                    }
          ID id = resource.getID();
          if (title == null) {
            title = id.labelResource();
          }
          return title;

        } else if (resolve instanceof IService) {
          IService service = (IService) resolve;
          ID id = service.getID();

          String title = service.getTitle();
          //                    if (title == null) {
          //                        IServiceInfo info = service.getInfo(new NullProgressMonitor());
          //                        if (info != null) {
          //                            title = info.getTitle();
          //                        }
          //                    }
          if (title == null) {
            // we are going to fake something here
            String name = id.toString();
            name = name.replace('_', ' ');
            name = name.replace("%20", " "); // $NON-NLS-1$ //$NON-NLS-2$
            return name;
          }
          if (id.getTypeQualifier() != null) {
            return title + "(" + id.getTypeQualifier() + ")";
          } else {
            return title;
          }
        } else if (resolve instanceof IProcess) {
          IProcess proc = (IProcess) element;
          return proc.getInfo(new NullProgressMonitor()).getTitle();
        } else if (resolve instanceof ISearch) {
          ISearch search = (ISearch) element;
          return search.getInfo(new NullProgressMonitor()).getTitle();
        } else if (resolve instanceof IResolveFolder) {
          IResolveFolder folder = (IResolveFolder) element;
          return folder.getID().toString();
        } else {
          return resolve.getID().toString();
        }
      } catch (IOException e) {
        CatalogUIPlugin.trace("Error fetching the Title for the resource", e); // $NON-NLS-1$
      }
    }
    return super.getText(element);
  }