util

Clone or download

Modified Files

/*
/*
Stream-Pi - Free & Open-Source Modular Cross-Platform Programmable Macro Pad
Stream-Pi - Free & Open-Source Modular Cross-Platform Programmable Macro Pad
Copyright (C) 2019-2021 Debayan Sutradhar (rnayabed), Samuel Quiñones (SamuelQuinones)
Copyright (C) 2019-2021 Debayan Sutradhar (rnayabed), Samuel Quiñones (SamuelQuinones)
This program is free software: you can redistribute it and/or modify
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
(at your option) any later version.
This program is distributed in the hope that it will be useful,
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
GNU General Public License for more details.
Originally Written by : Debayan Sutradhar (rnayabed)
Originally Written by : Debayan Sutradhar (rnayabed)
*/
*/
package com.stream_pi.util.uihelper;
package com.stream_pi.util.uihelper;
import javafx.geometry.Insets;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextField;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Priority;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser;
import org.kordamp.ikonli.javafx.FontIcon;
import org.kordamp.ikonli.javafx.FontIcon;
import java.io.File;
import java.io.File;
public class HBoxInputBoxWithFileChooser extends HBox
public class HBoxInputBoxWithFileChooser extends HBox
{
{
private static File initialDirectory = null;
private static File initialDirectory = null;
public HBoxInputBoxWithFileChooser(String labelText, TextField textField,
public HBoxInputBoxWithFileChooser(String labelText, TextField textField,
FileChooser.ExtensionFilter... extensionFilters)
FileChooser.ExtensionFilter... extensionFilters)
{
{
this(labelText, textField, null, true, extensionFilters);
this(labelText, textField, null, true, extensionFilters);
}
}
public HBoxInputBoxWithFileChooser(String labelText, TextField textField, CheckBox enablerCheckBox,
public HBoxInputBoxWithFileChooser(String labelText, TextField textField, CheckBox enablerCheckBox,
boolean useLast, FileChooser.ExtensionFilter... extensionFilters)
boolean useLast, FileChooser.ExtensionFilter... extensionFilters)
{
{
this(labelText, textField, enablerCheckBox, 150, useLast, extensionFilters);
this(labelText, textField, enablerCheckBox, 150, useLast, extensionFilters);
}
}
public HBoxInputBoxWithFileChooser(String labelText, TextField textField, CheckBox enablerCheckBox,
public HBoxInputBoxWithFileChooser(String labelText, TextField textField, CheckBox enablerCheckBox,
FileChooser.ExtensionFilter... extensionFilters)
FileChooser.ExtensionFilter... extensionFilters)
{
{
this(labelText, textField, enablerCheckBox, 150, true, extensionFilters);
this(labelText, textField, enablerCheckBox, 150, true, extensionFilters);
}
}
FileChooser fileChooser;
FileChooser fileChooser;
public void setInitialDirectory(File initialDirectory)
public void setInitialDirectory(File initialDirectory)
{
{
fileChooser.setInitialDirectory(initialDirectory);
fileChooser.setInitialDirectory(initialDirectory);
}
}
private final Button fileChooseButton;
public Button getFileChooseButton()
{
return fileChooseButton;
}
public HBoxInputBoxWithFileChooser(String labelText, TextField textField, CheckBox enablerCheckBox,
public HBoxInputBoxWithFileChooser(String labelText, TextField textField, CheckBox enablerCheckBox,
int prefWidth, boolean useLast,
int prefWidth, boolean useLast,
FileChooser.ExtensionFilter... extensionFilter)
FileChooser.ExtensionFilter... extensionFilter)
{
{
textField.setDisable(true);
textField.setDisable(true);
HBoxInputBox hBoxInputBox = new HBoxInputBox(labelText, textField, prefWidth);
HBoxInputBox hBoxInputBox = new HBoxInputBox(labelText, textField, prefWidth);
setHgrow(hBoxInputBox, Priority.ALWAYS);
setHgrow(hBoxInputBox, Priority.ALWAYS);
getChildren().addAll(hBoxInputBox);
getChildren().addAll(hBoxInputBox);
setSpacing(5.0);
setSpacing(5.0);
Button button = new Button();
fileChooseButton = new Button();
FontIcon fontIcon = new FontIcon("far-folder");
FontIcon fontIcon = new FontIcon("far-folder");
button.setGraphic(fontIcon);
fileChooseButton.setGraphic(fontIcon);
button.setOnAction(event -> {
fileChooseButton.setOnAction(event -> {
fileChooser = new FileChooser();
fileChooser = new FileChooser();
if(useLast && initialDirectory != null)
if(useLast && initialDirectory != null)
{
{
setInitialDirectory(initialDirectory);
setInitialDirectory(initialDirectory);
}
}
if(extensionFilter!=null)
if(extensionFilter!=null)
{
{
fileChooser.getExtensionFilters().addAll(
fileChooser.getExtensionFilters().addAll(
extensionFilter
extensionFilter
);
);
}
}
try
try
{
{
File selectedFile = fileChooser.showOpenDialog(button.getScene().getWindow());
File selectedFile = fileChooser.showOpenDialog(fileChooseButton.getScene().getWindow());
initialDirectory = selectedFile.getParentFile();
initialDirectory = selectedFile.getParentFile();
textField.setText(selectedFile.getAbsolutePath());
textField.setText(selectedFile.getAbsolutePath());
}
}
catch (NullPointerException e)
catch (NullPointerException e)
{
{
// No folder selected
// No folder selected
}
}
});
});
getChildren().add(button);
getChildren().add(fileChooseButton);
if(enablerCheckBox!=null)
if(enablerCheckBox!=null)
{
{
button.disableProperty().bind(enablerCheckBox.selectedProperty());
fileChooseButton.disableProperty().bind(enablerCheckBox.selectedProperty());
HBox.setMargin(enablerCheckBox, new Insets(0, 0, 0, 45));
HBox.setMargin(enablerCheckBox, new Insets(0, 0, 0, 45));
getChildren().add(enablerCheckBox);
getChildren().add(enablerCheckBox);
}
}
}
}
}
}
/*
/*
Stream-Pi - Free & Open-Source Modular Cross-Platform Programmable Macro Pad
Stream-Pi - Free & Open-Source Modular Cross-Platform Programmable Macro Pad
Copyright (C) 2019-2021 Debayan Sutradhar (rnayabed), Samuel Quiñones (SamuelQuinones)
Copyright (C) 2019-2021 Debayan Sutradhar (rnayabed), Samuel Quiñones (SamuelQuinones)
This program is free software: you can redistribute it and/or modify
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
(at your option) any later version.
This program is distributed in the hope that it will be useful,
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
GNU General Public License for more details.
Originally Written by : Debayan Sutradhar (rnayabed)
Originally Written by : Debayan Sutradhar (rnayabed)
*/
*/
package com.stream_pi.util.xmlconfighelper;
package com.stream_pi.util.xmlconfighelper;
import java.io.File;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.Logger;
import javax.xml.transform.Result;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Node;
public class XMLConfigHelper {
public class XMLConfigHelper {
private static final Logger logger = Logger.getLogger(XMLConfigHelper.class.getName());
private static final Logger logger = Logger.getLogger(XMLConfigHelper.class.getName());
public static String getStringProperty(Element parentElement, String propertyName) throws Exception
public static String getStringProperty(Node parentElement, String propertyName) throws Exception
{
{
return getProperty(parentElement, propertyName, false, null, null);
return getProperty(parentElement, propertyName, false, null, null);
}
}
public static int getIntProperty(Element parentElement, String propertyName) throws Exception
public static int getIntProperty(Node parentElement, String propertyName) throws Exception
{
{
return Integer.parseInt(getProperty(parentElement, propertyName, false, null, null));
return Integer.parseInt(getProperty(parentElement, propertyName, false, null, null));
}
}
public static double getDoubleProperty(Element parentElement, String propertyName) throws Exception
public static double getDoubleProperty(Node parentElement, String propertyName) throws Exception
{
{
return Double.parseDouble(getProperty(parentElement, propertyName, false, null, null));
return Double.parseDouble(getProperty(parentElement, propertyName, false, null, null));
}
}
public static boolean getBooleanProperty(Element parentElement, String propertyName) throws Exception
public static boolean getBooleanProperty(Node parentElement, String propertyName) throws Exception
{
{
return getProperty(parentElement, propertyName, false, null, null).equals("true");
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
public static String getProperty(Node parentElement, String propertyName, boolean createNewIfDoesntExist, Document document, File file) throws Exception
{
{
try {
try
return parentElement.getElementsByTagName(propertyName).item(0).getTextContent();
{
if(parentElement instanceof Document)
{
return ((Document) parentElement).getElementsByTagName(propertyName).item(0).getTextContent();
}
else if(parentElement instanceof Element)
{
return ((Element) parentElement).getElementsByTagName(propertyName).item(0).getTextContent();
}
else
{
logger.severe("Passed parentElement is not Document/Element");
return null;
}
}
}
catch (Exception e)
catch (Exception e)
{
{
logger.warning("CANNOT get property "+parentElement.getNodeName()+"."+propertyName+"!");
logger.warning("CANNOT get property "+parentElement.getNodeName()+"."+propertyName+"!");
if (createNewIfDoesntExist)
if (createNewIfDoesntExist)
{
{
logger.warning("Creating new property by that name ...");
logger.warning("Creating new property by that name ...");
Element newProp = document.createElement(propertyName);
Element newProp = document.createElement(propertyName);
parentElement.appendChild(newProp);
parentElement.appendChild(newProp);
save(document, file);
save(document, file);
}
}
throw e;
throw e;
}
}
}
}
public static void save(Document document, File file) throws Exception
public static void save(Document document, File file) throws Exception
{
{
Transformer transformer = TransformerFactory.newInstance().newTransformer();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
Result output = new StreamResult(file);
Result output = new StreamResult(file);
Source input = new DOMSource(document);
Source input = new DOMSource(document);
transformer.transform(input, output);
transformer.transform(input, output);
}
}
public static String getStringProperty(Element parentElement, String propertyName, String ifNotPresent, boolean printStackTrace)
public static String getStringProperty(Node parentElement, String propertyName, String ifNotPresent, boolean printStackTrace)
{
{
return getStringProperty(parentElement, propertyName, ifNotPresent, printStackTrace, false, null, null);
return getStringProperty(parentElement, propertyName, ifNotPresent, printStackTrace, false, null, null);
}
}
public static String getStringProperty(Element parentElement, String propertyName, String ifNotPresent, boolean printStackTrace, boolean createNewIfDoesntExist,
public static String getStringProperty(Node parentElement, String propertyName, String ifNotPresent, boolean printStackTrace, boolean createNewIfDoesntExist,
Document document, File file)
Document document, File file)
{
{
String tbr = ifNotPresent;
String tbr = ifNotPresent;
try
try
{
{
tbr = getProperty(parentElement, propertyName, createNewIfDoesntExist, document, file);
tbr = getProperty(parentElement, propertyName, createNewIfDoesntExist, document, file);
}
}
catch(Exception e)
catch(Exception e)
{
{
if(printStackTrace)
if(printStackTrace)
e.printStackTrace();
e.printStackTrace();
}
}
return tbr;
return tbr;
}
}
public static void removeChilds(Node node) {
public static void removeChilds(Node node) {
while (node.hasChildNodes())
while (node.hasChildNodes())
node.removeChild(node.getFirstChild());
node.removeChild(node.getFirstChild());
}
}
public static int getIntProperty(Element parentElement, String propertyName, int ifNotPresent, boolean printStackTrace)
public static int getIntProperty(Node parentElement, String propertyName, int ifNotPresent, boolean printStackTrace)
{
{
return getIntProperty(parentElement, propertyName, ifNotPresent, printStackTrace, false, null, null);
return getIntProperty(parentElement, propertyName, ifNotPresent, printStackTrace, false, null, null);
}
}
public static int getIntProperty(Element parentElement, String propertyName, int ifNotPresent, boolean printStackTrace, boolean createNewIfDoesntExist,
public static int getIntProperty(Node parentElement, String propertyName, int ifNotPresent, boolean printStackTrace, boolean createNewIfDoesntExist,
Document document, File file)
Document document, File file)
{
{
int tbr = ifNotPresent;
int tbr = ifNotPresent;
try
try
{
{
tbr = Integer.parseInt(getProperty(parentElement, propertyName, createNewIfDoesntExist, document, file));
tbr = Integer.parseInt(getProperty(parentElement, propertyName, createNewIfDoesntExist, document, file));
}
}
catch(Exception e)
catch(Exception e)
{
{
if(printStackTrace)
if(printStackTrace)
e.printStackTrace();
e.printStackTrace();
}
}
return tbr;
return tbr;
}
}
public static double getDoubleProperty(Element parentElement, String propertyName, double ifNotPresent)
public static double getDoubleProperty(Node parentElement, String propertyName, double ifNotPresent)
{
{
return getDoubleProperty(parentElement, propertyName, ifNotPresent, true, false, null, null);
return getDoubleProperty(parentElement, propertyName, ifNotPresent, true, false, null, null);
}
}
public static double getDoubleProperty(Element parentElement, String propertyName, double ifNotPresent, boolean printStackTrace)
public static double getDoubleProperty(Node parentElement, String propertyName, double ifNotPresent, boolean printStackTrace)
{
{
return getDoubleProperty(parentElement, propertyName, ifNotPresent, printStackTrace, false, null, null);
return getDoubleProperty(parentElement, propertyName, ifNotPresent, printStackTrace, false, null, null);
}
}
public static double getDoubleProperty(Element parentElement, String propertyName, double ifNotPresent, boolean printStackTrace, boolean createNewIfDoesntExist,
public static double getDoubleProperty(Node parentElement, String propertyName, double ifNotPresent, boolean printStackTrace, boolean createNewIfDoesntExist,
Document document, File file)
Document document, File file)
{
{
double tbr = ifNotPresent;
double tbr = ifNotPresent;
try
try
{
{
tbr = Double.parseDouble(getProperty(parentElement, propertyName, createNewIfDoesntExist, document, file));
tbr = Double.parseDouble(getProperty(parentElement, propertyName, createNewIfDoesntExist, document, file));
}
}
catch(Exception e)
catch(Exception e)
{
{
if(printStackTrace)
if(printStackTrace)
e.printStackTrace();
e.printStackTrace();
}
}
return tbr;
return tbr;
}
}
public static String getStringProperty(Element parentElement, String propertyName, String ifNotPresent)
public static String getStringProperty(Node parentElement, String propertyName, String ifNotPresent)
{
{
return getStringProperty(parentElement, propertyName, ifNotPresent, true, false, null, null);
return getStringProperty(parentElement, propertyName, ifNotPresent, true, false, null, null);
}
}
public static boolean getBooleanProperty(Element parentElement, String propertyName, boolean ifNotPresent, boolean printStackTrace)
public static boolean getBooleanProperty(Node parentElement, String propertyName, boolean ifNotPresent, boolean printStackTrace)
{
{
return getBooleanProperty(parentElement, propertyName, ifNotPresent, printStackTrace, false, null, null);
return getBooleanProperty(parentElement, propertyName, ifNotPresent, printStackTrace, false, null, null);
}
}
public static boolean getBooleanProperty(Element parentElement, String propertyName, boolean ifNotPresent, boolean printStackTrace, boolean createNewIfDoesntExist,
public static boolean getBooleanProperty(Node parentElement, String propertyName, boolean ifNotPresent, boolean printStackTrace, boolean createNewIfDoesntExist,
Document document, File file)
Document document, File file)
{
{
boolean tbr = ifNotPresent;
boolean tbr = ifNotPresent;
try
try
{
{
tbr = getProperty(parentElement, propertyName, createNewIfDoesntExist, document, file).equals("true");
tbr = getProperty(parentElement, propertyName, createNewIfDoesntExist, document, file).equals("true");
}
}
catch(Exception e)
catch(Exception e)
{
{
if(printStackTrace)
if(printStackTrace)
e.printStackTrace();
e.printStackTrace();
}
}
return tbr;
return tbr;
}
}
public static boolean getBooleanProperty(Element parentElement, String propertyName, boolean ifNotPresent)
public static boolean getBooleanProperty(Node parentElement, String propertyName, boolean ifNotPresent)
{
{
return getBooleanProperty(parentElement, propertyName, ifNotPresent, true, false, null, null);
return getBooleanProperty(parentElement, propertyName, ifNotPresent, true, false, null, null);
}
}
public static boolean doesElementExist(Element parent, String nameOfElement)
public static boolean doesElementExist(Element parent, String nameOfElement)
{
{
return parent.getElementsByTagName(nameOfElement).getLength() > 0;
return parent.getElementsByTagName(nameOfElement).getLength() > 0;
}
}
public static boolean doesElementExist(Document document, String nameOfElement)
public static boolean doesElementExist(Document document, String nameOfElement)
{
{
return document.getElementsByTagName(nameOfElement).getLength() > 0;
return document.getElementsByTagName(nameOfElement).getLength() > 0;
}
}
}
}