001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
008: //
009: // This program is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: // GNU General Public License for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020: package com.salmonllc.ideTools;
021:
022: /////////////////////////
023: //$Archive: /SOFIA/SourceCode/com/salmonllc/ideTools/JoinDialog.java $
024: //$Author: Dan $
025: //$Revision: 8 $
026: //$Modtime: 6/11/03 12:52p $
027: /////////////////////////
028: import java.awt.*;
029: import java.awt.event.ActionEvent;
030: import java.awt.event.ActionListener;
031: import java.util.Vector;
032:
033: import javax.swing.*;
034: import javax.swing.border.EmptyBorder;
035: import javax.swing.event.ListSelectionEvent;
036: import javax.swing.event.ListSelectionListener;
037:
038: import com.salmonllc.sql.ColumnDefinition;
039: import com.salmonllc.sql.DataDictionary;
040:
041: public class JoinDialog extends JDialog implements ActionListener,
042: ListSelectionListener {
043:
044: ModelDialog.AliasDef[] _aliases;
045: JButton _ok, _cancel, _add, _del;
046: JComboBox _leftTables, _rightTables;
047: JList _leftColumns, _rightColumns, _joins;
048: JCheckBox _outer;
049: DataDictionary _dd;
050: boolean _clickedCancel = true;
051:
052: public class JoinTermDef {
053: String left;
054: String right;
055: boolean outer;
056:
057: public JoinTermDef(String left, String right, boolean outer) {
058: this .left = left;
059: this .right = right;
060: this .outer = outer;
061: }
062:
063: public String toString() {
064: return left + (outer ? "*=" : "=") + right;
065: }
066: }
067:
068: public JoinDialog(Frame owner, ModelDialog.AliasDef[] aliases,
069: DataDictionary dd, int buttonHeight) {
070: super (owner, "Create a join", true);
071: _dd = dd;
072: int width = 620;
073: int height = 450;
074: Dimension frameBounds = Toolkit.getDefaultToolkit()
075: .getScreenSize();
076: int x = (frameBounds.width - width) / 2;
077: int y = (frameBounds.height - height) / 2;
078:
079: setBounds(x, y, width, height);
080: setResizable(false);
081: setModal(true);
082:
083: //main box
084: JPanel main = new JPanel();
085: main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS));
086: main.setBorder(new EmptyBorder(10, 10, 10, 10));
087:
088: //row 1, headings
089: JPanel box = new JPanel();
090: box.setLayout(new BoxLayout(box, BoxLayout.X_AXIS));
091: box.add(DialogComponentFactory.makeLabel(
092: "Tables/Columns Left Side:", 300));
093: box.add(DialogComponentFactory.makeLabel(
094: "Tables/Columns Right Side:", 300));
095: box.add(Box.createHorizontalGlue());
096: main.add(box);
097: main.add(Box.createRigidArea(new Dimension(0, 5)));
098:
099: //row 2, drop downs
100: box = new JPanel();
101: box.setLayout(new BoxLayout(box, BoxLayout.X_AXIS));
102: box.add(_leftTables = DialogComponentFactory.makeComboBox(290));
103: box.add(Box.createRigidArea(new Dimension(10, 0)));
104: box
105: .add(_rightTables = DialogComponentFactory
106: .makeComboBox(290));
107: box.add(Box.createHorizontalGlue());
108: main.add(box);
109: main.add(Box.createRigidArea(new Dimension(0, 5)));
110:
111: //row3, column lists
112: box = new JPanel();
113: box.setLayout(new BoxLayout(box, BoxLayout.X_AXIS));
114: box.add(_leftColumns = DialogComponentFactory
115: .makeList(new LModel()));
116: box.add(DialogComponentFactory.makeScrollPane(290, 150,
117: _leftColumns = DialogComponentFactory
118: .makeList(new LModel())));
119: box.add(Box.createRigidArea(new Dimension(10, 0)));
120: box.add(DialogComponentFactory.makeScrollPane(290, 150,
121: _rightColumns = DialogComponentFactory
122: .makeList(new LModel())));
123: box.add(Box.createHorizontalGlue());
124: main.add(box);
125: main.add(Box.createRigidArea(new Dimension(0, 5)));
126:
127: //row4, Add and Delete Buttons
128: box = new JPanel();
129: box.setLayout(new BoxLayout(box, BoxLayout.X_AXIS));
130: box.add(DialogComponentFactory.makeLabel(
131: "Column Pairs in Join:", 370));
132: box.add(DialogComponentFactory.makeLabel("Outer:", 50));
133: box.add(_outer = new JCheckBox());
134: box.add(Box.createRigidArea(new Dimension(30, 0)));
135: box.add(_add = DialogComponentFactory.makeButton("Add", 60,
136: buttonHeight));
137: box.add(_del = DialogComponentFactory.makeButton("Del", 60,
138: buttonHeight));
139: box.add(Box.createHorizontalGlue());
140: main.add(box);
141: main.add(Box.createRigidArea(new Dimension(0, 5)));
142:
143: //row5, Join Display
144: box = new JPanel();
145: box.setLayout(new BoxLayout(box, BoxLayout.X_AXIS));
146: box
147: .add(DialogComponentFactory.makeScrollPane(600, 100,
148: _joins = DialogComponentFactory
149: .makeList(new LModel())));
150: main.add(box);
151:
152: //button bar
153: _ok = new JButton("OK");
154: _ok.addActionListener(this );
155: _cancel = new JButton("Cancel");
156: _cancel.addActionListener(this );
157: JPanel buttonBar = new JPanel();
158: buttonBar.setLayout(new BoxLayout(buttonBar, BoxLayout.X_AXIS));
159: buttonBar.add(_ok = new JButton("OK"));
160: buttonBar.add(_cancel = new JButton("Cancel"));
161: main.add(buttonBar);
162:
163: //Add Listeners
164: _ok.addActionListener(this );
165: _cancel.addActionListener(this );
166: _leftTables.addActionListener(this );
167: _rightTables.addActionListener(this );
168: _add.addActionListener(this );
169: _del.addActionListener(this );
170: _leftColumns.addListSelectionListener(this );
171: _rightColumns.addListSelectionListener(this );
172: _joins.addListSelectionListener(this );
173: _leftColumns
174: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
175: _rightColumns
176: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
177: //Fill Drop Downs
178: loadTables(aliases, _leftTables);
179: loadTables(aliases, _rightTables);
180:
181: //Add it to the screen
182: Container cp = getContentPane();
183: cp.setLayout(new BoxLayout(cp, BoxLayout.Y_AXIS));
184: cp.add(main);
185: cp.add(Box.createVerticalGlue());
186: cp.add(buttonBar);
187: cp.add(Box.createRigidArea(new Dimension(0, 20)));
188: enableDisableButtons();
189: setVisible(true);
190:
191: }
192:
193: public void actionPerformed(ActionEvent e) {
194: if (e.getSource() == _leftTables) {
195: ModelDialog.AliasDef d = (ModelDialog.AliasDef) _leftTables
196: .getSelectedItem();
197: loadColumns(d.table, _leftColumns);
198: enableDisableButtons();
199: } else if (e.getSource() == _rightTables) {
200: ModelDialog.AliasDef d = (ModelDialog.AliasDef) _rightTables
201: .getSelectedItem();
202: loadColumns(d.table, _rightColumns);
203: enableDisableButtons();
204: } else if (e.getSource() == _cancel) {
205: _clickedCancel = true;
206: setVisible(false);
207: } else if (e.getSource() == _ok) {
208: _clickedCancel = false;
209: setVisible(false);
210: } else if (e.getSource() == _add)
211: addJoin();
212: else if (e.getSource() == _del)
213: deleteJoin();
214:
215: }
216:
217: public void addJoin() {
218: ModelDialog.AliasDef leftTable = (ModelDialog.AliasDef) _leftTables
219: .getSelectedItem();
220: ModelDialog.AliasDef rightTable = (ModelDialog.AliasDef) _rightTables
221: .getSelectedItem();
222: ColumnDefinition leftColumn = (ColumnDefinition) _leftColumns
223: .getSelectedValue();
224: ColumnDefinition rightColumn = (ColumnDefinition) _rightColumns
225: .getSelectedValue();
226: boolean outer = _outer.isSelected();
227: LModel joinModel = (LModel) _joins.getModel();
228: for (int i = 0; i < joinModel.getSize(); i++)
229: ((JoinTermDef) joinModel.getElementAt(i)).outer = outer;
230: JoinTermDef d = new JoinTermDef(
231: (leftTable.alias == null ? leftTable.table
232: : leftTable.alias)
233: + "." + leftColumn.getColumnName(),
234: (rightTable.alias == null ? rightTable.table
235: : rightTable.alias)
236: + "." + rightColumn.getColumnName(), outer);
237:
238: joinModel.addElement(d);
239: enableDisableButtons();
240: }
241:
242: private void deleteJoin() {
243: LModel mod = (LModel) _joins.getModel();
244: int sel[] = _joins.getSelectedIndices();
245: for (int i = sel.length - 1; i > -1; i--)
246: mod.remove(sel[i]);
247: _joins.getSelectionModel().clearSelection();
248: enableDisableButtons();
249: }
250:
251: private void enableDisableButtons() {
252: _ok.setEnabled(_joins.getModel().getSize() > 0);
253: _del.setEnabled(_joins.getModel().getSize() > 0
254: && _joins.getSelectedIndices().length > 0);
255: _add.setEnabled(_leftColumns.getSelectedIndex() > -1
256: && _rightColumns.getSelectedIndex() > -1);
257: }
258:
259: /**
260: * Returns the join terms specified by the user or an empty array if the user clicked cancel
261: */
262: public JoinTermDef[] getResult() {
263: if (_clickedCancel)
264: return new JoinTermDef[0];
265: LModel l = (LModel) _joins.getModel();
266: JoinTermDef ret[] = new JoinTermDef[l.getSize()];
267: for (int i = 0; i < l.getSize(); i++)
268: ret[i] = (JoinTermDef) l.getElementAt(i);
269: return ret;
270: }
271:
272: private void loadColumns(String tableName, JList list) {
273: if (tableName != null) {
274: Vector v = _dd.getColumns(tableName);
275: Vector mod = new Vector(v.size());
276: for (int i = 0; i < v.size(); i++) {
277: ColumnDefinition def = (ColumnDefinition) v
278: .elementAt(i);
279: mod.add(def);
280: }
281: list.setModel(new LModel(mod));
282: }
283: }
284:
285: private void loadTables(ModelDialog.AliasDef[] defs, JComboBox box) {
286: box.removeAllItems();
287: for (int i = 0; i < defs.length; i++)
288: box.addItem(defs[i]);
289: }
290:
291: public void valueChanged(ListSelectionEvent e) {
292: enableDisableButtons();
293: }
294: }
|