/*
   * (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();
      }
    }
  }
  /** Que acciones se ejecutaran al haber presionado el botón aceptar o aplicar */
  public void accept() {
    IRasterDataSource raster = ((FLyrRasterSE) getFilterPanel().getLayer()).getDataSource();
    if (raster == null) return;

    String path = null;
    if (!getFilterPanel().getNewLayerPanel().isOnlyViewSelected()) {
      path = getFilterPanel().getNewLayerPanel().getFileSelected();
      if (path == null) return;
    }

    // Rendering rendering = ((FLyrRasterSE)
    // getFilterPanel().getLayer()).getRender();
    IRasterRendering rendering = (IRasterRendering) getFilterPanel().getLayer();

    // Array para guardar los filtros que se van a usar en forma de
    // ParamStruct
    ArrayList listFilterUsed = applyFilters(rendering);

    if (filterPanel.getNewLayerPanel().isOnlyViewSelected()) {
      try {
        FilterProcess.addSelectedFilters(rendering.getRenderFilterList(), listFilterUsed);
        ((FLyrRasterSE) getFilterPanel().getLayer())
            .setRenderFilterList(rendering.getRenderFilterList());
        getFilterPanel().getLayer().getMapContext().invalidate();
      } catch (FilterTypeException e) {
        RasterToolsUtil.messageBoxError(
            PluginServices.getText(this, "error_adding_filters"), this, e);
      }
    } else {
      FilterProcess filterProcess = new FilterProcess();
      filterProcess.setActions(this);
      filterProcess.addParam("rendering", rendering);
      filterProcess.addParam("filename", path);
      filterProcess.addParam("rasterdatasource", raster);
      filterProcess.addParam("layer", ((FLyrRasterSE) getFilterPanel().getLayer()));
      filterProcess.addParam("listfilterused", listFilterUsed);
      filterProcess.start();
    }
  }
  /**
   * 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;
  }