Java Code Examples for org.netbeans.api.visual.widget.Scene#getView()

The following examples show how to use org.netbeans.api.visual.widget.Scene#getView() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Shows given Scene wrapped in TopComponent and JFrame.
 * @param scene Scene to be shown
 * @return TopComponent instance where scene resides
 */
public static TopComponent showScene(Scene scene) {
    JComponent sceneView = scene.getView();
    if (sceneView == null) {
        sceneView = scene.createView();
    }
    int width = 450, height = 250;
    JFrame frame = new JFrame("Test Scene");
    TopComponent tc = new TopComponent();
    tc.setLayout(new BorderLayout());
    tc.add(sceneView);
    frame.add(tc);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    frame.setBounds((screenSize.width - width) / 2, (screenSize.height - height) / 2, width, height);
    frame.setVisible(true);
    return tc;
}
 
Example 2
Source File: CenteredZoomAnimator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override public void tick(double progress) {
    double nextZoom = progress >= 1.0 ? targetZoom :
        (sourceZoom + progress * (targetZoom - sourceZoom));

    Scene scene = getScene();
    JComponent view = scene.getView ();

    if (view != null) {
        Point viewLocation = view.getVisibleRect ().getLocation();
        Dimension viewSize = view.getVisibleRect ().getSize();
        Point oldCenter = scene.convertSceneToView (center);

        ((DependencyGraphScene)scene).setMyZoomFactor (nextZoom);
        scene.validate (); // HINT - forcing to change preferred size of the JComponent view

        Point newCenter = scene.convertSceneToView (center);
        Rectangle viewBounds = view.getVisibleRect();
        Point visibleCenter = new Point((int)viewBounds.getCenterX(), (int)viewBounds.getCenterY());
        newCenter.x += Math.round((newCenter.x - visibleCenter.x) * progress);
        newCenter.y += Math.round((newCenter.y - visibleCenter.y) * progress);

        view.scrollRectToVisible (new Rectangle (
                newCenter.x - oldCenter.x + viewLocation.x,
                newCenter.y - oldCenter.y + viewLocation.y,
                viewSize.width,
                viewSize.height
        ));
    } else {
        ((DependencyGraphScene)scene).setMyZoomFactor (nextZoom);
    }
}
 
Example 3
Source File: MouseCenteredZoomAction.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public State mouseWheelMoved (Widget widget, WidgetMouseWheelEvent event) {
    Scene scene = widget.getScene ();

    int modifiers = scene.getInputBindings ().getZoomActionModifiers ();
    if ((event.getModifiers () & modifiers) != modifiers)
        return State.REJECTED;

    int amount = event.getWheelRotation ();

    double scale = 1.0;
    while (amount > 0) {
        scale /= zoomMultiplier;
        amount --;
    }
    while (amount < 0) {
        scale *= zoomMultiplier;
        amount ++;
    }

    JComponent view = scene.getView ();
    if (view != null) {
        Rectangle viewBounds = view.getVisibleRect ();

        Point center = widget.convertLocalToScene (event.getPoint ());
        Point mouseLocation = scene.convertSceneToView (center);

        scene.setZoomFactor (scale * scene.getZoomFactor ());
        scene.validate (); // HINT - forcing to change preferred size of the JComponent view

        center = scene.convertSceneToView (center);

        view.scrollRectToVisible (new Rectangle (
                center.x - (mouseLocation.x - viewBounds.x),
                center.y - (mouseLocation.y - viewBounds.y),
                viewBounds.width,
                viewBounds.height
        ));
    } else
        scene.setZoomFactor (scale * scene.getZoomFactor ());

    return State.CONSUMED;
}
 
