View Javadoc

1   /* OpenLogViewer
2    *
3    * Copyright 2011
4    *
5    * This file is part of the OpenLogViewer project.
6    *
7    * OpenLogViewer software is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation, either version 3 of the License, or
10   * (at your option) any later version.
11   *
12   * OpenLogViewer software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with any OpenLogViewer software.  If not, see http://www.gnu.org/licenses/
19   *
20   * I ask that if you make any changes to this file you fork the code on github.com!
21   *
22   */
23  
24  /*
25   * OpenLogViewerApp.java
26   *
27   * Created on Jan 26, 2011, 2:55:31 PM
28   */
29  package org.diyefi.openlogviewer;
30  
31  import java.awt.Dimension;
32  import java.awt.event.ActionEvent;
33  import java.awt.event.ActionListener;
34  import java.awt.event.KeyEvent;
35  import java.awt.event.KeyListener;
36  import java.io.File;
37  import java.io.IOException;
38  import java.io.FileInputStream;
39  import java.io.FileOutputStream;
40  import java.util.ArrayList;
41  import java.util.Properties;
42  
43  import javax.swing.JFileChooser;
44  import javax.swing.UIManager;
45  import javax.swing.UnsupportedLookAndFeelException;
46  import javax.swing.filechooser.FileFilter;
47  
48  import org.diyefi.openlogviewer.decoder.CSVTypeLog;
49  import org.diyefi.openlogviewer.decoder.FreeEMSBin;
50  import org.diyefi.openlogviewer.decoder.FreeEMSByteLA;
51  import org.diyefi.openlogviewer.filefilters.CSVTypeFileFilter;
52  import org.diyefi.openlogviewer.filefilters.FreeEMSFileFilter;
53  import org.diyefi.openlogviewer.filefilters.FreeEMSLAFileFilter;
54  import org.diyefi.openlogviewer.genericlog.GenericLog;
55  import org.diyefi.openlogviewer.graphing.EntireGraphingPanel;
56  import org.diyefi.openlogviewer.graphing.GraphPositionPanel;
57  import org.diyefi.openlogviewer.graphing.MultiGraphLayeredPane;
58  import org.diyefi.openlogviewer.optionpanel.OptionFrameV2;
59  import org.diyefi.openlogviewer.propertypanel.PropertiesPane;
60  import org.diyefi.openlogviewer.propertypanel.SingleProperty;
61  import org.diyefi.openlogviewer.utils.Utilities;
62  
63  /**
64   *
65   * @author Bryan
66   */
67  public class OpenLogViewerApp extends javax.swing.JFrame {
68  
69  	/** Creates new form OpenLogViewerApp */
70      public OpenLogViewerApp() {
71          initComponents();
72      }
73  
74      private void initComponents() {
75          properties = new ArrayList<SingleProperty>();
76          prefFrame = new PropertiesPane("Properties");
77          prefFrame.setProperties(properties);
78          optionFrame = new OptionFrameV2();
79          mainPanel = new javax.swing.JPanel();
80          graphingPanel = new EntireGraphingPanel();
81          playBar = new PlayBarPanel();
82          graphMenu = new GraphMenu();
83          mainMenuBar = new javax.swing.JMenuBar();
84          fileMenu = new javax.swing.JMenu();
85          editMenu = new javax.swing.JMenu();
86          openFileMenuItem = new javax.swing.JMenuItem();
87          quitFileMenuItem = new javax.swing.JMenuItem();
88          propertiesOptionMenuItem = new javax.swing.JMenuItem();
89  
90  
91          this.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
92          this.setLayout(new java.awt.BorderLayout());
93          this.setFocusable(true);
94  
95  
96          ////////////////////////////////////////////////////////////
97          ///setup mainpanel
98          ///////////////////////////////////////////////////////////
99          mainPanel.setName("Main Panel");
100         mainPanel.setLayout(new java.awt.BorderLayout());
101         this.add(mainPanel, java.awt.BorderLayout.CENTER);
102         graphingPanel.setPreferredSize(new Dimension(600, 420));
103         mainPanel.add(graphingPanel, java.awt.BorderLayout.CENTER);
104         mainPanel.add(playBar, java.awt.BorderLayout.SOUTH);
105 
106 
107         //////////////////////////////////////////////////////////////////
108         ///////////File Menu
109         //////////////////////////////////////////////////////////////////
110         openFileMenuItem.setText("Open Log");
111         openFileMenuItem.setName("openlog");
112         openFileMenuItem.addActionListener(new ActionListener() {
113 
114             @Override
115             public void actionPerformed(ActionEvent e) {
116                 openFileMenuItemMouseReleased(e);
117             }
118         });
119 
120 
121         quitFileMenuItem.setText("Quit");
122         quitFileMenuItem.setName("quit");
123         quitFileMenuItem.addActionListener(new ActionListener() {
124 
125             @Override
126             public void actionPerformed(ActionEvent e) {
127                 System.exit(0);
128             }
129         });
130 
131 
132         fileMenu.setText("File");
133         fileMenu.setName("file");
134         fileMenu.add(openFileMenuItem);
135         fileMenu.add(quitFileMenuItem);
136 
137         ////////////////////////////////////////////////////////////////////
138         ///////////////Edit Menu
139         ////////////////////////////////////////////////////////////////////
140         editMenu.setText("Edit");
141         editMenu.setName("edit"); // NOI18N
142 
143         propertiesOptionMenuItem.setText("Properties");
144         propertiesOptionMenuItem.addActionListener(new ActionListener() {
145 
146             @Override
147             public void actionPerformed(ActionEvent e) {
148                 OpenLogViewerApp.getInstance().getPropertyPane().setVisible(true);
149                 OpenLogViewerApp.getInstance().getPropertyPane().setAlwaysOnTop(true);
150                 OpenLogViewerApp.getInstance().getPropertyPane().setAlwaysOnTop(false);
151 
152             }
153         });
154         editMenu.add(propertiesOptionMenuItem);
155 
156 
157         //////////////////////////////////////////////////////////////////
158         //////////////Add to mainMenu
159         /////////////////////////////////////////////////////////////////
160         mainMenuBar.setName("Main Menu");
161         mainMenuBar.add(fileMenu);
162         mainMenuBar.add(editMenu);
163         ////////////////////////////////////////////////////////////////////
164         ///////////////////Graph menu
165         /////////////////////GraphMenu.java
166         ////////////////////////////////////////////////////////////////////
167         mainMenuBar.add(graphMenu);
168 
169         setJMenuBar(mainMenuBar);
170         this.addKeyListener(graphingPanel);
171         pack();
172     }
173 
174     private void openFileMenuItemMouseReleased(ActionEvent evt) {
175         openFile();
176     }
177 
178     public void setLog(GenericLog genericLog) {
179     	graphingPanel.setLog(genericLog);
180     }
181 
182     /**
183      * Returns the reference to this instance, it is meant to be a method to make getting the main frame simpler
184      * @return <code>this</code> instance
185      */
186     public static OpenLogViewerApp getInstance() {
187         return mainAppRef;
188     }
189 
190     /**
191      * @param args the command line arguments
192      */
193     public static void main(String args[]) {
194         java.awt.EventQueue.invokeLater(new Runnable() {
195 
196             @Override
197             public void run() {
198                 try {
199                     // Set cross-platform Java L&F (also called "Metal")
200                     UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
201                 } catch (UnsupportedLookAndFeelException e) {
202                     // handle exception
203                 } catch (ClassNotFoundException e) {
204                     // handle exception
205                 } catch (InstantiationException e) {
206                     // handle exception
207                 } catch (IllegalAccessException e) {
208                     // handle exception
209                 }
210                 mainAppRef = new OpenLogViewerApp();
211                 mainAppRef.setVisible(true);
212                 mainAppRef.setTitle("OpenLogViewer -");
213             }
214         });
215     }
216 
217     public EntireGraphingPanel getEntireGraphingPanel(){
218     	return graphingPanel;
219     }
220 
221     public MultiGraphLayeredPane getMultiGraphLayeredPane() {
222         return graphingPanel.getMultiGraphLayeredPane();
223     }
224     
225     public GraphPositionPanel getGraphPositionPanel() {
226         return graphingPanel.getGraphPositionPanel();
227     }
228 
229     public GraphMenu getGraphMenu() {
230         return graphMenu;
231     }
232 
233     public OptionFrameV2 getOptionFrame() {
234         return optionFrame;
235     }
236 
237     public PropertiesPane getPropertyPane() {
238         return prefFrame;
239     }
240 
241     public ArrayList<SingleProperty> getProperties() {
242         return properties;
243     }
244 
245     public void openFile() {
246         JFileChooser fileChooser = new JFileChooser();
247         String lastFingFile = getApplicationWideProperty("lastFingFile");
248         if (lastFingFile != null) {
249             fileChooser.setSelectedFile(new File(lastFingFile));
250         } else {
251             String lastFingDir = getApplicationWideProperty("lastFingDir");
252             if (lastFingDir != null) {
253                 fileChooser.setCurrentDirectory(new File(lastFingDir));
254             }
255         }
256         fileChooser.addChoosableFileFilter(new FreeEMSFileFilter());
257         fileChooser.addChoosableFileFilter(new CSVTypeFileFilter());
258         fileChooser.addChoosableFileFilter(new FreeEMSLAFileFilter());
259         String chooserClass = getApplicationWideProperty("chooserClass");
260         if (chooserClass != null) {
261             try {
262                 fileChooser.setFileFilter((FileFilter) Class.forName(chooserClass).newInstance());
263             } catch (ClassNotFoundException c) {
264                 removeApplicationWideProperty("chooserClass");
265                 System.out.println("Class not found! chooserClass removed from props!");
266             } catch (InstantiationException i) {
267                 removeApplicationWideProperty("chooserClass");
268                 System.out.println("Could not instantiate class! chooserClass removed from props!");
269             } catch (IllegalAccessException l) {
270                 removeApplicationWideProperty("chooserClass");
271                 System.out.println("Could access class! chooserClass removed from props!");
272             }
273         }
274 
275         int acceptValue = fileChooser.showOpenDialog(OpenLogViewerApp.getInstance());
276         if (acceptValue == JFileChooser.APPROVE_OPTION) {
277             File openFile = fileChooser.getSelectedFile();
278             if (Utilities.getExtension(openFile).equals("bin") || fileChooser.getFileFilter() instanceof FreeEMSFileFilter) {
279                 new FreeEMSBin(openFile);
280             } else if (Utilities.getExtension(openFile).equals("la") || fileChooser.getFileFilter() instanceof FreeEMSLAFileFilter) {
281                 new FreeEMSByteLA(openFile);
282             } else {
283                 new CSVTypeLog(openFile);
284             }
285             if (openFile != null) {
286                 OpenLogViewerApp.getInstance().setTitle("OpenLogViewer - " + openFile.getName());
287                 saveApplicationWideProperty("lastFingDir", openFile.getParent());
288                 saveApplicationWideProperty("lastFingFile", openFile.getPath());
289                 saveApplicationWideProperty("chooserClass", fileChooser.getFileFilter().getClass().getCanonicalName());
290                 System.gc();
291             }
292         }
293     }
294 
295     private String getApplicationWideProperty(String key) {
296         try {
297             Properties AppWide = new Properties();
298             File AppWideFile = openAppWideProps(AppWide);
299             if (AppWideFile != null) {
300                 return AppWide.getProperty(key);
301             } else {
302                 throw new IllegalArgumentException("received null instead of valid file");
303             }
304         } catch (IOException e) {
305             e.printStackTrace();
306             throw new RuntimeException(e.getMessage());
307         }
308     }
309 
310     private void saveApplicationWideProperty(String key, String value) {
311         try {
312             Properties AppWide = new Properties();
313             File AppWideFile = openAppWideProps(AppWide);
314             if (AppWideFile != null) {
315                 AppWide.setProperty(key, value);
316                 AppWide.store(new FileOutputStream(AppWideFile), "saved");
317             } else {
318                 throw new IllegalArgumentException("received null instead of valid file");
319             }
320         } catch (IOException e) {
321             e.printStackTrace();
322             throw new RuntimeException(e.getMessage());
323         }
324     }
325 
326     private void removeApplicationWideProperty(String key) {
327         try {
328             Properties AppWide = new Properties();
329             File AppWideFile = openAppWideProps(AppWide);
330             if (AppWideFile != null) {
331                 AppWide.remove(key);
332                 AppWide.store(new FileOutputStream(AppWideFile), "saved");
333             } else {
334                 throw new IllegalArgumentException("received null instead of valid file");
335             }
336         } catch (IOException e) {
337             e.printStackTrace();
338             throw new RuntimeException(e.getMessage());
339         }
340     }
341 
342     private File openAppWideProps(Properties AppWide) throws IOException {
343         File AppWideFile;
344         AppWideFile = new File(System.getProperty("user.home"));
345 
346         if (!AppWideFile.exists() || !AppWideFile.canRead() || !AppWideFile.canWrite()) {
347             System.out.println("Either you dont have a home director, or it isnt read/writeable... fix it!");
348         } else {
349             AppWideFile = new File(AppWideFile, ".OpenLogViewer");
350         }
351         if (!AppWideFile.exists()) {
352             try {
353                 if (AppWideFile.mkdir()) {
354                     AppWideFile = new File(AppWideFile, "OLVAllProperties.olv");
355                     if (AppWideFile.createNewFile()) {
356                         AppWide.load(new FileInputStream(AppWideFile));
357                     }
358                 } else {
359                     //find somewhere else
360                 }
361             } catch (IOException IOE) {
362                 System.out.print(IOE.getMessage());
363             }
364         } else {
365             AppWideFile = new File(AppWideFile, "OLVAllProperties.olv");
366             if (!AppWideFile.createNewFile()) {
367                 AppWide.load(new FileInputStream(AppWideFile));
368             }
369         }
370         return AppWideFile;
371     }
372     
373     // Variables declaration -
374     public static OpenLogViewerApp mainAppRef;
375     private javax.swing.JMenu fileMenu;
376     private javax.swing.JMenu editMenu;
377     private javax.swing.JMenuBar mainMenuBar;
378     private javax.swing.JMenuItem openFileMenuItem;
379     private javax.swing.JMenuItem quitFileMenuItem;
380     private javax.swing.JMenuItem propertiesOptionMenuItem;
381     private javax.swing.JPanel mainPanel;
382     private EntireGraphingPanel graphingPanel;
383     private PlayBarPanel playBar;
384     private GraphMenu graphMenu;
385     private OptionFrameV2 optionFrame;
386     private PropertiesPane prefFrame;
387     private ArrayList<SingleProperty> properties;
388 	private static final long serialVersionUID = 7987394054547975563L;
389 }