Search in sources :

Example 1 with TileEvent

use of eu.hansolo.tilesfx.events.TileEvent in project tilesfx by HanSolo.

the class Tile method registerListeners.

private void registerListeners() {
    disabledProperty().addListener(o -> setOpacity(isDisabled() ? 0.4 : 1));
    valueProperty().addListener((o, ov, nv) -> oldValue.set(ov.doubleValue()));
    currentValueProperty().addListener(o -> {
        double currentValue = getCurrentValue();
        if (currentValue > getMaxValue()) {
            fireTileEvent(MAX_VALUE_EXCEEDED);
        } else if (currentValue < getMinValue()) {
            fireTileEvent(MIN_VALUE_UNDERRUN);
        } else {
            fireTileEvent(VALUE_IN_RANGE);
        }
    });
    showing.addListener((o, ov, nv) -> {
        if (nv) {
            while (tileEventQueue.peek() != null) {
                TileEvent event = tileEventQueue.poll();
                for (TileEventListener listener : tileEventListeners) {
                    listener.onTileEvent(event);
                }
            }
        }
    });
}
Also used : TileEvent(eu.hansolo.tilesfx.events.TileEvent) TileEventListener(eu.hansolo.tilesfx.events.TileEventListener)

Example 2 with TileEvent

use of eu.hansolo.tilesfx.events.TileEvent in project tilesfx by HanSolo.

the class BarChartTileSkin method initGraphics.

// ******************** Initialization ************************************
@Override
protected void initGraphics() {
    super.initGraphics();
    updateHandler = e -> updateChart();
    paneSizeListener = o -> resizeItems();
    handlerMap = new HashMap<>();
    List<BarChartItem> barChartItems = tile.getBarChartItems().stream().sorted(Comparator.comparing(BarChartItem::getValue).reversed()).collect(Collectors.toList());
    tile.getBarChartItems().forEach(item -> {
        item.addChartDataEventListener(updateHandler);
        EventHandler<MouseEvent> clickHandler = e -> tile.fireTileEvent(new TileEvent(EventType.SELECTED_CHART_DATA, item.getChartData()));
        handlerMap.put(item, clickHandler);
        item.addEventHandler(MouseEvent.MOUSE_PRESSED, clickHandler);
        item.setMaxValue(tile.getMaxValue());
        if (null == item.getFormatString() || item.getFormatString().isEmpty()) {
            item.setFormatString(formatString);
        }
    });
    barChartPane = new Pane();
    barChartPane.getChildren().addAll(barChartItems);
    titleText = new Text();
    titleText.setFill(tile.getTitleColor());
    Helper.enableNode(titleText, !tile.getTitle().isEmpty());
    text = new Text(tile.getText());
    text.setFill(tile.getUnitColor());
    Helper.enableNode(text, tile.isTextVisible());
    getPane().getChildren().addAll(titleText, text, barChartPane);
}
Also used : Tile(eu.hansolo.tilesfx.Tile) EventHandler(javafx.event.EventHandler) Fonts(eu.hansolo.tilesfx.fonts.Fonts) MouseEvent(javafx.scene.input.MouseEvent) Font(javafx.scene.text.Font) ChartDataEventListener(eu.hansolo.tilesfx.events.ChartDataEventListener) EventType(eu.hansolo.tilesfx.events.TileEvent.EventType) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) InvalidationListener(javafx.beans.InvalidationListener) Platform(javafx.application.Platform) Text(javafx.scene.text.Text) List(java.util.List) Helper(eu.hansolo.tilesfx.tools.Helper) TileEvent(eu.hansolo.tilesfx.events.TileEvent) Map(java.util.Map) WeakListChangeListener(javafx.collections.WeakListChangeListener) Comparator(java.util.Comparator) Pane(javafx.scene.layout.Pane) TileEvent(eu.hansolo.tilesfx.events.TileEvent) MouseEvent(javafx.scene.input.MouseEvent) Text(javafx.scene.text.Text) Pane(javafx.scene.layout.Pane)

