samples/apifest1/infrastructure/testing-template/src/org/netbeans/apifest/testingtemplate/TestingTemplatePanelVisual.java
changeset 52 4257f4cf226b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/apifest1/infrastructure/testing-template/src/org/netbeans/apifest/testingtemplate/TestingTemplatePanelVisual.java	Sat Jun 14 09:52:45 2008 +0200
     1.3 @@ -0,0 +1,260 @@
     1.4 +package org.netbeans.apifest.testingtemplate;
     1.5 +
     1.6 +import java.io.File;
     1.7 +import javax.swing.JFileChooser;
     1.8 +import javax.swing.JPanel;
     1.9 +import javax.swing.event.DocumentEvent;
    1.10 +import javax.swing.event.DocumentListener;
    1.11 +import javax.swing.text.Document;
    1.12 +import org.netbeans.spi.project.ui.support.ProjectChooser;
    1.13 +import org.openide.WizardDescriptor;
    1.14 +import org.openide.WizardValidationException;
    1.15 +import org.openide.filesystems.FileUtil;
    1.16 +
    1.17 +public class TestingTemplatePanelVisual extends JPanel implements DocumentListener {
    1.18 +    
    1.19 +    public static final String PROP_PROJECT_NAME = "projectName";
    1.20 +    
    1.21 +    private TestingTemplateWizardPanel panel;
    1.22 +    
    1.23 +    /** Creates new form PanelProjectLocationVisual */
    1.24 +    public TestingTemplatePanelVisual(TestingTemplateWizardPanel panel) {
    1.25 +        initComponents();
    1.26 +        this.panel = panel;
    1.27 +        // Register listener on the textFields to make the automatic updates
    1.28 +        projectNameTextField.getDocument().addDocumentListener(this);
    1.29 +        projectLocationTextField.getDocument().addDocumentListener(this);
    1.30 +    }
    1.31 +    
    1.32 +    
    1.33 +    public String getProjectName() {
    1.34 +        return this.projectNameTextField.getText();
    1.35 +    }
    1.36 +    
    1.37 +    /** This method is called from within the constructor to
    1.38 +     * initialize the form.
    1.39 +     * WARNING: Do NOT modify this code. The content of this method is
    1.40 +     * always regenerated by the Form Editor.
    1.41 +     */
    1.42 +    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    1.43 +    private void initComponents() {
    1.44 +        projectNameLabel = new javax.swing.JLabel();
    1.45 +        projectNameTextField = new javax.swing.JTextField();
    1.46 +        projectLocationLabel = new javax.swing.JLabel();
    1.47 +        projectLocationTextField = new javax.swing.JTextField();
    1.48 +        browseButton = new javax.swing.JButton();
    1.49 +        createdFolderLabel = new javax.swing.JLabel();
    1.50 +        createdFolderTextField = new javax.swing.JTextField();
    1.51 +
    1.52 +        projectNameLabel.setLabelFor(projectNameTextField);
    1.53 +        org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel, "Project &Name:");
    1.54 +
    1.55 +        projectLocationLabel.setLabelFor(projectLocationTextField);
    1.56 +        org.openide.awt.Mnemonics.setLocalizedText(projectLocationLabel, "Project &Location:");
    1.57 +
    1.58 +        org.openide.awt.Mnemonics.setLocalizedText(browseButton, "Br&owse...");
    1.59 +        browseButton.setActionCommand("BROWSE");
    1.60 +        browseButton.addActionListener(new java.awt.event.ActionListener() {
    1.61 +            public void actionPerformed(java.awt.event.ActionEvent evt) {
    1.62 +                browseButtonActionPerformed(evt);
    1.63 +            }
    1.64 +        });
    1.65 +
    1.66 +        createdFolderLabel.setLabelFor(createdFolderTextField);
    1.67 +        org.openide.awt.Mnemonics.setLocalizedText(createdFolderLabel, "Project &Folder:");
    1.68 +
    1.69 +        createdFolderTextField.setEditable(false);
    1.70 +
    1.71 +        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
    1.72 +        this.setLayout(layout);
    1.73 +        layout.setHorizontalGroup(
    1.74 +            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
    1.75 +            .add(layout.createSequentialGroup()
    1.76 +                .addContainerGap()
    1.77 +                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
    1.78 +                    .add(projectNameLabel)
    1.79 +                    .add(projectLocationLabel)
    1.80 +                    .add(createdFolderLabel))
    1.81 +                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
    1.82 +                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
    1.83 +                    .add(org.jdesktop.layout.GroupLayout.TRAILING, projectNameTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
    1.84 +                    .add(org.jdesktop.layout.GroupLayout.TRAILING, projectLocationTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
    1.85 +                    .add(org.jdesktop.layout.GroupLayout.TRAILING, createdFolderTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE))
    1.86 +                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
    1.87 +                .add(browseButton)
    1.88 +                .addContainerGap())
    1.89 +        );
    1.90 +        layout.setVerticalGroup(
    1.91 +            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
    1.92 +            .add(layout.createSequentialGroup()
    1.93 +                .addContainerGap()
    1.94 +                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
    1.95 +                    .add(projectNameLabel)
    1.96 +                    .add(projectNameTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
    1.97 +                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
    1.98 +                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
    1.99 +                    .add(projectLocationLabel)
   1.100 +                    .add(projectLocationTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
   1.101 +                    .add(browseButton))
   1.102 +                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
   1.103 +                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
   1.104 +                    .add(createdFolderLabel)
   1.105 +                    .add(createdFolderTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
   1.106 +                .addContainerGap(213, Short.MAX_VALUE))
   1.107 +        );
   1.108 +    }// </editor-fold>//GEN-END:initComponents
   1.109 +    
   1.110 +    private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButtonActionPerformed
   1.111 +        String command = evt.getActionCommand();
   1.112 +        if ("BROWSE".equals(command)) {
   1.113 +            JFileChooser chooser = new JFileChooser();
   1.114 +            FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
   1.115 +            chooser.setDialogTitle("Select Project Location");
   1.116 +            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
   1.117 +            String path = this.projectLocationTextField.getText();
   1.118 +            if (path.length() > 0) {
   1.119 +                File f = new File(path);
   1.120 +                if (f.exists()) {
   1.121 +                    chooser.setSelectedFile(f);
   1.122 +                }
   1.123 +            }
   1.124 +            if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
   1.125 +                File projectDir = chooser.getSelectedFile();
   1.126 +                projectLocationTextField.setText(FileUtil.normalizeFile(projectDir).getAbsolutePath());
   1.127 +            }
   1.128 +            panel.fireChangeEvent();
   1.129 +        }
   1.130 +        
   1.131 +    }//GEN-LAST:event_browseButtonActionPerformed
   1.132 +    
   1.133 +    
   1.134 +    // Variables declaration - do not modify//GEN-BEGIN:variables
   1.135 +    private javax.swing.JButton browseButton;
   1.136 +    private javax.swing.JLabel createdFolderLabel;
   1.137 +    private javax.swing.JTextField createdFolderTextField;
   1.138 +    private javax.swing.JLabel projectLocationLabel;
   1.139 +    private javax.swing.JTextField projectLocationTextField;
   1.140 +    private javax.swing.JLabel projectNameLabel;
   1.141 +    private javax.swing.JTextField projectNameTextField;
   1.142 +    // End of variables declaration//GEN-END:variables
   1.143 +    
   1.144 +    public void addNotify() {
   1.145 +        super.addNotify();
   1.146 +        //same problem as in 31086, initial focus on Cancel button
   1.147 +        projectNameTextField.requestFocus();
   1.148 +    }
   1.149 +    
   1.150 +    boolean valid(WizardDescriptor wizardDescriptor) {
   1.151 +        
   1.152 +        if (projectNameTextField.getText().length() == 0) {
   1.153 +            wizardDescriptor.putProperty("WizardPanel_errorMessage",
   1.154 +                "Project Name is not a valid folder name.");
   1.155 +            return false; // Display name not specified
   1.156 +        }
   1.157 +        File f = FileUtil.normalizeFile(new File(projectLocationTextField.getText()).getAbsoluteFile());
   1.158 +        if (!f.isDirectory()) {
   1.159 +            String message = "Project Folder is not a valid path.";
   1.160 +            wizardDescriptor.putProperty("WizardPanel_errorMessage", message);
   1.161 +            return false;
   1.162 +        }
   1.163 +        final File destFolder = FileUtil.normalizeFile(new File(createdFolderTextField.getText()).getAbsoluteFile());
   1.164 +        
   1.165 +        File projLoc = destFolder;
   1.166 +        while (projLoc != null && !projLoc.exists()) {
   1.167 +            projLoc = projLoc.getParentFile();
   1.168 +        }
   1.169 +        if (projLoc == null || !projLoc.canWrite()) {
   1.170 +            wizardDescriptor.putProperty("WizardPanel_errorMessage",
   1.171 +                "Project Folder cannot be created.");
   1.172 +            return false;
   1.173 +        }
   1.174 +        
   1.175 +        if (FileUtil.toFileObject(projLoc) == null) {
   1.176 +            String message = "Project Folder is not a valid path.";
   1.177 +            wizardDescriptor.putProperty("WizardPanel_errorMessage", message);
   1.178 +            return false;
   1.179 +        }
   1.180 +        
   1.181 +        File[] kids = destFolder.listFiles();
   1.182 +        if (destFolder.exists() && kids != null && kids.length > 0) {
   1.183 +            // Folder exists and is not empty
   1.184 +            wizardDescriptor.putProperty("WizardPanel_errorMessage",
   1.185 +                "Project Folder already exists and is not empty.");
   1.186 +            return false;
   1.187 +        }
   1.188 +        wizardDescriptor.putProperty("WizardPanel_errorMessage", "");
   1.189 +        return true;
   1.190 +    }
   1.191 +    
   1.192 +    void store(WizardDescriptor d) {
   1.193 +        String name = projectNameTextField.getText().trim();
   1.194 +        String folder = createdFolderTextField.getText().trim();
   1.195 +        
   1.196 +        d.putProperty("projdir", new File(folder));
   1.197 +        d.putProperty("name", name);
   1.198 +    }
   1.199 +    
   1.200 +    void read(WizardDescriptor settings) {
   1.201 +        File projectLocation = (File) settings.getProperty("projdir");
   1.202 +        if (projectLocation == null || projectLocation.getParentFile() == null || !projectLocation.getParentFile().isDirectory()) {
   1.203 +            projectLocation = ProjectChooser.getProjectsFolder();
   1.204 +        } else {
   1.205 +            projectLocation = projectLocation.getParentFile();
   1.206 +        }
   1.207 +        this.projectLocationTextField.setText(projectLocation.getAbsolutePath());
   1.208 +        
   1.209 +        String projectName = (String) settings.getProperty("name");
   1.210 +        if(projectName == null) {
   1.211 +            projectName = "TestingTemplate";
   1.212 +        }
   1.213 +        this.projectNameTextField.setText(projectName);
   1.214 +        this.projectNameTextField.selectAll();
   1.215 +    }
   1.216 +    
   1.217 +    void validate(WizardDescriptor d) throws WizardValidationException {
   1.218 +        // nothing to validate
   1.219 +    }
   1.220 +    
   1.221 +    // Implementation of DocumentListener --------------------------------------
   1.222 +    
   1.223 +    public void changedUpdate(DocumentEvent e) {
   1.224 +        updateTexts(e);
   1.225 +        if (this.projectNameTextField.getDocument() == e.getDocument()) {
   1.226 +            firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText());
   1.227 +        }
   1.228 +    }
   1.229 +    
   1.230 +    public void insertUpdate(DocumentEvent e) {
   1.231 +        updateTexts(e);
   1.232 +        if (this.projectNameTextField.getDocument() == e.getDocument()) {
   1.233 +            firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText());
   1.234 +        }
   1.235 +    }
   1.236 +    
   1.237 +    public void removeUpdate(DocumentEvent e) {
   1.238 +        updateTexts(e);
   1.239 +        if (this.projectNameTextField.getDocument() == e.getDocument()) {
   1.240 +            firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText());
   1.241 +        }
   1.242 +    }
   1.243 +    
   1.244 +    /** Handles changes in the Project name and project directory, */
   1.245 +    private void updateTexts(DocumentEvent e) {
   1.246 +        
   1.247 +        Document doc = e.getDocument();
   1.248 +        
   1.249 +        if (doc == projectNameTextField.getDocument() || doc == projectLocationTextField.getDocument()) {
   1.250 +            // Change in the project name
   1.251 +            
   1.252 +            String projectName = projectNameTextField.getText();
   1.253 +            String projectFolder = projectLocationTextField.getText();
   1.254 +            
   1.255 +            //if (projectFolder.trim().length() == 0 || projectFolder.equals(oldName)) {
   1.256 +            createdFolderTextField.setText(projectFolder + File.separatorChar + projectName);
   1.257 +            //}
   1.258 +            
   1.259 +        }
   1.260 +        panel.fireChangeEvent(); // Notify that the panel changed
   1.261 +    }
   1.262 +    
   1.263 +}