samples/apifest1/infrastructure/testing-template/src/org/netbeans/apifest/testingtemplate/TestingTemplatePanelVisual.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:52:45 +0200
changeset 52 4257f4cf226b
permissions -rw-r--r--
Adding samples from API fest to the repository, including pieces of their code in the document, not just links
jtulach@52
     1
package org.netbeans.apifest.testingtemplate;
jtulach@52
     2
jtulach@52
     3
import java.io.File;
jtulach@52
     4
import javax.swing.JFileChooser;
jtulach@52
     5
import javax.swing.JPanel;
jtulach@52
     6
import javax.swing.event.DocumentEvent;
jtulach@52
     7
import javax.swing.event.DocumentListener;
jtulach@52
     8
import javax.swing.text.Document;
jtulach@52
     9
import org.netbeans.spi.project.ui.support.ProjectChooser;
jtulach@52
    10
import org.openide.WizardDescriptor;
jtulach@52
    11
import org.openide.WizardValidationException;
jtulach@52
    12
import org.openide.filesystems.FileUtil;
jtulach@52
    13
jtulach@52
    14
public class TestingTemplatePanelVisual extends JPanel implements DocumentListener {
jtulach@52
    15
    
jtulach@52
    16
    public static final String PROP_PROJECT_NAME = "projectName";
jtulach@52
    17
    
jtulach@52
    18
    private TestingTemplateWizardPanel panel;
jtulach@52
    19
    
jtulach@52
    20
    /** Creates new form PanelProjectLocationVisual */
jtulach@52
    21
    public TestingTemplatePanelVisual(TestingTemplateWizardPanel panel) {
jtulach@52
    22
        initComponents();
jtulach@52
    23
        this.panel = panel;
jtulach@52
    24
        // Register listener on the textFields to make the automatic updates
jtulach@52
    25
        projectNameTextField.getDocument().addDocumentListener(this);
jtulach@52
    26
        projectLocationTextField.getDocument().addDocumentListener(this);
jtulach@52
    27
    }
jtulach@52
    28
    
jtulach@52
    29
    
jtulach@52
    30
    public String getProjectName() {
jtulach@52
    31
        return this.projectNameTextField.getText();
jtulach@52
    32
    }
jtulach@52
    33
    
jtulach@52
    34
    /** This method is called from within the constructor to
jtulach@52
    35
     * initialize the form.
jtulach@52
    36
     * WARNING: Do NOT modify this code. The content of this method is
jtulach@52
    37
     * always regenerated by the Form Editor.
jtulach@52
    38
     */
jtulach@52
    39
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
jtulach@52
    40
    private void initComponents() {
jtulach@52
    41
        projectNameLabel = new javax.swing.JLabel();
jtulach@52
    42
        projectNameTextField = new javax.swing.JTextField();
jtulach@52
    43
        projectLocationLabel = new javax.swing.JLabel();
jtulach@52
    44
        projectLocationTextField = new javax.swing.JTextField();
jtulach@52
    45
        browseButton = new javax.swing.JButton();
jtulach@52
    46
        createdFolderLabel = new javax.swing.JLabel();
jtulach@52
    47
        createdFolderTextField = new javax.swing.JTextField();
jtulach@52
    48
jtulach@52
    49
        projectNameLabel.setLabelFor(projectNameTextField);
jtulach@52
    50
        org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel, "Project &Name:");
jtulach@52
    51
jtulach@52
    52
        projectLocationLabel.setLabelFor(projectLocationTextField);
jtulach@52
    53
        org.openide.awt.Mnemonics.setLocalizedText(projectLocationLabel, "Project &Location:");
jtulach@52
    54
jtulach@52
    55
        org.openide.awt.Mnemonics.setLocalizedText(browseButton, "Br&owse...");
jtulach@52
    56
        browseButton.setActionCommand("BROWSE");
jtulach@52
    57
        browseButton.addActionListener(new java.awt.event.ActionListener() {
jtulach@52
    58
            public void actionPerformed(java.awt.event.ActionEvent evt) {
jtulach@52
    59
                browseButtonActionPerformed(evt);
jtulach@52
    60
            }
jtulach@52
    61
        });
jtulach@52
    62
jtulach@52
    63
        createdFolderLabel.setLabelFor(createdFolderTextField);
jtulach@52
    64
        org.openide.awt.Mnemonics.setLocalizedText(createdFolderLabel, "Project &Folder:");
jtulach@52
    65
jtulach@52
    66
        createdFolderTextField.setEditable(false);
jtulach@52
    67
jtulach@52
    68
        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
jtulach@52
    69
        this.setLayout(layout);
jtulach@52
    70
        layout.setHorizontalGroup(
jtulach@52
    71
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
jtulach@52
    72
            .add(layout.createSequentialGroup()
jtulach@52
    73
                .addContainerGap()
jtulach@52
    74
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
jtulach@52
    75
                    .add(projectNameLabel)
jtulach@52
    76
                    .add(projectLocationLabel)
jtulach@52
    77
                    .add(createdFolderLabel))
jtulach@52
    78
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
jtulach@52
    79
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
jtulach@52
    80
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, projectNameTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
jtulach@52
    81
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, projectLocationTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
jtulach@52
    82
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, createdFolderTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE))
jtulach@52
    83
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
jtulach@52
    84
                .add(browseButton)
