Skip to content

Commit

Permalink
Warning before closing SOL editor with modified file
Browse files Browse the repository at this point in the history
  • Loading branch information
jindrapetrik committed Nov 10, 2024
1 parent 3528459 commit cb02c45
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
5 changes: 2 additions & 3 deletions src/com/jpexs/decompiler/flash/gui/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3107,8 +3107,7 @@ public static void main(String[] args) throws IOException {
checkLibraryVersion();
View.execInEventDispatch(() -> {
DefaultSyntaxKit.initKit();
SolEditorFrame solEditor = new SolEditorFrame();
solEditor.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
SolEditorFrame solEditor = new SolEditorFrame(true);
solEditor.setVisible(true);
});
} else {
Expand Down Expand Up @@ -3583,7 +3582,7 @@ public static SWF getSwfByHash(String hash) {
}

public static void openSolEditor() {
SolEditorFrame solEdit = new SolEditorFrame();
SolEditorFrame solEdit = new SolEditorFrame(false);
solEdit.setVisible(true);
}
}
50 changes: 33 additions & 17 deletions src/com/jpexs/decompiler/flash/gui/soleditor/SolEditorFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
Expand Down Expand Up @@ -72,10 +74,24 @@ public class SolEditorFrame extends AppFrame {

private final DocumentListener modifiedListener;

public SolEditorFrame() {
setDefaultCloseOperation(HIDE_ON_CLOSE);
public SolEditorFrame(boolean exitOnClose) {
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
setTitle(translate("dialog.title"));

addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
if (modified && ViewMessages.showConfirmDialog(SolEditorFrame.this, translate("warning.loseChanges"), AppStrings.translate("message.warning"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
return;
}
if (exitOnClose) {
System.exit(0);
} else {
setVisible(false);
}
}
});

modifiedListener = new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
Expand Down Expand Up @@ -105,19 +121,19 @@ private void setModified() {
cnt.setLayout(new BorderLayout());

JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));

JButton newButton = new JButton(translate("button.new"), View.getIcon("newswf16"));
newButton.addActionListener(this::newActionPerformed);

JButton openButton = new JButton(translate("button.open"), View.getIcon("open16"));
openButton.addActionListener(this::openActionPerformed);

JButton openNpApiButton = new JButton(translate("button.open.npapi"), View.getIcon("open16"));
openNpApiButton.addActionListener(this::openNpApiActionPerformed);

JButton openPpApiButton = new JButton(translate("button.open.ppapi"), View.getIcon("open16"));
openPpApiButton.addActionListener(this::openPpApiActionPerformed);

saveButton = new JButton(translate("button.save"), View.getIcon("save16"));
saveButton.addActionListener(this::saveActionPerformed);
saveButton.setEnabled(false);
Expand All @@ -127,7 +143,7 @@ private void setModified() {

topPanel.add(newButton);
topPanel.add(openButton);

File npApiDirectory = SharedObjectsStorage.getNpApiDirectory();
if (npApiDirectory != null && npApiDirectory.exists()) {
topPanel.add(openNpApiButton);
Expand All @@ -151,11 +167,11 @@ private void setModified() {
@Override
public void actionPerformed(ActionEvent e) {
amfVersionLabel.setText(amfVersionComboBox.getSelectedItem().toString());
}
}
});

amfVersionLabel.setVisible(false);

JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
bottomPanel.add(new JLabel(translate("filename")));
bottomPanel.add(fileNameField);
Expand All @@ -175,7 +191,7 @@ public void actionPerformed(ActionEvent e) {
}

private void newActionPerformed(ActionEvent e) {
if (modified && ViewMessages.showConfirmDialog(this, translate("warning.loseChanges"), AppStrings.translate("message.warning"), JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
if (modified && ViewMessages.showConfirmDialog(this, translate("warning.loseChanges"), AppStrings.translate("message.warning"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
return;
}
openedFile = null;
Expand All @@ -188,19 +204,19 @@ private void newActionPerformed(ActionEvent e) {
updateTitle();
fileNameField.setText(translate("untitled"));
}

private void openActionPerformed(ActionEvent e) {
openDirectory(new File(Configuration.lastSolEditorDirectory.get()));
}

private void openNpApiActionPerformed(ActionEvent e) {
openDirectory(SharedObjectsStorage.getNpApiDirectory());
}

private void openPpApiActionPerformed(ActionEvent e) {
openDirectory(SharedObjectsStorage.getPpApiDirectory());
}
}

private void openDirectory(File directory) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileHidingEnabled(false);
Expand Down Expand Up @@ -238,7 +254,7 @@ public String getDescription() {
}
fileNameField.setText(newFileName);
amfVersionComboBox.setSelectedItem(newAmfVersion);
amfVersionLabel.setText("" + newAmfVersion);
amfVersionLabel.setText("" + newAmfVersion);
} catch (IOException | IllegalArgumentException ex) {
ViewMessages.showMessageDialog(this, translate("error.cannotOpen") + " " + ex.getLocalizedMessage(), AppStrings.translate("error"), JOptionPane.ERROR_MESSAGE);
return;
Expand Down Expand Up @@ -307,7 +323,7 @@ private void saveAs(File saveFile) {

private void saveAsActionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileHidingEnabled(false);
fileChooser.setFileHidingEnabled(false);
fileChooser.setFileFilter(new FileFilter() {
@Override
public boolean accept(File f) {
Expand Down

0 comments on commit cb02c45

Please sign in to comment.