Parent: [a7a9ff] (diff)

Download this file

MasterController.java    510 lines (439 with data), 17.8 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*
* Copyright 2013-2014 TECO - Karlsruhe Institute of Technology.
*
* This file is part of TACET.
*
* TACET is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TACET is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TACET. If not, see <http://www.gnu.org/licenses/>.
*/
package squirrel.controller;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.security.CodeSource;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JDialog;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import squirrel.controller.video.MainController;
import squirrel.controller.video.SynchronisationController;
import squirrel.model.AnnotationModelImpl;
import squirrel.model.ModelFacade;
import squirrel.model.TimeCoordinator;
import squirrel.model.io.DataColumn;
import squirrel.model.io.DataSource;
import squirrel.model.video.SyncData;
import squirrel.view.AboutView;
import squirrel.view.Gui;
import squirrel.view.ImportWizard.ImportWizard;
import squirrel.view.video.SynchronisationView;
public class MasterController {
/**
* Main View Component
*/
private Gui gui;
private ImportController importController;
private TimeCoordinator timeCoordinator;
private SyncData syncData;
private TimeCoordinatorController timeCoordinatorController;
private MainController mainController;
private boolean mediaToPlay = false;
private ProjectController projectController;
private ExportController exportController;
private ClassificatorController classificatorController;
/**
* Actions of Menu
*/
private Action importAction;
private Action synchronizeAction;
private Action classifyAction;
private Action stopClassificationAction;
private Action startClassificationAction;
private Action exitAction;
private Action saveAction;
private Action loadAction;
private Action exportAction;
private Action statisticsAction;
private Action aboutAction;
private boolean imported = false;
private ModelFacade model;
/**
* Constructor - inits the whole programm
*/
public MasterController(ImportController importController) {
if (importController == null)
this.importController = new ImportController(this);
else
this.importController = importController;
timeCoordinator = new TimeCoordinator();
timeCoordinatorController = new TimeCoordinatorController(timeCoordinator);
projectController = new ProjectController(this);
exportController = new ExportController(this);
}
/**
* Default constructor
*/
public MasterController() {
this(null);
}
/**
* Returns an instance of ModelFacade representing model of mvc
* @return
*/
public ModelFacade getModel() {
return model;
}
/**
* Inits the JMenuBar of the Main Window and adds its functionality
*/
private void initJMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu);
JMenu viewMenu = new JMenu("View");
menuBar.add(viewMenu);
JMenu classifyMenu = new JMenu("Classify");
menuBar.add(classifyMenu);
JMenu helpMenu = new JMenu("Help");
menuBar.add(helpMenu);
aboutAction = new AbstractAction("About") {
@Override
public void actionPerformed(ActionEvent arg0) {
AboutView av = new AboutView();
av.showAsDialog(gui);
}
};
JMenuItem about = new JMenuItem("About");
about.addActionListener(aboutAction);
helpMenu.add(about);
importAction = new AbstractAction("Import") {
@Override
public void actionPerformed(ActionEvent e) {
importController.presentDialog(gui);
}
};
JMenuItem importProject = new JMenuItem(importAction);
importProject.setAccelerator(KeyStroke.getKeyStroke(
java.awt.event.KeyEvent.VK_I,
java.awt.Event.CTRL_MASK));
fileMenu.add(importProject);
exportAction = new AbstractAction("Export") {
@Override
public void actionPerformed(ActionEvent arg0) {
exportController.presentDialog(gui);
}
};
JMenuItem exportProject = new JMenuItem(exportAction);
exportProject.setAccelerator(KeyStroke.getKeyStroke(
java.awt.event.KeyEvent.VK_E,
java.awt.Event.CTRL_MASK));
fileMenu.add(exportProject);
exportAction.setEnabled(false);
saveAction = new AbstractAction("Save Project") {
@Override
public void actionPerformed(ActionEvent arg0) {
projectController.presentDialogSave(gui);
}
};
JMenuItem saveProject = new JMenuItem(saveAction);
saveProject.setAccelerator(KeyStroke.getKeyStroke(
java.awt.event.KeyEvent.VK_S,
java.awt.Event.CTRL_MASK));
fileMenu.add(saveProject);
saveAction.setEnabled(false);
loadAction = new AbstractAction("Load Project") {
@Override
public void actionPerformed(ActionEvent arg0) {
projectController.presentDialogLoad(gui);
}
};
JMenuItem loadProject = new JMenuItem(loadAction);
loadProject.setAccelerator(KeyStroke.getKeyStroke(
java.awt.event.KeyEvent.VK_L,
java.awt.Event.CTRL_MASK));
fileMenu.add(loadProject);
synchronizeAction = new AbstractAction("Synchronize") {
@Override
public void actionPerformed(ActionEvent e) {
String[] mediaPaths = model.getCurrentDataSource().getMediaPath().toArray(
new String[model.getCurrentDataSource().getMediaPath()
.size()]);
SynchronisationController synchronisationController =
new SynchronisationController(syncData, mediaPaths,
timeCoordinator.getMaxTime(), timeCoordinator.getMinimalDeltaTime());
synchronisationController.addSensorTracks(model);
synchronisationController.presentDialog(gui);
mainController.setStartTimes(syncData.getStartTimes());
mainController.setVideoSpeed(syncData.getVideoSpeed());
}
};
//fileMenu.add(synchronizeAction);
synchronizeAction.setEnabled(false);
// startClassificationAction = new AbstractAction("Start Classification") {
// @Override
// public void actionPerformed(ActionEvent arg0) {
// classificatorController.setClassify(true);
// startClassificationAction.setEnabled(false);
// stopClassificationAction.setEnabled(true);
// }
// };
// classifyMenu.add(startClassificationAction);
// startClassificationAction.setEnabled(false);
//
// stopClassificationAction = new AbstractAction("Stop Classification") {
// @Override
// public void actionPerformed(ActionEvent arg0) {
// classificatorController.setClassify(false);
// stopClassificationAction.setEnabled(false);
// startClassificationAction.setEnabled(true);
// }
// };
// classifyMenu.add(stopClassificationAction);
// stopClassificationAction.setEnabled(false);
classifyAction = new AbstractAction("Classify") {
@Override
public void actionPerformed(ActionEvent arg0) {
classificatorController.presentDialog(gui);
// if (classificatorController.isConfigured()) {
// startClassificationAction.setEnabled(true);
// }
gui.initializeClassifierScheduler();
}
};
classifyMenu.add(classifyAction);
classifyAction.setEnabled(false);
statisticsAction = new AbstractAction("Statistics") {
@Override
public void actionPerformed(ActionEvent e) {
gui.addStatistics();
}
};
viewMenu.add(statisticsAction);
statisticsAction.setEnabled(false);
exitAction = new AbstractAction("Quit") {
@Override
public void actionPerformed(ActionEvent e) {
int resp;
if (imported) {
Object[] options = { "Save",
"Don't save",
"Cancel" };
resp = JOptionPane.showOptionDialog(null,
"Save project before quitting?",
"Quit",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[2]);
if (resp == JOptionPane.YES_OPTION) {
projectController.presentDialogSave(gui);
}
} else {
resp = JOptionPane.showConfirmDialog(null, "Are you sure you want to quit?", "Quit",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
}
if (resp != JOptionPane.CANCEL_OPTION) {
if (mediaToPlay) {
try {
mainController.killMp();
} catch (RemoteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
System.exit(0);
}
}
};
JMenuItem exit = new JMenuItem(exitAction);
exit.setAccelerator(KeyStroke.getKeyStroke(
java.awt.event.KeyEvent.VK_Q,
java.awt.Event.CTRL_MASK));
fileMenu.add(exit);
this.gui.setJMenuBar(menuBar);
AbstractAction shortcutAction = new AbstractAction("Shortcuts") {
@Override
public void actionPerformed(ActionEvent arg0) {
JOptionPane pane = new JOptionPane();
pane.setMessage("Shortcuts for the Menu are CTRL + First letter of the MenuEntry \n Shortcuts for Buttons are ALT + First Letter of Button Titel");
JDialog d = pane.createDialog(null, "Shortcuts");
d.setVisible(true);
}
};
JMenuItem shortcuts = new JMenuItem(shortcutAction);
helpMenu.add(shortcuts);
}
/**
* Inits Mastercontroller and starts the whole programm
* @param args
*/
public static void main(String args[]) {
MasterController mcontroller = new MasterController();
mcontroller.onStart();
}
/**
* Init and show the main window.
*/
public void onStart() {
this.gui = new Gui();
initJMenuBar();
SwingUtilities.invokeLater(new Runnable() {
@Override public void run() {
gui.setVisible(true);
}
});
//startRegistry();
gui.addWindowListener(new WindowListener() {
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent e) {
if (mediaToPlay) {
try {
mainController.killMp();
} catch (RemoteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
System.exit(0);
}
@Override
public void windowClosing(WindowEvent e) {
if (mediaToPlay) {
try {
mainController.killMp();
} catch (RemoteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
System.exit(0);
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
});
gui.setVisible(true);
}
/**
* This method is called, when the import-Dialog has finished.
* @param dataSource The data source that describes the imported file.
*/
public void onImportFinish(DataSource<? extends DataColumn> dataSource) {
model = new ModelFacade(null, null, null, null, dataSource, null, timeCoordinator);
timeCoordinator.setMaxTime(model.getEndTimeStamp() - model.getStartTimeStamp());
gui.setModel(model);
TrackSynchroniser synchroniser = new TrackSynchroniser(model.getStartTimeStamp());
timeCoordinator.addListener(synchroniser);
gui.setTrackSynchroniser(synchroniser);
gui.setControls(timeCoordinatorController);
gui.setSensorViewVisible();
gui.setAnnotationViewVisible();
exportAction.setEnabled(true);
saveAction.setEnabled(true);
loadAction.setEnabled(false);
importAction.setEnabled(false);
statisticsAction.setEnabled(true);
classificatorController = new ClassificatorController(model);
classifyAction.setEnabled(true);
imported = true;
if (dataSource.getMediaPath().size() > 0) {
mediaToPlay = true;
syncData = new SyncData(dataSource.getMediaPath().size() + 1);
synchronizeAction.setEnabled(true);
String[] mediaPaths = dataSource.getMediaPath().toArray(
new String[model.getCurrentDataSource().getMediaPath()
.size()]);
mainController =
new MainController(mediaPaths,
timeCoordinator,
syncData.getStartTimes(), syncData.getVideoSpeed());
timeCoordinatorController.addMainController(mainController);
gui.addMediaView(mainController);
try {
startSynchronizedMediaPlayers();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void onLoadFinish(DataSource<? extends DataColumn> dataSource,
AnnotationModelImpl model) {
onImportFinish(dataSource);
this.model.insertAnnoationModel(model);
}
private void startRegistry() {
try {
LocateRegistry.createRegistry(1099);
} catch (RemoteException e1) {
JOptionPane.showMessageDialog(gui,
"An other instance of the program already running.",
"Squirrel is already running",
JOptionPane.WARNING_MESSAGE);
System.exit(0);
}
}
/**
* Starts the media players
* @throws URISyntaxException
*/
private void startSynchronizedMediaPlayers() throws URISyntaxException {
if (gui.isVisible()) {
long[] canvasIDs = gui.getCanvasId();
CodeSource codeSource = SynchronisationView.class.getProtectionDomain().getCodeSource();
File jarFile = new File(codeSource.getLocation().toURI().getPath());
String jarDir = jarFile.getParentFile().getPath();
for (int i = 0; i < canvasIDs.length; i++) {
ProcessBuilder pb =
new ProcessBuilder("java", "-cp", jarDir
+ "/squirrel-1.0-SNAPSHOT-jar-with-dependencies.jar",
"squirrel.model.video.RemoteControlledMediaPlayer",
Integer.toString(i), "0", Long.toString(canvasIDs[i]));
try {
pb.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}