jtulach@52
    85
                .addContainerGap())
jtulach@52
    86
        );
jtulach@52
    87
        layout.setVerticalGroup(
jtulach@52
    88
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
jtulach@52
    89
            .add(layout.createSequentialGroup()
jtulach@52
    90
                .addContainerGap()
jtulach@52
    91
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
jtulach@52
    92
                    .add(projectNameLabel)
jtulach@52
    93
                    .add(projectNameTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
jtulach@52
    94
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
jtulach@52
    95
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
jtulach@52
    96
                    .add(projectLocationLabel)
jtulach@52
    97
                    .add(projectLocationTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
jtulach@52
    98
                    .add(browseButton))
jtulach@52
    99
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
jtulach@52
   100
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
jtulach@52
   101
                    .add(createdFolderLabel)
jtulach@52
   102
                    .add(createdFolderTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
jtulach@52
   103
                .addContainerGap(213, Short.MAX_VALUE))
jtulach@52
   104
        );
jtulach@52
   105
    }// </editor-fold>//GEN-END:initComponents
jtulach@52
   106
    
jtulach@52
   107
    private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButtonActionPerformed
jtulach@52
   108
        String command = evt.getActionCommand();
jtulach@52
   109
        if ("BROWSE".equals(command)) {
jtulach@52
   110
            JFileChooser chooser = new JFileChooser();
jtulach@52
   111
            FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
jtulach@52
   112
            chooser.setDialogTitle("Select Project Location");
jtulach@52
   113
            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
jtulach@52
   114
            String path = this.projectLocationTextField.getText();
jtulach@52
   115
            if (path.length() > 0) {
jtulach@52
   116
                File f = new File(path);
jtulach@52
   117
                if (f.exists()) {
jtulach@52
   118
                    chooser.setSelectedFile(f);
jtulach@52
   119
                }
jtulach@52
   120
            }
jtulach@52
   121
            if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
jtulach@52
   122
                File projectDir = chooser.getSelectedFile();
jtulach@52
   123
                projectLocationTextField.setText(FileUtil.normalizeFile(projectDir).getAbsolutePath());
jtulach@52
   124
            }
jtulach@52
   125
            panel.fireChangeEvent();
jtulach@52
   126
        }
jtulach@52
   127
        
jtulach@52
   128
    }//GEN-LAST:event_browseButtonActionPerformed
jtulach@52
   129
    
jtulach@52
   130
    
jtulach@52
   131
    // Variables declaration - do not modify//GEN-BEGIN:variables
