Skip to content

Commit

Permalink
feature: Issue #17 Save data confirmdialog when finish application
Browse files Browse the repository at this point in the history
  • Loading branch information
astrapi69 committed Jul 22, 2023
1 parent 0454092 commit 9531963
Show file tree
Hide file tree
Showing 8 changed files with 396 additions and 284 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ izpackVersion=5.1.3
lombokVersion=1.18.28
modelDataVersion=2.1
swingComponentsVersion=8.1
swingBaseComponentsVersion=2.6
swingBaseComponentsVersion=2.7
swingTreeComponentVersion=2.5
swingRendererVersion=1.2
swingTableComponentsVersion=2
Expand Down Expand Up @@ -81,7 +81,7 @@ commonsCodecVersion=1.16.0
netExtensionsVersion=5.2
genTreeVersion=8.3
treeApiVersion=1.3
iconImgExtensionsVersion=2
iconImgExtensionsVersion=2.1
jremixIconVersion=1.1
batikVersion=1.16
imgscalrLibVersion=4.2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,22 @@
package io.github.astrapi69.mystic.crypt;

import java.awt.*;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.Serial;
import java.security.Security;

import javax.swing.*;

import io.github.astrapi69.design.pattern.observer.event.EventObject;
import io.github.astrapi69.design.pattern.observer.event.EventSource;
import io.github.astrapi69.model.enumtype.visibity.RenderMode;
import io.github.astrapi69.mystic.crypt.app.file.xml.ApplicationXmlFileStoreWorker;
import io.github.astrapi69.mystic.crypt.eventbus.ApplicationEventBus;
import io.github.astrapi69.swing.dialog.JOptionPaneExtensions;
import io.github.astrapi69.swing.enumeration.FrameMode;
import io.github.astrapi69.swing.panel.label.LabelPanel;
import io.github.astrapi69.window.adapter.CloseWindow;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NonNull;
Expand Down Expand Up @@ -202,7 +211,8 @@ protected void onBeforeInitialize()
// add once the default provider to the Security class
setSecurityProvider();
// initialize model and model object
setModel(BaseModel.of(ApplicationModelBean.builder().build()));
ApplicationModelBean applicationModelBean = ApplicationModelBean.builder().build();
setModel(BaseModel.of(applicationModelBean));
super.onBeforeInitialize();
}

Expand Down Expand Up @@ -264,6 +274,7 @@ protected void onAfterInitialize()
setDefaultLookAndFeel(LookAndFeels.NIMBUS, this);
this.setSize(ScreenSizeExtensions.getScreenWidth(), ScreenSizeExtensions.getScreenHeight());
onEnableMenu();
onWindowClosing();
}

public void onEnableMenu()
Expand Down Expand Up @@ -333,4 +344,39 @@ public void switchToApplicationPanel()
replaceMainComponent(getApplicationPanel());
instance.frameMode = FrameMode.APPLICATION_PANEL;
}

protected void onWindowClosing()
{
MysticCryptApplicationFrame.this.addWindowListener(new CloseWindow()
{
@Override
public void windowClosing(WindowEvent windowEvent)
{
ApplicationModelBean modelObject = MysticCryptApplicationFrame.this
.getModelObject();
boolean dirty = modelObject.isDirty();
if (dirty)
{
String defaultMessage = "<html><body>"
+ "<div>The current database file is modified.</div>"
+ "<div>Store your changes before finish application</div>"
+ "</body></html>";
String confirmMessage = Messages
.getString("dialog.confirm.save.before.close.message", defaultMessage);
LabelPanel panel = new LabelPanel(BaseModel.of(confirmMessage));
int option = JOptionPaneExtensions.getSelectedOption(panel,
JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_CANCEL_OPTION,
MysticCryptApplicationFrame.this,
Messages.getString("dialog.confirm.save.before.close.title",
"Save Database Before Close."),
null);
if (option == JOptionPane.YES_OPTION)
{
ApplicationXmlFileStoreWorker.storeApplicationFile(modelObject);
}
}
super.windowClosing(windowEvent);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@
package io.github.astrapi69.mystic.crypt.action;

import java.awt.event.ActionEvent;
import java.io.Serial;

import javax.swing.*;

import io.github.astrapi69.design.pattern.observer.event.EventObject;
import io.github.astrapi69.design.pattern.observer.event.EventSource;
import io.github.astrapi69.model.enumtype.visibity.RenderMode;
import io.github.astrapi69.mystic.crypt.ApplicationModelBean;
import io.github.astrapi69.mystic.crypt.MysticCryptApplicationFrame;
import io.github.astrapi69.mystic.crypt.eventbus.ApplicationEventBus;
import io.github.astrapi69.mystic.crypt.app.file.xml.ApplicationXmlFileStoreWorker;
Expand All @@ -40,6 +39,7 @@ public class SaveApplicationFileAction extends AbstractAction
{

/** The Constant serialVersionUID. */
@Serial
private static final long serialVersionUID = 1L;

public SaveApplicationFileAction(final String name)
Expand All @@ -53,11 +53,8 @@ public SaveApplicationFileAction(final String name)
@Override
public void actionPerformed(final ActionEvent e)
{
ApplicationModelBean applicationModelBean = MysticCryptApplicationFrame.getInstance()
.getModelObject();
ApplicationXmlFileStoreWorker.storeApplicationFile(applicationModelBean);

final EventSource<EventObject<RenderMode>> eventSource = ApplicationEventBus.getSaveState();
eventSource.fireEvent(new EventObject<>(RenderMode.VIEWABLE));
ApplicationXmlFileStoreWorker.storeApplicationFile(MysticCryptApplicationFrame.getInstance()
.getModelObject());
ApplicationEventBus.getSaveState().fireEvent(new EventObject<>(RenderMode.VIEWABLE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public final class ApplicationXmlFileStoreWorker
public static void storeApplicationFile(ApplicationModelBean applicationModelBean)
{
MasterPwFileModelBean modelObject = applicationModelBean.getMasterPwFileModelBean();
if (applicationModelBean.isDirty())
{
applicationModelBean.setDirty(false);
}
SignInType signInType = SignInType.toSignInType(modelObject);
if (SignInType.PASSWORD_AND_PRIVATE_KEY.equals(signInType))
{
Expand Down
Loading

0 comments on commit 9531963

Please sign in to comment.