| 1 | /* |
| 2 | * Copyright (C) 2017 EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com> |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are |
| 5 | * made available under the terms of the Eclipse Public License v1.0 which |
| 6 | * accompanies this distribution, and is available at |
| 7 | * http://www.eclipse.org/legal/epl-v10.html |
| 8 | */ |
| 9 | |
| 10 | package org.lttng.scope.tmf2.views.ui.timeline; |
| 11 | |
| 12 | import java.util.LinkedHashSet; |
| 13 | import java.util.Objects; |
| 14 | import java.util.Set; |
| 15 | |
| 16 | import org.lttng.scope.tmf2.views.core.NestingBoolean; |
| 17 | import org.lttng.scope.tmf2.views.core.context.ViewGroupContext; |
| 18 | import org.lttng.scope.tmf2.views.core.timegraph.control.TimeGraphModelControl; |
| 19 | import org.lttng.scope.tmf2.views.core.timegraph.model.provider.ITimeGraphModelProvider; |
| 20 | import org.lttng.scope.tmf2.views.core.timegraph.model.provider.ITimeGraphModelProviderFactory; |
| 21 | import org.lttng.scope.tmf2.views.core.timegraph.model.provider.TimeGraphModelProviderManager; |
| 22 | import org.lttng.scope.tmf2.views.core.timegraph.view.TimeGraphModelView; |
| 23 | import org.lttng.scope.tmf2.views.ui.timeline.widgets.timegraph.TimeGraphWidget; |
| 24 | |
| 25 | import com.google.common.collect.ImmutableSet; |
| 26 | |
| 27 | import javafx.application.Platform; |
| 28 | import javafx.beans.property.BooleanProperty; |
| 29 | import javafx.beans.property.DoubleProperty; |
| 30 | import javafx.beans.property.SimpleBooleanProperty; |
| 31 | import javafx.beans.property.SimpleDoubleProperty; |
| 32 | |
| 33 | public class TimelineManager { |
| 34 | |
| 35 | private static final double INITIAL_DIVIDER_POSITION = 0.2; |
| 36 | |
| 37 | private final Set<ITimelineWidget> fWidgets = new LinkedHashSet<>(); |
| 38 | |
| 39 | private final NestingBoolean fHScrollListenerStatus = new NestingBoolean(); |
| 40 | |
| 41 | private final DoubleProperty fDividerPosition = new SimpleDoubleProperty(INITIAL_DIVIDER_POSITION); |
| 42 | private final DoubleProperty fHScrollValue = new SimpleDoubleProperty(0); |
| 43 | |
| 44 | /* Properties to sync ongoing selection rectangles */ |
| 45 | private final BooleanProperty fSelectionVisible = new SimpleBooleanProperty(true); |
| 46 | private final DoubleProperty fOngoingSelectionX = new SimpleDoubleProperty(); |
| 47 | private final DoubleProperty fOngoingSelectionWidth = new SimpleDoubleProperty(); |
| 48 | private final BooleanProperty fOngoingSelectionVisible = new SimpleBooleanProperty(false); |
| 49 | |
| 50 | public TimelineManager(ViewGroupContext viewContext) { |
| 51 | |
| 52 | /* Add widgets for all known timegraph model providers */ |
| 53 | for (ITimeGraphModelProviderFactory factory : TimeGraphModelProviderManager.instance().getRegisteredProviderFactories()) { |
| 54 | /* Instantiate a widget for this provider type */ |
| 55 | ITimeGraphModelProvider provider = factory.get(); |
| 56 | TimeGraphModelControl control = new TimeGraphModelControl(viewContext, provider); |
| 57 | TimeGraphWidget viewer = new TimeGraphWidget(control, fHScrollListenerStatus); |
| 58 | control.attachView(viewer); |
| 59 | |
| 60 | fWidgets.add(viewer); |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | * Bind properties in a runLater() statement, so that the UI views have |
| 65 | * already been initialized. The divider position, for instance, only |
| 66 | * has effect after the view is visible. |
| 67 | */ |
| 68 | Platform.runLater(() -> { |
| 69 | /* Bind divider positions, where applicable */ |
| 70 | fWidgets.stream() |
| 71 | .map(w -> w.getSplitPane()) |
| 72 | .filter(Objects::nonNull).map(p -> Objects.requireNonNull(p)) |
| 73 | .forEach(splitPane -> splitPane.getDividers().get(0).positionProperty().bindBidirectional(fDividerPosition)); |
| 74 | |
| 75 | /* Bind h-scrollbar positions */ |
| 76 | fWidgets.stream() |
| 77 | .map(w -> w.getTimeBasedScrollPane()) |
| 78 | .filter(Objects::nonNull).map(p -> Objects.requireNonNull(p)) |
| 79 | .forEach(scrollPane -> scrollPane.hvalueProperty().bindBidirectional(fHScrollValue)); |
| 80 | |
| 81 | /* Bind the selection rectangles together */ |
| 82 | fWidgets.stream() |
| 83 | .map(w -> w.getSelectionRectangle()) |
| 84 | .filter(Objects::nonNull).map(r -> Objects.requireNonNull(r)) |
| 85 | .forEach(rect -> { |
| 86 | rect.visibleProperty().bindBidirectional(fSelectionVisible); |
| 87 | }); |
| 88 | fWidgets.stream() |
| 89 | .map(w -> w.getOngoingSelectionRectangle()) |
| 90 | .filter(Objects::nonNull).map(r -> Objects.requireNonNull(r)) |
| 91 | .forEach(rect -> { |
| 92 | rect.layoutXProperty().bindBidirectional(fOngoingSelectionX); |
| 93 | rect.widthProperty().bindBidirectional(fOngoingSelectionWidth); |
| 94 | rect.visibleProperty().bindBidirectional(fOngoingSelectionVisible); |
| 95 | }); |
| 96 | }); |
| 97 | } |
| 98 | |
| 99 | public void dispose() { |
| 100 | fWidgets.forEach(w -> { |
| 101 | if (w instanceof TimeGraphModelView) { |
| 102 | /* |
| 103 | * TimeGraphModelView's are disposed via their control |
| 104 | * |
| 105 | * FIXME Do this better. |
| 106 | */ |
| 107 | ((TimeGraphModelView) w).getControl().dispose(); |
| 108 | } else { |
| 109 | w.dispose(); |
| 110 | } |
| 111 | }); |
| 112 | } |
| 113 | |
| 114 | public Iterable<ITimelineWidget> getWidgets() { |
| 115 | return ImmutableSet.copyOf(fWidgets); |
| 116 | } |
| 117 | |
| 118 | void resetInitialSeparatorPosition() { |
| 119 | fDividerPosition.set(INITIAL_DIVIDER_POSITION); |
| 120 | } |
| 121 | |
| 122 | } |