001: package com.xoetrope.carousel.services.dialog;
002:
003: import java.sql.Connection;
004: import java.sql.DriverManager;
005: import java.sql.ResultSet;
006: import java.sql.ResultSetMetaData;
007: import java.sql.Statement;
008:
009: import java.awt.BorderLayout;
010: import java.awt.GridLayout;
011: import java.awt.event.ActionEvent;
012: import java.awt.event.ActionListener;
013: import javax.swing.DefaultListModel;
014: import javax.swing.JButton;
015: import javax.swing.JDialog;
016: import javax.swing.JList;
017: import javax.swing.JPanel;
018: import javax.swing.JScrollPane;
019: import javax.swing.ListSelectionModel;
020:
021: import net.xoetrope.xml.XmlElement;
022:
023: /**
024: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
025: * the GNU Public License (GPL), please see license.txt for more details. If
026: * you make commercial use of this software you must purchase a commercial
027: * license from Xoetrope.</p>
028: * <p> $Revision: 1.3 $</p>
029: */
030: public class DatabaseObjects extends JDialog implements ActionListener {
031:
032: private JScrollPane listPane;
033: private JList lstObjects;
034: private JPanel pnlControls;
035: private JButton btnOK, btnCancel;
036: private boolean isOK;
037:
038: private String selectedObject;
039: private DefaultListModel dataModel;
040:
041: public DatabaseObjects() {
042: getContentPane().setLayout(new BorderLayout());
043: setModal(true);
044: setSize(300, 400);
045: createComponents();
046: }
047:
048: private void createComponents() {
049: dataModel = new DefaultListModel();
050: lstObjects = new JList(dataModel);
051: listPane = new JScrollPane(lstObjects);
052: pnlControls = new JPanel();
053: pnlControls.setLayout(new GridLayout(1, 2));
054: btnOK = new JButton("OK");
055: btnCancel = new JButton("Cancel");
056:
057: btnOK.addActionListener(this );
058: btnCancel.addActionListener(this );
059:
060: pnlControls.add(btnOK);
061: pnlControls.add(btnCancel);
062: getContentPane().add(listPane, BorderLayout.CENTER);
063: getContentPane().add(pnlControls, BorderLayout.SOUTH);
064: }
065:
066: public boolean showDlg(String table, XmlElement connEle) {
067: lstObjects
068: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
069: selectedObject = table;
070: loadTables(connEle);
071: setVisible(true);
072: return isOK;
073: }
074:
075: public boolean showDlg(String table, String[] fields,
076: XmlElement connEle) {
077: selectedObject = table;
078: loadFields(table, fields, connEle);
079: setVisible(true);
080: return isOK;
081: }
082:
083: public String getSelectedObject() {
084: return lstObjects.getSelectedValue().toString();
085: }
086:
087: public Object[] getSelectedFields() {
088: return lstObjects.getSelectedValues();
089: }
090:
091: public void actionPerformed(ActionEvent evt) {
092: if (evt.getSource().equals(btnOK)) {
093: isOK = true;
094: } else if (evt.getSource().equals(btnCancel)) {
095: isOK = false;
096: }
097: setVisible(false);
098: }
099:
100: private void loadTables(XmlElement connEle) {
101: String types[] = { "TABLE" };
102: try {
103: Connection conn = getConnection(connEle);
104: ResultSet RS = conn.getMetaData().getTables(null, null,
105: null, types);
106: while (RS.next()) {
107: dataModel.addElement(RS.getString(3));
108: }
109: lstObjects.setModel(dataModel);
110: lstObjects.setSelectedValue(selectedObject, true);
111: } catch (Exception e) {
112: e.printStackTrace();
113: }
114: }
115:
116: private void loadFields(String table, String[] fields,
117: XmlElement connEle) {
118: try {
119: Connection conn = getConnection(connEle);
120: Statement stmt = conn.createStatement();
121: ResultSet RS = stmt.executeQuery("SELECT * FROM " + table);
122: ResultSetMetaData meta = RS.getMetaData();
123: for (int i = 1; i < meta.getColumnCount(); i++) {
124: dataModel.addElement(meta.getColumnName(i));
125: }
126: lstObjects.setModel(dataModel);
127: lstObjects.setSelectionMode(2);
128:
129: // Select the current fields
130: int[] sel = new int[fields.length];
131: for (int i = 0; i < fields.length; i++) {
132: lstObjects.setSelectedValue(fields[i].trim(), true);
133: sel[i] = lstObjects.getSelectedIndex();
134: }
135: lstObjects.setSelectedIndices(sel);
136: } catch (Exception ex) {
137: ex.printStackTrace();
138: }
139: }
140:
141: private Connection getConnection(XmlElement connEle) {
142: String url = connEle.getAttribute("url");
143: String user = connEle.getAttribute("user");
144: String pwd = connEle.getAttribute("pwd");
145:
146: Connection conn = null;
147: try {
148: Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
149: conn = DriverManager.getConnection(url, user, pwd);
150: } catch (Exception e) {
151: e.printStackTrace();
152: }
153: return conn;
154: }
155:
156: }
|