001: package jimm.datavision.gui.sql;
002:
003: import jimm.datavision.*;
004: import jimm.datavision.gui.Designer;
005: import jimm.datavision.gui.EditWin;
006: import jimm.datavision.source.*;
007: import jimm.datavision.gui.cmd.TableJoinCommand;
008: import jimm.util.I18N;
009: import java.awt.BorderLayout;
010: import java.awt.event.ActionListener;
011: import java.awt.event.ActionEvent;
012: import java.util.*;
013: import javax.swing.*;
014:
015: /**
016: * The dialog used for defining joins between database tables. Eventually,
017: * this will be nice and graphical.
018: * <p>
019: * This dialog should only be created if the report uses more than one table.
020: * The method {@link Designer#enableMenuItems} makes sure this is true.
021: *
022: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
023: */
024: public class VisTableWin extends EditWin implements ActionListener {
025:
026: /* ================================================================ */
027: static class RevertInfo {
028: ArrayList joins;
029:
030: RevertInfo(Query q) {
031: joins = new ArrayList();
032: for (Iterator iter = q.joins(); iter.hasNext();) {
033: Join j = (Join) iter.next();
034: joins.add(j.clone());
035: }
036: }
037: }
038:
039: /* ================================================================ */
040: static class JoinFields {
041:
042: Join join; // If null, it's a newly created join
043: JCheckBox del;
044: JComboBox from;
045: JComboBox relation;
046: JComboBox to;
047:
048: JoinFields(Join j, DataSource db) {
049: join = j;
050: del = new JCheckBox();
051: from = buildColDropdown((j == null) ? null : j.getFrom(),
052: db);
053: relation = buildRelDropdown((j == null) ? null : j
054: .getRelation());
055: to = buildColDropdown((j == null) ? null : j.getTo(), db);
056: }
057:
058: protected JComboBox buildColDropdown(Column col, DataSource db) {
059: JComboBox cb = new JComboBox();
060:
061: // Get iterator over tables actually used by the report. Will be
062: // null if the data source does not have tables.
063: for (Iterator iter = db.columnsInTablesUsedInReport(); iter
064: .hasNext();) {
065: Column c = (Column) iter.next();
066: cb.addItem(c.fullName());
067: if (col != null && col.equals(c))
068: cb.setSelectedItem(c.fullName());
069: }
070:
071: return cb;
072: }
073:
074: protected JComboBox buildRelDropdown(String rel) {
075: JComboBox cb = new JComboBox(Join.RELATIONS);
076: if (rel != null)
077: cb.setSelectedItem(rel);
078: return cb;
079: }
080: }
081:
082: /* ================================================================ */
083:
084: protected Report report;
085: protected Query query;
086: protected ArrayList joinFieldsList;
087: protected JPanel joinsPanel;
088: protected Box delCheckBoxPanel;
089: protected Box fromPanel;
090: protected Box relationPanel;
091: protected Box toPanel;
092: protected JButton deleteButton;
093:
094: /**
095: * Constructor.
096: *
097: * @param designer the window to which this dialog belongs
098: * @param report the...um...I forgot
099: */
100: public VisTableWin(Designer designer, Report report) {
101: this (designer, report, report.getDataSource().getQuery());
102: }
103:
104: /**
105: * Constructor.
106: *
107: * @param designer the window to which this dialog belongs
108: * @param report the...um...I forgot
109: * @param query a query
110: */
111: public VisTableWin(Designer designer, Report report, Query query) {
112: super (designer, I18N.get("VisTableWin.title"),
113: "TableJoinCommand.name");
114: this .report = report;
115: this .query = query;
116:
117: buildWindow();
118: pack();
119: setVisible(true);
120: }
121:
122: /**
123: * Builds the window contents.
124: */
125: protected void buildWindow() {
126: // Joins
127: buildJoinsPanel();
128:
129: // Add/Delete buttons
130: JPanel addDelButtons = new JPanel();
131: JButton button = new JButton(I18N.get("VisTableWin.add"));
132: button.addActionListener(this );
133: addDelButtons.add(button);
134:
135: deleteButton = new JButton(I18N
136: .get("VisTableWin.delete_selected"));
137: deleteButton.addActionListener(this );
138: addDelButtons.add(deleteButton);
139:
140: // Panel containing joins and add/delete buttons
141: JPanel centerPanel = new JPanel();
142: centerPanel.setLayout(new BorderLayout());
143: centerPanel.add(joinsPanel, BorderLayout.CENTER);
144: centerPanel.add(addDelButtons, BorderLayout.SOUTH);
145:
146: // OK, Apply, Revert, and Cancel Buttons
147: JPanel buttonPanel = closeButtonPanel();
148:
149: // Add tables and buttons to window
150: getContentPane().add(centerPanel, BorderLayout.CENTER);
151: getContentPane().add(buttonPanel, BorderLayout.SOUTH);
152: }
153:
154: protected void buildJoinsPanel() {
155: joinsPanel = new JPanel();
156: joinsPanel.setBorder(BorderFactory.createEmptyBorder(20, 20,
157: 20, 20));
158:
159: Box panel = Box.createHorizontalBox();
160: joinsPanel.add(panel);
161:
162: delCheckBoxPanel = Box.createVerticalBox();
163: fromPanel = Box.createVerticalBox();
164: relationPanel = Box.createVerticalBox();
165: toPanel = Box.createVerticalBox();
166:
167: fillJoinsPanel();
168:
169: panel.add(delCheckBoxPanel);
170: panel.add(fromPanel);
171: panel.add(relationPanel);
172: panel.add(toPanel);
173: }
174:
175: protected void fillJoinsPanel() {
176: joinFieldsList = new ArrayList();
177: for (Iterator iter = query.joins(); iter.hasNext();) {
178: Join j = (Join) iter.next();
179: joinFieldsList
180: .add(new JoinFields(j, report.getDataSource()));
181: }
182:
183: for (Iterator iter = joinFieldsList.iterator(); iter.hasNext();) {
184: JoinFields jf = (JoinFields) iter.next();
185: delCheckBoxPanel.add(jf.del);
186: fromPanel.add(jf.from);
187: relationPanel.add(jf.relation);
188: toPanel.add(jf.to);
189: }
190: }
191:
192: protected void emptyJoinsPanel() {
193: delCheckBoxPanel.removeAll();
194: fromPanel.removeAll();
195: relationPanel.removeAll();
196: toPanel.removeAll();
197: }
198:
199: /**
200: * Handles add and delete buttons.
201: */
202: public void actionPerformed(ActionEvent e) {
203: String cmd = e.getActionCommand();
204: if (cmd.equals(I18N.get("VisTableWin.add")))
205: addNewJoin();
206: else if (cmd.equals(I18N.get("VisTableWin.delete_selected")))
207: deleteSelectedJoins();
208: else
209: super .actionPerformed(e);
210: }
211:
212: protected void addNewJoin() {
213: JoinFields jf = new JoinFields(null, report.getDataSource());
214: joinFieldsList.add(jf);
215:
216: delCheckBoxPanel.add(jf.del);
217: fromPanel.add(jf.from);
218: relationPanel.add(jf.relation);
219: toPanel.add(jf.to);
220:
221: joinsPanel.invalidate();
222: pack();
223: }
224:
225: protected void deleteSelectedJoins() {
226: ArrayList copy = (ArrayList) joinFieldsList.clone();
227: for (Iterator iter = copy.iterator(); iter.hasNext();) {
228: JoinFields jf = (JoinFields) iter.next();
229: if (jf.del.isSelected()) {
230: joinFieldsList.remove(jf);
231:
232: delCheckBoxPanel.remove(jf.del);
233: fromPanel.remove(jf.from);
234: relationPanel.remove(jf.relation);
235: toPanel.remove(jf.to);
236: }
237: }
238: joinsPanel.invalidate();
239: pack();
240: }
241:
242: protected void doSave() {
243: ArrayList newJoins = new ArrayList();
244: for (Iterator iter = joinFieldsList.iterator(); iter.hasNext();) {
245: JoinFields jf = (JoinFields) iter.next();
246: Column from = columnFromDropdown(jf.from);
247: String relation = (String) jf.relation.getSelectedItem();
248: Column to = columnFromDropdown(jf.to);
249:
250: newJoins.add(new Join(from, relation, to));
251: }
252:
253: TableJoinCommand cmd = new TableJoinCommand(query, newJoins);
254: cmd.perform();
255: commands.add(cmd);
256: }
257:
258: protected Column columnFromDropdown(JComboBox cb) {
259: String sel = (String) cb.getSelectedItem();
260: return report.findColumn(sel);
261: }
262:
263: protected void doRevert() {
264: // Rebuild widgets
265: emptyJoinsPanel();
266: fillJoinsPanel();
267: joinsPanel.invalidate();
268: pack();
269: }
270:
271: }
|