Skip to content

Commit

Permalink
Add recipe #6 about JavaFX
Browse files Browse the repository at this point in the history
  • Loading branch information
fipro78 committed Jun 3, 2024
1 parent cb7cf2c commit e5f41ab
Show file tree
Hide file tree
Showing 14 changed files with 749 additions and 13 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,11 @@ The updated version is contained in this repository in [Topping Recipe](/tutoria
This is the _Decoration Recipe_, which means that we add the functionality that a user can change the behavior or the look&feel of an application by changing preferences. This is done by using Eclipse Preferences with a custom plugin that adapts a plain E4 preferences mechanism.

The tutorial is contained in this repository in [Decoration Recipe](/tutorials/Eclipse_RCP_Cookbook_Preferences.md)

## Recipe #6 - The Veggie Recipe (Add JavaFX controls to an SWT Eclipse 4 application)

This is the _Veggie Recipe_, which means that we use an *alternative* UI technology with our Eclipse application. So it is like an alternative to meat (SWT). But instead of doing a full SWT to JavaFX migration, we integrate JavaFX controls to the SWT based Eclipse application.

The original blog post can be found here: [Add JavaFX controls to a SWT Eclipse 4 application – Eclipse RCP Cookbook](https://www.codecentric.de/wissens-hub/blog/add-javafx-controls-swt-eclipse-4-application-eclipse-rcp-cookbook)
An updated was already published here: [Add JavaFX controls to a SWT Eclipse 4 application – Eclipse RCP Cookbook UPDATE](https://vogella.com/blog/add-javafx-controls-to-a-swt-eclipse-4-application-eclipse-rcp-cookbook-update/)
The most current updated version is contained in this repository in [Veggie Recipe](/tutorials/Eclipse_RCP_Cookbook_JavaFX.md)
2 changes: 1 addition & 1 deletion org.fipro.eclipse.tutorial.app/Application.e4xmi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<application:Application xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:application="http://www.eclipse.org/ui/2010/UIModel/application" xmlns:basic="http://www.eclipse.org/ui/2010/UIModel/application/ui/basic" xmlns:menu="http://www.eclipse.org/ui/2010/UIModel/application/ui/menu" xmi:id="_tv09MKYzEeSD7pZHnwEr_g" elementId="org.eclipse.e4.ide.application" bindingContexts="_tv09OaYzEeSD7pZHnwEr_g">
<children xsi:type="basic:TrimmedWindow" xmi:id="_tv09MaYzEeSD7pZHnwEr_g" elementId="org.eclipse.e4.window.main" label="Eclipse Cookbook Application" width="500" height="400">
<children xsi:type="basic:TrimmedWindow" xmi:id="_tv09MaYzEeSD7pZHnwEr_g" elementId="org.eclipse.e4.window.main" label="Eclipse Cookbook Application" width="800" height="600">
<children xsi:type="basic:PartSashContainer" xmi:id="_eJLHQKbJEeSD7pZHnwEr_g" elementId="org.fipro.eclipse.tutorial.app.main"/>
<mainMenu xmi:id="_TjcysAYtEe-e_PdO_TMKWw" elementId="org.eclipse.ui.main.menu">
<children xsi:type="menu:Menu" xmi:id="_fvhG8AYtEe-e_PdO_TMKWw" elementId="org.eclipse.ui.file.menu" label="File">
Expand Down
8 changes: 4 additions & 4 deletions org.fipro.eclipse.tutorial.inverter/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ Require-Bundle: org.eclipse.swt;bundle-version="3.125.0",
org.eclipse.jface;bundle-version="3.33.0",
org.eclipse.e4.ui.model.workbench;bundle-version="2.4.200.v20240109-1025"
Bundle-RequiredExecutionEnvironment: JavaSE-17
Import-Package: jakarta.annotation;version="[2.1.0,3.0.0]",
jakarta.inject;version="[2.0.0,3.0.0]",
Automatic-Module-Name: org.fipro.eclipse.tutorial.inverter
Model-Fragment: fragment.e4xmi
Import-Package: jakarta.annotation;version="[2.1.0,3.0.0)";resolution:=optional,
jakarta.inject;version="[2.0.0,3.0.0)",
org.eclipse.e4.core.di.annotations;version="[1.6.0,2.0.0]",
org.eclipse.e4.core.di.extensions;version="[0.16.0,1.0.0]",
org.eclipse.e4.core.services.events,
org.fipro.e4.service.preferences;version="[0.4.0,1.0.0]",
org.fipro.eclipse.tutorial.service.inverter;version="[1.0.0,2.0.0]"
Model-Fragment: fragment.e4xmi
Automatic-Module-Name: org.fipro.eclipse.tutorial.inverter
Bundle-ActivationPolicy: lazy
Service-Component: OSGI-INF/org.fipro.eclipse.tutorial.inverter.preferences.InverterPreferencesContribution.xml
5 changes: 4 additions & 1 deletion org.fipro.eclipse.tutorial.inverter/fragment.e4xmi
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<fragment:ModelFragments xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:basic="http://www.eclipse.org/ui/2010/UIModel/application/ui/basic" xmlns:fragment="http://www.eclipse.org/ui/2010/UIModel/fragment" xmi:id="_GBlIcP2CEe6ghYpA0qsMeA">
<imports xsi:type="basic:PartSashContainer" xmi:id="_Ywx9EP2DEe6ghYpA0qsMeA" elementId="org.fipro.eclipse.tutorial.app.main"/>
<fragments xsi:type="fragment:StringModelFragment" xmi:id="_HZoggP2CEe6ghYpA0qsMeA" featurename="children" parentElementId="org.fipro.eclipse.tutorial.app.main">
<elements xsi:type="basic:Part" xmi:id="_zS1fcP2DEe6ghYpA0qsMeA" elementId="org.fipro.eclipse.tutorial.inverter.part.0" contributionURI="bundleclass://org.fipro.eclipse.tutorial.inverter/org.fipro.eclipse.tutorial.inverter.part.InverterPart"/>
<elements xsi:type="basic:PartStack" xmi:id="_JxRIMBQpEe-ro58EMQ7HTQ" elementId="org.fipro.eclipse.tutorial.inverter.partstack.0">
<children xsi:type="basic:Part" xmi:id="_zS1fcP2DEe6ghYpA0qsMeA" elementId="org.fipro.eclipse.tutorial.inverter.part.inverterswt" contributionURI="bundleclass://org.fipro.eclipse.tutorial.inverter/org.fipro.eclipse.tutorial.inverter.part.InverterPart" label="Inverter (SWT)"/>
<children xsi:type="basic:Part" xmi:id="_RtVm4BQpEe-ro58EMQ7HTQ" elementId="org.fipro.eclipse.tutorial.inverter.part.inverterjavafx" contributionURI="bundleclass://org.fipro.eclipse.tutorial.inverter/org.fipro.eclipse.tutorial.inverter.part.InverterFXPart" label="Inverter (JavaFX)"/>
</elements>
</fragments>
</fragment:ModelFragments>
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package org.fipro.eclipse.tutorial.inverter.part;

import org.eclipse.e4.core.di.annotations.Optional;
import org.eclipse.e4.core.di.extensions.Preference;
import org.eclipse.e4.core.di.extensions.Service;
import org.eclipse.e4.core.services.events.IEventBroker;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.fipro.eclipse.tutorial.service.inverter.InverterService;

import jakarta.annotation.PostConstruct;
import jakarta.inject.Inject;
import javafx.animation.ParallelTransition;
import javafx.animation.RotateTransition;
import javafx.animation.ScaleTransition;
import javafx.embed.swt.FXCanvas;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.paint.Color;
import javafx.util.Duration;

public class InverterFXPart {

@Inject
@Service
private InverterService inverter;

@Inject
IEventBroker broker;

TextField input;
Label output;

Color textColor = Color.BLACK;

@PostConstruct
public void postConstruct(Composite parent) {
GridLayoutFactory.fillDefaults().applyTo(parent);

// add FXCanvas for adding JavaFX controls to the UI
FXCanvas canvas = new FXCanvas(parent, SWT.NONE);
GridDataFactory
.fillDefaults()
.grab(true, true)
.span(3, 1)
.applyTo(canvas);

// create the root layout pane
GridPane layout = new GridPane();

// create a Scene instance
// set the layout container as root
// set the background fill to the background color of the shell
Scene scene = new Scene(layout, Color.rgb(
parent.getShell().getBackground().getRed(),
parent.getShell().getBackground().getGreen(),
parent.getShell().getBackground().getBlue()));

// set the Scene to the FXCanvas
canvas.setScene(scene);

// create the controls
Label inputLabel = new Label();
inputLabel.setText("String to revert:");
GridPane.setConstraints(inputLabel, 0, 0);
GridPane.setMargin(inputLabel, new Insets(5.0));

input = new TextField();
input.setStyle("-fx-text-fill: " + (textColor == Color.BLUE ? "blue" : "black") + ";");
GridPane.setConstraints(input, 1, 0);
GridPane.setHgrow(input, Priority.ALWAYS);
GridPane.setMargin(input, new Insets(5.0));

Button button = new Button();
button.setText("Revert");
GridPane.setConstraints(button, 2, 0);
GridPane.setMargin(button, new Insets(5.0));

Label outputLabel = new Label();
outputLabel.setText("Inverted String:");
GridPane.setConstraints(outputLabel, 0, 1);
GridPane.setMargin(outputLabel, new Insets(5.0));

output = new Label();
output.setTextFill(textColor);
GridPane.setConstraints(output, 0, 2);
GridPane.setColumnSpan(output, 3);
GridPane.setHgrow(output, Priority.ALWAYS);
GridPane.setHalignment(output, HPos.CENTER);

// don't forget to add children to gridpane
layout.getChildren().addAll(
inputLabel, input, button, outputLabel, output);

// add an animation for the output
RotateTransition rotateTransition =
new RotateTransition(Duration.seconds(1), output);
rotateTransition.setByAngle(360);

ScaleTransition scaleTransition =
new ScaleTransition(Duration.seconds(1), output);
scaleTransition.setFromX(1.0);
scaleTransition.setFromY(1.0);
scaleTransition.setToX(4.0);
scaleTransition.setToY(4.0);

ParallelTransition parallelTransition =
new ParallelTransition(rotateTransition, scaleTransition);

// add the action listener
button.setOnAction(event -> {
output.setText(inverter.invert(input.getText()));
broker.post("TOPIC_LOGGING", "triggered via button (FX)");
parallelTransition.play();
});

input.setOnAction(event -> {
output.setText(inverter.invert(input.getText()));
broker.post("TOPIC_LOGGING", "triggered via field (FX)");
parallelTransition.play();
});

}

@Inject
@Optional
public void setTextColor(
@Preference(nodePath = "org.fipro.eclipse.tutorial.inverter", value = "inverter_color") String color) {

textColor = "blue".equals(color)
? Color.BLUE
: Color.BLACK;

if (input != null) {
input.setStyle("-fx-text-fill: " + color + ";");
}

if (output != null) {
output.setTextFill(textColor);
}
}

}
2 changes: 1 addition & 1 deletion org.fipro.eclipse.tutorial.logview/fragment.e4xmi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="ASCII"?>
<fragment:ModelFragments xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:basic="http://www.eclipse.org/ui/2010/UIModel/application/ui/basic" xmlns:fragment="http://www.eclipse.org/ui/2010/UIModel/fragment" xmi:id="_KxgI4ABvEe-Sga9XhUrP7g">
<imports xsi:type="basic:PartSashContainer" xmi:id="_Ma03IABvEe-Sga9XhUrP7g" elementId="org.fipro.eclipse.tutorial.app.main"/>
<fragments xsi:type="fragment:StringModelFragment" xmi:id="_PGfzMABvEe-Sga9XhUrP7g" featurename="children" parentElementId="org.fipro.eclipse.tutorial.app.main" positionInList="after:org.fipro.eclipse.tutorial.inverter.part.0">
<fragments xsi:type="fragment:StringModelFragment" xmi:id="_PGfzMABvEe-Sga9XhUrP7g" featurename="children" parentElementId="org.fipro.eclipse.tutorial.app.main" positionInList="after:org.fipro.eclipse.tutorial.inverter.partstack.0">
<elements xsi:type="basic:PartStack" xmi:id="_ml410ABvEe-Sga9XhUrP7g" elementId="org.fipro.eclipse.tutorial.logview.partstack.0">
<children xsi:type="basic:Part" xmi:id="_t7d8QABvEe-Sga9XhUrP7g" elementId="org.fipro.eclipse.tutorial.logview.part.logview" contributionURI="bundleclass://org.fipro.eclipse.tutorial.logview/org.fipro.eclipse.tutorial.logview.part.LogViewPart" label="Log View"/>
</elements>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<launcherArgs>
<programArgs>-clearPersistedState
</programArgs>
<vmArgs>-Dosgi.framework.extensions=org.eclipse.fx.osgi
</vmArgs>
<vmArgsMac>-XstartOnFirstThread -Dorg.eclipse.swt.internal.carbon.smallFonts
</vmArgsMac>
</launcherArgs>
Expand Down Expand Up @@ -38,6 +40,11 @@
<feature id="org.eclipse.ecf.filetransfer.feature" installMode="root"/>
<feature id="org.eclipse.ecf.filetransfer.httpclientjava.feature" installMode="root"/>
<feature id="org.fipro.e4.service.preferences.feature" installMode="root"/>
<feature id="org.eclipse.fx.runtime.min.feature" installMode="root"/>
<feature id="openjfx.media.feature" installMode="root"/>
<feature id="openjfx.standard.feature" installMode="root"/>
<feature id="openjfx.swt.feature" installMode="root"/>
<feature id="openjfx.web.feature" installMode="root"/>
</features>

<configurations>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<?pde version="3.5"?>

<product name="Eclipse Cookbook Application" uid="org.fipro.eclipse.tutorial" id="org.fipro.eclipse.tutorial.app.product" application="org.eclipse.e4.ui.workbench.swt.E4Application" version="1.1.0.qualifier" type="features" includeLaunchers="true" autoIncludeRequirements="true">

<configIni use="default">
</configIni>

<launcherArgs>
<programArgs>-clearPersistedState
</programArgs>
<vmArgs>-Dosgi.framework.extensions=org.eclipse.fx.osgi
</vmArgs>
<vmArgsMac>-XstartOnFirstThread -Dorg.eclipse.swt.internal.carbon.smallFonts
</vmArgsMac>
</launcherArgs>

<launcher>
<win useIco="false">
<bmp/>
</win>
</launcher>

<vm>
</vm>

<plugins>
</plugins>

<features>
<feature id="org.eclipse.e4.rcp"/>
<feature id="org.eclipse.emf.ecore"/>
<feature id="org.eclipse.emf.common"/>
<feature id="org.fipro.eclipse.tutorial.feature" installMode="root"/>
<feature id="org.eclipse.equinox.p2.core.feature" installMode="root"/>
<feature id="org.eclipse.ecf.core.ssl.feature" installMode="root"/>
<feature id="org.eclipse.ecf.filetransfer.ssl.feature" installMode="root"/>
<feature id="org.eclipse.ecf.filetransfer.httpclient5.feature" installMode="root"/>
<feature id="org.eclipse.ecf.core.feature" installMode="root"/>
<feature id="org.eclipse.ecf.filetransfer.feature" installMode="root"/>
<feature id="org.eclipse.ecf.filetransfer.httpclientjava.feature" installMode="root"/>
<feature id="org.fipro.e4.service.preferences.feature" installMode="root"/>
<feature id="openjfx.media.feature" installMode="root"/>
<feature id="openjfx.standard.feature" installMode="root"/>
<feature id="openjfx.swt.feature" installMode="root"/>
<feature id="openjfx.web.feature" installMode="root"/>
<feature id="org.eclipse.fx.target.rcp4.feature" installMode="root"/>
</features>

<configurations>
<plugin id="org.apache.felix.scr" autoStart="true" startLevel="2" />
<plugin id="org.eclipse.core.runtime" autoStart="true" startLevel="0" />
<plugin id="org.eclipse.equinox.common" autoStart="true" startLevel="2" />
<plugin id="org.eclipse.equinox.event" autoStart="true" startLevel="2" />
<plugin id="org.eclipse.equinox.simpleconfigurator" autoStart="true" startLevel="1" />
</configurations>

</product>
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,45 @@
<repository
location="https://github.com/fipro78/e4-preferences/raw/master/releases/0.5.0"/>
</location>
<location
includeAllPlatforms="false"
includeConfigurePhase="false"
includeMode="planner"
includeSource="true"
type="InstallableUnit">

<unit
id="org.eclipse.fx.runtime.min.feature.feature.group"
version="3.9.0.202210162353"/>
<unit
id="org.eclipse.fx.target.rcp4.feature.feature.group"
version="3.9.0.202210170008"/>

<repository
location="http://download.eclipse.org/efxclipse/runtime-released/3.9.0/site"/>
</location>
<location
includeAllPlatforms="false"
includeConfigurePhase="false"
includeMode="planner"
includeSource="true"
type="InstallableUnit">

<unit
id="openjfx.media.feature.feature.group"
version="17.0.2.202204012121"/>
<unit
id="openjfx.standard.feature.feature.group"
version="17.0.2.202204012121"/>
<unit
id="openjfx.swt.feature.feature.group"
version="17.0.2.202204012121"/>
<unit
id="openjfx.web.feature.feature.group"
version="17.0.2.202204012121"/>

<repository
location="https://downloads.efxclipse.bestsolution.at/p2-repos/openjfx-17.0.2/"/>
</location>
</locations>
</target>
30 changes: 26 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,39 @@
</plugin>
</plugins>

<!--
Add this to avoid the warning:
'build.plugins.plugin.version' for org.eclipse.tycho:tycho-p2-director-plugin is missing.
-->
<pluginManagement>
<plugins>
<!--
Add this to avoid the warning:
'build.plugins.plugin.version' for org.eclipse.tycho:tycho-p2-director-plugin is missing.
-->
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-p2-director-plugin</artifactId>
<version>${tycho.version}</version>
</plugin>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-compiler-plugin</artifactId>
<version>${tycho.version}</version>
<configuration>
<encoding>UTF-8</encoding>
<extraClasspathElements>
<extraClasspathElement>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>17.0.11</version>
</extraClasspathElement>
<extraClasspathElement>
<groupId>org.openjfx</groupId>
<artifactId>javafx-swt</artifactId>
<version>17.0.11</version>
<systemPath>${JAVAFX_HOME}/lib/javafx-swt.jar</systemPath>
<scope>system</scope>
</extraClasspathElement>
</extraClasspathElements>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
Expand Down
Loading

0 comments on commit e5f41ab

Please sign in to comment.