@Override
 public RenderTarget buildRenderTarget(PreviewModel previewModel) {
   double width = previewModel.getDimensions().getWidth();
   double height = previewModel.getDimensions().getHeight();
   width = Math.max(1, width);
   height = Math.max(1, height);
   int topLeftX = previewModel.getTopLeftPosition().x;
   int topLeftY = previewModel.getTopLeftPosition().y;
   PreviewProperties properties = previewModel.getProperties();
   float marginBottom = properties.getFloatValue(PDFTarget.MARGIN_BOTTOM);
   float marginLeft = properties.getFloatValue(PDFTarget.MARGIN_LEFT);
   float marginRight = properties.getFloatValue(PDFTarget.MARGIN_RIGHT);
   float marginTop = properties.getFloatValue(PDFTarget.MARGIN_TOP);
   Rectangle pageSize = properties.getValue(PDFTarget.PAGESIZE);
   boolean landscape = properties.getBooleanValue(PDFTarget.LANDSCAPE);
   PdfContentByte cb = properties.getValue(PDFTarget.PDF_CONTENT_BYTE);
   PDFRenderTargetImpl renderTarget =
       new PDFRenderTargetImpl(
           cb,
           width,
           height,
           topLeftX,
           topLeftY,
           pageSize,
           marginLeft,
           marginRight,
           marginTop,
           marginBottom,
           landscape);
   return renderTarget;
 }
  /**
   * Function that takes some value in a String form and converts it to the specified class type
   *
   * @param valueString the value in a String form
   * @param valueClass the class type to convert the value
   * @return
   */
  protected Object readValueFromText(String valueString, Class valueClass) {
    //        System.out.println("@Var: valueClass: "+valueClass);
    //        System.out.println("@Var: valueString: "+valueString);

    // bug
    if (valueString.startsWith("org.netbeans.beaninfo.editors.ColorEditor")) {
      // bug
      Pattern rgb = Pattern.compile(".*\\[r=(\\d+),g=(\\d+),b=(\\d+)\\]");
      Matcher matcher = rgb.matcher(valueString);
      if (matcher.matches()) {
        valueString =
            "[" + matcher.group(1) + "," + matcher.group(2) + "," + matcher.group(3) + "]";
      }
    }

    Object value = PreviewProperties.readValueFromText(valueString, valueClass);
    if (value != null) {
      return value;
    }

    if (valueClass.equals(LegendItem.Alignment.class)) {
      value = availableAlignments[Integer.parseInt(valueString)];
    } else if (valueClass.equals(LegendItem.Shape.class)) {
      value = availableShapes[Integer.parseInt(valueString)];
    } else if (valueClass.equals(LegendItem.Direction.class)) {
      value = availableDirections[Integer.parseInt(valueString)];
    } else if (valueClass.equals(Boolean.class)) {
      value = Boolean.parseBoolean(valueString);
    } else if (valueClass.equals(Integer.class)) {
      value = Integer.parseInt(valueString);
    } else if (valueClass.equals(File.class)) {
      value = new File(valueString);
    }
    return value;
  }
  /**
   * Function that automatically saves a property using its PropertyName and the Value attached to
   * it. Only works if property has a known value type. Known types: <code>Integer</code>, <code>
   *  Float</code>, <code> String</code>,, <code> Color</code>, <code> LegendItem.Alignment</code>,
   * <code> LegendItem.Shape</code> and <code> LegendItem.Direction</code>
   *
   * @param writer the XMLStreamWriter to write to
   * @param property property to be saved
   * @param previewProperties Current workspace PreviewProperties
   * @throws XMLStreamException
   */
  protected void writeXMLFromSingleProperty(
      XMLStreamWriter writer, PreviewProperty property, PreviewProperties previewProperties)
      throws XMLStreamException {
    // Better read from previewProperties instead of just the property, because LegendMouseListener
    // puts origin x and y in previewProperties.
    Object propertyValue = previewProperties.getValue(property.getName());

    if (propertyValue != null) {
      String text = writeValueAsText(propertyValue);
      writer.writeStartElement(XML_PROPERTY);
      String name = LegendModel.getPropertyFromPreviewProperty(property);
      System.out.println("@Var: SAVING XML name: " + name + " , " + text);
      writer.writeAttribute(XML_NAME, name);
      writer.writeAttribute(XML_CLASS, propertyValue.getClass().getName());
      writer.writeCharacters(text);
      writer.writeEndElement();
    }
  }
  /**
   * Converts the propertyValue of a known type to an String object
   *
   * @param propertyValue Known * types: <code> LegendItem.Alignment</code>, <code> LegendItem.Shape
   *     </code> and <code> LegendItem.Direction</code>
   * @return
   */
  protected String writeValueAsText(Object propertyValue) {

    String text = PreviewProperties.getValueAsText(propertyValue);
    if (text != null) {
      return text;
    }
    if (propertyValue instanceof LegendItem.Alignment) {
      LegendItem.Alignment propertyValueString = (LegendItem.Alignment) propertyValue;
      text = propertyValueString.getValue();
    } else if (propertyValue instanceof LegendItem.Direction) {
      LegendItem.Direction propertyValueString = (LegendItem.Direction) propertyValue;
      text = propertyValueString.getValue();
    } else if (propertyValue instanceof LegendItem.Shape) {
      LegendItem.Shape propertyValueString = (LegendItem.Shape) propertyValue;
      text = propertyValueString.getValue();
    } else {
      text = propertyValue.toString();
    }
    return text;
  }
