server
Clone or download
Modified Files
/*
/*
Stream-Pi - Free & Open-Source Modular Cross-Platform Programmable Macropad
Stream-Pi - Free & Open-Source Modular Cross-Platform Programmable Macropad
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.
Written by : Debayan Sutradhar (rnayabed)
Written by : Debayan Sutradhar (rnayabed)
*/
*/
package com.stream_pi.server.client;
package com.stream_pi.server.client;
import com.stream_pi.util.platform.Platform;
import com.stream_pi.util.platform.Platform;
import com.stream_pi.util.platform.ReleaseStatus;
import com.stream_pi.util.platform.ReleaseStatus;
import com.stream_pi.util.version.Version;
import com.stream_pi.util.version.Version;
import java.net.SocketAddress;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashMap;
import java.util.List;
import java.util.List;
public class Client
public class Client
{
{
private String nickName;
private String nickName;
private final SocketAddress remoteSocketAddress;
private final SocketAddress remoteSocketAddress;
private final Platform platform;
private final Platform platform;
private final Version version;
private final Version version;
private final Version commStandardVersion;
private final Version commStandardVersion;
private final Version themeAPIVersion;
private final Version themeAPIVersion;
private final ReleaseStatus releaseStatus;
private final ReleaseStatus releaseStatus;
private double startupDisplayHeight, startupDisplayWidth;
private double displayHeight, displayWidth;
private final HashMap<String,ClientProfile> profiles;
private final HashMap<String,ClientProfile> profiles;
private final HashMap<String,ClientTheme> themes;
private final HashMap<String,ClientTheme> themes;
private String defaultProfileID;
private String defaultProfileID;
private String defaultThemeFullName;
private String defaultThemeFullName;
public Client(Version version, ReleaseStatus releaseStatus, Version commStandardVersion, Version themeAPIVersion, String nickName, Platform platform, SocketAddress remoteSocketAddress)
public Client(Version version, ReleaseStatus releaseStatus, Version commStandardVersion, Version themeAPIVersion, String nickName, Platform platform, SocketAddress remoteSocketAddress)
{
{
this.version = version;
this.version = version;
this.releaseStatus = releaseStatus;
this.releaseStatus = releaseStatus;
this.commStandardVersion = commStandardVersion;
this.commStandardVersion = commStandardVersion;
this.themeAPIVersion = themeAPIVersion;
this.themeAPIVersion = themeAPIVersion;
this.nickName = nickName;
this.nickName = nickName;
this.remoteSocketAddress = remoteSocketAddress;
this.remoteSocketAddress = remoteSocketAddress;
this.platform = platform;
this.platform = platform;
this.profiles = new HashMap<>();
this.profiles = new HashMap<>();
this.themes = new HashMap<>();
this.themes = new HashMap<>();
}
}
public ReleaseStatus getReleaseStatus() {
public ReleaseStatus getReleaseStatus() {
return releaseStatus;
return releaseStatus;
}
}
public void setDefaultThemeFullName(String defaultThemeFullName) {
public void setDefaultThemeFullName(String defaultThemeFullName) {
this.defaultThemeFullName = defaultThemeFullName;
this.defaultThemeFullName = defaultThemeFullName;
}
}
public String getDefaultThemeFullName() {
public String getDefaultThemeFullName() {
return defaultThemeFullName;
return defaultThemeFullName;
}
}
public void setDefaultProfileID(String ID)
public void setDefaultProfileID(String ID)
{
{
defaultProfileID = ID;
defaultProfileID = ID;
}
}
public void addTheme(ClientTheme clientTheme) throws CloneNotSupportedException
public void addTheme(ClientTheme clientTheme) throws CloneNotSupportedException
{
{
themes.put(clientTheme.getThemeFullName(), (ClientTheme) clientTheme.clone());
themes.put(clientTheme.getThemeFullName(), (ClientTheme) clientTheme.clone());
}
}
public ArrayList<ClientTheme> getThemes()
public ArrayList<ClientTheme> getThemes()
{
{
ArrayList<ClientTheme> clientThemes = new ArrayList<>();
ArrayList<ClientTheme> clientThemes = new ArrayList<>();
for(String clientTheme : themes.keySet())
for(String clientTheme : themes.keySet())
{
{
clientThemes.add(themes.get(clientTheme));
clientThemes.add(themes.get(clientTheme));
}
}
return clientThemes;
return clientThemes;
}
}
public ClientTheme getThemeByFullName(String fullName)
public ClientTheme getThemeByFullName(String fullName)
{
{
return themes.getOrDefault(fullName, null);
return themes.getOrDefault(fullName, null);
}
}
public String getDefaultProfileID()
public String getDefaultProfileID()
{
{
return defaultProfileID;
return defaultProfileID;
}
}
//client Profiles
//client Profiles
public void setNickName(String nickName)
public void setNickName(String nickName)
{
{
this.nickName = nickName;
this.nickName = nickName;
}
}
public List<ClientProfile> getAllClientProfiles()
public List<ClientProfile> getAllClientProfiles()
{
{
ArrayList<ClientProfile> clientProfiles = new ArrayList<>();
ArrayList<ClientProfile> clientProfiles = new ArrayList<>();
for(String profile : profiles.keySet())
for(String profile : profiles.keySet())
clientProfiles.add(profiles.get(profile));
clientProfiles.add(profiles.get(profile));
return clientProfiles;
return clientProfiles;
}
}
public void removeProfileFromID(String ID)
public void removeProfileFromID(String ID)
{
{
profiles.remove(ID);
profiles.remove(ID);
}
}
public void addProfile(ClientProfile clientProfile) throws CloneNotSupportedException {
public void addProfile(ClientProfile clientProfile) throws CloneNotSupportedException {
profiles.put(clientProfile.getID(), (ClientProfile) clientProfile.clone());
profiles.put(clientProfile.getID(), (ClientProfile) clientProfile.clone());
}
}
public synchronized ClientProfile getProfileByID(String ID) {
public synchronized ClientProfile getProfileByID(String ID) {
return profiles.getOrDefault(ID, null);
return profiles.getOrDefault(ID, null);
}
}
public SocketAddress getRemoteSocketAddress()
public SocketAddress getRemoteSocketAddress()
{
{
return remoteSocketAddress;
return remoteSocketAddress;
}
}
public Platform getPlatform()
public Platform getPlatform()
{
{
return platform;
return platform;
}
}
public String getNickName()
public String getNickName()
{
{
return nickName;
return nickName;
}
}
public Version getVersion()
public Version getVersion()
{
{
return version;
return version;
}
}
public Version getCommStandardVersion()
public Version getCommStandardVersion()
{
{
return commStandardVersion;
return commStandardVersion;
}
}
public Version getThemeAPIVersion()
public Version getThemeAPIVersion()
{
{
return themeAPIVersion;
return themeAPIVersion;
}
}
public double getStartupDisplayHeight()
public double getDisplayHeight()
{
{
return startupDisplayHeight;
return displayHeight;
}
}
public double getStartupDisplayWidth()
public double getDisplayWidth()
{
{
return startupDisplayWidth;
return displayWidth;
}
}
public void setStartupDisplayHeight(double height)
public void setDisplayHeight(double height)
{
{
startupDisplayHeight = height;
displayHeight = height;
}
}
public void setStartupDisplayWidth(double width)
public void setDisplayWidth(double width)
{
{
startupDisplayWidth = width;
displayWidth = width;
}
}
private int getMaxRows(int eachActionSize)
private int getMaxRows(int eachActionSize)
{
{
return (int) (startupDisplayHeight / eachActionSize);
return (int) (displayHeight / eachActionSize);
}
}
public int getMaxCols(int eachActionSize)
public int getMaxCols(int eachActionSize)
{
{
return (int) (startupDisplayWidth / eachActionSize);
return (int) (displayWidth / eachActionSize);
}
}
}
}
package com.stream_pi.server.connection;
package com.stream_pi.server.connection;
import com.stream_pi.action_api.action.Action;
import com.stream_pi.action_api.action.Action;
import com.stream_pi.action_api.action.ActionType;
import com.stream_pi.action_api.action.ActionType;
import com.stream_pi.action_api.action.DisplayTextAlignment;
import com.stream_pi.action_api.action.DisplayTextAlignment;
import com.stream_pi.action_api.action.Location;
import com.stream_pi.action_api.action.Location;
import com.stream_pi.action_api.actionproperty.ClientProperties;
import com.stream_pi.action_api.actionproperty.ClientProperties;
import com.stream_pi.action_api.actionproperty.property.Property;
import com.stream_pi.action_api.actionproperty.property.Property;
import com.stream_pi.action_api.actionproperty.property.Type;
import com.stream_pi.action_api.actionproperty.property.Type;
import com.stream_pi.action_api.normalaction.NormalAction;
import com.stream_pi.action_api.normalaction.NormalAction;
import com.stream_pi.server.action.NormalActionPlugins;
import com.stream_pi.server.action.NormalActionPlugins;
import com.stream_pi.server.client.Client;
import com.stream_pi.server.client.Client;
import com.stream_pi.server.client.ClientProfile;
import com.stream_pi.server.client.ClientProfile;
import com.stream_pi.server.client.ClientTheme;
import com.stream_pi.server.client.ClientTheme;
import com.stream_pi.server.info.ServerInfo;
import com.stream_pi.server.info.ServerInfo;
import com.stream_pi.server.window.ExceptionAndAlertHandler;
import com.stream_pi.server.window.ExceptionAndAlertHandler;
import com.stream_pi.util.alert.StreamPiAlert;
import com.stream_pi.util.alert.StreamPiAlert;
import com.stream_pi.util.alert.StreamPiAlertType;
import com.stream_pi.util.alert.StreamPiAlertType;
import com.stream_pi.util.comms.Message;
import com.stream_pi.util.comms.Message;
import com.stream_pi.util.exception.MinorException;
import com.stream_pi.util.exception.MinorException;
import com.stream_pi.util.exception.SevereException;
import com.stream_pi.util.exception.SevereException;
import com.stream_pi.util.exception.StreamPiException;
import com.stream_pi.util.exception.StreamPiException;
import com.stream_pi.util.platform.Platform;
import com.stream_pi.util.platform.Platform;
import com.stream_pi.util.platform.ReleaseStatus;
import com.stream_pi.util.platform.ReleaseStatus;
import com.stream_pi.util.version.Version;
import com.stream_pi.util.version.Version;
import javafx.concurrent.Task;
import javafx.concurrent.Task;
import java.io.*;
import java.io.*;
import java.lang.reflect.Array;
import java.lang.reflect.Array;
import java.net.Socket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.Logger;
public class ClientConnection extends Thread
public class ClientConnection extends Thread
{
{
private Socket socket;
private Socket socket;
private ServerListener serverListener;
private ServerListener serverListener;
private AtomicBoolean stop = new AtomicBoolean(false);
private AtomicBoolean stop = new AtomicBoolean(false);
private ObjectOutputStream oos;
private ObjectOutputStream oos;
private ObjectInputStream ois;
private ObjectInputStream ois;
private Logger logger;
private Logger logger;
private Client client = null;
private Client client = null;
private ExceptionAndAlertHandler exceptionAndAlertHandler;
private ExceptionAndAlertHandler exceptionAndAlertHandler;
public ClientConnection(Socket socket, ServerListener serverListener, ExceptionAndAlertHandler exceptionAndAlertHandler)
public ClientConnection(Socket socket, ServerListener serverListener, ExceptionAndAlertHandler exceptionAndAlertHandler)
{
{
this.exceptionAndAlertHandler = exceptionAndAlertHandler;
this.exceptionAndAlertHandler = exceptionAndAlertHandler;
this.socket = socket;
this.socket = socket;
this.serverListener = serverListener;
this.serverListener = serverListener;
logger = Logger.getLogger(ClientConnection.class.getName());
logger = Logger.getLogger(ClientConnection.class.getName());
try
try
{
{
oos = new ObjectOutputStream(socket.getOutputStream());
oos = new ObjectOutputStream(socket.getOutputStream());
ois = new ObjectInputStream(socket.getInputStream());
ois = new ObjectInputStream(socket.getInputStream());
} catch (IOException e) {
} catch (IOException e) {
e.printStackTrace();
e.printStackTrace();
exceptionAndAlertHandler.handleMinorException(new MinorException("Unable to start socket streams"));
exceptionAndAlertHandler.handleMinorException(new MinorException("Unable to start socket streams"));
}
}
start();
start();
}
}
public synchronized void exit()
public synchronized void exit()
{
{
if(stop.get())
if(stop.get())
return;
return;
logger.info("Stopping ...");
logger.info("Stopping ...");
try
try
{
{
if(socket !=null)
if(socket !=null)
{
{
logger.info("Stopping connection "+socket.getRemoteSocketAddress());
logger.info("Stopping connection "+socket.getRemoteSocketAddress());
disconnect();
disconnect();
}
}
}
}
catch (SevereException e)
catch (SevereException e)
{
{
e.printStackTrace();
e.printStackTrace();
exceptionAndAlertHandler.handleSevereException(e);
exceptionAndAlertHandler.handleSevereException(e);
}
}
}
}
public synchronized void exitAndRemove()
public synchronized void exitAndRemove()
{
{
exit();
exit();
removeConnection();
removeConnection();
serverListener.clearTemp();
serverListener.clearTemp();
}
}
public void removeConnection()
public void removeConnection()
{
{
ClientConnections.getInstance().removeConnection(this);
ClientConnections.getInstance().removeConnection(this);
}
}
public void sendIcon(String profileID, String actionID, byte[] icon) throws SevereException
public void sendIcon(String profileID, String actionID, byte[] icon) throws SevereException
{
{
try
try
{
{
Thread.sleep(50);
Thread.sleep(50);
}
}
catch (InterruptedException e)
catch (InterruptedException e)
{
{
e.printStackTrace();
e.printStackTrace();
}
}
Message message = new Message("action_icon");
Message message = new Message("action_icon");
message.setStringArrValue(profileID, actionID);
message.setStringArrValue(profileID, actionID);
message.setByteArrValue(icon);
message.setByteArrValue(icon);
sendMessage(message);
sendMessage(message);
}
}
public void initAfterConnectionQueryReceive(Message message) throws StreamPiException
public void initAfterConnectionQueryReceive(Message message) throws StreamPiException
{
{
String[] ar = message.getStringArrValue();
String[] ar = message.getStringArrValue();
logger.info("Setting up client object ...");
logger.info("Setting up client object ...");
Version clientVersion;
Version clientVersion;
Version commsStandard;
Version commsStandard;
Version themesStandard;
Version themesStandard;
ReleaseStatus releaseStatus;
ReleaseStatus releaseStatus;
try
try
{
{
clientVersion = new Version(ar[0]);
clientVersion = new Version(ar[0]);
releaseStatus = ReleaseStatus.valueOf(ar[1]);
releaseStatus = ReleaseStatus.valueOf(ar[1]);
commsStandard = new Version(ar[2]);
commsStandard = new Version(ar[2]);
themesStandard = new Version(ar[3]);
themesStandard = new Version(ar[3]);
}
}
catch (MinorException e)
catch (MinorException e)
{
{
exitAndRemove();
exitAndRemove();
throw new MinorException(e.getShortMessage()+" (client '"+socket.getRemoteSocketAddress()+"' )");
throw new MinorException(e.getShortMessage()+" (client '"+socket.getRemoteSocketAddress()+"' )");
}
}
if(!commsStandard.isEqual(ServerInfo.getInstance().getCommStandardVersion()))
if(!commsStandard.isEqual(ServerInfo.getInstance().getCommStandardVersion()))
{
{
String errTxt = "Server and client Communication standards do not match. Make sure you are on the latest version of server and client.\n" +
String errTxt = "Server and client Communication standards do not match. Make sure you are on the latest version of server and client.\n" +
"Server Comms. Standard : "+ServerInfo.getInstance().getCommStandardVersion().getText()+
"Server Comms. Standard : "+ServerInfo.getInstance().getCommStandardVersion().getText()+
"\nclient Comms. Standard : "+commsStandard.getText();
"\nclient Comms. Standard : "+commsStandard.getText();
disconnect(errTxt);
disconnect(errTxt);
throw new MinorException(errTxt);
throw new MinorException(errTxt);
}
}
client = new Client(clientVersion, releaseStatus, commsStandard, themesStandard, ar[4], Platform.valueOf(ar[7]), socket.getRemoteSocketAddress());
client = new Client(clientVersion, releaseStatus, commsStandard, themesStandard, ar[4], Platform.valueOf(ar[7]), socket.getRemoteSocketAddress());
client.setStartupDisplayWidth(Double.parseDouble(ar[5]));
client.setDisplayWidth(Double.parseDouble(ar[5]));
client.setStartupDisplayHeight(Double.parseDouble(ar[6]));
client.setDisplayHeight(Double.parseDouble(ar[6]));
client.setDefaultProfileID(ar[8]);
client.setDefaultProfileID(ar[8]);
client.setDefaultThemeFullName(ar[9]);
client.setDefaultThemeFullName(ar[9]);
//call get profiles command.
//call get profiles command.
serverListener.clearTemp();
serverListener.clearTemp();
}
}
public synchronized Client getClient()
public synchronized Client getClient()
{
{
return client;
return client;
}
}
@Override
@Override
public void run() {
public void run() {
try {
try {
initAfterConnectionQuerySend();
initAfterConnectionQuerySend();
} catch (SevereException e) {
} catch (SevereException e) {
e.printStackTrace();
e.printStackTrace();
exceptionAndAlertHandler.handleSevereException(e);
exceptionAndAlertHandler.handleSevereException(e);
exitAndRemove();
exitAndRemove();
return;
return;
}
}
try
try
{
{
while(!stop.get())
while(!stop.get())
{
{
try
try
{
{
Message message = (Message) ois.readObject();
Message message = (Message) ois.readObject();
String header = message.getHeader();
String header = message.getHeader();
switch (header)
switch (header)
{
{
case "action_icon" : onActionIconReceived(message);
case "action_icon" : onActionIconReceived(message);
break;
break;
case "disconnect" : clientDisconnected(message);
case "disconnect" : clientDisconnected(message);
break;
break;
case "client_details" : initAfterConnectionQueryReceive(message);
case "client_details" : initAfterConnectionQueryReceive(message);
getProfilesFromClient();
getProfilesFromClient();
getThemesFromClient();
getThemesFromClient();
break;
break;
case "profiles" : registerProfilesFromClient(message);
case "profiles" : registerProfilesFromClient(message);
break;
break;
case "profile_details" : registerProfileDetailsFromClient(message);
case "profile_details" : registerProfileDetailsFromClient(message);
break;
break;
case "action_details" : registerActionToProfile(message);
case "action_details" : registerActionToProfile(message);
break;
break;
case "themes": registerThemes(message);
case "themes": registerThemes(message);
break;
break;
case "action_clicked": actionClicked(message);
case "action_clicked": actionClicked(message);
break;
break;
default: logger.warning("Command '"+header+"' does not match records. Make sure client and server versions are equal.");
default: logger.warning("Command '"+header+"' does not match records. Make sure client and server versions are equal.");
}
}
}
}
catch (IOException | ClassNotFoundException e)
catch (IOException | ClassNotFoundException e)
{
{
logger.log(Level.SEVERE, e.getMessage());
logger.log(Level.SEVERE, e.getMessage());
e.printStackTrace();
e.printStackTrace();
serverListener.clearTemp();
serverListener.clearTemp();
if(!stop.get())
if(!stop.get())
{
{
removeConnection();
removeConnection();
throw new MinorException("Accidentally disconnected from "+getClient().getNickName()+".");
throw new MinorException("Accidentally disconnected from "+getClient().getNickName()+".");
}
}
exitAndRemove();
exitAndRemove();
return;
return;
}
}
}
}
}
}
catch (StreamPiException e)
catch (StreamPiException e)
{
{
e.printStackTrace();
e.printStackTrace();
if(e instanceof MinorException)
if(e instanceof MinorException)
exceptionAndAlertHandler.handleMinorException((MinorException) e);
exceptionAndAlertHandler.handleMinorException((MinorException) e);
else if (e instanceof SevereException)
else if (e instanceof SevereException)
exceptionAndAlertHandler.handleSevereException((SevereException) e);
exceptionAndAlertHandler.handleSevereException((SevereException) e);
}
}
}
}
// commands
// commands
private void onActionIconReceived(Message message)
private void onActionIconReceived(Message message)
{
{
String[] s = message.getStringArrValue();
String[] s = message.getStringArrValue();
String profileID = s[0];
String profileID = s[0];
String actionID = s[1];
String actionID = s[1];
getClient().getProfileByID(profileID).getActionByID(actionID).setIcon(message.getByteArrValue());
getClient().getProfileByID(profileID).getActionByID(actionID).setIcon(message.getByteArrValue());
}
}
public void initAfterConnectionQuerySend() throws SevereException
public void initAfterConnectionQuerySend() throws SevereException
{
{
logger.info("Asking client details ...");
logger.info("Asking client details ...");
sendMessage(new Message("get_client_details"));
sendMessage(new Message("get_client_details"));
}
}
public void disconnect() throws SevereException {
public void disconnect() throws SevereException {
disconnect("");
disconnect("");
}
}
public void disconnect(String message) throws SevereException {
public void disconnect(String message) throws SevereException {
if(stop.get())
if(stop.get())
return;
return;
stop.set(true);
stop.set(true);
logger.info("Sending client disconnect message ...");
logger.info("Sending client disconnect message ...");
Message m = new Message("disconnect");
Message m = new Message("disconnect");
m.setStringValue(message);
m.setStringValue(message);
sendMessage(m);
sendMessage(m);
try
try
{
{
if(!socket.isClosed())
if(!socket.isClosed())
socket.close();
socket.close();
}
}
catch (IOException e)
catch (IOException e)
{
{
e.printStackTrace();
e.printStackTrace();
throw new SevereException("Unable to close socket");
throw new SevereException("Unable to close socket");
}
}
}
}
public synchronized void sendMessage(Message message) throws SevereException
public synchronized void sendMessage(Message message) throws SevereException
{
{
try
try
{
{
oos.writeObject(message);
oos.writeObject(message);
oos.flush();
oos.flush();
}
}
catch (IOException e)
catch (IOException e)
{
{
e.printStackTrace();
e.printStackTrace();
throw new SevereException("Unable to write to io Stream!");
throw new SevereException("Unable to write to io Stream!");
}
}
}
}
public void clientDisconnected(Message message)
public void clientDisconnected(Message message)
{
{
stop.set(true);
stop.set(true);
String txt = "Disconnected!";
String txt = "Disconnected!";
String msg = message.getStringValue();
String msg = message.getStringValue();
if(!msg.isBlank())
if(!msg.isBlank())
txt = "Message : "+msg;
txt = "Message : "+msg;
new StreamPiAlert("Disconnected from "+getClient().getNickName()+".", txt, StreamPiAlertType.WARNING).show();;
new StreamPiAlert("Disconnected from "+getClient().getNickName()+".", txt, StreamPiAlertType.WARNING).show();;
exitAndRemove();
exitAndRemove();
}
}
public void getProfilesFromClient() throws StreamPiException
public void getProfilesFromClient() throws StreamPiException
{
{
logger.info("Asking client to send profiles ...");
logger.info("Asking client to send profiles ...");
sendMessage(new Message("get_profiles"));
sendMessage(new Message("get_profiles"));
}
}
public void getThemesFromClient() throws StreamPiException
public void getThemesFromClient() throws StreamPiException
{
{
logger.info("Asking clients to send themes ...");
logger.info("Asking clients to send themes ...");
sendMessage(new Message("get_themes"));
sendMessage(new Message("get_themes"));
}
}
public void registerThemes(Message message)
public void registerThemes(Message message)
{
{
String[] r = message.getStringArrValue();
String[] r = message.getStringArrValue();
for(int i =0; i<(r.length);i+=4)
for(int i =0; i<(r.length);i+=4)
{
{
ClientTheme clientTheme = new ClientTheme(
ClientTheme clientTheme = new ClientTheme(
r[i],
r[i],
r[i+1],
r[i+1],
r[i+2],
r[i+2],
r[i+3]
r[i+3]
);
);
try
try
{
{
getClient().addTheme(clientTheme);
getClient().addTheme(clientTheme);
}
}
catch (CloneNotSupportedException e)
catch (CloneNotSupportedException e)
{
{
logger.log(Level.SEVERE, e.getMessage());
logger.log(Level.SEVERE, e.getMessage());
e.printStackTrace();
e.printStackTrace();
}
}
}
}
}
}
public void registerProfilesFromClient(Message message) throws StreamPiException
public void registerProfilesFromClient(Message message) throws StreamPiException
{
{
logger.info("Registering profiles ...");
logger.info("Registering profiles ...");
String[] r = message.getStringArrValue();
String[] r = message.getStringArrValue();
for (String profileID : r) {
for (String profileID : r) {
getProfileDetailsFromClient(profileID);
getProfileDetailsFromClient(profileID);
}
}
}
}
public void getProfileDetailsFromClient(String ID) throws StreamPiException
public void getProfileDetailsFromClient(String ID) throws StreamPiException
{
{
logger.info("Asking client to send details of profile : "+ID);
logger.info("Asking client to send details of profile : "+ID);
Message message = new Message("get_profile_details");
Message message = new Message("get_profile_details");
message.setStringValue(ID);
message.setStringValue(ID);
sendMessage(message);
sendMessage(message);
}
}
public void registerProfileDetailsFromClient(Message message)
public void registerProfileDetailsFromClient(Message message)
{
{
String[] r = message.getStringArrValue();
String[] r = message.getStringArrValue();
String ID = r[0];
String ID = r[0];
logger.info("Registering details for profile : "+ID);
logger.info("Registering details for profile : "+ID);
String name = r[1];
String name = r[1];
int rows = Integer.parseInt(r[2]);
int rows = Integer.parseInt(r[2]);
int cols = Integer.parseInt(r[3]);
int cols = Integer.parseInt(r[3]);
int actionSize = Integer.parseInt(r[4]);
int actionSize = Integer.parseInt(r[4]);
int actionGap = Integer.parseInt(r[5]);
int actionGap = Integer.parseInt(r[5]);
ClientProfile clientProfile = new ClientProfile(name, ID, rows, cols, actionSize, actionGap);
ClientProfile clientProfile = new ClientProfile(name, ID, rows, cols, actionSize, actionGap);
logger.info("Added client profile "+clientProfile.getName());
logger.info("Added client profile "+clientProfile.getName());
try
try
{
{
getClient().addProfile(clientProfile);
getClient().addProfile(clientProfile);
}
}
catch (CloneNotSupportedException e)
catch (CloneNotSupportedException e)
{
{
logger.severe(e.getMessage());
logger.severe(e.getMessage());
e.printStackTrace();
e.printStackTrace();
}
}
serverListener.clearTemp();
serverListener.clearTemp();
}
}
/*public void getActionIcon(String profileID, String actionID) throws StreamPiException
/*public void getActionIcon(String profileID, String actionID) throws StreamPiException
{
{
System.out.println("getting action icon from "+profileID+", "+actionID);
System.out.println("getting action icon from "+profileID+", "+actionID);
writeToStream("get_action_icon::"+profileID+"::"+actionID);
writeToStream("get_action_icon::"+profileID+"::"+actionID);
}*/
}*/
public synchronized void registerActionToProfile(Message message) throws StreamPiException
public synchronized void registerActionToProfile(Message message) throws StreamPiException
{
{
String[] r = message.getStringArrValue();
String[] r = message.getStringArrValue();
String profileID = r[0];
String profileID = r[0];
String ID = r[1];
String ID = r[1];
ActionType actionType = ActionType.valueOf(r[2]);
ActionType actionType = ActionType.valueOf(r[2]);
//3 - Version
//3 - Version
//4 - ModuleName
//4 - ModuleName
//display
//display
String bgColorHex = r[5];
String bgColorHex = r[5];
//icon
//icon
boolean isHasIcon = r[6].equals("true");
boolean isHasIcon = r[6].equals("true");
boolean isShowIcon = r[7].equals("true");
boolean isShowIcon = r[7].equals("true");
//text
//text
boolean isShowDisplayText = r[8].equals("true");
boolean isShowDisplayText = r[8].equals("true");
String displayFontColor = r[9];
String displayFontColor = r[9];
String displayText = r[10];
String displayText = r[10];
DisplayTextAlignment displayTextAlignment = DisplayTextAlignment.valueOf(r[11]);
DisplayTextAlignment displayTextAlignment = DisplayTextAlignment.valueOf(r[11]);
//location
//location
String row = r[12];
String row = r[12];
String col = r[13];
String col = r[13];
Location location = new Location(Integer.parseInt(row), Integer.parseInt(col));
Location location = new Location(Integer.parseInt(row), Integer.parseInt(col));
Action action = new Action(ID, actionType);
Action action = new Action(ID, actionType);
action.setBgColourHex(bgColorHex);
action.setBgColourHex(bgColorHex);
action.setShowIcon(isShowIcon);
action.setShowIcon(isShowIcon);
action.setHasIcon(isHasIcon);
action.setHasIcon(isHasIcon);
action.setShowDisplayText(isShowDisplayText);
action.setShowDisplayText(isShowDisplayText);
action.setDisplayTextFontColourHex(displayFontColor);
action.setDisplayTextFontColourHex(displayFontColor);
action.setDisplayText(displayText);
action.setDisplayText(displayText);
action.setDisplayTextAlignment(displayTextAlignment);
action.setDisplayTextAlignment(displayTextAlignment);
action.setLocation(location);
action.setLocation(location);
String root = r[14];
String root = r[14];
action.setParent(root);
action.setParent(root);
action.setDelayBeforeExecuting(Integer.parseInt(r[15]));
action.setDelayBeforeExecuting(Integer.parseInt(r[15]));
//client properties
//client properties
int clientPropertiesSize = Integer.parseInt(r[16]);
int clientPropertiesSize = Integer.parseInt(r[16]);
ClientProperties clientProperties = new ClientProperties();
ClientProperties clientProperties = new ClientProperties();
if(actionType == ActionType.FOLDER)
if(actionType == ActionType.FOLDER)
clientProperties.setDuplicatePropertyAllowed(true);
clientProperties.setDuplicatePropertyAllowed(true);
for(int i = 17;i<((clientPropertiesSize*2) + 17); i+=2)
for(int i = 17;i<((clientPropertiesSize*2) + 17); i+=2)
{
{
Property property = new Property(r[i], Type.STRING);
Property property = new Property(r[i], Type.STRING);
property.setRawValue(r[i+1]);
property.setRawValue(r[i+1]);
clientProperties.addProperty(property);
clientProperties.addProperty(property);
}
}
action.setClientProperties(clientProperties);
action.setClientProperties(clientProperties);
action.setModuleName(r[4]);
action.setModuleName(r[4]);
//set up action
//set up action
//action toBeAdded = null;
//action toBeAdded = null;
if(actionType == ActionType.NORMAL)
if(actionType == ActionType.NORMAL)
{
{
NormalAction actionCopy = NormalActionPlugins.getInstance().getPluginByModuleName(r[4]);
NormalAction actionCopy = NormalActionPlugins.getInstance().getPluginByModuleName(r[4]);
if(actionCopy == null)
if(actionCopy == null)
{
{
action.setInvalid(true);
action.setInvalid(true);
}
}
else
else
{
{
action.setVersion(new Version(r[3]));
action.setVersion(new Version(r[3]));
//action.setHelpLink(actionCopy.getHelpLink());
//action.setHelpLink(actionCopy.getHelpLink());
if(actionCopy.getVersion().getMajor() != action.getVersion().getMajor())
if(actionCopy.getVersion().getMajor() != action.getVersion().getMajor())
{
{
action.setInvalid(true);
action.setInvalid(true);
}
}
else
else
{
{
action.setName(actionCopy.getName());
action.setName(actionCopy.getName());
ClientProperties finalClientProperties = new ClientProperties();
ClientProperties finalClientProperties = new ClientProperties();
for(Property property : actionCopy.getClientProperties().get())
for(Property property : actionCopy.getClientProperties().get())
{
{
for(int i = 0;i<action.getClientProperties().getSize();i++)
for(int i = 0;i<action.getClientProperties().getSize();i++)
{
{
Property property1 = action.getClientProperties().get().get(i);
Property property1 = action.getClientProperties().get().get(i);
if (property.getName().equals(property1.getName()))
if (property.getName().equals(property1.getName()))
{
{
property.setRawValue(property1.getRawValue());
property.setRawValue(property1.getRawValue());
finalClientProperties.addProperty(property);
finalClientProperties.addProperty(property);
}
}
}
}
}
}
action.setClientProperties(finalClientProperties);
action.setClientProperties(finalClientProperties);
}
}
}
}
}
}
try
try
{
{
for(Property prop : action.getClientProperties().get())
for(Property prop : action.getClientProperties().get())
{
{
logger.info("G@@@@@ : "+prop.getRawValue());
logger.info("G@@@@@ : "+prop.getRawValue());
}
}
getClient().getProfileByID(profileID).addAction(action);
getClient().getProfileByID(profileID).addAction(action);
for(String action1x : getClient().getProfileByID(profileID).getActionsKeySet())
for(String action1x : getClient().getProfileByID(profileID).getActionsKeySet())
{
{
Action action1 = getClient().getProfileByID(profileID).getActionByID(action1x);
Action action1 = getClient().getProfileByID(profileID).getActionByID(action1x);
logger.info("231cc : "+action1.getID());
logger.info("231cc : "+action1.getID());
for(Property prop : action1.getClientProperties().get())
for(Property prop : action1.getClientProperties().get())
{
{
logger.info("G@VVVV@@@ : "+prop.getRawValue());
logger.info("G@VVVV@@@ : "+prop.getRawValue());
}
}
}
}
}
}
catch (CloneNotSupportedException e)
catch (CloneNotSupportedException e)
{
{
e.printStackTrace();
e.printStackTrace();
exceptionAndAlertHandler.handleMinorException(new MinorException("action", "Unable to clone"));
exceptionAndAlertHandler.handleMinorException(new MinorException("action", "Unable to clone"));
}
}
}
}
public void saveActionDetails(String profileID, Action action) throws SevereException
public void saveActionDetails(String profileID, Action action) throws SevereException
{
{
ArrayList<String> a = new ArrayList<>();
ArrayList<String> a = new ArrayList<>();
a.add(profileID);
a.add(profileID);
a.add(action.getID());
a.add(action.getID());
a.add(action.getActionType()+"");
a.add(action.getActionType()+"");
if(action.getActionType() == ActionType.NORMAL)
if(action.getActionType() == ActionType.NORMAL)
{
{
a.add(action.getVersion().getText());
a.add(action.getVersion().getText());
System.out.println("VERSION :sdd "+action.getVersion().getText());
System.out.println("VERSION :sdd "+action.getVersion().getText());
}
}
else
else
{
{
a.add("no");
a.add("no");
}
}
if(action.getActionType() == ActionType.NORMAL)
if(action.getActionType() == ActionType.NORMAL)
{
{
a.add(action.getModuleName());
a.add(action.getModuleName());
}
}
else
else
{
{
a.add("nut");
a.add("nut");
}
}
//display
//display
a.add(action.getBgColourHex());
a.add(action.getBgColourHex());
//icon
//icon
a.add(action.isHasIcon()+"");
a.add(action.isHasIcon()+"");
a.add(action.isShowIcon()+"");
a.add(action.isShowIcon()+"");
//text
//text
a.add(action.isShowDisplayText()+"");
a.add(action.isShowDisplayText()+"");
a.add(action.getDisplayTextFontColourHex());
a.add(action.getDisplayTextFontColourHex());
a.add(action.getDisplayText());
a.add(action.getDisplayText());
a.add(action.getDisplayTextAlignment()+"");
a.add(action.getDisplayTextAlignment()+"");
//location
//location
if(action.getLocation() == null)
if(action.getLocation() == null)
{
{
a.add("-1");
a.add("-1");
a.add("-1");
a.add("-1");
}
}
else
else
{
{
a.add(action.getLocation().getRow()+"");
a.add(action.getLocation().getRow()+"");
a.add(action.getLocation().getCol()+"");
a.add(action.getLocation().getCol()+"");
}
}
a.add(action.getParent());
a.add(action.getParent());
a.add(action.getDelayBeforeExecuting()+"");
a.add(action.getDelayBeforeExecuting()+"");
//client properties
//client properties
ClientProperties clientProperties = action.getClientProperties();
ClientProperties clientProperties = action.getClientProperties();
a.add(clientProperties.getSize()+"");
a.add(clientProperties.getSize()+"");
for(Property property : clientProperties.get())
for(Property property : clientProperties.get())
{
{
a.add(property.getName());
a.add(property.getName());
a.add(property.getRawValue());
a.add(property.getRawValue());
}
}
Message message = new Message("save_action_details");
Message message = new Message("save_action_details");
String[] x = new String[a.size()];
String[] x = new String[a.size()];
x = a.toArray(x);
x = a.toArray(x);
message.setStringArrValue(x);
message.setStringArrValue(x);
sendMessage(message);
sendMessage(message);
}
}
public void deleteAction(String profileID, String actionID) throws SevereException
public void deleteAction(String profileID, String actionID) throws SevereException
{
{
Message message = new Message("delete_action");
Message message = new Message("delete_action");
message.setStringArrValue(profileID, actionID);
message.setStringArrValue(profileID, actionID);
sendMessage(message);
sendMessage(message);
}
}
public void saveClientDetails(String clientNickname, String screenWidth, String screenHeight, String defaultProfileID,
public void saveClientDetails(String clientNickname, String defaultProfileID,
String defaultThemeFullName) throws SevereException
String defaultThemeFullName) throws SevereException
{
{
Message message = new Message("save_client_details");
Message message = new Message("save_client_details");
message.setStringArrValue(
message.setStringArrValue(
clientNickname,
clientNickname,
screenWidth,
screenHeight,
defaultProfileID,
defaultProfileID,
defaultThemeFullName
defaultThemeFullName
);
);
sendMessage(message);
sendMessage(message);
client.setNickName(clientNickname);
client.setNickName(clientNickname);
client.setStartupDisplayWidth(Double.parseDouble(screenWidth));
client.setStartupDisplayHeight(Double.parseDouble(screenHeight));
client.setDefaultProfileID(defaultProfileID);
client.setDefaultProfileID(defaultProfileID);
client.setDefaultThemeFullName(defaultThemeFullName);
client.setDefaultThemeFullName(defaultThemeFullName);
}
}
public void saveProfileDetails(ClientProfile clientProfile) throws SevereException, CloneNotSupportedException {
public void saveProfileDetails(ClientProfile clientProfile) throws SevereException, CloneNotSupportedException {
if(client.getProfileByID(clientProfile.getID()) !=null)
if(client.getProfileByID(clientProfile.getID()) !=null)
{
{
client.getProfileByID(clientProfile.getID()).setName(clientProfile.getName());
client.getProfileByID(clientProfile.getID()).setName(clientProfile.getName());
client.getProfileByID(clientProfile.getID()).setRows(clientProfile.getRows());
client.getProfileByID(clientProfile.getID()).setRows(clientProfile.getRows());
client.getProfileByID(clientProfile.getID()).setCols(clientProfile.getCols());
client.getProfileByID(clientProfile.getID()).setCols(clientProfile.getCols());
client.getProfileByID(clientProfile.getID()).setActionSize(clientProfile.getActionSize());
client.getProfileByID(clientProfile.getID()).setActionSize(clientProfile.getActionSize());
client.getProfileByID(clientProfile.getID()).setActionGap(clientProfile.getActionGap());
client.getProfileByID(clientProfile.getID()).setActionGap(clientProfile.getActionGap());
}
}
else
else
client.addProfile(clientProfile);
client.addProfile(clientProfile);
Message message = new Message("save_client_profile");
Message message = new Message("save_client_profile");
message.setStringArrValue(
message.setStringArrValue(
clientProfile.getID(),
clientProfile.getID(),
clientProfile.getName(),
clientProfile.getName(),
clientProfile.getRows()+"",
clientProfile.getRows()+"",
clientProfile.getCols()+"",
clientProfile.getCols()+"",
clientProfile.getActionSize()+"",
clientProfile.getActionSize()+"",
clientProfile.getActionGap()+""
clientProfile.getActionGap()+""
);
);
sendMessage(message);
sendMessage(message);
}
}
public void deleteProfile(String ID) throws SevereException
public void deleteProfile(String ID) throws SevereException
{
{
Message message = new Message("delete_profile");
Message message = new Message("delete_profile");
message.setStringValue(ID);
message.setStringValue(ID);
sendMessage(message);
sendMessage(message);
}
}
public void actionClicked(Message message)
public void actionClicked(Message message)
{
{
try
try
{
{
String[] r = message.getStringArrValue();
String[] r = message.getStringArrValue();
String profileID = r[0];
String profileID = r[0];
String actionID = r[1];
String actionID = r[1];
Action action = client.getProfileByID(profileID).getActionByID(actionID);
Action action = client.getProfileByID(profileID).getActionByID(actionID);
if(action.getActionType() == ActionType.NORMAL)
if(action.getActionType() == ActionType.NORMAL)
{
{
NormalAction original = NormalActionPlugins.getInstance().getPluginByModuleName(
NormalAction original = NormalActionPlugins.getInstance().getPluginByModuleName(
action.getModuleName()
action.getModuleName()
);
);
if(original == null)
if(original == null)
{
{
throw new MinorException(
throw new MinorException(
"The action isn't installed on the server."
"The action isn't installed on the server."
);
);
}
}
NormalAction normalAction = original.clone();
NormalAction normalAction = original.clone();
normalAction.setLocation(action.getLocation());
normalAction.setLocation(action.getLocation());
normalAction.setDisplayText(action.getDisplayText());
normalAction.setDisplayText(action.getDisplayText());
normalAction.setID(actionID);
normalAction.setID(actionID);
normalAction.setDelayBeforeExecuting(action.getDelayBeforeExecuting());
normalAction.setDelayBeforeExecuting(action.getDelayBeforeExecuting());
normalAction.setClientProperties(action.getClientProperties());
normalAction.setClientProperties(action.getClientProperties());
new Thread(new Task<Void>() {
new Thread(new Task<Void>() {
@Override
@Override
protected Void call()
protected Void call()
{
{
try
try
{
{
System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@2 "+normalAction.getDelayBeforeExecuting());
System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@2 "+normalAction.getDelayBeforeExecuting());
Thread.sleep(normalAction.getDelayBeforeExecuting());
Thread.sleep(normalAction.getDelayBeforeExecuting());
boolean result = serverListener.onNormalActionClicked(normalAction);
boolean result = serverListener.onNormalActionClicked(normalAction);
if(!result)
if(!result)
{
{
sendActionFailed(profileID, actionID);
sendActionFailed(profileID, actionID);
}
}
}
}
catch (SevereException e)
catch (SevereException e)
{
{
exceptionAndAlertHandler.handleSevereException(e);
exceptionAndAlertHandler.handleSevereException(e);
}
}
catch (Exception e)
catch (Exception e)
{
{
e.printStackTrace();
e.printStackTrace();
}
}
return null;
return null;
}
}
}).start();
}).start();
}
}
} catch (Exception e) {
} catch (Exception e) {
e.printStackTrace();
e.printStackTrace();
exceptionAndAlertHandler.handleMinorException(new MinorException(e.getMessage()));
exceptionAndAlertHandler.handleMinorException(new MinorException(e.getMessage()));
}
}
}
}
public void sendActionFailed(String profileID, String actionID) throws SevereException {
public void sendActionFailed(String profileID, String actionID) throws SevereException {
logger.info("Sending failed status ...");
logger.info("Sending failed status ...");
Message message = new Message("action_failed");
Message message = new Message("action_failed");
message.setStringArrValue(profileID, actionID);
message.setStringArrValue(profileID, actionID);
sendMessage(message);
sendMessage(message);
}
}
}
}
package com.stream_pi.server.window.settings;
package com.stream_pi.server.window.settings;
import com.stream_pi.server.client.Client;
import com.stream_pi.server.client.Client;
import com.stream_pi.server.client.ClientProfile;
import com.stream_pi.server.client.ClientProfile;
import com.stream_pi.server.client.ClientTheme;
import com.stream_pi.server.client.ClientTheme;
import com.stream_pi.server.connection.ClientConnection;
import com.stream_pi.server.connection.ClientConnection;
import com.stream_pi.server.connection.ClientConnections;
import com.stream_pi.server.connection.ClientConnections;
import com.stream_pi.server.connection.ServerListener;
import com.stream_pi.server.connection.ServerListener;
import com.stream_pi.server.window.ExceptionAndAlertHandler;
import com.stream_pi.server.window.ExceptionAndAlertHandler;
import com.stream_pi.util.exception.MinorException;
import com.stream_pi.util.exception.MinorException;
import com.stream_pi.util.exception.SevereException;
import com.stream_pi.util.exception.SevereException;
import com.stream_pi.util.uihelper.HBoxInputBox;
import com.stream_pi.util.uihelper.HBoxInputBox;
import com.stream_pi.util.uihelper.SpaceFiller;
import com.stream_pi.util.uihelper.SpaceFiller;
import javafx.application.Platform;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.FXCollections;
import javafx.concurrent.Task;
import javafx.concurrent.Task;
import javafx.geometry.Insets;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.geometry.Pos;
import javafx.scene.CacheHint;
import javafx.scene.CacheHint;
import javafx.scene.control.*;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.layout.VBox;
import javafx.util.Callback;
import javafx.util.Callback;
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.List;
import java.util.List;
import java.util.logging.Logger;
import java.util.logging.Logger;
public class ClientsSettings extends VBox {
public class ClientsSettings extends VBox {
private VBox clientsSettingsVBox;
private VBox clientsSettingsVBox;
private Button saveButton;
private Button saveButton;
private ServerListener serverListener;
private ServerListener serverListener;
private Logger logger;
private Logger logger;
private ExceptionAndAlertHandler exceptionAndAlertHandler;
private ExceptionAndAlertHandler exceptionAndAlertHandler;
public ClientsSettings(ExceptionAndAlertHandler exceptionAndAlertHandler, ServerListener serverListener)
public ClientsSettings(ExceptionAndAlertHandler exceptionAndAlertHandler, ServerListener serverListener)
{
{
getStyleClass().add("clients_settings");
getStyleClass().add("clients_settings");
this.serverListener = serverListener;
this.serverListener = serverListener;
this.exceptionAndAlertHandler = exceptionAndAlertHandler;
this.exceptionAndAlertHandler = exceptionAndAlertHandler;
clientSettingsVBoxArrayList = new ArrayList<>();
clientSettingsVBoxArrayList = new ArrayList<>();
logger = Logger.getLogger(ClientsSettings.class.getName());
logger = Logger.getLogger(ClientsSettings.class.getName());
clientsSettingsVBox = new VBox();
clientsSettingsVBox = new VBox();
clientsSettingsVBox.getStyleClass().add("clients_settings_vbox");
clientsSettingsVBox.getStyleClass().add("clients_settings_vbox");
clientsSettingsVBox.setAlignment(Pos.TOP_CENTER);
clientsSettingsVBox.setAlignment(Pos.TOP_CENTER);
setAlignment(Pos.TOP_CENTER);
setAlignment(Pos.TOP_CENTER);
ScrollPane scrollPane = new ScrollPane();
ScrollPane scrollPane = new ScrollPane();
scrollPane.getStyleClass().add("clients_settings_scroll_pane");
scrollPane.getStyleClass().add("clients_settings_scroll_pane");
scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);
scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);
VBox.setVgrow(scrollPane, Priority.ALWAYS);
VBox.setVgrow(scrollPane, Priority.ALWAYS);
scrollPane.maxWidthProperty().bind(widthProperty().multiply(0.8));
scrollPane.maxWidthProperty().bind(widthProperty().multiply(0.8));
clientsSettingsVBox.prefWidthProperty().bind(scrollPane.widthProperty().subtract(25));
clientsSettingsVBox.prefWidthProperty().bind(scrollPane.widthProperty().subtract(25));
scrollPane.setContent(clientsSettingsVBox);
scrollPane.setContent(clientsSettingsVBox);
saveButton = new Button("Save");
saveButton = new Button("Save");
saveButton.setOnAction(event -> onSaveButtonClicked());
saveButton.setOnAction(event -> onSaveButtonClicked());
HBox hBox = new HBox(saveButton);
HBox hBox = new HBox(saveButton);
hBox.setAlignment(Pos.CENTER_RIGHT);
hBox.setAlignment(Pos.CENTER_RIGHT);
getChildren().addAll(scrollPane, hBox);
getChildren().addAll(scrollPane, hBox);
setCache(true);
setCache(true);
setCacheHint(CacheHint.SPEED);
setCacheHint(CacheHint.SPEED);
}
}
public void onSaveButtonClicked()
public void onSaveButtonClicked()
{
{
new Thread(new Task<Void>() {
new Thread(new Task<Void>() {
@Override
@Override
protected Void call()
protected Void call()
{
{
try
try
{
{
Platform.runLater(()->saveButton.setDisable(true));
Platform.runLater(()->saveButton.setDisable(true));
StringBuilder finalErrors = new StringBuilder();
StringBuilder finalErrors = new StringBuilder();
for(ClientSettingsVBox clientSettingsVBox : clientSettingsVBoxArrayList)
for(ClientSettingsVBox clientSettingsVBox : clientSettingsVBoxArrayList)
{
{
StringBuilder errors = new StringBuilder();
StringBuilder errors = new StringBuilder();
if(clientSettingsVBox.getNickname().isBlank())
if(clientSettingsVBox.getNickname().isBlank())
errors.append(" Cannot have blank nickname. \n");
errors.append(" Cannot have blank nickname. \n");
try {
Double.parseDouble(clientSettingsVBox.getStartupWindowHeight());
}
catch (NumberFormatException e)
{
errors.append(" Must have integer display height. \n");
}
try {
Double.parseDouble(clientSettingsVBox.getStartupWindowWidth());
}
catch (NumberFormatException e)
{
errors.append(" Must have integer display width. \n");
}
for(ClientProfileVBox clientProfileVBox : clientSettingsVBox.getClientProfileVBoxes())
for(ClientProfileVBox clientProfileVBox : clientSettingsVBox.getClientProfileVBoxes())
{
{
StringBuilder errors2 = new StringBuilder();
StringBuilder errors2 = new StringBuilder();
if(clientProfileVBox.getName().isBlank())
if(clientProfileVBox.getName().isBlank())
errors2.append(" cannot have blank nickname. \n");
errors2.append(" cannot have blank nickname. \n");
try {
try {
Integer.parseInt(clientProfileVBox.getActionSize());
Integer.parseInt(clientProfileVBox.getActionSize());
}
}
catch (NumberFormatException e)
catch (NumberFormatException e)
{
{
errors2.append(" Must have integer action Size. \n");
errors2.append(" Must have integer action Size. \n");
}
}
try {
try {
Integer.parseInt(clientProfileVBox.getActionGap());
Integer.parseInt(clientProfileVBox.getActionGap());
}
}
catch (NumberFormatException e)
catch (NumberFormatException e)
{
{
errors2.append(" Must have integer action Gap. \n");
errors2.append(" Must have integer action Gap. \n");
}
}
try {
try {
int rows = Integer.parseInt(clientProfileVBox.getRows());
int rows = Integer.parseInt(clientProfileVBox.getRows());
int actionsSize = Integer.parseInt(clientProfileVBox.getActionSize());
int actionsSize = Integer.parseInt(clientProfileVBox.getActionSize());
double startupWidth = Double.parseDouble(clientSettingsVBox.getStartupWindowWidth());
if((rows*actionsSize) > (startupWidth - 50))
if((rows*actionsSize) > (clientSettingsVBox.getDisplayWidth() - 50))
{
{
errors2.append(" Rows out of bounds of screen size. \n");
errors2.append(" Rows out of bounds of screen size. \n");
}
}
}
}
catch (NumberFormatException e)
catch (NumberFormatException e)
{
{
errors2.append(" Must have integer Rows. \n");
errors2.append(" Must have integer Rows. \n");
}
}
try {
try {
int cols = Integer.parseInt(clientProfileVBox.getCols());
int cols = Integer.parseInt(clientProfileVBox.getCols());
int actionsSize = Integer.parseInt(clientProfileVBox.getActionSize());
int actionsSize = Integer.parseInt(clientProfileVBox.getActionSize());
double startupHeight = Double.parseDouble(clientSettingsVBox.getStartupWindowHeight());
if((cols*actionsSize) > (startupHeight - 50))
if((cols*actionsSize) > (clientSettingsVBox.getDisplayHeight() - 50))
{
{
errors2.append(" Cols out of bounds of screen size. \n");
errors2.append(" Cols out of bounds of screen size. \n");
}
}
}
}
catch (NumberFormatException e)
catch (NumberFormatException e)
{
{
errors2.append(" Must have integer Columns. \n");
errors2.append(" Must have integer Columns. \n");
}
}
if(!errors2.toString().isEmpty())
if(!errors2.toString().isEmpty())
{
{
errors.append(" ")
errors.append(" ")
.append(clientProfileVBox.getRealName())
.append(clientProfileVBox.getRealName())
.append("\n")
.append("\n")
.append(errors2.toString())
.append(errors2.toString())
.append("\n");
.append("\n");
}
}
}
}
if(!errors.toString().isEmpty())
if(!errors.toString().isEmpty())
{
{
finalErrors.append("* ")
finalErrors.append("* ")
.append(clientSettingsVBox.getRealNickName())
.append(clientSettingsVBox.getRealNickName())
.append("\n")
.append("\n")
.append(errors.toString())
.append(errors.toString())
.append("\n");
.append("\n");
}
}
}
}
if(!finalErrors.toString().isEmpty())
if(!finalErrors.toString().isEmpty())
throw new MinorException("You made form mistakes",
throw new MinorException("You made form mistakes",
"Please fix the following issues : \n"+finalErrors.toString());
"Please fix the following issues : \n"+finalErrors.toString());
//save details and values
//save details and values
for(ClientSettingsVBox clientSettingsVBox : clientSettingsVBoxArrayList)
for(ClientSettingsVBox clientSettingsVBox : clientSettingsVBoxArrayList)
{
{
clientSettingsVBox.saveClientAndProfileDetails();
clientSettingsVBox.saveClientAndProfileDetails();
}
}
loadData();
loadData();
serverListener.clearTemp();
serverListener.clearTemp();
}
}
catch (MinorException e)
catch (MinorException e)
{
{
e.printStackTrace();
e.printStackTrace();
exceptionAndAlertHandler.handleMinorException(e);
exceptionAndAlertHandler.handleMinorException(e);
}
}
catch (SevereException e)
catch (SevereException e)
{
{
e.printStackTrace();
e.printStackTrace();
exceptionAndAlertHandler.handleSevereException(e);
exceptionAndAlertHandler.handleSevereException(e);
}
}
catch (CloneNotSupportedException e)
catch (CloneNotSupportedException e)
{
{
e.printStackTrace();
e.printStackTrace();
exceptionAndAlertHandler.handleSevereException(new SevereException(
exceptionAndAlertHandler.handleSevereException(new SevereException(
e.getMessage()
e.getMessage()
));
));
}
}
finally
finally
{
{
Platform.runLater(()->saveButton.setDisable(false));
Platform.runLater(()->saveButton.setDisable(false));
}
}
return null;
return null;
}
}
}).start();
}).start();
}
}
private ArrayList<ClientSettingsVBox> clientSettingsVBoxArrayList;
private ArrayList<ClientSettingsVBox> clientSettingsVBoxArrayList;
public void loadData()
public void loadData()
{
{
logger.info("Loading client data into ClientsSettings ...");
logger.info("Loading client data into ClientsSettings ...");
Platform.runLater(()-> clientsSettingsVBox.getChildren().clear());
Platform.runLater(()-> clientsSettingsVBox.getChildren().clear());
clientSettingsVBoxArrayList.clear();
clientSettingsVBoxArrayList.clear();
List<ClientConnection> clientConnections = ClientConnections.getInstance().getConnections();
List<ClientConnection> clientConnections = ClientConnections.getInstance().getConnections();
if(clientConnections.size() == 0)
if(clientConnections.size() == 0)
{
{
Platform.runLater(()->{
Platform.runLater(()->{
clientsSettingsVBox.getChildren().add(new Label("No Clients Connected."));
clientsSettingsVBox.getChildren().add(new Label("No Clients Connected."));
saveButton.setVisible(false);
saveButton.setVisible(false);
});
});
}
}
else
else
{
{
Platform.runLater(()->saveButton.setVisible(true));
Platform.runLater(()->saveButton.setVisible(true));
for (ClientConnection clientConnection : clientConnections) {
for (ClientConnection clientConnection : clientConnections) {
ClientSettingsVBox clientSettingsVBox = new ClientSettingsVBox(clientConnection);
ClientSettingsVBox clientSettingsVBox = new ClientSettingsVBox(clientConnection);
clientSettingsVBoxArrayList.add(clientSettingsVBox);
clientSettingsVBoxArrayList.add(clientSettingsVBox);
Platform.runLater(()->clientsSettingsVBox.getChildren().add(clientSettingsVBox));
Platform.runLater(()->clientsSettingsVBox.getChildren().add(clientSettingsVBox));
}
}
}
}
logger.info("... Done!");
logger.info("... Done!");
}
}
public class ClientSettingsVBox extends VBox
public class ClientSettingsVBox extends VBox
{
{
private ComboBox<ClientProfile> profilesComboBox;
private ComboBox<ClientProfile> profilesComboBox;
private ComboBox<ClientTheme> themesComboBox;
private ComboBox<ClientTheme> themesComboBox;
private TextField startupWindowHeightTextField;
private double displayHeight;
private double displayWidth;
public double getDisplayHeight() {
return displayHeight;
}
public String getStartupWindowHeight() {
public void setDisplayHeight(double displayHeight) {
return startupWindowHeightTextField.getText();
this.displayHeight = displayHeight;
}
}
private TextField startupWindowWidthTextField;
public double getDisplayWidth() {
return displayWidth;
}
public String getStartupWindowWidth() {
public void setDisplayWidth(double displayWidth) {
return startupWindowWidthTextField.getText();
this.displayWidth = displayWidth;
}
}
private TextField nicknameTextField;
private TextField nicknameTextField;
public String getNickname() {
public String getNickname() {
return nicknameTextField.getText();
return nicknameTextField.getText();
}
}
private Label nickNameLabel;
private Label nickNameLabel;
private Label versionLabel;
private Label versionLabel;
public String getRealNickName()
public String getRealNickName()
{
{
return nickNameLabel.getText();
return nickNameLabel.getText();
}
}
private com.stream_pi.util.platform.Platform platform;
private com.stream_pi.util.platform.Platform platform;
public com.stream_pi.util.platform.Platform getPlatform() {
public com.stream_pi.util.platform.Platform getPlatform() {
return platform;
return platform;
}
}
private Label socketConnectionLabel;
private Label socketConnectionLabel;
private ClientConnection connection;
private ClientConnection connection;
private Accordion profilesAccordion;
private Accordion profilesAccordion;
private ArrayList<ClientProfileVBox> clientProfileVBoxes;
private ArrayList<ClientProfileVBox> clientProfileVBoxes;
private Label platformLabel;
private Label platformLabel;
private HBoxInputBox startupWindowHeightInputBox, startupWindowWidthInputBox;
public ArrayList<ClientProfileVBox> getClientProfileVBoxes() {
public ArrayList<ClientProfileVBox> getClientProfileVBoxes() {
return clientProfileVBoxes;
return clientProfileVBoxes;
}
}
public ClientSettingsVBox(ClientConnection connection)
public ClientSettingsVBox(ClientConnection connection)
{
{
this.connection = connection;
this.connection = connection;
this.platform = connection.getClient().getPlatform();
this.platform = connection.getClient().getPlatform();
clientProfileVBoxes = new ArrayList<>();
clientProfileVBoxes = new ArrayList<>();
getStyleClass().add("clients_settings_each_client_box");
getStyleClass().add("clients_settings_each_client_box");
initUI();
initUI();
loadValues();
loadValues();
}
}
public ClientConnection getConnection()
public ClientConnection getConnection()
{
{
return connection;
return connection;
}
}
public void saveClientAndProfileDetails() throws SevereException, CloneNotSupportedException, MinorException {
public void saveClientAndProfileDetails() throws SevereException, CloneNotSupportedException, MinorException {
getConnection().saveClientDetails(
getConnection().saveClientDetails(
nicknameTextField.getText(),
nicknameTextField.getText(),
startupWindowWidthTextField.getText(),
startupWindowHeightTextField.getText(),
profilesComboBox.getSelectionModel().getSelectedItem().getID(),
profilesComboBox.getSelectionModel().getSelectedItem().getID(),
themesComboBox.getSelectionModel().getSelectedItem().getThemeFullName()
themesComboBox.getSelectionModel().getSelectedItem().getThemeFullName()
);
);
System.out.println("OUT");
System.out.println("OUT");
logger.info("Profiles : ");
logger.info("Profiles : ");
for(ClientProfileVBox clientProfileVBox : clientProfileVBoxes)
for(ClientProfileVBox clientProfileVBox : clientProfileVBoxes)
{
{
logger.info("Name : "+clientProfileVBox.getClientProfile().getName());
logger.info("Name : "+clientProfileVBox.getClientProfile().getName());
getConnection().saveProfileDetails(clientProfileVBox.getClientProfile());
getConnection().saveProfileDetails(clientProfileVBox.getClientProfile());
}
}
//remove deleted client profiles
//remove deleted client profiles
for(ClientProfile clientProfile : connection.getClient().getAllClientProfiles())
for(ClientProfile clientProfile : connection.getClient().getAllClientProfiles())
{
{
boolean found = false;
boolean found = false;
for(ClientProfileVBox clientProfileVBox : clientProfileVBoxes)
for(ClientProfileVBox clientProfileVBox : clientProfileVBoxes)
{
{
if(clientProfileVBox.getClientProfile().getID().equals(clientProfile.getID()))
if(clientProfileVBox.getClientProfile().getID().equals(clientProfile.getID()))
{
{
found = true;
found = true;
break;
break;
}
}
}
}
if(!found)
if(!found)
{
{
connection.getClient().removeProfileFromID(clientProfile.getID());
connection.getClient().removeProfileFromID(clientProfile.getID());
connection.deleteProfile(clientProfile.getID());
connection.deleteProfile(clientProfile.getID());
}
}
}
}
}
}
public void initUI()
public void initUI()
{
{
profilesComboBox = new ComboBox<>();
profilesComboBox = new ComboBox<>();
Callback<ListView<ClientProfile>, ListCell<ClientProfile>> profilesComboBoxFactory = new Callback<>() {
Callback<ListView<ClientProfile>, ListCell<ClientProfile>> profilesComboBoxFactory = new Callback<>() {
@Override
@Override
public ListCell<ClientProfile> call(ListView<ClientProfile> clientConnectionListView) {
public ListCell<ClientProfile> call(ListView<ClientProfile> clientConnectionListView) {
return new ListCell<>() {
return new ListCell<>() {
@Override
@Override
protected void updateItem(ClientProfile clientProfile, boolean b) {
protected void updateItem(ClientProfile clientProfile, boolean b) {
super.updateItem(clientProfile, b);
super.updateItem(clientProfile, b);
if(clientProfile == null)
if(clientProfile == null)
{
{
setText(null);
setText(null);
}
}
else
else
{
{
setText(clientProfile.getName());
setText(clientProfile.getName());
}
}
}
}
};
};
}
}
};
};
profilesComboBox.setCellFactory(profilesComboBoxFactory);
profilesComboBox.setCellFactory(profilesComboBoxFactory);
profilesComboBox.setButtonCell(profilesComboBoxFactory.call(null));
profilesComboBox.setButtonCell(profilesComboBoxFactory.call(null));
themesComboBox = new ComboBox<>();
themesComboBox = new ComboBox<>();
Callback<ListView<ClientTheme>, ListCell<ClientTheme>> themesComboBoxFactory = new Callback<>() {
Callback<ListView<ClientTheme>, ListCell<ClientTheme>> themesComboBoxFactory = new Callback<>() {
@Override
@Override
public ListCell<ClientTheme> call(ListView<ClientTheme> clientConnectionListView) {
public ListCell<ClientTheme> call(ListView<ClientTheme> clientConnectionListView) {
return new ListCell<>() {
return new ListCell<>() {
@Override
@Override
protected void updateItem(ClientTheme clientTheme, boolean b) {
protected void updateItem(ClientTheme clientTheme, boolean b) {
super.updateItem(clientTheme, b);
super.updateItem(clientTheme, b);
if(clientTheme == null)
if(clientTheme == null)
{
{
setText(null);
setText(null);
}
}
else
else
{
{
setText(clientTheme.getShortName());
setText(clientTheme.getShortName());
}
}
}
}
};
};
}
}
};
};
themesComboBox.setCellFactory(themesComboBoxFactory);
themesComboBox.setCellFactory(themesComboBoxFactory);
themesComboBox.setButtonCell(themesComboBoxFactory.call(null));
themesComboBox.setButtonCell(themesComboBoxFactory.call(null));
startupWindowHeightTextField = new TextField();
startupWindowWidthTextField = new TextField();
platformLabel = new Label();
platformLabel = new Label();
platformLabel.getStyleClass().add("client_settings_each_client_platform_label");
platformLabel.getStyleClass().add("client_settings_each_client_platform_label");
socketConnectionLabel = new Label();
socketConnectionLabel = new Label();
socketConnectionLabel.getStyleClass().add("client_settings_each_client_socket_connection_label");
socketConnectionLabel.getStyleClass().add("client_settings_each_client_socket_connection_label");
nicknameTextField = new TextField();
nicknameTextField = new TextField();
nickNameLabel = new Label();
nickNameLabel = new Label();
nickNameLabel.getStyleClass().add("client_settings_each_client_nick_name_label");
nickNameLabel.getStyleClass().add("client_settings_each_client_nick_name_label");
versionLabel = new Label();
versionLabel = new Label();
versionLabel.getStyleClass().add("client_settings_each_client_version_label");
versionLabel.getStyleClass().add("client_settings_each_client_version_label");
profilesAccordion = new Accordion();
profilesAccordion = new Accordion();
profilesAccordion.getStyleClass().add("client_settings_each_client_profiles_accordion");
profilesAccordion.getStyleClass().add("client_settings_each_client_profiles_accordion");
VBox.setMargin(profilesAccordion, new Insets(0,0,20,0));
VBox.setMargin(profilesAccordion, new Insets(0,0,20,0));
Button addNewProfileButton = new Button("Add new Profile");
Button addNewProfileButton = new Button("Add new Profile");
addNewProfileButton.setOnAction(event -> onNewProfileButtonClicked());
addNewProfileButton.setOnAction(event -> onNewProfileButtonClicked());
setSpacing(10.0);
setSpacing(10.0);
getStyleClass().add("settings_clients_each_client");
getStyleClass().add("settings_clients_each_client");
startupWindowHeightInputBox = new HBoxInputBox("Startup window Height", startupWindowHeightTextField);
startupWindowHeightInputBox.managedProperty().bind(startupWindowHeightInputBox.visibleProperty());
startupWindowWidthInputBox = new HBoxInputBox("Startup window Width", startupWindowWidthTextField);
startupWindowWidthInputBox.managedProperty().bind(startupWindowWidthInputBox.visibleProperty());
this.getChildren().addAll(
this.getChildren().addAll(
nickNameLabel,
nickNameLabel,
socketConnectionLabel,
socketConnectionLabel,
platformLabel,
platformLabel,
versionLabel,
versionLabel,
new HBoxInputBox("Nickname",nicknameTextField),
new HBoxInputBox("Nickname",nicknameTextField),
new HBox(
new HBox(
new Label("Theme"),
new Label("Theme"),
SpaceFiller.horizontal(),
SpaceFiller.horizontal(),
themesComboBox
themesComboBox
),
),
startupWindowHeightInputBox,
startupWindowWidthInputBox,
new HBox(new Label("Startup Profile"),
new HBox(new Label("Startup Profile"),
SpaceFiller.horizontal(),
SpaceFiller.horizontal(),
profilesComboBox),
profilesComboBox),
addNewProfileButton,
addNewProfileButton,
profilesAccordion);
profilesAccordion);
}
}
public void loadValues()
public void loadValues()
{
{
Client client = connection.getClient();
Client client = connection.getClient();
profilesComboBox.setItems(FXCollections.observableList(client.getAllClientProfiles()));
profilesComboBox.setItems(FXCollections.observableList(client.getAllClientProfiles()));
profilesComboBox.getSelectionModel().select(
profilesComboBox.getSelectionModel().select(
client.getProfileByID(client.getDefaultProfileID())
client.getProfileByID(client.getDefaultProfileID())
);
);
themesComboBox.setItems(FXCollections.observableList(client.getThemes()));
themesComboBox.setItems(FXCollections.observableList(client.getThemes()));
themesComboBox.getSelectionModel().select(
themesComboBox.getSelectionModel().select(
client.getThemeByFullName(
client.getThemeByFullName(
client.getDefaultThemeFullName()
client.getDefaultThemeFullName()
)
)
);
);
nicknameTextField.setText(client.getNickName());
nicknameTextField.setText(client.getNickName());
if(client.getPlatform() == com.stream_pi.util.platform.Platform.ANDROID)
{
startupWindowHeightInputBox.setVisible(false);
startupWindowWidthInputBox.setVisible(false);
}
platformLabel.setText("Platform : "+client.getPlatform().getUIName());
platformLabel.setText("Platform : "+client.getPlatform().getUIName());
startupWindowWidthTextField.setText(client.getStartupDisplayWidth()+"");
setDisplayHeight(client.getDisplayHeight());
startupWindowHeightTextField.setText(client.getStartupDisplayHeight()+"");
setDisplayWidth(client.getDisplayWidth());
socketConnectionLabel.setText(client.getRemoteSocketAddress().toString().substring(1)); //substring removes the `/`
socketConnectionLabel.setText(client.getRemoteSocketAddress().toString().substring(1)); //substring removes the `/`
nickNameLabel.setText(client.getNickName());
nickNameLabel.setText(client.getNickName());
versionLabel.setText(client.getReleaseStatus().getUIName()+" "+client.getVersion().getText());
versionLabel.setText(client.getReleaseStatus().getUIName()+" "+client.getVersion().getText());
//add profiles
//add profiles
for(ClientProfile clientProfile : client.getAllClientProfiles())
for(ClientProfile clientProfile : client.getAllClientProfiles())
{
{
TitledPane titledPane = new TitledPane();
TitledPane titledPane = new TitledPane();
titledPane.getStyleClass().add("client_settings_each_client_accordion_each_titled_pane");
titledPane.getStyleClass().add("client_settings_each_client_accordion_each_titled_pane");
titledPane.setText(clientProfile.getName());
titledPane.setText(clientProfile.getName());
ClientProfileVBox clientProfileVBox = new ClientProfileVBox(clientProfile);
ClientProfileVBox clientProfileVBox = new ClientProfileVBox(clientProfile);
clientProfileVBox.getRemoveButton().setOnAction(event -> onProfileDeleteButtonClicked(clientProfileVBox, titledPane));
clientProfileVBox.getRemoveButton().setOnAction(event -> onProfileDeleteButtonClicked(clientProfileVBox, titledPane));
titledPane.setContent(clientProfileVBox);
titledPane.setContent(clientProfileVBox);
clientProfileVBoxes.add(clientProfileVBox);
clientProfileVBoxes.add(clientProfileVBox);
profilesAccordion.getPanes().add(titledPane);
profilesAccordion.getPanes().add(titledPane);
}
}
}
}
public void onNewProfileButtonClicked()
public void onNewProfileButtonClicked()
{
{
ClientProfile clientProfile = new ClientProfile(
ClientProfile clientProfile = new ClientProfile(
"Untitled Profile",
"Untitled Profile",
3,
3,
3,
3,
100,
100,
5
5
);
);
ClientProfileVBox clientProfileVBox = new ClientProfileVBox(clientProfile);
ClientProfileVBox clientProfileVBox = new ClientProfileVBox(clientProfile);
TitledPane titledPane = new TitledPane();
TitledPane titledPane = new TitledPane();
titledPane.setContent(clientProfileVBox);
titledPane.setContent(clientProfileVBox);
titledPane.setText(clientProfile.getName());
titledPane.setText(clientProfile.getName());
clientProfileVBox.getRemoveButton().setOnAction(event -> onProfileDeleteButtonClicked(clientProfileVBox, titledPane));
clientProfileVBox.getRemoveButton().setOnAction(event -> onProfileDeleteButtonClicked(clientProfileVBox, titledPane));
clientProfileVBoxes.add(clientProfileVBox);
clientProfileVBoxes.add(clientProfileVBox);
profilesAccordion.getPanes().add(titledPane);
profilesAccordion.getPanes().add(titledPane);
}
}
public void onProfileDeleteButtonClicked(ClientProfileVBox clientProfileVBox, TitledPane titledPane)
public void onProfileDeleteButtonClicked(ClientProfileVBox clientProfileVBox, TitledPane titledPane)
{
{
if(clientProfileVBoxes.size() == 1)
if(clientProfileVBoxes.size() == 1)
{
{
exceptionAndAlertHandler.handleMinorException(new MinorException("Only one",
exceptionAndAlertHandler.handleMinorException(new MinorException("Only one",
"You cannot delete all profiles"));
"You cannot delete all profiles"));
}
}
else
else
{
{
if(profilesComboBox.getSelectionModel().getSelectedItem().getID().equals(clientProfileVBox.getClientProfile().getID()))
if(profilesComboBox.getSelectionModel().getSelectedItem().getID().equals(clientProfileVBox.getClientProfile().getID()))
{
{
exceptionAndAlertHandler.handleMinorException(new MinorException("Default",
exceptionAndAlertHandler.handleMinorException(new MinorException("Default",
"You cannot delete default profile. Change to another one to delete this."));
"You cannot delete default profile. Change to another one to delete this."));
}
}
else
else
{
{
clientProfileVBoxes.remove(clientProfileVBox);
clientProfileVBoxes.remove(clientProfileVBox);
profilesComboBox.getItems().remove(clientProfileVBox.getClientProfile());
profilesComboBox.getItems().remove(clientProfileVBox.getClientProfile());
profilesAccordion.getPanes().remove(titledPane);
profilesAccordion.getPanes().remove(titledPane);
}
}
}
}
}
}
}
}
public class ClientProfileVBox extends VBox
public class ClientProfileVBox extends VBox
{
{
private TextField nameTextField;
private TextField nameTextField;
public String getName()
public String getName()
{
{
return nameTextField.getText();
return nameTextField.getText();
}
}
private TextField rowsTextField;
private TextField rowsTextField;
public String getRows()
public String getRows()
{
{
return rowsTextField.getText();
return rowsTextField.getText();
}
}
private TextField colsTextField;
private TextField colsTextField;
public String getCols()
public String getCols()
{
{
return colsTextField.getText();
return colsTextField.getText();
}
}
private TextField actionSizeTextField;
private TextField actionSizeTextField;
public String getActionSize()
public String getActionSize()
{
{
return actionSizeTextField.getText();
return actionSizeTextField.getText();
}
}
private TextField actionGapTextField;
private TextField actionGapTextField;
public String getActionGap()
public String getActionGap()
{
{
return actionGapTextField.getText();
return actionGapTextField.getText();
}
}
private Button removeButton;
private Button removeButton;
private ClientProfile clientProfile;
private ClientProfile clientProfile;
public String getRealName()
public String getRealName()
{
{
return clientProfile.getName();
return clientProfile.getName();
}
}
public ClientProfileVBox(ClientProfile clientProfile)
public ClientProfileVBox(ClientProfile clientProfile)
{
{
this.clientProfile = clientProfile;
this.clientProfile = clientProfile;
getStyleClass().add("client_settings_each_client_accordion_each_profile_box");
getStyleClass().add("client_settings_each_client_accordion_each_profile_box");
initUI();
initUI();
loadValues(clientProfile);
loadValues(clientProfile);
}
}
public void initUI()
public void initUI()
{
{
setPadding(new Insets(5.0));
setPadding(new Insets(5.0));
setSpacing(10.0);
setSpacing(10.0);
nameTextField = new TextField();
nameTextField = new TextField();
rowsTextField = new TextField();
rowsTextField = new TextField();
colsTextField = new TextField();
colsTextField = new TextField();
actionSizeTextField = new TextField();
actionSizeTextField = new TextField();
actionGapTextField = new TextField();
actionGapTextField = new TextField();
removeButton = new Button("Remove");
removeButton = new Button("Remove");
HBox hBox = new HBox(removeButton);
HBox hBox = new HBox(removeButton);
hBox.setAlignment(Pos.CENTER_RIGHT);
hBox.setAlignment(Pos.CENTER_RIGHT);
getChildren().addAll(
getChildren().addAll(
new HBoxInputBox("Name ", nameTextField),
new HBoxInputBox("Name ", nameTextField),
new HBoxInputBox("Columns", rowsTextField),
new HBoxInputBox("Columns", rowsTextField),
new HBoxInputBox("Rows", colsTextField),
new HBoxInputBox("Rows", colsTextField),
new HBoxInputBox("action Size", actionSizeTextField),
new HBoxInputBox("action Size", actionSizeTextField),
new HBoxInputBox("action Gap", actionGapTextField),
new HBoxInputBox("action Gap", actionGapTextField),
hBox
hBox
);
);
}
}
public Button getRemoveButton()
public Button getRemoveButton()
{
{
return removeButton;
return removeButton;
}
}
public void loadValues(ClientProfile clientProfile)
public void loadValues(ClientProfile clientProfile)
{
{
nameTextField.setText(clientProfile.getName());
nameTextField.setText(clientProfile.getName());
rowsTextField.setText(clientProfile.getRows()+"");
rowsTextField.setText(clientProfile.getRows()+"");
colsTextField.setText(clientProfile.getCols()+"");
colsTextField.setText(clientProfile.getCols()+"");
actionSizeTextField.setText(clientProfile.getActionSize()+"");
actionSizeTextField.setText(clientProfile.getActionSize()+"");
actionGapTextField.setText(clientProfile.getActionGap()+"");
actionGapTextField.setText(clientProfile.getActionGap()+"");
}
}
public ClientProfile getClientProfile()
public ClientProfile getClientProfile()
{
{
clientProfile.setActionGap(Integer.parseInt(actionGapTextField.getText()));
clientProfile.setActionGap(Integer.parseInt(actionGapTextField.getText()));
clientProfile.setActionSize(Integer.parseInt(actionSizeTextField.getText()));
clientProfile.setActionSize(Integer.parseInt(actionSizeTextField.getText()));
clientProfile.setRows(Integer.parseInt(rowsTextField.getText()));
clientProfile.setRows(Integer.parseInt(rowsTextField.getText()));
clientProfile.setCols(Integer.parseInt(colsTextField.getText()));
clientProfile.setCols(Integer.parseInt(colsTextField.getText()));
clientProfile.setName(nameTextField.getText());
clientProfile.setName(nameTextField.getText());
return clientProfile;
return clientProfile;
}
}
}
}
}
}