Example 4
Source File: CenteredZoomAction.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public State mouseWheelMoved (Widget widget, WidgetMouseWheelEvent event) {
    Scene scene = widget.getScene ();

    int modifiers = scene.getInputBindings ().getZoomActionModifiers ();
    if ((event.getModifiers () & modifiers) != modifiers)
        return State.REJECTED;

    int amount = event.getWheelRotation ();

    double scale = 1.0;
    while (amount > 0) {
        scale /= zoomMultiplier;
        amount --;
    }
    while (amount < 0) {
        scale *= zoomMultiplier;
        amount ++;
    }

    JComponent view = scene.getView ();
    if (view != null) {
        Rectangle viewBounds = view.getVisibleRect ();

        Point center = GeomUtil.center (viewBounds);
        center = scene.convertViewToScene (center);

        scene.setZoomFactor (scale * scene.getZoomFactor ());
        scene.validate (); // HINT - forcing to change preferred size of the JComponent view
        
        center = scene.convertSceneToView (center);

        view.scrollRectToVisible (new Rectangle (
            center.x - viewBounds.width / 2,
            center.y - viewBounds.height / 2,
            viewBounds.width,
            viewBounds.height
        ));
    } else
        scene.setZoomFactor (scale * scene.getZoomFactor ());

    return State.CONSUMED;
}
 
Example 5
Source File: InplaceEditorAction.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private boolean openEditor (Widget widget, InplaceEditorProvider.EditorInvocationType invocationType) {
    if (editor != null) {
        return false;
    }
    Scene scene = widget.getScene();
    JComponent component = scene.getView();
    if (component == null) {
        return false;
    }
    this.invocationType = invocationType;
    editor = provider.createEditorComponent(this, widget);
    if (editor == null) {
        this.invocationType = null;
        return false;
    }
    this.widget = widget;

    component.add(editor);
    provider.notifyOpened(this, widget, editor);

    Rectangle rectangle = widget.getScene().convertSceneToView(widget.convertLocalToScene(widget.getBounds()));

    Point center = GeomUtil.center(rectangle);
    Dimension size = editor.getMinimumSize();
    if (rectangle.width > size.width) {
        size.width = rectangle.width;
    }
    if (rectangle.height > size.height) {
        size.height = rectangle.height;
    }
    int x = center.x - size.width / 2;
    int y = center.y - size.height / 2;

    rectangle = new Rectangle(x, y, size.width, size.height);
    updateRectangleToFitToView(rectangle);

    Rectangle r = provider.getInitialEditorComponentBounds(this, widget, editor, rectangle);
    this.rectangle = r != null ? r : rectangle;

    editor.setBounds(x, y, size.width, size.height);
    notifyEditorComponentBoundsChanged();
    editor.requestFocusInWindow();

    return true;
}
 
Example 6
Source File: SceneExporter.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * 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 scene The Scene to be exported as an image.
 * @param file The file used to store the exported image. If null, then it is 
 * assumed that the raw image is to be returned only and not written to a file.
 * @param SceneExporter.ImageType The image type to be exported for the image map.
 * @param SceneExporter.ZoomType Defines the strategy
 * by which to set the exporting scale factor. Note that certain parameters are
 * nullified by the choice of ZoomType. For instance, if ZoomType.CUSTOM_SIZE is
 * not chosen, then the width and height parameters are not used.
 * @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.
 * @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 static BufferedImage createImage(Scene scene, File file, ImageType imageType, ZoomType zoomType,
        boolean visibleAreaOnly,
        boolean selectedOnly,
        int quality,
        int width,
        int height) throws IOException {

    if (scene == null) {
        return null;
    }
    if (!scene.isValidated()) {
        if (scene.getView() != null) {
            scene.validate();
        } else {
            BufferedImage emptyImage = new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR);
            Graphics2D emptyGraphics = emptyImage.createGraphics();
            scene.validate(emptyGraphics);
        }
    }
    Scene2Image s2i = new Scene2Image(scene, file);
    BufferedImage image = s2i.createImage(imageType, zoomType, visibleAreaOnly, selectedOnly, quality, width, height, false);

    return image;
}