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