Java Code Examples for javafx.scene.layout.Region#getWidth()

The following examples show how to use javafx.scene.layout.Region#getWidth() . 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: FxmlControl.java    From MyBox with Apache License 2.0 6 votes vote down vote up
public static void moveXCenter(Region pNnode, Node node) {
        try {
            if (node == null || pNnode == null) {
                return;
            }
            double xOffset = pNnode.getWidth() - node.getBoundsInLocal().getWidth();

            if (xOffset > 0) {
//                logger.debug(pNnode.getWidth() + " " + node.getBoundsInLocal().getWidth());
                node.setLayoutX(pNnode.getLayoutX() + xOffset / 2);
//                logger.debug(pNnode.getLayoutX() + " " + xOffset / 2 + " " + node.getLayoutX());
            } else {
                node.setLayoutX(0);
            }
        } catch (Exception e) {
        }
    }
 
Example 2
Source File: ModelLayoutUpdater.java    From graph-editor with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Checks if a node's JavaFX region has different layout values than those currently stored in the model.
 *
 * @param node the model instance for the node
 *
 * @return {@code true} if any layout value has changed, {@code false if not}
 */
private boolean checkNodeChanged(final GNode node) {

    final GNodeSkin nodeSkin = skinLookup.lookupNode(node);

    if (nodeSkin == null) {
        return false;
    }

    final Region nodeRegion = nodeSkin.getRoot();

    if (nodeRegion.getLayoutX() != node.getX()) {
        return true;
    } else if (nodeRegion.getLayoutY() != node.getY()) {
        return true;
    } else if (nodeRegion.getWidth() != node.getWidth()) {
        return true;
    } else if (nodeRegion.getHeight() != node.getHeight()) {
        return true;
    }
    return false;
}
 
Example 3
Source File: WorkbenchOverlay.java    From WorkbenchFX with Apache License 2.0 5 votes vote down vote up
private void initialize(Region overlay) {
  if (!isInitialized() && overlay.getWidth() > 0 && overlay.getHeight() > 0) {
    setInitialized(true);
    if (!Objects.isNull(getOnInitialized())) {
      getOnInitialized().handle(new Event(overlay, overlay, Event.ANY));
    }
  }
}
 
Example 4
Source File: CellGestures.java    From fxgraph with Do What The F*ck You Want To Public License 5 votes vote down vote up
private static void dragEast(MouseEvent event, Wrapper<Point2D> mouseLocation, Region region, double handleRadius) {
	final double deltaX = event.getSceneX() - mouseLocation.value.getX();
	final double newMaxX = region.getLayoutX() + region.getWidth() + deltaX;
	if(newMaxX >= region.getLayoutX() && newMaxX <= region.getParent().getBoundsInLocal().getWidth() - handleRadius) {
		region.setPrefWidth(region.getPrefWidth() + deltaX);
	}
}
 
Example 5
Source File: NodeParentageCrumbBar.java    From pmd-designer with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * If the node is already displayed on the crumbbar, only sets the focus on it. Otherwise, sets
 * the node to be the deepest one of the crumb bar. Noop if node is null.
 */
@Override
public void setFocusNode(final Node newSelection, DataHolder options) {

    if (newSelection == null) {
        setSelectedCrumb(null);
        return;
    }

    if (Objects.equals(newSelection, currentSelection)) {
        return;
    }

    currentSelection = newSelection;

    boolean found = false;

    // We're trying to estimate the ratio of px/crumb,
    // to make an educated guess about how many crumbs we can fit
    // in case we need to call setDeepestNode
    int totalNumChar = 0;
    int totalNumCrumbs = 0;
    // the sum of children width is the actual width with overflow
    // the width of this control is the max acceptable width *without* overflow
    double totalChildrenWidth = 0;
    // constant padding around the graphic of a BreadCrumbButton
    // (difference between width of a BreadCrumbButton and that of its graphic)
    double constantPadding = Double.NaN;


    int i = 0;
    // right to left
    for (javafx.scene.Node button : asReversed(getChildren())) {
        Node n = (Node) ((TreeItem<?>) button.getUserData()).getValue();
        // when recovering from a selection it's impossible that the node be found,
        // updating the style would cause visible twitching
        if (!options.hasData(SELECTION_RECOVERY)) {
            // set the focus on the one being selected, remove on the others
            // calling requestFocus would switch the focus from eg the treeview to the crumb bar (unusable)
            button.pseudoClassStateChanged(PseudoClass.getPseudoClass("focused"), newSelection.equals(n));
        }
        // update counters
        totalNumChar += ((Labeled) button).getText().length();
        double childWidth = ((Region) button).getWidth();
        totalChildrenWidth += childWidth;
        totalNumCrumbs++;
        if (Double.isNaN(constantPadding)) {
            Region graphic = (Region) ((Labeled) button).getGraphic();
            if (graphic != null) {
                constantPadding = childWidth - graphic.getWidth();
            }
        }

        if (newSelection.equals(n)) {
            found = true;
            selectedIdx = getChildren().size() - i;
        }

        i++;
    }

    if (!found && !options.hasData(SELECTION_RECOVERY) || options.hasData(SELECTION_RECOVERY) && selectedIdx != 0) {
        // Then we reset the deepest node.

        setDeepestNode(newSelection, getWidthEstimator(totalNumChar, totalChildrenWidth, totalNumCrumbs, constantPadding));
        // set the deepest as focused
        getChildren().get(getChildren().size() - 1)
                     .pseudoClassStateChanged(PseudoClass.getPseudoClass("focused"), true);
        selectedIdx = 0;
    } else if (options.hasData(SELECTION_RECOVERY)) {

        Node cur = newSelection;
        // right to left, update underlying nodes without changing display
        // this relies on the fact that selection recovery only selects nodes with exactly the same path
        for (javafx.scene.Node child : asReversed(getChildren())) {
            if (cur == null) {
                break;
            }
            @SuppressWarnings("unchecked")
            TreeItem<Node> userData = (TreeItem<Node>) child.getUserData();
            userData.setValue(cur);
            cur = cur.getParent();
        }
    }
}
 
