/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.wcs.netbeans.liquiface.ui.wizards.panel; /* * #%L * Liquiface - GUI for Liquibase * %% * Copyright (C) 2013 Webstar Csoport Kft. * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this program. If not, see * <http://www.gnu.org/licenses/gpl-3.0.html>. * #L% */ import com.wcs.netbeans.liquiface.util.DatabaseConnectionItem; import com.wcs.netbeans.liquiface.database.DatabaseConnectionFactory; import javax.swing.ComboBoxModel; import javax.swing.DefaultComboBoxModel; import javax.swing.JPanel; import org.netbeans.api.db.explorer.DatabaseConnection; public class DatabaseChooserPanel extends JPanel { private ComboBoxModel databases; private DatabaseConnection selectedDatabase; public DatabaseConnection getSelectedDatabase() { return selectedDatabase; } private ComboBoxModel getDatabases() { if (databases == null) { DatabaseConnection[] netbeansConnections = DatabaseConnectionFactory.getInstance().getNetbeansConnections(); DatabaseConnectionItem[] items = new DatabaseConnectionItem[netbeansConnections.length]; int i = 0; for (DatabaseConnection conn : netbeansConnections) { items[i++] = new DatabaseConnectionItem(conn); } databases = new DefaultComboBoxModel(items); } return databases; } /** * Creates new form LoadDatabaseVisualPanel1 */ public DatabaseChooserPanel() { initComponents(); initWithFirstDatabase(); } private void initWithFirstDatabase() { if (getDatabases().getSize() > 0) { databasesCombo.setSelectedIndex(0); } } @Override public String getName() { return "Select database"; } private void setDatabase(int index) { DatabaseConnectionItem item = (DatabaseConnectionItem) databases.getElementAt(index); if (item != null) { selectedDatabase = item.getDatabaseConnection(); System.out.format("selectedDatabase=%s%n", selectedDatabase.getName()); } } /** * 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. */ // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { databaseLabel = new javax.swing.JLabel(); databasesCombo = new javax.swing.JComboBox(); org.openide.awt.Mnemonics.setLocalizedText(databaseLabel, org.openide.util.NbBundle.getMessage(DatabaseChooserPanel.class, "DatabaseChooserPanel.databaseLabel.text")); // NOI18N databasesCombo.setModel(getDatabases()); databasesCombo.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { onDatabaseSelected(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(26, 26, 26) .addComponent(databaseLabel) .addGap(4, 4, 4) .addComponent(databasesCombo, 0, 639, Short.MAX_VALUE) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(57, 57, 57) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(databaseLabel) .addComponent(databasesCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(216, Short.MAX_VALUE)) ); }// </editor-fold>//GEN-END:initComponents private void onDatabaseSelected(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onDatabaseSelected if (evt.getSource() == databasesCombo) { if (databasesCombo.getSelectedIndex() < 0) { return; } setDatabase(databasesCombo.getSelectedIndex()); } }//GEN-LAST:event_onDatabaseSelected // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JLabel databaseLabel; private javax.swing.JComboBox databasesCombo; // End of variables declaration//GEN-END:variables }