From: Debayan Sutradhar Date: Thu, 01 Apr 2021 01:46:28 +0530 Subject: Merge pull request #36 from abhinayagarwal/updates --- Merge pull request #36 from abhinayagarwal/updates Refactor code --- --- 'a/src/main/java/com/stream_pi/server/Main.java' +++ b/src/main/java/com/stream_pi/server/Main.java @@ -13,8 +13,6 @@ GNU General Public License for more deta Written by : Debayan Sutradhar (rnayabed) */ - - package com.stream_pi.server; import com.stream_pi.server.controller.Controller; @@ -47,7 +45,6 @@ public class Main extends Application { ServerInfo.getInstance().setStartMinimised(val.equals("min")); } - Controller d = new Controller(); Scene s = new Scene(d); stage.setScene(s); @@ -61,7 +58,6 @@ public class Main extends Application { */ public static void main(String[] args) { - launch(args); } } --- 'a/src/main/java/com/stream_pi/server/action/ExternalPlugins.java' +++ b/src/main/java/com/stream_pi/server/action/ExternalPlugins.java @@ -16,15 +16,6 @@ Written by : Debayan Sutradhar (rnayabed package com.stream_pi.server.action; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.transform.Result; -import javax.xml.transform.Source; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; - import com.stream_pi.action_api.action.Action; import com.stream_pi.action_api.action.ActionType; import com.stream_pi.action_api.action.PropertySaver; @@ -33,18 +24,29 @@ import com.stream_pi.action_api.actionpr import com.stream_pi.action_api.actionproperty.property.Property; import com.stream_pi.action_api.actionproperty.property.Type; import com.stream_pi.action_api.externalplugin.ExternalPlugin; -import com.stream_pi.action_api.externalplugin.NormalAction; import com.stream_pi.util.exception.MinorException; import com.stream_pi.util.exception.SevereException; import com.stream_pi.util.exception.StreamPiException; import com.stream_pi.util.version.Version; import com.stream_pi.util.xmlconfighelper.XMLConfigHelper; -import org.w3c.dom.NodeList; import org.w3c.dom.Document; +import org.w3c.dom.Element; import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.Result; +import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; import java.io.File; -import java.lang.module.*; +import java.lang.module.Configuration; +import java.lang.module.ModuleDescriptor; +import java.lang.module.ModuleFinder; +import java.lang.module.ModuleReference; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; @@ -55,8 +57,6 @@ import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Collectors; -import org.w3c.dom.Element; - public class ExternalPlugins { private static ExternalPlugins instance = null; @@ -169,19 +169,15 @@ public class ExternalPlugins NodeList actionsNode = document.getElementsByTagName("actions").item(0).getChildNodes(); - for(int i =0;i eachPluginStoredProperties = action.getServerProperties().get(); List eachPluginCodeProperties = eachPlugin.getServerProperties().get(); - for (int i =0;i< eachPluginCodeProperties.size(); i++) { Property eachPluginCodeProperty = eachPluginCodeProperties.get(i); @@ -328,9 +320,7 @@ public class ExternalPlugins } } - eachPlugin.getServerProperties().set(eachPluginCodeProperties); - break; } } @@ -347,16 +337,10 @@ public class ExternalPlugins } } - - if (!sortedPlugins.containsKey(eachPlugin.getCategory())) { - ArrayList actions = new ArrayList<>(); - - sortedPlugins.put(eachPlugin.getCategory(), actions); + sortedPlugins.put(eachPlugin.getCategory(), new ArrayList<>()); } - sortedPlugins.get(eachPlugin.getCategory()).add(eachPlugin); - } catch (Exception e) { @@ -383,12 +367,10 @@ public class ExternalPlugins errors.append("\n * ").append(errorModules.get(i).getModuleName()).append("\n(") .append(errorModuleError.get(i)).append(")"); } - throw new MinorException("Plugins", errors.toString()); } - - for(int i = 0;i { - openSettingsTimeLine.play(); + openSettingsAnimation.play(); }); getSettingsPane().getCloseButton().setOnAction(event -> { - closeSettingsTimeLine.play(); + closeSettingsAnimation.play(); }); getSettingsPane().getThemesSettings().setController(this); - mainServer = new MainServer(this, this); - if(getConfig().isFirstTimeUse()) { Stage stage = new Stage(); @@ -144,8 +142,8 @@ public class Controller extends Base imp { handleMinorException(e); } - - new Thread(new Task() { + + executor.execute(new Task() { @Override protected Void call() { @@ -163,7 +161,6 @@ public class Controller extends Base imp try { - //Plugins Platform.runLater(()->{ getDashboardPane().getPluginsPane().clearData(); @@ -186,7 +183,6 @@ public class Controller extends Base imp //Server mainServer.setPort(getConfig().getPort()); mainServer.start(); - } catch (SevereException e) { @@ -194,7 +190,7 @@ public class Controller extends Base imp } return null; } - }).start(); + }); } @Override @@ -207,138 +203,8 @@ public class Controller extends Base imp Node settingsNode = getSettingsPane(); Node dashboardNode = getDashboardPane(); - openSettingsTimeLine = new Timeline(); - openSettingsTimeLine.setCycleCount(1); - - - openSettingsTimeLine.getKeyFrames().addAll( - new KeyFrame(Duration.millis(0.0D), - new KeyValue(settingsNode.opacityProperty(), - 0.0D, Interpolator.EASE_IN), - new KeyValue(settingsNode.scaleXProperty(), - 1.1D, Interpolator.EASE_IN), - new KeyValue(settingsNode.scaleYProperty(), - 1.1D, Interpolator.EASE_IN), - new KeyValue(settingsNode.scaleZProperty(), - 1.1D, Interpolator.EASE_IN)), - new KeyFrame(Duration.millis(90.0D), - new KeyValue(settingsNode.opacityProperty(), - 1.0D, Interpolator.LINEAR), - new KeyValue(settingsNode.scaleXProperty(), - 1.0D, Interpolator.LINEAR), - new KeyValue(settingsNode.scaleYProperty(), - 1.0D, Interpolator.LINEAR), - new KeyValue(settingsNode.scaleZProperty(), - 1.0D, Interpolator.LINEAR)), - - - new KeyFrame(Duration.millis(0.0D), - new KeyValue(dashboardNode.opacityProperty(), - 1.0D, Interpolator.LINEAR), - new KeyValue(dashboardNode.scaleXProperty(), - 1.0D, Interpolator.LINEAR), - new KeyValue(dashboardNode.scaleYProperty(), - 1.0D, Interpolator.LINEAR), - new KeyValue(dashboardNode.scaleZProperty(), - 1.0D, Interpolator.LINEAR)), - new KeyFrame(Duration.millis(90.0D), - new KeyValue(dashboardNode.opacityProperty(), - 0.0D, Interpolator.LINEAR), - new KeyValue(dashboardNode.scaleXProperty(), - 0.9D, Interpolator.LINEAR), - new KeyValue(dashboardNode.scaleYProperty(), - 0.9D, Interpolator.LINEAR), - new KeyValue(dashboardNode.scaleZProperty(), - 0.9D, Interpolator.LINEAR)) - ); - - openSettingsTimeLine.setOnFinished(event1 -> { - settingsNode.toFront(); - }); - - - closeSettingsTimeLine = new Timeline(); - closeSettingsTimeLine.setCycleCount(1); - - closeSettingsTimeLine.getKeyFrames().addAll( - - new KeyFrame(Duration.millis(0.0D), - new KeyValue(settingsNode.opacityProperty(), - 1.0D, Interpolator.LINEAR), - new KeyValue(settingsNode.scaleXProperty(), - 1.0D, Interpolator.LINEAR), - new KeyValue(settingsNode.scaleYProperty(), - 1.0D, Interpolator.LINEAR), - new KeyValue(settingsNode.scaleZProperty(), - 1.0D, Interpolator.LINEAR)), - new KeyFrame(Duration.millis(90.0D), - new KeyValue(settingsNode.opacityProperty(), - 0.0D, Interpolator.LINEAR), - new KeyValue(settingsNode.scaleXProperty(), - 1.1D, Interpolator.LINEAR), - new KeyValue(settingsNode.scaleYProperty(), - 1.1D, Interpolator.LINEAR), - new KeyValue(settingsNode.scaleZProperty(), - 1.1D, Interpolator.LINEAR)), - - new KeyFrame(Duration.millis(0.0D), - new KeyValue(dashboardNode.opacityProperty(), - 0.0D, Interpolator.LINEAR), - new KeyValue(dashboardNode.scaleXProperty(), - 0.9D, Interpolator.LINEAR), - new KeyValue(dashboardNode.scaleYProperty(), - 0.9D, Interpolator.LINEAR), - new KeyValue(dashboardNode.scaleZProperty(), - 0.9D, Interpolator.LINEAR)), - new KeyFrame(Duration.millis(90.0D), - new KeyValue(dashboardNode.opacityProperty(), - 1.0D, Interpolator.LINEAR), - new KeyValue(dashboardNode.scaleXProperty(), - 1.0D, Interpolator.LINEAR), - new KeyValue(dashboardNode.scaleYProperty(), - 1.0D, Interpolator.LINEAR), - new KeyValue(dashboardNode.scaleZProperty(), - 1.0D, Interpolator.LINEAR)) - - ); - - closeSettingsTimeLine.setOnFinished(event1 -> { - dashboardNode.toFront(); - new Thread(new Task() { - @Override - protected Void call() { - try { - getSettingsPane().getClientsSettings().loadData(); - - getSettingsPane().getGeneralSettings().loadDataFromConfig(); - getSettingsPane().getPluginsSettings().loadPlugins(); - - getSettingsPane().getThemesSettings().setThemes(getThemes()); - getSettingsPane().getThemesSettings().setCurrentThemeFullName(getCurrentTheme().getFullName()); - getSettingsPane().getThemesSettings().loadThemes(); - - getSettingsPane().setDefaultTabToGeneral(); - } - catch (SevereException e) - { - handleSevereException(e); - } - catch (MinorException e) - { - handleMinorException(e); - } - return null; - } - }).start(); - }); - } - - private Timeline openSettingsTimeLine; - private Timeline closeSettingsTimeLine; - - - public Controller(){ - mainServer = null; + openSettingsAnimation = createOpenSettingsAnimation(settingsNode, dashboardNode); + closeSettingsAnimation = createCloseSettingsAnimation(settingsNode, dashboardNode); } public void onCloseRequest(WindowEvent event) @@ -353,10 +219,7 @@ public class Controller extends Base imp return; } - getConfig().setStartupWindowSize( - getWidth(), - getHeight() - ); + getConfig().setStartupWindowSize(getWidth(), getHeight()); getConfig().save(); onQuitApp(); ExternalPlugins.getInstance().shutDownActions(); @@ -382,7 +245,7 @@ public class Controller extends Base imp mainServer.stopListeningForConnections(); ClientConnections.getInstance().disconnectAll(); - + executor.shutdown(); getLogger().info("Shutting down ..."); } @@ -480,7 +343,6 @@ public class Controller extends Base imp public synchronized boolean onNormalActionClicked(NormalAction action, String profileID) { try{ getLogger().info("action "+action.getID()+" clicked!"); - action.onActionClicked(); ActionBox actionBox = getDashboardBase().getActionGridPane().getActionBoxByIDAndProfileID( @@ -605,7 +467,6 @@ public class Controller extends Base imp Action action = clientProfile.getActionByID(actionID); clientConnection.saveActionDetails(profileID, action); - if(sendIcons && action.isHasIcon()) { saveAllIcons(profileID, actionID, socketAddress, false); @@ -650,14 +511,14 @@ public class Controller extends Base imp { if(runAsync) { - new Thread(new Task() { + executor.execute(new Task() { @Override protected Void call() { saveClientActionMain(profileID, actionID, socketAddress, sendIcons); return null; } - }).start(); + }); } else { @@ -676,14 +537,14 @@ public class Controller extends Base imp { if(async) { - new Thread(new Task() { + executor.execute(new Task() { @Override protected Void call() { saveAllIconsMain(profileID, actionID, socketAddress); return null; } - }).start(); + }); } else { @@ -713,7 +574,7 @@ public class Controller extends Base imp @Override public void saveIcon(String state, String profileID, String actionID, SocketAddress socketAddress) { - new Thread(new Task() { + executor.execute(new Task() { @Override protected Void call() { @@ -731,11 +592,137 @@ public class Controller extends Base imp } return null; } - }).start(); + }); } @Override public com.stream_pi.util.platform.Platform getPlatform() { return ServerInfo.getInstance().getPlatform(); } + + private Animation createOpenSettingsAnimation(Node settingsNode, Node dashboardNode) { + Timeline openSettingsTimeline = new Timeline(); + openSettingsTimeline.setCycleCount(1); + + openSettingsTimeline.getKeyFrames().addAll( + new KeyFrame(Duration.millis(0.0D), + new KeyValue(settingsNode.opacityProperty(), + 0.0D, Interpolator.EASE_IN), + new KeyValue(settingsNode.scaleXProperty(), + 1.1D, Interpolator.EASE_IN), + new KeyValue(settingsNode.scaleYProperty(), + 1.1D, Interpolator.EASE_IN), + new KeyValue(settingsNode.scaleZProperty(), + 1.1D, Interpolator.EASE_IN)), + new KeyFrame(Duration.millis(90.0D), + new KeyValue(settingsNode.opacityProperty(), + 1.0D, Interpolator.LINEAR), + new KeyValue(settingsNode.scaleXProperty(), + 1.0D, Interpolator.LINEAR), + new KeyValue(settingsNode.scaleYProperty(), + 1.0D, Interpolator.LINEAR), + new KeyValue(settingsNode.scaleZProperty(), + 1.0D, Interpolator.LINEAR)), + + new KeyFrame(Duration.millis(0.0D), + new KeyValue(dashboardNode.opacityProperty(), + 1.0D, Interpolator.LINEAR), + new KeyValue(dashboardNode.scaleXProperty(), + 1.0D, Interpolator.LINEAR), + new KeyValue(dashboardNode.scaleYProperty(), + 1.0D, Interpolator.LINEAR), + new KeyValue(dashboardNode.scaleZProperty(), + 1.0D, Interpolator.LINEAR)), + new KeyFrame(Duration.millis(90.0D), + new KeyValue(dashboardNode.opacityProperty(), + 0.0D, Interpolator.LINEAR), + new KeyValue(dashboardNode.scaleXProperty(), + 0.9D, Interpolator.LINEAR), + new KeyValue(dashboardNode.scaleYProperty(), + 0.9D, Interpolator.LINEAR), + new KeyValue(dashboardNode.scaleZProperty(), + 0.9D, Interpolator.LINEAR)) + ); + + openSettingsTimeline.setOnFinished(e -> settingsNode.toFront()); + return openSettingsTimeline; + } + + private Animation createCloseSettingsAnimation(Node settingsNode, Node dashboardNode) { + Timeline closeSettingsTimeline = new Timeline(); + closeSettingsTimeline.setCycleCount(1); + + closeSettingsTimeline.getKeyFrames().addAll( + + new KeyFrame(Duration.millis(0.0D), + new KeyValue(settingsNode.opacityProperty(), + 1.0D, Interpolator.LINEAR), + new KeyValue(settingsNode.scaleXProperty(), + 1.0D, Interpolator.LINEAR), + new KeyValue(settingsNode.scaleYProperty(), + 1.0D, Interpolator.LINEAR), + new KeyValue(settingsNode.scaleZProperty(), + 1.0D, Interpolator.LINEAR)), + new KeyFrame(Duration.millis(90.0D), + new KeyValue(settingsNode.opacityProperty(), + 0.0D, Interpolator.LINEAR), + new KeyValue(settingsNode.scaleXProperty(), + 1.1D, Interpolator.LINEAR), + new KeyValue(settingsNode.scaleYProperty(), + 1.1D, Interpolator.LINEAR), + new KeyValue(settingsNode.scaleZProperty(), + 1.1D, Interpolator.LINEAR)), + + new KeyFrame(Duration.millis(0.0D), + new KeyValue(dashboardNode.opacityProperty(), + 0.0D, Interpolator.LINEAR), + new KeyValue(dashboardNode.scaleXProperty(), + 0.9D, Interpolator.LINEAR), + new KeyValue(dashboardNode.scaleYProperty(), + 0.9D, Interpolator.LINEAR), + new KeyValue(dashboardNode.scaleZProperty(), + 0.9D, Interpolator.LINEAR)), + new KeyFrame(Duration.millis(90.0D), + new KeyValue(dashboardNode.opacityProperty(), + 1.0D, Interpolator.LINEAR), + new KeyValue(dashboardNode.scaleXProperty(), + 1.0D, Interpolator.LINEAR), + new KeyValue(dashboardNode.scaleYProperty(), + 1.0D, Interpolator.LINEAR), + new KeyValue(dashboardNode.scaleZProperty(), + 1.0D, Interpolator.LINEAR)) + + ); + + closeSettingsTimeline.setOnFinished(event1 -> { + dashboardNode.toFront(); + executor.execute(new Task() { + @Override + protected Void call() { + try { + getSettingsPane().getClientsSettings().loadData(); + + getSettingsPane().getGeneralSettings().loadDataFromConfig(); + getSettingsPane().getPluginsSettings().loadPlugins(); + + getSettingsPane().getThemesSettings().setThemes(getThemes()); + getSettingsPane().getThemesSettings().setCurrentThemeFullName(getCurrentTheme().getFullName()); + getSettingsPane().getThemesSettings().loadThemes(); + + getSettingsPane().setDefaultTabToGeneral(); + } + catch (SevereException e) + { + handleSevereException(e); + } + catch (MinorException e) + { + handleMinorException(e); + } + return null; + } + }); + }); + return closeSettingsTimeline; + } } --- 'a/src/main/java/com/stream_pi/server/window/Base.java' +++ b/src/main/java/com/stream_pi/server/window/Base.java @@ -29,22 +29,20 @@ import java.util.logging.Logger; public abstract class Base extends StackPane implements ExceptionAndAlertHandler, ServerListener { private Config config; - private ServerInfo serverInfo; - private Stage stage; - private HostServices hostServices; + private SettingsBase settingsBase; + private DashboardBase dashboardBase; + private StackPane alertStackPane; + private StreamPiLogFileHandler logFileHandler = null; + private StreamPiLogFallbackHandler logFallbackHandler = null; + private Logger logger = null; public Logger getLogger(){ return logger; } - private SettingsBase settingsBase; - private DashboardBase dashboardBase; - - private StackPane alertStackPane; - public void setHostServices(HostServices hostServices) { this.hostServices = hostServices; @@ -55,12 +53,6 @@ public abstract class Base extends Stack return hostServices; } - private Logger logger = null; - private StreamPiLogFileHandler logFileHandler = null; - - - private StreamPiLogFallbackHandler logFallbackHandler = null; - @Override public void initLogger() { @@ -134,7 +126,6 @@ public abstract class Base extends Stack getChildren().clear(); getChildren().addAll(alertStackPane); - initLogger(); checkPrePathDirectory(); @@ -149,10 +140,7 @@ public abstract class Base extends Stack stage.setHeight(config.getStartupWindowHeight()); stage.centerOnScreen(); - dashboardBase.toFront(); - - } private void checkPrePathDirectory() throws SevereException @@ -196,8 +184,6 @@ public abstract class Base extends Stack e.printStackTrace(); throw new SevereException(e.getMessage()); } - - } public void initThemes() throws SevereException { @@ -272,7 +258,7 @@ public abstract class Base extends Stack currentTheme = t; getStylesheets().addAll(t.getStylesheets()); - logger.info("... Done!"); + logger.info("... Theme applied successfully!"); } public void clearStylesheets() @@ -286,7 +272,7 @@ public abstract class Base extends Stack logger.info("Loading themes ..."); themes = new Themes(getConfig().getThemesPath(), getConfig().getCurrentThemeFullName(), serverInfo.getMinThemeSupportVersion()); - if(themes.getErrors().size()>0) + if(!themes.getErrors().isEmpty()) { StringBuilder themeErrors = new StringBuilder(); @@ -305,9 +291,7 @@ public abstract class Base extends Stack handleMinorException(new MinorException("Theme Loading issues", themeErrors.toString())); } - - - logger.info("... Done!"); + logger.info("...Themes loaded successfully !"); } public Themes getThemes() @@ -319,8 +303,6 @@ public abstract class Base extends Stack { logger.info("Applying default theme ..."); - - boolean foundTheme = false; for(Theme t: themes.getThemeList()) { @@ -332,9 +314,7 @@ public abstract class Base extends Stack } } - if(foundTheme) - logger.info("... Done!"); - else + if(!foundTheme) { logger.info("Theme not found. reverting to light theme ..."); try { @@ -348,9 +328,5 @@ public abstract class Base extends Stack handleSevereException(e); } } - - } - - } --- 'a/src/main/java/module-info.java' +++ b/src/main/java/module-info.java @@ -1,4 +1,5 @@ module com.stream_pi.server { + uses com.stream_pi.action_api.action.Action; uses com.stream_pi.action_api.externalplugin.NormalAction; uses com.stream_pi.action_api.externalplugin.ExternalPlugin;