/*
   * (non-Javadoc)
   *
   * @see
   * org.gvsig.gui.beans.imagenavigator.IClientImageNavigator#drawImage(java
   * .awt.Graphics2D, double, double, double, double, double, int, int)
   */
  public void drawImage(IRasterRendering rendering) {
    rendering.getRenderFilterList().clear();

    if (getFilterPanel().getCBShowFilters().isSelected()) {
      RasterFilterList filterList = rendering.getRenderFilterList();
      RasterFilterListManager stackManager = new RasterFilterListManager(filterList);

      ArrayList listFilterUsed = applyFilters(rendering);
      ArrayList exc = new ArrayList();
      for (int i = 0; i < listFilterUsed.size(); i++) {
        IRasterFilterListManager filterManager =
            stackManager.getManagerByFilterClass(
                ((ParamStruct) listFilterUsed.get(i)).getFilterClass());
        try {
          filterManager.addFilter(
              ((ParamStruct) listFilterUsed.get(i)).getFilterClass(),
              ((ParamStruct) listFilterUsed.get(i)).getFilterParam());
        } catch (FilterTypeException e) {
          exc.add(e);
        }
      }
      if (exc.size() != 0) {
        RasterToolsUtil.messageBoxError(
            PluginServices.getText(this, "error_adding_filters"), this, exc);
        exc.clear();
      }
    }
  }
  /**
   * Constructor
   *
   * @param filterStack
   */
  public RasterFilterListManager(RasterFilterList filterStack) {
    this.rasterFilterList = filterStack;

    // Cargamos el manager con los gestores de drivers registrados
    ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
    ExtensionPoint point = extensionPoints.get("RasterFilter");
    Iterator iterator = point.iterator();
    while (iterator.hasNext()) {
      ExtensionPoint.Extension entry = (ExtensionPoint.Extension) iterator.next();
      if (entry != null) {
        Class RasterClass = entry.getExtension();
        Object obj = RasterFilterListManager.loadClass(RasterClass, this);
        if (obj != null) managers.add(obj);
      }
    }
  }
  /**
   * Aqui se seleccionan que filtros se van a aplicar y se devuelven en forma de ArrayList tanto
   * para el dibujado como cuando aceptan o aplican el panel.
   *
   * @param rendering
   * @return
   */
  public ArrayList applyFilters(IRasterRendering rendering) {
    ArrayList listFilterUsed = new ArrayList();

    RasterFilterList filterList = new RasterFilterList();
    filterList.setEnv(rendering.getRenderFilterList().getEnv());
    RasterFilterListManager stackManager = new RasterFilterListManager(filterList);

    // Conservamos filtros no visibles ya existentes
    ArrayList filtersInit = getFilterPanel().getLayerVisualStatus().getLast().getFilterStatus();
    for (int i = 0; i < filtersInit.size(); i++) {
      // Si es visible no hacemos nada
      if (((RasterFilter) filtersInit.get(i)).isVisible()) continue;

      RasterFilter obj = null;
      for (int j = 0; j < stackManager.getRasterFilterList().size(); j++) {
        Class classFilter = (Class) stackManager.getRasterFilterList().get(j);
        try {
          obj = (RasterFilter) classFilter.newInstance();
          if (obj.getName().equals(((RasterFilter) filtersInit.get(i)).getName())) break;
        } catch (InstantiationException e) {
          RasterToolsUtil.messageBoxError("error_creando_filtro", this, e);
        } catch (IllegalAccessException e) {
          RasterToolsUtil.messageBoxError("error_creando_filtro", this, e);
        }
      }

      // Si no encontramos el filtro apropiado, nos olvidamos de el
      if (obj == null) continue;

      // Si no es visible tenemos que conservar el filtro
      try {
        Params params =
            (Params)
                ((RasterFilter) filtersInit.get(i))
                    .getUIParams(((RasterFilter) filtersInit.get(i)).getName())
                    .clone();
        // Añado el parametro RenderBands a los parametros del filtro
        String rgb =
            rendering.getRenderBands()[0]
                + " "
                + rendering.getRenderBands()[1]
                + " "
                + rendering.getRenderBands()[2];
        params.setParam("RenderBands", rgb, 0, null);
        params.setParam("alphaBand", new Integer(rendering.getAlphaBandNumber()), 0, null);

        ParamStruct newParam = new ParamStruct();
        newParam.setFilterClass(obj.getClass());
        newParam.setFilterName(((RasterFilter) filtersInit.get(i)).getName());
        newParam.setFilterParam(params);
        listFilterUsed.add(newParam);
      } catch (CloneNotSupportedException e) {
      }
    }

    // Metemos los filtros seleccionados en el panel
    ListModel list = getFilterPanel().getMainPanel().getTreeListContainer().getListModel();
    for (int i = 0; i < list.getSize(); i++) {
      Hashtable hastTable = getFilterPanel().getMainPanel().getTreeListContainer().getMap();
      for (int j = 0; j < paramsList.size(); j++) {
        boolean active = true;
        Param param = ((ParamStruct) paramsList.get(j)).getFilterParam().getParamById("enabled");
        if ((param != null)
            && param.defaultValue instanceof Boolean
            && ((((Boolean) param.defaultValue).booleanValue()) == false)) active = false;
        if (active) {
          if (((ParamStruct) paramsList.get(j))
              .getFilterName()
              .equals(hastTable.get(list.getElementAt(i)))) {
            try {
              Params params = (Params) ((ParamStruct) paramsList.get(j)).getFilterParam().clone();
              // Añado el parametro RenderBands a los parametros
              // del filtro
              String rgb =
                  rendering.getRenderBands()[0]
                      + " "
                      + rendering.getRenderBands()[1]
                      + " "
                      + rendering.getRenderBands()[2];
              params.setParam("RenderBands", rgb, 0, null);
              params.setParam("alphaBand", new Integer(rendering.getAlphaBandNumber()), 0, null);

              ParamStruct newParam = new ParamStruct();
              newParam.setFilterClass(((ParamStruct) paramsList.get(j)).getFilterClass());
              newParam.setFilterName(((ParamStruct) paramsList.get(j)).getFilterName());
              newParam.setFilterParam(params);
              listFilterUsed.add(newParam);
            } catch (CloneNotSupportedException e) {
            }
          }
        }
      }
    }
    return listFilterUsed;
  }