1 21 22 package org.armedbear.j; 23 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.KeyEvent ; 26 import javax.swing.JPanel ; 27 28 public class ConfirmDialog extends MessageDialog implements Constants 29 { 30 private StandardButton yesButton; 31 private StandardButton noButton; 32 private StandardButton yesToAllButton; 33 private StandardButton cancelButton; 34 35 private int result = RESPONSE_NO; 36 37 private boolean confirmAll; 38 protected boolean cancel; 39 40 protected ConfirmDialog(Editor editor) 41 { 42 super(editor != null ? editor : Editor.currentEditor()); 43 } 44 45 public static int showConfirmDialog(String text, String title) 46 { 47 return showConfirmDialog(Editor.currentEditor(), text, title); 48 } 49 50 public static int showConfirmDialog(Editor editor, String text, 51 String title) 52 { 53 ConfirmDialog d = new ConfirmDialog(editor); 54 d.initialize(text, title); 55 if (editor != null) 56 editor.setDefaultCursor(); 57 d.show(); 58 if (editor != null && Editor.getEditorList().contains(editor)) 59 editor.setFocusToDisplay(); 60 return d.result; 61 } 62 63 public static int showConfirmAllDialog(Editor editor, String text, 64 String title) 65 { 66 ConfirmDialog d = new ConfirmDialog(editor); 67 d.confirmAll = true; 68 d.initialize(text, title); 69 if (editor != null) 70 editor.setDefaultCursor(); 71 d.show(); 72 if (editor != null && Editor.getEditorList().contains(editor)) 73 editor.setFocusToDisplay(); 74 return d.result; 75 } 76 77 public static int showConfirmDialogWithCancelButton(Editor editor, 78 String text, 79 String title) 80 { 81 ConfirmDialog d = new ConfirmDialog(editor); 82 d.cancel = true; 83 d.initialize(text, title); 84 if (editor != null) 85 editor.setDefaultCursor(); 86 d.show(); 87 if (editor != null && Editor.getEditorList().contains(editor)) 88 editor.setFocusToDisplay(); 89 return d.result; 90 } 91 92 protected void initialize(String text, String title) 93 { 94 super.initialize(text, title); 95 yesButton.requestFocus(); 96 } 97 98 protected void addButtons() 99 { 100 JPanel buttonPanel = new JPanel (); 101 buttonPanel.setAlignmentX(LEFT_ALIGNMENT); 102 mainPanel.add(buttonPanel); 103 yesButton = new StandardButton("Yes"); 104 yesButton.setActionCommand("Yes"); 105 yesButton.setMnemonic('Y'); 106 yesButton.addActionListener(this); 107 yesButton.addKeyListener(this); 108 buttonPanel.add(yesButton); 109 noButton = new StandardButton("No"); 110 noButton.setActionCommand("No"); 111 noButton.setMnemonic('N'); 112 noButton.addActionListener(this); 113 noButton.addKeyListener(this); 114 buttonPanel.add(noButton); 115 if (confirmAll) { 116 yesToAllButton = new StandardButton("Yes To All"); 117 yesToAllButton.setActionCommand("Yes To All"); 118 yesToAllButton.setMnemonic('A'); 119 yesToAllButton.addActionListener(this); 120 yesToAllButton.addKeyListener(this); 121 buttonPanel.add(yesToAllButton); 122 } 123 if (confirmAll || cancel) { 124 cancelButton = new StandardButton("Cancel"); 125 cancelButton.setActionCommand("Cancel"); 126 cancelButton.setMnemonic('C'); 127 cancelButton.addActionListener(this); 128 cancelButton.addKeyListener(this); 129 buttonPanel.add(cancelButton); 130 } 131 } 132 133 protected void yes() 134 { 135 result = RESPONSE_YES; 136 dispose(); 137 } 138 139 protected void no() 140 { 141 result = RESPONSE_NO; 142 dispose(); 143 } 144 145 protected void yesToAll() 146 { 147 result = RESPONSE_YES_TO_ALL; 148 dispose(); 149 } 150 151 protected void cancel() 152 { 153 result = RESPONSE_CANCEL; 154 dispose(); 155 } 156 157 public void actionPerformed(ActionEvent e) 158 { 159 if (e.getActionCommand().equals("Yes")) 160 yes(); 161 else if (e.getActionCommand().equals("No")) 162 no(); 163 else if (e.getActionCommand().equals("Yes To All")) 164 yesToAll(); 165 else if (e.getActionCommand().equals("Cancel")) 166 cancel(); 167 } 168 169 public void keyPressed(KeyEvent e) 170 { 171 int keyCode = e.getKeyCode(); 172 if (confirmAll) { 173 switch (keyCode) { 174 case KeyEvent.VK_Y: 175 yes(); 176 break; 177 case KeyEvent.VK_N: 178 no(); 179 break; 180 case KeyEvent.VK_A: 181 yesToAll(); 182 break; 183 case KeyEvent.VK_C: 184 cancel(); 185 break; 186 } 187 } else if (cancel) { 188 switch (keyCode) { 189 case KeyEvent.VK_Y: 190 case KeyEvent.VK_ENTER: 191 yes(); 192 break; 193 case KeyEvent.VK_N: 194 no(); 195 break; 196 case KeyEvent.VK_ESCAPE: 197 cancel(); 198 break; 199 } 200 } else { 201 switch (keyCode) { 202 case KeyEvent.VK_Y: 203 case KeyEvent.VK_ENTER: 204 yes(); 205 break; 206 case KeyEvent.VK_N: 207 case KeyEvent.VK_ESCAPE: 208 no(); 209 break; 210 } 211 } 212 } 213 } 214 | Popular Tags |