Beispiel #1
0
  /**
   * Devuelve la tabla de color creada segun los parametros especificados en el panel.
   *
   * @return
   */
  public ColorTable getColorTable() {
    ColorTable colorTable = new ColorTable();
    colorTable.setName(getLibraryPanel().getLibraryName().getText());
    ArrayList items = new ArrayList();

    StretchSelectorData stretchListData = getLibraryPanel().getStretchListPanel().getData();
    double start = stretchListData.getMinimum();
    double end = stretchListData.getMaximum();

    if (start > end) {
      double aux = start;
      start = end;
      end = aux;
    }

    double num = getLibraryPanel().getStretchListPanel().getInterval();
    boolean quest = false;
    if (getLibraryPanel().getStretchListPanel().getIntervalNumber().isSelected())
      quest = (num > limit_question);
    else quest = (((end - start) / num) > limit_question);
    if (quest
        && JOptionPane.showConfirmDialog(
                this,
                PluginServices.getText(this, "addlibrary_supera_limite"),
                PluginServices.getText(this, "confirmar"),
                JOptionPane.YES_NO_OPTION)
            == NO_OPTION) return null;
    if (getLibraryPanel().getStretchListPanel().getIntervalNumber().isSelected()) {
      for (double i = 0; i <= num; i++) {
        ColorItem colorItem = new ColorItem();
        colorItem.setValue(start + (((end - start) / num) * i));
        items.add(colorItem);
      }
    } else {
      for (double i = start; i <= end; i += num) {
        ColorItem colorItem = new ColorItem();
        colorItem.setValue(i);
        items.add(colorItem);
      }
    }

    colorTable.createPaletteFromColorItems(items, false);
    return colorTable;
  }
  /**
   * 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;
  }
  /**
   * Crea los ficheros que forman la paleta de color de la version 1.1 a traves de un XML que se le
   * pasa por parametro
   *
   * @param palettesPath
   */
  private static void createVersionFromXML(String palettesBasePath, String xml) {
    new File(palettesBasePath).mkdir();
    KXmlParser parser = new KXmlParser();
    StringReader reader = new StringReader(xml);
    try {
      parser.setInput(reader);
      int tag = parser.nextTag();

      parser.require(KXmlParser.START_TAG, null, "palettes");
      tag = parser.nextTag();
      parser.require(KXmlParser.START_TAG, null, "palette_list");
      parser.skipSubTree();
      parser.require(KXmlParser.END_TAG, null, "palette_list");
      tag = parser.nextTag();

      while (tag == KXmlParser.START_TAG) {
        parser.require(KXmlParser.START_TAG, null, "palette");
        if (parser.getAttributeCount() == 2) {
          // Generar nuevo fichero
          KXmlSerializer parserOutput = new KXmlSerializer();
          FileOutputStream fileOutputStream =
              new FileOutputStream(
                  palettesBasePath + File.separator + parser.getAttributeValue(0) + ".xml");

          parserOutput.setOutput(fileOutputStream, null);
          parserOutput.startDocument("UTF-8", null);
          parserOutput.startTag(null, "ColorTable");
          parserOutput.attribute(null, "name", parser.getAttributeValue(0));
          parserOutput.attribute(null, "version", "1.1");

          tag = parser.nextTag();
          parser.require(KXmlParser.START_TAG, null, "table");
          tag = parser.nextTag();

          parserOutput.text("\n");
          ArrayList items = new ArrayList();
          while (tag == KXmlParser.START_TAG) {
            parser.require(KXmlParser.START_TAG, null, "entry");
            if (parser.getAttributeCount() == 3) {
              String rgb =
                  parser
                      .getAttributeValue(1)
                      .substring(
                          parser.getAttributeValue(1).indexOf(",") + 1,
                          parser.getAttributeValue(1).length());

              int a =
                  Integer.valueOf(
                          parser
                              .getAttributeValue(1)
                              .substring(0, parser.getAttributeValue(1).indexOf(",")))
                      .intValue();
              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 colorItem = new ColorItem();
              colorItem.setColor(new Color(r, g, b, a));
              colorItem.setInterpolated(50);
              colorItem.setNameClass(parser.getAttributeValue(0));
              colorItem.setValue(Double.parseDouble(parser.getAttributeValue(2)));
              items.add(colorItem);
            }
            tag = parser.nextTag();
            parser.require(KXmlParser.END_TAG, null, "entry");
            tag = parser.nextTag();
          }
          parser.require(KXmlParser.END_TAG, null, "table");
          tag = parser.nextTag();

          ColorTable colorTable = new ColorTable();
          colorTable.createPaletteFromColorItems(items, true);
          items = colorTable.getColorItems();
          for (int i = 0; i < items.size(); i++) {
            ColorItem colorItem = (ColorItem) items.get(i);
            parserOutput.startTag(null, "Color");
            parserOutput.attribute(null, "value", String.valueOf(colorItem.getValue()));
            parserOutput.attribute(null, "name", String.valueOf(colorItem.getNameClass()));
            Color color = colorItem.getColor();
            parserOutput.attribute(
                null,
                "rgb",
                String.valueOf(color.getRed() + "," + color.getGreen() + "," + color.getBlue()));
            parserOutput.attribute(
                null, "interpolated", String.valueOf(colorItem.getInterpolated()));
            parserOutput.endTag(null, "Color");
            parserOutput.text("\n");
          }

          for (int i = 0; i < items.size(); i++) {
            ColorItem colorItem = (ColorItem) items.get(i);
            parserOutput.startTag(null, "Alpha");
            parserOutput.attribute(null, "value", String.valueOf(colorItem.getValue()));
            parserOutput.attribute(null, "alpha", String.valueOf(colorItem.getColor().getAlpha()));
            parserOutput.attribute(
                null, "interpolated", String.valueOf(colorItem.getInterpolated()));
            parserOutput.endTag(null, "Alpha");
            parserOutput.text("\n");
          }

          parserOutput.endTag(null, "ColorTable");
          parserOutput.text("\n");
          parserOutput.endDocument();
          // Cerrar nuevo fichero
          fileOutputStream.close();
        }
        parser.require(KXmlParser.END_TAG, null, "palette");
        tag = parser.nextTag();
      }
      parser.require(KXmlParser.END_TAG, null, "palettes");
    } catch (XmlPullParserException xmlEx) {
      System.out.println(
          "El fichero de paletas predeterminadas no tiene la estructura correcta:\n	"
              + xmlEx.getMessage());
    } catch (IOException e) {
    }
  }