]> icculus.org git repositories - divverent/nexuiz.git/blob - misc/tools/NexuizDemoRecorder/main/src/main/java/com/nexuiz/demorecorder/application/DemoRecorderUtils.java
move NDR main program into subfolder
[divverent/nexuiz.git] / misc / tools / NexuizDemoRecorder / main / src / main / java / com / nexuiz / demorecorder / application / DemoRecorderUtils.java
1 package com.nexuiz.demorecorder.application;\r
2 \r
3 import java.io.File;\r
4 import java.io.IOException;\r
5 \r
6 import org.jdesktop.swingx.JXErrorPane;\r
7 import org.jdesktop.swingx.error.ErrorInfo;\r
8 \r
9 public class DemoRecorderUtils {\r
10         \r
11         public static void showNonCriticalErrorDialog(Throwable e) {\r
12                 if (!(e instanceof DemoRecorderException)) {\r
13                         e = new DemoRecorderException("Internal error", e);\r
14                 }\r
15                 ErrorInfo info = new ErrorInfo("Error occurred", e.getMessage(), null, null, e, null, null);\r
16                 JXErrorPane.showDialog(null, info);\r
17         }\r
18         \r
19         /**\r
20          * Shows an error dialog that contains the stack trace, catching the exception so that the program flow\r
21          * won't be interrupted.\r
22          * This method will maybe wrap e in a DemoRecorderException with the given message.\r
23          * @param customMessage\r
24          * @param e\r
25          * @param wrapException set to true if Exception should be wrapped into a DemoRecorderException\r
26          */\r
27         public static void showNonCriticalErrorDialog(String customMessage, Throwable e, boolean wrapException) {\r
28                 Throwable ex = e;\r
29                 if (wrapException && !(e instanceof DemoRecorderException)) {\r
30                         ex = new DemoRecorderException(customMessage, e);\r
31                 }\r
32                 \r
33                 ErrorInfo info = new ErrorInfo("Error occurred", ex.getMessage(), null, null, ex, null, null);\r
34                 JXErrorPane.showDialog(null, info);\r
35         }\r
36         \r
37         public static File computeLocalFile(String subDir, String fileName) {\r
38                 String path = System.getProperty("user.dir");\r
39                 if (subDir != null && !subDir.equals("")) {\r
40                         path += File.separator + subDir;\r
41                 }\r
42                 path += File.separator + fileName;\r
43                 return new File(path);\r
44         }\r
45         \r
46         /**\r
47          * Returns just the name of the file for a given File. E.g. if the File points to\r
48          * /home/someuser/somedir/somefile.end the function will return "somefile.end"\r
49          * @param file\r
50          * @return just the name of the file\r
51          */\r
52         public static String getJustFileNameOfPath(File file) {\r
53                 String fileString = file.getAbsolutePath();\r
54                 int lastIndex = fileString.lastIndexOf(File.separator);\r
55                 String newString = fileString.substring(lastIndex+1, fileString.length());\r
56                 return newString;\r
57         }\r
58         \r
59         /**\r
60          * Attempts to create an empty file (unless it already exists), including the creation\r
61          * of parent directories. If it succeeds to do so (or if the file already existed), true\r
62          * will be returned. Otherwise false will be returned\r
63          * @param file the file to be created\r
64          * @return true if file already existed or could successfully created, false otherwise\r
65          */\r
66         public static boolean attemptFileCreation(File file) {\r
67                 if (!file.exists()) {\r
68                         try {\r
69                                 file.createNewFile();\r
70                                 return true;\r
71                         } catch (IOException e) {\r
72                                 File parentDir = file.getParentFile();\r
73                                 if (!parentDir.exists()) {\r
74                                         try {\r
75                                                 if (parentDir.mkdirs() == true) {\r
76                                                         try {\r
77                                                                 file.createNewFile();\r
78                                                                 return true;\r
79                                                         } catch (Exception ex) {}\r
80                                                 }\r
81                                         } catch (Exception ex) {}\r
82                                 }\r
83                                 return false;\r
84                         }\r
85                 } else {\r
86                         return true;\r
87                 }\r
88         }\r
89         \r
90         public static final String getFileExtension(File file) {\r
91                 String fileName = file.getAbsolutePath();\r
92                 String ext = (fileName.lastIndexOf(".") == -1) ? "" : fileName.substring(fileName.lastIndexOf(".") + 1,fileName.length());\r
93                 return ext;\r
94         }\r
95 }\r