I have a Theme class for storing themes for my javaFX application. I created a serialize method that stores its information as a String and a constructor to create a Theme from a serialized Theme. I had to do this because I can't make Theme implement Serializable
, because its fields can't, and I need to store Theme objects after the program ends.
The code works fine, but it was tedious to write. I'm wondering if it's needlessly complicated or really inefficient, because it seems like there would be a native fix for this.
import javafx.scene.paint.Color;
public class Theme {
private Color btnBackground;
private Color btnBorder;
private Color labelFill;
private Color panesBackground;
private Color txtFieldBackground;
private Color txtFieldBorder;
private String btnStyle;
private String txtFieldStyle;
private String name;
public Theme(Color panesBackground, Color labelFill, Color txtFieldBackground, Color txtFieldBorder, String txtFieldStyle, Color btnBackground, Color btnBorder, String btnStyle, String name) {
this.panesBackground = panesBackground;
this.labelFill = labelFill;
this.txtFieldBackground = txtFieldBackground;
this.txtFieldBorder = txtFieldBorder;
this.txtFieldStyle = txtFieldStyle;
this.btnBackground = btnBackground;
this.btnBorder = btnBorder;
this.btnStyle = btnStyle;
this.name = name;
}
public Theme(String serializedTheme) {
String[] themeInfo = serializedTheme.split("#");
String[] colors = themeInfo[0].split("@");
String[][] strColorVals = new String[6][3];
for(int i = 0; i < 6; i++) {
strColorVals[i] = colors[i].split(",");
}
float[][] colorVals = new float[6][3];
for(int i = 0; i < colorVals.length; i++) {
for(int j = 0; j < colorVals[i].length; j++) {
colorVals[i][j] = Float.parseFloat(strColorVals[i][j]);
}
}
String[] strings = themeInfo[1].split("@");
panesBackground = Color.color(colorVals[3][0], colorVals[3][1], colorVals[3][2]);
labelFill = Color.color(colorVals[2][0], colorVals[2][1], colorVals[2][2]);
txtFieldBackground = Color.color(colorVals[4][0], colorVals[4][1], colorVals[4][2]);
txtFieldBorder = Color.color(colorVals[5][0], colorVals[5][1], colorVals[5][2]);
txtFieldStyle = strings[1];
btnBackground = Color.color(colorVals[0][0], colorVals[0][1], colorVals[0][2]);
btnBorder = Color.color(colorVals[1][0], colorVals[1][1], colorVals[1][2]);
btnStyle = strings[0];
name = strings[2];
}
public static float[] colorToArr(Color color) {
return new float[]{(float)color.getRed(), (float)color.getGreen(), (float)color.getBlue()};
}
public Color getBtnBorder() {
return btnBorder;
}
public Color getBtnBackground() {
return btnBackground;
}
public Color getTxtFieldBorder() {
return txtFieldBorder;
}
public Color getTxtFieldBackground() {
return txtFieldBackground;
}
public Color getLabelFill() {
return labelFill;
}
public Color getPanesBackground() {
return panesBackground;
}
public String getBtnStyle() {
return btnStyle;
}
public String getTxtFieldStyle() {
return txtFieldStyle;
}
public String getName() {
return name;
}
public String serialize() {
return colorToArr(btnBackground)[0] + "," +
colorToArr(btnBackground)[1] + "," +
colorToArr(btnBackground)[2] + "@" +
colorToArr(btnBorder)[0] + "," +
colorToArr(btnBorder)[1] + "," +
colorToArr(btnBorder)[2] + "@" +
colorToArr(labelFill)[0] + "," +
colorToArr(labelFill)[1] + "," +
colorToArr(labelFill)[2] + "@" +
colorToArr(panesBackground)[0] + "," +
colorToArr(panesBackground)[1] + "," +
colorToArr(panesBackground)[2] + "@" +
colorToArr(txtFieldBackground)[0] + "," +
colorToArr(txtFieldBackground)[1] + "," +
colorToArr(txtFieldBackground)[2] + "@" +
colorToArr(txtFieldBorder)[0] + "," +
colorToArr(txtFieldBorder)[1] + "," +
colorToArr(txtFieldBorder)[2] + "#" +
btnStyle + "@" + txtFieldStyle + "@" + name;
}
}
I have a method in my main class for reading the file to a list of Theme
's:
// create themes file or update themes list
if (themesFile.createNewFile()) {
themesList.add(darkTheme);
themesList.add(lightTheme);
try {
// write theme information to file:
FileWriter wrtr = new FileWriter(themesFile);
for(Theme theme : themesList) {
wrtr.append(theme.serialize());
}
wrtr.close();
System.out.println("Themes file created.");
} catch (IOException ex) {
ex.printStackTrace();
}
}else {
System.out.println("Themes file already exists.");
Scanner scnr = new Scanner(themesFile);
while(scnr.hasNext()) {
themesList.add(new Theme(scnr.next()));
}
scnr.close();
}
Note: themesList
is an ArrayList of themes, and Light
and Dark
are theme fields of the main class. Their serialize()
methods return
"0.2,0.2,[email protected],0.7,[email protected],0.8,[email protected],0.2,[email protected],0.3,[email protected],0.7,0.7#-fx-text-fill:lightgrey@-fx-text-fill:white@Dark"
and
"0.9,0.9,[email protected],0.5,[email protected],0.0,[email protected],1.0,[email protected],1.0,[email protected],0.5,0.5#-fx-text-fill:black@-fx-text-fill:black@Light"
respectively.