]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.workbench/src/org/simantics/workbench/internal/ApplicationUtil.java
Initial version of SCL tab contributions
[simantics/platform.git] / bundles / org.simantics.workbench / src / org / simantics / workbench / internal / ApplicationUtil.java
1 package org.simantics.workbench.internal;\r
2 \r
3 import java.util.concurrent.atomic.AtomicBoolean;\r
4 \r
5 import org.eclipse.jface.dialogs.MessageDialog;\r
6 import org.simantics.db.Session;\r
7 import org.simantics.ui.SimanticsUI;\r
8 import org.simantics.utils.ui.ErrorLogger;\r
9 import org.simantics.utils.ui.dialogs.SafeMessageDialog;\r
10 \r
11 /**\r
12  * @author Tuukka Lehtonen\r
13  */\r
14 final class ApplicationUtil {\r
15 \r
16     /**\r
17      * Used to make sure that the user has a choice to save the changes into the\r
18      * active database before closing.\r
19      */\r
20     public static boolean allowShutdown(AtomicBoolean saveAtExit) {\r
21         try {\r
22             if (hasUnsavedChanges()) {\r
23                 String[] buttons = { "&Save", "&Don't save", "&Cancel" };\r
24                 int result = SafeMessageDialog.doMessageDialog("Save Resources", null, "Save changes before closing ?",\r
25                         MessageDialog.QUESTION, buttons, 2);\r
26 \r
27                 switch (result) {\r
28                     case 0:\r
29                         saveAtExit.set(true);\r
30                         break;\r
31                     case 1:\r
32                         saveAtExit.set(false);\r
33                         break;\r
34                     case 2:\r
35                         return false;\r
36                     default:\r
37                         return false;\r
38                 }\r
39             }\r
40 \r
41             // If any errors occur during this check, just log them, but allow\r
42             // for the program to be closed and make sure nothing is saved,\r
43             // since it would probably be corrupt.\r
44         } catch (RuntimeException e) {\r
45             ErrorLogger.defaultLogError(\r
46                     "RuntimeException occured while querying database session for unsaved changes.", e);\r
47             saveAtExit.set(false);\r
48         } catch (Error e) {\r
49             ErrorLogger.defaultLogError("Error occured while querying database session for unsaved changes.", e);\r
50             saveAtExit.set(false);\r
51         }\r
52         return true;\r
53     }\r
54 \r
55     public static boolean hasUnsavedChanges() {\r
56         Session session = SimanticsUI.peekSession();\r
57         if (session == null)\r
58             return false;\r
59 \r
60         // hasChangesToSave is deprecated\r
61 //        LifecycleSupport lfs = session.getService(LifecycleSupport.class);\r
62 //        try {\r
63 //            return lfs.hasChangesToSave();\r
64 //        } catch (DatabaseException e) {\r
65 //            ErrorLogger.defaultLogError(\r
66 //                    "Problems encountered while checking for unsaved changes, see exception for details.", e);\r
67 //        }\r
68         return false;\r
69     }\r
70 \r
71 }\r