Example 3 with TileEvent

use of eu.hansolo.tilesfx.events.TileEvent in project tilesfx by HanSolo.

the class CountryTileSkin method initGraphics.

// ******************** Initialization ************************************
@Override
protected void initGraphics() {
    super.initGraphics();
    // poiLocations       = FXCollections.observableHashMap();
    // chartDataLocations = FXCollections.observableHashMap();
    // circleHandlerMap   = new HashMap<>();
    country = tile.getCountry();
    if (null == country) {
        country = Country.DE;
    }
    clickHandler = event -> tile.fireTileEvent(new TileEvent(EventType.SELECTED_CHART_DATA, new ChartData(country.getName(), country.getValue(), country.getColor())));
    countryPaths = Helper.getHiresCountryPaths().get(country.name());
    countryMinX = Helper.MAP_WIDTH;
    countryMinY = Helper.MAP_HEIGHT;
    countryMaxX = 0;
    countryMaxY = 0;
    countryPaths.forEach(path -> {
        path.setFill(tile.getBarColor());
        countryMinX = Math.min(countryMinX, path.getBoundsInParent().getMinX());
        countryMinY = Math.min(countryMinY, path.getBoundsInParent().getMinY());
        countryMaxX = Math.max(countryMaxX, path.getBoundsInParent().getMaxX());
        countryMaxY = Math.max(countryMaxY, path.getBoundsInParent().getMaxY());
    });
    /*
        tile.getPoiList()
            .forEach(poi -> {
                String tooltipText = new StringBuilder(poi.getName()).append("\n")
                                                                     .append(poi.getInfo())
                                                                     .toString();
                Circle circle = new Circle(3, poi.getColor());
                circle.setOnMousePressed(e -> poi.fireLocationEvent(new LocationEvent(poi)));
                Tooltip.install(circle, new Tooltip(tooltipText));
                poiLocations.put(poi, circle);
            });
        */
    titleText = new Text();
    titleText.setFill(tile.getTitleColor());
    Helper.enableNode(titleText, !tile.getTitle().isEmpty());
    text = new Text(tile.getCountry().getDisplayName());
    text.setFill(tile.getTextColor());
    Helper.enableNode(text, tile.isTextVisible());
    countryGroup = new Group();
    countryGroup.getChildren().setAll(countryPaths);
    countryContainer = new StackPane();
    countryContainer.setMinSize(size * 0.9, tile.isTextVisible() ? size * 0.72 : size * 0.795);
    countryContainer.setMaxSize(size * 0.9, tile.isTextVisible() ? size * 0.72 : size * 0.795);
    countryContainer.setPrefSize(size * 0.9, tile.isTextVisible() ? size * 0.72 : size * 0.795);
    countryContainer.getChildren().setAll(countryGroup);
    valueText = new Text(String.format(locale, formatString, ((tile.getValue() - minValue) / range * 100)));
    valueText.setFill(tile.getValueColor());
    valueText.setTextOrigin(VPos.BASELINE);
    Helper.enableNode(valueText, tile.isValueVisible());
    unitText = new Text(" " + tile.getUnit());
    unitText.setFill(tile.getUnitColor());
    unitText.setTextOrigin(VPos.BASELINE);
    Helper.enableNode(unitText, !tile.getUnit().isEmpty());
    valueUnitFlow = new TextFlow(valueText, unitText);
    valueUnitFlow.setTextAlignment(TextAlignment.RIGHT);
    valueUnitFlow.setMouseTransparent(true);
    getPane().getChildren().addAll(titleText, countryContainer, valueUnitFlow, text);
// getPane().getChildren().addAll(poiLocations.values());
}
Also used : TileEvent(eu.hansolo.tilesfx.events.TileEvent) Group(javafx.scene.Group) ChartData(eu.hansolo.tilesfx.chart.ChartData) Text(javafx.scene.text.Text) TextFlow(javafx.scene.text.TextFlow) StackPane(javafx.scene.layout.StackPane)

