~andreas.hoelzl/jhelioviewer/jhv-formatting

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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
package org.helioviewer.jhv.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.AbstractList;
import java.util.Date;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.helioviewer.jhv.JHVGlobals;
import org.helioviewer.jhv.JHVSplashScreen;
import org.helioviewer.jhv.gui.actions.ExitProgramAction;
import org.helioviewer.jhv.gui.components.SideContentPane;
import org.helioviewer.jhv.gui.components.ImageSelectorPanel;
import org.helioviewer.jhv.gui.components.MainImagePanel;
import org.helioviewer.jhv.gui.components.MenuBar;
import org.helioviewer.jhv.gui.components.ObservationPanel;
import org.helioviewer.jhv.gui.components.OverviewImagePanel;
import org.helioviewer.jhv.gui.components.StatusPanel;
import org.helioviewer.jhv.gui.components.TopToolBar;
import org.helioviewer.jhv.gui.components.CollapsibleObservation;
import org.helioviewer.jhv.gui.components.statusplugins.FramerateStatusPanel;
import org.helioviewer.jhv.gui.components.statusplugins.MetaDataStatusPanel;
import org.helioviewer.jhv.gui.components.statusplugins.JPIPStatusPanel;
import org.helioviewer.jhv.gui.components.statusplugins.PositionStatusPanel;
import org.helioviewer.jhv.gui.components.statusplugins.QualityStatusPanel;
import org.helioviewer.jhv.gui.components.statusplugins.RenderModeStatusPanel;
import org.helioviewer.jhv.gui.components.statusplugins.ZoomStatusPanel;
import org.helioviewer.jhv.gui.controller.ImagePanelEventPopupController;
import org.helioviewer.jhv.gui.controller.MainImagePanelMousePanController;
import org.helioviewer.jhv.internal_plugins.filter.opacity.OpacityFilter;
import org.helioviewer.jhv.io.CommandLineProcessor;
import org.helioviewer.jhv.io.APIRequestManager;
import org.helioviewer.jhv.io.FileDownloader;

import org.helioviewer.viewmodel.view.ComponentView;

import org.helioviewer.viewmodel.view.FilterView;
import org.helioviewer.viewmodel.view.ImageInfoView;
import org.helioviewer.viewmodel.view.LayeredView;
import org.helioviewer.viewmodel.view.SynchronizeView;
import org.helioviewer.viewmodel.view.View;

/**
 * A class that sets up the graphical user interface.
 * 
 * @author caplins
 * @author Benjamin Wamsler
 * @author Alen Agheksanterian
 * @author Stephan Pagel
 * @author Markus Langenberg
 * 
 */
public class ImageViewerGui {

    /** The sole instance of this class. */
    private static final ImageViewerGui singletonImageViewer = new ImageViewerGui();

    private ComponentView mainComponentView;   
    private ComponentView overviewComponentView;

    private static JFrame mainFrame;
    private JPanel contentPanel;
    private JScrollPane leftScrollPane;

    private TopToolBar topToolBar;
    private MainImagePanel mainImagePanel;
    private OverviewImagePanel overviewImagePanel;
    private SideContentPane leftPane;
    private RenderModeStatusPanel renderModeStatus;
    private ImageSelectorPanel imageSelectorPanel;

    private ObservationPanel observationPanel;
//    private SolarEventCatalogsPanel solarEventCatalogsPanel;

    public static final int SIDE_PANEL_WIDTH = 320;
    public static final int SIDE_PADDING = 10;

