Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #7: Create a new undocked DockNode #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dockfx.jar
dockfxdemo.jar
*.jar
*.db
.idea/
.git/modules
/bin
target
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,23 @@
<url>https://github.com/RobertBColton/DockFX.git</url>

<properties>
<junit.version>4.12</junit.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<netbeans.checkstyle.format>true</netbeans.checkstyle.format>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<fileExtensions>java, properties, xml</fileExtensions>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/org/dockfx/DockNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,17 @@ public void setFloating(boolean floating, Point2D translation) {
if (this.isDecorated()) {
Window owner = stage.getOwner();
stagePosition = floatScene.add(new Point2D(owner.getX(), owner.getY()));
} else {
} else if (floatScreen != null) {
// using coordinates the component was previously in (if available)
stagePosition = floatScreen;
} else {
// using the center of the screen if no relative position is available
Rectangle2D primScreenBounds = Screen.getPrimary().getVisualBounds();
double centerX = (primScreenBounds.getWidth() - Math.max(getWidth(), getMinWidth())) / 2;
double centerY = (primScreenBounds.getHeight() - Math.max(getHeight(), getMinHeight())) / 2;
stagePosition = new Point2D(centerX, centerY);
}

if (translation != null) {
stagePosition = stagePosition.add(translation);
}
Expand Down Expand Up @@ -296,9 +304,6 @@ public void setFloating(boolean floating, Point2D translation) {
stage.setX(stagePosition.getX() - insetsDelta.getLeft());
stage.setY(stagePosition.getY() - insetsDelta.getTop());

stage.setMinWidth(borderPane.minWidth(this.getHeight()) + insetsWidth);
stage.setMinHeight(borderPane.minHeight(this.getWidth()) + insetsHeight);

borderPane.setPrefSize(this.getWidth() + insetsWidth, this.getHeight() + insetsHeight);

stage.setScene(scene);
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/org/dockfx/demo/DockFX.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import java.nio.file.Paths;
import java.util.Random;

import javafx.application.Platform;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import org.dockfx.DockNode;
import org.dockfx.DockPane;
import org.dockfx.DockPos;
Expand Down Expand Up @@ -67,7 +70,7 @@ public void start(Stage primaryStage) {
}

// empty tabs ensure that dock node has its own background color when floating
tabs.getTabs().addAll(new Tab("Tab 1", htmlEditor), new Tab("Tab 2"), new Tab("Tab 3"));
tabs.getTabs().addAll(new Tab("Tab 1"), new Tab("Tab 2"), new Tab("Tab 3"));

TableView<String> tableView = new TableView<String>();
// this is why @SupressWarnings is used above
Expand Down Expand Up @@ -122,6 +125,11 @@ public void start(Stage primaryStage) {
// https://bugs.openjdk.java.net/browse/JDK-8132900
DockPane.initializeDefaultUserAgentStylesheet();

DockNode welcomeDock = new DockNode(htmlEditor, "Welcome", new ImageView(dockImage));
welcomeDock.setMinWidth(600);
welcomeDock.setMinHeight(400);
welcomeDock.setFloating(true);

// TODO: after this feel free to apply your own global stylesheet using the StyleManager class
}

Expand Down
21 changes: 21 additions & 0 deletions src/test/java/floating/FloatingDock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package floating;

import javafx.scene.control.Label;
import org.dockfx.DockNode;
import org.junit.Rule;
import org.junit.Test;
import utils.JavaFXThreadingRule;

import static org.junit.Assert.assertTrue;

public class FloatingDock {
@Rule
public JavaFXThreadingRule javafxRule = new JavaFXThreadingRule();

@Test
public void canCreateFloatingDock() {
final DockNode testDock = new DockNode(new Label(), "Some Label");
testDock.setFloating(true);
assertTrue(testDock.isFloating());
}
}
102 changes: 102 additions & 0 deletions src/test/java/utils/JavaFXThreadingRule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package utils;

import java.util.concurrent.CountDownLatch;

import javax.swing.SwingUtilities;

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;

import org.junit.Rule;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

/**
* A JUnit {@link Rule} for running tests on the JavaFX thread and performing
* JavaFX initialisation. To include in your test case, add the following code:
*
* <pre>
* {@literal @}Rule
* public JavaFXThreadingRule jfxRule = new JavaFXThreadingRule();
* </pre>
*
* @author Andy Till
*
*/
public class JavaFXThreadingRule implements TestRule {

/**
* Flag for setting up the JavaFX, we only need to do this once for all tests.
*/
private static boolean jfxIsSetup;

@Override
public Statement apply(Statement statement, Description description) {

return new OnJFXThreadStatement(statement);
}

private static class OnJFXThreadStatement extends Statement {

private final Statement statement;

public OnJFXThreadStatement(Statement aStatement) {
statement = aStatement;
}

private Throwable rethrownException = null;

@Override
public void evaluate() throws Throwable {

if(!jfxIsSetup) {
setupJavaFX();

jfxIsSetup = true;
}

final CountDownLatch countDownLatch = new CountDownLatch(1);

Platform.runLater(new Runnable() {
@Override
public void run() {
try {
statement.evaluate();
} catch (Throwable e) {
rethrownException = e;
}
countDownLatch.countDown();
}});

countDownLatch.await();

// if an exception was thrown by the statement during evaluation,
// then re-throw it to fail the test
if(rethrownException != null) {
throw rethrownException;
}
}

protected void setupJavaFX() throws InterruptedException {

long timeMillis = System.currentTimeMillis();

final CountDownLatch latch = new CountDownLatch(1);

SwingUtilities.invokeLater(new Runnable() {
public void run() {
// initializes JavaFX environment
new JFXPanel();

latch.countDown();
}
});

System.out.println("javafx initialising...");
latch.await();
System.out.println("javafx is initialised in " + (System.currentTimeMillis() - timeMillis) + "ms");
}

}
}