1 6 package org.oddjob.designer.view; 7 8 import java.awt.BorderLayout ; 9 import java.awt.Component ; 10 import java.awt.Dialog ; 11 import java.awt.Frame ; 12 import java.awt.Window ; 13 import java.awt.event.ActionEvent ; 14 import java.awt.event.ActionListener ; 15 16 import javax.swing.Action ; 17 import javax.swing.BorderFactory ; 18 import javax.swing.Box ; 19 import javax.swing.BoxLayout ; 20 import javax.swing.ButtonGroup ; 21 import javax.swing.JButton ; 22 import javax.swing.JDialog ; 23 import javax.swing.JOptionPane ; 24 import javax.swing.JPanel ; 25 import javax.swing.JRadioButton ; 26 27 30 public class RadioActions extends JPanel { 31 32 private Action selected; 33 private boolean result; 34 35 public RadioActions(final Action [] actions) { 36 setLayout(new BoxLayout (this, BoxLayout.PAGE_AXIS)); 37 ButtonGroup group = new ButtonGroup (); 38 39 class ButtonAction implements ActionListener { 40 int i; 41 ButtonAction(int i) { 42 this.i = i; 43 } 44 public void actionPerformed(ActionEvent e) { 45 selected = actions[i]; 46 } 47 48 }; 49 50 for (int i = 0; i < actions.length; ++i) { 51 JRadioButton button = new JRadioButton ((String ) actions[i].getValue(Action.NAME)); 52 button.addActionListener(new ButtonAction(i)); 53 group.add(button); 54 add(button); 55 if (i == 0) { 56 button.setSelected(true); 57 selected = actions[i]; 58 } 59 } 60 } 61 62 public Action getSelected() { 63 return selected; 64 } 65 66 public static boolean showDialog(final Component parent, Action [] actions) { 67 Window w = ViewHelper.getWindowForComponent(parent); 68 69 final JDialog dialog; 70 if (w instanceof Frame ) { 71 dialog = new JDialog ((Frame ) w); 72 } else { 73 dialog = new JDialog ((Dialog ) w); 74 } 75 76 final RadioActions radioActions = new RadioActions(actions); 77 78 JButton okButton = new JButton ("OK"); 79 okButton.addActionListener(new ActionListener () { 80 public void actionPerformed(ActionEvent e) { 81 dialog.setVisible(false); 82 dialog.dispose(); 83 Action action = radioActions.getSelected(); 84 try { 85 action.actionPerformed( 86 new ActionEvent (this, 0, null)); 87 radioActions.result = true; 88 } catch (Exception ex) { 89 JOptionPane.showMessageDialog(parent, ex, "Error!", 90 JOptionPane.ERROR_MESSAGE); 91 radioActions.result = false; 92 } 93 } 94 }); 95 96 JButton cancelButton = new JButton ("Cancel"); 97 cancelButton.addActionListener(new ActionListener () { 98 public void actionPerformed(ActionEvent e) { 99 dialog.setVisible(false); 100 dialog.dispose(); 101 radioActions.result = false; 102 } 103 }); 104 105 JPanel closePanel = new JPanel (); 106 closePanel.setLayout(new BoxLayout (closePanel, 107 BoxLayout.LINE_AXIS)); 108 closePanel.add(Box.createHorizontalGlue()); 109 closePanel.add(okButton); 110 closePanel.add(Box.createHorizontalStrut(5)); 111 closePanel.add(cancelButton); 112 closePanel.setBorder(BorderFactory. 113 createEmptyBorder(5, 5, 5, 5)); 114 radioActions.setBorder(BorderFactory. 115 createEmptyBorder(5, 5, 5, 5)); 116 117 JPanel contentPane = new JPanel (new BorderLayout ()); 118 contentPane.add(radioActions, BorderLayout.CENTER); 119 contentPane.add(closePanel, BorderLayout.PAGE_END); 120 contentPane.setOpaque(true); 121 dialog.setContentPane(contentPane); 122 123 dialog.setLocationRelativeTo(parent); 125 dialog.setModal(true); 126 dialog.pack(); 127 okButton.requestFocusInWindow(); 128 dialog.setVisible(true); 129 return radioActions.result; 130 } 131 132 } 133 | Popular Tags |