ada.project/src/org/netbeans/modules/ada/project/ui/wizards/ExistingAdaProjectPanelVisual.java
author Andrea Lucarelli <raster@netbeans.org>
Sun, 22 Aug 2010 23:37:11 +0200
branchrelease68
changeset 16367 d2820c029d3a
parent 15779 367c7fdb5d23
permissions -rw-r--r--
Add JVM compiler support.
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
     5  *
     6  * The contents of this file are subject to the terms of either the GNU
     7  * General Public License Version 2 only ("GPL") or the Common
     8  * Development and Distribution License("CDDL") (collectively, the
     9  * "License"). You may not use this file except in compliance with the
    10  * License. You can obtain a copy of the License at
    11  * http://www.netbeans.org/cddl-gplv2.html
    12  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    13  * specific language governing permissions and limitations under the
    14  * License.  When distributing the software, include this License Header
    15  * Notice in each file and include the License file at
    16  * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    17  * particular file as subject to the "Classpath" exception as provided
    18  * by Sun in the GPL Version 2 section of the License file that
    19  * accompanied this code. If applicable, add the following below the
    20  * License Header, with the fields enclosed by brackets [] replaced by
    21  * your own identifying information:
    22  * "Portions Copyrighted [year] [name of copyright owner]"
    23  *
    24  * If you wish your version of this file to be governed by only the CDDL
    25  * or only the GPL Version 2, indicate your decision by adding
    26  * "[Contributor] elects to include this software in this distribution
    27  * under the [CDDL or GPL Version 2] license." If you do not indicate a
    28  * single choice of license, a recipient has the option to distribute
    29  * your version of this file under either the CDDL, the GPL Version 2 or
    30  * to extend the choice of license to its licensees as provided above.
    31  * However, if you add GPL Version 2 code and therefore, elected the GPL
    32  * Version 2 license, then the option applies only if the new code is
    33  * made subject to such option by the copyright holder.
    34  *
    35  * Contributor(s):
    36  *
    37  * Portions Copyrighted 2008 Sun Microsystems, Inc.
    38  */
    39 
    40 package org.netbeans.modules.ada.project.ui.wizards;
    41 
    42 import java.io.File;
    43 import javax.swing.JFileChooser;
    44 import javax.swing.event.DocumentEvent;
    45 import javax.swing.event.DocumentListener;
    46 import javax.swing.text.Document;
    47 import org.netbeans.spi.project.ui.support.ProjectChooser;
    48 import org.openide.WizardDescriptor;
    49 import org.openide.WizardValidationException;
    50 import org.openide.filesystems.FileUtil;
    51 import org.openide.util.NbBundle;
    52 
    53 /**
    54  *
    55  * @author Andrea Lucarelli
    56  */
    57 class ExistingAdaProjectPanelVisual extends SettingsPanel implements DocumentListener {
    58 
    59     private PanelConfigureProject panel;
    60 
    61     /**
    62      *
    63      * Creates new form ExistingAdaProjectPanelVisual
    64      */
    65     public ExistingAdaProjectPanelVisual(final PanelConfigureProject panel) {
    66         assert panel != null;
    67         this.panel = panel;
    68         initComponents();
    69         postInitComponents();
    70     }
    71     
    72     private void postInitComponents () {
    73         this.projectFolderTextField.getDocument().addDocumentListener(this);
    74         this.projectNameTextField.getDocument().addDocumentListener(this);
    75     }
    76 
    77     /** This method is called from within the constructor to
    78      * initialize the form.
    79      * WARNING: Do NOT modify this code. The content of this method is
    80      * always regenerated by the Form Editor.
    81      */
    82     @SuppressWarnings("unchecked")
    83     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    84     private void initComponents() {
    85 
    86         jLabel1 = new javax.swing.JLabel();
    87         jLabel2 = new javax.swing.JLabel();
    88         projectNameTextField = new javax.swing.JTextField();
    89         projectFolderTextField = new javax.swing.JTextField();
    90         browse = new javax.swing.JButton();
    91 
    92         jLabel1.setLabelFor(projectNameTextField);
    93         org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(ExistingAdaProjectPanelVisual.class, "ExistingAdaProjectPanelVisual.projectName.text")); // NOI18N
    94 
    95         org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(ExistingAdaProjectPanelVisual.class, "ExistingAdaProjectPanelVisual.projectFolder.text")); // NOI18N
    96 
    97         org.openide.awt.Mnemonics.setLocalizedText(browse, org.openide.util.NbBundle.getMessage(ExistingAdaProjectPanelVisual.class, "ExistingAdaProjectPanelVisual.browse.text")); // NOI18N
    98         browse.addActionListener(new java.awt.event.ActionListener() {
    99             public void actionPerformed(java.awt.event.ActionEvent evt) {
   100                 browseAction(evt);
   101             }
   102         });
   103 
   104         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
   105         this.setLayout(layout);
   106         layout.setHorizontalGroup(
   107             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
   108             .add(layout.createSequentialGroup()
   109                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
   110                     .add(jLabel2)
   111                     .add(jLabel1))
   112                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
   113                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
   114                     .add(layout.createSequentialGroup()
   115                         .add(projectFolderTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
   116                         .add(1, 1, 1)
   117                         .add(browse))
   118                     .add(projectNameTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE))
   119                 .addContainerGap())
   120         );
   121         layout.setVerticalGroup(
   122             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
   123             .add(layout.createSequentialGroup()
   124                 .addContainerGap()
   125                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
   126                     .add(jLabel1)
   127                     .add(projectNameTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
   128                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
   129                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
   130                     .add(jLabel2)
   131                     .add(projectFolderTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
   132                     .add(browse))
   133                 .addContainerGap(29, Short.MAX_VALUE))
   134         );
   135     }// </editor-fold>//GEN-END:initComponents
   136 
   137     private void browseAction(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseAction
   138         JFileChooser chooser = new JFileChooser();
   139         FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
   140         chooser.setDialogTitle(NbBundle.getMessage(ExistingAdaProjectPanelVisual.class, "LBL_SelectProjectFolder"));
   141         chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
   142         String path = this.projectFolderTextField.getText();
   143         if (path.length() > 0) {
   144             File f = new File(path);
   145             if (f.exists()) {
   146                 chooser.setSelectedFile(f);
   147             }
   148         }
   149         if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
   150             File projectDir = chooser.getSelectedFile();
   151             projectFolderTextField.setText(FileUtil.normalizeFile(projectDir).getAbsolutePath());
   152         }
   153         panel.fireChangeEvent();
   154     }//GEN-LAST:event_browseAction
   155 
   156 
   157     // Variables declaration - do not modify//GEN-BEGIN:variables
   158     private javax.swing.JButton browse;
   159     private javax.swing.JLabel jLabel1;
   160     private javax.swing.JLabel jLabel2;
   161     private javax.swing.JTextField projectFolderTextField;
   162     private javax.swing.JTextField projectNameTextField;
   163     // End of variables declaration//GEN-END:variables
   164 
   165     @Override
   166     void store(WizardDescriptor settings) {
   167         assert  settings != null;
   168         String name = projectNameTextField.getText().trim();
   169         String folder = projectFolderTextField.getText().trim();
   170         settings.putProperty(NewAdaProjectWizardIterator.PROP_PROJECT_LOCATION, FileUtil.normalizeFile(new File(folder)));
   171         settings.putProperty(NewAdaProjectWizardIterator.PROP_PROJECT_NAME, name);
   172     }
   173 
   174     @Override
   175     void read(WizardDescriptor settings) {
   176         File projectLocation = (File) settings.getProperty(NewAdaProjectWizardIterator.PROP_PROJECT_LOCATION);
   177         String defaultName = null;
   178         if (projectLocation == null || projectLocation.getParentFile() == null || !projectLocation.getParentFile().isDirectory()) {
   179             defaultName = NewAdaProjectWizardIterator.getFreeFolderName(ProjectChooser.getProjectsFolder(), "AdaApplication");     //NOI18N
   180             projectLocation = new File (ProjectChooser.getProjectsFolder(),defaultName);
   181         }
   182         this.projectFolderTextField.setText(projectLocation.getAbsolutePath());
   183 
   184         String projectName = (String) settings.getProperty(NewAdaProjectWizardIterator.PROP_PROJECT_NAME);
   185         if (projectName == null) {
   186             assert defaultName != null;
   187             projectName = defaultName;
   188         }
   189         this.projectNameTextField.setText(projectName);
   190         this.projectNameTextField.selectAll();
   191     }
   192 
   193     @Override
   194     boolean valid(WizardDescriptor settings) {
   195         if (projectNameTextField.getText().length() == 0) {
   196             settings.putProperty("WizardPanel_errorMessage",
   197                     NbBundle.getMessage(ExistingAdaProjectPanelVisual.class, "ERR_WrongName"));
   198             return false; // Display name not specified
   199         }
   200         File f = FileUtil.normalizeFile(new File(projectFolderTextField.getText()).getAbsoluteFile());
   201         if (f.exists() && !f.isDirectory()) {
   202             String message = NbBundle.getMessage(ExistingAdaProjectPanelVisual.class, "ERR_WrongProjectFolder");
   203             settings.putProperty("WizardPanel_errorMessage", message);
   204             return false;
   205         }
   206 
   207         if (f.exists() && !f.canWrite()) {
   208             settings.putProperty("WizardPanel_errorMessage",
   209                     NbBundle.getMessage(ExistingAdaProjectPanelVisual.class, "ERR_ReadOnlyProjectFolder"));
   210             return false;
   211         }
   212 
   213         if (f.exists() && FileUtil.toFileObject(f) == null) {
   214             String message = NbBundle.getMessage(ExistingAdaProjectPanelVisual.class, "ERR_WrongProjectFolder");
   215             settings.putProperty("WizardPanel_errorMessage", message);
   216             return false;
   217         }
   218 
   219         File[] kids = f.listFiles();
   220         if (kids != null) {
   221             for (File kid : kids) {
   222                 if ("nbproject".equals(kid.getName())) {    //NOI18N
   223                     settings.putProperty("WizardPanel_errorMessage",
   224                     NbBundle.getMessage(ExistingAdaProjectPanelVisual.class, "ERR_AlreadyAnProject"));
   225                     return false;
   226                 }
   227             }
   228         }
   229         settings.putProperty("WizardPanel_errorMessage", "");   //NOI18N
   230         return true;
   231     }
   232 
   233     @Override
   234     void validate(WizardDescriptor settings) throws WizardValidationException {        
   235     }
   236 
   237     public void insertUpdate(DocumentEvent e) {
   238         handleDocChange ();
   239         }
   240 
   241     public void removeUpdate(DocumentEvent e) {
   242         handleDocChange ();
   243         }
   244 
   245     public void changedUpdate(DocumentEvent e) {
   246         handleDocChange ();
   247         }
   248     
   249     private void handleDocChange () {
   250         this.panel.fireChangeEvent();
   251         }
   252 
   253 }