All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.tentackle.fx.rdc.PdoTreeCell Maven / Gradle / Ivy

There is a newer version: 21.16.1.0
Show newest version
/*
 * Tentackle - https://tentackle.org.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

package org.tentackle.fx.rdc;

import javafx.collections.ObservableList;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Tooltip;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.input.DragEvent;
import javafx.scene.input.MouseEvent;

import org.tentackle.common.StringHelper;
import org.tentackle.pdo.PersistentDomainObject;

import java.util.Objects;

/**
 * A PDO tree cell.
 *
 * @author harald
 * @param  the pdo type
 */
public class PdoTreeCell> extends TreeCell {

  private GuiProvider provider;
  private ContextMenu contextMenu;    // != null if menu shown

  /**
   * Creates a pdo tree cell.
   */
  public PdoTreeCell() {
    addEventHandler(MouseEvent.ANY, this::showContextMenu);
    setOnDragDetected(this::handleDragDetected);
    setOnDragOver(this::handleDragOver);
    setOnDragDropped(this::handleDragDropped);
  }

  @Override
  public void updateItem(T pdo, boolean empty) {
    super.updateItem(pdo, empty);

    if (empty) {
      setText(null);
      setGraphic(null);
      provider = null;
    }
    else {
      provider = pdo == null ? null : Rdc.createGuiProvider(pdo);
      if (provider == null) {
        setText(null);
        setTooltip(null);
        setGraphic(null);
      }
      else {
        PdoTreeItem parentItem = getPdoTreeItem().getParentPdoItem();
        String treeText = provider.getTreeText(parentItem == null ? null : parentItem.getPdo());
        setText(treeText);
        String tooltipText = provider.getToolTipText(parentItem == null ? null : parentItem.getPdo());
        if (StringHelper.isAllWhitespace(tooltipText) || Objects.equals(treeText, tooltipText)) {
          tooltipText = null; // empty or same as treetext -> no tooltip (default)
        }
        setTooltip(tooltipText == null ? null : new Tooltip(tooltipText));
        setGraphic(provider.createIcon());
      }
    }
  }

  /**
   * Gets the PDO tree item.
   *
   * @return the PDO tree item
   */
  public PdoTreeItem getPdoTreeItem() {
    return (PdoTreeItem) getTreeItem();    // getTreeItem is final :(
  }

  /**
   * Gets the GUI provider.
   *
   * @return the provider, null if the items value (the PDO) is null
   */
  protected GuiProvider getGuiProvider() {
    return provider;
  }


  /**
   * Handles the drag detected mouse event.
   *
   * @param event the mouse event after drag has been detected
   */
  protected void handleDragDetected(MouseEvent event) {
    if (provider != null && provider.createDragboard(this) != null) {
      event.consume();
    }
  }

  /**
   * Handles the drag-over event.
   *
   * @param event the drag event
   */
  protected void handleDragOver(DragEvent event) {
    if (event.getGestureSource() != this && provider != null && provider.isDragAccepted(event)) {
      event.consume();
    }
  }

  /**
   * Handles the drag-dropped event.
   *
   * @param event the drag event
   */
  protected void handleDragDropped(DragEvent event) {
    if (provider != null) {
      provider.dropDragboard(event.getDragboard());
      event.setDropCompleted(true);
      T reloadedPdo = provider.getPdo().reload();
      TreeItem root = getTreeItem().getParent();
      if (root != null) {
        ObservableList> items = root.getChildren();
        int i=0;
        for (TreeItem item: items) {
          if (reloadedPdo.equals(item.getValue())) {
            TreeItem reloadedItem = Rdc.createGuiProvider(reloadedPdo).createTreeItem();
            items.set(i, reloadedItem);
            getTreeView().refresh();
            reloadedItem.setExpanded(true);
            break;
          }
          i++;
        }
      }
    }
    else {
      event.consume();
    }
  }


  /**
   * Shows the popup context menu for the current PDO.
   *
   * @param event the mouse event
   */
  private void showContextMenu(MouseEvent event) {
    if (event.isPopupTrigger()) {
      if (contextMenu != null) {
        contextMenu.hide();
      }
      if (getTreeView().getContextMenu() == null) {
        contextMenu = PdoContextMenuFactory.getInstance().create(this);
        if (contextMenu != null) {
          event.consume();
          contextMenu.setOnHidden(e -> contextMenu = null);   // -> GC
          contextMenu.show(this, event.getScreenX(), event.getScreenY());
        }
      }
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy