All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gluonhq.plugin.netbeans.template.visuals.GluonMobilePanelVisual Maven / Gradle / Ivy

Go to download

Gluon Plugin allows developers to create Java applications and their native images targeting Android, iOS, Linux, MacOS and Windows platforms using the same codebase.

There is a newer version: 2.10.4
Show newest version
/*
 * Copyright (c) 2018, 2020, Gluon Software
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 *
 * * Neither the name of the copyright holder nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.gluonhq.plugin.netbeans.template.visuals;

import com.gluonhq.plugin.templates.ProjectConstants;
import com.gluonhq.plugin.templates.TemplateUtils;
import org.netbeans.spi.project.ui.support.ProjectChooser;
import org.openide.WizardDescriptor;
import org.openide.filesystems.FileUtil;
import org.openide.util.Utilities;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.text.Document;
import java.io.File;

public class GluonMobilePanelVisual extends GluonBasePanelVisual {

    public GluonMobilePanelVisual(GluonMobileWizardPanel panel) {
        initComponents();
        this.panel = panel;
        // Register listener on the textFields to make the automatic updates
        projectNameTextField.getDocument().addDocumentListener(this);
        projectLocationTextField.getDocument().addDocumentListener(this);
        packageNameTextField.getDocument().addDocumentListener(this);
        mainClassNameTextField.getDocument().addDocumentListener(this);
        platformAndroidCheckbox.addChangeListener(this);
    }

    public String getProjectName() {
        return this.projectNameTextField.getText();
    }

    /**
     * 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.
     */
    // //GEN-BEGIN:initComponents
    private void initComponents() {

        buildTool = new javax.swing.ButtonGroup();
        projectNameLabel = new javax.swing.JLabel();
        projectNameTextField = new javax.swing.JTextField();
        projectLocationLabel = new javax.swing.JLabel();
        projectLocationTextField = new javax.swing.JTextField();
        browseButton = new javax.swing.JButton();
        createdFolderLabel = new javax.swing.JLabel();
        createdFolderTextField = new javax.swing.JTextField();
        packageNameTextField = new javax.swing.JTextField();
        mainClassNameTextField = new javax.swing.JTextField();
        createdMainClassTextField = new javax.swing.JTextField();
        packageNameLabel = new javax.swing.JLabel();
        mainClassNameLabel = new javax.swing.JLabel();
        mainClassLabel = new javax.swing.JLabel();
        platformsLabel = new javax.swing.JLabel();
        platformAndroidCheckbox = new javax.swing.JCheckBox();
        platformIosCheckbox = new javax.swing.JCheckBox();
        platformDesktopCheckbox = new javax.swing.JCheckBox();
        platformEmbeddedCheckbox = new javax.swing.JCheckBox();
        buildToolsLabel = new javax.swing.JLabel();
        buildToolMavenRadioButton = new javax.swing.JRadioButton();
        buildToolGradleRadioButton = new javax.swing.JRadioButton();

        projectNameLabel.setLabelFor(projectNameTextField);
        org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel, org.openide.util.NbBundle.getMessage(GluonMobilePanelVisual.class, "GluonMobilePanelVisual.projectNameLabel.text")); // NOI18N

        projectLocationLabel.setLabelFor(projectLocationTextField);
        org.openide.awt.Mnemonics.setLocalizedText(projectLocationLabel, org.openide.util.NbBundle.getMessage(GluonMobilePanelVisual.class, "GluonMobilePanelVisual.projectLocationLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(browseButton, org.openide.util.NbBundle.getMessage(GluonMobilePanelVisual.class, "GluonMobilePanelVisual.browseButton.text")); // NOI18N
        browseButton.setActionCommand(org.openide.util.NbBundle.getMessage(GluonMobilePanelVisual.class, "GluonMobilePanelVisual.browseButton.actionCommand")); // NOI18N
        browseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                browseButtonActionPerformed(evt);
            }
        });

        createdFolderLabel.setLabelFor(createdFolderTextField);
        org.openide.awt.Mnemonics.setLocalizedText(createdFolderLabel, org.openide.util.NbBundle.getMessage(GluonMobilePanelVisual.class, "GluonMobilePanelVisual.createdFolderLabel.text")); // NOI18N

        createdFolderTextField.setEditable(false);

        createdMainClassTextField.setEditable(false);

        packageNameLabel.setLabelFor(packageNameTextField);
        org.openide.awt.Mnemonics.setLocalizedText(packageNameLabel, org.openide.util.NbBundle.getMessage(GluonMobilePanelVisual.class, "GluonMobilePanelVisual.packageNameLabel.text")); // NOI18N

        mainClassNameLabel.setLabelFor(mainClassNameTextField);
        org.openide.awt.Mnemonics.setLocalizedText(mainClassNameLabel, org.openide.util.NbBundle.getMessage(GluonMobilePanelVisual.class, "GluonMobilePanelVisual.mainClassNameLabel.text")); // NOI18N

        mainClassLabel.setLabelFor(createdMainClassTextField);
        org.openide.awt.Mnemonics.setLocalizedText(mainClassLabel, org.openide.util.NbBundle.getMessage(GluonMobilePanelVisual.class, "GluonMobilePanelVisual.mainClassLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(platformsLabel, org.openide.util.NbBundle.getMessage(GluonMobilePanelVisual.class, "GluonMobilePanelVisual.platformsLabel.text")); // NOI18N

        platformAndroidCheckbox.setSelected(true);
        org.openide.awt.Mnemonics.setLocalizedText(platformAndroidCheckbox, org.openide.util.NbBundle.getMessage(GluonMobilePanelVisual.class, "GluonMobilePanelVisual.platformAndroidCheckbox.text")); // NOI18N

        platformIosCheckbox.setSelected(true);
        org.openide.awt.Mnemonics.setLocalizedText(platformIosCheckbox, org.openide.util.NbBundle.getMessage(GluonMobilePanelVisual.class, "GluonMobilePanelVisual.platformIosCheckbox.text")); // NOI18N

        platformDesktopCheckbox.setSelected(true);
        org.openide.awt.Mnemonics.setLocalizedText(platformDesktopCheckbox, org.openide.util.NbBundle.getMessage(GluonMobilePanelVisual.class, "GluonMobilePanelVisual.platformDesktopCheckbox.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(platformEmbeddedCheckbox, org.openide.util.NbBundle.getMessage(GluonMobilePanelVisual.class, "GluonMobilePanelVisual.platformEmbeddedCheckbox.text")); // NOI18N
        platformEmbeddedCheckbox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                platformEmbeddedCheckboxActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(buildToolsLabel, org.openide.util.NbBundle.getMessage(GluonMobilePanelVisual.class, "GluonMobilePanelVisual.buildToolsLabel.text")); // NOI18N

        buildTool.add(buildToolMavenRadioButton);
        buildToolMavenRadioButton.setSelected(true);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("com/gluonhq/plugin/netbeans/template/visuals/Bundle"); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(buildToolMavenRadioButton, bundle.getString("GluonMobilePanelVisual.buildToolMavenRadioButton.text")); // NOI18N
        buildToolMavenRadioButton.setActionCommand(org.openide.util.NbBundle.getMessage(GluonMobilePanelVisual.class, "GluonMobilePanelVisual.buildToolMavenRadioButton.actionCommand")); // NOI18N

        buildTool.add(buildToolGradleRadioButton);
        org.openide.awt.Mnemonics.setLocalizedText(buildToolGradleRadioButton, bundle.getString("GluonMobilePanelVisual.buildToolGradleRadioButton.text")); // NOI18N
        buildToolGradleRadioButton.setActionCommand(org.openide.util.NbBundle.getMessage(GluonMobilePanelVisual.class, "GluonMobilePanelVisual.buildToolGradleRadioButton.actionCommand")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(projectNameLabel)
                    .addComponent(projectLocationLabel)
                    .addComponent(createdFolderLabel)
                    .addComponent(packageNameLabel)
                    .addComponent(mainClassNameLabel)
                    .addComponent(mainClassLabel)
                    .addComponent(platformsLabel)
                    .addComponent(buildToolsLabel))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(projectNameTextField, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(projectLocationTextField, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(createdFolderTextField, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(packageNameTextField, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(mainClassNameTextField)
                            .addComponent(createdMainClassTextField, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(browseButton))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(platformAndroidCheckbox)
                            .addComponent(platformIosCheckbox)
                            .addComponent(platformDesktopCheckbox)
                            .addComponent(platformEmbeddedCheckbox)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(buildToolMavenRadioButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(buildToolGradleRadioButton)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(projectNameLabel)
                    .addComponent(projectNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(projectLocationLabel)
                    .addComponent(projectLocationTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(browseButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(createdFolderLabel)
                    .addComponent(createdFolderTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(packageNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(packageNameLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(mainClassNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(mainClassNameLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(createdMainClassTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(mainClassLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(platformsLabel)
                    .addComponent(platformAndroidCheckbox))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(platformIosCheckbox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(platformDesktopCheckbox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(platformEmbeddedCheckbox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buildToolsLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(buildToolMavenRadioButton)
                        .addComponent(buildToolGradleRadioButton)))
                .addGap(28, 28, 28))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {buildToolGradleRadioButton, buildToolMavenRadioButton});

    }// //GEN-END:initComponents

    private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButtonActionPerformed
        String command = evt.getActionCommand();
        if ("BROWSE".equals(command)) {
            JFileChooser chooser = new JFileChooser();
            chooser.setCurrentDirectory(null);
            chooser.setDialogTitle("Select Project Location");
            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            String path = this.projectLocationTextField.getText();
            if (path.length() > 0) {
                File f = new File(path);
                if (f.exists()) {
                    chooser.setSelectedFile(f);
                }
            }
            if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
                File projectDir = chooser.getSelectedFile();
                projectLocationTextField.setText(FileUtil.normalizeFile(projectDir).getAbsolutePath());
            }
            panel.fireChangeEvent();
        }

    }//GEN-LAST:event_browseButtonActionPerformed

    private void platformEmbeddedCheckboxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_platformEmbeddedCheckboxActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_platformEmbeddedCheckboxActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton browseButton;
    private javax.swing.ButtonGroup buildTool;
    private javax.swing.JRadioButton buildToolGradleRadioButton;
    private javax.swing.JRadioButton buildToolMavenRadioButton;
    private javax.swing.JLabel buildToolsLabel;
    private javax.swing.JLabel createdFolderLabel;
    private javax.swing.JTextField createdFolderTextField;
    private javax.swing.JTextField createdMainClassTextField;
    private javax.swing.JLabel mainClassLabel;
    private javax.swing.JLabel mainClassNameLabel;
    private javax.swing.JTextField mainClassNameTextField;
    private javax.swing.JLabel packageNameLabel;
    private javax.swing.JTextField packageNameTextField;
    private javax.swing.JCheckBox platformAndroidCheckbox;
    private javax.swing.JCheckBox platformDesktopCheckbox;
    private javax.swing.JCheckBox platformEmbeddedCheckbox;
    private javax.swing.JCheckBox platformIosCheckbox;
    private javax.swing.JLabel platformsLabel;
    private javax.swing.JLabel projectLocationLabel;
    private javax.swing.JTextField projectLocationTextField;
    private javax.swing.JLabel projectNameLabel;
    private javax.swing.JTextField projectNameTextField;
    // End of variables declaration//GEN-END:variables
    
    @Override
    public void addNotify() {
        super.addNotify();
        //same problem as in 31086, initial focus on Cancel button
        projectNameTextField.requestFocus();
    }

    @Override
    protected boolean valid(WizardDescriptor wizardDescriptor) {

        if (projectNameTextField.getText().length() == 0) {
            // TODO if using org.openide.dialogs >= 7.8, can use WizardDescriptor.PROP_ERROR_MESSAGE:
            wizardDescriptor.putProperty("WizardPanel_errorMessage",
                    "Project Name is not a valid folder name.");
            return false; // Display name not specified
        }
        File f = FileUtil.normalizeFile(new File(projectLocationTextField.getText()).getAbsoluteFile());
        if (!f.isDirectory()) {
            String message = "Project Folder is not a valid path.";
            wizardDescriptor.putProperty("WizardPanel_errorMessage", message);
            return false;
        }
        final File destFolder = FileUtil.normalizeFile(new File(createdFolderTextField.getText()).getAbsoluteFile());

        File projLoc = destFolder;
        while (projLoc != null && !projLoc.exists()) {
            projLoc = projLoc.getParentFile();
        }
        if (projLoc == null || !projLoc.canWrite()) {
            wizardDescriptor.putProperty("WizardPanel_errorMessage",
                    "Project Folder cannot be created.");
            return false;
        }

        if (FileUtil.toFileObject(projLoc) == null) {
            String message = "Project Folder is not a valid path.";
            wizardDescriptor.putProperty("WizardPanel_errorMessage", message);
            return false;
        }

        File[] kids = destFolder.listFiles();
        if (destFolder.exists() && kids != null && kids.length > 0) {
            // Folder exists and is not empty
            wizardDescriptor.putProperty("WizardPanel_errorMessage",
                    "Project Folder already exists and is not empty.");
            return false;
        }

        if (packageNameTextField.getText().length() == 0) {
            wizardDescriptor.putProperty("WizardPanel_errorMessage", "Provide valid Java Package name");
            return false;
        }

        if (!TemplateUtils.isValidPackageName(packageNameTextField.getText())) {
            wizardDescriptor.putProperty("WizardPanel_errorMessage", "The Package Name is not valid");
            return false;
        }

        if (platformAndroidCheckbox.isSelected()) {
            if (!isValidAndroidPackageName(packageNameTextField.getText())) {
                wizardDescriptor.putProperty("WizardPanel_errorMessage", "Android requires a Package Name with at least one dot.");
                return false;
            }
        }

        if (mainClassNameTextField.getText().length() == 0) {
            wizardDescriptor.putProperty("WizardPanel_errorMessage", "Provide valid Java Class name");
            return false;
        }

        if (!isValidTypeIdentifier(mainClassNameTextField.getText())) {
            wizardDescriptor.putProperty("WizardPanel_errorMessage", "The Class Name is not valid");
            return false;
        }

        wizardDescriptor.putProperty("WizardPanel_errorMessage", "");
        return true;
    }

    @Override
    protected void store(WizardDescriptor d) {
        String name = projectNameTextField.getText().trim();
        String folder = createdFolderTextField.getText().trim();
        String packageName = packageNameTextField.getText().trim();
        String mainClassName = mainClassNameTextField.getText().trim();
        String mainClass = createdMainClassTextField.getText().trim();

        d.putProperty(ProjectConstants.PARAM_PROJECT_DIR, new File(folder));
        d.putProperty(ProjectConstants.PARAM_PROJECT_NAME, name);
        d.putProperty(ProjectConstants.PARAM_PACKAGE_NAME, packageName);
        d.putProperty(ProjectConstants.PARAM_PACKAGE_FOLDER, packageName.replaceAll("\\.", "/"));
        d.putProperty(ProjectConstants.PARAM_MAIN_CLASS, mainClass);
        d.putProperty(ProjectConstants.PARAM_MAIN_CLASS_NAME, mainClassName);
        d.putProperty(ProjectConstants.PARAM_ANDROID_ENABLED, platformAndroidCheckbox.isSelected());
        d.putProperty(ProjectConstants.PARAM_IOS_ENABLED, platformIosCheckbox.isSelected());
        d.putProperty(ProjectConstants.PARAM_DESKTOP_ENABLED, platformDesktopCheckbox.isSelected());
        d.putProperty(ProjectConstants.PARAM_EMBEDDED_ENABLED, platformEmbeddedCheckbox.isSelected());
        d.putProperty(ProjectConstants.PARAM_BUILD_TOOL, buildTool.getSelection().getActionCommand());
    }

    @Override
    protected void read(WizardDescriptor settings) {
        File projectLocation = (File) settings.getProperty(ProjectConstants.PARAM_PROJECT_DIR);
        if (projectLocation == null || projectLocation.getParentFile() == null || !projectLocation.getParentFile().isDirectory()) {
            projectLocation = ProjectChooser.getProjectsFolder();
        } else {
            projectLocation = projectLocation.getParentFile();
        }
        this.projectLocationTextField.setText(projectLocation.getAbsolutePath());

        String projectName = (String) settings.getProperty(ProjectConstants.PARAM_PROJECT_NAME);
        if (projectName == null) {
            projectName = ProjectConstants.DEFAULT_PROJECT_NAME;
        }
        this.projectNameTextField.setText(projectName);
        this.projectNameTextField.selectAll();
    }

    // Implementation of DocumentListener --------------------------------------
    @Override
    public void changedUpdate(DocumentEvent e) {
        updateTexts(e);
        if (this.projectNameTextField.getDocument() == e.getDocument()) {
            firePropertyChange(ProjectConstants.PARAM_PROJECT_NAME, null, this.projectNameTextField.getText());
        }
    }

    @Override
    public void insertUpdate(DocumentEvent e) {
        updateTexts(e);
        if (this.projectNameTextField.getDocument() == e.getDocument()) {
            firePropertyChange(ProjectConstants.PARAM_PROJECT_NAME, null, this.projectNameTextField.getText());
        }
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        updateTexts(e);
        if (this.projectNameTextField.getDocument() == e.getDocument()) {
            firePropertyChange(ProjectConstants.PARAM_PROJECT_NAME, null, this.projectNameTextField.getText());
        }
    }

    /**
     * Handles changes in the Project name and project directory,
     */
    private void updateTexts(DocumentEvent e) {

        Document doc = e.getDocument();

        if (doc == projectNameTextField.getDocument() || doc == projectLocationTextField.getDocument()) {
            // Change in the project name

            String projectName = projectNameTextField.getText();
            String projectFolder = projectLocationTextField.getText();

            //if (projectFolder.trim().length() == 0 || projectFolder.equals(oldName)) {
            createdFolderTextField.setText(projectFolder + File.separatorChar + projectName);
            //}

            if (! projectName.trim().isEmpty()) {
                packageNameTextField.setText("com." + projectName.toLowerCase());
                if (projectName.trim().length() == 1) {
                    mainClassNameTextField.setText(projectName.toUpperCase());
                } else {
                    mainClassNameTextField.setText(projectName.substring(0, 1).toUpperCase() + projectName.substring(1));
                }
            }
        }

        if (doc == packageNameTextField.getDocument() || doc == mainClassNameTextField.getDocument()) {
            // Change in the main class

            String packageName = packageNameTextField.getText();
            String mainClassName = mainClassNameTextField.getText();

            if (packageName.isEmpty()) {
                createdMainClassTextField.setText(mainClassName);
            } else if (mainClassName.isEmpty()) {
                createdMainClassTextField.setText(packageName);
            } else {
                createdMainClassTextField.setText(packageName + '.' + mainClassName);
            }

        }

        panel.fireChangeEvent(); // Notify that the panel changed
    }

    private static boolean isValidAndroidPackageName(String str) {
        if (!TemplateUtils.isValidPackageName(str)) {
            return false;
        }

        return str.contains(".");
    }

    private static boolean isValidTypeIdentifier(String ident) {
        return ident != null && !"".equals(ident) && Utilities.isJavaIdentifier(ident);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy