1 /*
2  * Copyright (c) 2008-2019 Emmanuel Dupuy.
3  * This project is distributed under the GPLv3 license.
4  * This is a Copyleft license that gives the user the right to use,
5  * copy and modify the code freely for non-commercial purposes.
6  */
7 
8 package org.jd.gui;
9 
10 import org.jd.gui.controller.MainController;
11 import org.jd.gui.model.configuration.Configuration;
12 import org.jd.gui.service.configuration.ConfigurationPersister;
13 import org.jd.gui.service.configuration.ConfigurationPersisterService;
14 import org.jd.gui.util.exception.ExceptionUtil;
15 import org.jd.gui.util.net.InterProcessCommunicationUtil;
16 
17 import javax.swing.*;
18 import java.io.File;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.List;
22 
23 public class App {
24     protected static final String SINGLE_INSTANCE = "UIMainWindowPreferencesProvider.singleInstance";
25 
26     protected static MainController controller;
27 
main(String[] args)28     public static void main(String[] args) {
29 		if (checkHelpFlag(args)) {
30 			JOptionPane.showMessageDialog(null, "Usage: jd-gui [option] [input-file] ...\n\nOption:\n -h Show this help message and exit", Constants.APP_NAME, JOptionPane.INFORMATION_MESSAGE);
31 		} else {
32             // Load preferences
33             ConfigurationPersister persister = ConfigurationPersisterService.getInstance().get();
34             Configuration configuration = persister.load();
35             Runtime.getRuntime().addShutdownHook(new Thread(() -> persister.save(configuration)));
36 
37             if ("true".equals(configuration.getPreferences().get(SINGLE_INSTANCE))) {
38                 InterProcessCommunicationUtil ipc = new InterProcessCommunicationUtil();
39                 try {
40                     ipc.listen(receivedArgs -> controller.openFiles(newList(receivedArgs)));
41                 } catch (Exception notTheFirstInstanceException) {
42                     // Send args to main windows and exit
43                     ipc.send(args);
44                     System.exit(0);
45                 }
46             }
47 
48             // Create SwingBuilder, set look and feel
49             try {
50                 UIManager.setLookAndFeel(configuration.getLookAndFeel());
51             } catch (Exception e) {
52                 configuration.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
53                 try {
54                     UIManager.setLookAndFeel(configuration.getLookAndFeel());
55                 } catch (Exception ee) {
56                     assert ExceptionUtil.printStackTrace(ee);
57                 }
58            }
59 
60             // Create main controller and show main frame
61             controller = new MainController(configuration);
62             controller.show(newList(args));
63 		}
64 	}
65 
checkHelpFlag(String[] args)66     protected static boolean checkHelpFlag(String[] args) {
67         if (args != null) {
68             for (String arg : args) {
69                 if ("-h".equals(arg)) {
70                     return true;
71                 }
72             }
73         }
74         return false;
75     }
76 
newList(String[] paths)77     protected static List<File> newList(String[] paths) {
78         if (paths == null) {
79             return Collections.emptyList();
80         } else {
81             ArrayList<File> files = new ArrayList<>(paths.length);
82             for (String path : paths) {
83                 files.add(new File(path));
84             }
85             return files;
86         }
87     }
88 }
89