Skip to content

Commit

Permalink
#10 Draggable tabs by @gforman44
Browse files Browse the repository at this point in the history
  • Loading branch information
gillius committed May 15, 2016
1 parent 9964e50 commit 768a85f
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2013 Jason Winnebeck
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.gillius.jfxutils.examples;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import org.gillius.jfxutils.tab.TabUtil;

/**
* Demonstrate draggable Tabs from one TabPane to another, allowing the user to rearrange their GUI layout.
*
* Note: For this to work, instead of using new Tab("Label") you should use
* <pre>
* t = new Tab();
* t.setGraphic(new Label("Label"));
*
* @author gforman44
*/
public class DemoDragTabs extends Application {
private static Tab makeTab( String tabName, String tabContentDummy) {
Tab t = new Tab();
t.setGraphic(new Label(tabName)); // NOTE: must use setGraphic for this, not just a text label
t.setContent(new Label(tabContentDummy));
TabUtil.makeDraggable(t); ///////////////// see
return t;
}

@Override
public void start(Stage primaryStage) {

Tab f = makeTab("Files", "File system view here");
Tab t = makeTab("Type Hierarchy","Type hierarchy view here");
Tab d = makeTab("Debug","Debug view here");

Tab p = makeTab("Properties","Ah, the ubiquitous 'properties' panel");
Tab c = makeTab("Console","Console output here");
Tab o = makeTab("Outline","Outline of fields/methods view here");

TabPane left = new TabPane(f,t,d);
TabUtil.makeDroppable(left); //////////////// see

TabPane right = new TabPane(p,c,o);
TabUtil.makeDroppable(right); /////////////// see

left.setStyle("-fx-border-color: black;");
right.setStyle("-fx-border-color: black;");

BorderPane main = new BorderPane();
main.setPadding(new Insets(0, 20, 0, 20));
main.setTop(new Label("Menubar and toolbars"));
main.setLeft(left);
main.setCenter(new Label("Central work area here"));
main.setRight(right);
main.setBottom(new Label("Statusbar"));

primaryStage.setScene(new Scene(main, 800, 600));
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}
}
79 changes: 79 additions & 0 deletions jfxutils/src/main/java/org/gillius/jfxutils/tab/TabUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2013 Jason Winnebeck
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.gillius.jfxutils.tab;

import javafx.event.EventHandler;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.input.*;

/**
* @author gforman44
*/
public class TabUtil {
public static final DataFormat TAB_TYPE = new DataFormat("nonserializableObject/JfxTab");

public static Tab newDraggableTab(String label) {
Tab rr = new Tab();
rr.setGraphic(new Label(label));
makeDraggable(rr);
return rr;
}

/** global for drag-n-drop of non-serializable type */
private static Tab dndTab;

public static void makeDraggable( final Tab tab) {
tab.getGraphic().setOnDragDetected( new EventHandler<MouseEvent>() {
@Override
public void handle( MouseEvent event ) {
Dragboard dragboard = tab.getGraphic().startDragAndDrop( TransferMode.MOVE );
ClipboardContent clipboardContent = new ClipboardContent();
clipboardContent.put( TAB_TYPE, 1 );
dndTab = tab;
dragboard.setContent( clipboardContent );
event.consume();
}
} );
}

public static void makeDroppable( final TabPane tabPane) {
tabPane.setOnDragOver( new EventHandler<DragEvent>() {
@Override
public void handle( DragEvent event ) {
if ( event.getDragboard().hasContent( TAB_TYPE )
&& dndTab.getTabPane() != tabPane ) {// && different from source location
event.acceptTransferModes( TransferMode.MOVE );
event.consume();
}
}
} );
tabPane.setOnDragDropped( new EventHandler<DragEvent>() {
@Override
public void handle( DragEvent event ) {
if ( event.getDragboard().hasContent( TAB_TYPE )
&& dndTab.getTabPane() != tabPane ) {// && different from source location
dndTab.getTabPane().getTabs().remove( dndTab );
tabPane.getTabs().add( dndTab );
event.setDropCompleted( true );
event.consume();
}
}
} );
}
}

0 comments on commit 768a85f

Please sign in to comment.