package net.java.nboglpack.joglproject;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;
import net.java.nboglpack.jogl.util.JOGLDistribution;
import org.jdesktop.layout.GroupLayout;
import org.jdesktop.layout.LayoutStyle;
import org.netbeans.spi.project.ui.support.ProjectChooser;
import org.openide.WizardDescriptor;
import org.openide.WizardValidationException;
import org.openide.awt.Mnemonics;
import org.openide.filesystems.FileUtil;
/**
* Main panel to set up the new-JOGL-project.
* @author Mathias Henze
* @author Michael Bien
*/
public class ProjectPanelVisual extends JPanel implements DocumentListener
{
public static final String PROP_PROJECT_NAME = "projectName";
public static final String PROP_PROJECT_PACKAGE = "projectPackage";
private ProjectWizardPanel panel;
/** Creates new form PanelProjectLocationVisual */
public ProjectPanelVisual(ProjectWizardPanel panel)
{
initComponents();
this.panel = panel;
// Register listener on the textFields to make the automatic updates
projectNameTextField.getDocument().addDocumentListener(this);
projectLocationTextField.getDocument().addDocumentListener(this);
projectPackageTextField.getDocument().addDocumentListener(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.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
JLabel projectNameLabel = new JLabel();
JLabel projectLocationLabel = new JLabel();
JLabel createdFolderLabel = new JLabel();
JLabel projectPackageLabel = new JLabel();
JLabel createdMainClassLabel = new JLabel();
JLabel createdMainClassLabel1 = new JLabel();
projectNameTextField = new JTextField();
projectLocationTextField = new JTextField();
createdFolderTextField = new JTextField();
projectPackageTextField = new JTextField();
createdMainClassTextField = new JTextField();
JButton browseButton = new JButton();
platformDropDown = new JComboBox();
JTextArea platformHint = new JTextArea();
projectNameLabel.setLabelFor(projectNameTextField);
Mnemonics.setLocalizedText(projectNameLabel, "Project &Name:");
projectLocationLabel.setLabelFor(projectLocationTextField);
Mnemonics.setLocalizedText(projectLocationLabel, "Project &Location:");
createdFolderLabel.setLabelFor(createdFolderTextField);
Mnemonics.setLocalizedText(createdFolderLabel, "Project &Folder:");
projectPackageLabel.setLabelFor(projectNameTextField);
Mnemonics.setLocalizedText(projectPackageLabel, "Project Package:");
createdMainClassLabel.setLabelFor(createdFolderTextField);
Mnemonics.setLocalizedText(createdMainClassLabel, "Main Class:");
createdMainClassLabel1.setLabelFor(createdFolderTextField);
Mnemonics.setLocalizedText(createdMainClassLabel1, "Platform:");
createdFolderTextField.setEditable(false);
createdMainClassTextField.setEditable(false);
Mnemonics.setLocalizedText(browseButton, "Br&owse...");
browseButton.setActionCommand("BROWSE");
browseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
browseButtonActionPerformed(evt);
}
});
platformDropDown.setModel(createPlatformComboBoxModel());
platformHint.setColumns(20);
platformHint.setEditable(false);
platformHint.setLineWrap(true);
platformHint.setRows(5);
platformHint.setText("This is the platform of your development system. You can generate distributions for all supported platforms.");
platformHint.setWrapStyleWord(true);
platformHint.setOpaque(false);
GroupLayout layout = new GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(GroupLayout.LEADING)
.add(projectNameLabel)
.add(projectLocationLabel)
.add(createdFolderLabel)
.add(projectPackageLabel)
.add(createdMainClassLabel)
.add(createdMainClassLabel1))
.addPreferredGap(LayoutStyle.RELATED)
.add(layout.createParallelGroup(GroupLayout.LEADING)
.add(platformDropDown, 0, 357, Short.MAX_VALUE)
.add(createdMainClassTextField, GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
.add(projectPackageTextField, GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
.add(GroupLayout.TRAILING, projectNameTextField, GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
.add(GroupLayout.TRAILING, projectLocationTextField, GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
.add(GroupLayout.TRAILING, createdFolderTextField, GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
.add(platformHint, GroupLayout.PREFERRED_SIZE, 357, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.RELATED)
.add(browseButton)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(GroupLayout.BASELINE)
.add(projectNameLabel)
.add(projectNameTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.RELATED)
.add(layout.createParallelGroup(GroupLayout.BASELINE)
.add(projectLocationLabel)
.add(projectLocationTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.add(browseButton))
.addPreferredGap(LayoutStyle.RELATED)
.add(layout.createParallelGroup(GroupLayout.BASELINE)
.add(createdFolderLabel)
.add(createdFolderTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.RELATED)
.add(layout.createParallelGroup(GroupLayout.BASELINE)
.add(projectPackageLabel)
.add(projectPackageTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.RELATED)
.add(layout.createParallelGroup(GroupLayout.BASELINE)
.add(createdMainClassLabel)
.add(createdMainClassTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.RELATED)
.add(layout.createParallelGroup(GroupLayout.BASELINE)
.add(platformDropDown, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.add(createdMainClassLabel1))
.addPreferredGap(LayoutStyle.RELATED)
.add(platformHint, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
}// </editor-fold>//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();
FileUtil.preventFileChooserSymlinkTraversal(chooser, 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
// Variables declaration - do not modify//GEN-BEGIN:variables
private JTextField createdFolderTextField;
private JTextField createdMainClassTextField;
private JComboBox platformDropDown;
private JTextField projectLocationTextField;
private JTextField projectNameTextField;
private JTextField projectPackageTextField;
// End of variables declaration//GEN-END:variables
private ComboBoxModel createPlatformComboBoxModel() {
return new DefaultComboBoxModel( JOGLDistribution.values() );
}
@Override
public void addNotify()
{
super.addNotify();
//same problem as in 31086, initial focus on Cancel button
projectNameTextField.requestFocus();
}
boolean valid(WizardDescriptor wizardDescriptor)
{
boolean errors= false;
if (projectNameTextField.getText().length() == 0)
{
wizardDescriptor.putProperty("WizardPanel_errorMessage",
"Project Name is not a valid folder name.");
errors= true; //Display name not specified
}
if (!projectPackageTextField.getText().matches("^[\\w\\.]+$"))
{
wizardDescriptor.putProperty("WizardPanel_errorMessage",
"Project package is not a valid package name.");
errors= true; // Display name not specified
}
if(errors) return false;
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;
}
wizardDescriptor.putProperty("WizardPanel_errorMessage", "");
return true;
}
void store(WizardDescriptor d)
{
String name = projectNameTextField.getText().trim();
String packageName = projectPackageTextField.getText().trim();
String folder = createdFolderTextField.getText().trim();
char[] chars= name.replaceAll("\\W+"," ").toCharArray();
String mainClass="";
char lastChar=' ';
for (int i = 0; i < chars.length; i++)
{
if(lastChar==' ')
mainClass+=Character.toUpperCase(chars[i]);
else if(chars[i]!=' ')
mainClass+=chars[i];
lastChar=chars[i];
}
d.putProperty("projdir", new File(folder));
d.putProperty("name", name);
d.putProperty("projpackage", packageName);
d.putProperty("projclass", mainClass);
d.putProperty("platform", ((JOGLDistribution)platformDropDown.getSelectedItem()).key());
}
void read(WizardDescriptor settings)
{
File projectLocation = (File) settings.getProperty("projdir");
if (projectLocation == null || projectLocation.getParentFile() == null || !projectLocation.getParentFile().isDirectory())
{
projectLocation = ProjectChooser.getProjectsFolder();
}
else
{
projectLocation = projectLocation.getParentFile();
}
this.projectLocationTextField.setText(projectLocation.getAbsolutePath());
String projectPackage = (String)settings.getProperty("projpackage");
if(projectPackage == null)
{
projectPackage = "org.yourorghere";
}
this.projectPackageTextField.setText(projectPackage);
String platform = (String)settings.getProperty("platform");
if(platform == null) {
JOGLDistribution dist = JOGLDistribution.getCompatible();
if(dist != null)
this.platformDropDown.setSelectedItem(dist);
}else{
JOGLDistribution dist = JOGLDistribution.parseKey(platform);
if(dist != null)
this.platformDropDown.setSelectedItem(dist);
}
String projectName = (String) settings.getProperty("name");
if(projectName == null)
{
projectName = "JOGLApp";
}
this.projectNameTextField.setText(projectName);
this.projectNameTextField.selectAll();
}
void validate(WizardDescriptor d) throws WizardValidationException
{
// nothing to validate
}
// Implementation of DocumentListener --------------------------------------
public void changedUpdate(DocumentEvent e)
{
updateTexts(e);
if (this.projectNameTextField.getDocument() == e.getDocument())
{
firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText());
}
if (this.projectPackageTextField.getDocument() == e.getDocument())
{
firePropertyChange(PROP_PROJECT_PACKAGE,null,this.projectPackageTextField.getText());
}
}
public synchronized void insertUpdate(DocumentEvent e)
{
updateTexts(e);
if (this.projectNameTextField.getDocument() == e.getDocument())
{
firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText());
}
if (this.projectPackageTextField.getDocument() == e.getDocument())
{
firePropertyChange(PROP_PROJECT_PACKAGE,null,this.projectPackageTextField.getText());
}
}
public void removeUpdate(DocumentEvent e)
{
updateTexts(e);
if (this.projectNameTextField.getDocument() == e.getDocument())
{
firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText());
}
if (this.projectPackageTextField.getDocument() == e.getDocument())
{
firePropertyChange(PROP_PROJECT_PACKAGE,null,this.projectPackageTextField.getText());
}
}
/** Handles changes in the Project name and project directory, */
private void updateTexts(DocumentEvent e)
{
Document doc = e.getDocument();
if (doc == projectPackageTextField.getDocument() || doc == projectNameTextField.getDocument() || doc == projectLocationTextField.getDocument())
{
// Change in the project name
String projectName = projectNameTextField.getText();
String projectFolder = projectLocationTextField.getText();
String projectPackage = projectPackageTextField.getText();
//if (projectFolder.trim().length() == 0 || projectFolder.equals(oldName)) {
createdFolderTextField.setText(projectFolder + File.separatorChar + projectName);
//}
char[] chars= projectName.replaceAll("\\W+"," ").toCharArray();
String mainClass="";
char lastChar=' ';
for (int i = 0; i < chars.length; i++)
{
if(lastChar==' ')
mainClass+=Character.toUpperCase(chars[i]);
else if(chars[i]!=' ')
mainClass+=chars[i];
lastChar=chars[i];
}
createdMainClassTextField.setText(projectPackage + "." + mainClass);
}
panel.fireChangeEvent(); // Notify that the panel changed
}
}