Example 4 with TileEvent

use of eu.hansolo.tilesfx.events.TileEvent in project tilesfx by HanSolo.

the class DonutChartTileSkin method initGraphics.

// ******************** Initialization ************************************
@Override
protected void initGraphics() {
    super.initGraphics();
    chartEventListener = e -> redraw();
    tile.getChartData().forEach(chartData -> chartData.addChartDataEventListener(chartEventListener));
    chartDataListener = c -> {
        while (c.next()) {
            if (c.wasAdded()) {
                c.getAddedSubList().forEach(addedItem -> addedItem.addChartDataEventListener(chartEventListener));
            } else if (c.wasRemoved()) {
                c.getRemoved().forEach(removedItem -> removedItem.removeChartDataEventListener(chartEventListener));
            }
        }
        drawChart();
        drawLegend();
    };
    clickHandler = e -> {
        double x = e.getX();
        double y = e.getY();
        double startAngle = 90;
        double angle = 0;
        List<ChartData> dataList = tile.isSortedData() ? tile.getChartData().stream().sorted(Comparator.comparingDouble(ChartData::getValue)).collect(Collectors.toList()) : tile.getChartData();
        int noOfItems = dataList.size();
        double sum = dataList.stream().mapToDouble(ChartData::getValue).sum();
        double stepSize = 360.0 / sum;
        double barWidth = chartCanvas.getWidth() * 0.1;
        double ri = outerRadius - barWidth * 0.5;
        double ro = outerRadius + barWidth * 0.5;
        for (int i = 0; i < noOfItems; i++) {
            ChartData data = dataList.get(i);
            double value = data.getValue();
            startAngle -= angle;
            angle = value * stepSize;
            boolean hit = Helper.isInRingSegment(x, y, centerX, centerY, ro, ri, startAngle, angle);
            if (hit) {
                String tooltipText = new StringBuilder(data.getName()).append("\n").append(String.format(locale, formatString, value)).toString();
                Point2D popupLocation = new Point2D(e.getScreenX() - selectionTooltip.getWidth() * 0.5, e.getScreenY() - size * 0.025 - selectionTooltip.getHeight());
                selectionTooltip.setText(tooltipText);
                selectionTooltip.setX(popupLocation.getX());
                selectionTooltip.setY(popupLocation.getY());
                selectionTooltip.show(tile.getScene().getWindow());
                tile.fireTileEvent(new TileEvent(EventType.SELECTED_CHART_DATA, data));
                break;
            }
        }
    };
    moveHandler = e -> selectionTooltip.hide();
    titleText = new Text();
    titleText.setFill(tile.getTitleColor());
    Helper.enableNode(titleText, !tile.getTitle().isEmpty());
    text = new Text(tile.getText());
    text.setFill(tile.getTextColor());
    Helper.enableNode(text, tile.isTextVisible());
    chartCanvas = new Canvas(size * 0.9, tile.isTextVisible() ? height - size * 0.28 : height - size * 0.205);
    chartCtx = chartCanvas.getGraphicsContext2D();
    legendCanvas = new Canvas(size * 0.225, tile.isTextVisible() ? height - size * 0.28 : height - size * 0.205);
    legendCtx = legendCanvas.getGraphicsContext2D();
    selectionTooltip = new Tooltip("");
    selectionTooltip.setWidth(60);
    selectionTooltip.setHeight(48);
    Tooltip.install(chartCanvas, selectionTooltip);
    getPane().getChildren().addAll(titleText, legendCanvas, chartCanvas, text);
}
Also used : EventHandler(javafx.event.EventHandler) Fonts(eu.hansolo.tilesfx.fonts.Fonts) MouseEvent(javafx.scene.input.MouseEvent) ChartDataEventListener(eu.hansolo.tilesfx.events.ChartDataEventListener) EventType(eu.hansolo.tilesfx.events.TileEvent.EventType) ChartData(eu.hansolo.tilesfx.chart.ChartData) Helper(eu.hansolo.tilesfx.tools.Helper) ListChangeListener(javafx.collections.ListChangeListener) VPos(javafx.geometry.VPos) Locale(java.util.Locale) Point2D(javafx.geometry.Point2D) TextAlignment(javafx.scene.text.TextAlignment) Tooltip(javafx.scene.control.Tooltip) Tile(eu.hansolo.tilesfx.Tile) Color(javafx.scene.paint.Color) GraphicsContext(javafx.scene.canvas.GraphicsContext) Font(javafx.scene.text.Font) Canvas(javafx.scene.canvas.Canvas) Collectors(java.util.stream.Collectors) ArcType(javafx.scene.shape.ArcType) Text(javafx.scene.text.Text) List(java.util.List) TileEvent(eu.hansolo.tilesfx.events.TileEvent) StrokeLineCap(javafx.scene.shape.StrokeLineCap) Comparator(java.util.Comparator) TileEvent(eu.hansolo.tilesfx.events.TileEvent) ChartData(eu.hansolo.tilesfx.chart.ChartData) Canvas(javafx.scene.canvas.Canvas) Tooltip(javafx.scene.control.Tooltip) Text(javafx.scene.text.Text) Point2D(javafx.geometry.Point2D)

