<commit_before>/*
 * Copyright (C) 2016 <NAME>
 *
 * This file is distributed under the terms of the
 * GNU General Public License. See the file `LICENSE'
 * in the root directory of the present distribution,
 * or http://www.gnu.org/copyleft/gpl.txt .
 */

package burai.atoms.viewer;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import burai.atoms.design.Design;
import burai.atoms.model.Atom;
import burai.atoms.model.Cell;
import burai.atoms.model.property.CellProperty;
import burai.atoms.viewer.logger.AtomsLogger;
import burai.atoms.viewer.logger.AtomsLoggerPFactory;
import burai.atoms.viewer.operation.ViewerEventManager;
import burai.atoms.visible.VisibleAtom;
import burai.atoms.visible.VisibleCell;
import burai.com.math.Lattice;
import javafx.event.EventHandler;
import javafx.geometry.Point3D;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;

public class AtomsViewer extends AtomsViewerBase<Group> {

    private boolean compassMode;

    private boolean initiallyOperated;

    private ViewerCell viewerCell;
    private ViewerSample viewerSample;
    private ViewerXYZAxis viewerXYZAxis;
    private ViewerCompass viewerCompass;

    private AtomsLogger logger;

    private Design design;
    private List<Node> backgroundNodes;

    private boolean busyLinkedViewers;
    private List<AtomsViewer> linkedViewers;

    public AtomsViewer(Cell cell, double size) {
        this(cell, size, size);
    }

    public AtomsViewer(Cell cell, double size, boolean silent) {
        this(cell, size, size, silent);
    }

    public AtomsViewer(Cell cell, double width, double height) {
        this(cell, width, height, false);
    }

    public AtomsViewer(Cell cell, double width, double height, boolean silent) {
        super(cell, width, height);

        this.compassMode = false;

        this.viewerCell = new ViewerCell(this, this.cell, silent);
        this.viewerSample = new ViewerSample(this, this.cell);
        this.viewerXYZAxis = new ViewerXYZAxis(this);
        if (!silent) {
            this.viewerCompass = new ViewerCompass(this.viewerCell);
        } else {
            this.viewerCompass = null;
        }

        if (!silent) {
            this.logger = new AtomsLogger(this.cell);
        } else {
            this.logger = null;
        }

        this.design = this.createDesign();
        this.backgroundNodes = null;

        this.busyLinkedViewers = false;
        this.linkedViewers = null;

        this.sceneRoot.getChildren().add(this.viewerCell.getNode());
        this.sceneRoot.getChildren().add(this.viewerSample.getNode());
        this.sceneRoot.getChildren().add(this.viewerXYZAxis.getNode());
        if (!silent) {
            this.sceneRoot.getChildren().add(this.viewerCompass.getNode());
        }

        ViewerEventManager viewerEventManager = new ViewerEventManager(this, silent);
        this.subScene.setOnMousePressed(viewerEventManager.getMousePressedHandler());
        this.subScene.setOnMouseDragged(viewerEventManager.getMouseDraggedHandler());
        this.subScene.setOnMouseReleased(viewerEventManager.getMouseReleasedHandler());
        this.subScene.setOnScroll(viewerEventManager.getScrollHandler());

        this.subScene.setOnKeyPressed(event -> {
            EventHandler<KeyEvent> handler = viewerEventManager.getKeyPressedHandler();
            if (handler != null) {
                handler.handle(event);
            }
            if (this.subKeyHandler != null) {
                subKeyHandler.handle(event);
            }
        });

        this.applyDesign();
        this.initialOperations();
    }

    private Design createDesign() {
        Design design = new Design();

        if (this.cell != null) {
            if (this.cell.hasProperty(CellProperty.MOLECULE)) {
                if (this.cell.booleanProperty(CellProperty.MOLECULE)) {
                    design.setShowingCell(false);
                }
            }
        }

        design.addOnBackColorChanged(color -> {
            if (color == null) {
                return;
            }

            this.subScene.setFill(color);

            if (this.backgroundNodes != null && !this.backgroundNodes.isEmpty()) {
                String strColor = color.toString();
                strColor = strColor == null ? null : strColor.replaceAll("0x", "#");
                if (strColor != null) {
                    for (Node node : this.backgroundNodes) {
                        node.setStyle("-fx-background-color: " + strColor);
                    }
                }
            }
        });

        design.addOnShowingLegendChanged(showing -> {
            this.viewerSample.getNode().setVisible(showing);
        });

        design.addOnShowingAxisChanged(showing -> {
            this.viewerXYZAxis.getNode().setVisible(showing);
        });

        design.addOnShowingCellChanged(showing -> {
            this.setCellToCenter();
        });

        return design;
    }

    private void applyDesign() {
        this.subScene.setFill(this.design.getBackColor());
        this.viewerSample.getNode().setVisible(this.design.isShowingLegend());
        this.viewerXYZAxis.getNode().setVisible(this.design.isShowingAxis());
    }

