samples/apifest1/infrastructure/testing-template/src/org/netbeans/apifest/testingtemplate/TestingTemplatePanelVisual.java
Adding samples from API fest to the repository, including pieces of their code in the document, not just links
1 package org.netbeans.apifest.testingtemplate;
4 import javax.swing.JFileChooser;
5 import javax.swing.JPanel;
6 import javax.swing.event.DocumentEvent;
7 import javax.swing.event.DocumentListener;
8 import javax.swing.text.Document;
9 import org.netbeans.spi.project.ui.support.ProjectChooser;
10 import org.openide.WizardDescriptor;
11 import org.openide.WizardValidationException;
12 import org.openide.filesystems.FileUtil;
14 public class TestingTemplatePanelVisual extends JPanel implements DocumentListener {
16 public static final String PROP_PROJECT_NAME = "projectName";
18 private TestingTemplateWizardPanel panel;
20 /** Creates new form PanelProjectLocationVisual */
21 public TestingTemplatePanelVisual(TestingTemplateWizardPanel panel) {
24 // Register listener on the textFields to make the automatic updates
25 projectNameTextField.getDocument().addDocumentListener(this);
26 projectLocationTextField.getDocument().addDocumentListener(this);
30 public String getProjectName() {
31 return this.projectNameTextField.getText();
34 /** This method is called from within the constructor to
35 * initialize the form.
36 * WARNING: Do NOT modify this code. The content of this method is
37 * always regenerated by the Form Editor.
39 // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
40 private void initComponents() {
41 projectNameLabel = new javax.swing.JLabel();
42 projectNameTextField = new javax.swing.JTextField();
43 projectLocationLabel = new javax.swing.JLabel();
44 projectLocationTextField = new javax.swing.JTextField();
45 browseButton = new javax.swing.JButton();
46 createdFolderLabel = new javax.swing.JLabel();
47 createdFolderTextField = new javax.swing.JTextField();
49 projectNameLabel.setLabelFor(projectNameTextField);
50 org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel, "Project &Name:");
52 projectLocationLabel.setLabelFor(projectLocationTextField);
53 org.openide.awt.Mnemonics.setLocalizedText(projectLocationLabel, "Project &Location:");
55 org.openide.awt.Mnemonics.setLocalizedText(browseButton, "Br&owse...");
56 browseButton.setActionCommand("BROWSE");
57 browseButton.addActionListener(new java.awt.event.ActionListener() {
58 public void actionPerformed(java.awt.event.ActionEvent evt) {
59 browseButtonActionPerformed(evt);
63 createdFolderLabel.setLabelFor(createdFolderTextField);
64 org.openide.awt.Mnemonics.setLocalizedText(createdFolderLabel, "Project &Folder:");
66 createdFolderTextField.setEditable(false);
68 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
69 this.setLayout(layout);
70 layout.setHorizontalGroup(
71 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
72 .add(layout.createSequentialGroup()
74 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
75 .add(projectNameLabel)
76 .add(projectLocationLabel)
77 .add(createdFolderLabel))
78 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
79 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
80 .add(org.jdesktop.layout.GroupLayout.TRAILING, projectNameTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
81 .add(org.jdesktop.layout.GroupLayout.TRAILING, projectLocationTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
82 .add(org.jdesktop.layout.GroupLayout.TRAILING, createdFolderTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE))
83 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
87 layout.setVerticalGroup(
88 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
89 .add(layout.createSequentialGroup()
91 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
92 .add(projectNameLabel)
93 .add(projectNameTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
94 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
95 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
96 .add(projectLocationLabel)
97 .add(projectLocationTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
99 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
100 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
101 .add(createdFolderLabel)
102 .add(createdFolderTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
103 .addContainerGap(213, Short.MAX_VALUE))
105 }// </editor-fold>//GEN-END:initComponents
107 private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButtonActionPerformed
108 String command = evt.getActionCommand();
109 if ("BROWSE".equals(command)) {
110 JFileChooser chooser = new JFileChooser();
111 FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
112 chooser.setDialogTitle("Select Project Location");
113 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
114 String path = this.projectLocationTextField.getText();
115 if (path.length() > 0) {
116 File f = new File(path);
118 chooser.setSelectedFile(f);
121 if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
122 File projectDir = chooser.getSelectedFile();
123 projectLocationTextField.setText(FileUtil.normalizeFile(projectDir).getAbsolutePath());
125 panel.fireChangeEvent();
128 }//GEN-LAST:event_browseButtonActionPerformed
131 // Variables declaration - do not modify//GEN-BEGIN:variables
132 private javax.swing.JButton browseButton;
133 private javax.swing.JLabel createdFolderLabel;
134 private javax.swing.JTextField createdFolderTextField;
135 private javax.swing.JLabel projectLocationLabel;
136 private javax.swing.JTextField projectLocationTextField;
137 private javax.swing.JLabel projectNameLabel;
138 private javax.swing.JTextField projectNameTextField;
139 // End of variables declaration//GEN-END:variables
141 public void addNotify() {
143 //same problem as in 31086, initial focus on Cancel button
144 projectNameTextField.requestFocus();
147 boolean valid(WizardDescriptor wizardDescriptor) {
149 if (projectNameTextField.getText().length() == 0) {
150 wizardDescriptor.putProperty("WizardPanel_errorMessage",
151 "Project Name is not a valid folder name.");
152 return false; // Display name not specified
154 File f = FileUtil.normalizeFile(new File(projectLocationTextField.getText()).getAbsoluteFile());
155 if (!f.isDirectory()) {
156 String message = "Project Folder is not a valid path.";
157 wizardDescriptor.putProperty("WizardPanel_errorMessage", message);
160 final File destFolder = FileUtil.normalizeFile(new File(createdFolderTextField.getText()).getAbsoluteFile());
162 File projLoc = destFolder;
163 while (projLoc != null && !projLoc.exists()) {
164 projLoc = projLoc.getParentFile();
166 if (projLoc == null || !projLoc.canWrite()) {
167 wizardDescriptor.putProperty("WizardPanel_errorMessage",
168 "Project Folder cannot be created.");
172 if (FileUtil.toFileObject(projLoc) == null) {
173 String message = "Project Folder is not a valid path.";
174 wizardDescriptor.putProperty("WizardPanel_errorMessage", message);
178 File[] kids = destFolder.listFiles();
179 if (destFolder.exists() && kids != null && kids.length > 0) {
180 // Folder exists and is not empty
181 wizardDescriptor.putProperty("WizardPanel_errorMessage",
182 "Project Folder already exists and is not empty.");
185 wizardDescriptor.putProperty("WizardPanel_errorMessage", "");
189 void store(WizardDescriptor d) {
190 String name = projectNameTextField.getText().trim();
191 String folder = createdFolderTextField.getText().trim();
193 d.putProperty("projdir", new File(folder));
194 d.putProperty("name", name);
197 void read(WizardDescriptor settings) {
198 File projectLocation = (File) settings.getProperty("projdir");
199 if (projectLocation == null || projectLocation.getParentFile() == null || !projectLocation.getParentFile().isDirectory()) {
200 projectLocation = ProjectChooser.getProjectsFolder();
202 projectLocation = projectLocation.getParentFile();
204 this.projectLocationTextField.setText(projectLocation.getAbsolutePath());
206 String projectName = (String) settings.getProperty("name");
207 if(projectName == null) {
208 projectName = "TestingTemplate";
210 this.projectNameTextField.setText(projectName);
211 this.projectNameTextField.selectAll();
214 void validate(WizardDescriptor d) throws WizardValidationException {
215 // nothing to validate
218 // Implementation of DocumentListener --------------------------------------
220 public void changedUpdate(DocumentEvent e) {
222 if (this.projectNameTextField.getDocument() == e.getDocument()) {
223 firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText());
227 public void insertUpdate(DocumentEvent e) {
229 if (this.projectNameTextField.getDocument() == e.getDocument()) {
230 firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText());
234 public void removeUpdate(DocumentEvent e) {
236 if (this.projectNameTextField.getDocument() == e.getDocument()) {
237 firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText());
241 /** Handles changes in the Project name and project directory, */
242 private void updateTexts(DocumentEvent e) {
244 Document doc = e.getDocument();
246 if (doc == projectNameTextField.getDocument() || doc == projectLocationTextField.getDocument()) {
247 // Change in the project name
249 String projectName = projectNameTextField.getText();
250 String projectFolder = projectLocationTextField.getText();
252 //if (projectFolder.trim().length() == 0 || projectFolder.equals(oldName)) {
253 createdFolderTextField.setText(projectFolder + File.separatorChar + projectName);
257 panel.fireChangeEvent(); // Notify that the panel changed