/*
* 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.visualweb.dataconnectivity.ui;
import java.awt.Component;
import java.awt.Dialog;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
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.project.Project;
import org.netbeans.modules.visualweb.dataconnectivity.datasource.DataSourceResolver;
import org.netbeans.modules.visualweb.dataconnectivity.datasource.NoSelectedServerWarning;
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(Project currentProject) {
this.project = currentProject;
initComponents();
// add MainClassChooser
datasourceList.setModel(new ServerListModel(this.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 {
initServerList();
}
}
private void initServerList() {
datasourceList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
datasourceList.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
boolean enabled = !datasourceList.isSelectionEmpty();
firePropertyChange(OK_ENABLED, !enabled, enabled);
}
}
);
datasourceList.setCellRenderer(new ServersRenderer());
}
/**
* 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 currentProject
* @return serverInstanceId of the selected server instance, null
* if canceled.
*/
public static String selectDatabaseDialog(String title, String description, Project currentProject) {
NoSelectedDatabaseConnectionWarning panel = new NoSelectedDatabaseConnectionWarning(currentProject);
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;
}
/** 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.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_NoSelectedServerWarning_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_NoSuitableServerWarning_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_AddServer")); // 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;
BrokenDatabaseSupport.addDataSourceAndConnection(project, item);
}
((ServerListModel) 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"));
initServerList();
}
//refresh datasourceList
datasourceList.setCellRenderer(new ServersRenderer());
}//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 ServerListModel extends AbstractListModel {
private String[] instances;
private Project project;
public ServerListModel(Project currentProject) {
this.project = currentProject;
instances = DataSourceResolver.getInstance().getProblemDataSourceInstances(currentProject);
}
public synchronized int getSize() {
return instances.length;
}
public synchronized Object getElementAt(int index) {
if (index >= 0 && index < instances.length) {
return instances [index];
} else {
return null;
}
}
public synchronized void refreshModel() {
instances = DataSourceResolver.getInstance().getProblemDataSourceInstances(this.project);
}
}
private static final class ServersRenderer extends JLabel implements ListCellRenderer {
ServersRenderer() {
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;
}
}
}