001: package jimm.datavision.gui;
002:
003: import jimm.datavision.*;
004: import jimm.datavision.gui.cmd.NewGroupCommand;
005: import jimm.util.I18N;
006: import java.awt.*;
007: import java.awt.event.*;
008: import java.util.Iterator;
009: import javax.swing.*;
010:
011: /**
012: * A dialog for creating a new group.
013: * <p>
014: * This dialog should only be created if the report has at least one field.
015: * The method {@link Designer#enableMenuItems} makes sure this is true.
016: *
017: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
018: */
019: class NewGroupWin extends JDialog implements ActionListener {
020:
021: protected Designer designer;
022: protected Report report;
023: protected JComboBox combo;
024: protected JRadioButton ascendingButton, descendingButton;
025:
026: /**
027: * Constructor; uses format of first selected field.
028: *
029: * @param designer the window to which this dialog belongs
030: * @param report the report
031: */
032: public NewGroupWin(Designer designer, Report report) {
033: super (designer.getFrame(), I18N.get("NewGroupWin.title"));
034: this .designer = designer;
035: this .report = report;
036: buildWindow();
037: pack();
038: setVisible(true);
039: }
040:
041: /**
042: * Builds the window contents.
043: */
044: protected void buildWindow() {
045: JPanel gutsPanel = buildGuts();
046: JPanel buttonPanel = buildButtonPanel();
047:
048: getContentPane().add(gutsPanel, BorderLayout.CENTER);
049: getContentPane().add(buttonPanel, BorderLayout.SOUTH);
050: }
051:
052: /**
053: * Builds and returns a panel containing the stuff from which groups are
054: * made.
055: *
056: * @return a panel
057: */
058: protected JPanel buildGuts() {
059: GridBagLayout bag = new GridBagLayout();
060: GridBagConstraints c = new GridBagConstraints();
061: c.insets = new Insets(6, 6, 6, 6);
062:
063: JPanel panel = new JPanel();
064: panel.setLayout(bag);
065: panel
066: .setBorder(BorderFactory.createEmptyBorder(20, 20, 20,
067: 20));
068:
069: // "Group Column" label
070: JLabel label;
071: label = new JLabel(I18N.get("NewGroupWin.group_column"));
072: c.gridx = 0;
073: c.gridy = 0;
074: c.anchor = GridBagConstraints.EAST;
075: bag.setConstraints(label, c);
076: panel.add(label);
077:
078: // Group column dropdown
079: JPanel comboPanel = buildColumnComboBox();
080: c.gridx = 1;
081: c.gridy = 0;
082: c.anchor = GridBagConstraints.NORTHWEST;
083: bag.setConstraints(comboPanel, c);
084: panel.add(comboPanel);
085:
086: // "Sort Order" label
087: label = new JLabel(I18N.get("NewGroupWin.sort_order"));
088: c.gridx = 0;
089: c.gridy = 1;
090: c.anchor = GridBagConstraints.EAST;
091: bag.setConstraints(label, c);
092: panel.add(label);
093:
094: // Sort order radion buttons
095: Box rbBox = buildSortOrderRadioButtons();
096: c.gridx = 1;
097: c.gridy = 1;
098: c.anchor = GridBagConstraints.NORTHWEST;
099: c.gridheight = 2;
100: bag.setConstraints(rbBox, c);
101: panel.add(rbBox);
102:
103: return panel;
104: }
105:
106: protected JPanel buildColumnComboBox() {
107: DefaultComboBoxModel model = new DefaultComboBoxModel();
108:
109: // Iterate through columns in tables used by report. We will remove
110: // those associated with extant groups in a moment.
111: Iterator iter;
112: for (iter = report.userColumns(); iter.hasNext();)
113: model.addElement((Selectable) iter.next());
114: for (iter = report.getDataSource()
115: .columnsInTablesUsedInReport(); iter.hasNext();)
116: model.addElement((Selectable) iter.next());
117:
118: // Remove all user columns and columns already in a group.
119: for (iter = report.groups(); iter.hasNext();) {
120: Group group = (Group) iter.next();
121: model.removeElement(group.getSelectable());
122: }
123:
124: combo = new JComboBox(model);
125: combo.setSelectedIndex(0);
126:
127: JPanel panel = new JPanel();
128: panel.add(combo);
129: return panel;
130: }
131:
132: protected Box buildSortOrderRadioButtons() {
133: Box box = Box.createVerticalBox();
134:
135: ButtonGroup bg = new ButtonGroup();
136:
137: ascendingButton = new JRadioButton(I18N.get("GUI.ascending"));
138: ascendingButton.addActionListener(this );
139: box.add(ascendingButton);
140: bg.add(ascendingButton);
141:
142: descendingButton = new JRadioButton(I18N.get("GUI.descending"));
143: descendingButton.addActionListener(this );
144: box.add(descendingButton);
145: bg.add(descendingButton);
146:
147: ascendingButton.setSelected(true);
148: return box;
149: }
150:
151: /**
152: * Builds and returns a panel containing the OK and Cancel
153: *
154: * @return a panel
155: */
156: protected JPanel buildButtonPanel() {
157: JPanel buttonPanel = new JPanel();
158: JButton button;
159:
160: buttonPanel.add(button = new JButton(I18N.get("GUI.ok")));
161: button.addActionListener(this );
162: button.setDefaultCapable(true);
163:
164: buttonPanel.add(button = new JButton(I18N.get("GUI.cancel")));
165: button.addActionListener(this );
166:
167: return buttonPanel;
168: }
169:
170: /**
171: * Handles the OK and Cancel buttons.
172: *
173: * @param e action event
174: */
175: public void actionPerformed(ActionEvent e) {
176: String cmd = e.getActionCommand();
177: if (I18N.get("GUI.ok").equals(cmd)) {
178: int sortOrder = ascendingButton.isSelected() ? Group.SORT_ASCENDING
179: : Group.SORT_DESCENDING;
180: NewGroupCommand ngc = new NewGroupCommand(designer, report,
181: (Selectable) combo.getSelectedItem(), sortOrder);
182: designer.performCommand(ngc);
183: dispose();
184: } else if (I18N.get("GUI.cancel").equals(cmd)) {
185: dispose();
186: }
187: }
188:
189: }
|