package tk.amberide.ide.gui.dialogs; import tk.amberide.Amber; import javax.swing.*; import java.io.File; /** * @author Tudor */ public class NewProjectDialog extends javax.swing.JDialog { private JFileChooser browser; private File dir; private boolean initialized; /** * Creates new form NewProjectDialog */ public NewProjectDialog(java.awt.Frame parent) { super(parent); initComponents(); browser = new JFileChooser("Choose project location..."); browser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); browser.setApproveButtonText("Choose directory"); // TODO platform: http://stackoverflow.com/questions/2883447/jfilechooser-select-directory-but-show-files updateOutputFolder(); } public boolean isInitialized() { return initialized; } public File getProjectRoot() { return dir; } private String getUnusedUnamed(File root) { int n = 1; while (true) { File pr = new File(root, "untitled" + n); if (!pr.exists()) { return "untitled" + n; } n++; } } private void updateOutputFolder() { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { getUnusedUnamed(new File(locationField.getText())); folderField.setText(locationField.getText() + File.separatorChar + nameField.getText()); dir = new File(folderField.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. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { projectGroup = new javax.swing.JPanel(); browseButton = new javax.swing.JButton(); nameLabel = new javax.swing.JLabel(); folderLabel = new javax.swing.JLabel(); locationLabel = new javax.swing.JLabel(); locationField = new javax.swing.JTextField(); nameField = new javax.swing.JTextField(); folderField = new javax.swing.JTextField(); createButton = new javax.swing.JButton(); cancelButton = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setTitle("New project..."); setModal(true); setResizable(false); projectGroup.setBorder(javax.swing.BorderFactory.createTitledBorder("Project")); browseButton.setText("..."); browseButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { browseButtonActionPerformed(evt); } }); nameLabel.setText("Name:"); folderLabel.setText("Folder:"); locationLabel.setText("Location:"); locationField.setText(System.getProperty("user.home")); locationField.setPreferredSize(null); locationField.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { locationFieldKeyTyped(evt); } }); nameField.setText(getUnusedUnamed(new File(System.getProperty("user.home")))); nameField.setPreferredSize(null); nameField.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { nameFieldKeyTyped(evt); } }); folderField.setEditable(false); folderField.setPreferredSize(null); javax.swing.GroupLayout projectGroupLayout = new javax.swing.GroupLayout(projectGroup); projectGroup.setLayout(projectGroupLayout); projectGroupLayout.setHorizontalGroup( projectGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(projectGroupLayout.createSequentialGroup() .addContainerGap() .addGroup(projectGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(nameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(locationLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(folderLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(projectGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(projectGroupLayout.createSequentialGroup() .addGroup(projectGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(locationField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(folderField, javax.swing.GroupLayout.PREFERRED_SIZE, 297, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(browseButton, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(nameField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); projectGroupLayout.setVerticalGroup( projectGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(projectGroupLayout.createSequentialGroup() .addContainerGap() .addGroup(projectGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(nameLabel)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(projectGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(locationLabel) .addComponent(locationField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(browseButton)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(projectGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(folderLabel) .addComponent(folderField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap()) ); createButton.setText("Create"); createButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { createButtonActionPerformed(evt); } }); cancelButton.setText("Cancel"); cancelButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cancelButtonActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(projectGroup, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(createButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(cancelButton) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(projectGroup, 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(cancelButton) .addComponent(createButton)) .addGap(6, 6, 6)) ); pack(); setLocationRelativeTo(null); }// </editor-fold>//GEN-END:initComponents private void locationFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_locationFieldKeyTyped updateOutputFolder(); }//GEN-LAST:event_locationFieldKeyTyped private void nameFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nameFieldKeyTyped updateOutputFolder(); }//GEN-LAST:event_nameFieldKeyTyped private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButtonActionPerformed if (browser.showOpenDialog(Amber.getUI()) == JFileChooser.APPROVE_OPTION) { locationField.setText(browser.getSelectedFile().getAbsolutePath()); updateOutputFolder(); } }//GEN-LAST:event_browseButtonActionPerformed private void createButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createButtonActionPerformed if (dir != null) { Amber.initializeProject(dir); } dispose(); }//GEN-LAST:event_createButtonActionPerformed private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed dispose(); }//GEN-LAST:event_cancelButtonActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton browseButton; private javax.swing.JButton cancelButton; private javax.swing.JButton createButton; private javax.swing.JTextField folderField; private javax.swing.JLabel folderLabel; private javax.swing.JTextField locationField; private javax.swing.JLabel locationLabel; private javax.swing.JTextField nameField; private javax.swing.JLabel nameLabel; private javax.swing.JPanel projectGroup; // End of variables declaration//GEN-END:variables }