    private void initialOperations() {
        if (this.cell == null) {
            return;
        }<commit_msg>fix AtomsViewer<commit_after>/*
 * Copyright (C) 2016 <NAME>
 *
 * This file is distributed under the terms of the
 * GNU General Public License. See the file `LICENSE'
 * in the root directory of the present distribution,
 * or http://www.gnu.org/copyleft/gpl.txt .
 */

package burai.atoms.viewer;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import burai.atoms.design.Design;
import burai.atoms.model.Atom;
import burai.atoms.model.Cell;
import burai.atoms.model.property.CellProperty;
import burai.atoms.viewer.logger.AtomsLogger;
import burai.atoms.viewer.logger.AtomsLoggerPFactory;
import burai.atoms.viewer.operation.ViewerEventManager;
import burai.atoms.visible.VisibleAtom;
import burai.atoms.visible.VisibleCell;
import burai.com.math.Lattice;
import javafx.event.EventHandler;
import javafx.geometry.Point3D;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;

public class AtomsViewer extends AtomsViewerBase<Group> {

    private boolean compassMode;

    private boolean initiallyOperated;

    private ViewerCell viewerCell;
    private ViewerSample viewerSample;
    private ViewerXYZAxis viewerXYZAxis;
    private ViewerCompass viewerCompass;

    private AtomsLogger logger;

    private Design design;
    private List<Node> backgroundNodes;
    private boolean showingLegend;
    private boolean showingAxis;
    private boolean showingCell;

    private boolean busyLinkedViewers;
    private List<AtomsViewer> linkedViewers;

    public AtomsViewer(Cell cell, double size) {
        this(cell, size, size);
    }

    public AtomsViewer(Cell cell, double size, boolean silent) {
        this(cell, size, size, silent);
    }

    public AtomsViewer(Cell cell, double width, double height) {
        this(cell, width, height, false);
    }

    public AtomsViewer(Cell cell, double width, double height, boolean silent) {
        super(cell, width, height);

        this.compassMode = false;

        this.viewerCell = new ViewerCell(this, this.cell, silent);
        this.viewerSample = new ViewerSample(this, this.cell);
        this.viewerXYZAxis = new ViewerXYZAxis(this);
        if (!silent) {
            this.viewerCompass = new ViewerCompass(this.viewerCell);
        } else {
            this.viewerCompass = null;
        }

        if (!silent) {
            this.logger = new AtomsLogger(this.cell);
        } else {
            this.logger = null;
        }

        this.design = this.createDesign();
        this.backgroundNodes = null;
        this.showingLegend = this.design.isShowingLegend();
        this.showingAxis = this.design.isShowingAxis();
        this.showingCell = this.design.isShowingCell();

        this.busyLinkedViewers = false;
        this.linkedViewers = null;

        this.sceneRoot.getChildren().add(this.viewerCell.getNode());
        this.sceneRoot.getChildren().add(this.viewerSample.getNode());
        this.sceneRoot.getChildren().add(this.viewerXYZAxis.getNode());
        if (!silent) {
            this.sceneRoot.getChildren().add(this.viewerCompass.getNode());
        }

        ViewerEventManager viewerEventManager = new ViewerEventManager(this, silent);
        this.subScene.setOnMousePressed(viewerEventManager.getMousePressedHandler());
        this.subScene.setOnMouseDragged(viewerEventManager.getMouseDraggedHandler());
        this.subScene.setOnMouseReleased(viewerEventManager.getMouseReleasedHandler());
        this.subScene.setOnScroll(viewerEventManager.getScrollHandler());

        this.subScene.setOnKeyPressed(event -> {
            EventHandler<KeyEvent> handler = viewerEventManager.getKeyPressedHandler();
            if (handler != null) {
                handler.handle(event);
            }
            if (this.subKeyHandler != null) {
                subKeyHandler.handle(event);
            }
        });

        this.applyDesign();

        this.initiallyOperated = false;
        this.initialOperations();
    }

    private Design createDesign() {
        Design design = new Design();

        if (this.cell != null) {
            if (this.cell.hasProperty(CellProperty.MOLECULE)) {
                if (this.cell.booleanProperty(CellProperty.MOLECULE)) {
                    design.setShowingCell(false);
                }
            }
        }

        design.addOnBackColorChanged(color -> {
            if (color == null) {
                return;
            }

            this.subScene.setFill(color);

            if (this.backgroundNodes != null && !this.backgroundNodes.isEmpty()) {
                String strColor = color.toString();
                strColor = strColor == null ? null : strColor.replaceAll("0x", "#");
                if (strColor != null) {
                    for (Node node : this.backgroundNodes) {
                        node.setStyle("-fx-background-color: " + strColor);
                    }
                }
            }
        });

        design.addOnShowingLegendChanged(showing -> {
            if (this.showingLegend != showing) {
                this.showingLegend = showing;
                this.viewerSample.getNode().setVisible(showing);
            }
        });

        design.addOnShowingAxisChanged(showing -> {
            if (this.showingAxis != showing) {
                this.showingAxis = showing;
                this.viewerXYZAxis.getNode().setVisible(showing);
            }
        });

        design.addOnShowingCellChanged(showing -> {
            if (this.showingCell != showing) {
                this.showingCell = showing;
                this.setCellToCenter();
            }
        });

        return design;
    }

    private void applyDesign() {
        this.subScene.setFill(this.design.getBackColor());
        this.viewerSample.getNode().setVisible(this.design.isShowingLegend());
        this.viewerXYZAxis.getNode().setVisible(this.design.isShowingAxis());
    }

    private void initialOperations() {
        if (this.cell == null) {
            return;
        }
{'fraction_non_alphanumeric': 0.10640522875816993, 'fraction_numerical': 0.0010457516339869282, 'mean_word_length': 2.404331059032928, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3855814', 'n_tokens_mistral': 3316, 'n_tokens_neox': 3225, 'n_words': 597}