jtulach@52
   132
    private javax.swing.JButton browseButton;
jtulach@52
   133
    private javax.swing.JLabel createdFolderLabel;
jtulach@52
   134
    private javax.swing.JTextField createdFolderTextField;
jtulach@52
   135
    private javax.swing.JLabel projectLocationLabel;
jtulach@52
   136
    private javax.swing.JTextField projectLocationTextField;
jtulach@52
   137
    private javax.swing.JLabel projectNameLabel;
jtulach@52
   138
    private javax.swing.JTextField projectNameTextField;
jtulach@52
   139
    // End of variables declaration//GEN-END:variables
jtulach@52
   140
    
jtulach@52
   141
    public void addNotify() {
jtulach@52
   142
        super.addNotify();
jtulach@52
   143
        //same problem as in 31086, initial focus on Cancel button
jtulach@52
   144
        projectNameTextField.requestFocus();
jtulach@52
   145
    }
jtulach@52
   146
    
jtulach@52
   147
    boolean valid(WizardDescriptor wizardDescriptor) {
jtulach@52
   148
        
jtulach@52
   149
        if (projectNameTextField.getText().length() == 0) {
jtulach@52
   150
            wizardDescriptor.putProperty("WizardPanel_errorMessage",
jtulach@52
   151
                "Project Name is not a valid folder name.");
jtulach@52
   152
            return false; // Display name not specified
jtulach@52
   153
        }
jtulach@52
   154
        File f = FileUtil.normalizeFile(new File(projectLocationTextField.getText()).getAbsoluteFile());
jtulach@52
   155
        if (!f.isDirectory()) {
jtulach@52
   156
            String message = "Project Folder is not a valid path.";
jtulach@52
   157
            wizardDescriptor.putProperty("WizardPanel_errorMessage", message);
jtulach@52
   158
            return false;
jtulach@52
   159
        }
jtulach@52
   160
        final File destFolder = FileUtil.normalizeFile(new File(createdFolderTextField.getText()).getAbsoluteFile());
jtulach@52
   161
        
jtulach@52
   162
        File projLoc = destFolder;
jtulach@52
   163
        while (projLoc != null && !projLoc.exists()) {
jtulach@52
   164
            projLoc = projLoc.getParentFile();
jtulach@52
   165
        }
jtulach@52
   166
        if (projLoc == null || !projLoc.canWrite()) {
jtulach@52
   167
            wizardDescriptor.putProperty("WizardPanel_errorMessage",
jtulach@52
   168
                "Project Folder cannot be created.");
jtulach@52
   169
            return false;
jtulach@52
   170
        }
jtulach@52
   171
        
jtulach@52
   172
        if (FileUtil.toFileObject(projLoc) == null) {
jtulach@52
   173
            String message = "Project Folder is not a valid path.";
jtulach@52
   174
            wizardDescriptor.putProperty("WizardPanel_errorMessage", message);
jtulach@52
   175
            return false;
jtulach@52
   176
        }
jtulach@52
   177
        
jtulach@52
   178
        File[] kids = destFolder.listFiles();
jtulach@52
   179
        if (destFolder.exists() && kids != null && kids.length > 0) {
jtulach@52
   180
            // Folder exists and is not empty
jtulach@52
   181
            wizardDescriptor.putProperty("WizardPanel_errorMessage",
jtulach@52
   182
                "Project Folder already exists and is not empty.");
jtulach@52
   183
            return false;
jtulach@52
   184
        }
jtulach@52
   185
        wizardDescriptor.putProperty("WizardPanel_errorMessage", "");
jtulach@52
   186
        return true;
jtulach@52
   187
    }
jtulach@52
   188
    
