1 19 20 package org.netbeans.modules.project.ui; 21 22 import java.awt.Component ; 23 import java.awt.event.ActionListener ; 24 import java.io.File ; 25 import java.text.MessageFormat ; 26 import java.util.ArrayList ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import javax.swing.DefaultComboBoxModel ; 30 import javax.swing.JLabel ; 31 import javax.swing.JList ; 32 import javax.swing.ListCellRenderer ; 33 import javax.swing.event.ChangeEvent ; 34 import javax.swing.event.ChangeListener ; 35 import javax.swing.event.DocumentListener ; 36 import org.netbeans.api.project.Project; 37 import org.netbeans.api.project.ProjectUtils; 38 import org.netbeans.api.project.SourceGroup; 39 import org.netbeans.api.project.Sources; 40 import org.netbeans.spi.project.support.GenericSources; 41 import org.openide.awt.Mnemonics; 42 import org.openide.filesystems.FileObject; 43 import org.openide.filesystems.FileUtil; 44 import org.openide.loaders.DataObject; 45 import org.openide.loaders.DataObjectNotFoundException; 46 import org.openide.util.NbBundle; 47 48 52 public class SimpleTargetChooserPanelGUI extends javax.swing.JPanel implements ActionListener , DocumentListener { 53 54 55 private static final java.awt.Dimension PREF_DIM = new java.awt.Dimension (500, 340); 56 57 private static final String NEW_FILE_PREFIX = 58 NbBundle.getMessage( SimpleTargetChooserPanelGUI.class, "LBL_SimpleTargetChooserPanelGUI_NewFilePrefix" ); 60 private final ListCellRenderer CELL_RENDERER = new GroupCellRenderer(); 61 62 private Project project; 63 private String expectedExtension; 64 private final List <ChangeListener > listeners = new ArrayList <ChangeListener >(); 65 private SourceGroup[] folders; 66 private boolean isFolder; 67 68 69 public SimpleTargetChooserPanelGUI( Project project, SourceGroup[] folders, Component bottomPanel, boolean isFolder ) { 70 this.project = project; 71 this.folders=folders; 72 this.isFolder = isFolder; 73 initComponents(); 74 75 locationComboBox.setRenderer( CELL_RENDERER ); 76 77 if ( bottomPanel != null ) { 78 bottomPanelContainer.add( bottomPanel, java.awt.BorderLayout.CENTER ); 79 } 80 initValues( null, null ); 81 82 browseButton.addActionListener( this ); 83 locationComboBox.addActionListener( this ); 84 documentNameTextField.getDocument().addDocumentListener( this ); 85 folderTextField.getDocument().addDocumentListener( this ); 86 87 setName (NbBundle.getMessage(SimpleTargetChooserPanelGUI.class, "LBL_SimpleTargetChooserPanel_Name")); } 89 90 public void initValues( FileObject template, FileObject preselectedFolder ) { 91 initValues(template, preselectedFolder, null); 92 } 93 94 public void initValues( FileObject template, FileObject preselectedFolder, String documentName ) { 95 assert project != null; 96 97 projectTextField.setText(ProjectUtils.getInformation(project).getDisplayName()); 98 99 Sources sources = ProjectUtils.getSources( project ); 100 101 folders = sources.getSourceGroups( Sources.TYPE_GENERIC ); 102 103 if ( folders.length < 2 ) { 104 locationLabel.setVisible( false ); 106 locationComboBox.setVisible( false ); 107 } 108 else { 109 locationLabel.setVisible( true ); 111 locationComboBox.setVisible( true ); 112 113 } 114 115 locationComboBox.setModel( new DefaultComboBoxModel ( folders ) ); 116 SourceGroup preselectedGroup = getPreselectedGroup( folders, preselectedFolder ); 118 locationComboBox.setSelectedItem( preselectedGroup ); 119 folderTextField.setText( getRelativeNativeName( preselectedGroup.getRootFolder(), preselectedFolder ) ); 121 122 String ext = template == null ? "" : template.getExt(); expectedExtension = ext.length() == 0 ? "" : "." + ext; 125 String displayName = null; 126 try { 127 if (template != null) { 128 DataObject templateDo = DataObject.find (template); 129 displayName = templateDo.getNodeDelegate ().getDisplayName (); 130 } 131 } catch (DataObjectNotFoundException ex) { 132 displayName = template.getName (); 133 } 134 putClientProperty ("NewFileWizard_Title", displayName); 136 if (template != null) { 137 if (documentName == null) { 138 documentName = NEW_FILE_PREFIX + template.getName (); 139 } 140 documentNameTextField.setText (documentName); 141 documentNameTextField.selectAll (); 142 } 143 144 if (isFolder) { 145 Mnemonics.setLocalizedText(jLabel3, NbBundle.getMessage(SimpleTargetChooserPanelGUI.class, "LBL_TargetChooser_FolderName_Label")); Mnemonics.setLocalizedText(jLabel2, NbBundle.getMessage(SimpleTargetChooserPanelGUI.class, "LBL_TargetChooser_ParentFolder_Label")); Mnemonics.setLocalizedText(jLabel4, NbBundle.getMessage(SimpleTargetChooserPanelGUI.class, "LBL_TargetChooser_CreatedFolder_Label")); } else { 149 Mnemonics.setLocalizedText(jLabel3, NbBundle.getMessage(SimpleTargetChooserPanelGUI.class, "LBL_TargetChooser_FileName_Label")); Mnemonics.setLocalizedText(jLabel2, NbBundle.getMessage(SimpleTargetChooserPanelGUI.class, "LBL_TargetChooser_Folder_Label")); Mnemonics.setLocalizedText(jLabel4, NbBundle.getMessage(SimpleTargetChooserPanelGUI.class, "LBL_TargetChooser_CreatedFile_Label")); } 153 } 154 155 public SourceGroup getTargetGroup() { 156 return (SourceGroup)locationComboBox.getSelectedItem(); 157 } 158 159 public String getTargetFolder() { 160 161 String folderName = folderTextField.getText().trim(); 162 163 if ( folderName.length() == 0 ) { 164 return null; 165 } 166 else { 167 return folderName.replace( File.separatorChar, '/' ); } 169 } 170 171 public String getTargetName() { 172 173 String text = documentNameTextField.getText().trim(); 174 175 if ( text.length() == 0 ) { 176 return null; 177 } 178 else { 179 return text; 180 } 181 } 182 183 184 public java.awt.Dimension getPreferredSize() { 185 return PREF_DIM; 186 } 187 188 public synchronized void addChangeListener(ChangeListener l) { 189 listeners.add(l); 190 } 191 192 public synchronized void removeChangeListener(ChangeListener l) { 193 listeners.remove(l); 194 } 195 196 private void fireChange() { 197 ChangeEvent e = new ChangeEvent (this); 198 List <ChangeListener > templist; 199 synchronized (this) { 200 templist = new ArrayList <ChangeListener > (listeners); 201 } 202 for (ChangeListener l: templist) { 203 l.stateChanged(e); 204 } 205 } 206 207 212 private void initComponents() { 214 java.awt.GridBagConstraints gridBagConstraints; 215 216 jPanel1 = new javax.swing.JPanel (); 217 jLabel3 = new javax.swing.JLabel (); 218 documentNameTextField = new javax.swing.JTextField (); 219 jLabel1 = new javax.swing.JLabel (); 220 projectTextField = new javax.swing.JTextField (); 221 locationLabel = new javax.swing.JLabel (); 222 locationComboBox = new javax.swing.JComboBox (); 223 jLabel2 = new javax.swing.JLabel (); 224 folderTextField = new javax.swing.JTextField (); 225 browseButton = new javax.swing.JButton (); 226 jLabel4 = new javax.swing.JLabel (); 227 fileTextField = new javax.swing.JTextField (); 228 targetSeparator = new javax.swing.JSeparator (); 229 bottomPanelContainer = new javax.swing.JPanel (); 230 231 setLayout(new java.awt.GridBagLayout ()); 232 233 jPanel1.setLayout(new java.awt.GridBagLayout ()); 234 235 jLabel3.setLabelFor(documentNameTextField); 236 org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(SimpleTargetChooserPanelGUI.class, "LBL_TargetChooser_FileName_Label")); gridBagConstraints = new java.awt.GridBagConstraints (); 238 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 239 jPanel1.add(jLabel3, gridBagConstraints); 240 gridBagConstraints = new java.awt.GridBagConstraints (); 241 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 242 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 243 gridBagConstraints.weightx = 1.0; 244 gridBagConstraints.insets = new java.awt.Insets (0, 6, 0, 0); 245 jPanel1.add(documentNameTextField, gridBagConstraints); 246 documentNameTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(SimpleTargetChooserPanelGUI.class).getString("AD_documentNameTextField")); 248 gridBagConstraints = new java.awt.GridBagConstraints (); 249 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 250 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 251 gridBagConstraints.insets = new java.awt.Insets (0, 0, 24, 0); 252 add(jPanel1, gridBagConstraints); 253 254 jLabel1.setLabelFor(projectTextField); 255 org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(SimpleTargetChooserPanelGUI.class, "LBL_TargetChooser_Project_Label")); gridBagConstraints = new java.awt.GridBagConstraints (); 257 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 258 gridBagConstraints.insets = new java.awt.Insets (0, 0, 6, 0); 259 add(jLabel1, gridBagConstraints); 260 261 projectTextField.setEditable(false); 262 gridBagConstraints = new java.awt.GridBagConstraints (); 263 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 264 gridBagConstraints.weightx = 1.0; 265 gridBagConstraints.insets = new java.awt.Insets (0, 6, 6, 0); 266 add(projectTextField, gridBagConstraints); 267 projectTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(SimpleTargetChooserPanelGUI.class).getString("AD_projectTextField")); 269 locationLabel.setLabelFor(locationComboBox); 270 org.openide.awt.Mnemonics.setLocalizedText(locationLabel, org.openide.util.NbBundle.getMessage(SimpleTargetChooserPanelGUI.class, "LBL_TargetChooser_Location_Label")); gridBagConstraints = new java.awt.GridBagConstraints (); 272 gridBagConstraints.gridy = 2; 273 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 274 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 0); 275 add(locationLabel, gridBagConstraints); 276 gridBagConstraints = new java.awt.GridBagConstraints (); 277 gridBagConstraints.gridy = 2; 278 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 279 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 280 gridBagConstraints.insets = new java.awt.Insets (0, 6, 5, 0); 281 add(locationComboBox, gridBagConstraints); 282 locationComboBox.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(SimpleTargetChooserPanelGUI.class).getString("AD_locationComboBox")); 284 jLabel2.setLabelFor(folderTextField); 285 org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(SimpleTargetChooserPanelGUI.class, "LBL_TargetChooser_Folder_Label")); gridBagConstraints = new java.awt.GridBagConstraints (); 287 gridBagConstraints.gridx = 0; 288 gridBagConstraints.gridy = 3; 289 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 290 gridBagConstraints.insets = new java.awt.Insets (0, 0, 12, 0); 291 add(jLabel2, gridBagConstraints); 292 gridBagConstraints = new java.awt.GridBagConstraints (); 293 gridBagConstraints.gridy = 3; 294 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 295 gridBagConstraints.weightx = 1.0; 296 gridBagConstraints.insets = new java.awt.Insets (0, 6, 12, 0); 297 add(folderTextField, gridBagConstraints); 298 folderTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(SimpleTargetChooserPanelGUI.class).getString("AD_folderTextField")); 300 org.openide.awt.Mnemonics.setLocalizedText(browseButton, org.openide.util.NbBundle.getMessage(SimpleTargetChooserPanelGUI.class, "LBL_TargetChooser_Browse_Button")); gridBagConstraints = new java.awt.GridBagConstraints (); 302 gridBagConstraints.gridy = 3; 303 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 304 gridBagConstraints.insets = new java.awt.Insets (0, 6, 12, 0); 305 add(browseButton, gridBagConstraints); 306 browseButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(SimpleTargetChooserPanelGUI.class).getString("AD_browseButton")); 308 jLabel4.setLabelFor(fileTextField); 309 org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(SimpleTargetChooserPanelGUI.class, "LBL_TargetChooser_CreatedFile_Label")); gridBagConstraints = new java.awt.GridBagConstraints (); 311 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 312 gridBagConstraints.insets = new java.awt.Insets (0, 0, 12, 0); 313 add(jLabel4, gridBagConstraints); 314 315 fileTextField.setEditable(false); 316 gridBagConstraints = new java.awt.GridBagConstraints (); 317 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 318 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 319 gridBagConstraints.weightx = 1.0; 320 gridBagConstraints.insets = new java.awt.Insets (0, 6, 12, 0); 321 add(fileTextField, gridBagConstraints); 322 fileTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(SimpleTargetChooserPanelGUI.class).getString("AD_fileTextField")); 324 gridBagConstraints = new java.awt.GridBagConstraints (); 325 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 326 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 327 gridBagConstraints.insets = new java.awt.Insets (0, 0, 12, 0); 328 add(targetSeparator, gridBagConstraints); 329 330 bottomPanelContainer.setLayout(new java.awt.BorderLayout ()); 331 gridBagConstraints = new java.awt.GridBagConstraints (); 332 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 333 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 334 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 335 gridBagConstraints.weighty = 1.0; 336 add(bottomPanelContainer, gridBagConstraints); 337 338 getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(SimpleTargetChooserPanelGUI.class).getString("AD_SimpleTargetChooserPanelGUI")); } 341 342 343 private javax.swing.JPanel bottomPanelContainer; 345 private javax.swing.JButton browseButton; 346 private javax.swing.JTextField documentNameTextField; 347 private javax.swing.JTextField fileTextField; 348 private javax.swing.JTextField folderTextField; 349 private javax.swing.JLabel jLabel1; 350 private javax.swing.JLabel jLabel2; 351 private javax.swing.JLabel jLabel3; 352 private javax.swing.JLabel jLabel4; 353 private javax.swing.JPanel jPanel1; 354 private javax.swing.JComboBox locationComboBox; 355 private javax.swing.JLabel locationLabel; 356 private javax.swing.JTextField projectTextField; 357 private javax.swing.JSeparator targetSeparator; 358 360 private SourceGroup getPreselectedGroup( SourceGroup[] groups, FileObject folder ) { 361 for( int i = 0; folder != null && i < groups.length; i++ ) { 362 if( FileUtil.isParentOf( groups[i].getRootFolder(), folder ) 363 || groups[i].getRootFolder().equals(folder)) { 364 return groups[i]; 365 } 366 } 367 return groups[0]; 368 } 369 370 private String getRelativeNativeName( FileObject root, FileObject folder ) { 371 if (root == null) { 372 throw new NullPointerException ("null root passed to getRelativeNativeName"); } 374 375 String path; 376 377 if (folder == null) { 378 path = ""; } 380 else { 381 path = FileUtil.getRelativePath( root, folder ); 382 } 383 384 return path == null ? "" : path.replace( '/', File.separatorChar ); } 386 387 private void updateCreatedFolder() { 388 389 FileObject root = ((SourceGroup)locationComboBox.getSelectedItem()).getRootFolder(); 390 391 String folderName = folderTextField.getText().trim(); 392 String documentName = documentNameTextField.getText().trim(); 393 394 String createdFileName = FileUtil.getFileDisplayName( root ) + 395 ( folderName.startsWith("/") || folderName.startsWith( File.separator ) ? "" : "/" ) + folderName + 397 ( folderName.endsWith("/") || folderName.endsWith( File.separator ) || folderName.length() == 0 ? "" : "/" ) + documentName + expectedExtension; 399 400 fileTextField.setText( createdFileName.replace( '/', File.separatorChar ) ); 402 fireChange(); 403 } 404 405 406 408 public void actionPerformed(java.awt.event.ActionEvent e) { 409 if ( browseButton == e.getSource() ) { 410 FileObject fo=null; 411 413 SourceGroup group = (SourceGroup)locationComboBox.getSelectedItem(); 414 415 fo = BrowseFolders.showDialog( new SourceGroup[] { group }, 416 project, 417 folderTextField.getText().replace( File.separatorChar, '/' ) ); 419 if ( fo != null && fo.isFolder() ) { 420 String relPath = FileUtil.getRelativePath( group.getRootFolder(), fo ); 421 folderTextField.setText( relPath.replace( '/', File.separatorChar ) ); } 423 } 424 else if ( locationComboBox == e.getSource() ) { 425 updateCreatedFolder(); 426 } 427 } 428 429 431 public void changedUpdate(javax.swing.event.DocumentEvent e) { 432 updateCreatedFolder(); 433 } 434 435 public void insertUpdate(javax.swing.event.DocumentEvent e) { 436 updateCreatedFolder(); 437 } 438 439 public void removeUpdate(javax.swing.event.DocumentEvent e) { 440 updateCreatedFolder(); 441 } 442 443 444 446 private class GroupCellRenderer extends JLabel implements ListCellRenderer { 447 448 public GroupCellRenderer() { 449 setOpaque( true ); 450 } 451 452 public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus ) { 453 if (value instanceof SourceGroup) { 454 SourceGroup group = (SourceGroup)value; 455 String projectDisplayName = ProjectUtils.getInformation( project ).getDisplayName(); 456 String groupDisplayName = group.getDisplayName(); 457 if ( projectDisplayName.equals( groupDisplayName ) ) { 458 setText( groupDisplayName ); 459 } 460 else { 461 setText( MessageFormat.format( PhysicalView.GroupNode.GROUP_NAME_PATTERN, 462 new Object [] { groupDisplayName, projectDisplayName, group.getRootFolder().getName() } ) ); 463 468 } 469 470 setIcon( group.getIcon( false ) ); 471 } 472 else { 473 setText( value.toString () ); 474 setIcon( null ); 475 } 476 if ( isSelected ) { 477 setBackground(list.getSelectionBackground()); 478 setForeground(list.getSelectionForeground()); 479 } 480 else { 481 setBackground(list.getBackground()); 482 setForeground(list.getForeground()); 483 484 } 485 return this; 486 } 487 488 } 489 } 490 | Popular Tags |