View Javadoc
1   package com.jsql;
2   
3   import com.formdev.flatlaf.util.SystemInfo;
4   import com.jsql.model.InjectionModel;
5   import com.jsql.util.LogLevelUtil;
6   import com.jsql.view.swing.JFrameView;
7   import com.jsql.view.swing.util.MediatorHelper;
8   import com.jsql.view.swing.util.UiUtil;
9   import org.apache.logging.log4j.Level;
10  import org.apache.logging.log4j.LogManager;
11  import org.apache.logging.log4j.Logger;
12  
13  import javax.swing.*;
14  import java.awt.*;
15  
16  /**
17   * Main class of the application and called from the .jar.
18   * This class set the general environment of execution and start the software.
19   */
20  public class MainApp {
21      
22      private static final Logger LOGGER = LogManager.getRootLogger();
23      private static final InjectionModel INJECTION_MODEL;  // required to load preferences first
24      
25      static {
26          System.setProperty("jdk.httpclient.allowRestrictedHeaders", "connection,content-length,expect,host,upgrade");
27          System.setProperty("log4j2.formatMsgNoLookups", "true");
28  
29          if (GraphicsEnvironment.isHeadless()) {
30              LOGGER.log(Level.ERROR, "Headless runtime not supported, use default Java runtime instead");
31              System.exit(1);
32          }
33  
34          INJECTION_MODEL = new InjectionModel();
35          MainApp.INJECTION_MODEL.getMediatorUtils().getPreferencesUtil().loadSavedPreferences();
36  
37          if (SystemInfo.isLinux) {  // enable flatlaf decorated window
38              JFrame.setDefaultLookAndFeelDecorated(true);
39              JDialog.setDefaultLookAndFeelDecorated(true);
40          }
41          var nameTheme = MainApp.INJECTION_MODEL.getMediatorUtils().getPreferencesUtil().getThemeFlatLafName();
42          UiUtil.applyTheme(nameTheme);  // required init but not enough, reapplied next
43          MainApp.apply4K();
44      }
45  
46      private MainApp() {
47          // nothing
48      }
49      
50      /**
51       * Application starting point.
52       * @param args CLI parameters (not used)
53       */
54      public static void main(String[] args) {
55          MediatorHelper.register(MainApp.INJECTION_MODEL);
56  
57          MainApp.INJECTION_MODEL.getMediatorUtils().getExceptionUtil().setUncaughtExceptionHandler();
58          MainApp.INJECTION_MODEL.getMediatorUtils().getProxyUtil().initProxy();
59          MainApp.INJECTION_MODEL.getMediatorUtils().getAuthenticationUtil().setKerberosCifs();
60  
61          try {
62              var view = new JFrameView(MainApp.INJECTION_MODEL);
63              MediatorHelper.register(view);
64              MainApp.INJECTION_MODEL.subscribe(view.getSubscriber());
65          } catch (HeadlessException e) {
66              LOGGER.log(LogLevelUtil.CONSOLE_JAVA, "HeadlessException, command line execution not supported: %s", e);
67          } catch (AWTError e) {
68              // Fix #22668: Assistive Technology not found
69              LOGGER.log(LogLevelUtil.CONSOLE_JAVA, String.format(
70                  "Java Access Bridge missing or corrupt, check your access bridge definition in JDK_HOME/jre/lib/accessibility.properties: %s",
71                  e.getMessage()
72              ), e);
73          }
74      }
75  
76      private static void apply4K() {  // required not in UiUtil before frame is set
77          if (MainApp.INJECTION_MODEL.getMediatorUtils().getPreferencesUtil().is4K()) {
78              System.setProperty("sun.java2d.uiScale", "2.5");  // jdk >= 9
79          }
80      }
81  }