    /**
     * The private constructor that creates and positions all the gui
     * components.
     */
    private ImageViewerGui() {
    	mainFrame = createMainFrame();
        
    	contentPanel = new JPanel(new BorderLayout());
    	
    	JMenuBar menuBar = new MenuBar();
    	menuBar.setFocusable(false);
    	
        mainFrame.setContentPane(contentPanel);
        mainFrame.setJMenuBar(menuBar);
    	mainFrame.setFocusable(true);
        
        topToolBar = new TopToolBar();
        contentPanel.add(topToolBar, BorderLayout.PAGE_START);
        
        // ////////////////////////////////////////////////////////////////////////////////
        // MAIN IMAGE PANEL
        // ////////////////////////////////////////////////////////////////////////////////
        
        // set up main image panel
        mainImagePanel = new MainImagePanel();
        mainImagePanel.setAutoscrolls(true);
        mainImagePanel.setFocusable(false);

        mainImagePanel.setInputController(new MainImagePanelMousePanController());
        mainImagePanel.addPlugin(new ImagePanelEventPopupController());
        
        contentPanel.add(mainImagePanel, BorderLayout.CENTER);
        
        // ////////////////////////////////////////////////////////////////////////////////
        // LEFT CONTROL PANEL
        // ////////////////////////////////////////////////////////////////////////////////
        
        leftPane = new SideContentPane();
        
        // create overview image panel instance
        overviewImagePanel = new OverviewImagePanel();
        
        // set up the overview image panel
        overviewImagePanel.setAutoscrolls(true);
        overviewImagePanel.setPreferredSize(new Dimension(175, 175));
        overviewImagePanel.setFocusable(false);
        
        JPanel overviewImagePanelContainer = new JPanel();
        overviewImagePanelContainer.setLayout(new BoxLayout(overviewImagePanelContainer, BoxLayout.Y_AXIS));
        overviewImagePanelContainer.add(overviewImagePanel);
        
        leftPane.add("Overview", overviewImagePanelContainer, true);

        // OBSERVATION
        observationPanel = new ObservationPanel();
        CollapsibleObservation newPane = new CollapsibleObservation(
                observationPanel);
        // newPane.setBorder(normalBorder);

        // LAYERS
        imageSelectorPanel = new ImageSelectorPanel();

        imageSelectorPanel.add("New Layer", newPane);     
  
        leftPane.add("Layers", imageSelectorPanel, true);

//        // FEATURES / EVENTS
//        solarEventCatalogsPanel = new SolarEventCatalogsPanel();
//        leftPane.add("Features/Events", solarEventCatalogsPanel, false);

        
        JPanel leftPanelContainer = new JPanel();
        leftPanelContainer.add(leftPane);
        
        leftScrollPane = new JScrollPane(leftPanelContainer,
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        leftScrollPane.setFocusable(false);
         
        contentPanel.add(leftScrollPane, BorderLayout.WEST);

        // ///////////////////////////////////////////////////////////////////////////////
        // STATUS PANEL
        // ///////////////////////////////////////////////////////////////////////////////
        ZoomStatusPanel zoomStatusPanel = new ZoomStatusPanel();
        QualityStatusPanel qualityStatusPanel = new QualityStatusPanel();
        FramerateStatusPanel framerateStatus = new FramerateStatusPanel();
        PositionStatusPanel positionStatusPanel = new PositionStatusPanel(mainImagePanel);
        MetaDataStatusPanel jhvXMLStatusPanel = new MetaDataStatusPanel();
        renderModeStatus = new RenderModeStatusPanel();
        JPIPStatusPanel jpipStatusPanel = new JPIPStatusPanel();

        StatusPanel statusPanel = new StatusPanel(SIDE_PANEL_WIDTH + 20, 5);
        statusPanel.addPlugin(zoomStatusPanel, StatusPanel.Alignment.LEFT);
        statusPanel.addPlugin(qualityStatusPanel, StatusPanel.Alignment.LEFT);
        statusPanel.addPlugin(framerateStatus, StatusPanel.Alignment.LEFT);
        statusPanel.addPlugin(renderModeStatus, StatusPanel.Alignment.RIGHT);
        statusPanel.addPlugin(jhvXMLStatusPanel, StatusPanel.Alignment.RIGHT);
        statusPanel.addPlugin(jpipStatusPanel, StatusPanel.Alignment.RIGHT);
        statusPanel.addPlugin(positionStatusPanel, StatusPanel.Alignment.RIGHT);        
        contentPanel.add(statusPanel, BorderLayout.PAGE_END);
    }
    
    /**
     * Packs, positions and shows the GUI
     * 
     * @param _show
     *            If GUI should be displayed.
     */
    public void packAndShow(final boolean _show) {
        
        final JHVSplashScreen splash = JHVSplashScreen.getSingletonInstance();
        
        // load images which should be displayed first in a separated thread
        // that splash screen will be updated
    	splash.setProgressText("Loading Images...");
    	
    	Thread thread = new Thread(new Runnable() {
            
            public void run() {
                
                loadImagesAtStartup();
                
                // show GUI
                splash.setProgressText("Starting JHelioviewer...");
                splash.nextStep();
                mainFrame.pack();
                mainFrame.setLocationRelativeTo(null);
                mainFrame.setVisible(_show);        
                splash.setProgressValue(100);
                
                // remove splash screen
                splash.dispose();
            }
        }, "LoadImagesOnStartUp");
    	
    	thread.start();
    }
    
    /**
     * Initializes the main and overview view chain.
     * 
     * 
     */
    public void createViewchains() {
    	boolean firstTime = (mainComponentView == null);
    	
    	renderModeStatus.updateStatus();
    	
    	// Create main view chain
    	ViewchainFactory mainFactory = new ViewchainFactory();
    	mainComponentView = mainFactory.createViewchainMain(mainComponentView, false);
    	if (mainComponentView != null) {
    		mainImagePanel.setView(mainComponentView);
    	}
    	
    	// create overview view chain
    	if(firstTime) {

    		ViewchainFactory overviewFactory = new ViewchainFactory(true);
    	    overviewComponentView = overviewFactory.createViewchainOverview(mainComponentView, overviewComponentView, false);
    	
	    	// Connect Viewchain to GUI
	    	if (overviewComponentView != null) {
	    		overviewImagePanel.setView(overviewComponentView);
	    	}
    	} else {
    		overviewComponentView.getAdapter(SynchronizeView.class).setObservedView(mainComponentView);
    	}
       
    	ViewListenerDistributor.getSingletonInstance().setView(mainComponentView);
        imageSelectorPanel.setLayeredView(mainComponentView.getAdapter(LayeredView.class));
        
        if(firstTime) {
            packAndShow(true);
        } else {
        	mainFrame.validate();
        }
    }
    
    /**
     * Loads the images which have to be displayed when the program starts.
     * 
     * If there are any images defined in the command line, than this messages
     * tries to load this images. Otherwise it tries to load a default image
     * which is defined by the default entries of the observation panel.
     * */
    private void loadImagesAtStartup() {
        
        CommandLineProcessor commandLineProcessor = new CommandLineProcessor();
        
        // get values for different command line options
        AbstractList<String[]> jhvRequests = commandLineProcessor.getJHVOptionValues();
        URI jpipUri = commandLineProcessor.getJPIPOptionValue();
        URI downloadAddress = commandLineProcessor.getDownloadOptionValue();
        
        // load default image when no images specifications are available in command line
        if (jhvRequests.size() == 0 && jpipUri == null && downloadAddress == null) {
            
        	/*try {
//        		APIRequestManager.requestAndOpenRemoteFile( 
//	                    observationPanel.getStartDate(), observationPanel.getStartTime(),
//	                    observationPanel.getObservation(), observationPanel.getInstrument(), 
//	                    observationPanel.getDetector(), observationPanel.getMeasurement());
        		APIRequestManager.requestAndOpenRemoteFile(false, observationPanel.getCadence(), 
        				observationPanel.getStartDate(), observationPanel.getStartTime(), 
        			observationPanel.getEndDate(), observationPanel.getEndTime(), 
        				observationPanel.getObservation(), observationPanel.getInstrument(),
        			observationPanel.getDetector(), observationPanel.getMeasurement());
        	} catch (Exception e) {e.printStackTrace();
        		JHVGlobals.err("An error occured while opening the remote file!", e.getMessage(), false);
            }
            */
            return;
        } 
        
        // //////////////////////
        // -jhv
        // //////////////////////
        
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy/MM/dd");
        
        // go through all jhv values
        for (String[] jhvRequest : jhvRequests) { 
            try {
                Date date = dateformat.parse(jhvRequest[0]);
              
                if (APIRequestManager.dateIsValid(date) && APIRequestManager.timeIsValid(jhvRequest[1])) {
                    
                    // load image and memorize corresponding view
                    URI address = APIRequestManager.getImageURI(jhvRequest[2], jhvRequest[3], jhvRequest[4], jhvRequest[5], date, jhvRequest[1]);            
                    
                    ImageInfoView imageInfoView = APIRequestManager.newLoad(address);
                    
                    if (imageInfoView != null && mainComponentView != null) {
                        // get the layered view
                        LayeredView layeredView = mainComponentView.getAdapter(LayeredView.class);
                        
                        // go through all sub view chains of the layered view and try to find the
                        // view chain of the corresponding image info view
                        for (int i = 0; i < layeredView.getNumLayers(); i++) {
                            View subView = layeredView.getLayer(i);
                            
                            // if view has been found try to find the opacity filter view
                            if (imageInfoView.equals(subView.getAdapter(ImageInfoView.class))) {
                                
                                // get opacity value
                                int opacity = 100;
                                try {
                                    opacity = Integer.parseInt(jhvRequest[6]);
                                    opacity = opacity < 1 ? 100 : (opacity > 100 ? 100 : opacity);
                                } catch (NumberFormatException e) {}
                                
                                // find opacity filter view 
                                FilterView filterView = subView.getAdapter(FilterView.class);
                                
                                while (filterView != null) {
                                    
                                    // if opacity filter has been found set opacity value 
                                    if (filterView.getFilter() instanceof OpacityFilter) {
                                        ((OpacityFilter)(filterView.getFilter())).setOpacityExternal(opacity / 100.0f);
                                        break;
                                    }
                                    
                                    // find next filter view
                                    View view = filterView.getView();
                                    
                                    if (view == null)
                                        filterView = null;
                                    else
                                        filterView = view.getAdapter(FilterView.class);
                                }
                                
                                break;
                            }                             
                        }
                    }
                }              
            } catch (ParseException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				JHVGlobals.err("An error occured while opening the remote file!", e.getMessage(), false);
			}    
        }
        
        // //////////////////////
        // -jpip
        // //////////////////////
        
        try {
        	APIRequestManager.newLoad(jpipUri);
        } catch (IOException e) {
        	JHVGlobals.err("An error occured while opening the remote file!", e.getMessage(), false);
        }
        
        // //////////////////////
        // -download
        // //////////////////////
        
        try {
        	FileDownloader fileDownloader = new FileDownloader();
        	File downloadFile = fileDownloader.getDefaultDownloadLocation(downloadAddress);
        	fileDownloader.get(downloadAddress, downloadFile);
        	APIRequestManager.newLoad(downloadFile.toURI());
        } catch (IOException e) {
        	JHVGlobals.err("An error occured while opening the remote file!", e.getMessage(), false);
        }
    }

    /**
     * Method that creates and initializes the main JFrame.
     * 
     * @return the created and initialized main frame.
     */
    private JFrame createMainFrame() {
        JFrame frame = new JFrame("ESA JHelioviewer");

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent arg0) {
            	ExitProgramAction exitAction = new ExitProgramAction();
            	exitAction.actionPerformed(new ActionEvent(this, 0, ""));
            }
        });

        Dimension maxSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension minSize = new Dimension(800, 600);

        maxSize.width -= 200;
        maxSize.height -= 150;

        minSize.width = Math.min(minSize.width, maxSize.width);
        minSize.height = Math.min(minSize.height, maxSize.height);
        frame.setMaximumSize(maxSize);
        frame.setMinimumSize(minSize);
        frame.setPreferredSize(maxSize);
        frame.setFont(new Font("SansSerif", Font.BOLD, 12));
        return frame;
    }

    /**
     * Returns the only instance of this class.
     * 
     * @return the only instance of this class.
     * */
    public static ImageViewerGui getSingletonInstance() {
        return singletonImageViewer;
    }

    /** 
     * Returns the main frame. 
     * 
     * @return the main frame.
     * */
    public static JFrame getMainFrame() {
        return mainFrame;
    }

    /** 
     * Returns instance of the main image panel. 
     * 
     * @return instance of the main image panel.
     * */
    public MainImagePanel getMainImagePanel() {
        return mainImagePanel;
    }
    
    /**
     * Returns instance of the main ComponentView.
     * 
     * @return instance of the main ComponentView.
     */
    public ComponentView getMainView() {
    	return mainComponentView;
    }
    
    /**
     * Returns instance of the overview ComponentView.
     * 
     * @return instance of the overview ComponentView.
     */
    public ComponentView getOverviewView() {
    	return overviewComponentView;
    }
    
    /**
     * Returns the left content pane.
     * 
     * @return instance of the left content pane.
     * */
    public SideContentPane getLeftContentPane() {
    	return leftPane;
    }

    /**
     * Returns the instance of the ImageSelectorPanel.
     * 
     * @return instance of the image selector panel.
     * */
    public ImageSelectorPanel getImageSelectorPanel() {
        return imageSelectorPanel;
    }
    
    /** 
     * Returns the top tool bar instance.
     * 
     * @return instance of the top tool bar.
     * */
    public TopToolBar getTopToolBar() {
    	return topToolBar;
    }

    /**
     * Calls the update methods of sub components of the main frame so they can
     * e.g. reload data.
     * */
    public void updateComponents() {
        
        observationPanel.updateComponent();
        imageSelectorPanel.updateComponent();
//        solarEventCatalogsPanel.updateComponents();
    }
    
    /**
     * Toggles the visibility of the control panel on the left side.
     */
    public void toggleShowSidePanel() {
    	leftScrollPane.setVisible(!leftScrollPane.isVisible());
    	contentPanel.revalidate();
    }
}