1 package com.ca.commons.cbutil; 2 3 import javax.swing.*; 4 import java.awt.*; 5 import java.awt.event.*; 6 7 8 26 27 public class CBDialog extends JDialog 28 { 29 boolean cancelled = false; 30 31 public CBButton OK, Cancel, Help; 33 37 38 public CBPanel display; 39 40 43 44 protected Frame owner; 45 46 49 50 protected JPanel buttonPanel; 51 52 public CBDialog(Frame owner, String title, String helpLink) 53 { 54 super(owner); 56 this.owner = owner; 57 58 setModal(true); 59 60 setTitle(title); 61 62 display = new CBPanel(); 63 64 Container pane = getContentPane(); 65 66 pane.setLayout(new BorderLayout()); 67 68 buttonPanel = new JPanel(); 69 buttonPanel.add(OK = new CBButton(CBIntText.get("OK"), CBIntText.get("Click here to make the changes."))); 70 buttonPanel.add(Cancel = new CBButton(CBIntText.get("Cancel"), CBIntText.get("Click here to exit."))); 71 72 setHelpLink(helpLink); 73 74 pane.add(display); 75 pane.add(buttonPanel, BorderLayout.SOUTH); 76 77 display.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ENTER"), "enter"); 79 display.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"), "escape"); 80 display.getActionMap().put("enter", new MyAction(CBAction.ENTER)); 81 display.getActionMap().put("escape", new MyAction(CBAction.ESCAPE)); 82 83 Cancel.addActionListener(new ActionListener() 84 { 85 public void actionPerformed(ActionEvent e) 86 { 87 doCancel(); 88 } 89 }); 90 91 OK.addActionListener(new ActionListener() 92 { 93 public void actionPerformed(ActionEvent e) 94 { 95 doOK(); 96 } 97 }); 98 99 addWindowListener(new WindowAdapter() 100 { 101 public void windowClosing(WindowEvent e) 102 { 103 doCancel(); 104 } 105 }); 106 } 107 108 120 private class MyAction extends CBAction 121 { 122 127 public MyAction(int key) 128 { 129 super(key); 130 } 131 132 138 public void actionPerformed(ActionEvent e) 139 { 140 if (getKey() == ESCAPE) 141 doCancel(); 142 else if (getKey() == ENTER) 143 OK.doClick(); 144 } 145 } 146 147 150 public void setHelpLink(String helpLink) 151 { 152 if (helpLink != null) 153 { 154 buttonPanel.add(Help = new CBButton(CBIntText.get("Help"), CBIntText.get("Click here for Help."))); 155 CBHelpSystem.useDefaultHelp(Help, helpLink); 156 } 157 } 158 159 162 public CBPanel getDisplayPanel() 163 { 164 return display; 165 } 166 167 171 172 public Component add(Component comp) 173 { 174 display.add(comp); 175 return comp; 176 } 177 178 182 183 public void addln(Component comp) 184 { 185 display.addln(comp); 186 } 187 188 192 193 public void makeHeavy() 194 { 195 display.makeHeavy(); 196 } 197 198 202 203 public void makeLight() 204 { 205 display.makeLight(); 206 } 207 208 212 213 public void makeWide() 214 { 215 display.makeWide(); 216 } 217 218 219 222 223 public void doCancel() 224 { 225 cancelled = true; 226 quit(); 227 } 228 229 234 235 public boolean wasCancelled() 236 { 237 return cancelled; 238 } 239 240 244 245 public void doOK() 246 { 247 quit(); 248 } 249 250 254 255 public void quit() 256 { 257 setVisible(false); 258 dispose(); 259 } 260 } | Popular Tags |