Esempio n. 5
0
  @Test
  public void testPNG() {
    try {

      // Init a project - and therefore a workspace
      ProjectController pc = Lookup.getDefault().lookup(ProjectController.class);
      pc.newProject();
      Workspace workspace = pc.getCurrentWorkspace();

      // Append container to graph structure
      String sample = "/org/gephi/desktop/welcome/samples/Les Miserables.gexf";
      final InputStream stream = WelcomeTopComponent.class.getResourceAsStream(sample);
      ImportController importController = Lookup.getDefault().lookup(ImportController.class);
      FileImporter fileImporter = importController.getFileImporter(".gexf");
      Container container = importController.importFile(stream, fileImporter);

      importController.process(container, new DefaultProcessor(), workspace);

      // Add self loop
      GraphController graphController = Lookup.getDefault().lookup(GraphController.class);
      Graph graph = graphController.getModel().getGraph();
      Node node = graph.getNode(12);
      System.out.println("Self loop " + node.getNodeData().getLabel());
      graph.addEdge(graphController.getModel().factory().newEdge(node, node, 31, true));

      // Set label edges
      for (Edge edge : graphController.getModel().getGraph().getEdges()) {
        edge.getEdgeData().setLabel("Label test");
      }

      // svg document

      // attribute ???
      AttributeController attributeController =
          Lookup.getDefault().lookup(AttributeController.class);
      AttributeModel attributeModel = null;

      // preview
      PreviewController previewController = Lookup.getDefault().lookup(PreviewController.class);
      PreviewModel previewModel = previewController.getModel(workspace);
      PreviewProperties previewProperties = previewModel.getProperties();
      previewController.refreshPreview(workspace);

      previewModel.getProperties().putValue("width", 1000);
      previewModel.getProperties().putValue("height", 1000);
      ProcessingTarget target =
          (ProcessingTarget)
              previewController.getRenderTarget(RenderTarget.PROCESSING_TARGET, workspace);

      previewProperties.putValue(LegendModel.LEGEND_PROPERTIES, new LegendModel(workspace));

      LegendModel legendManager = previewProperties.getValue(LegendModel.LEGEND_PROPERTIES);
      Integer itemIndex = legendManager.getCurrentIndex();

      // creating item
      Item item = addGroupsItem(itemIndex, graph, attributeModel);
      //            Item item = addTextItem(itemIndex, graph, attributeModel);

      // add item
      LegendController.getInstance().addItemToLegendModel(workspace, item);

      // render
      previewController.refreshPreview(workspace);
      previewController.render(target);

      File fileToSave = new File("/Volumes/edubecks/edubecks/Untitled.png");
      OutputStream fos = new FileOutputStream(fileToSave);
      Writer writer = new OutputStreamWriter(fos, "UTF-8");

      //            // saving
      //            int width = 1000;
      //            int height = 1000;
      //            PGraphicsJava2D pg2 = (PGraphicsJava2D) target.getGraphics();
      //            BufferedImage img = new BufferedImage(width, height,
      // BufferedImage.TYPE_INT_ARGB);
      //
      //            System.out.println("@Var: pg2.pixels: "+pg2.pixels);
      //            img.setRGB(0, 0, width, height, pg2.pixels, 0, width);
      //            ImageIO.write(img, "png", fileToSave);
      //            stream.close();

      PNGExporter pngExporter = new PNGExporter();
      //            pngExporter.setHeight(2000);
      //            pngExporter.setWidth(2000);
      pngExporter.setWorkspace(workspace);
      pngExporter.setOutputStream(fos);
      pngExporter.execute();

    } catch (Exception e) {
      Exceptions.printStackTrace(e);
    }
  }