001: /*
002: * SQLeonardo :: java database frontend
003: * Copyright (C) 2004 nickyb@users.sourceforge.net
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
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: *
019: */
020:
021: package nickyb.sqleonardo.environment.ctrl.define;
022:
023: import java.awt.event.ItemEvent;
024: import java.awt.event.ItemListener;
025: import java.sql.ResultSet;
026: import java.sql.ResultSetMetaData;
027: import java.sql.SQLException;
028: import java.util.ArrayList;
029: import java.util.Arrays;
030: import java.util.Enumeration;
031: import java.util.Vector;
032:
033: import javax.swing.JComboBox;
034: import javax.swing.JList;
035: import javax.swing.JScrollPane;
036:
037: import nickyb.sqleonardo.common.gui.AbstractDialogConfirm;
038: import nickyb.sqleonardo.common.gui.BorderLayoutPanel;
039: import nickyb.sqleonardo.common.gui.ListView;
040: import nickyb.sqleonardo.environment.Application;
041:
042: public class ColumnsChooser extends AbstractDialogConfirm implements
043: ItemListener {
044: private JComboBox listviews;
045: private JList columns;
046:
047: private ColumnsChooser(String dvname, String defaultmetaview) {
048: super (Application.window, dvname + " - choose columns", 350,
049: 275);
050:
051: Application.session.mount(Application.ENTRY_PREFERENCES);
052: Application.session.home();
053: Application.session.jump("metaview." + dvname);
054:
055: Vector vmv = new Vector();
056: for (Enumeration e = Application.session.jumps(); e
057: .hasMoreElements();)
058: vmv.addElement(e.nextElement());
059:
060: Object[] omv = vmv.toArray();
061: Arrays.sort(omv);
062:
063: BorderLayoutPanel mask = new BorderLayoutPanel();
064: mask.setComponentNorth(listviews = new JComboBox(omv));
065: mask.setComponentCenter(new JScrollPane(columns = new JList()));
066:
067: listviews.addItemListener(this );
068: listviews.setSelectedItem(defaultmetaview);
069:
070: getContentPane().add(mask);
071:
072: // insertButton(2,"apply");
073: }
074:
075: protected boolean onConfirm() {
076: if (listviews.getSelectedItem() == null)
077: return false;
078:
079: String metaview = listviews.getSelectedItem().toString();
080: String dvname = this .getTitle().substring(0,
081: this .getTitle().lastIndexOf('-') - 1);
082:
083: Application.session.mount(Application.ENTRY_PREFERENCES);
084: Application.session.home();
085: Application.session.jump("metaview." + dvname);
086:
087: ArrayList al = Application.session.jump(metaview);
088: for (int i = 0; i < al.size(); i++) {
089: Object[] col = (Object[]) al.get(i);
090: if (metaview.equals("table types")
091: && (col[0].equals("TABLE_NAME") || col[0]
092: .equals("TABLE_TYPE")))
093: col[1] = new Boolean(true);
094: else
095: col[1] = new Boolean(columns.isSelectedIndex(i));
096: }
097:
098: return true;
099: }
100:
101: protected void onOpen() {
102: itemStateChanged(null);
103: }
104:
105: public void itemStateChanged(ItemEvent ie) {
106: if (listviews.getSelectedItem() == null)
107: return;
108:
109: String metaview = listviews.getSelectedItem().toString();
110: String dvname = this .getTitle().substring(0,
111: this .getTitle().lastIndexOf('-') - 1);
112:
113: Application.session.mount(Application.ENTRY_PREFERENCES);
114: Application.session.home();
115: Application.session.jump("metaview." + dvname);
116:
117: ArrayList al = Application.session.jump(metaview);
118: Vector vColumns = new Vector();
119:
120: for (int i = 0; i < al.size(); i++) {
121: Object[] col = (Object[]) al.get(i);
122: vColumns.addElement(col[0].toString());
123: }
124:
125: columns.setListData(vColumns);
126:
127: for (int i = 0; i < al.size(); i++) {
128: Object[] col = (Object[]) al.get(i);
129: if (((Boolean) col[1]).booleanValue())
130: columns.addSelectionInterval(i, i);
131: }
132: }
133:
134: public static void showDialog(String dvname, String defaultmetaview) {
135: ColumnsChooser dlg = new ColumnsChooser(dvname, defaultmetaview);
136: dlg.show();
137: }
138:
139: public static void list(String dvname, String metaview,
140: ListView lv, ResultSet rs) throws SQLException {
141: Application.session.mount(Application.ENTRY_PREFERENCES);
142: Application.session.home();
143: Application.session.jump("metaview." + dvname);
144:
145: ArrayList cols = Application.session.jump(metaview);
146: lv.reset();
147:
148: if (cols.size() == 0) {
149: ResultSetMetaData rsmd = rs.getMetaData();
150: for (int i = 1; i <= rsmd.getColumnCount(); i++) {
151: String name = rsmd.getColumnName(i);
152: lv.addColumn(name);
153:
154: cols.add(new Object[] { name, new Boolean(true) });
155: }
156: } else {
157: for (int i = 0; i < cols.size(); i++) {
158: Object[] col = (Object[]) cols.get(i);
159: if (((Boolean) col[1]).booleanValue())
160: lv.addColumn(col[0].toString());
161: }
162: }
163:
164: while (rs.next()) {
165: Object[] rowdata = new Object[cols.size()];
166: for (int i = 0, j = 0; i < rowdata.length; i++) {
167: Object[] col = (Object[]) cols.get(i);
168: if (((Boolean) col[1]).booleanValue()) {
169: rowdata[j++] = rs.getObject(i + 1);
170: if (rowdata[j - 1] != null
171: && rowdata[j - 1] instanceof String)
172: rowdata[j - 1] = rowdata[j - 1].toString()
173: .trim();
174: }
175: }
176: lv.addRow(rowdata);
177: }
178:
179: rs.close();
180: }
181: }
|