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