jtulach@52
   189
    void store(WizardDescriptor d) {
jtulach@52
   190
        String name = projectNameTextField.getText().trim();
jtulach@52
   191
        String folder = createdFolderTextField.getText().trim();
jtulach@52
   192
        
jtulach@52
   193
        d.putProperty("projdir", new File(folder));
jtulach@52
   194
        d.putProperty("name", name);
jtulach@52
   195
    }
jtulach@52
   196
    
jtulach@52
   197
    void read(WizardDescriptor settings) {
jtulach@52
   198
        File projectLocation = (File) settings.getProperty("projdir");
jtulach@52
   199
        if (projectLocation == null || projectLocation.getParentFile() == null || !projectLocation.getParentFile().isDirectory()) {
jtulach@52
   200
            projectLocation = ProjectChooser.getProjectsFolder();
jtulach@52
   201
        } else {
jtulach@52
   202
            projectLocation = projectLocation.getParentFile();
jtulach@52
   203
        }
jtulach@52
   204
        this.projectLocationTextField.setText(projectLocation.getAbsolutePath());
jtulach@52
   205
        
jtulach@52
   206
        String projectName = (String) settings.getProperty("name");
jtulach@52
   207
        if(projectName == null) {
jtulach@52
   208
            projectName = "TestingTemplate";
jtulach@52
   209
        }
jtulach@52
   210
        this.projectNameTextField.setText(projectName);
jtulach@52
   211
        this.projectNameTextField.selectAll();
jtulach@52
   212
    }
jtulach@52
   213
    
jtulach@52
   214
    void validate(WizardDescriptor d) throws WizardValidationException {
jtulach@52
   215
        // nothing to validate
jtulach@52
   216
    }
jtulach@52
   217
    
jtulach@52
   218
    // Implementation of DocumentListener --------------------------------------
jtulach@52
   219
    
jtulach@52
   220
    public void changedUpdate(DocumentEvent e) {
jtulach@52
   221
        updateTexts(e);
jtulach@52
   222
        if (this.projectNameTextField.getDocument() == e.getDocument()) {
jtulach@52
   223
            firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText());
jtulach@52
   224
        }
jtulach@52
   225
    }
jtulach@52
   226
    
jtulach@52
   227
    public void insertUpdate(DocumentEvent e) {
jtulach@52
   228
        updateTexts(e);
jtulach@52
   229
        if (this.projectNameTextField.getDocument() == e.getDocument()) {
jtulach@52
   230
            firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText());
jtulach@52
   231
        }
jtulach@52
   232
    }
jtulach@52
   233
    
jtulach@52
   234
    public void removeUpdate(DocumentEvent e) {
jtulach@52
   235
        updateTexts(e);
jtulach@52
   236
        if (this.projectNameTextField.getDocument() == e.getDocument()) {
jtulach@52
   237
            firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText());
jtulach@52
   238
        }
jtulach@52
   239
    }
jtulach@52
   240
    
jtulach@52
   241
    /** Handles changes in the Project name and project directory, */
jtulach@52
   242
    private void updateTexts(DocumentEvent e) {
jtulach@52
   243
        
jtulach@52
   244
        Document doc = e.getDocument();
jtulach@52
   245
        
jtulach@52
   246
        if (doc == projectNameTextField.getDocument() || doc == projectLocationTextField.getDocument()) {
jtulach@52
   247
            // Change in the project name
jtulach@52
   248
            
jtulach@52
   249
            String projectName = projectNameTextField.getText();
jtulach@52
   250
            String projectFolder = projectLocationTextField.getText();
jtulach@52
   251
            
jtulach@52
   252
            //if (projectFolder.trim().length() == 0 || projectFolder.equals(oldName)) {
jtulach@52
   253
            createdFolderTextField.setText(projectFolder + File.separatorChar + projectName);
jtulach@52
   254
            //}
jtulach@52
   255
            
jtulach@52
   256
        }
jtulach@52
   257
        panel.fireChangeEvent(); // Notify that the panel changed
jtulach@52
   258
    }
jtulach@52
   259
    
jtulach@52
   260
}