added confirmation dialog on exit. fixes issue 117

master
beholder 2011-12-07 12:04:54 +01:00
parent 07a87d9393
commit c5000be6bf
3 changed files with 71 additions and 21 deletions

View File

@ -31,6 +31,7 @@ public class GeneralConfig {
private static final String STRENGTH_GAMES="strengthGames";
private static final String HIGH_QUALITY="hq";
private static final String SOUND="sound";
private static final String CONFIRM_EXIT = "confirmExit";
private static final int DEFAULT_LEFT=0;
private static final int DEFAULT_TOP=0;
@ -52,6 +53,7 @@ public class GeneralConfig {
private static final int DEFAULT_STRENGTH_GAMES=100;
private static final boolean DEFAULT_HIGH_QUALITY=false;
private static final boolean DEFAULT_SOUND=false;
private static final boolean DEFAULT_CONFIRM_EXIT = true;
private int left=DEFAULT_LEFT;
private int top=DEFAULT_TOP;
@ -73,6 +75,7 @@ public class GeneralConfig {
private int strengthGames=DEFAULT_STRENGTH_GAMES;
private boolean highQuality=DEFAULT_HIGH_QUALITY;
private boolean sound=DEFAULT_SOUND;
private boolean confirmExit = DEFAULT_CONFIRM_EXIT;
private GeneralConfig() {
@ -279,6 +282,14 @@ public class GeneralConfig {
this.sound=sound;
}
public boolean isConfirmExit() {
return confirmExit;
}
public void setConfirmExit(final boolean confirmExit) {
this.confirmExit = confirmExit;
}
private void load(final Properties properties) {
left=Integer.parseInt(properties.getProperty(LEFT,""+DEFAULT_LEFT));
@ -301,6 +312,7 @@ public class GeneralConfig {
strengthGames=Integer.parseInt(properties.getProperty(STRENGTH_GAMES,""+DEFAULT_STRENGTH_GAMES));
highQuality=Boolean.parseBoolean(properties.getProperty(HIGH_QUALITY,""+DEFAULT_HIGH_QUALITY));
sound=Boolean.parseBoolean(properties.getProperty(SOUND,""+DEFAULT_SOUND));
confirmExit = Boolean.parseBoolean(properties.getProperty(CONFIRM_EXIT,""+DEFAULT_CONFIRM_EXIT));
}
public void load() {
@ -328,6 +340,7 @@ public class GeneralConfig {
properties.setProperty(STRENGTH_GAMES,String.valueOf(strengthGames));
properties.setProperty(HIGH_QUALITY,String.valueOf(highQuality));
properties.setProperty(SOUND,String.valueOf(sound));
properties.setProperty(CONFIRM_EXIT,String.valueOf(confirmExit));
}
public void save() {

View File

@ -16,6 +16,7 @@ import magic.ui.widget.ZoneBackgroundLabel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
@ -24,6 +25,7 @@ import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
@ -85,7 +87,8 @@ public class MagicFrame extends JFrame implements ActionListener {
private DuelPanel duelPanel;
private ExplorerPanel explorerPanel;
private GamePanel gamePanel;
private final LinkedList<JComponent> contents;
private final LinkedList<JComponent> contents;
private boolean dontShowAgain = true;
public MagicFrame() {
this.explorerPanel = null;
@ -113,28 +116,32 @@ public class MagicFrame extends JFrame implements ActionListener {
contents=new LinkedList<JComponent>();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(final WindowEvent event) {
final boolean maximized=(MagicFrame.this.getExtendedState()&JFrame.MAXIMIZED_BOTH)==JFrame.MAXIMIZED_BOTH;
if (maximized) {
config.setMaximized(true);
} else {
config.setLeft(getX());
config.setTop(getY());
config.setWidth(getWidth());
config.setHeight(getHeight());
config.setMaximized(false);
if (config.isConfirmExit()) {
// show confirmation dialog
final JCheckBox checkbox = new JCheckBox("Do not show this message again.");
checkbox.setFont(new Font("dialog", Font.PLAIN, 11));
final String message = "Are you sure you want to quit Magarena?\n\n\n";
final Object[] params = {message, checkbox};
final int n = JOptionPane.showConfirmDialog(
contentPanel,
params,
"Confirm Exit",
JOptionPane.YES_NO_OPTION);
dontShowAgain = checkbox.isSelected();
if (n == JOptionPane.YES_OPTION) {
exit();
} else if (n == JOptionPane.NO_OPTION) {
config.setConfirmExit(!dontShowAgain);
config.save();
}
} else {
exit();
}
config.save();
/*
if (gamePanel != null) {
gamePanel.getController().haltGame();
}
*/
}
});
@ -580,7 +587,29 @@ public class MagicFrame extends JFrame implements ActionListener {
}
}
}
private void exit() {
final boolean maximized =
(MagicFrame.this.getExtendedState() & JFrame.MAXIMIZED_BOTH) == JFrame.MAXIMIZED_BOTH;
if (maximized) {
config.setMaximized(true);
} else {
config.setLeft(getX());
config.setTop(getY());
config.setWidth(getWidth());
config.setHeight(getHeight());
config.setMaximized(false);
}
config.setConfirmExit(!dontShowAgain);
config.save();
/*
if (gamePanel != null) {
gamePanel.getController().haltGame();
}
*/
System.exit(0);
}
private void openKeywords() {
enableMenuItem(KEYWORDS_ITEM,false);

View File

@ -29,6 +29,7 @@ public class PreferencesDialog extends JDialog implements ActionListener {
private final JComboBox themeComboBox;
private final JComboBox avatarComboBox;
private final JComboBox highlightComboBox;
private final JCheckBox confirmExitCheckBox;
private final JCheckBox soundCheckBox;
private final JCheckBox highQualityCheckBox;
private final JCheckBox skipSingleCheckBox;
@ -43,7 +44,7 @@ public class PreferencesDialog extends JDialog implements ActionListener {
super(frame,true);
this.setTitle("Preferences");
this.setSize(400,465);
this.setSize(400,500);
this.setLocationRelativeTo(frame);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
@ -111,7 +112,13 @@ public class PreferencesDialog extends JDialog implements ActionListener {
highlightComboBox.setSelectedItem(config.getHighlight());
mainPanel.add(highlightComboBox);
Y += 35;
Y += 35;
confirmExitCheckBox=new JCheckBox("Show confirmation dialog on exit",config.isConfirmExit());
confirmExitCheckBox.setBounds(X3,Y,W3,H3);
confirmExitCheckBox.setFocusable(false);
mainPanel.add(confirmExitCheckBox);
Y += 30;
soundCheckBox=new JCheckBox("Enable sound effects",config.isSound());
soundCheckBox.setBounds(X3,Y,W3,H3);
soundCheckBox.setFocusable(false);
@ -167,6 +174,7 @@ public class PreferencesDialog extends JDialog implements ActionListener {
config.setTheme((String)themeComboBox.getSelectedItem());
config.setAvatar((String)avatarComboBox.getSelectedItem());
config.setHighlight((String)highlightComboBox.getSelectedItem());
config.setConfirmExit(confirmExitCheckBox.isSelected());
config.setSound(soundCheckBox.isSelected());
config.setHighQuality(highQualityCheckBox.isSelected());
config.setSkipSingle(skipSingleCheckBox.isSelected());