/* Copyright 2004-2014 Jim Voris * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.qumasoft.guitools.qwin.dialog; import com.qumasoft.guitools.qwin.QWinFrame; import com.qumasoft.guitools.qwin.operation.OperationAddDirectory; import com.qumasoft.qvcslib.QVCSConstants; import com.qumasoft.qvcslib.QVCSException; import javax.swing.DefaultComboBoxModel; import javax.swing.JOptionPane; import javax.swing.SwingUtilities; /** * Add directory dialog. * * @author Jim Voris */ public class AddDirectoryDialog extends AbstractQWinCommandDialog { private static final long serialVersionUID = -368057130439452190L; private final OperationAddDirectory addDirectoryOperation; /** * Create an add directory dialog. * * @param parent the parent frame. * @param modal is this modal. * @param existingDirectories the list of existing directories. * @param operation the add directory operation that will do the work. */ public AddDirectoryDialog(java.awt.Frame parent, boolean modal, String[] existingDirectories, OperationAddDirectory operation) { super(parent, modal); initComponents(); DefaultComboBoxModel<String> comboModel = new DefaultComboBoxModel<>(); for (String existingDirectorie : existingDirectories) { comboModel.addElement(existingDirectorie); } addDirectoryComboBox.setModel(comboModel); addDirectoryOperation = operation; setFont(); center(); } /** * 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() { okButton = new javax.swing.JButton(); cancelButton = new javax.swing.JButton(); addDirectoryComboBox = new javax.swing.JComboBox(); chooseDirectory = new javax.swing.JLabel(); setTitle("Add Directory"); setResizable(false); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { closeDialog(evt); } }); okButton.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N okButton.setText(" OK "); okButton.setMaximumSize(new java.awt.Dimension(80, 25)); okButton.setMinimumSize(new java.awt.Dimension(80, 25)); okButton.setPreferredSize(new java.awt.Dimension(80, 25)); okButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { okButtonActionPerformed(evt); } }); cancelButton.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N cancelButton.setText("Cancel"); cancelButton.setMaximumSize(new java.awt.Dimension(80, 25)); cancelButton.setMinimumSize(new java.awt.Dimension(80, 25)); cancelButton.setPreferredSize(new java.awt.Dimension(80, 25)); cancelButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cancelButtonActionPerformed(evt); } }); addDirectoryComboBox.setEditable(true); addDirectoryComboBox.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N chooseDirectory.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N chooseDirectory.setText("Choose Directory to Add:"); org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .add(12, 12, 12) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(chooseDirectory, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 310, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false) .add(layout.createSequentialGroup() .add(okButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .add(cancelButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .add(addDirectoryComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 310, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))) .add(16, 16, 16)) ); layout.setVerticalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .add(12, 12, 12) .add(chooseDirectory) .add(4, 4, 4) .add(addDirectoryComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(4, 4, 4) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(okButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(cancelButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .addContainerGap(21, Short.MAX_VALUE)) ); pack(); }// </editor-fold>//GEN-END:initComponents private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_cancelButtonActionPerformed {//GEN-HEADEREND:event_cancelButtonActionPerformed closeDialog(null); }//GEN-LAST:event_cancelButtonActionPerformed private void okButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_okButtonActionPerformed {//GEN-HEADEREND:event_okButtonActionPerformed String addDirectory = getAddDirectory(); try { validateDirectory(addDirectory); if (addDirectory.length() > 0) { closeDialog(null); addDirectoryOperation.processDialogResult(getAddDirectory()); } else { JOptionPane.showMessageDialog(QWinFrame.getQWinFrame(), "The new directory name cannot be blank.", "Directory Name Required", JOptionPane.INFORMATION_MESSAGE); } } catch (final QVCSException e) { // Run the update on the Swing thread. Runnable later = new Runnable() { @Override public void run() { // Let the user know that the password change worked. JOptionPane.showMessageDialog(QWinFrame.getQWinFrame(), e.getLocalizedMessage(), "Invalid Choices", JOptionPane.WARNING_MESSAGE); } }; SwingUtilities.invokeLater(later); } }//GEN-LAST:event_okButtonActionPerformed /** * Closes the dialog */ private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog setVisible(false); dispose(); }//GEN-LAST:event_closeDialog public String getAddDirectory() { String directoryString = ""; String selectedDirectoryString = (String) addDirectoryComboBox.getModel().getSelectedItem(); if (selectedDirectoryString != null) { directoryString = selectedDirectoryString; } return directoryString; } @Override public void dismissDialog() { cancelButtonActionPerformed(null); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JComboBox addDirectoryComboBox; private javax.swing.JButton cancelButton; private javax.swing.JLabel chooseDirectory; private javax.swing.JButton okButton; // End of variables declaration//GEN-END:variables private void validateDirectory(String addDirectory) throws QVCSException { if (0 == addDirectory.compareTo(QVCSConstants.QVCS_BRANCH_ARCHIVES_DIRECTORY)) { throw new QVCSException("Invalid directory name. (It matches a directory that QVCS uses internally)"); } if (0 == addDirectory.compareTo(QVCSConstants.QVCS_CEMETERY_DIRECTORY)) { throw new QVCSException("Invalid directory name. (It matches a directory that QVCS uses internally)"); } } }