/*
* The MIT License
*
* Copyright 2013 Tim Boudreau.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.timboudreau.netbeans.mongodb;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.regex.Pattern;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.JTextComponent;
import org.openide.util.ChangeSupport;
import org.openide.util.NbBundle.Messages;
/**
*
* @author Tim Boudreau
*/
public class NewConnectionPanel extends javax.swing.JPanel implements DocumentListener, FocusListener {
private final ChangeSupport supp = new ChangeSupport(this);
public void addChangeListener(ChangeListener listener) {
supp.addChangeListener(listener);
}
public void removeChangeListener(ChangeListener listener) {
supp.removeChangeListener(listener);
}
/**
* Creates new form NewConnectionPanel
*/
public NewConnectionPanel() {
initComponents();
nameField.addFocusListener(this);
portField.addFocusListener(this);
hostField.addFocusListener(this);
}
/**
* 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() {
hostLabel = new javax.swing.JLabel();
hostField = new javax.swing.JTextField();
portLabel = new javax.swing.JLabel();
portField = new javax.swing.JTextField();
problemLabel = new javax.swing.JLabel();
nameLabel = new javax.swing.JLabel();
nameField = new javax.swing.JTextField();
hostLabel.setLabelFor(hostField);
org.openide.awt.Mnemonics.setLocalizedText(hostLabel, org.openide.util.NbBundle.getMessage(NewConnectionPanel.class, "NewConnectionPanel.hostLabel.text")); // NOI18N
hostField.setText(org.openide.util.NbBundle.getMessage(NewConnectionPanel.class, "NewConnectionPanel.hostField.text")); // NOI18N
portLabel.setLabelFor(portField);
org.openide.awt.Mnemonics.setLocalizedText(portLabel, org.openide.util.NbBundle.getMessage(NewConnectionPanel.class, "NewConnectionPanel.portLabel.text")); // NOI18N
portField.setText(org.openide.util.NbBundle.getMessage(NewConnectionPanel.class, "NewConnectionPanel.portField.text")); // NOI18N
problemLabel.setForeground(javax.swing.UIManager.getDefaults().getColor("nb.errorForeground"));
org.openide.awt.Mnemonics.setLocalizedText(problemLabel, org.openide.util.NbBundle.getMessage(NewConnectionPanel.class, "NewConnectionPanel.problemLabel.text")); // NOI18N
nameLabel.setLabelFor(nameField);
org.openide.awt.Mnemonics.setLocalizedText(nameLabel, org.openide.util.NbBundle.getMessage(NewConnectionPanel.class, "NewConnectionPanel.nameLabel.text")); // NOI18N
nameField.setText(org.openide.util.NbBundle.getMessage(NewConnectionPanel.class, "NewConnectionPanel.nameField.text")); // NOI18N
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(problemLabel)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(nameLabel)
.addComponent(hostLabel)
.addComponent(portLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(portField)
.addComponent(hostField)
.addComponent(nameField, javax.swing.GroupLayout.DEFAULT_SIZE, 469, Short.MAX_VALUE))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(nameLabel)
.addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(hostLabel)
.addComponent(hostField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(portField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(portLabel))
.addGap(42, 42, 42)
.addComponent(problemLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
}// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JTextField hostField;
private javax.swing.JLabel hostLabel;
private javax.swing.JTextField nameField;
private javax.swing.JLabel nameLabel;
private javax.swing.JTextField portField;
private javax.swing.JLabel portLabel;
private javax.swing.JLabel problemLabel;
// End of variables declaration//GEN-END:variables
@Override
public void insertUpdate(DocumentEvent e) {
changedUpdate(e);
}
@Override
@Messages("HostIsEmpty=Host not set")
public void removeUpdate(DocumentEvent e) {
changedUpdate(e);
}
@Messages("PortInvalid=Invalid port")
public int getPort() {
return Integer.parseInt(portField.getText().trim());
}
@Messages("HostInvalid=Invalid host")
public String getHost() {
return hostField.getText().trim();
}
private boolean ok = true;
@Messages("PortIsNegative=Port cannot be negative")
private void setOk(boolean ok) {
if (ok != this.ok) {
this.ok = ok;
supp.fireChange();
}
}
@Messages("PortIsZero=Port cannot be zero")
public boolean isOk() {
return ok;
}
@Override
@Messages("PortNotSet=Invalid port")
public void changedUpdate(DocumentEvent e) {
String portString = portField.getText().trim();
if (portString.isEmpty()) {
setProblem(Bundle.PortNotSet());
return;
}
try {
int port = Integer.parseInt(portString);
if (port == 0) {
setProblem(Bundle.PortIsZero());
return;
}
if (port < 0) {
setProblem(Bundle.PortIsNegative());
return;
}
} catch (NumberFormatException nfe) {
setProblem(Bundle.PortInvalid());
return;
}
String host = getHost();
if (host.isEmpty()) {
setProblem(Bundle.HostIsEmpty());
return;
} else {
Pattern validIp = Pattern.compile("^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\\-]*[A-Za-z0-9])$");
Pattern validHost = Pattern.compile("^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\\-]*[A-Za-z0-9])$");
if (!validIp.matcher(host).matches() && !validHost.matcher(host).matches()) {
setProblem(Bundle.HostInvalid());
return;
}
}
setProblem(null);
}
private void setProblem(String problem) {
if (problem == null) {
problemLabel.setText(" ");
setOk(true);
} else {
problemLabel.setText(problem);
setOk(false);
}
}
@Override
public void focusGained(FocusEvent e) {
((JTextComponent) e.getComponent()).selectAll();
}
@Override
public void focusLost(FocusEvent e) {
// do nothing
}
String getConnectionName() {
return nameField.getText().trim();
}
}