1 19 20 package org.netbeans.modules.project.ui.groups; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.Arrays ; 25 import java.util.HashSet ; 26 import javax.swing.JFileChooser ; 27 import javax.swing.event.DocumentEvent ; 28 import javax.swing.event.DocumentListener ; 29 import org.netbeans.api.project.Project; 30 import org.netbeans.api.project.ProjectManager; 31 import org.netbeans.api.project.ProjectUtils; 32 import org.netbeans.api.project.ui.OpenProjects; 33 import org.netbeans.spi.project.ui.support.ProjectChooser; 34 import org.openide.filesystems.FileObject; 35 import org.openide.filesystems.FileStateInvalidException; 36 import org.openide.filesystems.FileUtil; 37 import org.openide.util.Exceptions; 38 39 44 public class NewGroupPanel extends AbstractNewGroupPanel { 45 46 public NewGroupPanel() { 47 initComponents(); 48 DocumentListener l = new DocumentListener () { 49 public void insertUpdate(DocumentEvent e) { 50 firePropertyChange(PROP_READY, null, null); 51 } 52 public void removeUpdate(DocumentEvent e) { 53 firePropertyChange(PROP_READY, null, null); 54 } 55 public void changedUpdate(DocumentEvent e) {} 56 }; 57 directoryField.getDocument().addDocumentListener(l); 58 nameField.getDocument().addDocumentListener(l); 59 updateNameField(); 60 } 61 62 public boolean isReady() { 63 if (adHocKindRadio.isSelected()) { 64 return nameField.getText() != null && nameField.getText().trim().length() > 0; 65 } else if (subprojectsKindRadio.isSelected()) { 66 String s = masterProjectField.getText(); 67 if (s != null && s.length() > 0) { 68 File f = new File (s); 69 FileObject fo = FileUtil.toFileObject(f); 70 if (fo != null && fo.isFolder()) { 71 try { 72 return ProjectManager.getDefault().findProject(fo) != null; 73 } catch (IOException x) { 74 Exceptions.printStackTrace(x); 75 } 76 } 77 } 78 return false; 79 } else { 80 assert directoryKindRadio.isSelected(); 81 if (nameField.getText() == null || nameField.getText().trim().length() == 0) { 82 return false; 83 } 84 String s = directoryField.getText(); 85 if (s != null) { 86 return new File (s.trim()).isDirectory(); 87 } else { 88 return false; 89 } 90 } 91 } 92 93 private void updateNameField() { 94 if (adHocKindRadio.isSelected() && useOpenCheckbox.isSelected()) { 95 Project p = OpenProjects.getDefault().getMainProject(); 96 if (p != null) { 97 nameField.setText(ProjectUtils.getInformation(p).getDisplayName()); 98 } 99 } else if (subprojectsKindRadio.isSelected()) { 100 String s = masterProjectField.getText(); 101 if (s != null && s.length() > 0) { 102 File f = new File (s); 103 FileObject fo = FileUtil.toFileObject(f); 104 if (fo != null && fo.isFolder()) { 105 try { 106 Project p = ProjectManager.getDefault().findProject(fo); 107 if (p != null) { 108 nameField.setText(ProjectUtils.getInformation(p).getDisplayName()); 109 } 110 } catch (IOException x) { 111 Exceptions.printStackTrace(x); 112 } 113 } 114 } 115 } else if (directoryKindRadio.isSelected()) { 116 String s = directoryField.getText(); 117 if (s != null && s.length() > 0) { 118 File f = new File (s); 119 nameField.setText(f.getName()); 120 } 121 } 122 } 123 124 public Group create() { 125 assert isReady(); 126 if (adHocKindRadio.isSelected()) { 127 AdHocGroup g = AdHocGroup.create(nameField.getText().trim(), autoSynchCheckbox.isSelected()); 128 if (useOpenCheckbox.isSelected()) { 129 g.setProjects(new HashSet <Project>(Arrays.asList(OpenProjects.getDefault().getOpenProjects()))); 130 g.setMainProject(OpenProjects.getDefault().getMainProject()); 131 } 132 return g; 133 } else if (subprojectsKindRadio.isSelected()) { 134 FileObject fo = FileUtil.toFileObject(new File (masterProjectField.getText())); 135 try { 136 return SubprojectsGroup.create(ProjectManager.getDefault().findProject(fo)); 137 } catch (IOException x) { 138 throw new AssertionError (x); 139 } 140 } else { 141 assert directoryKindRadio.isSelected(); 142 FileObject f = FileUtil.toFileObject(FileUtil.normalizeFile(new File (directoryField.getText().trim()))); 143 try { 144 return DirectoryGroup.create(nameField.getText().trim(), f); 145 } catch (FileStateInvalidException x) { 146 throw new AssertionError (x); 147 } 148 } 149 } 150 151 156 private void initComponents() { 158 159 kindButtonGroup = new javax.swing.ButtonGroup (); 160 adHocKindRadio = new javax.swing.JRadioButton (); 161 adHocKindLabel = new javax.swing.JLabel (); 162 useOpenCheckbox = new javax.swing.JCheckBox (); 163 autoSynchCheckbox = new javax.swing.JCheckBox (); 164 subprojectsKindRadio = new javax.swing.JRadioButton (); 165 subprojectsKindLabel = new javax.swing.JLabel (); 166 masterProjectLabel = new javax.swing.JLabel (); 167 masterProjectField = new javax.swing.JTextField (); 168 masterProjectButton = new javax.swing.JButton (); 169 directoryKindRadio = new javax.swing.JRadioButton (); 170 directoryKindLabel = new javax.swing.JLabel (); 171 directoryLabel = new javax.swing.JLabel (); 172 directoryField = new javax.swing.JTextField (); 173 directoryButton = new javax.swing.JButton (); 174 nameLabel = new javax.swing.JLabel (); 175 nameField = new javax.swing.JTextField (); 176 177 kindButtonGroup.add(adHocKindRadio); 178 adHocKindRadio.setSelected(true); 179 org.openide.awt.Mnemonics.setLocalizedText(adHocKindRadio, org.openide.util.NbBundle.getMessage(NewGroupPanel.class, "NewGroupPanel.adHocKindRadio.text")); adHocKindRadio.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 181 adHocKindRadio.addActionListener(new java.awt.event.ActionListener () { 182 public void actionPerformed(java.awt.event.ActionEvent evt) { 183 adHocKindRadioActionPerformed(evt); 184 } 185 }); 186 187 adHocKindLabel.setLabelFor(adHocKindRadio); 188 org.openide.awt.Mnemonics.setLocalizedText(adHocKindLabel, org.openide.util.NbBundle.getMessage(NewGroupPanel.class, "NewGroupPanel.adHocKindLabel.text")); 190 useOpenCheckbox.setSelected(true); 191 org.openide.awt.Mnemonics.setLocalizedText(useOpenCheckbox, org.openide.util.NbBundle.getMessage(NewGroupPanel.class, "NewGroupPanel.useOpenCheckbox.text")); 193 autoSynchCheckbox.setSelected(true); 194 org.openide.awt.Mnemonics.setLocalizedText(autoSynchCheckbox, org.openide.util.NbBundle.getMessage(NewGroupPanel.class, "NewGroupPanel.autoSynchCheckbox.text")); 196 kindButtonGroup.add(subprojectsKindRadio); 197 org.openide.awt.Mnemonics.setLocalizedText(subprojectsKindRadio, org.openide.util.NbBundle.getMessage(NewGroupPanel.class, "NewGroupPanel.subprojectsKindRadio.text")); subprojectsKindRadio.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 199 subprojectsKindRadio.addActionListener(new java.awt.event.ActionListener () { 200 public void actionPerformed(java.awt.event.ActionEvent evt) { 201 subprojectsKindRadioActionPerformed(evt); 202 } 203 }); 204 205 subprojectsKindLabel.setLabelFor(subprojectsKindRadio); 206 org.openide.awt.Mnemonics.setLocalizedText(subprojectsKindLabel, org.openide.util.NbBundle.getMessage(NewGroupPanel.class, "NewGroupPanel.subprojectsKindLabel.text")); 208 masterProjectLabel.setLabelFor(masterProjectField); 209 org.openide.awt.Mnemonics.setLocalizedText(masterProjectLabel, org.openide.util.NbBundle.getMessage(NewGroupPanel.class, "NewGroupPanel.masterProjectLabel.text")); masterProjectLabel.setEnabled(false); 211 212 masterProjectField.setEditable(false); 213 masterProjectField.setEnabled(false); 214 215 org.openide.awt.Mnemonics.setLocalizedText(masterProjectButton, org.openide.util.NbBundle.getMessage(NewGroupPanel.class, "NewGroupPanel.masterProjectButton.text")); masterProjectButton.setEnabled(false); 217 masterProjectButton.addActionListener(new java.awt.event.ActionListener () { 218 public void actionPerformed(java.awt.event.ActionEvent evt) { 219 masterProjectButtonActionPerformed(evt); 220 } 221 }); 222 223 kindButtonGroup.add(directoryKindRadio); 224 org.openide.awt.Mnemonics.setLocalizedText(directoryKindRadio, org.openide.util.NbBundle.getMessage(NewGroupPanel.class, "NewGroupPanel.directoryKindRadio.text")); directoryKindRadio.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 226 directoryKindRadio.addActionListener(new java.awt.event.ActionListener () { 227 public void actionPerformed(java.awt.event.ActionEvent evt) { 228 directoryKindRadioActionPerformed(evt); 229 } 230 }); 231 232 directoryKindLabel.setLabelFor(directoryKindRadio); 233 org.openide.awt.Mnemonics.setLocalizedText(directoryKindLabel, org.openide.util.NbBundle.getMessage(NewGroupPanel.class, "NewGroupPanel.directoryKindLabel.text")); 235 directoryLabel.setLabelFor(directoryField); 236 org.openide.awt.Mnemonics.setLocalizedText(directoryLabel, org.openide.util.NbBundle.getMessage(NewGroupPanel.class, "NewGroupPanel.directoryLabel.text")); directoryLabel.setEnabled(false); 238 239 directoryField.setEnabled(false); 240 241 org.openide.awt.Mnemonics.setLocalizedText(directoryButton, org.openide.util.NbBundle.getMessage(NewGroupPanel.class, "NewGroupPanel.directoryButton.text")); directoryButton.setEnabled(false); 243 directoryButton.addActionListener(new java.awt.event.ActionListener () { 244 public void actionPerformed(java.awt.event.ActionEvent evt) { 245 directoryButtonActionPerformed(evt); 246 } 247 }); 248 249 nameLabel.setLabelFor(nameField); 250 org.openide.awt.Mnemonics.setLocalizedText(nameLabel, org.openide.util.NbBundle.getMessage(NewGroupPanel.class, "NewGroupPanel.nameLabel.text")); 252 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 253 this.setLayout(layout); 254 layout.setHorizontalGroup( 255 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 256 .add(layout.createSequentialGroup() 257 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 258 .add(layout.createSequentialGroup() 259 .add(12, 12, 12) 260 .add(directoryKindRadio)) 261 .add(layout.createSequentialGroup() 262 .add(12, 12, 12) 263 .add(nameLabel) 264 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 265 .add(nameField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 546, Short.MAX_VALUE)) 266 .add(layout.createSequentialGroup() 267 .add(12, 12, 12) 268 .add(adHocKindRadio)) 269 .add(layout.createSequentialGroup() 270 .add(29, 29, 29) 271 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 272 .add(layout.createSequentialGroup() 273 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) 274 .add(layout.createSequentialGroup() 275 .add(directoryLabel) 276 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 277 .add(directoryField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)) 278 .add(layout.createSequentialGroup() 279 .add(103, 103, 103) 280 .add(masterProjectField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 381, Short.MAX_VALUE))) 281 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 282 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 283 .add(org.jdesktop.layout.GroupLayout.TRAILING, masterProjectButton) 284 .add(org.jdesktop.layout.GroupLayout.TRAILING, directoryButton))) 285 .add(directoryKindLabel))) 286 .add(layout.createSequentialGroup() 287 .add(12, 12, 12) 288 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false) 289 .add(subprojectsKindRadio) 290 .add(layout.createSequentialGroup() 291 .add(17, 17, 17) 292 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false) 293 .add(layout.createSequentialGroup() 294 .add(masterProjectLabel) 295 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 315, Short.MAX_VALUE)) 296 .add(subprojectsKindLabel))) 297 .add(layout.createSequentialGroup() 298 .add(17, 17, 17) 299 .add(adHocKindLabel)) 300 .add(layout.createSequentialGroup() 301 .add(17, 17, 17) 302 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 303 .add(autoSynchCheckbox) 304 .add(useOpenCheckbox)))) 305 .add(115, 115, 115))) 306 .addContainerGap()) 307 ); 308 layout.setVerticalGroup( 309 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 310 .add(layout.createSequentialGroup() 311 .add(12, 12, 12) 312 .add(adHocKindRadio) 313 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 314 .add(adHocKindLabel) 315 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 316 .add(useOpenCheckbox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 23, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 317 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 318 .add(autoSynchCheckbox) 319 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 320 .add(subprojectsKindRadio) 321 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 322 .add(subprojectsKindLabel) 323 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 324 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 325 .add(masterProjectButton) 326 .add(masterProjectLabel) 327 .add(masterProjectField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 328 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 329 .add(directoryKindRadio) 330 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 331 .add(directoryKindLabel) 332 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 333 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 334 .add(directoryButton) 335 .add(directoryLabel) 336 .add(directoryField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 337 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 338 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 339 .add(nameLabel) 340 .add(nameField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 341 .addContainerGap()) 342 ); 343 } 345 private void directoryButtonActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser chooser = new JFileChooser (); 347 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 348 chooser.setMultiSelectionEnabled(false); 349 File start = ProjectChooser.getProjectsFolder(); 350 if (directoryField.getText() != null && directoryField.getText().trim().length() > 0) { 351 start = new File (directoryField.getText().trim()); 352 } 353 FileUtil.preventFileChooserSymlinkTraversal(chooser, start); 354 if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { 355 File f = chooser.getSelectedFile(); 356 if (f != null) { 357 directoryField.setText(f.getAbsolutePath()); 358 updateNameField(); 359 } 360 } 361 } 363 private void masterProjectButtonActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser chooser = ProjectChooser.projectChooser(); 365 if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { 366 File f = chooser.getSelectedFile(); 367 if (f != null) { 368 masterProjectField.setText(f.getAbsolutePath()); 369 updateNameField(); 370 firePropertyChange(PROP_READY, null, null); 371 } 372 } 373 } 375 private void directoryKindRadioActionPerformed(java.awt.event.ActionEvent evt) { useOpenCheckbox.setEnabled(false); 377 autoSynchCheckbox.setEnabled(false); 378 masterProjectLabel.setEnabled(false); 379 masterProjectField.setEnabled(false); 380 masterProjectButton.setEnabled(false); 381 directoryLabel.setEnabled(true); 382 directoryField.setEnabled(true); 383 directoryButton.setEnabled(true); 384 updateNameField(); 385 firePropertyChange(PROP_READY, null, null); 386 } 388 private void subprojectsKindRadioActionPerformed(java.awt.event.ActionEvent evt) { useOpenCheckbox.setEnabled(false); 390 autoSynchCheckbox.setEnabled(false); 391 masterProjectLabel.setEnabled(true); 392 masterProjectField.setEnabled(true); 393 masterProjectButton.setEnabled(true); 394 directoryLabel.setEnabled(false); 395 directoryField.setEnabled(false); 396 directoryButton.setEnabled(false); 397 updateNameField(); 398 firePropertyChange(PROP_READY, null, null); 399 } 401 private void adHocKindRadioActionPerformed(java.awt.event.ActionEvent evt) { useOpenCheckbox.setEnabled(true); 403 autoSynchCheckbox.setEnabled(true); 404 masterProjectLabel.setEnabled(false); 405 masterProjectField.setEnabled(false); 406 masterProjectButton.setEnabled(false); 407 directoryLabel.setEnabled(false); 408 directoryField.setEnabled(false); 409 directoryButton.setEnabled(false); 410 updateNameField(); 411 firePropertyChange(PROP_READY, null, null); 412 } 414 415 private javax.swing.JLabel adHocKindLabel; 417 private javax.swing.JRadioButton adHocKindRadio; 418 private javax.swing.JCheckBox autoSynchCheckbox; 419 private javax.swing.JButton directoryButton; 420 private javax.swing.JTextField directoryField; 421 private javax.swing.JLabel directoryKindLabel; 422 private javax.swing.JRadioButton directoryKindRadio; 423 private javax.swing.JLabel directoryLabel; 424 private javax.swing.ButtonGroup kindButtonGroup; 425 private javax.swing.JButton masterProjectButton; 426 private javax.swing.JTextField masterProjectField; 427 private javax.swing.JLabel masterProjectLabel; 428 private javax.swing.JTextField nameField; 429 private javax.swing.JLabel nameLabel; 430 private javax.swing.JLabel subprojectsKindLabel; 431 private javax.swing.JRadioButton subprojectsKindRadio; 432 private javax.swing.JCheckBox useOpenCheckbox; 433 435 } 436 | Popular Tags |