Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
   *
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common
   * Development and Distribution License("CDDL") (collectively, the
   * "License"). You may not use this file except in compliance with the
  * License. You can obtain a copy of the License at
  * http://www.netbeans.org/cddl-gplv2.html
  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  * specific language governing permissions and limitations under the
  * License.  When distributing the software, include this License Header
  * Notice in each file and include the License file at
  * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
  * particular file as subject to the "Classpath" exception as provided
  * by Sun in the GPL Version 2 section of the License file that
  * accompanied this code. If applicable, add the following below the
  * License Header, with the fields enclosed by brackets [] replaced by
  * your own identifying information:
  * "Portions Copyrighted [year] [name of copyright owner]"
  *
  * Contributor(s):
  *
  * The Original Software is NetBeans. The Initial Developer of the Original
  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
  * Microsystems, Inc. All Rights Reserved.
  *
  * If you wish your version of this file to be governed by only the CDDL
  * or only the GPL Version 2, indicate your decision by adding
  * "[Contributor] elects to include this software in this distribution
  * under the [CDDL or GPL Version 2] license." If you do not indicate a
  * single choice of license, a recipient has the option to distribute
  * your version of this file under either the CDDL, the GPL Version 2 or
  * to extend the choice of license to its licensees as provided above.
  * However, if you add GPL Version 2 code and therefore, elected the GPL
  * Version 2 license, then the option applies only if the new code is
  * made subject to such option by the copyright holder.
  */
 
 package org.netbeans.modules.cnd.makeproject.api;
 
 import java.io.File;
 
 public class RunDialogPanel extends javax.swing.JPanel {
     private Project[] projectChoices = null;
     private boolean executableReadOnly = true;
     private JButton actionButton;
     
     private static String lastSelectedExecutable = null;
     private static Project lastSelectedProject = null;
     
     private static DefaultPicklistModel picklist = null;
     private static String picklistHomeDir = null;
     private static final String picklistName = "executables"// NOI18N
     private boolean isValidating = false;
     
     public RunDialogPanel() {
         initialize(nullfalse);
         .setText(""); //NOI18N
         initAccessibility();
     }
     
     private void initAccessibility() {
         // Accessibility
         getAccessibleContext().setAccessibleDescription(getString("RUN_DIALOG_PANEL_AD"));
         .getAccessibleContext().setAccessibleDescription(getString("ASSOCIATED_PROJECT_AD"));
         .getAccessibleContext().setAccessibleDescription(getString("RUN_DIRECTORY_LABEL_AD"));
     }
    
    public RunDialogPanel(String exePathboolean executableReadOnlyJButton actionButton) {
        this. = actionButton;
        initialize(exePathexecutableReadOnly);
        .setText(""); //NOI18N
        initAccessibility();
    }
    
    protected  void initialize(String exePathboolean executableReadOnly) {
        initComponents();
        .setForeground(javax.swing.UIManager.getColor("nb.errorForeground")); // NOI18N
        this. = executableReadOnly;
        //modifiedRunDirectoryListener = new ModifiedRunDirectoryListener();
        if (executableReadOnly) {
//            executableTextField.setEditable(false);
//            executableBrowseButton.setEnabled(false);
        }
        if (exePath != null)
            .setText(exePath);
        .setText(getString("DIALOG_GUIDANCETEXT"));
        String[] savedExePaths = getExecutablePicklist().getElementsDisplayName();
        String feed = null;
        if (exePath != null) {
            feed = exePath;
        } else if (savedExePaths.length > 0) {
            feed = savedExePaths[0];
        } else {
            feed = ""// NOI18N
        }
        
        initGui();
        
        setPreferredSize(new java.awt.Dimension(700, (int)getPreferredSize().getHeight()));
    }
    
    
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() {
        java.awt.GridBagConstraints gridBagConstraints;
         = new javax.swing.JTextArea();
         = new javax.swing.JLabel();
         = new javax.swing.JButton();
         = new javax.swing.JTextField();
         = new javax.swing.JLabel();
         = new javax.swing.JComboBox();
         = new javax.swing.JLabel();
         = new javax.swing.JLabel();
         = new javax.swing.JTextField();
         = new javax.swing.JButton();
         = new javax.swing.JLabel();
         = new javax.swing.JTextField();
         = new javax.swing.JLabel();
         = new javax.swing.JTextField();
        setLayout(new java.awt.GridBagLayout());
        .setEditable(false);
        .setLineWrap(true);
        .setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/cnd/makeproject/api/Bundle").getString("DIALOG_GUIDANCETEXT"));
        .setWrapStyleWord(true);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = ...;
        gridBagConstraints.anchor = ...;
        gridBagConstraints.insets = new java.awt.Insets(12, 12, 12, 12);
        add(gridBagConstraints);
        .setDisplayedMnemonic(java.util.ResourceBundle.getBundle("org/netbeans/modules/cnd/makeproject/api/Bundle").getString("EXECUTABLE_MN").charAt(0));
        .setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/cnd/makeproject/api/Bundle").getString("EXECUTABLE_LBL"));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = ...;
        gridBagConstraints.insets = new java.awt.Insets(0, 12, 8, 0);
        add(gridBagConstraints);
        .setMnemonic(java.util.ResourceBundle.getBundle("org/netbeans/modules/cnd/makeproject/api/Bundle").getString("BROWSE_BUTTON_MN").charAt(0));
        .setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/cnd/makeproject/api/Bundle").getString("BROWSE_BUTTON_TXT"));
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                executableBrowseButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = ...;
        gridBagConstraints.anchor = ...;
        gridBagConstraints.insets = new java.awt.Insets(0, 4, 8, 12);
        add(gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = ...;
        gridBagConstraints.anchor = ...;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 4, 8, 0);
        add(gridBagConstraints);
        .setDisplayedMnemonic(java.util.ResourceBundle.getBundle("org/netbeans/modules/cnd/makeproject/api/Bundle").getString("ASSOCIATED_PROJECT_MN").charAt(0));
        .setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/cnd/makeproject/api/Bundle").getString("ASSOCIATED_PROJECT_LBL"));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = ...;
        gridBagConstraints.insets = new java.awt.Insets(0, 12, 6, 0);
        add(gridBagConstraints);
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                projectComboBoxActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = ...;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 4, 6, 12);
        add(gridBagConstraints);
        .setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/cnd/makeproject/api/Bundle").getString("ERROR_NOTAEXEFILE"));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = ...;
        gridBagConstraints.anchor = ...;
        gridBagConstraints.insets = new java.awt.Insets(24, 12, 0, 12);
        add(gridBagConstraints);
        .setDisplayedMnemonic(java.util.ResourceBundle.getBundle("org/netbeans/modules/cnd/makeproject/api/Bundle").getString("RUN_DIRECTORY_LABEL_MN").charAt(0));
        .setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/cnd/makeproject/api/Bundle").getString("RUN_DIRECTORY_LABEL_TXT"));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = ...;
        gridBagConstraints.insets = new java.awt.Insets(10, 12, 0, 0);
        add(gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = ...;
        gridBagConstraints.anchor = ...;
        gridBagConstraints.insets = new java.awt.Insets(10, 4, 0, 0);
        add(gridBagConstraints);
        .setMnemonic(java.util.ResourceBundle.getBundle("org/netbeans/modules/cnd/makeproject/api/Bundle").getString("RUN_DIRECTORY_BUTTON_MN").charAt(0));
        .setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/cnd/makeproject/api/Bundle").getString("RUN_DIRECTORY_BUTTON_TXT"));
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                runDirectoryBrowseButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = ...;
        gridBagConstraints.anchor = ...;
        gridBagConstraints.insets = new java.awt.Insets(10, 4, 0, 12);
        add(gridBagConstraints);
        .setDisplayedMnemonic(java.util.ResourceBundle.getBundle("org/netbeans/modules/cnd/makeproject/api/Bundle").getString("ARGUMENTS_LABEL_MN").charAt(0));
        .setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/cnd/makeproject/api/Bundle").getString("ARGUMENTS_LABEL_TXT"));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = ...;
        gridBagConstraints.insets = new java.awt.Insets(6, 12, 0, 0);
        add(gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = ...;
        gridBagConstraints.anchor = ...;
        gridBagConstraints.insets = new java.awt.Insets(6, 4, 0, 12);
        add(gridBagConstraints);
        .setDisplayedMnemonic(java.util.ResourceBundle.getBundle("org/netbeans/modules/cnd/makeproject/api/Bundle").getString("ENVIRONMENT_LABEL_MN").charAt(0));
        .setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/cnd/makeproject/api/Bundle").getString("ENVIRONMENT_LABEL_TXT"));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = ...;
        gridBagConstraints.insets = new java.awt.Insets(6, 12, 0, 0);
        add(gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = ...;
        gridBagConstraints.anchor = ...;
        gridBagConstraints.insets = new java.awt.Insets(6, 4, 0, 12);
        add(gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents
    private void runDirectoryBrowseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_runDirectoryBrowseButtonActionPerformed
        String seed;
        if (.getText().length() > 0)
            seed = .getText();
        else
            seed = getExecutablePath();
        // Show the file chooser
        FileChooser fileChooser = new FileChooser(
                getString("SelectWorkingDir"),
                getString("SelectLabel"),
                .,
                null,
                seed,
                true
                );
        int ret = fileChooser.showOpenDialog(this);
        if (ret == .)
            return;
        .setText(fileChooser.getSelectedFile().getPath());
    }//GEN-LAST:event_runDirectoryBrowseButtonActionPerformed
    
    private void projectComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_projectComboBoxActionPerformed
        int selectedIndex = .getSelectedIndex();
        if (selectedIndex == 0) {
            if (new File(.getText()).getParentFile() != null) {
            } else {
                if (!) {
                    .setText(""); // NOI18N
                }
            }
            .setText(""); // NOI18N
            .setText(""); // NOI18N
        }
        else {
            Project project = [.getSelectedIndex()-1];
            ConfigurationDescriptorProvider pdp = project.getLookup().lookup(ConfigurationDescriptorProvider.class);
            if (pdp == null)
                return;
            MakeConfigurationDescriptor projectDescriptor = pdp.getConfigurationDescriptor();
            MakeConfiguration conf = projectDescriptor.getActiveConfiguration();
            RunProfile runProfile = conf.getProfile();
            .setText(runProfile.getRunDirectory());
            .setText(runProfile.getArgsFlat());
            .setText(runProfile.getEnvironment().toString());
        }
    }//GEN-LAST:event_projectComboBoxActionPerformed
    
    private void executableBrowseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_executableBrowseButtonActionPerformed
        String seed = getExecutablePath();
        if (seed.length() == 0 && FileChooser.getCurrectChooserFile() != null)
            seed = FileChooser.getCurrectChooserFile().getPath();
        if (seed.length() == 0)
            seed = System.getProperty("user.home"); // NOI18N
        
        FileFilter[] filter;
        if (Utilities.isWindows()){
            filter = new FileFilter[] {PeExecutableFileFilter.getInstance()};
        } else if (Utilities.getOperatingSystem() == .) {
            filter = new FileFilter[] {MacOSXExecutableFileFilter.getInstance()};
        } else {
            filter = new FileFilter[] {ElfExecutableFileFilter.getInstance()};
        }
        // Show the file chooser
        FileChooser fileChooser = new FileChooser(
                getString("SelectExecutable"),
                getString("SelectLabel"),
                .,
                filter,
                seed,
                false
                );
        int ret = fileChooser.showOpenDialog(this);
        if (ret == .)
            return;
        .setText(fileChooser.getSelectedFile().getPath());
    }//GEN-LAST:event_executableBrowseButtonActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel argumentLabel;
    private javax.swing.JLabel errorLabel;
    private javax.swing.JLabel projectLabel;
    // End of variables declaration//GEN-END:variables
    
    private void initGui() {
         = OpenProjects.getDefault().getOpenProjects();
        ActionListener projectComboBoxActionListener = .getActionListeners()[0];
        .removeActionListener(projectComboBoxActionListener);
        .addItem(getString("NO_PROJECT")); // always first
        for (int i = 0; i < .i++) {
            .addItem(ProjectUtils.getInformation([i]).getName());
        }
        
        int index = 0;
        // preselect project ???
        if ( != null && getExecutablePath().equals() &&  != null) {
            for (int i = 0; i < .i++) {
                if ([i] == ) {
                    index = i+1;
                    break;
                }
            }
        }
        .setSelectedIndex(index);
        .addActionListener(projectComboBoxActionListener);
        projectComboBoxActionPerformed(null);
        //validateRunDirectory();
    }
    
    private boolean validateExecutable() {
        String exePath = getExecutablePath();
        File exeFile = new File(exePath);
        if (!exeFile.exists()) {
            setError("ERROR_DONTEXIST"true); // NOI18N
            return false;
        }
        if (exeFile.isDirectory()) {
            setError("ERROR_NOTAEXEFILE"true); // NOI18N
            return false;
        }
        return true;
    }
    
    private boolean validateRunDirectory() {
        String runDirectory = .getText();
        
        File runDirectoryFile = new File(.getText());
     
        if (!runDirectoryFile.exists()) {
            setError("ERROR_RUNDIR_DONTEXIST"false); // NOI18N
            return false;
        }
        if (!runDirectoryFile.isDirectory()) {
            setError("ERROR_RUNDIR_INVALID"false); // NOI18N
            return false;
        }
        return true;
    }
    
    private void setError(String errorMsgboolean disable) {
        setErrorMsg(getString(errorMsg));
        if (disable) {
            .setEnabled(false);
            .setEnabled(false);
            .setEnabled(false);
            .setEnabled(false);
            .setEnabled(false);
            .setEnabled(false);
            .setEnabled(false);
            .setEnabled(false);
        }
        .setEnabled(false);
    }
    
    private void clearError() {
        setErrorMsg(" "); // NOI18N
        
        .setEnabled(true);
        .setEnabled(true);
        .setEnabled(true);
        .setEnabled(true);
        .setEnabled(true);
        .setEnabled(true);
        .setEnabled(true);
        
        .setEnabled(true);
    }
    
    private void validateFields(javax.swing.event.DocumentEvent documentEvent) {
         = true;
        try {
            clearError();
            if (documentEvent.getDocument() == .getDocument()) {
                .setSelectedIndex(0);
                if (!validateExecutable())
                    return;
            }
            validateRunDirectory();
        } finally {
             = false;
        }
    }
    
    // ModifiedDocumentListener
    public class ModifiedValidateDocumentListener implements DocumentListener {
        public void changedUpdate(javax.swing.event.DocumentEvent documentEvent) {
            validateFields(documentEvent);
        }
        
        public void insertUpdate(javax.swing.event.DocumentEvent documentEvent) {
            validateFields(documentEvent);
        }
        
        public void removeUpdate(javax.swing.event.DocumentEvent documentEvent) {
            validateFields(documentEvent);
        }
    }
    
    public Project getSelectedProject() {
        Project project;
        if (.getSelectedIndex() > 0) {
            project = ;
            ConfigurationDescriptorProvider pdp = project.getLookup().lookup(ConfigurationDescriptorProvider.class);
            MakeConfigurationDescriptor projectDescriptor = pdp.getConfigurationDescriptor();
            MakeConfiguration conf = projectDescriptor.getActiveConfiguration();
            updateRunProfile(conf.getBaseDir(), conf.getProfile());
        } else {
            try {
                String projectFolder = ProjectGenerator.getDefaultProjectFolder();
                String projectName = ProjectGenerator.getValidProjectName(projectFoldernew File(getExecutablePath()).getName());
                String baseDir = projectFolder + . + projectName;
                MakeConfiguration conf = new MakeConfiguration(baseDir"Default".);  // NOI18N
                // Working dir
                String wd = new File(getExecutablePath()).getParentFile().getPath();
                wd = IpeUtils.toRelativePath(baseDirwd);
                wd = FilePathAdaptor.normalize(wd);
                conf.getMakefileConfiguration().getBuildCommandWorkingDir().setValue(wd);
                // Executable
                String exe = getExecutablePath();
                exe = IpeUtils.toRelativePath(baseDirexe);
                exe = FilePathAdaptor.normalize(exe);
                conf.getMakefileConfiguration().getOutput().setValue(exe);
                
                updateRunProfile(baseDirconf.getProfile());
                
                project = MakeProjectGenerator.createBlankProject(projectNameprojectFoldernew MakeConfiguration[] {conf}, true);
            } catch (Exception e) {
                project = null;
            }
             = project;
        }
        return project;
    }
    
    private void updateRunProfile(String baseDirRunProfile runProfile) {
        // Arguments
        runProfile.setArgs(.getText());
        // Working dir
        String wd = .getText();
        wd = IpeUtils.toRelativePath(baseDirwd);
        wd = FilePathAdaptor.normalize(wd);
        runProfile.setRunDirectory(wd);
        // Environment
        Env env = runProfile.getEnvironment();
	env.removeAll();
        env.decode(.getText());
    }
    
    public String getExecutablePath() {
        return .getText();
    }
    
    private void setErrorMsg(String msg) {
        .setText(msg);
    }
    
    private static DefaultPicklistModel getExecutablePicklist() {
        if ( == null) {
             = System.getProperty("netbeans.user") + . + "var" + . + "picklists"// NOI18N
             = (DefaultPicklistModel)DefaultPicklistModel.restorePicklist();
            if ( == null)
                 = new DefaultPicklistModel(16);
        }
        return ;
    }
    
    public static void addElementToExecutablePicklist(String exePath) {
        getExecutablePicklist().addElement(exePath);
    }
    
    
Look up i18n strings here
    private ResourceBundle bundle;
    private String getString(String s) {
        if ( == null) {
             = NbBundle.getBundle(RunDialogPanel.class);
        }
        return .getString(s);
    }
    
    public boolean asynchronous() {
        return false;
    }