X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.workbench%2Fsrc%2Forg%2Fsimantics%2Fworkbench%2Finternal%2FApplicationUtil.java;fp=bundles%2Forg.simantics.workbench%2Fsrc%2Forg%2Fsimantics%2Fworkbench%2Finternal%2FApplicationUtil.java;h=ae0746ba17b1f9c39a5350a006cefa7ee0a8f14d;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.workbench/src/org/simantics/workbench/internal/ApplicationUtil.java b/bundles/org.simantics.workbench/src/org/simantics/workbench/internal/ApplicationUtil.java new file mode 100644 index 000000000..ae0746ba1 --- /dev/null +++ b/bundles/org.simantics.workbench/src/org/simantics/workbench/internal/ApplicationUtil.java @@ -0,0 +1,71 @@ +package org.simantics.workbench.internal; + +import java.util.concurrent.atomic.AtomicBoolean; + +import org.eclipse.jface.dialogs.MessageDialog; +import org.simantics.db.Session; +import org.simantics.ui.SimanticsUI; +import org.simantics.utils.ui.ErrorLogger; +import org.simantics.utils.ui.dialogs.SafeMessageDialog; + +/** + * @author Tuukka Lehtonen + */ +final class ApplicationUtil { + + /** + * Used to make sure that the user has a choice to save the changes into the + * active database before closing. + */ + public static boolean allowShutdown(AtomicBoolean saveAtExit) { + try { + if (hasUnsavedChanges()) { + String[] buttons = { "&Save", "&Don't save", "&Cancel" }; + int result = SafeMessageDialog.doMessageDialog("Save Resources", null, "Save changes before closing ?", + MessageDialog.QUESTION, buttons, 2); + + switch (result) { + case 0: + saveAtExit.set(true); + break; + case 1: + saveAtExit.set(false); + break; + case 2: + return false; + default: + return false; + } + } + + // If any errors occur during this check, just log them, but allow + // for the program to be closed and make sure nothing is saved, + // since it would probably be corrupt. + } catch (RuntimeException e) { + ErrorLogger.defaultLogError( + "RuntimeException occured while querying database session for unsaved changes.", e); + saveAtExit.set(false); + } catch (Error e) { + ErrorLogger.defaultLogError("Error occured while querying database session for unsaved changes.", e); + saveAtExit.set(false); + } + return true; + } + + public static boolean hasUnsavedChanges() { + Session session = SimanticsUI.peekSession(); + if (session == null) + return false; + + // hasChangesToSave is deprecated +// LifecycleSupport lfs = session.getService(LifecycleSupport.class); +// try { +// return lfs.hasChangesToSave(); +// } catch (DatabaseException e) { +// ErrorLogger.defaultLogError( +// "Problems encountered while checking for unsaved changes, see exception for details.", e); +// } + return false; + } + +}