From: rnayabed Date: Mon, 01 Feb 2021 20:19:12 +0530 Subject: Refactored according to Java Naming Conventions --- Refactored according to Java Naming Conventions --- --- 'a/src/main/java/com/StreamPi/Util/Alert/StreamPiAlert.java' +++ /dev/null @@ -1,211 +0,0 @@ -package com.StreamPi.Util.Alert; - -import com.StreamPi.Util.FormHelper.SpaceFiller; -import com.StreamPi.Util.FormHelper.SpaceFiller.FillerType; -import org.kordamp.ikonli.javafx.FontIcon; - -import javafx.application.Platform; -import javafx.collections.ListChangeListener; -import javafx.geometry.Insets; -import javafx.geometry.Pos; -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.Region; -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() - { - @Override - public void onChanged(Change 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(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); - }); - } -} --- 'a/src/main/java/com/StreamPi/Util/Alert/StreamPiAlertListener.java' +++ /dev/null @@ -1,5 +0,0 @@ -package com.StreamPi.Util.Alert; - -public abstract class StreamPiAlertListener { - public abstract void onClick(String buttonClicked); -} --- 'a/src/main/java/com/StreamPi/Util/Alert/StreamPiAlertType.java' +++ /dev/null @@ -1,30 +0,0 @@ -package com.StreamPi.Util.Alert; - -import org.kordamp.ikonli.javafx.FontIcon; - -import javafx.scene.paint.Paint; - -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; - } -} --- 'a/src/main/java/com/StreamPi/Util/ComboBox/StreamPiComboBox.java' +++ /dev/null @@ -1,188 +0,0 @@ -package com.StreamPi.Util.ComboBox; - -import java.util.ArrayList; -import java.util.List; - -import com.StreamPi.Util.FormHelper.SpaceFiller; -import com.StreamPi.Util.FormHelper.SpaceFiller.FillerType; - -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 extends HBox -{ - private List options; - - private static StackPane stackPaneParent; - - public static void setParent(StackPane parent) { - stackPaneParent = parent; - - stackPaneParent.getStyleClass().add("combobox_pane_parent"); - - stackPaneParent.getChildren().addListener(new ListChangeListener() - { - @Override - public void onChanged(Change c) { - if(stackPaneParent.getChildren().size() > 0) - { - stackPaneParent.setVisible(true); - stackPaneParent.toFront(); - } - else - { - stackPaneParent.setVisible(false); - stackPaneParent.toBack(); - } - } - }); - } - - public StreamPiComboBox(List options) - { - setup(); - - setOptions(options); - } - - public List 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(FillerType.HBox), - fontIcon - ); - } - - public void setOptions(List options) - { - this.options = options; - setCurrentSelectedItemIndex(0); - } - - - private int currentIndex = 0; - private List 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 - { - 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 streamPiComboBoxListener = null; - - public void setStreamPiComboBoxListener(StreamPiComboBoxListener 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 streamPiComboBoxFactory; - public void setStreamPiComboBoxFactory(StreamPiComboBoxFactory streamPiComboBoxFactory) - { - this.streamPiComboBoxFactory = streamPiComboBoxFactory; - } -} \ No newline at end of file --- 'a/src/main/java/com/StreamPi/Util/ComboBox/StreamPiComboBoxFactory.java' +++ /dev/null @@ -1,6 +0,0 @@ -package com.StreamPi.Util.ComboBox; - -public abstract class StreamPiComboBoxFactory -{ - public abstract String getOptionDisplayText(T object); -} \ No newline at end of file --- 'a/src/main/java/com/StreamPi/Util/ComboBox/StreamPiComboBoxListener.java' +++ /dev/null @@ -1,5 +0,0 @@ -package com.StreamPi.Util.ComboBox; - -public abstract class StreamPiComboBoxListener { - public abstract void onNewItemSelected(T selectedItem); -} --- 'a/src/main/java/com/StreamPi/Util/Exception/MinorException.java' +++ /dev/null @@ -1,14 +0,0 @@ -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); - } -} --- 'a/src/main/java/com/StreamPi/Util/Exception/SevereException.java' +++ /dev/null @@ -1,13 +0,0 @@ -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); - } -} --- 'a/src/main/java/com/StreamPi/Util/Exception/StreamPiException.java' +++ /dev/null @@ -1,42 +0,0 @@ -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; - } -} --- 'a/src/main/java/com/StreamPi/Util/IOHelper/IOHelper.java' +++ /dev/null @@ -1,51 +0,0 @@ -package com.StreamPi.Util.IOHelper; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -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(); - } -} --- 'a/src/main/java/com/StreamPi/Util/LoggerHelper/StreamPiLogFallbackHandler.java' +++ /dev/null @@ -1,17 +0,0 @@ -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(); - } -} --- 'a/src/main/java/com/StreamPi/Util/LoggerHelper/StreamPiLogFileHandler.java' +++ /dev/null @@ -1,21 +0,0 @@ -package com.StreamPi.Util.LoggerHelper; - -import java.io.IOException; -import java.io.Serializable; -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(); - } - -} --- 'a/src/main/java/com/StreamPi/Util/LoggerHelper/StreamPiLogFormatter.java' +++ /dev/null @@ -1,12 +0,0 @@ -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"; - } -} --- 'a/src/main/java/com/StreamPi/Util/Platform/Platform.java' +++ /dev/null @@ -1,23 +0,0 @@ -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; - } -} --- 'a/src/main/java/com/StreamPi/Util/Platform/ReleaseStatus.java' +++ /dev/null @@ -1,25 +0,0 @@ -/* -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; - } -} --- 'a/src/main/java/com/StreamPi/Util/StartAtBoot/SoftwareType.java' +++ /dev/null @@ -1,5 +0,0 @@ -package com.StreamPi.Util.StartAtBoot; - -public enum SoftwareType { - SERVER, CLIENT -} --- 'a/src/main/java/com/StreamPi/Util/StartAtBoot/StartAtBoot.java' +++ /dev/null @@ -1,137 +0,0 @@ -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."); - } -} --- 'a/src/main/java/com/StreamPi/Util/UIHelper/HBoxInputBox.java' +++ /dev/null @@ -1,52 +0,0 @@ -package com.StreamPi.Util.FormHelper; - -import javafx.geometry.Insets; -import javafx.scene.control.CheckBox; -import javafx.scene.control.Label; -import javafx.scene.control.TextField; -import javafx.scene.layout.HBox; -import javafx.scene.layout.Priority; -import javafx.scene.layout.Region; - -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; - } -} --- 'a/src/main/java/com/StreamPi/Util/UIHelper/HBoxInputBoxWithFileChooser.java' +++ /dev/null @@ -1,60 +0,0 @@ -package com.StreamPi.Util.FormHelper; - -import javafx.geometry.Insets; -import javafx.scene.control.Button; -import javafx.scene.control.CheckBox; -import javafx.scene.control.Label; -import javafx.scene.control.TextField; -import javafx.scene.layout.HBox; -import javafx.scene.layout.Priority; -import javafx.scene.layout.Region; -import javafx.stage.FileChooser; -import javafx.stage.Window; -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); - } - } -} --- 'a/src/main/java/com/StreamPi/Util/UIHelper/SpaceFiller.java' +++ /dev/null @@ -1,26 +0,0 @@ -package com.StreamPi.Util.FormHelper; - -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 - } -} --- 'a/src/main/java/com/StreamPi/Util/Version/Version.java' +++ /dev/null @@ -1,109 +0,0 @@ -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; - } - -} --- 'a/src/main/java/com/StreamPi/Util/XMLConfigHelper/XMLConfigHelper.java' +++ /dev/null @@ -1,206 +0,0 @@ -package com.StreamPi.Util.XMLConfigHelper; - -import java.io.File; -import java.util.logging.Logger; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -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; - } - -} --- /dev/null +++ b/src/main/java/com/streampi/util/alert/StreamPiAlert.java @@ -0,0 +1,208 @@ +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() + { + @Override + public void onChanged(Change 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); + }); + } +} --- /dev/null +++ b/src/main/java/com/streampi/util/alert/StreamPiAlertListener.java @@ -0,0 +1,5 @@ +package com.streampi.util.alert; + +public abstract class StreamPiAlertListener { + public abstract void onClick(String buttonClicked); +} --- /dev/null +++ b/src/main/java/com/streampi/util/alert/StreamPiAlertType.java @@ -0,0 +1,26 @@ +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; + } +} --- /dev/null +++ b/src/main/java/com/streampi/util/combobox/StreamPiComboBox.java @@ -0,0 +1,187 @@ +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 extends HBox +{ + private List options; + + private static StackPane stackPaneParent; + + public static void setParent(StackPane parent) { + stackPaneParent = parent; + + stackPaneParent.getStyleClass().add("combobox_pane_parent"); + + stackPaneParent.getChildren().addListener(new ListChangeListener() + { + @Override + public void onChanged(Change c) { + if(stackPaneParent.getChildren().size() > 0) + { + stackPaneParent.setVisible(true); + stackPaneParent.toFront(); + } + else + { + stackPaneParent.setVisible(false); + stackPaneParent.toBack(); + } + } + }); + } + + public StreamPiComboBox(List options) + { + setup(); + + setOptions(options); + } + + public List 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 options) + { + this.options = options; + setCurrentSelectedItemIndex(0); + } + + + private int currentIndex = 0; + private List 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 + { + 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 streamPiComboBoxListener = null; + + public void setStreamPiComboBoxListener(StreamPiComboBoxListener 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 streamPiComboBoxFactory; + public void setStreamPiComboBoxFactory(StreamPiComboBoxFactory streamPiComboBoxFactory) + { + this.streamPiComboBoxFactory = streamPiComboBoxFactory; + } +} \ No newline at end of file --- /dev/null +++ b/src/main/java/com/streampi/util/combobox/StreamPiComboBoxFactory.java @@ -0,0 +1,6 @@ +package com.streampi.util.combobox; + +public abstract class StreamPiComboBoxFactory +{ + public abstract String getOptionDisplayText(T object); +} \ No newline at end of file --- /dev/null +++ b/src/main/java/com/streampi/util/combobox/StreamPiComboBoxListener.java @@ -0,0 +1,5 @@ +package com.streampi.util.combobox; + +public abstract class StreamPiComboBoxListener { + public abstract void onNewItemSelected(T selectedItem); +} --- /dev/null +++ b/src/main/java/com/streampi/util/exception/MinorException.java @@ -0,0 +1,14 @@ +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); + } +} --- /dev/null +++ b/src/main/java/com/streampi/util/exception/SevereException.java @@ -0,0 +1,13 @@ +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); + } +} --- /dev/null +++ b/src/main/java/com/streampi/util/exception/StreamPiException.java @@ -0,0 +1,42 @@ +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; + } +} --- /dev/null +++ b/src/main/java/com/streampi/util/iohelper/IOHelper.java @@ -0,0 +1,50 @@ +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(); + } +} --- /dev/null +++ b/src/main/java/com/streampi/util/loggerhelper/StreamPiLogFallbackHandler.java @@ -0,0 +1,17 @@ +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(); + } +} --- /dev/null +++ b/src/main/java/com/streampi/util/loggerhelper/StreamPiLogFileHandler.java @@ -0,0 +1,19 @@ +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(); + } + +} --- /dev/null +++ b/src/main/java/com/streampi/util/loggerhelper/StreamPiLogFormatter.java @@ -0,0 +1,12 @@ +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"; + } +} --- /dev/null +++ b/src/main/java/com/streampi/util/platform/Platform.java @@ -0,0 +1,23 @@ +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; + } +} --- /dev/null +++ b/src/main/java/com/streampi/util/platform/ReleaseStatus.java @@ -0,0 +1,25 @@ +/* +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; + } +} --- /dev/null +++ b/src/main/java/com/streampi/util/startatboot/SoftwareType.java @@ -0,0 +1,5 @@ +package com.streampi.util.startatboot; + +public enum SoftwareType { + SERVER, CLIENT +} --- /dev/null +++ b/src/main/java/com/streampi/util/startatboot/StartAtBoot.java @@ -0,0 +1,137 @@ +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."); + } +} --- /dev/null +++ b/src/main/java/com/streampi/util/uihelper/HBoxInputBox.java @@ -0,0 +1,50 @@ +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; + } +} --- /dev/null +++ b/src/main/java/com/streampi/util/uihelper/HBoxInputBoxWithFileChooser.java @@ -0,0 +1,57 @@ +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); + } + } +} --- /dev/null +++ b/src/main/java/com/streampi/util/uihelper/SpaceFiller.java @@ -0,0 +1,26 @@ +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 + } +} --- /dev/null +++ b/src/main/java/com/streampi/util/version/Version.java @@ -0,0 +1,109 @@ +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; + } + +} --- /dev/null +++ b/src/main/java/com/streampi/util/xmlconfighelper/XMLConfigHelper.java @@ -0,0 +1,204 @@ +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; + } + +} --- 'a/src/main/java/module-info.java' +++ b/src/main/java/module-info.java @@ -1,4 +1,4 @@ -module com.StreamPi.Util { +module com.streampi.util { requires transitive org.kordamp.ikonli.javafx; requires transitive org.kordamp.ikonli.fontawesome5; @@ -8,14 +8,14 @@ module com.StreamPi.Util { requires transitive java.xml; - exports com.StreamPi.Util.Version; - exports com.StreamPi.Util.Exception; - exports com.StreamPi.Util.Platform; - exports com.StreamPi.Util.FormHelper; - exports com.StreamPi.Util.StartAtBoot; - exports com.StreamPi.Util.Alert; - exports com.StreamPi.Util.ComboBox; - exports com.StreamPi.Util.XMLConfigHelper; - exports com.StreamPi.Util.LoggerHelper; - exports com.StreamPi.Util.IOHelper; + exports com.streampi.util.version; + exports com.streampi.util.exception; + exports com.streampi.util.platform; + exports com.streampi.util.uihelper; + exports com.streampi.util.startatboot; + exports com.streampi.util.alert; + exports com.streampi.util.combobox; + exports com.streampi.util.xmlconfighelper; + exports com.streampi.util.loggerhelper; + exports com.streampi.util.iohelper; } \ No newline at end of file