util

Clone or download

refactor to suit java naming conventions

Modified Files

M pom.xml
+2 −2
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<modelVersion>4.0.0</modelVersion>
<groupId>com.StreamPi</groupId>
<groupId>com.stream_pi</groupId>
<artifactId>Util</artifactId>
<artifactId>util</artifactId>
<version>1.0.0</version>
<version>1.0.0</version>
<url>https://stream-pi.com/</url>
<url>https://stream-pi.com/</url>
<build>
<build>
<plugins>
<plugins>
<plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<version>3.1.0</version>
<executions>
<executions>
<execution>
<execution>
<id>test-jar</id>
<id>test-jar</id>
<phase>package</phase>
<phase>package</phase>
<goals>
<goals>
<goal>test-jar</goal>
<goal>test-jar</goal>
</goals>
</goals>
</execution>
</execution>
</executions>
</executions>
</plugin>
</plugin>
<plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<version>3.8.1</version>
<configuration>
<configuration>
<release>11</release>
<release>11</release>
</configuration>
</configuration>
</plugin>
</plugin>
</plugins>
</plugins>
</build>
</build>
<properties>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.target>11</maven.compiler.target>
<IkonliVersion>11.5.0</IkonliVersion>
<IkonliVersion>11.5.0</IkonliVersion>
<IkonliFA5PackVersion>11.5.0</IkonliFA5PackVersion>
<IkonliFA5PackVersion>11.5.0</IkonliFA5PackVersion>
<JavaFXVersion>16-ea+5</JavaFXVersion>
<JavaFXVersion>16-ea+5</JavaFXVersion>
</properties>
</properties>
<dependencies>
<dependencies>
<dependency>
<dependency>
<groupId>org.kordamp.ikonli</groupId>
<groupId>org.kordamp.ikonli</groupId>
<artifactId>ikonli-fontawesome5-pack</artifactId>
<artifactId>ikonli-fontawesome5-pack</artifactId>
<version>${IkonliFA5PackVersion}</version>
<version>${IkonliFA5PackVersion}</version>
</dependency>
</dependency>
<dependency>
<dependency>
<groupId>org.openjfx</groupId>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<artifactId>javafx-controls</artifactId>
<version>${JavaFXVersion}</version>
<version>${JavaFXVersion}</version>
</dependency>
</dependency>
<dependency>
<dependency>
<groupId>org.openjfx</groupId>
<groupId>org.openjfx</groupId>
<artifactId>javafx-base</artifactId>
<artifactId>javafx-base</artifactId>
<version>${JavaFXVersion}</version>
<version>${JavaFXVersion}</version>
</dependency>
</dependency>
<dependency>
<dependency>
<groupId>org.kordamp.ikonli</groupId>
<groupId>org.kordamp.ikonli</groupId>
<artifactId>ikonli-javafx</artifactId>
<artifactId>ikonli-javafx</artifactId>
<version>${IkonliVersion}</version>
<version>${IkonliVersion}</version>
</dependency>
</dependency>
</dependencies>
</dependencies>
</project>
</project>
package com.stream_pi.util.alert;
import com.stream_pi.util.uihelper.SpaceFiller;
import org.kordamp.ikonli.javafx.FontIcon;
import javafx.application.Platform;
import javafx.collections.ListChangeListener;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
public class StreamPiAlert {
private String title;
private String[] buttons;
private StreamPiAlertType streamPiAlertType;
private Pane contentPane;
private static StackPane stackPaneParent = null;
public static void setParent(StackPane parent) {
stackPaneParent = parent;
stackPaneParent.getStyleClass().add("alert_pane_parent");
stackPaneParent.getChildren().addListener(new ListChangeListener<Node>()
{
@Override
public void onChanged(Change<? extends Node> c) {
if(stackPaneParent.getChildren().size() > 0)
{
stackPaneParent.setVisible(true);
stackPaneParent.toFront();
}
else
{
stackPaneParent.setVisible(false);
stackPaneParent.toBack();
}
}
});
}
public static StackPane getParent() {
return stackPaneParent;
}
private StreamPiAlertListener streamPiAlertListener = null;
public StreamPiAlert(String title, StreamPiAlertType streamPiAlertType,
Pane contentPane) {
set(title, streamPiAlertType, contentPane, new String[]{ "OK" });
}
public void setStreamPiAlertType(StreamPiAlertType streamPiAlertType)
{
this.streamPiAlertType = streamPiAlertType;
}
public StreamPiAlert(String title, String contentText)
{
this(title, contentText, StreamPiAlertType.INFORMATION);
}
public StreamPiAlert(String contentText, StreamPiAlertType alertType)
{
this("Alert", contentText, alertType);
}
public StreamPiAlert(String title, StreamPiAlertType streamPiAlertType, String... buttons)
{
set(title, streamPiAlertType, null, buttons);
}
public StreamPiAlert(String title, StreamPiAlertType streamPiAlertType, Pane contentPane, String... buttons)
{
set(title, streamPiAlertType, contentPane, buttons);
}
public StreamPiAlert(String title, String contentText, StreamPiAlertType streamPiAlertType)
{
Label label = new Label(contentText);
label.setWrapText(true);
VBox vBox = new VBox(label);
set(title, streamPiAlertType, vBox, new String[]{ "OK" });
}
private void set(String title, StreamPiAlertType streamPiAlertType,
Pane contentPane, String... buttons)
{
this.title = title;
this.buttons = buttons;
this.contentPane = contentPane;
this.streamPiAlertType = streamPiAlertType;
}
public void setOnClicked(StreamPiAlertListener streamPiAlertListener) {
this.streamPiAlertListener = streamPiAlertListener;
}
public String getTitle() {
return title;
}
public String[] getButtons() {
return buttons;
}
public void setAlertContent(Pane contentPane) {
this.contentPane = contentPane;
}
public void setButtons(String... buttons) {
this.buttons = buttons;
}
public VBox getAlertPane(String title, Pane alertPane) {
VBox alertVBox = new VBox();
alertVBox.getStyleClass().add("alert_pane");
if(title.isEmpty())
title = "Alert";
Label label = new Label(title);
label.getStyleClass().add("alert_pane_header_text");
FontIcon fontIcon = new FontIcon(streamPiAlertType.getIconCode());
fontIcon.getStyleClass().addAll("alert_header_icon", streamPiAlertType.getIconStyleClassName());
HBox header = new HBox(label, new SpaceFiller(SpaceFiller.FillerType.HBox), fontIcon);
header.getStyleClass().add("alert_header");
header.setPadding(new Insets(10));
HBox buttonBar = new HBox();
buttonBar.getStyleClass().add("alert_button_bar");
for (String eachButtonString : buttons) {
Button button = new Button(eachButtonString);
button.setOnAction(event -> {
if(this.streamPiAlertListener != null)
this.streamPiAlertListener.onClick(eachButtonString);
destroy();
});
button.getStyleClass().add("alert_button");
buttonBar.getChildren().add(button);
}
alertPane.getStyleClass().add("alert_content_pane");
ScrollPane scrollPane = new ScrollPane(alertPane);
scrollPane.prefHeightProperty().bind(alertPane.heightProperty().add(20));
scrollPane.getStyleClass().add("alert_scroll_pane");
alertPane.prefWidthProperty().bind(scrollPane.widthProperty().subtract(10));
scrollPane.setHbarPolicy(ScrollBarPolicy.NEVER);
scrollPane.setVbarPolicy(ScrollBarPolicy.AS_NEEDED);
VBox.setVgrow(scrollPane, Priority.ALWAYS);
alertVBox.getChildren().addAll(
header,
scrollPane,
buttonBar
);
alertVBox.setMaxHeight(Double.NEGATIVE_INFINITY);
return alertVBox;
}
public Pane getContentPane()
{
return contentPane;
}
private Node popupNode;
public void show()
{
Platform.runLater(()->{
popupNode = getAlertPane(getTitle(), getContentPane());
stackPaneParent.getChildren().add(popupNode);
});
}
public void destroy()
{
Platform.runLater(()->{
stackPaneParent.getChildren().remove(popupNode);
});
}
}
package com.stream_pi.util.alert;
public abstract class StreamPiAlertListener {
public abstract void onClick(String buttonClicked);
}
package com.stream_pi.util.alert;
public enum StreamPiAlertType {
INFORMATION("fas-info", "alert_information_icon"),
WARNING("fas-exclamation-triangle", "alert_warning_icon"),
ERROR("fas-times", "alert_error_icon");
private final String fontAwesomeIconCode;
private final String alertIconStyleClassName;
StreamPiAlertType(String fontAwesomeIconCode, String alertIconStyleClassName)
{
this.fontAwesomeIconCode = fontAwesomeIconCode;
this.alertIconStyleClassName = alertIconStyleClassName;
}
public String getIconCode()
{
return fontAwesomeIconCode;
}
public String getIconStyleClassName()
{
return alertIconStyleClassName;
}
}
package com.stream_pi.util.combobox;
import java.util.ArrayList;
import java.util.List;
import com.stream_pi.util.uihelper.SpaceFiller;
import org.kordamp.ikonli.javafx.FontIcon;
import javafx.application.Platform;
import javafx.collections.ListChangeListener;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
public class StreamPiComboBox<T> extends HBox
{
private List<T> options;
private static StackPane stackPaneParent;
public static void setParent(StackPane parent) {
stackPaneParent = parent;
stackPaneParent.getStyleClass().add("combobox_pane_parent");
stackPaneParent.getChildren().addListener(new ListChangeListener<Node>()
{
@Override
public void onChanged(Change<? extends Node> c) {
if(stackPaneParent.getChildren().size() > 0)
{
stackPaneParent.setVisible(true);
stackPaneParent.toFront();
}
else
{
stackPaneParent.setVisible(false);
stackPaneParent.toBack();
}
}
});
}
public StreamPiComboBox(List<T> options)
{
setup();
setOptions(options);
}
public List<T> getOptions()
{
return options;
}
public StreamPiComboBox()
{
setup();
}
private Label currentSelectedLabel;
private void setup()
{
buttons = new ArrayList<>();
getStyleClass().add("combo_box");
setOnMouseClicked(event->show());
currentSelectedLabel = new Label();
FontIcon fontIcon = new FontIcon();
fontIcon.getStyleClass().add("combo_box_drop_down_icon");
getChildren().addAll(
currentSelectedLabel,
new SpaceFiller(SpaceFiller.FillerType.HBox),
fontIcon
);
}
public void setOptions(List<T> options)
{
this.options = options;
setCurrentSelectedItemIndex(0);
}
private int currentIndex = 0;
private List<ToggleButton> buttons;
public ScrollPane getPopupScrollPane()
{
buttons.clear();
ScrollPane scrollPane = new ScrollPane();
scrollPane.setHbarPolicy(ScrollBarPolicy.NEVER);
scrollPane.setVbarPolicy(ScrollBarPolicy.AS_NEEDED);
scrollPane.getStyleClass().add("combo_box_popup");
VBox vBox = new VBox();
vBox.getStyleClass().add("combo_box_popup_vbox");
vBox.prefWidthProperty().bind(scrollPane.widthProperty().subtract(10));
scrollPane.setContent(vBox);
for(int i = 0;i<options.size();i++)
{
T eachOptionObj = options.get(i);
String displayText = streamPiComboBoxFactory.getOptionDisplayText(eachOptionObj);
ToggleButton optionButton = new ToggleButton(displayText);
optionButton.setSelected(i == currentIndex);
optionButton.setUserData(i);
optionButton.setOnAction(event->
{
setCurrentSelectedItemIndex((int) optionButton.getUserData());
if(streamPiComboBoxListener != null)
streamPiComboBoxListener.onNewItemSelected(options.get(currentIndex));
destroy();
});
vBox.getChildren().addAll(optionButton);
buttons.add(optionButton);
}
return scrollPane;
}
public int getCurrentIndex()
{
return currentIndex;
}
public T getCurrentSelectedItem()
{
return options.get(currentIndex);
}
public void setCurrentSelectedItemIndex(int index)
{
this.currentIndex = index;
setCurrentSelectedLabelText(streamPiComboBoxFactory.getOptionDisplayText(options.get(index)));
}
private void setCurrentSelectedLabelText(String text)
{
currentSelectedLabel.setText(text);
}
private StreamPiComboBoxListener<T> streamPiComboBoxListener = null;
public void setStreamPiComboBoxListener(StreamPiComboBoxListener<T> streamPiComboBoxListener)
{
this.streamPiComboBoxListener = streamPiComboBoxListener;
}
private Node popupNode;
public void show()
{
Platform.runLater(()->{
popupNode = getPopupScrollPane();
stackPaneParent.getChildren().add(popupNode);
});
}
public void destroy()
{
Platform.runLater(()->{
stackPaneParent.getChildren().remove(popupNode);
});
}
private StreamPiComboBoxFactory<T> streamPiComboBoxFactory;
public void setStreamPiComboBoxFactory(StreamPiComboBoxFactory<T> streamPiComboBoxFactory)
{
this.streamPiComboBoxFactory = streamPiComboBoxFactory;
}
}
package com.stream_pi.util.combobox;
public abstract class StreamPiComboBoxFactory<T>
{
public abstract String getOptionDisplayText(T object);
}
package com.stream_pi.util.combobox;
public abstract class StreamPiComboBoxListener<T> {
public abstract void onNewItemSelected(T selectedItem);
}
package com.stream_pi.util.exception;
public class MinorException extends StreamPiException {
public MinorException(String message)
{
super(message);
}
public MinorException(String title, String message)
{
super(title, message);
}
}
package com.stream_pi.util.exception;
public class SevereException extends StreamPiException{
public SevereException(String message)
{
super(message);
}
public SevereException(String title, String message)
{
super(title, message);
}
}
package com.stream_pi.util.exception;
public class StreamPiException extends Exception {
private String title;
private String message;
public StreamPiException(String message)
{
super(message);
this.title = "";
this.message = message;
}
public StreamPiException(String title, String message)
{
super(message);
this.title = title;
this.message = message;
}
public String getShortMessage()
{
return message;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public void setShortMessage(String message)
{
this.message = message;
}
}
package com.stream_pi.util.iohelper;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class IOHelper {
public static void unzip(InputStream inputStream, String destDir) throws Exception{
File dir = new File(destDir);
// create output directory if it doesn't exist
if(!dir.exists()) dir.mkdirs();
InputStream fis;
//buffer for read and write data to file
byte[] buffer = new byte[1024];
fis = inputStream;
ZipInputStream zis = new ZipInputStream(fis);
ZipEntry ze = zis.getNextEntry();
while(ze != null){
String fileName = ze.getName();
File newFile = new File(destDir + File.separator + fileName);
if(ze.isDirectory())
{
newFile.mkdirs();
}
else
{
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
}
//close this ZipEntry
zis.closeEntry();
ze = zis.getNextEntry();
}
//close last ZipEntry
zis.closeEntry();
zis.close();
fis.close();
}
}
package com.stream_pi.util.loggerhelper;
import java.util.logging.ConsoleHandler;
public class StreamPiLogFallbackHandler extends ConsoleHandler{
public StreamPiLogFallbackHandler() throws Exception
{
super();
setFormatter(new StreamPiLogFormatter());
}
public void closeHandler()
{
close();
}
}
package com.stream_pi.util.loggerhelper;
import java.util.logging.FileHandler;
public class StreamPiLogFileHandler extends FileHandler {
public StreamPiLogFileHandler(String logFilePath) throws Exception
{
super(logFilePath, false);
setFormatter(new StreamPiLogFormatter());
}
public void closeHandler()
{
close();
}
}
package com.stream_pi.util.loggerhelper;
import java.util.logging.LogRecord;
import java.util.logging.SimpleFormatter;
public class StreamPiLogFormatter extends SimpleFormatter{
@Override
public String format(LogRecord record)
{
return "["+Thread.currentThread().getName()+"] :: "+ record.getSourceClassName()+"@"+record.getSourceMethodName()+" -> " + ":" + record.getLevel() + " = "+ record.getMessage() + "\n";
}
}
package com.stream_pi.util.platform;
public enum Platform {
WINDOWS("Windows"),
LINUX("Linux"),
MAC("MacOS"),
ANDROID("Android"),
IOS("iOS"),
LINUX_RPI("Raspberry Pi"),
UNKNOWN("Unknown");
final private String UIName;
Platform(String UIName)
{
this.UIName = UIName;
}
public String getUIName()
{
return UIName;
}
}
/*
ReleaseStatus.java
Written By : Debayan Sutradhar (@rnayabed)
Enum to store the current status of the Server Release
*/
package com.stream_pi.util.platform;
public enum ReleaseStatus {
EA("Early Access"), GA("General Availability");
private final String UIName;
ReleaseStatus(String UIName)
{
this.UIName = UIName;
}
public String getUIName()
{
return UIName;
}
}
package com.stream_pi.util.startatboot;
public enum SoftwareType {
SERVER, CLIENT
}
package com.stream_pi.util.startatboot;
import com.stream_pi.util.platform.Platform;
import com.stream_pi.util.exception.MinorException;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
public class StartAtBoot {
SoftwareType softwareType;
Platform platform;
public StartAtBoot(SoftwareType softwareType, Platform platform)
{
this.softwareType = softwareType;
this.platform = platform;
}
public void create(File runnerFile) throws MinorException
{
if(platform == Platform.WINDOWS)
createStarterForWindows(runnerFile);
else if(platform == Platform.LINUX || platform == Platform.LINUX_RPI)
createStarterForLinux(runnerFile);
else if(platform == Platform.MAC)
createStarterForMac(runnerFile);
else if(platform == Platform.UNKNOWN)
unknownPlatformException();
}
public boolean delete() throws MinorException {
if(platform == Platform.WINDOWS)
return deleteStarterForWindows();
else if (platform == Platform.LINUX || platform == Platform.LINUX_RPI)
return deleteStarterForLinux();
else if(platform == Platform.MAC)
deleteStarterForMac();
else if(platform == Platform.UNKNOWN)
unknownPlatformException();
return false;
}
private void createStarterForLinux(File runnerFile) throws MinorException
{
try
{
String sysDDirectoryPath = System.getProperty("user.home")+"/.local/share/systemd/user/";
File sysDDirectoryFile = new File(sysDDirectoryPath);
if(!sysDDirectoryFile.exists())
sysDDirectoryFile.mkdirs();
File sysDServiceFile = new File(sysDDirectoryPath+"stream-pi-"+ softwareType+".service");
FileWriter fw = new FileWriter(sysDServiceFile);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("[Unit]\n" +
"Description=Stream-Pi "+softwareType+"\n" +
"[Service]\n" +
"Type=oneshot\n" +
"WorkingDirectory="+runnerFile.getAbsoluteFile().getParent()+"\n" +
"ExecStart="+runnerFile.getAbsoluteFile().getParent()+"/"+runnerFile.getName()+"\n" +
"[Install]\n" +
"WantedBy=default.target\n");
bw.close();
Runtime.getRuntime().exec("systemctl --user daemon-reload");
Runtime.getRuntime().exec("systemctl --user enable stream-pi-"+softwareType+".service");
}
catch (Exception e)
{
e.printStackTrace();
throw new MinorException("Unable to set start at boot",e.getMessage());
}
}
private boolean deleteStarterForLinux() throws MinorException
{
try
{
boolean f1 = new File(System.getProperty("user.home")+"/.local/share/systemd/user/stream-pi-"+
softwareType+".service").delete();
Runtime.getRuntime().exec("systemctl --user daemon-reload");
return f1;
}
catch (Exception e)
{
e.printStackTrace();
throw new MinorException("Unable to unset start at boot",e.getMessage());
}
}
private void createStarterForWindows(File runnerFile) throws MinorException
{
File initFile = new File(System.getenv("APPDATA")+"/Microsoft/Windows/Start Menu/Programs/Startup/streampi_starter_"+ softwareType +".bat");
try
{
FileWriter fw = new FileWriter(initFile);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("cd "+runnerFile.getAbsoluteFile().getParent()+"\n" +
runnerFile.getName());
bw.close();
}
catch (Exception e)
{
throw new MinorException(e.getMessage());
}
}
private boolean deleteStarterForWindows()
{
return new File(System.getenv("APPDATA")+"/Microsoft/Windows/Start Menu/Programs/Startup/streampi_starter_"+ softwareType +".bat").delete();
}
private void createStarterForMac(File runnerFile) throws MinorException
{
throw new MinorException("Mac Starter feature is not implemented yet.");
}
private void deleteStarterForMac() throws MinorException
{
throw new MinorException("Mac Starter feature is not implemented yet.");
}
private void unknownPlatformException() throws MinorException
{
throw new MinorException("Cannot implemented starter feature. Unknown platform.");
}
}
package com.stream_pi.util.uihelper;
import javafx.geometry.Insets;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
public class HBoxInputBox extends HBox {
private TextField textField;
public HBoxInputBox(String labelText, TextField textField, int prefWidth, CheckBox enablerCheckBox)
{
textField.setPrefWidth(prefWidth);
Label label = new Label(labelText);
getChildren().addAll(label, new SpaceFiller(SpaceFiller.FillerType.HBox), textField);
if(enablerCheckBox != null)
{
textField.disableProperty().bind(enablerCheckBox.selectedProperty());
HBox.setMargin(enablerCheckBox, new Insets(0, 0, 0, 45));
getChildren().add(enablerCheckBox);
}
this.textField = textField;
}
public HBoxInputBox(String labelText, TextField textField, CheckBox enablerCheckBox)
{
this(labelText, textField, 100, enablerCheckBox);
}
public HBoxInputBox(String labelText, TextField textField)
{
this(labelText, textField, 100, null);
}
public HBoxInputBox(String labelText, TextField textField, int prefWidth)
{
this(labelText, textField, prefWidth, null);
}
public TextField getTextField()
{
return textField;
}
}
package com.stream_pi.util.uihelper;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.stage.FileChooser;
import org.kordamp.ikonli.javafx.FontIcon;
import java.io.File;
public class HBoxInputBoxWithFileChooser extends HBox {
public HBoxInputBoxWithFileChooser(String labelText, TextField textField, CheckBox enablerCheckBox, FileChooser.ExtensionFilter extensionFilter)
{
textField.setDisable(true);
HBoxInputBox hBoxInputBox = new HBoxInputBox(labelText, textField, 300);
setHgrow(hBoxInputBox, Priority.ALWAYS);
getChildren().addAll(hBoxInputBox);
setSpacing(5.0);
Button button = new Button();
FontIcon fontIcon = new FontIcon("far-folder");
//fontIcon.setIconColor(Paint.valueOf(iconColorHex));
//fontIcon.setIconSize(iconSize);
button.setGraphic(fontIcon);
button.setOnAction(event -> {
FileChooser fileChooser = new FileChooser();
fileChooser.getExtensionFilters().addAll(
extensionFilter
);
try {
File selectedDirectory = fileChooser.showOpenDialog(button.getScene().getWindow());
textField.setText(selectedDirectory.getAbsolutePath());
}
catch (NullPointerException e)
{
// No folder selected
}
});
getChildren().add(button);
if(enablerCheckBox!=null)
{
button.disableProperty().bind(enablerCheckBox.selectedProperty());
HBox.setMargin(enablerCheckBox, new Insets(0, 0, 0, 45));
getChildren().add(enablerCheckBox);
}
}
}
package com.stream_pi.util.uihelper;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
public class SpaceFiller extends Region {
public SpaceFiller(FillerType fillerType)
{
if(fillerType == FillerType.HBox)
{
HBox.setHgrow(this, Priority.ALWAYS);
}
else if(fillerType == FillerType.VBox)
{
VBox.setVgrow(this, Priority.ALWAYS);
}
}
public enum FillerType
{
HBox, VBox
}
}
package com.stream_pi.util.version;
import com.stream_pi.util.exception.MinorException;
import java.io.Serializable;
public class Version implements Serializable {
private int major, minor, revision;
public Version()
{
major = 0;
minor = 0;
revision = 0;
}
public Version(int major, int minor, int revision)
{
this.major = major;
this.minor = minor;
this.revision = revision;
}
public Version(String version) throws MinorException
{
String[] v = version.trim().split("\\.");
if(v.length == 3)
{
try
{
major = Integer.parseInt(v[0]);
minor = Integer.parseInt(v[1]);
revision = Integer.parseInt(v[2]);
}
catch (NumberFormatException e)
{
throw new MinorException("Invalid versioning!");
}
}
else
{
throw new MinorException("Invalid versioning!");
}
}
public int getMajor()
{
return major;
}
public int getMinor()
{
return minor;
}
public int getRevision()
{
return minor;
}
public String getText()
{
return this.major+"."+this.minor+"."+this.revision;
}
public void setMajor(int major)
{
this.major = major;
}
public void setMinor(int minor)
{
this.minor = minor;
}
public void setRevision(int revision)
{
this.revision = revision;
}
public boolean isBiggerThan(Version version)
{
if(major>version.major)
return true;
else if(major < version.major)
return false;
else
{
if (minor > version.minor)
return true;
else if(minor < version.minor)
return false;
else
{
if(revision > version.revision)
return true;
else
return false;
}
}
}
public boolean isEqual(Version version)
{
return version.major == this.major && version.minor == this.minor && version.revision == this.revision;
}
}
package com.stream_pi.util.xmlconfighelper;
import java.io.File;
import java.util.logging.Logger;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
public class XMLConfigHelper {
private static Logger logger = Logger.getLogger(XMLConfigHelper.class.getName());
public static String getStringProperty(Element parentElement, String propertyName) throws Exception
{
return getProperty(parentElement, propertyName, false, null, null);
}
public static int getIntProperty(Element parentElement, String propertyName) throws Exception
{
return Integer.parseInt(getProperty(parentElement, propertyName, false, null, null));
}
public static double getDoubleProperty(Element parentElement, String propertyName) throws Exception
{
return Double.parseDouble(getProperty(parentElement, propertyName, false, null, null));
}
public static boolean getBooleanProperty(Element parentElement, String propertyName) throws Exception
{
return getProperty(parentElement, propertyName, false, null, null).equals("true");
}
public static String getProperty(Element parentElement, String propertyName, boolean createNewIfDoesntExist, Document document, File file) throws Exception
{
try {
return parentElement.getElementsByTagName(propertyName).item(0).getTextContent();
}
catch (Exception e)
{
logger.warning("CANNOT get property "+parentElement.getNodeName()+"."+propertyName+"!");
if(createNewIfDoesntExist)
{
logger.warning("Creating new property by that name ...");
Element newProp = document.createElement(propertyName);
parentElement.appendChild(newProp);
save(document, file);
}
throw e;
}
}
public static void save(Document document, File file) throws Exception
{
Transformer transformer = TransformerFactory.newInstance().newTransformer();
Result output = new StreamResult(file);
Source input = new DOMSource(document);
transformer.transform(input, output);
}
public static String getStringProperty(Element parentElement, String propertyName, String ifNotPresent, boolean printStackTrace)
{
return getStringProperty(parentElement, propertyName, ifNotPresent, printStackTrace, false, null, null);
}
public static String getStringProperty(Element parentElement, String propertyName, String ifNotPresent, boolean printStackTrace, boolean createNewIfDoesntExist,
Document document, File file)
{
String tbr = ifNotPresent;
try
{
tbr = getProperty(parentElement, propertyName, createNewIfDoesntExist, document, file);
}
catch(Exception e)
{
if(printStackTrace)
e.printStackTrace();
}
return tbr;
}
public static void removeChilds(Node node) {
while (node.hasChildNodes())
node.removeChild(node.getFirstChild());
}
public static int getIntProperty(Element parentElement, String propertyName, int ifNotPresent, boolean printStackTrace)
{
return getIntProperty(parentElement, propertyName, ifNotPresent, printStackTrace, false, null, null);
}
public static int getIntProperty(Element parentElement, String propertyName, int ifNotPresent, boolean printStackTrace, boolean createNewIfDoesntExist,
Document document, File file)
{
int tbr = ifNotPresent;
try
{
tbr = Integer.parseInt(getProperty(parentElement, propertyName, createNewIfDoesntExist, document, file));
}
catch(Exception e)
{
if(printStackTrace)
e.printStackTrace();
}
return tbr;
}
public static double getDoubleProperty(Element parentElement, String propertyName, double ifNotPresent)
{
return getDoubleProperty(parentElement, propertyName, ifNotPresent, true, false, null, null);
}
public static double getDoubleProperty(Element parentElement, String propertyName, double ifNotPresent, boolean printStackTrace)
{
return getDoubleProperty(parentElement, propertyName, ifNotPresent, printStackTrace, false, null, null);
}
public static double getDoubleProperty(Element parentElement, String propertyName, double ifNotPresent, boolean printStackTrace, boolean createNewIfDoesntExist,
Document document, File file)
{
double tbr = ifNotPresent;
try
{
tbr = Double.parseDouble(getProperty(parentElement, propertyName, createNewIfDoesntExist, document, file));
}
catch(Exception e)
{
if(printStackTrace)
e.printStackTrace();
}
return tbr;
}
public static String getStringProperty(Element parentElement, String propertyName, String ifNotPresent)
{
return getStringProperty(parentElement, propertyName, ifNotPresent, true, false, null, null);
}
public static boolean getBooleanProperty(Element parentElement, String propertyName, boolean ifNotPresent, boolean printStackTrace)
{
return getBooleanProperty(parentElement, propertyName, ifNotPresent, printStackTrace, false, null, null);
}
public static boolean getBooleanProperty(Element parentElement, String propertyName, boolean ifNotPresent, boolean printStackTrace, boolean createNewIfDoesntExist,
Document document, File file)
{
boolean tbr = ifNotPresent;
try
{
tbr = getProperty(parentElement, propertyName, createNewIfDoesntExist, document, file).equals("true");
}
catch(Exception e)
{
if(printStackTrace)
e.printStackTrace();
}
return tbr;
}
public static boolean getBooleanProperty(Element parentElement, String propertyName, boolean ifNotPresent)
{
return getBooleanProperty(parentElement, propertyName, ifNotPresent, true, false, null, null);
}
public static boolean doesElementExist(Element parent, String nameOfElement)
{
return parent.getElementsByTagName(nameOfElement).getLength() > 0;
}
public static boolean doesElementExist(Document document, String nameOfElement)
{
return document.getElementsByTagName(nameOfElement).getLength() > 0;
}
}
package com.streampi.util.alert;
import com.streampi.util.uihelper.SpaceFiller;
import org.kordamp.ikonli.javafx.FontIcon;
import javafx.application.Platform;
import javafx.collections.ListChangeListener;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
public class StreamPiAlert {
private String title;
private String[] buttons;
private StreamPiAlertType streamPiAlertType;
private Pane contentPane;
private static StackPane stackPaneParent = null;
public static void setParent(StackPane parent) {
stackPaneParent = parent;
stackPaneParent.getStyleClass().add("alert_pane_parent");
stackPaneParent.getChildren().addListener(new ListChangeListener<Node>()
{
@Override
public void onChanged(Change<? extends Node> c) {
if(stackPaneParent.getChildren().size() > 0)
{
stackPaneParent.setVisible(true);
stackPaneParent.toFront();
}
else
{
stackPaneParent.setVisible(false);
stackPaneParent.toBack();
}
}
});
}
public static StackPane getParent() {
return stackPaneParent;
}
private StreamPiAlertListener streamPiAlertListener = null;
public StreamPiAlert(String title, StreamPiAlertType streamPiAlertType,
Pane contentPane) {
set(title, streamPiAlertType, contentPane, new String[]{ "OK" });
}
public void setStreamPiAlertType(StreamPiAlertType streamPiAlertType)
{
this.streamPiAlertType = streamPiAlertType;
}
public StreamPiAlert(String title, String contentText)
{
this(title, contentText, StreamPiAlertType.INFORMATION);
}
public StreamPiAlert(String contentText, StreamPiAlertType alertType)
{
this("Alert", contentText, alertType);
}
public StreamPiAlert(String title, StreamPiAlertType streamPiAlertType, String... buttons)
{
set(title, streamPiAlertType, null, buttons);
}
public StreamPiAlert(String title, StreamPiAlertType streamPiAlertType, Pane contentPane, String... buttons)
{
set(title, streamPiAlertType, contentPane, buttons);
}
public StreamPiAlert(String title, String contentText, StreamPiAlertType streamPiAlertType)
{
Label label = new Label(contentText);
label.setWrapText(true);
VBox vBox = new VBox(label);
set(title, streamPiAlertType, vBox, new String[]{ "OK" });
}
private void set(String title, StreamPiAlertType streamPiAlertType,
Pane contentPane, String... buttons)
{
this.title = title;
this.buttons = buttons;
this.contentPane = contentPane;
this.streamPiAlertType = streamPiAlertType;
}
public void setOnClicked(StreamPiAlertListener streamPiAlertListener) {
this.streamPiAlertListener = streamPiAlertListener;
}
public String getTitle() {
return title;
}
public String[] getButtons() {
return buttons;
}
public void setAlertContent(Pane contentPane) {
this.contentPane = contentPane;
}
public void setButtons(String... buttons) {
this.buttons = buttons;
}
public VBox getAlertPane(String title, Pane alertPane) {
VBox alertVBox = new VBox();
alertVBox.getStyleClass().add("alert_pane");
if(title.isEmpty())
title = "Alert";
Label label = new Label(title);
label.getStyleClass().add("alert_pane_header_text");
FontIcon fontIcon = new FontIcon(streamPiAlertType.getIconCode());
fontIcon.getStyleClass().addAll("alert_header_icon", streamPiAlertType.getIconStyleClassName());
HBox header = new HBox(label, new SpaceFiller(SpaceFiller.FillerType.HBox), fontIcon);
header.getStyleClass().add("alert_header");
header.setPadding(new Insets(10));
HBox buttonBar = new HBox();
buttonBar.getStyleClass().add("alert_button_bar");
for (String eachButtonString : buttons) {
Button button = new Button(eachButtonString);
button.setOnAction(event -> {
if(this.streamPiAlertListener != null)
this.streamPiAlertListener.onClick(eachButtonString);
destroy();
});
button.getStyleClass().add("alert_button");
buttonBar.getChildren().add(button);
}
alertPane.getStyleClass().add("alert_content_pane");
ScrollPane scrollPane = new ScrollPane(alertPane);
scrollPane.prefHeightProperty().bind(alertPane.heightProperty().add(20));
scrollPane.getStyleClass().add("alert_scroll_pane");
alertPane.prefWidthProperty().bind(scrollPane.widthProperty().subtract(10));
scrollPane.setHbarPolicy(ScrollBarPolicy.NEVER);
scrollPane.setVbarPolicy(ScrollBarPolicy.AS_NEEDED);
VBox.setVgrow(scrollPane, Priority.ALWAYS);
alertVBox.getChildren().addAll(
header,
scrollPane,
buttonBar
);
alertVBox.setMaxHeight(Double.NEGATIVE_INFINITY);
return alertVBox;
}
public Pane getContentPane()
{
return contentPane;
}
private Node popupNode;
public void show()
{
Platform.runLater(()->{
popupNode = getAlertPane(getTitle(), getContentPane());
stackPaneParent.getChildren().add(popupNode);
});
}
public void destroy()
{
Platform.runLater(()->{
stackPaneParent.getChildren().remove(popupNode);
});
}
}
package com.streampi.util.alert;
public abstract class StreamPiAlertListener {
public abstract void onClick(String buttonClicked);
}
package com.streampi.util.alert;
public enum StreamPiAlertType {
INFORMATION("fas-info", "alert_information_icon"),
WARNING("fas-exclamation-triangle", "alert_warning_icon"),
ERROR("fas-times", "alert_error_icon");
private final String fontAwesomeIconCode;
private final String alertIconStyleClassName;
StreamPiAlertType(String fontAwesomeIconCode, String alertIconStyleClassName)
{
this.fontAwesomeIconCode = fontAwesomeIconCode;
this.alertIconStyleClassName = alertIconStyleClassName;
}
public String getIconCode()
{
return fontAwesomeIconCode;
}
public String getIconStyleClassName()
{
return alertIconStyleClassName;
}
}
package com.streampi.util.combobox;
import java.util.ArrayList;
import java.util.List;
import com.streampi.util.uihelper.SpaceFiller;
import org.kordamp.ikonli.javafx.FontIcon;
import javafx.application.Platform;
import javafx.collections.ListChangeListener;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
public class StreamPiComboBox<T> extends HBox
{
private List<T> options;
private static StackPane stackPaneParent;
public static void setParent(StackPane parent) {
stackPaneParent = parent;
stackPaneParent.getStyleClass().add("combobox_pane_parent");
stackPaneParent.getChildren().addListener(new ListChangeListener<Node>()
{
@Override
public void onChanged(Change<? extends Node> c) {
if(stackPaneParent.getChildren().size() > 0)
{
stackPaneParent.setVisible(true);
stackPaneParent.toFront();
}
else
{
stackPaneParent.setVisible(false);
stackPaneParent.toBack();
}
}
});
}
public StreamPiComboBox(List<T> options)
{
setup();
setOptions(options);
}
public List<T> getOptions()
{
return options;
}
public StreamPiComboBox()
{
setup();
}
private Label currentSelectedLabel;
private void setup()
{
buttons = new ArrayList<>();
getStyleClass().add("combo_box");
setOnMouseClicked(event->show());
currentSelectedLabel = new Label();
FontIcon fontIcon = new FontIcon();
fontIcon.getStyleClass().add("combo_box_drop_down_icon");
getChildren().addAll(
currentSelectedLabel,
new SpaceFiller(SpaceFiller.FillerType.HBox),
fontIcon
);
}
public void setOptions(List<T> options)
{
this.options = options;
setCurrentSelectedItemIndex(0);
}
private int currentIndex = 0;
private List<ToggleButton> buttons;
public ScrollPane getPopupScrollPane()
{
buttons.clear();
ScrollPane scrollPane = new ScrollPane();
scrollPane.setHbarPolicy(ScrollBarPolicy.NEVER);
scrollPane.setVbarPolicy(ScrollBarPolicy.AS_NEEDED);
scrollPane.getStyleClass().add("combo_box_popup");
VBox vBox = new VBox();
vBox.getStyleClass().add("combo_box_popup_vbox");
vBox.prefWidthProperty().bind(scrollPane.widthProperty().subtract(10));
scrollPane.setContent(vBox);
for(int i = 0;i<options.size();i++)
{
T eachOptionObj = options.get(i);
String displayText = streamPiComboBoxFactory.getOptionDisplayText(eachOptionObj);
ToggleButton optionButton = new ToggleButton(displayText);
optionButton.setSelected(i == currentIndex);
optionButton.setUserData(i);
optionButton.setOnAction(event->
{
setCurrentSelectedItemIndex((int) optionButton.getUserData());
if(streamPiComboBoxListener != null)
streamPiComboBoxListener.onNewItemSelected(options.get(currentIndex));
destroy();
});
vBox.getChildren().addAll(optionButton);
buttons.add(optionButton);
}
return scrollPane;
}
public int getCurrentIndex()
{
return currentIndex;
}
public T getCurrentSelectedItem()
{
return options.get(currentIndex);
}
public void setCurrentSelectedItemIndex(int index)
{
this.currentIndex = index;
setCurrentSelectedLabelText(streamPiComboBoxFactory.getOptionDisplayText(options.get(index)));
}
private void setCurrentSelectedLabelText(String text)
{
currentSelectedLabel.setText(text);
}
private StreamPiComboBoxListener<T> streamPiComboBoxListener = null;
public void setStreamPiComboBoxListener(StreamPiComboBoxListener<T> streamPiComboBoxListener)
{
this.streamPiComboBoxListener = streamPiComboBoxListener;
}
private Node popupNode;
public void show()
{
Platform.runLater(()->{
popupNode = getPopupScrollPane();
stackPaneParent.getChildren().add(popupNode);
});
}
public void destroy()
{
Platform.runLater(()->{
stackPaneParent.getChildren().remove(popupNode);
});
}
private StreamPiComboBoxFactory<T> streamPiComboBoxFactory;
public void setStreamPiComboBoxFactory(StreamPiComboBoxFactory<T> streamPiComboBoxFactory)
{
this.streamPiComboBoxFactory = streamPiComboBoxFactory;
}
}
package com.streampi.util.combobox;
public abstract class StreamPiComboBoxFactory<T>
{
public abstract String getOptionDisplayText(T object);
}
package com.streampi.util.combobox;
public abstract class StreamPiComboBoxListener<T> {
public abstract void onNewItemSelected(T selectedItem);
}
package com.streampi.util.exception;
public class MinorException extends StreamPiException {
public MinorException(String message)
{
super(message);
}
public MinorException(String title, String message)
{
super(title, message);
}
}
package com.streampi.util.exception;
public class SevereException extends StreamPiException{
public SevereException(String message)
{
super(message);
}
public SevereException(String title, String message)
{
super(title, message);
}
}
package com.streampi.util.exception;
public class StreamPiException extends Exception {
private String title;
private String message;
public StreamPiException(String message)
{
super(message);
this.title = "";
this.message = message;
}
public StreamPiException(String title, String message)
{
super(message);
this.title = title;
this.message = message;
}
public String getShortMessage()
{
return message;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public void setShortMessage(String message)
{
this.message = message;
}
}
package com.streampi.util.iohelper;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class IOHelper {
public static void unzip(InputStream inputStream, String destDir) throws Exception{
File dir = new File(destDir);
// create output directory if it doesn't exist
if(!dir.exists()) dir.mkdirs();
InputStream fis;
//buffer for read and write data to file
byte[] buffer = new byte[1024];
fis = inputStream;
ZipInputStream zis = new ZipInputStream(fis);
ZipEntry ze = zis.getNextEntry();
while(ze != null){
String fileName = ze.getName();
File newFile = new File(destDir + File.separator + fileName);
if(ze.isDirectory())
{
newFile.mkdirs();
}
else
{
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
}
//close this ZipEntry
zis.closeEntry();
ze = zis.getNextEntry();
}
//close last ZipEntry
zis.closeEntry();
zis.close();
fis.close();
}
}
package com.streampi.util.loggerhelper;
import java.util.logging.ConsoleHandler;
public class StreamPiLogFallbackHandler extends ConsoleHandler{
public StreamPiLogFallbackHandler() throws Exception
{
super();
setFormatter(new StreamPiLogFormatter());
}
public void closeHandler()
{
close();
}
}
package com.streampi.util.loggerhelper;
import java.util.logging.FileHandler;
public class StreamPiLogFileHandler extends FileHandler {
public StreamPiLogFileHandler(String logFilePath) throws Exception
{
super(logFilePath, false);
setFormatter(new StreamPiLogFormatter());
}
public void closeHandler()
{
close();
}
}
package com.streampi.util.loggerhelper;
import java.util.logging.LogRecord;
import java.util.logging.SimpleFormatter;
public class StreamPiLogFormatter extends SimpleFormatter{
@Override
public String format(LogRecord record)
{
return "["+Thread.currentThread().getName()+"] :: "+ record.getSourceClassName()+"@"+record.getSourceMethodName()+" -> " + ":" + record.getLevel() + " = "+ record.getMessage() + "\n";
}
}
package com.streampi.util.platform;
public enum Platform {
WINDOWS("Windows"),
LINUX("Linux"),
MAC("MacOS"),
ANDROID("Android"),
IOS("iOS"),
LINUX_RPI("Raspberry Pi"),
UNKNOWN("Unknown");
final private String UIName;
Platform(String UIName)
{
this.UIName = UIName;
}
public String getUIName()
{
return UIName;
}
}
/*
ReleaseStatus.java
Written By : Debayan Sutradhar (@rnayabed)
Enum to store the current status of the Server Release
*/
package com.streampi.util.platform;
public enum ReleaseStatus {
EA("Early Access"), GA("General Availability");
private final String UIName;
ReleaseStatus(String UIName)
{
this.UIName = UIName;
}
public String getUIName()
{
return UIName;
}
}
package com.streampi.util.startatboot;
public enum SoftwareType {
SERVER, CLIENT
}
package com.streampi.util.startatboot;
import com.streampi.util.exception.MinorException;
import com.streampi.util.platform.Platform;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
public class StartAtBoot {
SoftwareType softwareType;
Platform platform;
public StartAtBoot(SoftwareType softwareType, Platform platform)
{
this.softwareType = softwareType;
this.platform = platform;
}
public void create(File runnerFile) throws MinorException
{
if(platform == Platform.WINDOWS)
createStarterForWindows(runnerFile);
else if(platform == Platform.LINUX || platform == Platform.LINUX_RPI)
createStarterForLinux(runnerFile);
else if(platform == Platform.MAC)
createStarterForMac(runnerFile);
else if(platform == Platform.UNKNOWN)
unknownPlatformException();
}
public boolean delete() throws MinorException {
if(platform == Platform.WINDOWS)
return deleteStarterForWindows();
else if (platform == Platform.LINUX || platform == Platform.LINUX_RPI)
return deleteStarterForLinux();
else if(platform == Platform.MAC)
deleteStarterForMac();
else if(platform == Platform.UNKNOWN)
unknownPlatformException();
return false;
}
private void createStarterForLinux(File runnerFile) throws MinorException
{
try
{
String sysDDirectoryPath = System.getProperty("user.home")+"/.local/share/systemd/user/";
File sysDDirectoryFile = new File(sysDDirectoryPath);
if(!sysDDirectoryFile.exists())
sysDDirectoryFile.mkdirs();
File sysDServiceFile = new File(sysDDirectoryPath+"stream-pi-"+ softwareType+".service");
FileWriter fw = new FileWriter(sysDServiceFile);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("[Unit]\n" +
"Description=Stream-Pi "+softwareType+"\n" +
"[Service]\n" +
"Type=oneshot\n" +
"WorkingDirectory="+runnerFile.getAbsoluteFile().getParent()+"\n" +
"ExecStart="+runnerFile.getAbsoluteFile().getParent()+"/"+runnerFile.getName()+"\n" +
"[Install]\n" +
"WantedBy=default.target\n");
bw.close();
Runtime.getRuntime().exec("systemctl --user daemon-reload");
Runtime.getRuntime().exec("systemctl --user enable stream-pi-"+softwareType+".service");
}
catch (Exception e)
{
e.printStackTrace();
throw new MinorException("Unable to set start at boot",e.getMessage());
}
}
private boolean deleteStarterForLinux() throws MinorException
{
try
{
boolean f1 = new File(System.getProperty("user.home")+"/.local/share/systemd/user/stream-pi-"+
softwareType+".service").delete();
Runtime.getRuntime().exec("systemctl --user daemon-reload");
return f1;
}
catch (Exception e)
{
e.printStackTrace();
throw new MinorException("Unable to unset start at boot",e.getMessage());
}
}
private void createStarterForWindows(File runnerFile) throws MinorException
{
File initFile = new File(System.getenv("APPDATA")+"/Microsoft/Windows/Start Menu/Programs/Startup/streampi_starter_"+ softwareType +".bat");
try
{
FileWriter fw = new FileWriter(initFile);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("cd "+runnerFile.getAbsoluteFile().getParent()+"\n" +
runnerFile.getName());
bw.close();
}
catch (Exception e)
{
throw new MinorException(e.getMessage());
}
}
private boolean deleteStarterForWindows()
{
return new File(System.getenv("APPDATA")+"/Microsoft/Windows/Start Menu/Programs/Startup/streampi_starter_"+ softwareType +".bat").delete();
}
private void createStarterForMac(File runnerFile) throws MinorException
{
throw new MinorException("Mac Starter feature is not implemented yet.");
}
private void deleteStarterForMac() throws MinorException
{
throw new MinorException("Mac Starter feature is not implemented yet.");
}
private void unknownPlatformException() throws MinorException
{
throw new MinorException("Cannot implemented starter feature. Unknown platform.");
}
}
package com.streampi.util.uihelper;
import javafx.geometry.Insets;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
public class HBoxInputBox extends HBox {
private TextField textField;
public HBoxInputBox(String labelText, TextField textField, int prefWidth, CheckBox enablerCheckBox)
{
textField.setPrefWidth(prefWidth);
Label label = new Label(labelText);
getChildren().addAll(label, new SpaceFiller(SpaceFiller.FillerType.HBox), textField);
if(enablerCheckBox != null)
{
textField.disableProperty().bind(enablerCheckBox.selectedProperty());
HBox.setMargin(enablerCheckBox, new Insets(0, 0, 0, 45));
getChildren().add(enablerCheckBox);
}
this.textField = textField;
}
public HBoxInputBox(String labelText, TextField textField, CheckBox enablerCheckBox)
{
this(labelText, textField, 100, enablerCheckBox);
}
public HBoxInputBox(String labelText, TextField textField)
{
this(labelText, textField, 100, null);
}
public HBoxInputBox(String labelText, TextField textField, int prefWidth)
{
this(labelText, textField, prefWidth, null);
}
public TextField getTextField()
{
return textField;
}
}
package com.streampi.util.uihelper;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.stage.FileChooser;
import org.kordamp.ikonli.javafx.FontIcon;
import java.io.File;
public class HBoxInputBoxWithFileChooser extends HBox {
public HBoxInputBoxWithFileChooser(String labelText, TextField textField, CheckBox enablerCheckBox, FileChooser.ExtensionFilter extensionFilter)
{
textField.setDisable(true);
HBoxInputBox hBoxInputBox = new HBoxInputBox(labelText, textField, 300);
setHgrow(hBoxInputBox, Priority.ALWAYS);
getChildren().addAll(hBoxInputBox);
setSpacing(5.0);
Button button = new Button();
FontIcon fontIcon = new FontIcon("far-folder");
//fontIcon.setIconColor(Paint.valueOf(iconColorHex));
//fontIcon.setIconSize(iconSize);
button.setGraphic(fontIcon);
button.setOnAction(event -> {
FileChooser fileChooser = new FileChooser();
fileChooser.getExtensionFilters().addAll(
extensionFilter
);
try {
File selectedDirectory = fileChooser.showOpenDialog(button.getScene().getWindow());
textField.setText(selectedDirectory.getAbsolutePath());
}
catch (NullPointerException e)
{
// No folder selected
}
});
getChildren().add(button);
if(enablerCheckBox!=null)
{
button.disableProperty().bind(enablerCheckBox.selectedProperty());
HBox.setMargin(enablerCheckBox, new Insets(0, 0, 0, 45));
getChildren().add(enablerCheckBox);
}
}
}
package com.streampi.util.uihelper;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
public class SpaceFiller extends Region {
public SpaceFiller(FillerType fillerType)
{
if(fillerType == FillerType.HBox)
{
HBox.setHgrow(this, Priority.ALWAYS);
}
else if(fillerType == FillerType.VBox)
{
VBox.setVgrow(this, Priority.ALWAYS);
}
}
public enum FillerType
{
HBox, VBox
}
}
package com.streampi.util.version;
import com.streampi.util.exception.MinorException;
import java.io.Serializable;
public class Version implements Serializable {
private int major, minor, revision;
public Version()
{
major = 0;
minor = 0;
revision = 0;
}
public Version(int major, int minor, int revision)
{
this.major = major;
this.minor = minor;
this.revision = revision;
}
public Version(String version) throws MinorException
{
String[] v = version.trim().split("\\.");
if(v.length == 3)
{
try
{
major = Integer.parseInt(v[0]);
minor = Integer.parseInt(v[1]);
revision = Integer.parseInt(v[2]);
}
catch (NumberFormatException e)
{
throw new MinorException("Invalid versioning!");
}
}
else
{
throw new MinorException("Invalid versioning!");
}
}
public int getMajor()
{
return major;
}
public int getMinor()
{
return minor;
}
public int getRevision()
{
return minor;
}
public String getText()
{
return this.major+"."+this.minor+"."+this.revision;
}
public void setMajor(int major)
{
this.major = major;
}
public void setMinor(int minor)
{
this.minor = minor;
}
public void setRevision(int revision)
{
this.revision = revision;
}
public boolean isBiggerThan(Version version)
{
if(major>version.major)
return true;
else if(major < version.major)
return false;
else
{
if (minor > version.minor)
return true;
else if(minor < version.minor)
return false;
else
{
if(revision > version.revision)
return true;
else
return false;
}
}
}
public boolean isEqual(Version version)
{
return version.major == this.major && version.minor == this.minor && version.revision == this.revision;
}
}
package com.streampi.util.xmlconfighelper;
import java.io.File;
import java.util.logging.Logger;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
public class XMLConfigHelper {
private static Logger logger = Logger.getLogger(XMLConfigHelper.class.getName());
public static String getStringProperty(Element parentElement, String propertyName) throws Exception
{
return getProperty(parentElement, propertyName, false, null, null);
}
public static int getIntProperty(Element parentElement, String propertyName) throws Exception
{
return Integer.parseInt(getProperty(parentElement, propertyName, false, null, null));
}
public static double getDoubleProperty(Element parentElement, String propertyName) throws Exception
{
return Double.parseDouble(getProperty(parentElement, propertyName, false, null, null));
}
public static boolean getBooleanProperty(Element parentElement, String propertyName) throws Exception
{
return getProperty(parentElement, propertyName, false, null, null).equals("true");
}
public static String getProperty(Element parentElement, String propertyName, boolean createNewIfDoesntExist, Document document, File file) throws Exception
{
try {
return parentElement.getElementsByTagName(propertyName).item(0).getTextContent();
}
catch (Exception e)
{
logger.warning("CANNOT get property "+parentElement.getNodeName()+"."+propertyName+"!");
if(createNewIfDoesntExist)
{
logger.warning("Creating new property by that name ...");
Element newProp = document.createElement(propertyName);
parentElement.appendChild(newProp);
save(document, file);
}
throw e;
}
}
public static void save(Document document, File file) throws Exception
{
Transformer transformer = TransformerFactory.newInstance().newTransformer();
Result output = new StreamResult(file);
Source input = new DOMSource(document);
transformer.transform(input, output);
}
public static String getStringProperty(Element parentElement, String propertyName, String ifNotPresent, boolean printStackTrace)
{
return getStringProperty(parentElement, propertyName, ifNotPresent, printStackTrace, false, null, null);
}
public static String getStringProperty(Element parentElement, String propertyName, String ifNotPresent, boolean printStackTrace, boolean createNewIfDoesntExist,
Document document, File file)
{
String tbr = ifNotPresent;
try
{
tbr = getProperty(parentElement, propertyName, createNewIfDoesntExist, document, file);
}
catch(Exception e)
{
if(printStackTrace)
e.printStackTrace();
}
return tbr;
}
public static void removeChilds(Node node) {
while (node.hasChildNodes())
node.removeChild(node.getFirstChild());
}
public static int getIntProperty(Element parentElement, String propertyName, int ifNotPresent, boolean printStackTrace)
{
return getIntProperty(parentElement, propertyName, ifNotPresent, printStackTrace, false, null, null);
}
public static int getIntProperty(Element parentElement, String propertyName, int ifNotPresent, boolean printStackTrace, boolean createNewIfDoesntExist,
Document document, File file)
{
int tbr = ifNotPresent;
try
{
tbr = Integer.parseInt(getProperty(parentElement, propertyName, createNewIfDoesntExist, document, file));
}
catch(Exception e)
{
if(printStackTrace)
e.printStackTrace();
}
return tbr;
}
public static double getDoubleProperty(Element parentElement, String propertyName, double ifNotPresent)
{
return getDoubleProperty(parentElement, propertyName, ifNotPresent, true, false, null, null);
}
public static double getDoubleProperty(Element parentElement, String propertyName, double ifNotPresent, boolean printStackTrace)
{
return getDoubleProperty(parentElement, propertyName, ifNotPresent, printStackTrace, false, null, null);
}
public static double getDoubleProperty(Element parentElement, String propertyName, double ifNotPresent, boolean printStackTrace, boolean createNewIfDoesntExist,
Document document, File file)
{
double tbr = ifNotPresent;
try
{
tbr = Double.parseDouble(getProperty(parentElement, propertyName, createNewIfDoesntExist, document, file));
}
catch(Exception e)
{
if(printStackTrace)
e.printStackTrace();
}
return tbr;
}
public static String getStringProperty(Element parentElement, String propertyName, String ifNotPresent)
{
return getStringProperty(parentElement, propertyName, ifNotPresent, true, false, null, null);
}
public static boolean getBooleanProperty(Element parentElement, String propertyName, boolean ifNotPresent, boolean printStackTrace)
{
return getBooleanProperty(parentElement, propertyName, ifNotPresent, printStackTrace, false, null, null);
}
public static boolean getBooleanProperty(Element parentElement, String propertyName, boolean ifNotPresent, boolean printStackTrace, boolean createNewIfDoesntExist,
Document document, File file)
{
boolean tbr = ifNotPresent;
try
{
tbr = getProperty(parentElement, propertyName, createNewIfDoesntExist, document, file).equals("true");
}
catch(Exception e)
{
if(printStackTrace)
e.printStackTrace();
}
return tbr;
}
public static boolean getBooleanProperty(Element parentElement, String propertyName, boolean ifNotPresent)
{
return getBooleanProperty(parentElement, propertyName, ifNotPresent, true, false, null, null);
}
public static boolean doesElementExist(Element parent, String nameOfElement)
{
return parent.getElementsByTagName(nameOfElement).getLength() > 0;
}
public static boolean doesElementExist(Document document, String nameOfElement)
{
return document.getElementsByTagName(nameOfElement).getLength() > 0;
}
}
module com.streampi.util {
module com.stream_pi.util {
requires transitive org.kordamp.ikonli.javafx;
requires transitive org.kordamp.ikonli.javafx;
requires transitive org.kordamp.ikonli.fontawesome5;
requires transitive org.kordamp.ikonli.fontawesome5;
requires transitive javafx.base;
requires transitive javafx.base;
requires transitive java.logging;
requires transitive java.logging;
requires transitive javafx.controls;
requires transitive javafx.controls;
requires transitive java.xml;
requires transitive java.xml;
exports com.streampi.util.version;
exports com.stream_pi.util.version;
exports com.streampi.util.exception;
exports com.stream_pi.util.exception;
exports com.streampi.util.platform;
exports com.stream_pi.util.platform;
exports com.streampi.util.uihelper;
exports com.stream_pi.util.uihelper;
exports com.streampi.util.startatboot;
exports com.stream_pi.util.startatboot;
exports com.streampi.util.alert;
exports com.stream_pi.util.alert;
exports com.streampi.util.combobox;
exports com.stream_pi.util.combobox;
exports com.streampi.util.xmlconfighelper;
exports com.stream_pi.util.xmlconfighelper;
exports com.streampi.util.loggerhelper;
exports com.stream_pi.util.loggerhelper;
exports com.streampi.util.iohelper;
exports com.stream_pi.util.iohelper;
}
}