KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > designer > view > RadioActions


1 /*
2  *
3  *
4  * (c) Rob Gordon 2005
5  */

6 package org.oddjob.designer.view;
7
8 import java.awt.BorderLayout JavaDoc;
9 import java.awt.Component JavaDoc;
10 import java.awt.Dialog JavaDoc;
11 import java.awt.Frame JavaDoc;
12 import java.awt.Window JavaDoc;
13 import java.awt.event.ActionEvent JavaDoc;
14 import java.awt.event.ActionListener JavaDoc;
15
16 import javax.swing.Action JavaDoc;
17 import javax.swing.BorderFactory JavaDoc;
18 import javax.swing.Box JavaDoc;
19 import javax.swing.BoxLayout JavaDoc;
20 import javax.swing.ButtonGroup JavaDoc;
21 import javax.swing.JButton JavaDoc;
22 import javax.swing.JDialog JavaDoc;
23 import javax.swing.JOptionPane JavaDoc;
24 import javax.swing.JPanel JavaDoc;
25 import javax.swing.JRadioButton JavaDoc;
26
27 /**
28  *
29  */

30 public class RadioActions extends JPanel JavaDoc {
31
32     private Action JavaDoc selected;
33     private boolean result;
34     
35     public RadioActions(final Action JavaDoc[] actions) {
36         setLayout(new BoxLayout JavaDoc(this, BoxLayout.PAGE_AXIS));
37         ButtonGroup JavaDoc group = new ButtonGroup JavaDoc();
38         
39         class ButtonAction implements ActionListener JavaDoc {
40             int i;
41             ButtonAction(int i) {
42                 this.i = i;
43             }
44             public void actionPerformed(ActionEvent JavaDoc e) {
45                 selected = actions[i];
46             }
47             
48         };
49         
50         for (int i = 0; i < actions.length; ++i) {
51             JRadioButton JavaDoc button = new JRadioButton JavaDoc((String JavaDoc) 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 JavaDoc getSelected() {
63         return selected;
64     }
65     
66     public static boolean showDialog(final Component JavaDoc parent, Action JavaDoc[] actions) {
67         Window JavaDoc w = ViewHelper.getWindowForComponent(parent);
68
69         final JDialog JavaDoc dialog;
70         if (w instanceof Frame JavaDoc) {
71             dialog = new JDialog JavaDoc((Frame JavaDoc) w);
72         } else {
73             dialog = new JDialog JavaDoc((Dialog JavaDoc) w);
74         }
75                 
76         final RadioActions radioActions = new RadioActions(actions);
77         
78         JButton JavaDoc okButton = new JButton JavaDoc("OK");
79         okButton.addActionListener(new ActionListener JavaDoc() {
80             public void actionPerformed(ActionEvent JavaDoc e) {
81                 dialog.setVisible(false);
82                 dialog.dispose();
83                 Action JavaDoc action = radioActions.getSelected();
84                 try {
85                     action.actionPerformed(
86                             new ActionEvent JavaDoc(this, 0, null));
87                     radioActions.result = true;
88                 } catch (Exception JavaDoc ex) {
89                     JOptionPane.showMessageDialog(parent, ex, "Error!",
90                             JOptionPane.ERROR_MESSAGE);
91                     radioActions.result = false;
92                 }
93             }
94         });
95         
96         JButton JavaDoc cancelButton = new JButton JavaDoc("Cancel");
97         cancelButton.addActionListener(new ActionListener JavaDoc() {
98             public void actionPerformed(ActionEvent JavaDoc e) {
99                 dialog.setVisible(false);
100                 dialog.dispose();
101                 radioActions.result = false;
102             }
103         });
104         
105         JPanel JavaDoc closePanel = new JPanel JavaDoc();
106         closePanel.setLayout(new BoxLayout JavaDoc(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 JavaDoc contentPane = new JPanel JavaDoc(new BorderLayout JavaDoc());
118         contentPane.add(radioActions, BorderLayout.CENTER);
119         contentPane.add(closePanel, BorderLayout.PAGE_END);
120         contentPane.setOpaque(true);
121         dialog.setContentPane(contentPane);
122
123         //Show it.
124
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