/**
   * 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;
  }
  /**
   * 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();
        }
      }
    }
  }