Search in sources :

Example 1 with MoveMomentCommand

use of controller.command.MoveMomentCommand in project uPMT by coco35700.

the class MainViewTransformations method addPaneOnDragListener.

/*
	 * Listener du panel qu'il y a entre plusieurs moment
	 * */
public static void addPaneOnDragListener(DropPane p, Main main) {
    p.setOnDragExited(new EventHandler<DragEvent>() {

        @Override
        public void handle(DragEvent event) {
            p.setBackground(new Background(new BackgroundFill(Color.TRANSPARENT, CornerRadii.EMPTY, Insets.EMPTY)));
        }
    });
    p.setOnDragOver(new EventHandler<DragEvent>() {

        public void handle(DragEvent event) {
            // setting the drag autorizations
            boolean cond = true;
            MomentExperience draggedMoment = null;
            try {
                draggedMoment = (MomentExperience) Serializer.deserialize((byte[]) event.getDragboard().getContent(MomentExpVBox.df));
                int pos = p.getCol();
                int i = ((Integer) event.getDragboard().getContent(MomentExpVBox.realCol));
                if (draggedMoment != null) {
                    if (!draggedMoment.hasParent()) {
                        // int pos = main.getGrid().getChildren().indexOf(p)/2;
                        if (p.hasMomentParent()) {
                            if (p.getMomentParent().getMoment().equals(draggedMoment))
                                cond = false;
                            else if (p.getMomentParent().isAChildOf(draggedMoment))
                                cond = false;
                        } else
                            cond = ((pos - i) > 1) || ((pos - i) < 0);
                    // System.out.println("pos-i="+(pos-i)+" -- 1 < "+(pos-i)+" < 0 ?"+cond);
                    } else {
                        if (p.hasMomentParent()) {
                            if (p.getMomentParent().getMoment().getID() == draggedMoment.getParentID()) {
                                cond = ((pos - i) > 1) || ((pos - i) < 0);
                            }
                        } else
                            cond = true;
                    }
                } else
                    cond = false;
            } catch (Exception e) {
            // System.out.println("null");
            }
            // System.out.println(Math.abs(i - pos));
            if ((event.getDragboard().getString().equals("ajoutMoment")) || (event.getDragboard().getString().equals("moveMoment") && cond)) {
                event.acceptTransferModes(TransferMode.ANY);
                // p.setBackground(new Background(new BackgroundFill(Color.GRAY, CornerRadii.EMPTY, Insets.EMPTY)));
                p.onDragOver(event.getDragboard().getString());
            }
            event.consume();
        }
    });
    p.setOnDragDropped(new EventHandler<DragEvent>() {

        public void handle(DragEvent event) {
            p.onDragExited();
            if (main.getCurrentDescription().getNumberOfMoments() == 0) {
                p.emptyProjectPane();
            }
            // int pos = main.getGrid().getColumnIndex(p)/2;
            int pos = p.getCol();
            if (event.getDragboard().getString().equals("ajoutMoment")) {
                AddMomentCommand cmd = null;
                MomentExperience moment = moment = new MomentExperience();
                if (event.getDragboard().getContent(DataFormat.HTML) != null) {
                    moment.setDescripteme((String) event.getDragboard().getContent(DataFormat.HTML));
                }
                if (p.hasMomentParent())
                    cmd = new AddMomentCommand(pos, moment, p.getMomentParent().getMoment(), main);
                else
                    cmd = new AddMomentCommand(pos, moment, main);
                cmd.execute();
                UndoCollector.INSTANCE.add(cmd);
            }
            if (event.getDragboard().getString().equals("moveMoment")) {
                // System.out.println("Panel, move a moment in index "+pos);
                MomentExperience serial = null;
                try {
                    serial = (MomentExperience) Serializer.deserialize((byte[]) event.getDragboard().getContent(MomentExpVBox.df));
                } catch (ClassNotFoundException | IOException e) {
                    e.printStackTrace();
                }
                if (p.hasMomentParent()) {
                    MoveMomentToMomentCommand cmd = new MoveMomentToMomentCommand(serial, p.getMomentParent().getMoment(), pos, main);
                    cmd.execute();
                    UndoCollector.INSTANCE.add(cmd);
                } else {
                    MoveMomentCommand cmd = new MoveMomentCommand(serial, pos, main);
                    cmd.execute();
                    UndoCollector.INSTANCE.add(cmd);
                }
            }
            // p.setBackground(new Background(new BackgroundFill(Color.TRANSPARENT, CornerRadii.EMPTY, Insets.EMPTY)));
            event.consume();
        }
    });
    p.setOnDragExited(new EventHandler<DragEvent>() {

        @Override
        public void handle(DragEvent arg0) {
            p.onDragExited();
            if (main.getCurrentDescription().getNumberOfMoments() == 0) {
                p.emptyProjectPane();
            }
        }
    });
}
Also used : DragEvent(javafx.scene.input.DragEvent) AddMomentCommand(controller.command.AddMomentCommand) Background(javafx.scene.layout.Background) BackgroundFill(javafx.scene.layout.BackgroundFill) MoveMomentCommand(controller.command.MoveMomentCommand) MomentExperience(model.MomentExperience) MoveMomentToMomentCommand(controller.command.MoveMomentToMomentCommand) IOException(java.io.IOException)

Aggregations

AddMomentCommand (controller.command.AddMomentCommand)1 MoveMomentCommand (controller.command.MoveMomentCommand)1 MoveMomentToMomentCommand (controller.command.MoveMomentToMomentCommand)1 IOException (java.io.IOException)1 DragEvent (javafx.scene.input.DragEvent)1 Background (javafx.scene.layout.Background)1 BackgroundFill (javafx.scene.layout.BackgroundFill)1 MomentExperience (model.MomentExperience)1