1 19 20 package org.netbeans.modules.java.project; 21 22 import java.awt.Component ; 23 import java.awt.Dimension ; 24 import java.awt.event.ActionListener ; 25 import java.io.File ; 26 import java.util.ArrayList ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import javax.swing.ComboBoxModel ; 30 import javax.swing.DefaultComboBoxModel ; 31 import javax.swing.DefaultListCellRenderer ; 32 import javax.swing.JComboBox ; 33 import javax.swing.JList ; 34 import javax.swing.ListModel ; 35 import javax.swing.SwingUtilities ; 36 import javax.swing.event.ChangeEvent ; 37 import javax.swing.event.ChangeListener ; 38 import javax.swing.event.DocumentListener ; 39 import org.netbeans.api.project.Project; 40 import org.netbeans.api.project.ProjectUtils; 41 import org.netbeans.api.project.SourceGroup; 42 import org.netbeans.spi.java.project.support.ui.PackageView; 43 import org.openide.filesystems.FileObject; 44 import org.openide.filesystems.FileUtil; 45 import org.openide.awt.Mnemonics; 46 import org.openide.loaders.DataObject; 47 import org.openide.loaders.DataObjectNotFoundException; 48 import org.openide.util.NbBundle; 49 import org.openide.util.RequestProcessor; 50 51 55 public class JavaTargetChooserPanelGUI extends javax.swing.JPanel implements ActionListener , DocumentListener { 56 57 private static final String DEFAULT_NEW_PACKAGE_NAME = 58 NbBundle.getMessage( JavaTargetChooserPanelGUI.class, "LBL_JavaTargetChooserPanelGUI_DefaultNewPackageName" ); private static final String NEW_CLASS_PREFIX = 60 NbBundle.getMessage( JavaTargetChooserPanelGUI.class, "LBL_JavaTargetChooserPanelGUI_NewJavaClassPrefix" ); 62 63 private static final Dimension PREF_DIM = new Dimension (500, 340); 64 65 private Project project; 66 private String expectedExtension; 67 private final List <ChangeListener > listeners = new ArrayList <ChangeListener >(); 68 private int type; 69 private SourceGroup groups[]; 70 private boolean ignoreRootCombo; 71 72 73 public JavaTargetChooserPanelGUI( Project p, SourceGroup[] groups, Component bottomPanel, int type ) { 74 this.type = type; 75 this.project = p; 76 this.groups = groups; 77 78 initComponents(); 79 80 if ( type == NewJavaFileWizardIterator.TYPE_PACKAGE ) { 81 packageComboBox.setVisible( false ); 82 packageLabel.setVisible( false ); 83 Mnemonics.setLocalizedText (fileLabel, NbBundle.getMessage (JavaTargetChooserPanelGUI.class, "LBL_JavaTargetChooserPanelGUI_CreatedFolder_Label")); Mnemonics.setLocalizedText (documentNameLabel, NbBundle.getMessage (JavaTargetChooserPanelGUI.class, "LBL_JavaTargetChooserPanelGUI_PackageName_Label")); documentNameTextField.getDocument().addDocumentListener( this ); 86 } 87 else if ( type == NewJavaFileWizardIterator.TYPE_PKG_INFO ) { 88 documentNameTextField.setEditable (false); 89 } 90 else { 91 packageComboBox.getEditor().addActionListener( this ); 92 documentNameTextField.getDocument().addDocumentListener( this ); 93 } 94 95 96 if ( bottomPanel != null ) { 97 bottomPanelContainer.add( bottomPanel, java.awt.BorderLayout.CENTER ); 98 } 99 100 102 103 Component packageEditor = packageComboBox.getEditor().getEditorComponent(); 105 if ( packageEditor instanceof javax.swing.JTextField ) { 106 ((javax.swing.JTextField )packageEditor).getDocument().addDocumentListener( this ); 107 } 108 else { 109 packageComboBox.addActionListener( this ); 110 } 111 112 rootComboBox.setRenderer(new GroupListCellRenderer()); 113 packageComboBox.setRenderer(PackageView.listRenderer()); 114 rootComboBox.addActionListener( this ); 115 116 setPreferredSize( PREF_DIM ); 117 setName( NbBundle.getBundle (JavaTargetChooserPanelGUI.class).getString ("LBL_JavaTargetChooserPanelGUI_Name") ); } 119 120 public void addNotify () { 121 Dimension panel2Size = this.jPanel2.getPreferredSize(); 122 Dimension bottomPanelSize = this.bottomPanelContainer.getPreferredSize (); 123 Dimension splitterSize = this.targetSeparator.getPreferredSize(); 124 int vmax = panel2Size.height + bottomPanelSize.height + splitterSize.height + 12; if (vmax > PREF_DIM.height) { 127 this.setPreferredSize (new Dimension (PREF_DIM.width,vmax)); 128 } 129 super.addNotify(); 130 } 131 132 public void initValues( FileObject template, FileObject preselectedFolder ) { 133 assert project != null : "Project must be specified."; projectTextField.setText( ProjectUtils.getInformation(project).getDisplayName() ); 136 assert template != null; 137 138 String displayName = null; 139 try { 140 DataObject templateDo = DataObject.find (template); 141 displayName = templateDo.getNodeDelegate ().getDisplayName (); 142 } catch (DataObjectNotFoundException ex) { 143 displayName = template.getName (); 144 } 145 146 putClientProperty ("NewFileWizard_Title", displayName); rootComboBox.setModel(new DefaultComboBoxModel (groups)); 149 SourceGroup preselectedGroup = getPreselectedGroup( preselectedFolder ); 150 ignoreRootCombo = true; 151 rootComboBox.setSelectedItem( preselectedGroup ); 152 ignoreRootCombo = false; 153 Object preselectedPackage = getPreselectedPackage(preselectedGroup, preselectedFolder, packageComboBox.getModel()); 154 if ( type == NewJavaFileWizardIterator.TYPE_PACKAGE ) { 155 String docName = preselectedPackage == null || preselectedPackage.toString().length() == 0 ? 156 DEFAULT_NEW_PACKAGE_NAME : 157 preselectedPackage.toString() + "." + DEFAULT_NEW_PACKAGE_NAME; 158 159 documentNameTextField.setText( docName ); 160 int docNameLen = docName.length(); 161 int defPackageNameLen = DEFAULT_NEW_PACKAGE_NAME.length(); 162 163 documentNameTextField.setSelectionEnd( docNameLen - 1 ); 164 documentNameTextField.setSelectionStart( docNameLen - defPackageNameLen ); 165 } else { 166 if (preselectedPackage != null) { 167 packageComboBox.getEditor().setItem( preselectedPackage ); 169 } 170 if (template != null) { 171 if ( documentNameTextField.getText().trim().length() == 0 ) { if (this.type == NewJavaFileWizardIterator.TYPE_PKG_INFO) { 173 documentNameTextField.setText (template.getName ()); 174 } 175 else { 176 documentNameTextField.setText (NEW_CLASS_PREFIX + template.getName ()); 178 documentNameTextField.selectAll (); 179 } 180 } 181 } 182 updatePackages( false ); 183 } 184 String ext = template == null ? "" : template.getExt(); expectedExtension = ext.length() == 0 ? "" : "." + ext; 188 updateText(); 189 190 } 191 192 public FileObject getRootFolder() { 193 return ((SourceGroup) rootComboBox.getSelectedItem()).getRootFolder(); 194 } 195 196 public String getPackageFileName() { 197 198 if ( type == NewJavaFileWizardIterator.TYPE_PACKAGE ) { 199 return ""; } 201 202 String packageName = packageComboBox.getEditor().getItem().toString(); 203 return packageName.replace( '.', '/' ); } 205 206 209 String getPackageName() { 210 if ( type == NewJavaFileWizardIterator.TYPE_PACKAGE ) { 211 return ""; } 213 return packageComboBox.getEditor().getItem().toString(); 214 } 215 216 public String getTargetName() { 217 String text = documentNameTextField.getText().trim(); 218 219 if ( text.length() == 0 ) { 220 return null; 221 } 222 else { 223 return text; 224 } 225 226 } 227 228 public void addChangeListener(ChangeListener l) { 229 listeners.add(l); 230 } 231 232 public void removeChangeListener(ChangeListener l) { 233 listeners.remove(l); 234 } 235 236 private void fireChange() { 237 ChangeEvent e = new ChangeEvent (this); 238 for (ChangeListener l : listeners) { 239 l.stateChanged(e); 240 } 241 } 242 243 248 private void initComponents() { 250 java.awt.GridBagConstraints gridBagConstraints; 251 252 targetSeparator = new javax.swing.JSeparator (); 253 bottomPanelContainer = new javax.swing.JPanel (); 254 jPanel2 = new javax.swing.JPanel (); 255 jPanel1 = new javax.swing.JPanel (); 256 documentNameLabel = new javax.swing.JLabel (); 257 documentNameTextField = new javax.swing.JTextField (); 258 jLabel5 = new javax.swing.JLabel (); 259 projectTextField = new javax.swing.JTextField (); 260 jLabel1 = new javax.swing.JLabel (); 261 rootComboBox = new javax.swing.JComboBox (); 262 packageLabel = new javax.swing.JLabel (); 263 packageComboBox = new javax.swing.JComboBox (); 264 fileLabel = new javax.swing.JLabel (); 265 fileTextField = new javax.swing.JTextField (); 266 267 setLayout(new java.awt.GridBagLayout ()); 268 gridBagConstraints = new java.awt.GridBagConstraints (); 269 gridBagConstraints.gridx = 0; 270 gridBagConstraints.gridy = 1; 271 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 272 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 273 gridBagConstraints.insets = new java.awt.Insets (0, 0, 12, 0); 274 add(targetSeparator, gridBagConstraints); 275 276 bottomPanelContainer.setLayout(new java.awt.BorderLayout ()); 277 gridBagConstraints = new java.awt.GridBagConstraints (); 278 gridBagConstraints.gridx = 0; 279 gridBagConstraints.gridy = 2; 280 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 281 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 282 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 283 gridBagConstraints.weighty = 1.0; 284 add(bottomPanelContainer, gridBagConstraints); 285 286 jPanel2.setLayout(new java.awt.GridBagLayout ()); 287 288 jPanel1.setLayout(new java.awt.GridBagLayout ()); 289 290 documentNameLabel.setLabelFor(documentNameTextField); 291 org.openide.awt.Mnemonics.setLocalizedText(documentNameLabel, org.openide.util.NbBundle.getMessage(JavaTargetChooserPanelGUI.class, "LBL_JavaTargetChooserPanelGUI_ClassName_Label")); gridBagConstraints = new java.awt.GridBagConstraints (); 293 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 294 jPanel1.add(documentNameLabel, gridBagConstraints); 295 gridBagConstraints = new java.awt.GridBagConstraints (); 296 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 297 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 298 gridBagConstraints.weightx = 1.0; 299 gridBagConstraints.insets = new java.awt.Insets (0, 6, 0, 0); 300 jPanel1.add(documentNameTextField, gridBagConstraints); 301 java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/netbeans/modules/java/project/Bundle"); documentNameTextField.getAccessibleContext().setAccessibleDescription(bundle.getString("AD_documentNameTextField")); 304 gridBagConstraints = new java.awt.GridBagConstraints (); 305 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 306 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 307 gridBagConstraints.weightx = 1.0; 308 gridBagConstraints.insets = new java.awt.Insets (0, 0, 24, 0); 309 jPanel2.add(jPanel1, gridBagConstraints); 310 311 jLabel5.setLabelFor(projectTextField); 312 org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(JavaTargetChooserPanelGUI.class, "LBL_JavaTargetChooserPanelGUI_jLabel5")); gridBagConstraints = new java.awt.GridBagConstraints (); 314 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 315 gridBagConstraints.insets = new java.awt.Insets (0, 0, 6, 0); 316 jPanel2.add(jLabel5, gridBagConstraints); 317 318 projectTextField.setEditable(false); 319 gridBagConstraints = new java.awt.GridBagConstraints (); 320 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 321 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 322 gridBagConstraints.insets = new java.awt.Insets (0, 6, 6, 0); 323 jPanel2.add(projectTextField, gridBagConstraints); 324 projectTextField.getAccessibleContext().setAccessibleDescription(bundle.getString("AD_projectTextField")); 326 jLabel1.setLabelFor(rootComboBox); 327 org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(JavaTargetChooserPanelGUI.class, "LBL_JavaTargetChooserPanelGUI_jLabel1")); gridBagConstraints = new java.awt.GridBagConstraints (); 329 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 330 gridBagConstraints.insets = new java.awt.Insets (0, 0, 6, 0); 331 jPanel2.add(jLabel1, gridBagConstraints); 332 gridBagConstraints = new java.awt.GridBagConstraints (); 333 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 334 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 335 gridBagConstraints.weightx = 1.0; 336 gridBagConstraints.insets = new java.awt.Insets (0, 6, 6, 0); 337 jPanel2.add(rootComboBox, gridBagConstraints); 338 rootComboBox.getAccessibleContext().setAccessibleDescription(bundle.getString("AD_rootComboBox")); 340 packageLabel.setLabelFor(packageComboBox); 341 org.openide.awt.Mnemonics.setLocalizedText(packageLabel, org.openide.util.NbBundle.getMessage(JavaTargetChooserPanelGUI.class, "LBL_JavaTargetChooserPanelGUI_jLabel2")); gridBagConstraints = new java.awt.GridBagConstraints (); 343 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 344 gridBagConstraints.insets = new java.awt.Insets (0, 0, 6, 0); 345 jPanel2.add(packageLabel, gridBagConstraints); 346 347 packageComboBox.setEditable(true); 348 gridBagConstraints = new java.awt.GridBagConstraints (); 349 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 350 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 351 gridBagConstraints.weightx = 1.0; 352 gridBagConstraints.insets = new java.awt.Insets (0, 6, 6, 0); 353 jPanel2.add(packageComboBox, gridBagConstraints); 354 packageComboBox.getAccessibleContext().setAccessibleDescription(bundle.getString("AD_packageComboBox")); 356 fileLabel.setLabelFor(fileTextField); 357 org.openide.awt.Mnemonics.setLocalizedText(fileLabel, org.openide.util.NbBundle.getMessage(JavaTargetChooserPanelGUI.class, "LBL_JavaTargetChooserPanelGUI_CreatedFile_Label")); gridBagConstraints = new java.awt.GridBagConstraints (); 359 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 360 gridBagConstraints.insets = new java.awt.Insets (6, 0, 12, 0); 361 jPanel2.add(fileLabel, gridBagConstraints); 362 363 fileTextField.setEditable(false); 364 gridBagConstraints = new java.awt.GridBagConstraints (); 365 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 366 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 367 gridBagConstraints.weightx = 1.0; 368 gridBagConstraints.insets = new java.awt.Insets (6, 6, 12, 0); 369 jPanel2.add(fileTextField, gridBagConstraints); 370 fileTextField.getAccessibleContext().setAccessibleDescription(bundle.getString("AD_fileTextField")); 372 gridBagConstraints = new java.awt.GridBagConstraints (); 373 gridBagConstraints.gridx = 0; 374 gridBagConstraints.gridy = 0; 375 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 376 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 377 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 378 gridBagConstraints.weightx = 1.0; 379 add(jPanel2, gridBagConstraints); 380 381 getAccessibleContext().setAccessibleDescription(bundle.getString("AD_JavaTargetChooserPanelGUI")); } 384 385 386 private javax.swing.JPanel bottomPanelContainer; 388 private javax.swing.JLabel documentNameLabel; 389 private javax.swing.JTextField documentNameTextField; 390 private javax.swing.JLabel fileLabel; 391 private javax.swing.JTextField fileTextField; 392 private javax.swing.JLabel jLabel1; 393 private javax.swing.JLabel jLabel5; 394 private javax.swing.JPanel jPanel1; 395 private javax.swing.JPanel jPanel2; 396 private javax.swing.JComboBox packageComboBox; 397 private javax.swing.JLabel packageLabel; 398 private javax.swing.JTextField projectTextField; 399 private javax.swing.JComboBox rootComboBox; 400 private javax.swing.JSeparator targetSeparator; 401 403 405 public void actionPerformed(java.awt.event.ActionEvent e) { 406 if ( rootComboBox == e.getSource() ) { 407 if ( !ignoreRootCombo && type != NewJavaFileWizardIterator.TYPE_PACKAGE ) { 408 updatePackages( true ); 409 } 410 updateText(); 411 } 412 else if ( packageComboBox == e.getSource() ) { 413 updateText(); 414 fireChange(); 415 } 416 else if ( packageComboBox.getEditor() == e.getSource() ) { 417 updateText(); 418 fireChange(); 419 } 420 } 421 422 424 public void changedUpdate(javax.swing.event.DocumentEvent e) { 425 updateText(); 426 fireChange(); 427 } 428 429 public void insertUpdate(javax.swing.event.DocumentEvent e) { 430 changedUpdate( e ); 431 } 432 433 public void removeUpdate(javax.swing.event.DocumentEvent e) { 434 changedUpdate( e ); 435 } 436 437 439 private RequestProcessor.Task updatePackagesTask = null; 440 441 private static final ComboBoxModel WAIT_MODEL = new DefaultComboBoxModel ( 442 new String [] { 443 NbBundle.getMessage( JavaTargetChooserPanelGUI.class, "LBL_JavaTargetChooserPanelGUI_PackageName_PleaseWait" ) } 445 ); 446 447 private void updatePackages( final boolean clean ) { 448 WAIT_MODEL.setSelectedItem( packageComboBox.getEditor().getItem() ); 449 packageComboBox.setModel( WAIT_MODEL ); 450 451 if ( updatePackagesTask != null ) { 452 updatePackagesTask.cancel(); 453 } 454 455 updatePackagesTask = new RequestProcessor( "ComboUpdatePackages" ).post( 456 new Runnable () { 457 458 private ComboBoxModel model; 459 460 public void run() { 461 if ( !SwingUtilities.isEventDispatchThread() ) { 462 model = PackageView.createListView((SourceGroup) rootComboBox.getSelectedItem()); 463 SwingUtilities.invokeLater( this ); 464 } 465 else { 466 if ( !clean ) { 467 model.setSelectedItem( packageComboBox.getEditor().getItem() ); 468 } 469 packageComboBox.setModel( model ); 470 } 471 } 472 } 473 ); 474 475 } 476 477 private void updateText() { 478 479 SourceGroup g = (SourceGroup) rootComboBox.getSelectedItem(); 480 FileObject rootFolder = g.getRootFolder(); 481 String packageName = getPackageFileName(); 482 String documentName = documentNameTextField.getText().trim(); 483 if ( type == NewJavaFileWizardIterator.TYPE_PACKAGE ) { 484 documentName = documentName.replace( '.', '/' ); } 486 else if ( documentName.length() > 0 ) { 487 documentName = documentName + expectedExtension; 488 } 489 String createdFileName = FileUtil.getFileDisplayName( rootFolder ) + 490 ( packageName.startsWith("/") || packageName.startsWith( File.separator ) ? "" : "/" ) + packageName + 492 ( packageName.endsWith("/") || packageName.endsWith( File.separator ) || packageName.length() == 0 ? "" : "/" ) + documentName; 494 495 fileTextField.setText( createdFileName.replace( '/', File.separatorChar ) ); } 497 498 private SourceGroup getPreselectedGroup(FileObject folder) { 499 for(int i = 0; folder != null && i < groups.length; i++) { 500 FileObject root = groups[i].getRootFolder(); 501 if (root.equals(folder) || FileUtil.isParentOf(root, folder)) { 502 return groups[i]; 503 } 504 } 505 return groups[0]; 506 } 507 508 513 private Object getPreselectedPackage(SourceGroup group, FileObject folder, ListModel model) { 514 if ( folder == null ) { 515 return null; 516 } 517 FileObject root = group.getRootFolder(); 518 519 String relPath = FileUtil.getRelativePath( root, folder ); 520 521 if ( relPath == null ) { 522 return null; 525 } 526 else { 527 String name = relPath.replace('/', '.'); 529 538 return name; 541 } 542 } 543 544 546 549 private static final class GroupListCellRenderer extends DefaultListCellRenderer { 550 551 public GroupListCellRenderer() {} 552 553 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 554 SourceGroup g = (SourceGroup) value; 555 super.getListCellRendererComponent(list, g.getDisplayName(), index, isSelected, cellHasFocus); 556 setIcon(g.getIcon(false)); 557 return this; 558 } 559 560 } 561 562 } 563 | Popular Tags |