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