Skip to content

Commit 89f0738

Browse files
loader/NeptusMain: Cleanup and small tweak for loader.
1 parent d2f5069 commit 89f0738

File tree

2 files changed

+114
-18
lines changed

2 files changed

+114
-18
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (c) 2004-2025 Universidade do Porto - Faculdade de Engenharia
3+
* Laboratório de Sistemas e Tecnologia Subaquática (LSTS)
4+
* All rights reserved.
5+
* Rua Dr. Roberto Frias s/n, sala I203, 4200-465 Porto, Portugal
6+
*
7+
* This file is part of Neptus, Command and Control Framework.
8+
*
9+
* Commercial Licence Usage
10+
* Licencees holding valid commercial Neptus licences may use this file
11+
* in accordance with the commercial licence agreement provided with the
12+
* Software or, alternatively, in accordance with the terms contained in a
13+
* written agreement between you and Universidade do Porto. For licensing
14+
* terms, conditions, and further information contact lsts@fe.up.pt.
15+
*
16+
* Modified European Union Public Licence - EUPL v.1.1 Usage
17+
* Alternatively, this file may be used under the terms of the Modified EUPL,
18+
* Version 1.1 only (the "Licence"), appearing in the file LICENCE.md
19+
* included in the packaging of this file. You may not use this work
20+
* except in compliance with the Licence. Unless required by applicable
21+
* law or agreed to in writing, software distributed under the Licence is
22+
* distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF
23+
* ANY KIND, either express or implied. See the Licence for the specific
24+
* language governing permissions and limitations at
25+
* https://github.com/LSTS/neptus/blob/develop/LICENSE.md
26+
* and http://ec.europa.eu/idabc/eupl.html.
27+
*
28+
* For more information please see <http://lsts.fe.up.pt/neptus>.
29+
*
30+
* Author: Paulo Dias
31+
* 2025/Sep/25
32+
*/
33+
package pt.lsts.neptus.loader;
34+
35+
import pt.lsts.neptus.console.ConsoleLayout;
36+
import pt.lsts.neptus.gui.Loader;
37+
import pt.lsts.neptus.loader.autonaut.AutonautConsole;
38+
39+
import java.util.LinkedHashMap;
40+
import java.util.Objects;
41+
import java.util.function.Function;
42+
43+
public class LaunchInitializerHolder {
44+
public final LinkedHashMap<String, String> appNames;
45+
public final LinkedHashMap<String, Class<?>> fileHandlers;
46+
47+
public LaunchInitializerHolder(LinkedHashMap<String, String> appNames, LinkedHashMap<String, Class<?>> fileHandlers) {
48+
this.appNames = appNames;
49+
this.fileHandlers = fileHandlers;
50+
}
51+
52+
public void registerLauncher(String key, String name, Runnable launcher) {
53+
NeptusMain.registerLauncher("au", "Autonaut Console", launcher);
54+
}
55+
56+
public void setDefaultApp(String key) {
57+
NeptusMain.defaultApp = "au";
58+
}
59+
}

src/java/pt/lsts/neptus/loader/NeptusMain.java

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@
4545
import java.util.ArrayList;
4646
import java.util.LinkedHashMap;
4747
import java.util.List;
48+
import java.util.Map;
4849
import java.util.Objects;
50+
import java.util.function.Function;
4951

