/* * The contents of this file are subject to the terms of the Common Development * and Distribution License (the License). You may not use this file except in * compliance with the License. * * You can obtain a copy of the License at http://www.netbeans.org/cddl.html * or http://www.netbeans.org/cddl.txt. * * When distributing Covered Code, include this CDDL Header Notice in each file * and include the License file at http://www.netbeans.org/cddl.txt. * If applicable, add the following below the CDDL Header, with the fields * enclosed by brackets [] replaced by your own identifying information: * "Portions Copyrighted [year] [name of copyright owner]" * * The Original Software is NetBeans. The Initial Developer of the Original * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.modules.j2ee.common.ui; import java.awt.Component; import java.awt.Dialog; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import javax.swing.AbstractListModel; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.ListCellRenderer; import javax.swing.ListSelectionModel; import javax.swing.border.Border; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import org.netbeans.api.db.explorer.ConnectionListener; import org.netbeans.api.db.explorer.ConnectionManager; import org.netbeans.api.project.Project; import org.netbeans.modules.j2ee.deployment.common.api.Datasource; import org.openide.DialogDescriptor; import org.openide.DialogDisplayer; import org.openide.util.NbBundle; /** * Shows a warning that no server is set and allows choose it. * * @author Pavel Buzek, John Baker */ public final class NoSelectedDatabaseConnectionWarning extends JPanel { public static final String OK_ENABLED = "ok_enabled"; //NOI18N private static Dialog dlg = null; private final Border scrollPaneBorder; private Project project; private NoSelectedDatabaseConnectionWarning(Set brokenDataSources, Project project) { initComponents(); // add MainClassChooser this.project = project; datasourceList.setModel(new DatasourceListModel(brokenDataSources, project)); scrollPaneBorder = jScrollPane2.getBorder(); if (datasourceList.getModel().getSize() == 0) { datasourceList.setVisible(false); jScrollPane2.setBorder(null); jTextArea2.setText(NbBundle.getMessage(NoSelectedServerWarning.class, "LBL_NoSuitableDatabaseConnectionWarning_jLabel2")); } else { initDatasourceList(); } } private void initDatasourceList() { datasourceList.setCellRenderer(new DatasourceRenderer()); datasourceList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); datasourceList.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { boolean enabled = !datasourceList.isSelectionEmpty(); firePropertyChange(OK_ENABLED, !enabled, enabled); } } ); } /** * Show the "no selected server" dialog and let the user choose server instance from * the list. * * @param title dialog title * @param description dialog accessible description * @param brokenDatasources set of data sources with missing database connections * @param project * @return datasource name, null * if canceled. */ public static String selectDatabaseDialog(String title, String description, Set brokenDatasources, Project project) { NoSelectedDatabaseConnectionWarning panel = new NoSelectedDatabaseConnectionWarning(brokenDatasources, project); Object[] options = new Object[] { DialogDescriptor.OK_OPTION, DialogDescriptor.CANCEL_OPTION }; final DialogDescriptor desc = new DialogDescriptor(panel, title, true, options, DialogDescriptor.OK_OPTION, DialogDescriptor.DEFAULT_ALIGN, null, null); desc.setMessageType(DialogDescriptor.WARNING_MESSAGE); dlg = null; try { dlg = DialogDisplayer.getDefault().createDialog(desc); dlg.getAccessibleContext().setAccessibleDescription(description); panel.addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { if (evt.getPropertyName().equals(NoSelectedDatabaseConnectionWarning.OK_ENABLED)) { Object newvalue = evt.getNewValue(); if ((newvalue != null) && (newvalue instanceof Boolean)) { desc.setValid(((Boolean)newvalue).booleanValue()); } } } } ); desc.setValid(panel.getSelectedInstance() != null); panel.setSize(panel.getPreferredSize()); dlg.pack(); dlg.setVisible(true); } finally { if (dlg != null) { dlg.dispose(); } } return desc.getValue() == DialogDescriptor.OK_OPTION ? panel.getSelectedInstance() : null; } /** * * Return an instance of the dialog to dismiss, if list is empty * * @return instance of this dialog */ public static Dialog getNoSelectedDatabaseConnectionWarningDialog() { return dlg; } /** * * Refresh list of broken datasources * */ public void refresh() { ((DatasourceListModel) datasourceList.getModel()).refreshModel(); } /** Returns the selected server instance Id or null if no instance was selected. * * @return server instance ID or null if no instance is selected */ public String getSelectedInstance() { if (datasourceList.getSelectedIndex() == -1) { return null; } else { return (String)datasourceList.getSelectedValue(); } } /** 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. */ // //GEN-BEGIN:initComponents private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; jScrollPane2 = new javax.swing.JScrollPane(); datasourceList = new javax.swing.JList(); jTextArea1 = new javax.swing.JTextArea(); jTextArea2 = new javax.swing.JTextArea(); jButtonAddServer = new javax.swing.JButton(); setLayout(new java.awt.GridBagLayout()); jScrollPane2.setMinimumSize(new java.awt.Dimension(200, 100)); datasourceList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); datasourceList.setPreferredSize(null); datasourceList.setVerifyInputWhenFocusTarget(false); datasourceList.setVisibleRowCount(4); jScrollPane2.setViewportView(datasourceList); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 2; gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; gridBagConstraints.weightx = 1.0; gridBagConstraints.weighty = 1.0; gridBagConstraints.insets = new java.awt.Insets(6, 12, 12, 12); add(jScrollPane2, gridBagConstraints); jTextArea1.setColumns(25); jTextArea1.setEditable(false); jTextArea1.setLineWrap(true); jTextArea1.setText(NbBundle.getMessage(NoSelectedDatabaseConnectionWarning.class, "LBL_NoSelectedDatabaseConnectionWarning_jLabel1")); // NOI18N jTextArea1.setWrapStyleWord(true); jTextArea1.setFocusable(false); jTextArea1.setOpaque(false); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.insets = new java.awt.Insets(12, 12, 6, 12); add(jTextArea1, gridBagConstraints); jTextArea2.setEditable(false); jTextArea2.setLineWrap(true); jTextArea2.setText(org.openide.util.NbBundle.getMessage(NoSelectedDatabaseConnectionWarning.class, "LBL_NoSuitableDatabaseConnectionWarning_jLabel2")); // NOI18N jTextArea2.setWrapStyleWord(true); jTextArea2.setFocusable(false); jTextArea2.setOpaque(false); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(12, 12, 6, 12); add(jTextArea2, gridBagConstraints); org.openide.awt.Mnemonics.setLocalizedText(jButtonAddServer, org.openide.util.NbBundle.getMessage(NoSelectedDatabaseConnectionWarning.class, "LBL_AddDatabaseConnection")); // NOI18N jButtonAddServer.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonAddServerActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 3; gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.anchor = java.awt.GridBagConstraints.LAST_LINE_START; gridBagConstraints.insets = new java.awt.Insets(0, 12, 5, 0); add(jButtonAddServer, gridBagConstraints); jButtonAddServer.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(NoSelectedDatabaseConnectionWarning.class, "ACSN_AddServer")); // NOI18N jButtonAddServer.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(NoSelectedDatabaseConnectionWarning.class, "ACSD_AddServer")); // NOI18N }// //GEN-END:initComponents private void jButtonAddServerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddServerActionPerformed String itemSelected = (String)datasourceList.getSelectedValue(); String item = "defaultDataSource"; //NOI18N if (itemSelected != null) { item = itemSelected; Datasource brokenDatasource = BrokenDatabaseSupport.getBrokenDatasource(project, item); BrokenDatabaseSupport.addDatabaseConnection(brokenDatasource, item); } ((DatasourceListModel) datasourceList.getModel()).refreshModel(); if (datasourceList.getModel().getSize() > 0 && !datasourceList.isVisible()) { datasourceList.setVisible(true); jScrollPane2.setBorder(scrollPaneBorder); jTextArea2.setText(org.openide.util.NbBundle.getMessage(NoSelectedServerWarning.class, "LBL_NoSuitableDatabaseConnectionWarning_jLabel2")); initDatasourceList(); } }//GEN-LAST:event_jButtonAddServerActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JList datasourceList; private javax.swing.JButton jButtonAddServer; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JTextArea jTextArea1; private javax.swing.JTextArea jTextArea2; // End of variables declaration//GEN-END:variables private static final class DatasourceListModel extends AbstractListModel implements ConnectionListener { private Set instances; private Project project; public DatasourceListModel(Set brokenDatasources, Project project) { Iterator it = brokenDatasources.iterator(); int i = 0; String name; this.project = project; instances = new HashSet(); addConnectionListener(); while (it.hasNext()) { name = ((Datasource)it.next()).getJndiName(); instances.add(name.substring(name.indexOf("/")+1)); } } public synchronized int getSize() { return instances.size(); } public synchronized Object getElementAt(int index) { Iterator it = instances.iterator(); int i = 0; while (it.hasNext()) { if (index >= 0 && index < instances.size()) { if (i == index) { return it.next(); } i++; } } return null; } public synchronized void refreshModel() { Set dss = BrokenDatabaseSupport.getBrokenDatasources(project); Iterator it = dss.iterator(); int i = 0; String name; while (it.hasNext()) { name = ((Datasource)it.next()).getJndiName(); instances.add(name.substring(name.indexOf("/")+1)); } if (dss.isEmpty()) { removeConnectionListener(); NoSelectedDatabaseConnectionWarning.getNoSelectedDatabaseConnectionWarningDialog().dispose(); } } // Listen for any connections changed in DB Explorer and refresh model accordingly public void connectionsChanged() { refreshModel(); } private void addConnectionListener() { ConnectionManager.getDefault().addConnectionListener(this); } private void removeConnectionListener() { ConnectionManager.getDefault().removeConnectionListener(this); } } private static final class DatasourceRenderer extends JLabel implements ListCellRenderer { DatasourceRenderer() { setOpaque(true); } public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { if (value instanceof String) { setText((String)value); } else { setText(value.toString()); setIcon(null); } if (isSelected) { setBackground(list.getSelectionBackground()); setForeground(list.getSelectionForeground()); } else { setBackground(list.getBackground()); setForeground(list.getForeground()); } return this; } } }