KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > gui > NewGroupWin


1 package jimm.datavision.gui;
2 import jimm.datavision.*;
3 import jimm.datavision.gui.cmd.NewGroupCommand;
4 import jimm.util.I18N;
5 import java.awt.*;
6 import java.awt.event.*;
7 import java.util.Iterator JavaDoc;
8 import javax.swing.*;
9
10 /**
11  * A dialog for creating a new group.
12  * <p>
13  * This dialog should only be created if the report has at least one field.
14  * The method {@link Designer#enableMenuItems} makes sure this is true.
15  *
16  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
17  */

18 class NewGroupWin extends JDialog implements ActionListener {
19
20 protected Designer designer;
21 protected Report report;
22 protected JComboBox combo;
23 protected JRadioButton ascendingButton, descendingButton;
24
25 /**
26  * Constructor; uses format of first selected field.
27  *
28  * @param designer the window to which this dialog belongs
29  * @param report the report
30  */

31 public NewGroupWin(Designer designer, Report report) {
32     super(designer.getFrame(), I18N.get("NewGroupWin.title"));
33     this.designer = designer;
34     this.report = report;
35     buildWindow();
36     pack();
37     setVisible(true);
38 }
39
40 /**
41  * Builds the window contents.
42  */

43 protected void buildWindow() {
44     JPanel gutsPanel = buildGuts();
45     JPanel buttonPanel = buildButtonPanel();
46
47     getContentPane().add(gutsPanel, BorderLayout.CENTER);
48     getContentPane().add(buttonPanel, BorderLayout.SOUTH);
49 }
50
51 /**
52  * Builds and returns a panel containing the stuff from which groups are
53  * made.
54  *
55  * @return a panel
56  */

57 protected JPanel buildGuts() {
58     GridBagLayout bag = new GridBagLayout();
59     GridBagConstraints c = new GridBagConstraints();
60     c.insets = new Insets(6, 6, 6, 6);
61
62     JPanel panel = new JPanel();
63     panel.setLayout(bag);
64     panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
65
66     // "Group Column" label
67
JLabel label;
68     label = new JLabel(I18N.get("NewGroupWin.group_column"));
69     c.gridx = 0;
70     c.gridy = 0;
71     c.anchor = GridBagConstraints.EAST;
72     bag.setConstraints(label, c);
73     panel.add(label);
74
75     // Group column dropdown
76
JPanel comboPanel = buildColumnComboBox();
77     c.gridx = 1;
78     c.gridy = 0;
79     c.anchor = GridBagConstraints.NORTHWEST;
80     bag.setConstraints(comboPanel, c);
81     panel.add(comboPanel);
82
83     // "Sort Order" label
84
label = new JLabel(I18N.get("NewGroupWin.sort_order"));
85     c.gridx = 0;
86     c.gridy = 1;
87     c.anchor = GridBagConstraints.EAST;
88     bag.setConstraints(label, c);
89     panel.add(label);
90
91     // Sort order radion buttons
92
Box rbBox = buildSortOrderRadioButtons();
93     c.gridx = 1;
94     c.gridy = 1;
95     c.anchor = GridBagConstraints.NORTHWEST;
96     c.gridheight = 2;
97     bag.setConstraints(rbBox, c);
98     panel.add(rbBox);
99
100     return panel;
101 }
102
103 protected JPanel buildColumnComboBox() {
104     DefaultComboBoxModel model = new DefaultComboBoxModel();
105
106     // Iterate through columns in tables used by report. We will remove
107
// those associated with extant groups in a moment.
108
Iterator JavaDoc iter;
109     for (iter = report.userColumns(); iter.hasNext(); )
110     model.addElement((Selectable)iter.next());
111     for (iter = report.getDataSource().columnsInTablesUsedInReport();
112      iter.hasNext(); )
113     model.addElement((Selectable)iter.next());
114
115     // Remove all user columns and columns already in a group.
116
for (iter = report.groups(); iter.hasNext(); ) {
117     Group group = (Group)iter.next();
118     model.removeElement(group.getSelectable());
119     }
120
121     combo = new JComboBox(model);
122     combo.setSelectedIndex(0);
123
124     JPanel panel = new JPanel();
125     panel.add(combo);
126     return panel;
127 }
128
129 protected Box buildSortOrderRadioButtons() {
130     Box box = Box.createVerticalBox();
131
132     ButtonGroup bg = new ButtonGroup();
133
134     ascendingButton = new JRadioButton(I18N.get("GUI.ascending"));
135     ascendingButton.addActionListener(this);
136     box.add(ascendingButton);
137     bg.add(ascendingButton);
138
139     descendingButton = new JRadioButton(I18N.get("GUI.descending"));
140     descendingButton.addActionListener(this);
141     box.add(descendingButton);
142     bg.add(descendingButton);
143
144     ascendingButton.setSelected(true);
145     return box;
146 }
147
148 /**
149  * Builds and returns a panel containing the OK and Cancel
150  *
151  * @return a panel
152  */

153 protected JPanel buildButtonPanel() {
154     JPanel buttonPanel = new JPanel();
155     JButton button;
156
157     buttonPanel.add(button = new JButton(I18N.get("GUI.ok")));
158     button.addActionListener(this);
159     button.setDefaultCapable(true);
160
161     buttonPanel.add(button = new JButton(I18N.get("GUI.cancel")));
162     button.addActionListener(this);
163
164     return buttonPanel;
165 }
166
167 /**
168  * Handles the OK and Cancel buttons.
169  *
170  * @param e action event
171  */

172 public void actionPerformed(ActionEvent e) {
173     String JavaDoc cmd = e.getActionCommand();
174     if (I18N.get("GUI.ok").equals(cmd)) {
175     int sortOrder = ascendingButton.isSelected()
176         ? Group.SORT_ASCENDING : Group.SORT_DESCENDING;
177     NewGroupCommand ngc =
178         new NewGroupCommand(designer, report,
179                 (Selectable)combo.getSelectedItem(),
180                 sortOrder);
181     designer.performCommand(ngc);
182     dispose();
183     }
184     else if (I18N.get("GUI.cancel").equals(cmd)) {
185     dispose();
186     }
187 }
188
189 }
190
Popular Tags