Home Forums FlexGanttFX GanttChartToolBar

Viewing 2 reply threads
  • Author
    Posts
    • Martin
      Participant
      Post count: 10
      #12871 |

      Is it possible to provide source code of class GanttChartToolBar? This would help to write my own ToolBar.

    • Dirk Lemmermann
      Keymaster
      Post count: 69
      #12875 |

      Can do.

      
      /**
      * Copyright (C) 2014, 2015 Dirk Lemmermann Software & Consulting (dlsc.com)
      *
      * This file is part of FlexGanttFX.
      */
      package com.flexganttfx.extras;
      
      import static java.util.Objects.requireNonNull;
      import static org.controlsfx.control.PopOver.ArrowLocation.TOP_CENTER;
      
      import java.text.MessageFormat;
      
      import com.flexganttfx.view.GanttChartBase;
      import org.controlsfx.control.PopOver;
      
      import com.flexganttfx.extras.util.Messages;
      import com.flexganttfx.model.Row;
      import com.flexganttfx.view.GanttChart;
      
      import javafx.beans.binding.Bindings;
      import javafx.beans.property.ObjectProperty;
      import javafx.beans.property.SimpleObjectProperty;
      import javafx.event.ActionEvent;
      import javafx.event.EventHandler;
      import javafx.geometry.Orientation;
      import javafx.geometry.Point2D;
      import javafx.scene.control.Button;
      import javafx.scene.control.MenuButton;
      import javafx.scene.control.MenuItem;
      import javafx.scene.control.Separator;
      import javafx.scene.control.ToggleButton;
      import javafx.scene.control.ToolBar;
      import javafx.stage.PopupWindow.AnchorLocation;
      
      /**
      * A toolbar implementation that can be used in combination with the Gantt chart
      * control. Please note that this toolbar is used for rapid prototyping and does
      * not present a feature-complete implementation that could be used for any kind
      * of application. An entire framework could be written just for that purpose.
      *
      * @param
      * the type of the rows in the Gantt chart
      *
      * @since 1.0
      */
      public class GanttChartToolBar> extends ToolBar {
      
      /**
      * Constructs a new toolbar control. The Gantt chart has to be set later by
      * calling {@link #setGanttChart(GanttChartBase)}.
      *
      * @since 1.0
      */
      public GanttChartToolBar() {
      setOrientation(Orientation.HORIZONTAL);
      
      getStylesheets().add(
      GanttChart.class.getResource("icons16/icons.css") //$NON-NLS-1$
      .toExternalForm());
      
      ganttChartProperty().addListener(observable -> buildToolBar());
      }
      
      /**
      * Constructs a new toolbar control.
      *
      * @param ganttChart
      * the Gantt chart for which the toolbar will be used
      * @since 1.0
      */
      public GanttChartToolBar(GanttChartBase ganttChart) {
      this();
      
      setGanttChart(ganttChart);
      }
      
      private final ObjectProperty ganttChart = new SimpleObjectProperty<>(
      this, "ganttChart"); //$NON-NLS-1$
      
      /**
      * A property used to store the reference to the Gantt chart that will be
      * watched by this status bar.
      *
      * @return the Gantt chart property
      * @since 1.0
      */
      public final ObjectProperty ganttChartProperty() {
      return ganttChart;
      }
      
      /**
      * Returns the value of {@link #ganttChartProperty()}.
      *
      * @return the property used for the Gantt chart reference
      * @since 1.0
      */
      public final GanttChartBase getGanttChart() {
      return ganttChartProperty().get();
      }
      
      /**
      * Sets the value of {@link #ganttChartProperty()}.
      *
      * @param ganttChart
      * the Gantt chart
      * @since 1.0
      */
      public final void setGanttChart(GanttChartBase ganttChart) {
      requireNonNull(ganttChart);
      ganttChartProperty().set(ganttChart);
      }
      
      private void buildToolBar() {
      getItems().clear();
      
      if (layerControlsPopOver != null) {
      layerControlsPopOver.hide();
      layerControlsPopOver = null;
      }
      
      GanttChartBase ganttChart = getGanttChart();
      
      if (ganttChart != null) {
      Button print = new Button(Messages.getString("GanttChartToolBar.BUTTON_PRINT")); //$NON-NLS-1$
      print.getStyleClass().add("printer-icon-16"); //$NON-NLS-1$
      // print.setOnAction(print());
      // getItems().add(print);
      
      ToggleButton detail = new ToggleButton(Messages.getString("GanttChartToolBar.BUTTON_DETAIL")); //$NON-NLS-1$
      detail.getStyleClass().add("gearwheel-icon-16"); //$NON-NLS-1$
      Bindings.bindBidirectional(detail.selectedProperty(),
      getGanttChart().showDetailProperty());
      getItems().add(detail);
      
      getItems().add(new Separator());
      
      Button timeNow = new Button(Messages.getString("GanttChartToolBar.BUTTON_NOW")); //$NON-NLS-1$
      timeNow.getStyleClass().add("clock-icon-16"); //$NON-NLS-1$
      timeNow.setOnAction(showTimeNow());
      getItems().add(timeNow);
      
      Button earliest = new Button(Messages.getString("GanttChartToolBar.BUTTON_EARLIEST")); //$NON-NLS-1$
      earliest.getStyleClass().add("navigate-beginning-icon-16"); //$NON-NLS-1$
      earliest.setOnAction(showEarliestActivities());
      getItems().add(earliest);
      
      Button latest = new Button(Messages.getString("GanttChartToolBar.BUTTON_LATEST")); //$NON-NLS-1$
      latest.getStyleClass().add("navigate-end-icon-16"); //$NON-NLS-1$
      latest.setOnAction(showLatestActivities());
      getItems().add(latest);
      
      getItems().add(new Separator());
      
      Button showAll = new Button(Messages.getString("GanttChartToolBar.BUTTON_ALL")); //$NON-NLS-1$
      showAll.getStyleClass().add("fit-to-width-icon-16"); //$NON-NLS-1$
      showAll.setOnAction(showAllActivities());
      getItems().add(showAll);
      
      getItems().add(new Separator());
      
      Button zoomIn = new Button(Messages.getString("GanttChartToolBar.BUTTON_ZOOM_IN")); //$NON-NLS-1$
      zoomIn.getStyleClass().add("zoom-in-icon-16"); //$NON-NLS-1$
      zoomIn.setOnAction(zoomIn());
      getItems().add(zoomIn);
      
      Button zoomOut = new Button(Messages.getString("GanttChartToolBar.BUTTON_ZOOM_OUT")); //$NON-NLS-1$
      zoomOut.getStyleClass().add("zoom-out-icon-16"); //$NON-NLS-1$
      zoomOut.setOnAction(zoomOut());
      getItems().add(zoomOut);
      
      getItems().add(new Separator());
      
      Button layers = new Button(Messages.getString("GanttChartToolBar.BUTTON_LAYERS")); //$NON-NLS-1$
      layers.getStyleClass().add("front-icon-16"); //$NON-NLS-1$
      layers.setOnAction(showLayerControls(layers));
      getItems().add(layers);
      
      Button radar = new Button(Messages.getString("GanttChartToolBar.BUTTON_RADAR")); //$NON-NLS-1$
      radar.getStyleClass().add("radar-icon-16"); //$NON-NLS-1$
      radar.setOnAction(showRadarPopOver(radar));
      getItems().add(radar);
      
      if (ganttChart instanceof GanttChart) {
      ToggleButton table = new ToggleButton(Messages.getString("GanttChartToolBar.BUTTON_TABLE")); //$NON-NLS-1$
      Bindings.bindBidirectional(table.selectedProperty(),
      ((GanttChart)ganttChart).showTreeTableProperty());
      table.getStyleClass().add("close-treetable-icon-16"); //$NON-NLS-1$
      getItems().add(table);
      }
      
      getItems().add(new Separator());
      
      ToggleButton cursor = new ToggleButton(Messages.getString("GanttChartToolBar.BUTTON_CURSOR")); //$NON-NLS-1$
      cursor.getStyleClass().add("cursor-icon-16"); //$NON-NLS-1$
      cursor.setSelected(getGanttChart().getGraphics()
      .isShowVerticalCursor());
      cursor.setOnAction(toggleCursor());
      getItems().add(cursor);
      
      MenuButton gridLines = new MenuButton(Messages.getString("GanttChartToolBar.BUTTON_GRID")); //$NON-NLS-1$
      gridLines.getStyleClass().add("table-icon-16"); //$NON-NLS-1$
      MenuItem gridOff = new MenuItem(Messages.getString("GanttChartToolBar.MENU_ITEM_GRID_OFF")); //$NON-NLS-1$
      gridOff.setOnAction(hideGridLines());
      gridLines.getItems().add(gridOff);
      
      for (int i = 1; i <= 5; i++) {
      final int level = i;
      MenuItem gridOn = new MenuItem(MessageFormat.format(Messages.getString("GanttChartToolBar.MENU_ITEM_GRID_LEVELS"), i)); //$NON-NLS-1$
      gridLines.getItems().add(gridOn);
      gridOn.setOnAction(showGridLines(level));
      }
      
      getItems().add(gridLines);
      
      ToggleButton calendars = new ToggleButton(Messages.getString("GanttChartToolBar.BUTTON_CALENDARS")); //$NON-NLS-1$
      calendars.setSelected(getGanttChart().getGraphics()
      .isShowCalendarLayer());
      calendars.setOnAction(toggleCalendars());
      getItems().add(calendars);
      
      ToggleButton nowLine = new ToggleButton(Messages.getString("GanttChartToolBar.BUTTON_NOW_LINE")); //$NON-NLS-1$
      nowLine.setSelected(getGanttChart().getGraphics()
      .isShowNowLineLayer());
      nowLine.setOnAction(toggleNowLine());
      getItems().add(nowLine);
      
      getItems().add(new Separator());
      }
      }
      
      private PopOver layerControlsPopOver;
      
      private EventHandler showLayerControls(Button button) {
      return evt -> {
      if (layerControlsPopOver == null) {
      LayersView layersView = new LayersView<>();
      layersView.setGraphics(getGanttChart().getGraphics());
      layerControlsPopOver = new PopOver(layersView);
      layerControlsPopOver.setTitle(Messages.getString("GanttChartToolBar.BUTTON_LAYERS")); //$NON-NLS-1$
      layerControlsPopOver.setArrowLocation(TOP_CENTER);
      }
      
      Point2D localToScreen = button.localToScreen(0, 0);
      layerControlsPopOver
      .setAnchorLocation(AnchorLocation.WINDOW_TOP_LEFT);
      layerControlsPopOver.show(button,
      localToScreen.getX() + button.getWidth() / 2,
      localToScreen.getY() + button.getHeight() - 2);
      };
      }
      
      private PopOver radarPopOver;
      
      private EventHandler showRadarPopOver(Button button) {
      return evt -> {
      if (radarPopOver == null) {
      RadarView radarView = new RadarView();
      radarView.setGraphics(getGanttChart().getGraphics());
      radarPopOver = new PopOver(radarView);
      radarPopOver.setTitle(Messages.getString("GanttChartToolBar.TITLE_RADAR")); //$NON-NLS-1$
      radarPopOver.setArrowLocation(TOP_CENTER);
      }
      
      Point2D localToScreen = button.localToScreen(0, 0);
      radarPopOver.setAnchorLocation(AnchorLocation.WINDOW_TOP_LEFT);
      radarPopOver.show(button, localToScreen.getX() + button.getWidth()
      / 2, localToScreen.getY() + button.getHeight() - 2);
      };
      }
      
      private EventHandler toggleNowLine() {
      return evt -> getGanttChart().getGraphics().setShowNowLineLayer(
      !getGanttChart().getGraphics().isShowNowLineLayer());
      }
      
      private EventHandler toggleCalendars() {
      return evt -> getGanttChart().getGraphics().setShowCalendarLayer(
      !getGanttChart().getGraphics().isShowCalendarLayer());
      }
      
      private EventHandler showGridLines(final int level) {
      return evt -> {
      getGanttChart().getGraphics().setMaxGridLevel(level);
      getGanttChart().getGraphics().setShowGridLineLayer(true);
      };
      }
      
      private EventHandler hideGridLines() {
      return evt -> getGanttChart().getGraphics().setShowGridLineLayer(false);
      }
      
      private EventHandler toggleCursor() {
      return evt -> getGanttChart().getGraphics().setShowVerticalCursor(
      !getGanttChart().getGraphics().isShowVerticalCursor());
      }
      
      private EventHandler zoomOut() {
      return evt -> getGanttChart().getMasterTimeline().zoomOut();
      }
      
      private EventHandler zoomIn() {
      return evt -> getGanttChart().getMasterTimeline().zoomIn();
      }
      
      private EventHandler showAllActivities() {
      return evt -> getGanttChart().getGraphics().showAllActivities();
      }
      
      private EventHandler showLatestActivities() {
      return evt -> getGanttChart().getGraphics().showLatestActivities();
      }
      
      private EventHandler showEarliestActivities() {
      return evt -> getGanttChart().getGraphics().showEarliestActivities();
      }
      
      private EventHandler showTimeNow() {
      return evt -> getGanttChart().getMasterTimeline().showNow();
      }
      }
      
    • Dirk Lemmermann
      Keymaster
      Post count: 69
      #12886 |

      I should mention that the source code I posted is already the one used by the new version 1.6. The class GanttChartBase does not exist in pre-1.6 releases. Simply replace it with “GanttChart”.

Viewing 2 reply threads
  • You must be logged in to reply to this topic.