diff --git a/java/java.project.ui/apichanges.xml b/java/java.project.ui/apichanges.xml index 2b3cf91d7dbb..ec37c54baab7 100644 --- a/java/java.project.ui/apichanges.xml +++ b/java/java.project.ui/apichanges.xml @@ -83,6 +83,19 @@ is the proper place. + + + Added the superclass and interfaces variables for FreeMarker file templates + + + + + + Added the superclass and interfaces variables for use in FreeMarker + templates used by the New Java File Wizard. + + + Extended the BrokenReferencesSupport.createPlatformVersionProblemProvider by the mininal source level supported by the project diff --git a/java/java.project.ui/src/org/netbeans/modules/java/project/ui/Bundle.properties b/java/java.project.ui/src/org/netbeans/modules/java/project/ui/Bundle.properties index 6e606346b713..3f4718070fd5 100644 --- a/java/java.project.ui/src/org/netbeans/modules/java/project/ui/Bundle.properties +++ b/java/java.project.ui/src/org/netbeans/modules/java/project/ui/Bundle.properties @@ -96,8 +96,3 @@ LBL_MissingPlatform=The Java Platform called "{1} LBL_CreateNewPlatform=Either create a new Java Platform LBL_UseExistingPlatform=or use an &existing Java Platform: LBL_PlatformHint=Hint: For version controlled projects it''s recommended to create a new Java Platform "{0}". -ExtensionAndImplementationVisualPanel.superclassLabel.text=&Superclass: -ExtensionAndImplementationVisualPanel.superclassTextField.text= -ExtensionAndImplementationVisualPanel.browseSuperclassButton.text=Browse... -ExtensionAndImplementationVisualPanel.interfacesLabel.text=&Interfaces: -ExtensionAndImplementationVisualPanel.browseInterfacesButton.text=Browse... \ No newline at end of file diff --git a/java/java.project.ui/src/org/netbeans/modules/java/project/ui/ExtensionAndImplementationVisualPanel.form b/java/java.project.ui/src/org/netbeans/modules/java/project/ui/ExtensionAndImplementationVisualPanel.form index b0a219287d7a..39d1a1e85fb9 100644 --- a/java/java.project.ui/src/org/netbeans/modules/java/project/ui/ExtensionAndImplementationVisualPanel.form +++ b/java/java.project.ui/src/org/netbeans/modules/java/project/ui/ExtensionAndImplementationVisualPanel.form @@ -32,118 +32,10 @@ + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/java/java.project.ui/src/org/netbeans/modules/java/project/ui/ExtensionAndImplementationVisualPanel.java b/java/java.project.ui/src/org/netbeans/modules/java/project/ui/ExtensionAndImplementationVisualPanel.java index 6d18a9dae864..fd856fa929fc 100644 --- a/java/java.project.ui/src/org/netbeans/modules/java/project/ui/ExtensionAndImplementationVisualPanel.java +++ b/java/java.project.ui/src/org/netbeans/modules/java/project/ui/ExtensionAndImplementationVisualPanel.java @@ -18,12 +18,22 @@ */ package org.netbeans.modules.java.project.ui; +import static java.awt.Component.CENTER_ALIGNMENT; +import static java.awt.Component.LEFT_ALIGNMENT; +import static java.awt.Component.TOP_ALIGNMENT; +import java.awt.Dimension; +import java.awt.event.ActionEvent; import java.lang.reflect.Modifier; import java.util.Set; import java.util.StringTokenizer; import javax.lang.model.element.ElementKind; import javax.lang.model.element.TypeElement; +import javax.swing.Box; +import javax.swing.BoxLayout; +import javax.swing.JButton; +import javax.swing.JLabel; import javax.swing.JPanel; +import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.event.DocumentEvent; @@ -31,50 +41,44 @@ import javax.swing.text.BadLocationException; import org.netbeans.api.java.source.*; import org.netbeans.api.java.source.ui.TypeElementFinder; +import org.netbeans.spi.project.ui.templates.support.Templates; import org.openide.WizardDescriptor; +import org.openide.awt.Mnemonics; +import org.openide.filesystems.FileObject; import org.openide.util.Exceptions; import org.openide.util.NbBundle; import org.openide.util.Utilities; /** - * * @author Arthur Sadykov */ public class ExtensionAndImplementationVisualPanel extends JPanel implements DocumentListener { private static final String SUPERCLASS = "superclass"; // NOI18N private static final String INTERFACES = "interfaces"; // NOI18N + private static final String NEW_LINE = "\n"; // NOI18N + private static final String EMPTY_STRING = ""; // NOI18N + private static final String EXCEPTION_TEMPLATE_NAME = "Exception"; // NOI18N + private static final String INTERFACE_TEMPLATE_NAME = "Interface"; // NOI18N private final ExtensionAndImplementationWizardPanel wizardPanel; + private JTextField superclassTextField; + private JTextArea interfacesTextArea; - /** - * Creates new form ExtensionPanel - */ private ExtensionAndImplementationVisualPanel(ExtensionAndImplementationWizardPanel wizardPanel) { initComponents(); this.wizardPanel = wizardPanel; } - public static ExtensionAndImplementationVisualPanel create(ExtensionAndImplementationWizardPanel wizardPanel) { - ExtensionAndImplementationVisualPanel extensionPanel = new ExtensionAndImplementationVisualPanel(wizardPanel); - extensionPanel.getSuperclassTextField().getDocument().addDocumentListener(extensionPanel); - extensionPanel.getInterfacesTextArea().getDocument().addDocumentListener(extensionPanel); - return extensionPanel; - } - - private JTextField getSuperclassTextField() { - return superclassTextField; + public static ExtensionAndImplementationVisualPanelBuilder builder(ExtensionAndImplementationWizardPanel wizardPanel) { + return new ExtensionAndImplementationVisualPanelBuilder(wizardPanel); } - private JTextArea getInterfacesTextArea() { - return interfacesTextArea; + private void setSuperclassTextField(JTextField superclassTextField) { + this.superclassTextField = superclassTextField; } - String getSuperclass() { - return superclassTextField.getText(); - } - - String getInterfaces() { - return interfacesTextArea.getText(); + private void setInterfacesTextArea(JTextArea interfacesTextArea) { + this.interfacesTextArea = interfacesTextArea; } /** @@ -85,165 +89,39 @@ String getInterfaces() { // //GEN-BEGIN:initComponents private void initComponents() { - superclassLabel = new javax.swing.JLabel(); - interfacesLabel = new javax.swing.JLabel(); - superclassTextField = new javax.swing.JTextField(); - browseSuperclassButton = new javax.swing.JButton(); - browseInterfacesButton = new javax.swing.JButton(); - interfacesScrollPane = new javax.swing.JScrollPane(); - interfacesTextArea = new javax.swing.JTextArea(); - - superclassLabel.setLabelFor(superclassTextField); - org.openide.awt.Mnemonics.setLocalizedText(superclassLabel, org.openide.util.NbBundle.getMessage(ExtensionAndImplementationVisualPanel.class, "ExtensionAndImplementationVisualPanel.superclassLabel.text")); // NOI18N - - interfacesLabel.setLabelFor(interfacesTextArea); - org.openide.awt.Mnemonics.setLocalizedText(interfacesLabel, org.openide.util.NbBundle.getMessage(ExtensionAndImplementationVisualPanel.class, "ExtensionAndImplementationVisualPanel.interfacesLabel.text")); // NOI18N - - superclassTextField.setEditable(false); - superclassTextField.setText(org.openide.util.NbBundle.getMessage(ExtensionAndImplementationVisualPanel.class, "ExtensionAndImplementationVisualPanel.superclassTextField.text")); // NOI18N - - org.openide.awt.Mnemonics.setLocalizedText(browseSuperclassButton, org.openide.util.NbBundle.getMessage(ExtensionAndImplementationVisualPanel.class, "ExtensionAndImplementationVisualPanel.browseSuperclassButton.text")); // NOI18N - browseSuperclassButton.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - browseSuperclassButtonActionPerformed(evt); - } - }); - - org.openide.awt.Mnemonics.setLocalizedText(browseInterfacesButton, org.openide.util.NbBundle.getMessage(ExtensionAndImplementationVisualPanel.class, "ExtensionAndImplementationVisualPanel.browseInterfacesButton.text")); // NOI18N - browseInterfacesButton.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - browseInterfacesButtonActionPerformed(evt); - } - }); - - interfacesTextArea.setEditable(false); - interfacesTextArea.setColumns(20); - interfacesTextArea.setRows(5); - interfacesScrollPane.setViewportView(interfacesTextArea); - - javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); - this.setLayout(layout); - layout.setHorizontalGroup( - layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(superclassLabel) - .addComponent(interfacesLabel)) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(interfacesScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE) - .addComponent(superclassTextField)) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(browseSuperclassButton, javax.swing.GroupLayout.Alignment.TRAILING) - .addComponent(browseInterfacesButton, javax.swing.GroupLayout.Alignment.TRAILING))) - ); - layout.setVerticalGroup( - layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addContainerGap() - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(superclassTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addComponent(browseSuperclassButton) - .addComponent(superclassLabel)) - .addGap(4, 4, 4) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(interfacesScrollPane) - .addGroup(layout.createSequentialGroup() - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(interfacesLabel) - .addComponent(browseInterfacesButton)) - .addGap(0, 0, Short.MAX_VALUE))) - .addContainerGap()) - ); + setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.Y_AXIS)); }// //GEN-END:initComponents - private void browseSuperclassButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseSuperclassButtonActionPerformed - ElementHandle handle = TypeElementFinder.find(null, new TypeElementFinder.Customizer() { - @Override - public Set> query(ClasspathInfo classpathInfo, String textForQuery, - ClassIndex.NameKind nameKind, Set searchScopes) { - return classpathInfo.getClassIndex().getDeclaredTypes(textForQuery, nameKind, searchScopes); - } - - @Override - public boolean accept(ElementHandle typeHandle) { - return isNotFinalClass(typeHandle); - } - }); - if (handle != null) { - String fqn = handle.getQualifiedName(); - superclassTextField.setText(fqn); - } - }//GEN-LAST:event_browseSuperclassButtonActionPerformed - - private boolean isNotFinalClass(ElementHandle typeHandle) { - boolean isNotFinal = false; - try { - Class clazz = Class.forName(typeHandle.getQualifiedName()); - int modifiers = clazz.getModifiers(); - isNotFinal = !Modifier.isFinal(modifiers); - } catch (ClassNotFoundException ex) { - } - return typeHandle.getKind() == ElementKind.CLASS && isNotFinal; - } - - private void browseInterfacesButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseInterfacesButtonActionPerformed - ElementHandle handle = TypeElementFinder.find(null, new TypeElementFinder.Customizer() { - @Override - public Set> query(ClasspathInfo classpathInfo, String textForQuery, - ClassIndex.NameKind nameKind, Set searchScopes) { - return classpathInfo.getClassIndex().getDeclaredTypes(textForQuery, nameKind, searchScopes); - } - - @Override - public boolean accept(ElementHandle typeHandle) { - return isInterface(typeHandle); - } - }); - if (handle != null) { - String fqn = handle.getQualifiedName(); - if (interfacesTextArea.getText().isEmpty()) { - interfacesTextArea.setText(fqn); - } else { - String interfaces = interfacesTextArea.getText(); - if (!interfaces.contains(fqn)) { - interfacesTextArea.append("\n" + fqn); - } - } - } - }//GEN-LAST:event_browseInterfacesButtonActionPerformed - - private boolean isInterface(ElementHandle typeHandle) { - return typeHandle.getKind() == ElementKind.INTERFACE; - } - @NbBundle.Messages({ - "INFO_JavaTargetChooser_ProvideValidSuperclass=Provide valid superclass.", + "INFO_JavaTargetChooser_ProvideValidSuperclass=Provide a valid superclass.", "INFO_JavaTargetChooser_ProvideValidInterfaces=Provide valid interfaces." }) boolean isValid(WizardDescriptor wizardDescriptor) { if (wizardDescriptor == null) { return false; } - String superclass = superclassTextField.getText(); - if (!superclass.isEmpty() && !isValidSuperclass(superclass)) { - wizardDescriptor.putProperty(WizardDescriptor.PROP_ERROR_MESSAGE, - NbBundle.getMessage(ExtensionAndImplementationVisualPanel.class, - "INFO_JavaTargetChooser_ProvideValidSuperclass")); - return false; + if (superclassTextField != null) { + String superclass = superclassTextField.getText(); + if (!superclass.isEmpty() && !isValidSuperclassString(superclass)) { + wizardDescriptor.putProperty(WizardDescriptor.PROP_ERROR_MESSAGE, + NbBundle.getMessage(ExtensionAndImplementationVisualPanel.class, + "INFO_JavaTargetChooser_ProvideValidSuperclass")); // NOI18N + return false; + } } - String interfaces = interfacesTextArea.getText(); - if (!interfaces.isEmpty() && !isValidInterfaces(interfaces)) { - wizardDescriptor.putProperty(WizardDescriptor.PROP_ERROR_MESSAGE, - NbBundle.getMessage(ExtensionAndImplementationVisualPanel.class, - "INFO_JavaTargetChooser_ProvideValidInterfaces")); - return false; + if (interfacesTextArea != null) { + String interfaces = interfacesTextArea.getText(); + if (!interfaces.isEmpty() && !isValidInterfacesString(interfaces)) { + wizardDescriptor.putProperty(WizardDescriptor.PROP_ERROR_MESSAGE, + NbBundle.getMessage(ExtensionAndImplementationVisualPanel.class, + "INFO_JavaTargetChooser_ProvideValidInterfaces")); // NOI18N + return false; + } } return true; } - private boolean isValidSuperclass(String superclass) { + private boolean isValidSuperclassString(String superclass) { if (!superclass.isEmpty()) { if (superclass.charAt(0) == '.' || superclass.charAt(superclass.length() - 1) == '.') { return false; @@ -253,7 +131,7 @@ private boolean isValidSuperclass(String superclass) { } private boolean hasValidTokens(String fqn) { - StringTokenizer tokenizer = new StringTokenizer(fqn, "."); + StringTokenizer tokenizer = new StringTokenizer(fqn, "."); // NOI18N while (tokenizer.hasMoreTokens()) { String token = tokenizer.nextToken(); if (token.isEmpty()) { @@ -266,46 +144,53 @@ private boolean hasValidTokens(String fqn) { return true; } - private boolean isValidInterfaces(String interfaces) { + private boolean isValidInterfacesString(String interfaces) { if (!interfaces.isEmpty()) { if (interfaces.charAt(0) == '.' || interfaces.charAt(interfaces.length() - 1) == '.') { return false; } } - for (int i = 0; i < interfacesTextArea.getLineCount(); i++) { - try { - int lineStartOffset = interfacesTextArea.getLineStartOffset(i); - int lineEndOffset = interfacesTextArea.getLineEndOffset(i); - if (lineEndOffset > lineStartOffset) { - String implementationClass = - interfacesTextArea.getText(lineStartOffset, lineEndOffset - lineStartOffset - 1); - if (!hasValidTokens(implementationClass)) { - return false; + if (interfacesTextArea != null) { + for (int i = 0; i < interfacesTextArea.getLineCount(); i++) { + try { + int lineStartOffset = interfacesTextArea.getLineStartOffset(i); + int lineEndOffset = interfacesTextArea.getLineEndOffset(i); + if (lineEndOffset > lineStartOffset) { + String implementationClass = + interfacesTextArea.getText(lineStartOffset, lineEndOffset - lineStartOffset - 1); + if (!hasValidTokens(implementationClass)) { + return false; + } } + } catch (BadLocationException ex) { + Exceptions.printStackTrace(ex); + return false; } - } catch (BadLocationException ex) { - Exceptions.printStackTrace(ex); } } return true; } void readSettings(WizardDescriptor wizardDescriptor) { - String superclass = (String) wizardDescriptor.getProperty(SUPERCLASS); - if (superclass != null) { + Object superclassProperty = wizardDescriptor.getProperty(SUPERCLASS); + String superclass = superclassProperty != null ? (String) superclassProperty : EMPTY_STRING; + if (!superclass.isEmpty() && superclassTextField != null) { superclassTextField.setText(superclass); } - String interfaces = (String) wizardDescriptor.getProperty(INTERFACES); - if (interfaces != null) { + Object interfacesProperty = wizardDescriptor.getProperty(INTERFACES); + String interfaces = interfacesProperty != null ? (String) interfacesProperty : EMPTY_STRING; + if (!interfaces.isEmpty() && interfacesTextArea != null) { interfacesTextArea.setText(interfaces); } } void storeSettings(WizardDescriptor wizardDescriptor) { - String superclass = superclassTextField.getText(); - String interfaces = interfacesTextArea.getText(); - wizardDescriptor.putProperty(SUPERCLASS, superclass); - wizardDescriptor.putProperty(INTERFACES, interfaces); + if (superclassTextField != null) { + wizardDescriptor.putProperty(SUPERCLASS, superclassTextField.getText()); + } + if (interfacesTextArea != null) { + wizardDescriptor.putProperty(INTERFACES, interfacesTextArea.getText()); + } } @Override @@ -323,13 +208,204 @@ public void changedUpdate(DocumentEvent event) { wizardPanel.fireChangeEvent(); } + private void browseSuperclassButtonActionPerformed(ActionEvent evt) { + ElementHandle handle = TypeElementFinder.find(null, new TypeElementFinder.Customizer() { + @Override + public Set> query(ClasspathInfo classpathInfo, String textForQuery, + ClassIndex.NameKind nameKind, Set searchScopes) { + return classpathInfo.getClassIndex().getDeclaredTypes(textForQuery, nameKind, searchScopes); + } + + @Override + public boolean accept(ElementHandle typeHandle) { + WizardDescriptor wizardDescriptor = wizardPanel.getWizardDescriptor(); + FileObject template = Templates.getTemplate(wizardDescriptor); + if (template != null) { + String templateName = template.getName(); + switch (templateName) { + case EXCEPTION_TEMPLATE_NAME: + return isNotFinalExceptionType(typeHandle); + case INTERFACE_TEMPLATE_NAME: + return isInterface(typeHandle); + default: + return isNotFinalClass(typeHandle); + } + } + return false; + } + }); + if (handle != null) { + superclassTextField.setText(handle.getQualifiedName()); + } + } + + private boolean isNotFinalExceptionType(ElementHandle typeHandle) { + try { + Class clazz = Class.forName(typeHandle.getQualifiedName()); + return typeHandle.getKind() == ElementKind.CLASS + && Exception.class.isAssignableFrom(clazz) + && !Modifier.isFinal(clazz.getModifiers()); + } catch (ClassNotFoundException ex) { + } + return false; + } + + private boolean isInterface(ElementHandle typeHandle) { + return typeHandle.getKind() == ElementKind.INTERFACE; + } + + private boolean isNotFinalClass(ElementHandle typeHandle) { + try { + Class clazz = Class.forName(typeHandle.getQualifiedName()); + return typeHandle.getKind() == ElementKind.CLASS && !Modifier.isFinal(clazz.getModifiers()); + } catch (ClassNotFoundException ex) { + } + return false; + } + + private void browseInterfacesButtonActionPerformed(ActionEvent evt) { + ElementHandle handle = TypeElementFinder.find(null, new TypeElementFinder.Customizer() { + @Override + public Set> query(ClasspathInfo classpathInfo, String textForQuery, + ClassIndex.NameKind nameKind, Set searchScopes) { + return classpathInfo.getClassIndex().getDeclaredTypes(textForQuery, nameKind, searchScopes); + } + + @Override + public boolean accept(ElementHandle typeHandle) { + return isInterface(typeHandle); + } + }); + if (handle != null && interfacesTextArea != null) { + String fqn = handle.getQualifiedName(); + if (interfacesTextArea.getText().isEmpty()) { + interfacesTextArea.setText(fqn); + } else { + String interfaces = interfacesTextArea.getText(); + if (!interfaces.contains(fqn)) { + interfacesTextArea.append(NEW_LINE + fqn); + } + } + } + } + + public static class ExtensionAndImplementationVisualPanelBuilder { + + private static final int SMALL_VERTICAL_SPACING = 8; + private static final int SMALL_HORIZONTAL_SPACING = 8; + private static final int MINIMUM_COMPONENT_HEIGHT = 25; + private static final int MINIMUM_COMPONENT_WIDTH = 0; + private static final int DEFAULT_ROW_COUNT = 5; + private static final int DEFAULT_COLUMN_COUNT = 20; + private static final int MINIMUM_LABEL_WIDTH = 85; + private static final int MINIMUM_TEXT_AREA_HEIGHT = 100; + private final ExtensionAndImplementationWizardPanel wizardPanel; + private final ExtensionAndImplementationVisualPanel visualPanel; + private JTextArea interfacesTextArea; + private JTextField superclassTextField; + + public ExtensionAndImplementationVisualPanelBuilder(ExtensionAndImplementationWizardPanel wizardPanel) { + this.wizardPanel = wizardPanel; + visualPanel = new ExtensionAndImplementationVisualPanel(wizardPanel); + } + + @NbBundle.Messages({ + "ExtensionAndImplementationVisualPanel.superclassLabel.text=&Superclass:", + "ExtensionAndImplementationVisualPanel.superinterfaceLabel.text=&Superinterface:", + "ExtensionAndImplementationVisualPanel.browseSuperclassButton.text=Browse..." + }) + public ExtensionAndImplementationVisualPanelBuilder withExtensionBox() { + Box extensionBox = new Box(BoxLayout.X_AXIS); + JLabel superclassLabel = new JLabel(); + FileObject template = Templates.getTemplate(wizardPanel.getWizardDescriptor()); + if (template != null) { + String templateName = template.getName(); + if (templateName.equals(INTERFACE_TEMPLATE_NAME)) { + Mnemonics.setLocalizedText(superclassLabel, + NbBundle.getMessage(ExtensionAndImplementationVisualPanel.class, + "ExtensionAndImplementationVisualPanel.superinterfaceLabel.text")); // NOI18N + } else { + Mnemonics.setLocalizedText(superclassLabel, + NbBundle.getMessage(ExtensionAndImplementationVisualPanel.class, + "ExtensionAndImplementationVisualPanel.superclassLabel.text")); // NOI18N + } + } + superclassLabel.setAlignmentX(LEFT_ALIGNMENT); + superclassLabel.setAlignmentY(CENTER_ALIGNMENT); + superclassLabel.setMinimumSize(new Dimension(MINIMUM_LABEL_WIDTH, MINIMUM_COMPONENT_HEIGHT)); + superclassLabel.setPreferredSize(new Dimension(MINIMUM_LABEL_WIDTH, MINIMUM_COMPONENT_HEIGHT)); + superclassTextField = new JTextField(); + superclassTextField.setEditable(false); + superclassTextField.setMinimumSize(new Dimension(MINIMUM_COMPONENT_WIDTH, MINIMUM_COMPONENT_HEIGHT)); + superclassTextField.setAlignmentX(LEFT_ALIGNMENT); + superclassTextField.setAlignmentY(CENTER_ALIGNMENT); + superclassTextField.getDocument().addDocumentListener(visualPanel); + superclassLabel.setLabelFor(superclassTextField); + JButton browseSuperclassButton = new JButton(); + Mnemonics.setLocalizedText(browseSuperclassButton, + NbBundle.getMessage(ExtensionAndImplementationVisualPanel.class, + "ExtensionAndImplementationVisualPanel.browseSuperclassButton.text")); // NOI18N + browseSuperclassButton.addActionListener(visualPanel::browseSuperclassButtonActionPerformed); + browseSuperclassButton.setAlignmentX(LEFT_ALIGNMENT); + browseSuperclassButton.setAlignmentY(CENTER_ALIGNMENT); + extensionBox.add(superclassLabel); + extensionBox.add(superclassTextField); + extensionBox.add(Box.createHorizontalStrut(SMALL_HORIZONTAL_SPACING)); + extensionBox.add(browseSuperclassButton); + extensionBox.add(Box.createHorizontalStrut(SMALL_HORIZONTAL_SPACING)); + visualPanel.add(extensionBox); + visualPanel.add(Box.createVerticalStrut(SMALL_VERTICAL_SPACING)); + return this; + } + + @NbBundle.Messages({ + "ExtensionAndImplementationVisualPanel.interfacesLabel.text=&Interfaces:", + "ExtensionAndImplementationVisualPanel.browseInterfacesButton.text=Browse..." + }) + public ExtensionAndImplementationVisualPanelBuilder withImplementationBox() { + Box implementationBox = new Box(BoxLayout.X_AXIS); + JLabel interfacesLabel = new JLabel(); + Mnemonics.setLocalizedText(interfacesLabel, + NbBundle.getMessage(ExtensionAndImplementationVisualPanel.class, + "ExtensionAndImplementationVisualPanel.interfacesLabel.text")); // NOI18N + interfacesLabel.setAlignmentX(LEFT_ALIGNMENT); + interfacesLabel.setAlignmentY(TOP_ALIGNMENT); + interfacesLabel.setMinimumSize(new Dimension(MINIMUM_LABEL_WIDTH, MINIMUM_COMPONENT_HEIGHT)); + interfacesLabel.setPreferredSize(new Dimension(MINIMUM_LABEL_WIDTH, MINIMUM_COMPONENT_HEIGHT)); + JScrollPane interfacesScrollPane = new JScrollPane(); + interfacesTextArea = new JTextArea(); + interfacesTextArea.setEditable(false); + interfacesTextArea.setColumns(DEFAULT_COLUMN_COUNT); + interfacesTextArea.setRows(DEFAULT_ROW_COUNT); + interfacesTextArea.getDocument().addDocumentListener(visualPanel); + interfacesLabel.setLabelFor(interfacesTextArea); + interfacesScrollPane.setViewportView(interfacesTextArea); + interfacesScrollPane.setAlignmentX(LEFT_ALIGNMENT); + interfacesScrollPane.setAlignmentY(TOP_ALIGNMENT); + interfacesScrollPane.setMinimumSize(new Dimension(MINIMUM_COMPONENT_WIDTH, MINIMUM_TEXT_AREA_HEIGHT)); + JButton browseInterfacesButton = new JButton(); + Mnemonics.setLocalizedText(browseInterfacesButton, + NbBundle.getMessage(ExtensionAndImplementationVisualPanel.class, + "ExtensionAndImplementationVisualPanel.browseInterfacesButton.text")); // NOI18N + browseInterfacesButton.addActionListener(visualPanel::browseInterfacesButtonActionPerformed); + browseInterfacesButton.setAlignmentX(LEFT_ALIGNMENT); + browseInterfacesButton.setAlignmentY(TOP_ALIGNMENT); + implementationBox.add(interfacesLabel); + implementationBox.add(interfacesScrollPane); + implementationBox.add(Box.createHorizontalStrut(SMALL_HORIZONTAL_SPACING)); + implementationBox.add(browseInterfacesButton); + implementationBox.add(Box.createHorizontalStrut(SMALL_HORIZONTAL_SPACING)); + visualPanel.add(implementationBox); + return this; + } + + public ExtensionAndImplementationVisualPanel build() { + visualPanel.setSuperclassTextField(superclassTextField); + visualPanel.setInterfacesTextArea(interfacesTextArea); + visualPanel.add(Box.createRigidArea(new Dimension(MINIMUM_COMPONENT_WIDTH, Integer.MAX_VALUE))); + return visualPanel; + } + } // Variables declaration - do not modify//GEN-BEGIN:variables - private javax.swing.JButton browseInterfacesButton; - private javax.swing.JButton browseSuperclassButton; - private javax.swing.JLabel interfacesLabel; - private javax.swing.JScrollPane interfacesScrollPane; - private javax.swing.JTextArea interfacesTextArea; - private javax.swing.JLabel superclassLabel; - private javax.swing.JTextField superclassTextField; // End of variables declaration//GEN-END:variables } diff --git a/java/java.project.ui/src/org/netbeans/modules/java/project/ui/ExtensionAndImplementationWizardPanel.java b/java/java.project.ui/src/org/netbeans/modules/java/project/ui/ExtensionAndImplementationWizardPanel.java index 4c323cde8fcc..30052a8dd482 100644 --- a/java/java.project.ui/src/org/netbeans/modules/java/project/ui/ExtensionAndImplementationWizardPanel.java +++ b/java/java.project.ui/src/org/netbeans/modules/java/project/ui/ExtensionAndImplementationWizardPanel.java @@ -23,22 +23,72 @@ import java.util.Set; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; +import org.netbeans.spi.project.ui.templates.support.Templates; import org.openide.WizardDescriptor; +import org.openide.filesystems.FileObject; import org.openide.util.HelpCtx; +import org.openide.util.Parameters; /** * @author Arthur Sadykov */ public class ExtensionAndImplementationWizardPanel implements WizardDescriptor.Panel { + private static final String ANNOTATION_TYPE_TEMPLATE_NAME = "AnnotationType"; + private static final String APPLET_TEMPLATE_NAME = "Applet"; + private static final String EMPTY_TEMPLATE_NAME = "Empty"; + private static final String ENUM_TEMPLATE_NAME = "Enum"; + private static final String INTERFACE_TEMPLATE_NAME = "Interface"; + private static final String JAPPLET_TEMPLATE_NAME = "JApplet"; + private static final String RECORD_TEMPLATE_NAME = "Record"; private ExtensionAndImplementationVisualPanel component; - private WizardDescriptor wizardDescriptor; + private final WizardDescriptor wizardDescriptor; private final Set listeners = new HashSet<>(1); + public ExtensionAndImplementationWizardPanel(WizardDescriptor wizardDescriptor) { + Parameters.notNull("wizardDescriptor", wizardDescriptor); // NOI18N + this.wizardDescriptor = wizardDescriptor; + } + + WizardDescriptor getWizardDescriptor() { + return wizardDescriptor; + } + @Override public Component getComponent() { if (component == null) { - component = ExtensionAndImplementationVisualPanel.create(this); + FileObject template = Templates.getTemplate(wizardDescriptor); + if (template != null) { + switch (template.getName()) { + case ANNOTATION_TYPE_TEMPLATE_NAME: + case EMPTY_TEMPLATE_NAME: { + component = ExtensionAndImplementationVisualPanel.builder(this) + .build(); + break; + } + case INTERFACE_TEMPLATE_NAME: { + component = ExtensionAndImplementationVisualPanel.builder(this) + .withExtensionBox() + .build(); + break; + } + case APPLET_TEMPLATE_NAME: + case ENUM_TEMPLATE_NAME: + case JAPPLET_TEMPLATE_NAME: + case RECORD_TEMPLATE_NAME: { + component = ExtensionAndImplementationVisualPanel.builder(this) + .withImplementationBox() + .build(); + break; + } + default: { + component = ExtensionAndImplementationVisualPanel.builder(this) + .withExtensionBox() + .withImplementationBox() + .build(); + } + } + } } return component; } @@ -50,18 +100,24 @@ public HelpCtx getHelp() { @Override public void readSettings(WizardDescriptor wizardDescriptor) { - this.wizardDescriptor = wizardDescriptor; - component.readSettings(wizardDescriptor); + if (component != null) { + component.readSettings(wizardDescriptor); + } } @Override public void storeSettings(WizardDescriptor wizardDescriptor) { - component.storeSettings(wizardDescriptor); + if (component != null) { + component.storeSettings(wizardDescriptor); + } } @Override public boolean isValid() { getComponent(); + if (component == null) { + return false; + } return component.isValid(wizardDescriptor); } diff --git a/java/java.project.ui/src/org/netbeans/modules/java/project/ui/NewJavaFileWizardIterator.java b/java/java.project.ui/src/org/netbeans/modules/java/project/ui/NewJavaFileWizardIterator.java index defe83734707..e288197a4fec 100644 --- a/java/java.project.ui/src/org/netbeans/modules/java/project/ui/NewJavaFileWizardIterator.java +++ b/java/java.project.ui/src/org/netbeans/modules/java/project/ui/NewJavaFileWizardIterator.java @@ -163,7 +163,7 @@ private WizardDescriptor.Panel[] createPanels ( else { final List> panels = new ArrayList<>(); if (this.type == Type.FILE) { - panels.add(JavaTemplates.createPackageChooser(project, groups, new ExtensionAndImplementationWizardPanel())); + panels.add(JavaTemplates.createPackageChooser(project, groups, new ExtensionAndImplementationWizardPanel(wizardDescriptor))); } else if (type == Type.PKG_INFO) { panels.add(new JavaTargetChooserPanel(project, groups, null, Type.PKG_INFO, true)); } else if (type == Type.MODULE_INFO) { @@ -325,12 +325,18 @@ public Set instantiate () throws IOException { Collections.singletonMap("moduleName", moduleName)); //NOI18N createdFile = dobj.getPrimaryFile(); } else { - DataObject dTemplate = DataObject.find( template ); - String superclass = (String) wiz.getProperty(SUPERCLASS); - String interfaces = (String) wiz.getProperty(INTERFACES); - Map parameters = new HashMap<>(); - parameters.put(SUPERCLASS, superclass); - parameters.put(INTERFACES, interfaces); + DataObject dTemplate = DataObject.find(template); + Object superclassProperty = wiz.getProperty(SUPERCLASS); + String superclass = superclassProperty != null ? (String) superclassProperty : ""; //NOI18N + Object interfacesProperty = wiz.getProperty(INTERFACES); + String interfaces = interfacesProperty != null ? (String) interfacesProperty : ""; //NOI18N + Map parameters = new HashMap<>(Short.BYTES); + if (!superclass.isEmpty()) { + parameters.put(SUPERCLASS, superclass); + } + if (!interfaces.isEmpty()) { + parameters.put(INTERFACES, interfaces); + } DataObject dobj = dTemplate.createFromTemplate(df, targetName, parameters); createdFile = dobj.getPrimaryFile(); } diff --git a/java/java.project.ui/src/org/netbeans/modules/java/project/ui/resources/Applet.java.template b/java/java.project.ui/src/org/netbeans/modules/java/project/ui/resources/Applet.java.template index d8e28e625e1e..b2113cb092cc 100644 --- a/java/java.project.ui/src/org/netbeans/modules/java/project/ui/resources/Applet.java.template +++ b/java/java.project.ui/src/org/netbeans/modules/java/project/ui/resources/Applet.java.template @@ -8,12 +8,26 @@ package ${package}; import java.applet.Applet; +<#if interfaces?? && interfaces != ""> + <#list "${interfaces}"?split("\n") as interface> + <#if interface?? && interface != "" && !interface?starts_with("java.lang") && !interface?matches(package + "\\.\\w+")> +import ${interface}; + + + /** * * @author ${user} */ -public class ${name} extends Applet { +<#if interfaces?? && interfaces != ""> + <#assign implementation = ""> + <#list "${interfaces}"?split("\n") as interface> + <#assign implementation += "${interface}"[("${interface}"?last_index_of(".") + 1)..] + ", "> + + <#assign implementation = "${implementation}"?remove_ending(", ")> + +public class ${name} extends Applet<#if implementation?? && implementation != ""> implements ${implementation} { /** * Initialization method that will be called after the applet is loaded diff --git a/java/java.project.ui/src/org/netbeans/modules/java/project/ui/resources/Enum.java.template b/java/java.project.ui/src/org/netbeans/modules/java/project/ui/resources/Enum.java.template index 20d8e3de8b20..f535afd8e40f 100644 --- a/java/java.project.ui/src/org/netbeans/modules/java/project/ui/resources/Enum.java.template +++ b/java/java.project.ui/src/org/netbeans/modules/java/project/ui/resources/Enum.java.template @@ -6,11 +6,26 @@ <#if package?? && package != ""> package ${package}; + +<#if interfaces?? && interfaces != ""> + <#list "${interfaces}"?split("\n") as interface> + <#if interface?? && interface != "" && !interface?starts_with("java.lang") && !interface?matches(package + "\\.\\w+")> +import ${interface}; + + + /** * * @author ${user} */ -public enum ${name} { +<#if interfaces?? && interfaces != ""> + <#assign implementation = ""> + <#list "${interfaces}"?split("\n") as interface> + <#assign implementation += "${interface}"[("${interface}"?last_index_of(".") + 1)..] + ", "> + + <#assign implementation = "${implementation}"?remove_ending(", ")> + +public enum ${name}<#if implementation?? && implementation != ""> implements ${implementation} { } diff --git a/java/java.project.ui/src/org/netbeans/modules/java/project/ui/resources/Exception.java.template b/java/java.project.ui/src/org/netbeans/modules/java/project/ui/resources/Exception.java.template index 0c4f2949be0e..ab05caa21d2e 100644 --- a/java/java.project.ui/src/org/netbeans/modules/java/project/ui/resources/Exception.java.template +++ b/java/java.project.ui/src/org/netbeans/modules/java/project/ui/resources/Exception.java.template @@ -6,12 +6,34 @@ <#if package?? && package != ""> package ${package}; + +<#if superclass?? && superclass != "" && !superclass?starts_with("java.lang") && !superclass?matches(package + "\\.\\w+")> +import ${superclass}; + + +<#if interfaces?? && interfaces != ""> + <#list "${interfaces}"?split("\n") as interface> + <#if interface?? && interface != "" && !interface?starts_with("java.lang") && !interface?matches(package + "\\.\\w+")> +import ${interface}; + + + /** * * @author ${user} */ -public class ${name} extends Exception { +<#if superclass?? && superclass != ""> + <#assign extension = "${superclass}"[("${superclass}"?last_index_of(".") + 1)..]> + +<#if interfaces?? && interfaces != ""> + <#assign implementation = ""> + <#list "${interfaces}"?split("\n") as interface> + <#assign implementation += "${interface}"[("${interface}"?last_index_of(".") + 1)..] + ", "> + + <#assign implementation = "${implementation}"?remove_ending(", ")> + +public class ${name}<#if extension?? && extension != ""> extends ${extension}<#if implementation?? && implementation != ""> implements ${implementation} { /** * Creates a new instance of ${name} without detail message. diff --git a/java/java.project.ui/src/org/netbeans/modules/java/project/ui/resources/Interface.java.template b/java/java.project.ui/src/org/netbeans/modules/java/project/ui/resources/Interface.java.template index a830a8b410d2..159666cf495c 100644 --- a/java/java.project.ui/src/org/netbeans/modules/java/project/ui/resources/Interface.java.template +++ b/java/java.project.ui/src/org/netbeans/modules/java/project/ui/resources/Interface.java.template @@ -6,11 +6,18 @@ <#if package?? && package != ""> package ${package}; + +<#if superclass?? && superclass != "" && !superclass?starts_with("java.lang") && !superclass?matches(package + "\\.\\w+")> +import ${superclass}; + /** * * @author ${user} */ -public interface ${name} { +<#if superclass?? && superclass != ""> + <#assign extension = "${superclass}"[("${superclass}"?last_index_of(".") + 1)..]> + +public interface ${name}<#if extension?? && extension != ""> extends ${extension} { } diff --git a/java/java.project.ui/src/org/netbeans/modules/java/project/ui/resources/JApplet.java.template b/java/java.project.ui/src/org/netbeans/modules/java/project/ui/resources/JApplet.java.template index 51e58140cc4b..19646e7e2698 100644 --- a/java/java.project.ui/src/org/netbeans/modules/java/project/ui/resources/JApplet.java.template +++ b/java/java.project.ui/src/org/netbeans/modules/java/project/ui/resources/JApplet.java.template @@ -8,12 +8,26 @@ package ${package}; import javax.swing.JApplet; +<#if interfaces?? && interfaces != ""> + <#list "${interfaces}"?split("\n") as interface> + <#if interface?? && interface != "" && !interface?starts_with("java.lang") && !interface?matches(package + "\\.\\w+")> +import ${interface}; + + + /** * * @author ${user} */ -public class ${name} extends JApplet { +<#if interfaces?? && interfaces != ""> + <#assign implementation = ""> + <#list "${interfaces}"?split("\n") as interface> + <#assign implementation += "${interface}"[("${interface}"?last_index_of(".") + 1)..] + ", "> + + <#assign implementation = "${implementation}"?remove_ending(", ")> + +public class ${name} extends JApplet<#if implementation?? && implementation != ""> implements ${implementation} { /** * Initialization method that will be called after the applet is loaded diff --git a/java/java.project.ui/src/org/netbeans/modules/java/project/ui/resources/Main.java.template b/java/java.project.ui/src/org/netbeans/modules/java/project/ui/resources/Main.java.template index 1c29c8487ddf..a4e3bfb311b0 100644 --- a/java/java.project.ui/src/org/netbeans/modules/java/project/ui/resources/Main.java.template +++ b/java/java.project.ui/src/org/netbeans/modules/java/project/ui/resources/Main.java.template @@ -6,12 +6,34 @@ <#if package?? && package != ""> package ${package}; + +<#if superclass?? && superclass != "" && !superclass?starts_with("java.lang") && !superclass?matches(package + "\\.\\w+")> +import ${superclass}; + + +<#if interfaces?? && interfaces != ""> + <#list "${interfaces}"?split("\n") as interface> + <#if interface?? && interface != "" && !interface?starts_with("java.lang") && !interface?matches(package + "\\.\\w+")> +import ${interface}; + + + /** * * @author ${user} */ -public class ${name} { +<#if superclass?? && superclass != ""> + <#assign extension = "${superclass}"[("${superclass}"?last_index_of(".") + 1)..]> + +<#if interfaces?? && interfaces != ""> + <#assign implementation = ""> + <#list "${interfaces}"?split("\n") as interface> + <#assign implementation += "${interface}"[("${interface}"?last_index_of(".") + 1)..] + ", "> + + <#assign implementation = "${implementation}"?remove_ending(", ")> + +public class ${name}<#if extension?? && extension != ""> extends ${extension}<#if implementation?? && implementation != ""> implements ${implementation} { /** * @param args the command line arguments diff --git a/java/java.project.ui/src/org/netbeans/modules/java/project/ui/resources/Record.java.template b/java/java.project.ui/src/org/netbeans/modules/java/project/ui/resources/Record.java.template index 7d689609ed92..eb757424ae1b 100644 --- a/java/java.project.ui/src/org/netbeans/modules/java/project/ui/resources/Record.java.template +++ b/java/java.project.ui/src/org/netbeans/modules/java/project/ui/resources/Record.java.template @@ -6,11 +6,26 @@ <#if package?? && package != ""> package ${package}; + +<#if interfaces?? && interfaces != ""> + <#list "${interfaces}"?split("\n") as interface> + <#if interface?? && interface != "" && !interface?starts_with("java.lang") && !interface?matches(package + "\\.\\w+")> +import ${interface}; + + + /** * * @author ${user} */ -public record ${name}() { +<#if interfaces?? && interfaces != ""> + <#assign implementation = ""> + <#list "${interfaces}"?split("\n") as interface> + <#assign implementation += "${interface}"[("${interface}"?last_index_of(".") + 1)..] + ", "> + + <#assign implementation = "${implementation}"?remove_ending(", ")> + +public record ${name}()<#if implementation?? && implementation != ""> implements ${implementation} { } diff --git a/java/java.project.ui/src/org/netbeans/modules/java/project/ui/resources/Singleton.java.template b/java/java.project.ui/src/org/netbeans/modules/java/project/ui/resources/Singleton.java.template index 8438f11a5c62..a56566a6f65c 100644 --- a/java/java.project.ui/src/org/netbeans/modules/java/project/ui/resources/Singleton.java.template +++ b/java/java.project.ui/src/org/netbeans/modules/java/project/ui/resources/Singleton.java.template @@ -6,12 +6,34 @@ <#if package?? && package != ""> package ${package}; + +<#if superclass?? && superclass != "" && !superclass?starts_with("java.lang") && !superclass?matches(package + "\\.\\w+")> +import ${superclass}; + + +<#if interfaces?? && interfaces != ""> + <#list "${interfaces}"?split("\n") as interface> + <#if interface?? && interface != "" && !interface?starts_with("java.lang") && !interface?matches(package + "\\.\\w+")> +import ${interface}; + + + /** * * @author ${user} */ -public class ${name} { +<#if superclass?? && superclass != ""> + <#assign extension = "${superclass}"[("${superclass}"?last_index_of(".") + 1)..]> + +<#if interfaces?? && interfaces != ""> + <#assign implementation = ""> + <#list "${interfaces}"?split("\n") as interface> + <#assign implementation += "${interface}"[("${interface}"?last_index_of(".") + 1)..] + ", "> + + <#assign implementation = "${implementation}"?remove_ending(", ")> + +public class ${name}<#if extension?? && extension != ""> extends ${extension}<#if implementation?? && implementation != ""> implements ${implementation} { private ${name}() { } diff --git a/java/java.project.ui/test/qa-functional/data/SampleProject/build.xml b/java/java.project.ui/test/qa-functional/data/TestProject/build.xml similarity index 86% rename from java/java.project.ui/test/qa-functional/data/SampleProject/build.xml rename to java/java.project.ui/test/qa-functional/data/TestProject/build.xml index 8cc09d330d3f..a883debc31e9 100644 --- a/java/java.project.ui/test/qa-functional/data/SampleProject/build.xml +++ b/java/java.project.ui/test/qa-functional/data/TestProject/build.xml @@ -18,7 +18,7 @@ under the License. --> - - Builds, tests, and runs the project SampleProject. + + Builds, tests, and runs the project TestProject. diff --git a/java/java.project.ui/test/qa-functional/data/SampleProject/nbproject/project.properties b/java/java.project.ui/test/qa-functional/data/TestProject/nbproject/project.properties similarity index 95% rename from java/java.project.ui/test/qa-functional/data/SampleProject/nbproject/project.properties rename to java/java.project.ui/test/qa-functional/data/TestProject/nbproject/project.properties index 9770653837fb..c714ac094609 100644 --- a/java/java.project.ui/test/qa-functional/data/SampleProject/nbproject/project.properties +++ b/java/java.project.ui/test/qa-functional/data/TestProject/nbproject/project.properties @@ -20,7 +20,7 @@ annotation.processing.enabled.in.editor=false annotation.processing.processors.list= annotation.processing.run.all.processors=true annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output -application.title=SampleProject +application.title=TestProject build.classes.dir=${build.dir}/classes build.classes.excludes=**/*.java,**/*.form # This directory is removed when the project is cleaned: @@ -45,10 +45,10 @@ debug.test.modulepath=\ dist.archive.excludes= # This directory is removed when the project is cleaned: dist.dir=dist -dist.jar=${dist.dir}/SampleProject.jar +dist.jar=${dist.dir}/TestProject.jar dist.javadoc.dir=${dist.dir}/javadoc dist.jlink.dir=${dist.dir}/jlink -dist.jlink.output=${dist.jlink.dir}/SampleProject +dist.jlink.output=${dist.jlink.dir}/TestProject endorsed.classpath= excludes= includes=** @@ -88,7 +88,7 @@ jlink.additionalmodules= # The jlink additional command line parameters jlink.additionalparam= jlink.launcher=true -jlink.launcher.name=SampleProject +jlink.launcher.name=TestProject manifest.file=manifest.mf meta.inf.dir=${src.dir}/META-INF mkdist.disabled=false diff --git a/java/java.project.ui/test/qa-functional/data/SampleProject/nbproject/project.xml b/java/java.project.ui/test/qa-functional/data/TestProject/nbproject/project.xml similarity index 97% rename from java/java.project.ui/test/qa-functional/data/SampleProject/nbproject/project.xml rename to java/java.project.ui/test/qa-functional/data/TestProject/nbproject/project.xml index 5733f6b80d03..ee59a8f1b941 100644 --- a/java/java.project.ui/test/qa-functional/data/SampleProject/nbproject/project.xml +++ b/java/java.project.ui/test/qa-functional/data/TestProject/nbproject/project.xml @@ -22,7 +22,7 @@ org.netbeans.modules.java.j2seproject - SampleProject + TestProject diff --git a/java/java.project.ui/test/qa-functional/src/org/netbeans/modules/java/project/ui/NewJavaFileWizardTest.java b/java/java.project.ui/test/qa-functional/src/org/netbeans/modules/java/project/ui/NewJavaFileWizardTest.java index e2bdb5128e0b..2eebd850c550 100644 --- a/java/java.project.ui/test/qa-functional/src/org/netbeans/modules/java/project/ui/NewJavaFileWizardTest.java +++ b/java/java.project.ui/test/qa-functional/src/org/netbeans/modules/java/project/ui/NewJavaFileWizardTest.java @@ -21,6 +21,7 @@ import java.io.File; import java.io.IOException; import javax.swing.JComboBox; +import javax.swing.JLabel; import javax.swing.JTextArea; import javax.swing.JTextField; import junit.framework.Test; @@ -34,19 +35,29 @@ import org.netbeans.jemmy.operators.JTextFieldOperator; /** - * * @author: Arthur Sadykov */ public class NewJavaFileWizardTest extends JellyTestCase { - private static final String SAMPLE_PROJECT_NAME = "SampleProject"; + private static final String TEST_PROJECT_NAME = "TestProject"; private static final String JAVA_CATEGORY = "Java"; private static final String JAVA_CLASS_FILE_TYPE = "Java Class"; + private static final String JAVA_INTERFACE_FILE_TYPE = "Java Interface"; + private static final String JAVA_ENUM_FILE_TYPE = "Java Enum"; + private static final String JAVA_ANNOTATION_TYPE_FILE_TYPE = "Java Annotation Type"; + private static final String JAVA_EXCEPTION_FILE_TYPE = "Java Exception"; + private static final String JAPPLET_FILE_TYPE = "JApplet"; + private static final String APPLET_FILE_TYPE = "Applet"; + private static final String JAVA_MAIN_CLASS_FILE_TYPE = "Java Main Class"; + private static final String JAVA_SINGLETON_CLASS_FILE_TYPE = "Java Singleton Class"; + private static final String EMPTY_JAVA_FILE_TYPE = "Empty Java File"; + private static final String JAVA_RECORD_FILE_TYPE = "Java Record"; private static final String SERIALIZABLE_FQN = "java.io.Serializable"; private static final String OBSERVER_FQN = "java.util.Observer"; private static final String INTERFACES_LABEL_TEXT = "Interfaces:"; private static final String SUPERCLASS_LABEL_TEXT = "Superclass:"; - private static final String SAMPLE_PACKAGE_NAME = "sample"; + private static final String SUPERINTERFACE_LABEL_TEXT = "Superinterface:"; + private static final String TEST_PACKAGE_NAME = "test"; private static final String MAIN_CLASS_NAME = "Main"; private static final String PACKAGE_LABEL_TEXT = "Package:"; private static final String CLASS_NAME_LABEL_TEXT = "Class Name:"; @@ -58,8 +69,9 @@ public class NewJavaFileWizardTest extends JellyTestCase { private static final String IMPORT_OBSERVER = "import java.util.Observer;"; private static final String IMPORT_THREAD = "import java.lang.Thread;"; private static final String IMPORT_CLONEABLE = "import java.lang.Cloneable;"; - private static final String BASE_CLASS_FQN = "sample.BaseClass"; - private static final String IMPORT_BASE_CLASS = "import sample.BaseClass;"; + private static final String TEST_CLASS_FQN = "test.Test"; + private static final String IMPORT_TEST_CLASS = "import test.Test;"; + private static final String NEW_LINE = "\n"; private JTextFieldOperator superclassTextFieldOperator; private JTextAreaOperator interfacesTextAreaOperator; private NewFileWizardOperator wizardOperator; @@ -75,72 +87,217 @@ public static Test suite() { @Override public void setUp() throws IOException { clearWorkDir(); - openDataProjects(SAMPLE_PROJECT_NAME); + openDataProjects(TEST_PROJECT_NAME); ProjectsTabOperator.invoke(); wizardOperator = NewFileWizardOperator.invoke(); - wizardOperator.selectProject(SAMPLE_PROJECT_NAME); + wizardOperator.selectProject(TEST_PROJECT_NAME); wizardOperator.selectCategory(JAVA_CATEGORY); + } + + public void testNewJavaClassWizardShouldContainExtensionAndImplementationBoxes() { wizardOperator.selectFileType(JAVA_CLASS_FILE_TYPE); wizardOperator.next(); - JLabelOperator classNameLabelOperator = new JLabelOperator(wizardOperator, CLASS_NAME_LABEL_TEXT); - JTextFieldOperator classNameTextFieldOperator = - new JTextFieldOperator((JTextField) classNameLabelOperator.getLabelFor()); - classNameTextFieldOperator.setText(MAIN_CLASS_NAME); - JLabelOperator packageLabelOperator = new JLabelOperator(wizardOperator, PACKAGE_LABEL_TEXT); - JComboBoxOperator packageComboBoxOperator = - new JComboBoxOperator((JComboBox) packageLabelOperator.getLabelFor()); - packageComboBoxOperator.typeText(SAMPLE_PACKAGE_NAME); - JLabelOperator superclassLabelOperator = new JLabelOperator(wizardOperator, SUPERCLASS_LABEL_TEXT); - superclassTextFieldOperator = new JTextFieldOperator((JTextField) superclassLabelOperator.getLabelFor()); - JLabelOperator interfacesLabelOperator = - new JLabelOperator(wizardOperator, INTERFACES_LABEL_TEXT); - interfacesTextAreaOperator = - new JTextAreaOperator((JTextArea) interfacesLabelOperator.getLabelFor()); + assertExtensionBoxIsPresent(); + assertImplementationBoxIsPresent(); + wizardOperator.cancel(); + } + + public void testNewJavaInterfaceWizardShouldContainOnlyExtensionBoxForInterface() { + wizardOperator.selectFileType(JAVA_INTERFACE_FILE_TYPE); + wizardOperator.next(); + assertExtensionBoxForInterfaceIsPresent(); + assertImplementationBoxIsNotPresent(); + wizardOperator.cancel(); + } + + public void testNewJavaEnumWizardShouldContainOnlyImplementationBox() { + wizardOperator.selectFileType(JAVA_ENUM_FILE_TYPE); + wizardOperator.next(); + assertExtensionBoxIsNotPresent(); + assertImplementationBoxIsPresent(); + wizardOperator.cancel(); + } + + public void testNewJavaAnnotationTypeWizardShouldContainNeitherExtensionNorImplementationBox() { + wizardOperator.selectFileType(JAVA_ANNOTATION_TYPE_FILE_TYPE); + wizardOperator.next(); + assertExtensionBoxIsNotPresent(); + assertImplementationBoxIsNotPresent(); + wizardOperator.cancel(); + } + + public void testNewJavaExceptionWizardShouldContainExtensionAndImplementationBoxes() { + wizardOperator.selectFileType(JAVA_EXCEPTION_FILE_TYPE); + wizardOperator.next(); + assertExtensionBoxIsPresent(); + assertImplementationBoxIsPresent(); + wizardOperator.cancel(); + } + + public void testNewJAppletWizardShouldContainOnlyImplementationBox() { + wizardOperator.selectFileType(JAPPLET_FILE_TYPE); + wizardOperator.next(); + assertExtensionBoxIsNotPresent(); + assertImplementationBoxIsPresent(); + wizardOperator.cancel(); + } + + public void testNewAppletWizardShouldContainOnlyImplementationBox() { + wizardOperator.selectFileType(APPLET_FILE_TYPE); + wizardOperator.next(); + assertExtensionBoxIsNotPresent(); + assertImplementationBoxIsPresent(); + wizardOperator.cancel(); + } + + public void testNewJavaMainClassWizardShouldContainExtensionAndImplementationBoxes() { + wizardOperator.selectFileType(JAVA_MAIN_CLASS_FILE_TYPE); + wizardOperator.next(); + assertExtensionBoxIsPresent(); + assertImplementationBoxIsPresent(); + wizardOperator.cancel(); + } + + public void testNewJavaSingletonClassWizardShouldContainExtensionAndImplementationBoxes() { + wizardOperator.selectFileType(JAVA_SINGLETON_CLASS_FILE_TYPE); + wizardOperator.next(); + assertExtensionBoxIsPresent(); + assertImplementationBoxIsPresent(); + wizardOperator.cancel(); + } + + public void testNewEmptyJavaFileWizardShouldContainNeitherExtensionNorImplementationBox() { + wizardOperator.selectFileType(EMPTY_JAVA_FILE_TYPE); + wizardOperator.next(); + assertExtensionBoxIsNotPresent(); + assertImplementationBoxIsNotPresent(); + wizardOperator.cancel(); + } + + public void testNewJavaRecordWizardShouldContainOnlyImplementationBox() { + wizardOperator.selectFileType(JAVA_RECORD_FILE_TYPE); + wizardOperator.next(); + assertExtensionBoxIsNotPresent(); + assertImplementationBoxIsPresent(); + wizardOperator.cancel(); + } + + public void testShouldAddRequiredImports() { + instantiateOperatorsForNewJavaClassWizard(); + superclassTextFieldOperator.setText(STACK_FQN); + interfacesTextAreaOperator.append(SERIALIZABLE_FQN); + interfacesTextAreaOperator.append(NEW_LINE); + interfacesTextAreaOperator.append(OBSERVER_FQN); + wizardOperator.finish(); + EditorOperator editorOperator = new EditorOperator(MAIN_CLASS_NAME); + String text = editorOperator.getText(); + assertGeneratedClassContainsImport(IMPORT_STACK, text); + assertGeneratedClassContainsImport(IMPORT_SERIALIZABLE, text); + assertGeneratedClassContainsImport(IMPORT_OBSERVER, text); } - public void testShouldAddImportsExtendsAndImplementsClauses() { + public void testShouldAddRequiredExtendsAndImplementsClauses() { + instantiateOperatorsForNewJavaClassWizard(); superclassTextFieldOperator.setText(STACK_FQN); interfacesTextAreaOperator.append(SERIALIZABLE_FQN); - interfacesTextAreaOperator.append("\n"); + interfacesTextAreaOperator.append(NEW_LINE); interfacesTextAreaOperator.append(OBSERVER_FQN); wizardOperator.finish(); EditorOperator editorOperator = new EditorOperator(MAIN_CLASS_NAME); String text = editorOperator.getText(); - assertTrue("Generated class should contain import for java.util.Stack", text.contains(IMPORT_STACK)); - assertTrue("Generated class should contain import for java.io.Serializable", text.contains(IMPORT_SERIALIZABLE)); - assertTrue("Generated class should contain import for java.util.Observer", text.contains(IMPORT_OBSERVER)); - String expectedClassSignature = "public class Main extends Stack implements Serializable, Observer"; - assertTrue("Generated class should have expected signature", text.contains(expectedClassSignature)); + assertGeneratedClassContainsImport(IMPORT_STACK, text); + assertGeneratedClassContainsImport(IMPORT_SERIALIZABLE, text); + assertGeneratedClassContainsImport(IMPORT_OBSERVER, text); + assertGeneratedClassHasHeader("public class Main extends Stack implements Serializable, Observer", text); } public void testShouldNotAddImportsForClassesFromJavaLangPackage() { + instantiateOperatorsForNewJavaClassWizard(); superclassTextFieldOperator.setText(THREAD_FQN); interfacesTextAreaOperator.setText(CLONEABLE_FQN); wizardOperator.finish(); EditorOperator editorOperator = new EditorOperator(MAIN_CLASS_NAME); String text = editorOperator.getText(); - assertTrue("Generated class should not contain import for java.lang.Thread", !text.contains(IMPORT_THREAD)); - assertTrue("Generated class should not contain import for java.lang.Cloneable", !text.contains(IMPORT_CLONEABLE)); - String expectedClassSignature = "public class Main extends Thread implements Cloneable"; - assertTrue("Generated class should have expected signature", text.contains(expectedClassSignature)); + assertGeneratedClassNotContainsImport(IMPORT_THREAD, text); + assertGeneratedClassNotContainsImport(IMPORT_CLONEABLE, text); + assertGeneratedClassHasHeader("public class Main extends Thread implements Cloneable", text); } - + public void testShouldNotAddImportsForClassesFromSamePackage() { - superclassTextFieldOperator.setText(BASE_CLASS_FQN); + instantiateOperatorsForNewJavaClassWizard(); + superclassTextFieldOperator.setText(TEST_CLASS_FQN); wizardOperator.finish(); EditorOperator editorOperator = new EditorOperator(MAIN_CLASS_NAME); String text = editorOperator.getText(); - assertTrue("Generated class should not contain import for sample.BaseClass", !text.contains(IMPORT_BASE_CLASS)); - String expectedClassSignature = "public class Main extends Base"; - assertTrue("Generated class should have expected signature", text.contains(expectedClassSignature)); + assertGeneratedClassNotContainsImport(IMPORT_TEST_CLASS, text); + assertGeneratedClassHasHeader("public class Main extends Test", text); + } + + private void instantiateOperatorsForNewJavaClassWizard() { + wizardOperator.selectFileType(JAVA_CLASS_FILE_TYPE); + wizardOperator.next(); + JLabelOperator classNameLabelOperator = new JLabelOperator(wizardOperator, CLASS_NAME_LABEL_TEXT); + JTextFieldOperator classNameTextFieldOperator = + new JTextFieldOperator((JTextField) classNameLabelOperator.getLabelFor()); + classNameTextFieldOperator.setText(MAIN_CLASS_NAME); + JLabelOperator packageLabelOperator = new JLabelOperator(wizardOperator, PACKAGE_LABEL_TEXT); + JComboBoxOperator packageComboBoxOperator = + new JComboBoxOperator((JComboBox) packageLabelOperator.getLabelFor()); + packageComboBoxOperator.typeText(TEST_PACKAGE_NAME); + JLabelOperator superclassLabelOperator = new JLabelOperator(wizardOperator, SUPERCLASS_LABEL_TEXT); + superclassTextFieldOperator = new JTextFieldOperator((JTextField) superclassLabelOperator.getLabelFor()); + JLabelOperator interfacesLabelOperator = new JLabelOperator(wizardOperator, INTERFACES_LABEL_TEXT); + interfacesTextAreaOperator = new JTextAreaOperator((JTextArea) interfacesLabelOperator.getLabelFor()); + } + + private void assertExtensionBoxIsPresent() { + JLabel superclassLabel = + JLabelOperator.findJLabel(wizardOperator.getContentPane(), SUPERCLASS_LABEL_TEXT, true, true); + assertNotNull("The wizard should contain the 'Superclass' label", superclassLabel); + } + + private void assertExtensionBoxForInterfaceIsPresent() { + JLabel superclassLabel = + JLabelOperator.findJLabel(wizardOperator.getContentPane(), SUPERINTERFACE_LABEL_TEXT, true, true); + assertNotNull("The wizard should contain the 'Superinterface' label", superclassLabel); + } + + private void assertExtensionBoxIsNotPresent() { + JLabel superclassLabel = + JLabelOperator.findJLabel(wizardOperator.getContentPane(), SUPERCLASS_LABEL_TEXT, true, true); + assertNull("The wizard should not contain the 'Superclass' label", superclassLabel); + } + + private void assertImplementationBoxIsPresent() { + JLabel interfacesLabel = + JLabelOperator.findJLabel(wizardOperator.getContentPane(), INTERFACES_LABEL_TEXT, true, true); + assertNotNull("The wizard should contain the 'Interfaces' label", interfacesLabel); + } + + private void assertImplementationBoxIsNotPresent() { + JLabel interfacesLabel = + JLabelOperator.findJLabel(wizardOperator.getContentPane(), INTERFACES_LABEL_TEXT, true, true); + assertNull("The wizard should not contain the 'Interfaces' label", interfacesLabel); + } + + private void assertGeneratedClassContainsImport(String expected, String actual) { + assertTrue("Generated class should contain import for " + expected.substring(7), actual.contains(expected)); + } + + private void assertGeneratedClassNotContainsImport(String expected, String actual) { + assertFalse("Generated class should not contain import for " + expected.substring(7), actual.contains(expected)); + } + + private void assertGeneratedClassHasHeader(String expected, String actual) { + assertTrue("Generated class should have expected header", actual.contains(expected)); } @Override protected void tearDown() throws Exception { closeOpenedProjects(); String separator = File.separator; - String testFileName = getDataDir().getAbsolutePath() + separator + SAMPLE_PROJECT_NAME + separator + "src" - + separator + SAMPLE_PACKAGE_NAME + separator + MAIN_CLASS_NAME + ".java"; + String testFileName = getDataDir().getAbsolutePath() + separator + TEST_PROJECT_NAME + separator + "src" + + separator + TEST_PACKAGE_NAME + separator + MAIN_CLASS_NAME + ".java"; File testFile = new File(testFileName); testFile.delete(); }