Example 5 with TileEvent

use of eu.hansolo.tilesfx.events.TileEvent in project tilesfx by HanSolo.

the class MatrixTileSkin method initGraphics.

// ******************** Initialization ************************************
@Override
protected void initGraphics() {
    super.initGraphics();
    matrix = PixelMatrixBuilder.create().pixelShape(PixelShape.SQUARE).useSpacer(true).squarePixels(false).colsAndRows(tile.getMatrixSize()).pixelOnColor(tile.getBarColor()).pixelOffColor(Helper.isDark(tile.getBackgroundColor()) ? tile.getBackgroundColor().brighter() : tile.getBackgroundColor().darker()).build();
    if (!tile.getChartData().isEmpty() && tile.getChartData().size() > 2) {
        matrix.setColsAndRows(tile.getChartData().size(), matrix.getRows());
    }
    chartEventListener = e -> updateMatrixWithChartData();
    tile.getChartData().forEach(chartData -> chartData.addChartDataEventListener(chartEventListener));
    chartDataListener = c -> {
        while (c.next()) {
            if (c.wasAdded()) {
                c.getAddedSubList().forEach(addedItem -> addedItem.addChartDataEventListener(chartEventListener));
                if (!tile.getChartData().isEmpty() && tile.getChartData().size() > 2) {
                    matrix.setColsAndRows(tile.getChartData().size(), matrix.getRows());
                }
            } else if (c.wasRemoved()) {
                c.getRemoved().forEach(removedItem -> removedItem.removeChartDataEventListener(chartEventListener));
                if (!tile.getChartData().isEmpty() && tile.getChartData().size() > 2) {
                    matrix.setColsAndRows(tile.getChartData().size(), matrix.getRows());
                }
            }
        }
        updateMatrixWithChartData();
    };
    matrixListener = e -> {
        if (tile.getChartData().isEmpty()) {
            return;
        }
        int column = e.getX();
        ChartData data = tile.getChartData().get(column);
        String tooltipText = new StringBuilder(data.getName()).append("\n").append(String.format(locale, formatString, data.getValue())).toString();
        Point2D popupLocation = new Point2D(e.getMouseScreenX() - selectionTooltip.getWidth() * 0.5, e.getMouseScreenY() - size * 0.025 - selectionTooltip.getHeight());
        selectionTooltip.setText(tooltipText);
        selectionTooltip.setX(popupLocation.getX());
        selectionTooltip.setY(popupLocation.getY());
        selectionTooltip.show(tile.getScene().getWindow());
        tile.fireTileEvent(new TileEvent(EventType.SELECTED_CHART_DATA, data));
    };
    mouseHandler = e -> {
        final javafx.event.EventType<? extends MouseEvent> TYPE = e.getEventType();
        if (MouseEvent.MOUSE_CLICKED.equals(TYPE)) {
            matrix.checkForClick(e);
        } else if (MouseEvent.MOUSE_MOVED.equals(TYPE)) {
            selectionTooltip.hide();
        } else if (MouseEvent.MOUSE_EXITED.equals(TYPE)) {
            selectionTooltip.hide();
        }
    };
    titleText = new Text();
    titleText.setFill(tile.getTitleColor());
    Helper.enableNode(titleText, !tile.getTitle().isEmpty());
    text = new Text(tile.getText());
    text.setFill(tile.getTextColor());
    Helper.enableNode(text, tile.isTextVisible());
    selectionTooltip = new Tooltip("");
    selectionTooltip.setWidth(60);
    selectionTooltip.setHeight(48);
    Tooltip.install(matrix, selectionTooltip);
    getPane().getChildren().addAll(titleText, matrix, text);
}
Also used : Tile(eu.hansolo.tilesfx.Tile) EventHandler(javafx.event.EventHandler) Color(javafx.scene.paint.Color) Fonts(eu.hansolo.tilesfx.fonts.Fonts) PixelShape(eu.hansolo.tilesfx.chart.PixelMatrix.PixelShape) MouseEvent(javafx.scene.input.MouseEvent) PixelMatrixEventListener(eu.hansolo.tilesfx.events.PixelMatrixEventListener) Font(javafx.scene.text.Font) ChartDataEventListener(eu.hansolo.tilesfx.events.ChartDataEventListener) EventType(eu.hansolo.tilesfx.events.TileEvent.EventType) Text(javafx.scene.text.Text) ChartData(eu.hansolo.tilesfx.chart.ChartData) List(java.util.List) PixelMatrixBuilder(eu.hansolo.tilesfx.chart.PixelMatrixBuilder) Helper(eu.hansolo.tilesfx.tools.Helper) ListChangeListener(javafx.collections.ListChangeListener) TileEvent(eu.hansolo.tilesfx.events.TileEvent) Point2D(javafx.geometry.Point2D) Tooltip(javafx.scene.control.Tooltip) PixelMatrix(eu.hansolo.tilesfx.chart.PixelMatrix) TileEvent(eu.hansolo.tilesfx.events.TileEvent) ChartData(eu.hansolo.tilesfx.chart.ChartData) Point2D(javafx.geometry.Point2D) Tooltip(javafx.scene.control.Tooltip) Text(javafx.scene.text.Text)

Aggregations

TileEvent (eu.hansolo.tilesfx.events.TileEvent)12 Text (javafx.scene.text.Text)10 ChartData (eu.hansolo.tilesfx.chart.ChartData)9 Tile (eu.hansolo.tilesfx.Tile)8 Fonts (eu.hansolo.tilesfx.fonts.Fonts)8 Helper (eu.hansolo.tilesfx.tools.Helper)8 List (java.util.List)8 EventHandler (javafx.event.EventHandler)8 MouseEvent (javafx.scene.input.MouseEvent)8 Font (javafx.scene.text.Font)8 EventType (eu.hansolo.tilesfx.events.TileEvent.EventType)7 ListChangeListener (javafx.collections.ListChangeListener)6 Color (javafx.scene.paint.Color)6 ChartDataEventListener (eu.hansolo.tilesfx.events.ChartDataEventListener)5 Locale (java.util.Locale)5 Comparator (java.util.Comparator)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 WeakListChangeListener (javafx.collections.WeakListChangeListener)4 Pane (javafx.scene.layout.Pane)4