-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathMain.java
More file actions
201 lines (161 loc) · 7.19 KB
/
Main.java
File metadata and controls
201 lines (161 loc) · 7.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.datatransfer.DataFlavor;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.io.File;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class Main {
static final String versionString = "Telemetry Viewer v0.8";
static final String versionDate = "2021-07-24";
@SuppressWarnings("serial")
static JFrame window = new JFrame(versionString) {
int dataStructureViewWidth = -1;
@Override public Dimension getPreferredSize() {
if(dataStructureViewWidth < 0)
dataStructureViewWidth = Integer.max( new DataStructureCsvView(ConnectionsController.telemetryConnections.get(0)).getPreferredSize().width,
new DataStructureBinaryView(ConnectionsController.telemetryConnections.get(0)).getPreferredSize().width);
int settingsViewWidth = SettingsView.instance.getPreferredSize().width;
int settingsViewHeight = SettingsView.instance.getPreferredSize().height;
int configureViewWidth = ConfigureView.instance.getPreferredSize().width;
int controlsViewHeight = CommunicationView.instance.getPreferredSize().height;
int controlsViewWidth = CommunicationView.instance.getPreferredSize().width;
int width = controlsViewWidth;
if(width < dataStructureViewWidth)
width = dataStructureViewWidth;
if(width < settingsViewWidth + configureViewWidth)
width = settingsViewWidth + configureViewWidth;
int height = settingsViewHeight + controlsViewHeight + (8 * Theme.padding);
return new Dimension(width, height);
}
@Override public Dimension getMinimumSize() {
return getPreferredSize();
}
};
static LogitechSmoothScrolling mouse = new LogitechSmoothScrolling();
/**
* Entry point for the program.
* This just creates and configures the main window.
*
* @param args Command line arguments (not currently used.)
*/
@SuppressWarnings("serial")
public static void main(String[] args) {
try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch(Exception e){}
// create the cache folder
Path cacheDir = Paths.get("cache");
try { Files.createDirectory(cacheDir); } catch(FileAlreadyExistsException e) {} catch(Exception e) { e.printStackTrace(); }
// populate the window
window.setLayout(new BorderLayout());
window.add(OpenGLChartsView.instance, BorderLayout.CENTER);
window.add(SettingsView.instance, BorderLayout.WEST);
window.add(CommunicationView.instance, BorderLayout.SOUTH);
window.add(ConfigureView.instance, BorderLayout.EAST);
NotificationsController.showHintUntil("Start by connecting to a device or opening a file by using the buttons below.", () -> false, true);
window.setSize(window.getPreferredSize());
window.setMinimumSize(window.getMinimumSize());
window.setLocationRelativeTo(null);
window.setExtendedState(JFrame.MAXIMIZED_BOTH);
// support smooth scrolling
window.addWindowFocusListener(new WindowFocusListener() {
@Override public void windowGainedFocus(WindowEvent we) { mouse.updateScrolling(); }
@Override public void windowLostFocus(WindowEvent we) { }
});
// allow the user to drag-n-drop settings/CSV/camera files
window.setDropTarget(new DropTarget() {
@Override public void drop(DropTargetDropEvent event) {
try {
event.acceptDrop(DnDConstants.ACTION_LINK);
@SuppressWarnings("unchecked")
List<File> files = (List<File>) event.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
String[] filepaths = new String[files.size()];
for(int i = 0; i < files.size(); i++)
filepaths[i] = files.get(i).getAbsolutePath();
ConnectionsController.importFiles(filepaths);
} catch(Exception e) {
NotificationsController.showFailureUntil("Error while processing files: " + e.getMessage(), () -> false, true);
e.printStackTrace();
}
}
});
// allow load settings/CSV/camera files at startup
if (args.length>0) ConnectionsController.importFiles(args);
// handle window close events
window.addWindowListener(new WindowAdapter() {
@Override public void windowClosing(java.awt.event.WindowEvent windowEvent) {
// cancel importing
if(ConnectionsController.importing) {
if(ConnectionsController.realtimeImporting)
ConnectionsController.allConnections.forEach(connection -> connection.finishImporting()); // exit real-time
ConnectionsController.allConnections.forEach(connection -> connection.finishImporting()); // abort
}
// cancel exporting if the user confirms it
if(ConnectionsController.exporting) {
int result = JOptionPane.showConfirmDialog(window, "Exporting in progress. Exit anyway?", "Confirm", JOptionPane.YES_NO_OPTION);
if(result == JOptionPane.YES_OPTION)
ConnectionsController.cancelExporting();
else
return; // don't close
}
// close connections and remove their cache files
ConnectionsController.allConnections.forEach(connection -> connection.dispose());
try { Files.deleteIfExists(cacheDir); } catch(Exception e) { }
// die
window.dispose();
System.exit(0);
}
});
// show the window
window.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); // so the windowClosing listener can cancel the close
window.setVisible(true);
}
/**
* Hides the charts and settings panels, then shows the data structure screen in the middle of the main window.
* This method is thread-safe.
*/
public static void showConfigurationGui(JPanel gui) {
SwingUtilities.invokeLater(() -> {
OpenGLChartsView.instance.animator.pause();
CommunicationView.instance.showSettings(false);
ConfigureView.instance.close();
window.remove(OpenGLChartsView.instance);
window.add(gui, BorderLayout.CENTER);
window.revalidate();
window.repaint();
});
}
/**
* Hides the data structure screen and shows the charts in the middle of the main window.
* This method is thread-safe.
*/
public static void hideConfigurationGui() {
SwingUtilities.invokeLater(() -> {
// do nothing if already hidden
for(Component c : window.getContentPane().getComponents())
if(c == OpenGLChartsView.instance)
return;
// remove the configuration GUI
for(Component c : window.getContentPane().getComponents())
if(c instanceof DataStructureCsvView || c instanceof DataStructureBinaryView)
window.remove(c);
window.add(OpenGLChartsView.instance, BorderLayout.CENTER);
window.revalidate();
window.repaint();
OpenGLChartsView.instance.animator.resume();
});
}
}