Skip to content

Commit 7df1078

Browse files
author
Hugo Miguel Ferreira
committed
Backported changes from javaFXMill project.
1 parent 810b57d commit 7df1078

File tree

5 files changed

+113
-87
lines changed

5 files changed

+113
-87
lines changed

HelloWorldScala/src/button/ButtonApp.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
package button;
1+
package button
22

3-
import javafx.application.Application;
4-
import javafx.stage.Stage;
5-
import javafx.scene.Scene;
6-
import javafx.scene.layout.VBox;
7-
import javafx.scene.control.Button;
3+
import javafx.application.Application
4+
import javafx.stage.Stage
5+
import javafx.scene.Scene
6+
import javafx.scene.layout.VBox
7+
import javafx.scene.control.Button
88

99
class ButtonApp extends Application {
1010

@@ -17,7 +17,7 @@ class ButtonApp extends Application {
1717
var scene = new Scene(box)
1818
stage.setScene(scene)
1919
stage.setTitle("Just a Scala button")
20-
stage.show();
20+
stage.show()
2121
}
2222

2323
def launchIt():Unit={

HelloWorldScala/src/button/Main.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package button;
1+
package button
22

33
/**
44
*

HelloWorldScala/src/helloworld/HelloWorld.scala

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
package helloworld;
1+
package helloworld
22

3-
import javafx.application.Application;
4-
import javafx.event.ActionEvent;
5-
import javafx.event.EventHandler;
6-
import javafx.scene.Scene;
7-
import javafx.scene.control.Button;
8-
import javafx.scene.layout.StackPane;
9-
import javafx.stage.Stage;
3+
// cSpell:ignore helloworld
4+
5+
import javafx.application.Application
6+
import javafx.event.ActionEvent
7+
import javafx.event.EventHandler
8+
import javafx.scene.Scene
9+
import javafx.scene.control.Button
10+
import javafx.scene.layout.StackPane
11+
import javafx.stage.Stage
1012

1113
/**
1214
*
@@ -20,23 +22,20 @@ import javafx.stage.Stage;
2022
*/
2123
class HelloWorld extends Application {
2224

23-
@Override
24-
def start(primaryStage: Stage) = {
25-
primaryStage.setTitle("Hello Scala World!")
26-
val btn = new Button()
27-
btn.setText("Say 'Hello Scala World'")
28-
btn.setOnAction(new EventHandler[ActionEvent]() {
29-
30-
@Override
31-
def handle(event: ActionEvent) = {
32-
System.out.println("Hello Scala World!");
33-
}
34-
})
25+
override def start(primaryStage: Stage) = {
26+
primaryStage.setTitle("Hello Scala World!")
27+
val btn = new Button()
28+
btn.setText("Say 'Hello Scala World'")
29+
btn.setOnAction(new EventHandler[ActionEvent]() {
30+
override def handle(event: ActionEvent) = {
31+
System.out.println("Hello Scala World!")
32+
}
33+
})
3534

36-
val root = new StackPane()
37-
root.getChildren().add(btn)
38-
primaryStage.setScene(new Scene(root, 300, 250))
39-
primaryStage.show()
35+
val root = new StackPane()
36+
root.getChildren().add(btn)
37+
primaryStage.setScene(new Scene(root, 300, 250))
38+
primaryStage.show()
4039
}
4140
}
4241

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# javaFXClientMill
2-
Examples of JavaFX using Scala
2+
Examples of Scala using JavaFX

build.sc

Lines changed: 81 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,62 @@ val mUnitVersion = "0.7.27"
3333
* overriding certain modules on boot-up. This allows for example the use the
3434
* TestFX for use in headless UI testing.
3535
*
36+
* To add other libraries as modules see `controlsFXModule` as an example.
37+
*
3638
* ./mill mill.scalalib.GenIdea/idea
39+
*
40+
* TODO: https://stackoverflow.com/questions/46616520/list-modules-in-jar-file
3741
*
3842
* @see https://github.com/com-lihaoyi/mill/pull/775#issuecomment-826091576
3943
*/
40-
trait javaFX extends ScalaModule with JavaModule {
41-
def scalaVersion = T{ ScalaVersion }
44+
trait OpenJFX extends JavaModule {
45+
46+
// Modules
47+
48+
val BASE_ = s"base"
49+
val CONTROLS_ = s"controls"
50+
val FXML_ = s"fxml"
51+
val GRAPHICS_ = s"graphics"
52+
val MEDIA_ = s"media"
53+
val SWING_ = s"swing"
54+
val WEB_ = s"web"
55+
val CONTROLSFX_ = s"controlsfx"
56+
57+
// Extra modules
58+
// Note that the module name and the library name are not the same
59+
val controlsFXModule = "org.controlsfx.controls"
60+
61+
// Module libraries
62+
val BASE = s"org.openjfx:javafx-$BASE_:$javaFXVersion"
63+
val CONTROLS = s"org.openjfx:javafx-$CONTROLS_:$javaFXVersion"
64+
val FXML = s"org.openjfx:javafx-$FXML_:$javaFXVersion"
65+
val GRAPHICS = s"org.openjfx:javafx-$GRAPHICS_:$javaFXVersion"
66+
val MEDIA = s"org.openjfx:javafx-$MEDIA_:$javaFXVersion"
67+
val SWING = s"org.openjfx:javafx-$SWING_:$javaFXVersion"
68+
val WEB = s"org.openjfx:javafx-$WEB_:$javaFXVersion"
69+
val CONTROLSFX = s"org.controlsfx:$CONTROLSFX_:$controlsFXVersion"
4270

71+
// OpenFX/JavaFX libraries
72+
val javaFXModuleNames = Seq(BASE_, CONTROLS_, FXML_, GRAPHICS_, MEDIA_, SWING_, WEB_)
73+
74+
75+
/* TODO: we need a better way to identify modules in the JARs
76+
see: https://stackoverflow.com/questions/46616520/list-modules-in-jar-file
77+
see: https://www.daniweb.com/programming/software-development/threads/291837/best-way-executing-jar-from-java-code-then-killing-parent-java-code
78+
see: https://in.relation.to/2017/12/06/06-calling-jdk-tools-programmatically-on-java-9/
79+
see: https://www.pluralsight.com/guides/creating-opening-jar-files-java-programming-language
80+
see: https://stackoverflow.com/questions/320510/viewing-contents-of-a-jar-file
81+
see: https://www.baeldung.com/java-compress-and-uncompress
82+
see: https://github.com/srikanth-lingala/zip4j
83+
// List of modules (note that a single Jar may have ore than one module)
84+
val modules = javaFXModuleNames.map(n => n -> s"org.openjfx:javafx-$n:$javaFXVersion") // OpenFX
85+
.toMap
86+
++ // Other modules
87+
Map( "controlsfx" -> s"org.controlsfx:controlsfx:$controlsFXVersion") // ControlsFX
88+
println(modules)
89+
*/
90+
91+
// TODO: after version 0.10.0 iof Mill put test in the managed/unmanaged classes
4392
val ivyMunit = ivy"org.scalameta::munit::0.7.27"
4493
val ivyMunitInterface = "munit.Framework"
4594

@@ -55,58 +104,28 @@ trait javaFX extends ScalaModule with JavaModule {
55104
Some((_: coursier.core.Resolution).withOsInfo(coursier.core.Activation.Os.fromProperties(sys.props.toMap)))
56105
}
57106

58-
/**
59-
* We setup JavaFX using managed libraries pretty much as any other library.
60-
* However, we must use the [[resolutionCustomizer]] to ensure that the proper
61-
* OS dependent libraries are correctly downloaded. In order to ise JavaFX or
62-
* OpenJFX we must include these libraries in the module path and module names
63-
* to the JVM parameters. We can automate this via [[forkArgs]].
64-
*
65-
* Here we list the dependencies via the Mill `ivy` macro (uses Coursier). We
66-
* could automate this too because the naming of the libraries and models uses
67-
* a consistent convention. We leave that as an exercise to the reader.
68-
*
69-
* Note that any dependencies are loaded automatically so no need to add
70-
* all the JavaFX libraries. We have these here as an example.
71-
*
72-
* @return an aggregation of the dependencies
73-
*/
74-
override def ivyDeps = Agg(
75-
/*ivy"org.openjfx:javafx-base:$javaFXVersion",
76-
ivy"org.openjfx:javafx-controls:$javaFXVersion",
77-
ivy"org.openjfx:javafx-fxml:$javaFXVersion",
78-
ivy"org.openjfx:javafx-graphics:$javaFXVersion",
79-
ivy"org.openjfx:javafx-media:$javaFXVersion",
80-
ivy"org.openjfx:javafx-swing:$javaFXVersion",
81-
ivy"org.openjfx:javafx-web:$javaFXVersion",*/
82-
ivy"org.openjfx:javafx-controls:$javaFXVersion",
83-
ivy"org.controlsfx:controlsfx:$controlsFXVersion"
84-
)
85-
86-
87-
// OpenFX/JavaFX libraries
88-
//private lazy val javaFXModuleNames = Seq("base", "controls", "fxml", "graphics", "media", "swing", "web")
89-
// Extra OpenFX library
90-
private lazy val controlsFXModuleName = "org.controlsfx.controls"
91-
92107
/**
93108
* Here we setup the Java modules so that they can be loaded prior to
94109
* application boot. We can indicate which modules are visible and even opt
95110
* to substitute some of those. For example using TestFX to allow for headless
96111
* testing.
97112
*
113+
* Note that with managed libraries, we may pull in additional modules. So we
114+
* attempt here to identify (via naming convention), which libraries are modules.
115+
* These corresponding modules are then added to the JVM command line.
116+
*
98117
* @return the list of parameters for the JVM
99118
*/
100119
override def forkArgs: Target[Seq[String]] = T {
101120
// get the managed libraries
102-
val unmanaged: Loose.Agg[PathRef] = runClasspath()
121+
val allLibs: Loose.Agg[PathRef] = runClasspath()
103122
// get the OpenJFX and related managed libraries
104-
val s: Loose.Agg[String] = unmanaged.map(_.path.toString())
105-
.filter{
106-
s =>
107-
val t= s.toLowerCase()
108-
t.contains("javafx") || t.contains("controlsfx")
109-
}
123+
val s: Loose.Agg[String] = allLibs.map(_.path.toString())
124+
.filter{
125+
s =>
126+
val t= s.toLowerCase()
127+
t.contains("javafx") || t.contains("controlsfx")
128+
}
110129

111130
// Create the JavaFX module names (convention is amenable to automation)
112131
import scala.util.matching.Regex
@@ -119,7 +138,7 @@ trait javaFX extends ScalaModule with JavaModule {
119138
.map(_.get)
120139
// Now generate the module names
121140
val modulesNames = javaFXModules.map( m => s"javafx.$m") ++
122-
Seq(controlsFXModuleName) // no standard convention, so add it manually
141+
Seq(controlsFXModule) // no standard convention, so add it manually
123142

124143
// Add to the modules list
125144
Seq(
@@ -134,36 +153,44 @@ trait javaFX extends ScalaModule with JavaModule {
134153
}
135154

136155

156+
// TODO: after version 0.10.0 of Mill put test in the managed/unmanaged classes
137157
object test extends Tests {
138158

159+
// TODO: after version 0.10.0 of Mill remove this
160+
// sse https://github.com/com-lihaoyi/mill/issues/1406
139161
override def resolutionCustomizer: Task[Option[Resolution => Resolution]] = T.task {
140162
Some((_: coursier.core.Resolution).withOsInfo(coursier.core.Activation.Os.fromProperties(sys.props.toMap)))
141163
}
142-
143-
//override def ivyDeps = Agg(ivyMunit)
144-
// sse https://github.com/com-lihaoyi/mill/issues/1406
145-
override def ivyDeps = Agg(
146-
ivy"org.openjfx:javafx-controls:$javaFXVersion",
147-
ivy"org.controlsfx:controlsfx:$controlsFXVersion",
148-
ivyMunit)
149164

150165
// https://github.com/com-lihaoyi/mill#097---2021-05-14
151166
//def testFrameworks = Seq(ivyMunitInterface)
152167
def testFramework = ivyMunitInterface
153168
}
154169

155170
}
171+
object HelloWorldJava extends OpenJFX {
172+
173+
override def mainClass: T[Option[String]] = Some("helloworld.HelloWorld")
174+
175+
override def ivyDeps = Agg(
176+
ivy"$CONTROLS",
177+
ivy"$CONTROLSFX"
178+
)
156179

157-
object HelloWorldJava extends javaFX {
158180

159-
override def mainClass: T[Option[String]] = Some("helloworld.HelloWorld")
160181

161182
}
162183

163-
object HelloWorldScala extends javaFX {
184+
185+
object HelloWorldScala extends OpenJFX with ScalaModule {
186+
def scalaVersion = T{ ScalaVersion }
164187

165188
override def mainClass: T[Option[String]] = Some("helloworld.HelloWorld")
166189

190+
override def ivyDeps = Agg(
191+
ivy"$CONTROLS",
192+
ivy"$CONTROLSFX"
193+
)
167194
}
168195

169196

0 commit comments

Comments
 (0)