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/ColumnListLookupDialog.java $
024: //$Author: Dan $
025: //$Revision: 2 $
026: //$Modtime: 12/18/03 11:02a $
027: /////////////////////////
028: import java.awt.Container;
029: import java.awt.Dimension;
030: import java.awt.Frame;
031: import java.awt.Toolkit;
032: import java.awt.event.ActionEvent;
033: import java.awt.event.ActionListener;
034: import java.util.Vector;
035:
036: import javax.swing.Box;
037: import javax.swing.BoxLayout;
038: import javax.swing.JButton;
039: import javax.swing.JComboBox;
040: import javax.swing.JDialog;
041: import javax.swing.JList;
042: import javax.swing.JPanel;
043: import javax.swing.JScrollPane;
044: import javax.swing.JTextArea;
045: import javax.swing.border.EmptyBorder;
046: import javax.swing.event.ListSelectionEvent;
047: import javax.swing.event.ListSelectionListener;
048:
049: import com.salmonllc.sql.ColumnDefinition;
050: import com.salmonllc.sql.DataDictionary;
051:
052: public class ColumnListLookupDialog extends JDialog implements
053: ActionListener, ListSelectionListener {
054:
055: JButton _ok, _cancel, _addTable, _addColumn;
056: JTextArea _colList;
057: JComboBox _tables;
058: JList _columns;
059: DataDictionary _dd;
060: boolean _clickedCancel = true;
061:
062: public ColumnListLookupDialog(Frame owner, String columnList,
063: DataDictionary dd, int buttonHeight) {
064: super (owner, "Build a table/column list", true);
065: _dd = dd;
066: int width = 500;
067: int height = 350;
068: Dimension frameBounds = Toolkit.getDefaultToolkit()
069: .getScreenSize();
070: int x = (frameBounds.width - width) / 2;
071: int y = (frameBounds.height - height) / 2;
072:
073: setBounds(x, y, width, height);
074: setResizable(false);
075: setModal(true);
076:
077: //main box
078: JPanel main = new JPanel();
079: main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS));
080: main.setBorder(new EmptyBorder(10, 10, 10, 10));
081:
082: //row1, column headings
083: JPanel box = new JPanel();
084: box.setLayout(new BoxLayout(box, BoxLayout.X_AXIS));
085: box.add(DialogComponentFactory.makeLabel(
086: "Tables/Columns in DataBase:", 200));
087: box.add(Box.createRigidArea(new Dimension(50, 0)));
088: box.add(DialogComponentFactory.makeLabel(
089: "Table/Columns Selected:", 200));
090: main.add(box);
091:
092: JPanel b2 = new JPanel();
093: b2.setLayout(new BoxLayout(b2, BoxLayout.X_AXIS));
094: main.add(b2);
095:
096: JPanel b3 = new JPanel();
097: b3.setLayout(new BoxLayout(b3, BoxLayout.Y_AXIS));
098: b2.add(b3);
099:
100: //row2, tables box
101: box = new JPanel();
102: box.setLayout(new BoxLayout(box, BoxLayout.X_AXIS));
103: box.add(_tables = DialogComponentFactory.makeComboBox(200));
104: box.add(Box.createRigidArea(new Dimension(5, 0)));
105: box.add(_addTable = DialogComponentFactory.makeButton("+", 40,
106: buttonHeight));
107: b3.add(box);
108:
109: //row2 right side
110: box = new JPanel();
111: box.setLayout(new BoxLayout(box, BoxLayout.X_AXIS));
112: box.add(DialogComponentFactory.makeScrollPane(200, 170,
113: _columns = DialogComponentFactory
114: .makeList(new LModel())));
115: box.add(Box.createRigidArea(new Dimension(5, 0)));
116: box.add(_addColumn = DialogComponentFactory.makeButton("+", 40,
117: buttonHeight));
118: b3.add(box);
119:
120: JScrollPane p = DialogComponentFactory.makeScrollPane(200, 200,
121: _colList = new JTextArea(50, 30));
122: p
123: .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
124: _colList.setWrapStyleWord(true);
125: _colList.setLineWrap(true);
126: _colList.setText(columnList);
127: b2.add(Box.createRigidArea(new Dimension(5, 0)));
128: b2.add(p);
129:
130: //row 4
131: _ok = new JButton("OK");
132: _ok.addActionListener(this );
133: _cancel = new JButton("Cancel");
134: _cancel.addActionListener(this );
135: JPanel buttonBar = new JPanel();
136: buttonBar.setLayout(new BoxLayout(buttonBar, BoxLayout.X_AXIS));
137: buttonBar.add(_ok = new JButton("OK"));
138: buttonBar.add(_cancel = new JButton("Cancel"));
139: main.add(buttonBar);
140:
141: //Add Listeners
142: _ok.addActionListener(this );
143: _cancel.addActionListener(this );
144: _addTable.addActionListener(this );
145: _addColumn.addActionListener(this );
146: _tables.addActionListener(this );
147: _columns.addListSelectionListener(this );
148:
149: //load the tables from the db
150: loadTables();
151:
152: //Add it to the screen
153: Container cp = getContentPane();
154: cp.setLayout(new BoxLayout(cp, BoxLayout.Y_AXIS));
155: cp.add(main);
156: cp.add(Box.createVerticalGlue());
157: cp.add(buttonBar);
158: cp.add(Box.createRigidArea(new Dimension(0, 20)));
159: enableDisableButtons();
160: setVisible(true);
161: }
162:
163: public void actionPerformed(ActionEvent e) {
164: if (e.getSource() == _cancel) {
165: _clickedCancel = true;
166: setVisible(false);
167: } else if (e.getSource() == _ok) {
168: _clickedCancel = false;
169: setVisible(false);
170: } else if (e.getSource() == _tables) {
171: String st = (String) _tables.getSelectedItem();
172: loadColumns(st);
173: enableDisableButtons();
174: } else if (e.getSource() == _addTable) {
175: String val = _colList.getText();
176: if (val == null)
177: val = "";
178: if (val.length() != 0)
179: val += ", ";
180: val += _tables.getSelectedItem() + ".*";
181: _colList.setText(val);
182: } else if (e.getSource() == _addColumn) {
183: String val = _colList.getText();
184: if (val == null)
185: val = "";
186: if (val.length() != 0)
187: val += ", ";
188: Object def[] = (Object[]) _columns.getSelectedValues();
189: for (int i = 0; i < def.length; i++) {
190: ColumnDefinition cd = (ColumnDefinition) def[i];
191: val += cd.getTableName() + "." + cd.getColumnName();
192: if (i < def.length - 1)
193: val += ", ";
194: }
195: _colList.setText(val);
196: }
197:
198: }
199:
200: private void enableDisableButtons() {
201: _addColumn.setEnabled(_columns.getSelectedIndices().length > 0);
202: }
203:
204: /**
205: * @return whether or not the user clicked cancel to close the dialog
206: */
207: public boolean isClickedCancel() {
208: return _clickedCancel;
209: }
210:
211: public String getColumnList() {
212: return _colList.getText();
213: }
214:
215: private void loadColumns(String tableName) {
216: if (tableName != null) {
217: Vector v = _dd.getColumns(tableName);
218: Vector mod = new Vector(v.size());
219: for (int i = 0; i < v.size(); i++) {
220: mod.add(v.elementAt(i));
221: }
222: _columns.setModel(new LModel(mod));
223: }
224: }
225:
226: private void loadTables() {
227: Vector v = _dd.getTableNames();
228: for (int i = 0; i < v.size(); i++) {
229: _tables.addItem(v.elementAt(i));
230: }
231: }
232:
233: public void valueChanged(ListSelectionEvent e) {
234: enableDisableButtons();
235:
236: }
237: }
|