Пример #1
0
    public void select(Widget widget, Point localLocation, boolean invertSelection) {
      Object object =
          scene.findObject(widget); // consider a map "string" => ShapeObj at creation (drop)

      if (((ConfigNode) object).getId().startsWith("L0")) {
        PerceptionConfigPropNode propNode =
            new PerceptionConfigPropNode((PerceptionConfigNode) object);
        propNode.setDisplayName("Node Settings");
        propNode.setShortDescription("Short Discription");
        NodeOperation.getDefault().showProperties(propNode);
      } else {
        ConfigPropNode propNode = new ConfigPropNode((ConfigNode) object);
        propNode.setDisplayName("Node Settings");
        propNode.setShortDescription("Short Discription");
        NodeOperation.getDefault().showProperties(propNode);
      }

      if (object != null) {
        if (scene.getSelectedObjects().contains(object)) {
          return;
        }
        scene.userSelectionSuggested(Collections.singleton(object), invertSelection);
      } else {
        scene.userSelectionSuggested(Collections.emptySet(), invertSelection);
      }
    }
 /**
  * Returns the index of visible widget represented by given object using depth first search.
  * Integer.MAX_VALUE if none found.
  */
 private static int getIdentityCode(ObjectScene scene, Object object) {
   List<Widget> widgets = scene.findWidgets(object);
   Widget w = null;
   ArrayList<Widget> pathToRoot = new ArrayList<Widget>();
   if (widgets != null) {
     for (Widget w1 : widgets) {
       pathToRoot = new ArrayList<Widget>();
       if (!w1.isVisible()) continue;
       Widget parent = w1.getParentWidget();
       while (parent != null && parent.isVisible()) {
         pathToRoot.add(0, parent);
         parent = parent.getParentWidget();
       }
       if (!pathToRoot.isEmpty() && pathToRoot.get(0) == scene) {
         w = w1;
         pathToRoot.add(w);
         break;
       }
     }
   }
   if (w == null) return Integer.MAX_VALUE;
   int code = 0;
   for (int i = 0; i < pathToRoot.size(); ) {
     Widget widgetOnPath = pathToRoot.get(i++);
     code++;
     if (i == pathToRoot.size()) break;
     int nextWidgetOnPathIndex = widgetOnPath.getChildren().indexOf(pathToRoot.get(i));
     for (int j = 0; j < nextWidgetOnPathIndex; j++) {
       code += getTreeSize(widgetOnPath.getChildren().get(j));
     }
   }
   return code;
 }
Пример #3
0
 public void clear() {
   mainLayer.removeChildren();
   constraintsLayer.removeChildren();
   connectionLayer.removeChildren();
   constants.clear();
   functions.clear();
   scene.validate();
 }
Пример #4
0
 public GraphSceneGlassPane() {
   this.scene = new ObjectScene();
   this.scene.setOpaque(false);
   this.mainLayer = new LayerWidget(this.scene);
   this.connectionLayer = new LayerWidget(this.scene);
   this.constraintsLayer = new LayerWidget(this.scene);
   this.jScroolPane = new JScrollPane();
   this.jScroolPane.setOpaque(false);
   this.scene.addChild(Costanti.INDEX_MAIN_LAYER, this.mainLayer);
   this.scene.addChild(Costanti.INDEX_CONNECTION_LAYER, this.connectionLayer);
   this.scene.addChild(Costanti.INDEX_CONSTRAINTS_LAYER, this.constraintsLayer);
   this.view = scene.createView();
   this.view.setVisible(true);
   this.view.setOpaque(false);
   this.setLayout(new BorderLayout());
   this.add(this.view, BorderLayout.CENTER);
 }
