private void dbComboActionPerformed( java.awt.event.ActionEvent evt) { // GEN-FIRST:event_dbComboActionPerformed try { // TODO add your handling code here: JComboBox cb = (JComboBox) evt.getSource(); String sel = (String) cb.getSelectedItem(); fillJDBCParams(sel.trim()); if (dbMeta != null) dbMeta.close(); dbMeta = new JvxDBMetas(sel); if (!dbMeta.valid) return; List<String> tabs = dbMeta.getTablesAndViews(null); tabList.setListData(tabs.toArray()); } catch (Exception ex) { clearTabs(); if (dbMeta != null) dbMeta.close(); Logger.getLogger(JvxDBMgr.class.getName()).log(Level.SEVERE, null, ex); return; } } // GEN-LAST:event_dbComboActionPerformed
private void tabListValueChanged( javax.swing.event.ListSelectionEvent evt) { // GEN-FIRST:event_tabListValueChanged // TODO add your handling code here: // System.out.println("[JvxDbMgr]: " + evt.getFirstIndex() +" "+ evt.getLastIndex() +" "+ // evt.getValueIsAdjusting()); if (evt.getValueIsAdjusting()) return; // System.out.println("[JvxDbMgr]: IN: " + evt.getFirstIndex() +" "+ evt.getLastIndex() +" "+ // evt.getValueIsAdjusting()); try { // TODO add your handling code here: int sels[] = tabList.getSelectedIndices(); ListModel model = tabList.getModel(); Object vals[][] = new Object[sels.length][]; int row = 0; int ncols = 0; for (int ind : sels) { String sel = (String) model.getElementAt(ind); List<String> fields = dbMeta.getTableFields(sel.trim()); List<Object> rows = new ArrayList<Object>(); rows.add(Boolean.FALSE); rows.add(sel); rows.addAll(rows.size(), fields); ncols = Math.max(ncols, rows.size()); vals[row++] = rows.toArray(); } List<String> names = new ArrayList<String>(); names.add("Select"); names.add("Table"); for (int i = 0; i < ncols - 2; i++) { // "Table", "Field1", "Field2", "Field2", names.add("Field" + (i + 1)); } for (int i = 0; i < row; i++) { if (vals[i].length < ncols) { List<Object> l = new ArrayList<Object>(Arrays.asList(vals[i])); for (int j = vals[i].length; j < ncols; j++) l.add(""); vals[i] = l.toArray(); } } dbTab.setModel( new javax.swing.table.DefaultTableModel(vals, names.toArray()) { @Override public Class<?> getColumnClass(int col) { return getValueAt(0, col).getClass(); } }); } catch (SQLException ex) { Logger.getLogger(JvxDBMgr.class.getName()).log(Level.SEVERE, null, ex); } } // GEN-LAST:event_tabListValueChanged
private void fillJDBCParams(String type) { Map<String, String> kv = JvxDBMetas.getKeysVals(type); Object vals[][] = new Object[kv.size()][]; int row = 0; int ncols = 0; for (Map.Entry es : kv.entrySet()) { String k = (String) es.getKey(); List<String> rows = new ArrayList<String>(); rows.add(k); rows.add((String) es.getValue()); vals[row++] = rows.toArray(); } this.jdbcTab.setModel( new javax.swing.table.DefaultTableModel(vals, new String[] {"Param", "Value"})); }
/** * This method is called from within the constructor to initialize the form. WARNING: Do NOT * modify this code. The content of this method is always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { dbCombo = new javax.swing.JComboBox(); jScrollPane1 = new javax.swing.JScrollPane(); jdbcTab = new javax.swing.JTable(); jScrollPane2 = new javax.swing.JScrollPane(); dbTab = new javax.swing.JTable(); jScrollPane3 = new javax.swing.JScrollPane(); tabList = new javax.swing.JList(); jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setTitle("Configure DB Interface"); dbCombo.setModel(new javax.swing.DefaultComboBoxModel(JvxDBMetas.getDbTypes())); // new javax.swing.DefaultComboBoxModel(new String[] { "Select DB...", "MariaDB", "MySQL", // "SQLite", "Oracle", "MS SQL Server", "DB2", "Sybase", "Other" })); dbCombo.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { dbComboActionPerformed(evt); } }); jdbcTab.setModel( new javax.swing.table.DefaultTableModel( new Object[][] { {"url", null}, {"driver", null}, {"protocol", null}, {"subprotocol", null}, {"username", null}, {"password", null} }, new String[] {"Param", "Value"})); jScrollPane1.setViewportView(jdbcTab); dbTab.setModel( new javax.swing.table.DefaultTableModel( new Object[][] {{null, "Road", "Name", "Type", "Fast", "Smooth"}}, new String[] {"Select", "Table", "Field1", "Field2", "Field3", "Field4"}) { Class[] types = new Class[] { java.lang.Boolean.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class }; public Class getColumnClass(int columnIndex) { return types[columnIndex]; } }); jScrollPane2.setViewportView(dbTab); tabList.setModel( new javax.swing.AbstractListModel() { String[] strings = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"}; public int getSize() { return strings.length; } public Object getElementAt(int i) { return strings[i]; } }); tabList.setLayoutOrientation(javax.swing.JList.VERTICAL_WRAP); tabList.setValueIsAdjusting(true); tabList.setVisibleRowCount(10); tabList.addListSelectionListener( new javax.swing.event.ListSelectionListener() { public void valueChanged(javax.swing.event.ListSelectionEvent evt) { tabListValueChanged(evt); } }); jScrollPane3.setViewportView(tabList); jButton1.setText("Done...Go Back"); jButton1.setActionCommand("DBInterface"); /* jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); */ jButton1.addActionListener((ActionListener) this.getParent()); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 606, Short.MAX_VALUE) .addGroup( layout .createSequentialGroup() .addGroup( layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup( layout .createSequentialGroup() .addGroup( layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent( jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE) .addComponent(dbCombo, 0, 269, Short.MAX_VALUE)) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED)) .addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout .createSequentialGroup() .addComponent( jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(58, 58, 58))) .addComponent(jScrollPane3))); layout.setVerticalGroup( layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup( layout .createSequentialGroup() .addGroup( layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup( layout .createSequentialGroup() .addGap(1, 1, 1) .addComponent( jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)) .addGroup( layout .createSequentialGroup() .addComponent( dbCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent( jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent( jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 18, Short.MAX_VALUE))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent( jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE))); pack(); } // </editor-fold>//GEN-END:initComponents