1 56 package org.objectstyle.cayenne.modeler.dialog; 57 58 import java.awt.Component ; 59 60 import javax.swing.JDialog ; 61 import javax.swing.JOptionPane ; 62 63 66 public class UnsavedChangesDialog { 67 68 private static final String SAVE_AND_CLOSE = "Save Changes"; 69 private static final String CLOSE_WITHOUT_SAVE = "Ignore Changes"; 70 private static final String CANCEL = "Cancel"; 71 72 private static final String [] OPTIONS = new String [] { 73 SAVE_AND_CLOSE, CLOSE_WITHOUT_SAVE, CANCEL 74 }; 75 76 protected Component parent; 77 protected String result = CANCEL; 78 79 public UnsavedChangesDialog(Component parent) { 80 this.parent = parent; 81 } 82 83 public void show() { 84 JOptionPane pane = new JOptionPane ( 85 "You have unsaved changes. Do you want to save them?", 86 JOptionPane.QUESTION_MESSAGE); 87 pane.setOptions(OPTIONS); 88 89 JDialog dialog = pane.createDialog(parent, "Unsaved Changes"); 90 dialog.setVisible(true); 91 92 Object selectedValue = pane.getValue(); 93 if (SAVE_AND_CLOSE.equals(selectedValue)) { 96 result = SAVE_AND_CLOSE; 97 } 98 else if (CLOSE_WITHOUT_SAVE.equals(selectedValue)) { 99 result = CLOSE_WITHOUT_SAVE; 100 } 101 else { 102 result = CANCEL; 103 } 104 } 105 106 public boolean shouldSave() { 107 return SAVE_AND_CLOSE.equals(result); 108 } 109 110 public boolean shouldNotSave() { 111 return CLOSE_WITHOUT_SAVE.equals(result); 112 } 113 114 public boolean shouldCancel() { 115 return result == null || CANCEL.equals(result); 116 } 117 } 118 119 | Popular Tags |