5052
import javax.swing.ImageIcon;
5153
import javax.swing.JFrame;
@@ -80,33 +82,43 @@
8082
public class NeptusMain {
8183

8284
private static final LinkedHashMap<String, String> appNames = new LinkedHashMap<>();
85+
private static final Map<String, Runnable> appLauncher = new LinkedHashMap<>();
8386
private static final LinkedHashMap<String, Class<?>> fileHandlers = new LinkedHashMap<>();
8487
private static final List<Window> openAppWindows = new ArrayList<>();
8588
private static Loader loader;
8689

90+
static String defaultApp = "auv";
91+
8792
private static void init() {
8893
GeneralPreferences.initialize();
8994

9095
// appNames.put("ws", I18n.text("Workspace"));
91-
appNames.put("auv", I18n.text("LAUV Console"));
92-
appNames.put("mra", I18n.text("Mission Review & Analysis"));
93-
appNames.put("la", I18n.text("LAUV SE Console"));
94-
appNames.put("uav", I18n.text("UAV Console"));
95-
appNames.put("cl", I18n.text("Empty Console"));
96+
appNames.putIfAbsent("auv", I18n.text("LAUV Console"));
97+
appNames.putIfAbsent("mra", I18n.text("Mission Review & Analysis"));
98+
appNames.putIfAbsent("la", I18n.text("LAUV SE Console"));
99+
appNames.putIfAbsent("uav", I18n.text("UAV Console"));
100+
appNames.putIfAbsent("cl", I18n.text("Empty Console"));
96101

97102
// fileHandlers.put(FileUtil.FILE_TYPE_MISSION, Workspace.class);
98103
// fileHandlers.put(FileUtil.FILE_TYPE_MISSION_COMPRESSED, Workspace.class);
99-
fileHandlers.put(FileUtil.FILE_TYPE_CONFIG, EditorLauncher.class);
100-
fileHandlers.put(FileUtil.FILE_TYPE_CONSOLE, ConsoleParse.class);
104+
fileHandlers.putIfAbsent(FileUtil.FILE_TYPE_CONFIG, EditorLauncher.class);
105+
fileHandlers.putIfAbsent(FileUtil.FILE_TYPE_CONSOLE, ConsoleParse.class);
101106
// fileHandlers.put(FileUtil.FILE_TYPE_VEHICLE, Workspace.class);
102-
fileHandlers.put(FileUtil.FILE_TYPE_CHECKLIST, Workspace.class);
103-
fileHandlers.put(FileUtil.FILE_TYPE_INI, EditorLauncher.class);
104-
fileHandlers.put(FileUtil.FILE_TYPE_RMF, RMFEditor.class);
105-
fileHandlers.put(FileUtil.FILE_TYPE_XML, EditorLauncher.class);
106-
107-
fileHandlers.put(FileUtil.FILE_TYPE_LSF, NeptusMRA.class);
108-
fileHandlers.put(FileUtil.FILE_TYPE_LSF_COMPRESSED, NeptusMRA.class);
109-
fileHandlers.put(FileUtil.FILE_TYPE_LSF_COMPRESSED_BZIP2, NeptusMRA.class);
107+
fileHandlers.putIfAbsent(FileUtil.FILE_TYPE_CHECKLIST, Workspace.class);
108+
fileHandlers.putIfAbsent(FileUtil.FILE_TYPE_INI, EditorLauncher.class);
109+
fileHandlers.putIfAbsent(FileUtil.FILE_TYPE_RMF, RMFEditor.class);
110+
fileHandlers.putIfAbsent(FileUtil.FILE_TYPE_XML, EditorLauncher.class);
111+
112+
fileHandlers.putIfAbsent(FileUtil.FILE_TYPE_LSF, NeptusMRA.class);
113+
fileHandlers.putIfAbsent(FileUtil.FILE_TYPE_LSF_COMPRESSED, NeptusMRA.class);
114+
fileHandlers.putIfAbsent(FileUtil.FILE_TYPE_LSF_COMPRESSED_BZIP2, NeptusMRA.class);
115+
}
116+
117+
static void registerLauncher(String key, String name, Runnable launcher) {
118+
appNames.put(key, name);
119+
if (launcher != null) {
120+
appLauncher.put(key, launcher);
121+
}
110122
}
111123

112124
/**
@@ -120,11 +132,15 @@ public static void launch(String[] appargs) {
120132
launch(new Loader(), appargs);
121133
}
122134

135+
public static void launch(Loader loader, String[] appargs) {
136+
launch(loader, appargs, null);
137+
}
138+
123139
/**
124140
* @param loader A {@link Loader} to use on the opening of the program
125141
* @param appargs The commandline arguments for the program
126142
*/
127-
public static void launch(Loader loader, String[] appargs) {
143+
public static void launch(Loader loader, String[] appargs, Function<LaunchInitializerHolder, Boolean> afterInit) {
128144
ConfigFetch.initialize(); // Don't touch this, leave it as it his
129145
// benchmark
130146
long start = System.currentTimeMillis();
@@ -133,13 +149,22 @@ public static void launch(Loader loader, String[] appargs) {
133149
if (appNames.isEmpty()) {
134150
init();
135151
}
136-
152+
137153
String app = appargs[0];
138154
loader.start();
139155
ConfigFetch.setSuperParentFrameForced(loader);
140156

141157
loadPreRequirementsDataExceptConfigFetch(loader, true);
142158

159+
if (afterInit != null) {
160+
LaunchInitializerHolder launchInitializerHolder = new LaunchInitializerHolder(appNames, fileHandlers);
161+
Boolean ret = afterInit.apply(launchInitializerHolder);
162+
if (ret != null && !ret) {
163+
loader.end();
164+
return;
165+
}
166+
}
167+
143168
// When loading one can type the application to start
144169
String typ = loader.getTypedString();
145170
if (!typ.equalsIgnoreCase("")) {
@@ -161,7 +186,7 @@ JOptionPane.QUESTION_MESSAGE, new ImageIcon(ImageUtils.getImage("images/neptus-i
161186
if (appT != null)
162187
app = typ;
163188
else
164-
app = "auv";
189+
app = defaultApp;
165190
}
166191
else if (app.equalsIgnoreCase("-f") && appargs.length >= 2) {
167192
loader.setText(I18n.text("Opening file..."));
@@ -224,6 +249,18 @@ else if (app.equalsIgnoreCase("uav")) {
224249
appC.setVisible(true);
225250
wrapMainApplicationWindowWithCloseActionWindowAdapter(appC);
226251
}
252+
else if (appLauncher.containsKey(app)) {
253+
ConfigFetch.initialize();
254+
try {
255+
appLauncher.get(app).run();
256+
}
257+
catch (Exception e) {
258+
NeptusLog.pub().error(I18n.textf("Error launching application %app", app), e);
259+
GuiUtils.errorMessage(loader, I18n.text("Error launching application"),
260+
I18n.textf("An error occurred while launching the application %app: %error", app,
261+
e.getMessage()));
262+
}
263+
}
227264
// File loading
228265
else {
229266
ConfigFetch.initialize();

0 commit comments

Comments
 (0)