package qubexplorer.ui; import java.util.Collections; import java.util.Comparator; import java.util.List; import javax.swing.DefaultComboBoxModel; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator; import org.jdesktop.swingx.autocomplete.ObjectToStringConverter; import qubexplorer.AuthorizationException; import qubexplorer.SonarQubeProjectConfiguration; import qubexplorer.UserCredentials; import qubexplorer.server.SonarQube; import qubexplorer.ui.task.Task; import qubexplorer.ui.task.TaskExecutionException; import qubexplorer.ui.task.TaskExecutor; /** * * @author Victor */ public class ServerConnectionDialog extends javax.swing.JDialog { public enum Option { ACCEPT, CANCEL } private Option option; /** * Creates new form ProyectChooser */ public ServerConnectionDialog(java.awt.Frame parent, boolean modal) { super(parent, modal); initComponents(); resourceCombox.setRenderer(new ProjectRenderer()); url.getDocument().addDocumentListener(new DocumentListener() { @Override public void insertUpdate(DocumentEvent de) { validateDialog(); } @Override public void removeUpdate(DocumentEvent de) { validateDialog(); } @Override public void changedUpdate(DocumentEvent de) { validateDialog(); } }); AutoCompleteDecorator.decorate(resourceCombox, new ObjectToStringConverter() { @Override public String getPreferredStringForItem(Object item) { String represantion = ""; if (item != null) { SonarQubeProjectConfiguration project = (SonarQubeProjectConfiguration) item; represantion = ProjectRenderer.toString(project); } return represantion; } }); } public Option showDialog() { validateDialog(); setLocationRelativeTo(getParent()); setVisible(true); return option; } public void setSelectedUrl(String selectedUrl) { url.setText(selectedUrl); } public String getSelectedUrl() { return url.getText(); } public UserCredentials getUserCredentials() { String username = user.getText(); return username.isEmpty() ? null : new UserCredentials(username, password.getPassword()); } public void setUserCredentials(UserCredentials userCredentials) { user.setText(userCredentials.getUsername()); password.setText(new String(userCredentials.getPassword())); } public SonarQubeProjectConfiguration getSelectedProject() { return resourceCombox.getSelectedItem() == null ? null : ((SonarQubeProjectConfiguration) resourceCombox.getSelectedItem()); } public void validateDialog() { boolean valid = resourceCombox.getSelectedItem() != null && url.getText() != null; okButton.setEnabled(valid); } public void loadProjectKeys() { Projects2Task projects2Task = new Projects2Task(new SonarQube(getSelectedUrl()), new ProjectContext(null, null)); projects2Task.setRetryIfNoAuthorization(false); UserCredentials userCredentials = getUserCredentials(); if (userCredentials != null) { projects2Task.setUserCredentials(userCredentials); } TaskExecutor.execute(projects2Task); } /** * 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() { jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); url = new javax.swing.JTextField(); resourceCombox = new javax.swing.JComboBox(); jButton1 = new javax.swing.JButton(); okButton = new javax.swing.JButton(); loadButton = new javax.swing.JButton(); jLabel3 = new javax.swing.JLabel(); user = new javax.swing.JTextField(); jLabel4 = new javax.swing.JLabel(); password = new javax.swing.JPasswordField(); credentialsWarning = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setTitle(org.openide.util.NbBundle.getMessage(ServerConnectionDialog.class, "ServerConnectionDialog.title")); // NOI18N setModal(true); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { formWindowClosing(evt); } }); org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(ServerConnectionDialog.class, "ServerConnectionDialog.jLabel1.text")); // NOI18N org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(ServerConnectionDialog.class, "ServerConnectionDialog.jLabel2.text")); // NOI18N url.setText(org.openide.util.NbBundle.getMessage(ServerConnectionDialog.class, "ServerConnectionDialog.url.text")); // NOI18N resourceCombox.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { resourceComboxItemStateChanged(evt); } }); org.openide.awt.Mnemonics.setLocalizedText(jButton1, org.openide.util.NbBundle.getMessage(ServerConnectionDialog.class, "ServerConnectionDialog.jButton1.text")); // NOI18N jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); org.openide.awt.Mnemonics.setLocalizedText(okButton, org.openide.util.NbBundle.getMessage(ServerConnectionDialog.class, "ServerConnectionDialog.okButton.text")); // NOI18N okButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { okButtonActionPerformed(evt); } }); org.openide.awt.Mnemonics.setLocalizedText(loadButton, org.openide.util.NbBundle.getMessage(ServerConnectionDialog.class, "ServerConnectionDialog.loadButton.text")); // NOI18N loadButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { loadButtonActionPerformed(evt); } }); org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(ServerConnectionDialog.class, "ServerConnectionDialog.jLabel3.text")); // NOI18N user.setColumns(20); user.setText(org.openide.util.NbBundle.getMessage(ServerConnectionDialog.class, "ServerConnectionDialog.user.text")); // NOI18N org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(ServerConnectionDialog.class, "ServerConnectionDialog.jLabel4.text")); // NOI18N password.setColumns(20); password.setText(org.openide.util.NbBundle.getMessage(ServerConnectionDialog.class, "ServerConnectionDialog.password.text")); // NOI18N credentialsWarning.setForeground(new java.awt.Color(204, 0, 0)); credentialsWarning.setHorizontalAlignment(javax.swing.SwingConstants.LEFT); org.openide.awt.Mnemonics.setLocalizedText(credentialsWarning, org.openide.util.NbBundle.getMessage(ServerConnectionDialog.class, "ServerConnectionDialog.credentialsWarning.text")); // NOI18N javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(url) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(loadButton)) .addGroup(layout.createSequentialGroup() .addGap(0, 0, Short.MAX_VALUE) .addComponent(okButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton1)) .addGroup(layout.createSequentialGroup() .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(resourceCombox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup() .addComponent(user, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(credentialsWarning, javax.swing.GroupLayout.DEFAULT_SIZE, 278, 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(jLabel1) .addComponent(url, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(loadButton)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3) .addComponent(user, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(credentialsWarning)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel4) .addComponent(password, 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(jLabel2) .addComponent(resourceCombox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 47, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton1) .addComponent(okButton)) .addContainerGap()) ); pack(); }// </editor-fold>//GEN-END:initComponents private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed option = Option.ACCEPT; setVisible(false); }//GEN-LAST:event_okButtonActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed option = Option.CANCEL; setVisible(false); }//GEN-LAST:event_jButton1ActionPerformed private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing option = Option.CANCEL; }//GEN-LAST:event_formWindowClosing private void loadButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadButtonActionPerformed loadProjectKeys(); }//GEN-LAST:event_loadButtonActionPerformed private void resourceComboxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_resourceComboxItemStateChanged validateDialog(); }//GEN-LAST:event_resourceComboxItemStateChanged // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JLabel credentialsWarning; private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JButton loadButton; private javax.swing.JButton okButton; private javax.swing.JPasswordField password; private javax.swing.JComboBox resourceCombox; private javax.swing.JTextField url; private javax.swing.JTextField user; // End of variables declaration//GEN-END:variables /*Is odd on this task to appear an additional prompt for credentials, maybe just show an error message in this dialog. */ public class Projects2Task extends Task<List<SonarQubeProjectConfiguration>> { private final SonarQube sonarQube; public Projects2Task(SonarQube sonarQube, ProjectContext projectContext) { super(projectContext, sonarQube.getServerUrl()); this.sonarQube = sonarQube; loadButton.setEnabled(false); } @Override protected void init() { credentialsWarning.setText(" "); } @Override public List<SonarQubeProjectConfiguration> execute() throws TaskExecutionException { return sonarQube.getProjects(ServerConnectionDialog.this.getUserCredentials()); } @Override protected void success(List<SonarQubeProjectConfiguration> result) { resourceCombox.removeAllItems(); Collections.sort(result, new Comparator<SonarQubeProjectConfiguration>() { @Override public int compare(SonarQubeProjectConfiguration t, SonarQubeProjectConfiguration t1) { return t.getName().compareToIgnoreCase(t1.getName()); } }); DefaultComboBoxModel model = (DefaultComboBoxModel) resourceCombox.getModel(); for (SonarQubeProjectConfiguration sonarProject : result) { model.addElement(sonarProject); } } @Override protected void fail(Throwable ex) { if (ex instanceof AuthorizationException) { credentialsWarning.setText("Please verify your user or password"); } else { super.fail(ex); } } @Override protected void destroy() { loadButton.setEnabled(true); } } }