001: package org.netbeans.modules.mashup.tables.wizard;
002:
003: import java.awt.event.MouseEvent;
004: import java.awt.event.MouseListener;
005: import java.util.HashMap;
006: import java.util.Map;
007: import java.util.Vector;
008: import javax.swing.DefaultListModel;
009: import javax.swing.JComboBox;
010: import javax.swing.JList;
011: import javax.swing.JPanel;
012: import javax.swing.ListSelectionModel;
013: import javax.swing.SwingUtilities;
014: import javax.swing.table.DefaultTableModel;
015: import net.java.hulp.i18n.Logger;
016: import org.netbeans.api.db.explorer.ConnectionManager;
017: import org.netbeans.api.db.explorer.DatabaseConnection;
018: import org.netbeans.modules.etl.logger.Localizer;
019: import org.netbeans.modules.etl.logger.LogUtil;
020: import org.netbeans.modules.sql.framework.common.utils.DBExplorerUtil;
021: import org.netbeans.modules.sql.framework.model.DBMetaDataFactory;
022:
023: public final class JDBCTableVisualPanel extends JPanel {
024:
025: private JDBCTablePanel owner;
026: private boolean canAdvance = false;
027: private Map<String, String> userMap = new HashMap<String, String>();
028: private Map<String, String> passwdMap = new HashMap<String, String>();
029: private DBMetaDataFactory meta = new DBMetaDataFactory();
030: DatabaseConnection conn = null;
031: private Map<String, String> driverMap = new HashMap<String, String>();
032: private static transient final Logger mLogger = LogUtil
033: .getLogger(JDBCTableVisualPanel.class.getName());
034: private static transient final Localizer mLoc = Localizer.get();
035:
036: /**
037: * Creates new form ChooseTableVisualPanel
038: */
039: public JDBCTableVisualPanel(JDBCTablePanel panel) {
040: owner = panel;
041: initComponents();
042: connectionList.setModel(new DefaultListModel());
043: tableList.setModel(new DefaultListModel());
044: connectionList
045: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
046: selectButton.setEnabled(false);
047: selectButton.setMnemonic('S');
048: removeButton.setEnabled(false);
049: removeButton.setMnemonic('R');
050: populateConnections();
051: connectionList.addMouseListener(new MouseListener() {
052:
053: public void mouseClicked(MouseEvent e) {
054: }
055:
056: public void mousePressed(MouseEvent e) {
057: }
058:
059: public void mouseReleased(MouseEvent e) {
060: DefaultListModel model = (DefaultListModel) connectionList
061: .getModel();
062: String jdbcUrl = (String) connectionList
063: .getSelectedValue();
064: DatabaseConnection dbConn = null;
065: DatabaseConnection dbConns[] = ConnectionManager
066: .getDefault().getConnections();
067: for (DatabaseConnection dc : dbConns) {
068: if (dc.getDatabaseURL().equals(jdbcUrl)) {
069: dbConn = dc;
070: break;
071: }
072: }
073:
074: conn = dbConn;
075: ConnectionManager.getDefault().showConnectionDialog(
076: conn);
077: try {
078: userMap.put(conn.getDatabaseURL(), conn.getUser());
079: passwdMap.put(conn.getDatabaseURL(), conn
080: .getPassword());
081: driverMap.put(conn.getDatabaseURL(), conn
082: .getDriverClass());
083: meta.connectDB(DBExplorerUtil
084: .createConnection(conn));
085: String[] schemas = meta.getSchemas();
086: schemaCombo.removeAllItems();
087: for (String schema : schemas) {
088: schemaCombo.addItem(schema);
089: }
090: if (schemaCombo.getItemCount() != 0) {
091: String schema = (String) schemaCombo
092: .getItemAt(0);
093: populateTable(schema);
094: }
095: } catch (Exception ex) {
096: ex.printStackTrace();
097: }
098: }
099:
100: public void mouseEntered(MouseEvent e) {
101: }
102:
103: public void mouseExited(MouseEvent e) {
104: }
105: });
106: }
107:
108: String nbBundle2 = mLoc.t("PRSR001: Choose Connection Details");
109:
110: @Override
111: public String getName() {
112: return Localizer.parse(nbBundle2);
113: }
114:
115: public DefaultTableModel getTables() {
116: return (DefaultTableModel) jTable1.getModel();
117: }
118:
119: public void cleanup() {
120: try {
121: if (meta != null) {
122: meta.disconnectDB();
123: }
124: } catch (Exception ex) {
125: ex.printStackTrace();
126: }
127: }
128:
129: /** This method is called from within the constructor to
130: * initialize the form.
131: * WARNING: Do NOT modify this code. The content of this method is
132: * always regenerated by the Form Editor.
133: */
134: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
135: private void initComponents() {
136: jScrollPane3 = new javax.swing.JScrollPane();
137: jTable1 = new javax.swing.JTable();
138: jScrollPane1 = new javax.swing.JScrollPane();
139: connectionList = new javax.swing.JList();
140: jScrollPane2 = new javax.swing.JScrollPane();
141: tableList = new javax.swing.JList();
142: schemaCombo = new javax.swing.JComboBox();
143: jLabel1 = new javax.swing.JLabel();
144: error = new javax.swing.JLabel();
145: jPanel1 = new javax.swing.JPanel();
146: selectButton = new javax.swing.JButton();
147: removeButton = new javax.swing.JButton();
148: jLabel2 = new javax.swing.JLabel();
149:
150: jScrollPane3.setBorder(javax.swing.BorderFactory
151: .createTitledBorder("Table Selection"));
152: jTable1.setModel(new javax.swing.table.DefaultTableModel(
153: new Object[][] {
154:
155: }, new String[] { "Table Name", "Schema",
156: "Connection Url" }) {
157: boolean[] canEdit = new boolean[] { false, false, false };
158:
159: public boolean isCellEditable(int rowIndex, int columnIndex) {
160: return canEdit[columnIndex];
161: }
162: });
163: jTable1
164: .setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_LAST_COLUMN);
165: jScrollPane3.setViewportView(jTable1);
166:
167: connectionList.setBorder(javax.swing.BorderFactory
168: .createTitledBorder("Connections"));
169: connectionList
170: .setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
171: jScrollPane1.setViewportView(connectionList);
172:
173: jScrollPane2.setViewportView(tableList);
174:
175: schemaCombo
176: .addActionListener(new java.awt.event.ActionListener() {
177: public void actionPerformed(
178: java.awt.event.ActionEvent evt) {
179: schemaComboActionPerformed(evt);
180: }
181: });
182:
183: jLabel1.setForeground(new java.awt.Color(0, 0, 255));
184: org.openide.awt.Mnemonics.setLocalizedText(jLabel1, "Schema");
185:
186: error.setForeground(new java.awt.Color(255, 0, 0));
187:
188: org.openide.awt.Mnemonics.setLocalizedText(selectButton,
189: "Select");
190: selectButton
191: .addActionListener(new java.awt.event.ActionListener() {
192: public void actionPerformed(
193: java.awt.event.ActionEvent evt) {
194: selectButtonActionPerformed(evt);
195: }
196: });
197:
198: org.openide.awt.Mnemonics.setLocalizedText(removeButton,
199: "Remove");
200: removeButton
201: .addActionListener(new java.awt.event.ActionListener() {
202: public void actionPerformed(
203: java.awt.event.ActionEvent evt) {
204: removeButtonActionPerformed(evt);
205: }
206: });
207:
208: org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(
209: jPanel1);
210: jPanel1.setLayout(jPanel1Layout);
211: jPanel1Layout
212: .setHorizontalGroup(jPanel1Layout
213: .createParallelGroup(
214: org.jdesktop.layout.GroupLayout.LEADING)
215: .add(
216: jPanel1Layout
217: .createSequentialGroup()
218: .addContainerGap(
219: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
220: Short.MAX_VALUE)
221: .add(
222: jPanel1Layout
223: .createParallelGroup(
224: org.jdesktop.layout.GroupLayout.LEADING)
225: .add(
226: org.jdesktop.layout.GroupLayout.TRAILING,
227: selectButton)
228: .add(
229: org.jdesktop.layout.GroupLayout.TRAILING,
230: removeButton))));
231: jPanel1Layout
232: .setVerticalGroup(jPanel1Layout
233: .createParallelGroup(
234: org.jdesktop.layout.GroupLayout.LEADING)
235: .add(
236: jPanel1Layout
237: .createSequentialGroup()
238: .add(selectButton)
239: .addPreferredGap(
240: org.jdesktop.layout.LayoutStyle.RELATED)
241: .add(removeButton)));
242:
243: jLabel2.setForeground(new java.awt.Color(0, 51, 255));
244: org.openide.awt.Mnemonics.setLocalizedText(jLabel2, "Tables");
245:
246: org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
247: this );
248: this .setLayout(layout);
249: layout
250: .setHorizontalGroup(layout
251: .createParallelGroup(
252: org.jdesktop.layout.GroupLayout.LEADING)
253: .add(
254: org.jdesktop.layout.GroupLayout.TRAILING,
255: layout
256: .createSequentialGroup()
257: .addContainerGap()
258: .add(
259: layout
260: .createParallelGroup(
261: org.jdesktop.layout.GroupLayout.LEADING)
262: .add(
263: error,
264: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
265: 454,
266: Short.MAX_VALUE)
267: .add(
268: org.jdesktop.layout.GroupLayout.TRAILING,
269: layout
270: .createSequentialGroup()
271: .add(
272: layout
273: .createParallelGroup(
274: org.jdesktop.layout.GroupLayout.LEADING)
275: .add(
276: org.jdesktop.layout.GroupLayout.TRAILING,
277: layout
278: .createSequentialGroup()
279: .add(
280: jScrollPane1,
281: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
282: 232,
283: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
284: .addPreferredGap(
285: org.jdesktop.layout.LayoutStyle.RELATED)
286: .add(
287: layout
288: .createParallelGroup(
289: org.jdesktop.layout.GroupLayout.LEADING)
290: .add(
291: jLabel1)
292: .add(
293: jLabel2))
294: .addPreferredGap(
295: org.jdesktop.layout.LayoutStyle.RELATED,
296: 17,
297: Short.MAX_VALUE)
298: .add(
299: layout
300: .createParallelGroup(
301: org.jdesktop.layout.GroupLayout.LEADING,
302: false)
303: .add(
304: schemaCombo,
305: 0,
306: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
307: Short.MAX_VALUE)
308: .add(
309: jScrollPane2,
310: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
311: 154,
312: Short.MAX_VALUE)))
313: .add(
314: layout
315: .createSequentialGroup()
316: .add(
317: jScrollPane3,
318: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
319: 357,
320: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
321: .addPreferredGap(
322: org.jdesktop.layout.LayoutStyle.RELATED)
323: .add(
324: jPanel1,
325: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
326: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
327: Short.MAX_VALUE)))
328: .addContainerGap()))));
329: layout
330: .setVerticalGroup(layout
331: .createParallelGroup(
332: org.jdesktop.layout.GroupLayout.LEADING)
333: .add(
334: layout
335: .createSequentialGroup()
336: .addContainerGap()
337: .add(
338: layout
339: .createParallelGroup(
340: org.jdesktop.layout.GroupLayout.LEADING,
341: false)
342: .add(
343: layout
344: .createSequentialGroup()
345: .add(
346: layout
347: .createParallelGroup(
348: org.jdesktop.layout.GroupLayout.BASELINE)
349: .add(
350: jLabel1)
351: .add(
352: schemaCombo,
353: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
354: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
355: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
356: .addPreferredGap(
357: org.jdesktop.layout.LayoutStyle.RELATED)
358: .add(
359: layout
360: .createParallelGroup(
361: org.jdesktop.layout.GroupLayout.LEADING)
362: .add(
363: layout
364: .createSequentialGroup()
365: .add(
366: jLabel2)
367: .addPreferredGap(
368: org.jdesktop.layout.LayoutStyle.RELATED))
369: .add(
370: jScrollPane2,
371: 0,
372: 0,
373: Short.MAX_VALUE)))
374: .add(
375: jScrollPane1,
376: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
377: 112,
378: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
379: .add(
380: layout
381: .createParallelGroup(
382: org.jdesktop.layout.GroupLayout.LEADING)
383: .add(
384: layout
385: .createSequentialGroup()
386: .addPreferredGap(
387: org.jdesktop.layout.LayoutStyle.RELATED,
388: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
389: Short.MAX_VALUE)
390: .add(
391: jScrollPane3,
392: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
393: 131,
394: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
395: .addPreferredGap(
396: org.jdesktop.layout.LayoutStyle.RELATED))
397: .add(
398: layout
399: .createSequentialGroup()
400: .addPreferredGap(
401: org.jdesktop.layout.LayoutStyle.RELATED)
402: .add(
403: jPanel1,
404: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
405: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
406: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
407: .addPreferredGap(
408: org.jdesktop.layout.LayoutStyle.RELATED)))
409: .add(
410: error,
411: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
412: 17, Short.MAX_VALUE)));
413: }// </editor-fold>//GEN-END:initComponents
414:
415: private void removeButtonActionPerformed(
416: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
417: int[] rows = jTable1.getSelectedRows();
418: final DefaultTableModel model = (DefaultTableModel) jTable1
419: .getModel();
420: for (int row : rows) {
421: model.removeRow(row);
422: }
423: Runnable run = new Runnable() {
424:
425: public void run() {
426: jTable1.setModel(model);
427: }
428: };
429: SwingUtilities.invokeLater(run);
430: owner.fireChangeEvent();
431: }//GEN-LAST:event_removeButtonActionPerformed
432:
433: private void selectButtonActionPerformed(
434: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectButtonActionPerformed
435: final DefaultTableModel model = (DefaultTableModel) jTable1
436: .getModel();
437: Object[] tables = tableList.getSelectedValues();
438: String schema = (String) schemaCombo.getSelectedItem();
439: String jdbcUrl = (String) connectionList.getSelectedValue();
440: for (Object table : tables) {
441: Vector row = new Vector();
442: row.add(table);
443: row.add(schema);
444: row.add(jdbcUrl);
445: model.addRow(row);
446: }
447: if (model.getRowCount() != 0) {
448: removeButton.setEnabled(true);
449: error.setText("");
450: canAdvance = true;
451: } else {
452: removeButton.setEnabled(false);
453: String nbBundle1 = mLoc
454: .t("PRSR001: No table available for processing.");
455: error.setText(Localizer.parse(nbBundle1));
456: canAdvance = false;
457: }
458: Runnable run = new Runnable() {
459:
460: public void run() {
461: jTable1.setModel(model);
462: }
463: };
464: SwingUtilities.invokeLater(run);
465: owner.fireChangeEvent();
466: }//GEN-LAST:event_selectButtonActionPerformed
467:
468: private void schemaComboActionPerformed(
469: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_schemaComboActionPerformed
470: JComboBox combo = (JComboBox) evt.getSource();
471: String schema = (String) combo.getSelectedItem();
472: populateTable(schema);
473: }//GEN-LAST:event_schemaComboActionPerformed
474:
475: private void populateTable(String schema) {
476: if (conn != null) {
477: try {
478: DefaultListModel model = (DefaultListModel) tableList
479: .getModel();
480: model.clear();
481: meta.connectDB(DBExplorerUtil.createConnection(conn));
482: String[][] tables = meta.getTablesAndViews("", schema,
483: "", false);
484: String[] currTable = null;
485: if (tables != null) {
486: for (int i = 0; i < tables.length; i++) {
487: currTable = tables[i];
488: model
489: .addElement(currTable[DBMetaDataFactory.NAME]);
490: }
491: }
492: if (model.getSize() != 0) {
493: selectButton.setEnabled(true);
494: } else {
495: selectButton.setEnabled(false);
496: }
497: tableList.setModel(model);
498: } catch (Exception ex) {
499: ex.printStackTrace();
500: }
501: }
502: }
503:
504: private void populateConnections() {
505: DefaultListModel model = (DefaultListModel) connectionList
506: .getModel();
507: model.clear();
508: driverMap.clear();
509: DatabaseConnection connections[] = ConnectionManager
510: .getDefault().getConnections();
511: for (DatabaseConnection conn1 : connections) {
512: model.addElement(conn1.getDatabaseURL());
513: }
514: setModel(connectionList, model);
515: }
516:
517: private void setModel(final JList list, final DefaultListModel model) {
518: Runnable run = new Runnable() {
519:
520: public void run() {
521: list.setModel(model);
522: }
523: };
524: SwingUtilities.invokeLater(run);
525: }
526:
527: public Map<String, String> getUserMap() {
528: return userMap;
529: }
530:
531: public Map<String, String> getPasswordMap() {
532: return passwdMap;
533: }
534:
535: public Map<String, String> getDriverMap() {
536: return driverMap;
537: }
538:
539: public boolean canAdvance() {
540: return (jTable1.getModel().getRowCount() != 0);
541: }
542:
543: // Variables declaration - do not modify//GEN-BEGIN:variables
544: private javax.swing.JList connectionList;
545: private javax.swing.JLabel error;
546: private javax.swing.JLabel jLabel1;
547: private javax.swing.JLabel jLabel2;
548: private javax.swing.JPanel jPanel1;
549: private javax.swing.JScrollPane jScrollPane1;
550: private javax.swing.JScrollPane jScrollPane2;
551: private javax.swing.JScrollPane jScrollPane3;
552: private javax.swing.JTable jTable1;
553: private javax.swing.JButton removeButton;
554: private javax.swing.JComboBox schemaCombo;
555: private javax.swing.JButton selectButton;
556: private javax.swing.JList tableList;
557: // End of variables declaration//GEN-END:variables
558: }
|