Пример #5
0
  /**
   * Takes the Scene and writes an image file according to the constraints defined by the caller.
   * This returns a BufferedImage of the Scene even if the file can not be written.
   *
   * @param visibleAreaOnly Eliminates all zoom features. If true, the exported image will be a
   *     created from the visible area of the scene.
   * @param selectedOnly Create an image including only the objects selected on the scene. Note that
   *     this feature requires that the scene is an instance of an ObjectScene since it is the
   *     implementation that allows for object selection.
   * @param quality And integer value between 0-100. This is for JPG images only. Parameter is not
   *     used if an image type other than jpg is selected.
   * @param width Directly sets the horizontal dimension of the exported image. This is only used
   *     when the zoomType is ZoomType.CUSTOM_SIZE
   * @param height Directly sets the vertical dimension of the exported image. This is only used
   *     when the zoomType is ZoomType.CUSTOM_SIZE.
   * @param createImageMap If true, the necessary steps are taken to setup the sequential call to
   *     getSceneImageMapCoordinates.
   * @return image The raw image that was written to the file.
   * @throws java.io.IOException If for some reason the file cannot be written, an IOExeption will
   *     be thrown.
   */
  public BufferedImage createImage(
      ImageType imageType,
      ZoomType zoomType,
      boolean visibleAreaOnly,
      boolean selectedOnly,
      int quality,
      int width,
      int height,
      boolean createImageMap)
      throws IOException {
    double _scale = scene.getZoomFactor();

    Rectangle sceneRec = scene.getPreferredBounds();
    Rectangle viewRect = scene.getView().getVisibleRect();

    BufferedImage bufferedImage;
    Graphics2D g;
    ArrayList<Widget> hiddenWidgets = new ArrayList<Widget>();

    int _imageWidth = sceneRec.width;
    int _imageHeight = sceneRec.height;

    Set<?> _selectedObjects = null;

    if (selectedOnly) {
      // in order to use getSelectedObject the scene must be an
      // ObjectScene
      if (scene instanceof ObjectScene) {

        ObjectScene gScene = (ObjectScene) scene;
        // hide unselected widget
        HashSet<Object> invisible = new HashSet<Object>();
        invisible.addAll(gScene.getObjects());
        _selectedObjects = gScene.getSelectedObjects();
        invisible.removeAll(_selectedObjects);

        for (Object o : invisible) {
          Widget widget = gScene.findWidget(o);
          if (widget != null && widget.isVisible()) {
            widget.setVisible(false);
            hiddenWidgets.add(widget);
          }
        }
      }
    }

    if (visibleAreaOnly) {
      _imageWidth = viewRect.width;
      _imageHeight = viewRect.height;
    } else {
      switch (zoomType) {
        case CUSTOM_SIZE:
          _imageWidth = width;
          _imageHeight = height;
          _scale =
              Math.min(
                  (double) width / (double) sceneRec.width,
                  (double) height / (double) sceneRec.height);
          break;
        case FIT_IN_WINDOW:
          _scale =
              Math.min(
                  (double) viewRect.width / (double) sceneRec.width,
                  (double) viewRect.height / (double) sceneRec.height);
          _imageWidth = (int) ((double) sceneRec.width * _scale);
          _imageHeight = (int) ((double) sceneRec.height * _scale);
          break;
        case CURRENT_ZOOM_LEVEL:
          _imageWidth = (int) (sceneRec.width * scene.getZoomFactor());
          _imageHeight = (int) (sceneRec.height * scene.getZoomFactor());
          break;
        case ACTUAL_SIZE:
          _imageWidth = sceneRec.width;
          _imageHeight = sceneRec.height;
          _scale = 1.0;
          break;
      }
    }

    // Note that the field variable are being set to method local variable.
    // This
    // is for the call to getSceneImageMapCoordinates that will come since
    // createImageMap is true.
    if (createImageMap) {
      this.selectedObjects = _selectedObjects;
      this.imageHeight = _imageHeight;
      this.imageWidth = _imageWidth;
      this.scale = _scale;
    }

    bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    g = bufferedImage.createGraphics();
    g.translate(0, 0);
    g.scale(_scale, _scale);
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, width, height);
    scene.paint(g);

    // restore widget visibility
    for (Widget w : hiddenWidgets) {
      w.setVisible(true);
    }

    if (file != null) {
      FileImageOutputStream fo = new FileImageOutputStream(file);

      if (imageType == ImageType.JPG) {
        Iterator<?> iter = ImageIO.getImageWritersByFormatName("jpg");
        ImageWriter writer = (ImageWriter) iter.next();

        ImageWriteParam iwp = writer.getDefaultWriteParam();
        iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        if (quality > 100) {
          quality = 100;
        }
        if (quality < 0) {
          quality = 0;
        }
        iwp.setCompressionQuality(quality / 100);
        writer.setOutput(fo);
        IIOImage image = new IIOImage(bufferedImage, null, null);
        writer.write(null, image, iwp);

        writer.dispose();
      } else {
        ImageIO.write(bufferedImage, "" + imageType, fo);
      }

      fo.flush();
      fo.close();
    }

    return bufferedImage;
  }
