/**
   * Devuelve la lista de ficheros de paletas, si no existe el fichero devolvera una lista vacia
   *
   * @param palettesBasePath
   * @return
   */
  private static ArrayList getPaletteFileListDisc(String palettesBasePath) {
    updateVersion(palettesBasePath);

    File paletteFiles = new File(palettesBasePath);

    ArrayList fileList = new ArrayList();

    if (paletteFiles.exists()) {
      File[] list = paletteFiles.listFiles();
      for (int i = 0; i < list.length; i++) fileList.add(list[i].getName());
    }

    return fileList;
  }
  /**
   * Lee una paleta del fichero xml de paletas y la carga en la tabla del panel.
   *
   * @param palettesPath Camino al fichero de paletas predefinidas.
   * @param paletteName Nombre de paleta a cargar desde el fichero xml.
   * @return Nombre de la paleta
   */
  public static String loadPalette(
      String palettesBasePath, String paletteFileName, ArrayList items) {
    updateVersion(palettesBasePath);

    items.clear();

    File palettesFile = new File(palettesBasePath + File.separator + paletteFileName);
    if (!palettesFile.exists()) return null;

    try {
      String paletteName = "";
      ArrayList rows = new ArrayList();

      KXmlParser parser = new KXmlParser();
      FileInputStream in = new FileInputStream(palettesBasePath + File.separator + paletteFileName);

      InputStreamReader reader = new InputStreamReader(in, "UTF8");
      parser.setInput(reader);
      int tag = parser.nextTag();

      parser.require(KXmlParser.START_TAG, null, "ColorTable");
      for (int i = 0; i < parser.getAttributeCount(); i++) {
        if (parser.getAttributeName(i).equals("name")) {
          paletteName = parser.getAttributeValue(i);
        }
      }
      tag = parser.nextTag();

      while (!((tag == KXmlParser.END_TAG) && (parser.getName().equals("ColorTable")))) {
        try {
          if (tag == KXmlParser.START_TAG) {
            if (parser.getName().equals("Color")) {
              ColorItem colorItem = new ColorItem();
              int a = 255;
              for (int i = 0; i < parser.getAttributeCount(); i++) {
                if (parser.getAttributeName(i).equals("value")) {
                  colorItem.setValue(Double.parseDouble((String) parser.getAttributeValue(i)));
                  ColorItem aux =
                      getColorItem(rows, Double.parseDouble((String) parser.getAttributeValue(i)));
                  if (aux != null) a = aux.getColor().getAlpha();
                }
                if (parser.getAttributeName(i).equals("name")) {
                  colorItem.setNameClass((String) parser.getAttributeValue(i));
                }
                if (parser.getAttributeName(i).equals("rgb")) {
                  String rgb = parser.getAttributeValue(i);
                  int r = Integer.valueOf(rgb.substring(0, rgb.indexOf(","))).intValue();
                  int g =
                      Integer.valueOf(rgb.substring(rgb.indexOf(",") + 1, rgb.lastIndexOf(",")))
                          .intValue();
                  int b =
                      Integer.valueOf(rgb.substring(rgb.lastIndexOf(",") + 1, rgb.length()))
                          .intValue();

                  colorItem.setColor(new Color(r, g, b, a));
                }
                if (parser.getAttributeName(i).equals("interpolated")) {
                  colorItem.setInterpolated(
                      Double.parseDouble((String) parser.getAttributeValue(i)));
                }
              }

              rows.add(colorItem);
              continue;
            }
            if (parser.getName().equals("Alpha")) {
              ColorItem colorItem = new ColorItem();
              for (int i = 0; i < parser.getAttributeCount(); i++) {
                if (parser.getAttributeName(i).equals("value")) {
                  colorItem.setValue(Double.parseDouble((String) parser.getAttributeValue(i)));
                  ColorItem aux =
                      getColorItem(rows, Double.parseDouble((String) parser.getAttributeValue(i)));
                  if (aux != null) {
                    colorItem.setNameClass(aux.getNameClass());
                    colorItem.setInterpolated(aux.getInterpolated());
                    colorItem.setColor(
                        new Color(
                            aux.getColor().getRed(),
                            aux.getColor().getGreen(),
                            aux.getColor().getBlue(),
                            colorItem.getColor().getAlpha()));
                  }
                }
                if (parser.getAttributeName(i).equals("alpha")) {
                  int a = Integer.parseInt(parser.getAttributeValue(i));

                  colorItem.setColor(
                      new Color(
                          colorItem.getColor().getRed(),
                          colorItem.getColor().getGreen(),
                          colorItem.getColor().getBlue(),
                          a));
                }
                if (parser.getAttributeName(i).equals("interpolated")) {
                  colorItem.setInterpolated(
                      Double.parseDouble((String) parser.getAttributeValue(i)));
                }
              }

              rows.add(colorItem);
              continue;
            }
          }
        } finally {
          tag = parser.nextTag();
        }
      }

      for (int i = 0; i < rows.size(); i++) items.add(rows.get(i));

      reader.close();
      return paletteName;
    } catch (FileNotFoundException fnfEx) {
      fnfEx.printStackTrace();
    } catch (XmlPullParserException xmlEx) {
      System.out.println(
          "El fichero de paletas predeterminadas no tiene la estructura correcta:\n	"
              + xmlEx.getMessage());
    } catch (IOException e) {
    }
    return null;
  }