001/**
002 * Get more info at : www.jrebirth.org .
003 * Copyright JRebirth.org © 2011-2013
004 * Contact : sebastien.bordes@jrebirth.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.jrebirth.af.component.ui.tab;
019
020import javafx.animation.ScaleTransition;
021import javafx.scene.Node;
022import javafx.scene.control.ToggleButton;
023import javafx.scene.input.DragEvent;
024import javafx.scene.input.Dragboard;
025import javafx.scene.input.TransferMode;
026import javafx.scene.layout.Pane;
027import javafx.util.Duration;
028
029import org.jrebirth.af.component.behavior.dockable.data.Dockable;
030import org.jrebirth.af.component.ui.CustomDataFormat;
031import org.jrebirth.af.core.ui.adapter.AbstractDefaultAdapter;
032import org.jrebirth.af.core.ui.adapter.DragAdapter;
033
034/**
035 * The Class TabTargetDragAdapter.
036 */
037class TabbedPaneTargetDragAdapter extends AbstractDefaultAdapter<TabbedPaneController> implements DragAdapter {
038
039    /**
040     * {@inheritDoc}
041     */
042    @Override
043    public void dragOver(final DragEvent dragEvent) {
044        // System.out.println("drag OVER");
045
046        if (dragEvent.getGestureSource() != getController().view().getBox()
047                && dragEvent.getDragboard().hasContent(CustomDataFormat.DOCKABLE)) {
048
049            dragEvent.acceptTransferModes(TransferMode.MOVE);
050
051            getController().view().drawMarker((ToggleButton) dragEvent.getGestureSource(), dragEvent.getX(), dragEvent.getY());
052
053            dragEvent.consume();
054        }
055
056    }
057
058    /**
059     * {@inheritDoc}
060     */
061    @Override
062    public void dragEnteredTarget(final DragEvent dragEvent) {
063        // System.out.println("drag ENTERED TARGET");
064
065    }
066
067    /**
068     * {@inheritDoc}
069     */
070    @Override
071    public void dragEntered(final DragEvent dragEvent) {
072        // System.out.println("drag ENTERED");
073        if (dragEvent.getGestureSource() != getController().view().getBox()
074                && dragEvent.getDragboard().hasContent(CustomDataFormat.DOCKABLE)) {
075
076            // getController().getView().drawMarker(dragEvent.getX(), dragEvent.getY());
077
078            // getController().getView().getBox().setBorder(new Border(new BorderStroke(Color.AQUAMARINE, BorderStrokeStyle.SOLID, new CornerRadii(1.0), BorderStroke.THICK)));
079        }
080
081        dragEvent.consume();
082    }
083
084    /**
085     * {@inheritDoc}
086     */
087    @Override
088    public void dragExited(final DragEvent dragEvent) {
089        // System.out.println("drag EXITED");
090        if (dragEvent.getGestureSource() != getController().view().getBox() &&
091                dragEvent.getDragboard().hasContent(CustomDataFormat.DOCKABLE)) {
092
093            getController().view().removeMarker();
094            // getController().getView().getBox().setBorder(null);
095        }
096
097        dragEvent.consume();
098    }
099
100    /**
101     * {@inheritDoc}
102     */
103    @Override
104    public void dragExitedTarget(final DragEvent dragEvent) {
105        // System.out.println("drag EXIT TARGET");
106        // if (/*
107        // * dragEvent.getGestureSource() != getController().getView().getBox() &&
108        // */dragEvent.getDragboard().hasContent(CustomDataFormat.TAB)) {
109        //
110        // getController().getView().removeMarker();
111        // // getController().getView().getBox().setBorder(null);
112        // }
113        //
114        // dragEvent.consume();
115    }
116
117    /**
118     * {@inheritDoc}
119     */
120    @Override
121    public void dragDropped(final DragEvent dragEvent) {
122
123        // System.out.println("drag DROPPED");
124
125        /* data dropped */
126        /* if there is a string data on dragboard, read it and use it */
127        final Dragboard db = dragEvent.getDragboard();
128        boolean success = false;
129
130        if (db.hasContent(CustomDataFormat.DOCKABLE)) {
131
132            final Dockable serializedTab = (Dockable) db.getContent(CustomDataFormat.DOCKABLE);
133            final ToggleButton b = getController().view().getButtonByTab(serializedTab);
134
135            final Pane targetBox = getController().view().getBox();
136
137            int idx = getController().view().removeMarker();
138
139            System.out.println("Add tab " + serializedTab.name() + " at " + idx);
140
141            if (targetBox.getChildren().contains(b)) {
142
143                final Dockable realTab = (Dockable) b.getUserData();
144                final int currentIdx = targetBox.getChildren().indexOf(b);
145                
146                // Same tab is moved at the end so we have to remove one to the marker position
147                if (currentIdx < idx) {
148                         idx--;
149                }
150                // targetBox.getChildren().remove(b);
151                // targetBox.getChildren().add(idx, b);
152                // getController().getView().removeMarker();
153                if (currentIdx != idx) {
154                    getController().model().object().tabs().remove(realTab);
155
156                    getController().model().object().tabs().add(idx, realTab);
157
158                    b.fire();
159                }
160            } else {
161
162                final Node n = (Node) dragEvent.getGestureSource();
163
164                final ScaleTransition st = new ScaleTransition(Duration.millis(600));
165                st.setNode(n);
166                st.setFromX(1.0);
167                st.setToX(0.0);
168                st.setOnFinished(event -> ((Pane) n.getParent()).getChildren().remove(n));
169
170                st.play();
171
172                getController().model().doInsertTab(idx, serializedTab, null);
173            }
174            success = true;
175        }
176        /*
177         * let the source know whether the string was successfully transferred and used
178         */
179        dragEvent.setDropCompleted(success);
180
181        dragEvent.consume();
182    }
183
184    /**
185     * {@inheritDoc}
186     */
187    @Override
188    public void dragDone(final DragEvent dragEvent) {
189
190        // System.out.println("drag DONE");
191
192        /* the drag and drop gesture ended */
193        /* if the data was successfully moved, clear it */
194        // if (dragEvent.getTransferMode() == TransferMode.MOVE) {
195        // Button b = ((Button) dragEvent.getGestureSource());
196        // ((Pane) b.getParent()).getChildren().remove(b);
197        // }
198        dragEvent.consume();
199    }
200
201}