Пример #6
0
  /**
   * Takes the Scene and writes an image file according to the constraints defined by the caller.
   * This returns a BufferedImage of the {@link Scene} even if the file can not be written. This
   * function creates the image for the entire {@link Scene}.
   *
   * @param visibleAreaOnly Eliminates all zoom features. If true, the exported image will be a
   *     created from the visible area of the scene.
   * @param selectedOnly Create an image including only the objects selected on the scene. Note that
   *     this feature requires that the scene is an instance of an ObjectScene since it is the
   *     implementation that allows for object selection.
   * @param quality And integer value between 0-100. This is for JPG images only. Parameter is not
   *     used if an image type other than jpg is selected.
   * @param canvas The {@link WorkareaCanvas} that is to exported.
   * @param createImageMap If true, the necessary steps are taken to setup the sequential call to
   *     getSceneImageMapCoordinates.
   * @return image The raw image that was written to the file.
   * @throws java.io.IOException If for some reason the file cannot be written, an IOExeption will
   *     be thrown.
   */
  public BufferedImage createImage(
      ImageType imageType,
      ZoomType zoomType,
      boolean visibleAreaOnly,
      boolean selectedOnly,
      int quality,
      WorkareaCanvas canvas,
      boolean createImageMap)
      throws IOException {
    // Creates a rectangle with the starting point and the dimensions of the
    // scene seleceted for export
    Rectangle rectangle = canvas.getScene().getClientArea();

    double _scale = scene.getZoomFactor();

    Rectangle sceneRec = scene.getPreferredBounds();
    Rectangle viewRect = scene.getView().getVisibleRect();

    BufferedImage bufferedImage;
    Graphics2D g;
    ArrayList<Widget> hiddenWidgets = new ArrayList<Widget>();

    int _imageWidth = sceneRec.width;
    int _imageHeight = sceneRec.height;

    Set<?> _selectedObjects = null;

    if (selectedOnly) {
      // in order to use getSelectedObject the scene must be an
      // ObjectScene
      if (scene instanceof ObjectScene) {

        ObjectScene gScene = (ObjectScene) scene;
        // hide unselected widget
        HashSet<Object> invisible = new HashSet<Object>();
        invisible.addAll(gScene.getObjects());
        _selectedObjects = gScene.getSelectedObjects();
        invisible.removeAll(_selectedObjects);

        for (Object o : invisible) {
          Widget widget = gScene.findWidget(o);
          if (widget != null && widget.isVisible()) {
            widget.setVisible(false);
            hiddenWidgets.add(widget);
          }
        }
      }
    }

    if (visibleAreaOnly) {
      _imageWidth = viewRect.width;
      _imageHeight = viewRect.height;
    } else {
      switch (zoomType) {
        case CUSTOM_SIZE:
          _imageWidth = rectangle.width;
          _imageHeight = rectangle.height;
          _scale =
              Math.min(
                  (double) rectangle.width / (double) sceneRec.width,
                  (double) rectangle.height / (double) sceneRec.height);
          break;
        case FIT_IN_WINDOW:
          _scale =
              Math.min(
                  (double) viewRect.width / (double) sceneRec.width,
                  (double) viewRect.height / (double) sceneRec.height);
          _imageWidth = (int) ((double) sceneRec.width * _scale);
          _imageHeight = (int) ((double) sceneRec.height * _scale);
          break;
        case CURRENT_ZOOM_LEVEL:
          _imageWidth = (int) (sceneRec.width * scene.getZoomFactor());
          _imageHeight = (int) (sceneRec.height * scene.getZoomFactor());
          break;
        case ACTUAL_SIZE:
          _imageWidth = sceneRec.width;
          _imageHeight = sceneRec.height;
          _scale = 1.0;
          break;
      }
    }

    // Note that the field variable are being set to method local variable.
    // This
    // is for the call to getSceneImageMapCoordinates that will come since
    // createImageMap is true.
    if (createImageMap) {
      this.selectedObjects = _selectedObjects;
      this.imageHeight = _imageHeight;
      this.imageWidth = _imageWidth;
      this.scale = _scale;
    }

    bufferedImage =
        new BufferedImage(rectangle.width, rectangle.height, BufferedImage.TYPE_INT_RGB);
    g = bufferedImage.createGraphics();
    g.setRenderingHint(
        RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g.scale(_scale, _scale);
    g.setColor(Color.WHITE);
    g.fillRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
    scene.paint(g);

    // restore widget visibility
    for (Widget w : hiddenWidgets) {
      w.setVisible(true);
    }

    if (file != null) {
      boolean save = true;

      if (file.exists()) {
        int answer =
            JOptionPane.showConfirmDialog(
                null,
                "This file exists. Do you wish to overwrite the file?",
                "Confirm",
                JOptionPane.YES_NO_OPTION);

        // The user has not pressed YES
        if (answer != 0) {
          save = false;
        }
      }

      if (save) {
        FileImageOutputStream fo = new FileImageOutputStream(file);

        if (imageType == ImageType.JPG) {
          Iterator<?> iter = ImageIO.getImageWritersByFormatName("jpg");
          ImageWriter writer = (ImageWriter) iter.next();

          ImageWriteParam iwp = writer.getDefaultWriteParam();
          iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
          if (quality > 100) {
            quality = 100;
          }
          if (quality < 0) {
            quality = 0;
          }
          iwp.setCompressionQuality(quality / 100);
          writer.setOutput(fo);
          IIOImage image = new IIOImage(bufferedImage, null, null);
          writer.write(null, image, iwp);

          writer.dispose();
        } else {
          ImageIO.write(bufferedImage, "" + imageType, fo);
        }

        fo.flush();
        fo.close();
      }
    }

    return bufferedImage;
  }
Пример #7
0
 public void clearConstants() {
   mainLayer.removeChildren(constants);
   constants.clear();
   scene.validate();
 }
Пример #8
0
 public void clearFunctionalDependencies() {
   mainLayer.removeChildren(functionalDependencies);
   functionalDependencies.clear();
   scene.validate();
 }
Пример #9
0
 public void clearFunctions() {
   mainLayer.removeChildren(functions);
   functions.clear();
   scene.validate();
 }
Пример #10
0
  /**
   * This is the master print call. All other print calls in this class pass through to here after
   * setting the appropriate parameters. Note that the scale type parameter take precedent over the
   * horizontal and vertical scaling percentages. Therefore, if the scale type was "SCALE_TO_FIT_Y",
   * then the values of horizontal and vertical scaling percentages would not be used.
   *
   * @param scene The Scene to be printed.
   * @param format format used for printing Scene. If null then a new default PageFormat is created.
   * @param scaleStrategy value representing the how to scale the printing.
   * @param scaleX Directly set the horizontal scale percentage. This parameter is only used when
   *     the scale strategy is ScaleStrategy.SCALE_PERCENT. Otherwise it is ignored.
   * @param scaleY Directly set the vertical scale percentage. This parameter is only used when the
   *     scale strategy is ScaleStrategy.SCALE_PERCENT. Otherwise it is ignored.
   * @param selectedOnly Print only the objects from the Scene that have been selected. Note that in
   *     this case the Scene must be an instnace of an ObjectScene since this is required to
   *     determine the selected objects.
   * @param visibleOnly Print only the object in the visible window.
   * @param region The rectangle representing the are of the Scene to be printed.
   * @param hiddenLayerWidgets Layer that are not to be printed. Might be used to hide the
   *     background while printing.
   */
  public static void print(
      Scene scene,
      PageFormat format,
      ScaleStrategy scaleStrategy,
      double scaleX,
      double scaleY,
      boolean selectedOnly,
      boolean visibleOnly,
      Rectangle region,
      List<LayerWidget> hiddenLayers) {

    // quick return if the scene is null
    if (scene == null) return;
    if (scaleStrategy == null) scaleStrategy = ScaleStrategy.NO_SCALING;

    PageableScene ps =
        new PageableScene(
            scene, format, scaleStrategy, scaleX, scaleY, selectedOnly, visibleOnly, region);

    PrinterJob printJob = PrinterJob.getPrinterJob();
    printJob.setPageable(ps);

    if (printJob.printDialog()) {

      try {
        // this can not go here because it hides all the widgets everytime.
        ArrayList<Widget> hiddenWidgets = new ArrayList<Widget>();

        if (hiddenLayers != null && hiddenLayers.size() > 0) {
          for (Widget widget : hiddenLayers) {
            widget.setVisible(false);
            hiddenWidgets.add(widget);
          }
        }

        // if selectedOnly is true then we need to hide all the non-selected
        // widgets. Note that if the widgets were already hidden due to hiding
        // a layer, they are not hidden again.
        if (selectedOnly) {
          // in order to use getSelectedObject the scene must be an ObjectScene
          if (scene instanceof ObjectScene) {
            ObjectScene gScene = (ObjectScene) scene;
            // hide unselected widget
            HashSet<Object> invisible = new HashSet<Object>();
            invisible.addAll(gScene.getObjects());
            Set selectedObjects = gScene.getSelectedObjects();
            invisible.removeAll(selectedObjects);

            for (Object o : invisible) {
              Widget widget = gScene.findWidget(o);
              if (widget != null && widget.isVisible()) {
                widget.setVisible(false);
                hiddenWidgets.add(widget);
              }
            }
          }
        }

        // Similar to the selectedOnly, if visibleOnly is true we have to
        // hide the widgets not in the visible window. The reason for this
        // is because setting the size alone does not hide the widgets. Any
        // page that is printed will include all its contained widgets if
        // they are not hidden. Note also that this must work with any
        // Scene, not just the ObjectScene. Therefore, we have to gather
        // all the widgets.
        if (visibleOnly || region != null) {

          // if the region is null, then the user is not trying to print a
          // region. The other way into this condition is if we need to
          // to print the visibleArea. So we set the region to be the
          // visible area.
          if (region == null) region = scene.getView().getVisibleRect();

          List<Widget> allWidgets = new ArrayList<Widget>();
          for (Widget widget : scene.getChildren()) {
            allWidgets.addAll(getAllNodeWidgets(widget, null));
          }

          for (Widget widget : allWidgets) {
            Rectangle widgetInSceneCoordinates = widget.convertLocalToScene(widget.getBounds());
            boolean included = region.contains(widgetInSceneCoordinates);
            if (!included && widget.isVisible()) {
              widget.setVisible(false);
              hiddenWidgets.add(widget);
            }
          }
        }

        printJob.print();

        // restore widget visibility
        for (Widget w : hiddenWidgets) {
          w.setVisible(true);
        }

        scene.validate();

      } catch (PrinterException pe) {
        System.out.println("Error printing: " + pe);
      }
    }
  }