Example 6
Source File: WorkbenchPresenter.java    From WorkbenchFX with Apache License 2.0 4 votes vote down vote up
/**
 * Makes the {@code overlay} visible, along with its {@code glassPane}.
 *
 * @param workbenchOverlay the {@code overlay}'s corresponding model object
 * @param blocking if false, will make {@code overlay} hide, if its {@code glassPane} was clicked
 */
private void showOverlay(WorkbenchOverlay workbenchOverlay, boolean blocking) {
  LOGGER.trace("showOverlay - Blocking: " + blocking);
  Region overlay = workbenchOverlay.getOverlay();
  if (workbenchOverlay.isAnimated()) {
    if (overlay.getWidth() != 0) {
      workbenchOverlay.getAnimationStart().play();
    }
  }
  view.showOverlay(overlay);

  // if overlay is not blocking, make the overlay hide when the glass pane is clicked
  if (!blocking) {
    LOGGER.trace("showOverlay - Set GlassPane EventHandler");
    workbenchOverlay.getGlassPane().setOnMouseClicked(event -> {
      // check if overlay is really not blocking, is needed to avoid false-positives
      if (overlaysShown.contains(overlay)) {

        if (overlay == model.getDrawerShown()) {
          // if the overlay is the drawer that is currently being shown
          LOGGER.trace("GlassPane was clicked, hiding drawer");
          model.hideDrawer();
        } else if (overlay instanceof DialogControl) {
          // if the overlay is a dialog
          LOGGER.trace("GlassPane was clicked, hiding dialog");
          WorkbenchDialog dialog = ((DialogControl) overlay).getDialog();
          // send cancel button type as result of the dialog if available
          ButtonType cancelButtonType = dialog.getDialogControl().getCancelButtonType();
          // if not available, send the defined cancelDialogButtonType
          if (Objects.isNull(cancelButtonType)) {
            cancelButtonType = ButtonType.CANCEL;
          }
          dialog.getOnResult().accept(cancelButtonType);
          model.hideDialog(dialog);
        } else {
          LOGGER.trace("GlassPane was clicked, hiding overlay");
          model.hideOverlay(overlay);
        }
      }
    });
  }
}
 
Example 7
Source File: XYChartInfo.java    From jfxutils with Apache License 2.0 4 votes vote down vote up
private Rectangle2D getComponentArea( Region childRegion ) {
	double xStart = getXShift( childRegion, referenceNode );
	double yStart = getYShift( childRegion, referenceNode );

	return new Rectangle2D( xStart, yStart, childRegion.getWidth(), childRegion.getHeight() );
}
 
Example 8
Source File: SelectionCopier.java    From graph-editor with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Gets the start and end x- and y-positions of the given region (including insets).
 *
 * @param region a {@link Region}
 * @return the bounds of the given region
 */
private Bounds getBounds(final Region region) {

    final Bounds bounds = new Bounds();

    bounds.startX = region.getInsets().getLeft();
    bounds.startY = region.getInsets().getTop();

    bounds.endX = region.getWidth() - region.getInsets().getRight();
    bounds.endY = region.getHeight() - region.getInsets().getBottom();

    return bounds;
}