diff --git a/plugins/ui-designer/src/com/intellij/uiDesigner/make/FormSourceCodeGenerator.java b/plugins/ui-designer/src/com/intellij/uiDesigner/make/FormSourceCodeGenerator.java index 94bd43fe6002..16a1928c4aeb 100644 --- a/plugins/ui-designer/src/com/intellij/uiDesigner/make/FormSourceCodeGenerator.java +++ b/plugins/ui-designer/src/com/intellij/uiDesigner/make/FormSourceCodeGenerator.java @@ -233,9 +233,33 @@ public final class FormSourceCodeGenerator { PsiClass newClass = (PsiClass) classToBind.copy(); + PsiElement initializerComment = null; + PsiElement firstMethodMarker = null; + PsiMethod[] methods = newClass.findMethodsByName(AsmCodeGenerator.SETUP_METHOD_NAME, false); + PsiClassInitializer[] initializers = newClass.getInitializers(); + + for (PsiMethod method: methods) { + if (method.hasParameters()) { + continue; + } + for (PsiClassInitializer initializer : initializers) { + if (containsMethodIdentifier(initializer, method)) { + PsiElement child = initializer.getFirstChild(); + // very smart java PSI move comment node before class initializer to initializer children + if (child instanceof PsiComment && child.getTextOffset() == initializer.getTextOffset()) { + initializerComment = child.copy(); + break; + } + } + } + firstMethodMarker = newClass + .addBefore(elementFactory.createFieldFromText("private int ____temp_field__" + System.currentTimeMillis() + ";", null), method); + break; + } + cleanup(newClass); - // [anton] the comments are written according to the SCR 26896 + // [anton] the comments are written according to the SCR 26896 final PsiClass fakeClass = elementFactory.createClassFromText( "{\n" + "// GUI initializer generated by " + ApplicationNamesInfo.getInstance().getFullProductName() + " GUI Designer\n" + @@ -259,7 +283,12 @@ public final class FormSourceCodeGenerator { final CodeStyleManager formatter = CodeStyleManager.getInstance(module.getProject()); final JavaCodeStyleManager styler = JavaCodeStyleManager.getInstance(module.getProject()); - PsiMethod method = (PsiMethod) newClass.add(fakeClass.getMethods()[0]); + PsiMethod method = (PsiMethod)(firstMethodMarker == null + ? newClass.add(fakeClass.getMethods()[0]) + : newClass.addAfter(fakeClass.getMethods()[0], firstMethodMarker)); + if (firstMethodMarker != null) { + firstMethodMarker.delete(); + } // don't generate initializer block if $$$setupUI$$$() is called explicitly from one of the constructors boolean needInitializer = true; @@ -281,7 +310,10 @@ public final class FormSourceCodeGenerator { } if (needInitializer) { - newClass.addBefore(fakeClass.getInitializers()[0], method); + PsiElement initializer = newClass.addBefore(fakeClass.getInitializers()[0], method); + if (initializerComment != null) { + newClass.addBefore(initializerComment, initializer); + } } @NonNls final String grcMethodText = "/** @noinspection ALL */ public javax.swing.JComponent " + @@ -456,8 +488,8 @@ public final class FormSourceCodeGenerator { " component.setText(result.toString()); " + " if (haveMnemonic) {" + (componentClass.equals(AbstractButton.class) - ? " component.setMnemonic(mnemonic);" - : " component.setDisplayedMnemonic(mnemonic);") + + ? " component.setMnemonic(mnemonic);" + : " component.setDisplayedMnemonic(mnemonic);") + (needIndex ? "component.setDisplayedMnemonicIndex(mnemonicIndex);" : "") + "} }"; } @@ -481,7 +513,7 @@ public final class FormSourceCodeGenerator { } } - public static void cleanup(final PsiClass aClass) throws IncorrectOperationException{ + public static void cleanup(final PsiClass aClass) throws IncorrectOperationException { final PsiMethod[] methods = aClass.findMethodsByName(AsmCodeGenerator.SETUP_METHOD_NAME, false); for (final PsiMethod method: methods) { final PsiClassInitializer[] initializers = aClass.getInitializers(); @@ -1025,7 +1057,7 @@ public final class FormSourceCodeGenerator { pushFont(variable, fontDescriptor, getterName); myBuffer.append("if (").append(variable).append("Font != null) ").append(variable).append(".").append(setterName).append("(") - .append(variable).append("Font);\n"); + .append(variable).append("Font);\n"); } private void pushFont(final String variable, final FontDescriptor fontDescriptor, @NonNls final String getterName) { @@ -1213,7 +1245,7 @@ public final class FormSourceCodeGenerator { } void append(short value) { - myBuffer.append("(short) "); + myBuffer.append("(short) "); myBuffer.append(value); } diff --git a/plugins/ui-designer/testData/sourceCodeGenerator/duplicateSetupCall/BindingTest.java.after b/plugins/ui-designer/testData/sourceCodeGenerator/duplicateSetupCall/BindingTest.java.after index d5709bedaac7..7b4a8aaacc3e 100644 --- a/plugins/ui-designer/testData/sourceCodeGenerator/duplicateSetupCall/BindingTest.java.after +++ b/plugins/ui-designer/testData/sourceCodeGenerator/duplicateSetupCall/BindingTest.java.after @@ -57,4 +57,5 @@ public class BindingTest { public JComponent $$$getRootComponent$$$() { return myRootComponent; } + } diff --git a/plugins/ui-designer/testData/sourceCodeGenerator/initializerWithComments/BindingTest.java b/plugins/ui-designer/testData/sourceCodeGenerator/initializerWithComments/BindingTest.java new file mode 100644 index 000000000000..d3e63fd6d789 --- /dev/null +++ b/plugins/ui-designer/testData/sourceCodeGenerator/initializerWithComments/BindingTest.java @@ -0,0 +1,52 @@ +/* + * Copyright 2000-2009 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import javax.swing.*; + +public class BindingTest { + public JComponent myRootComponent; + public static JComponent myStaticField; + public final JComponent myFinalField = null; + public int myIntField; + public String myStringField; + + // CHECKSTYLE:OFF + { + // GUI initializer generated by IntelliJ IDEA GUI Designer + // >>> IMPORTANT!! <<< + // DO NOT EDIT OR ADD ANY CODE HERE! + $$$setupUI$$$(); + } + + /** + * Method generated by IntelliJ IDEA GUI Designer + * >>> IMPORTANT!! <<< + * DO NOT edit this method OR call it in your code! + * + * @noinspection ALL + */ + private void $$$setupUI$$$() { + myRootComponent = new JPanel(); + myRootComponent.setLayout(new com.intellij.uiDesigner.core.GridLayoutManager(1, 1, new java.awt.Insets(0, 0, 0, 0), -1, -1)); + } + + /** + * @noinspection ALL + */ + public JComponent $$$getRootComponent$$$() { + return myRootComponent; + } + // CHECKSTYLE:ON +} diff --git a/plugins/ui-designer/testData/sourceCodeGenerator/initializerWithComments/BindingTest.java.after b/plugins/ui-designer/testData/sourceCodeGenerator/initializerWithComments/BindingTest.java.after new file mode 100644 index 000000000000..d3e63fd6d789 --- /dev/null +++ b/plugins/ui-designer/testData/sourceCodeGenerator/initializerWithComments/BindingTest.java.after @@ -0,0 +1,52 @@ +/* + * Copyright 2000-2009 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import javax.swing.*; + +public class BindingTest { + public JComponent myRootComponent; + public static JComponent myStaticField; + public final JComponent myFinalField = null; + public int myIntField; + public String myStringField; + + // CHECKSTYLE:OFF + { + // GUI initializer generated by IntelliJ IDEA GUI Designer + // >>> IMPORTANT!! <<< + // DO NOT EDIT OR ADD ANY CODE HERE! + $$$setupUI$$$(); + } + + /** + * Method generated by IntelliJ IDEA GUI Designer + * >>> IMPORTANT!! <<< + * DO NOT edit this method OR call it in your code! + * + * @noinspection ALL + */ + private void $$$setupUI$$$() { + myRootComponent = new JPanel(); + myRootComponent.setLayout(new com.intellij.uiDesigner.core.GridLayoutManager(1, 1, new java.awt.Insets(0, 0, 0, 0), -1, -1)); + } + + /** + * @noinspection ALL + */ + public JComponent $$$getRootComponent$$$() { + return myRootComponent; + } + // CHECKSTYLE:ON +} diff --git a/plugins/ui-designer/testData/sourceCodeGenerator/initializerWithComments/Test.form b/plugins/ui-designer/testData/sourceCodeGenerator/initializerWithComments/Test.form new file mode 100644 index 000000000000..3e597d395d63 --- /dev/null +++ b/plugins/ui-designer/testData/sourceCodeGenerator/initializerWithComments/Test.form @@ -0,0 +1,13 @@ + +
+ + + + + + + + + + +
diff --git a/plugins/ui-designer/testData/sourceCodeGenerator/setupCallWithComments/BindingTest.java b/plugins/ui-designer/testData/sourceCodeGenerator/setupCallWithComments/BindingTest.java new file mode 100644 index 000000000000..a2499cef75ed --- /dev/null +++ b/plugins/ui-designer/testData/sourceCodeGenerator/setupCallWithComments/BindingTest.java @@ -0,0 +1,60 @@ +/* + * Copyright 2000-2009 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import javax.swing.*; + +public class BindingTest { + private int a; + public JComponent myRootComponent; + private JLabel myLabel; + + public BindingTest() { + a = 0; + $$$setupUI$$$(); + myLabel.setText("My Text"); + } + + public BindingTest(int i) { + a = i; + myLabel.setText("My Text"); + } + + private void createUIComponents() { + myLabel = new JLabel(); + } + + // CHECKSTYLE:OFF + /** + * Method generated by IntelliJ IDEA GUI Designer + * >>> IMPORTANT!! <<< + * DO NOT edit this method OR call it in your code! + * + * @noinspection ALL + */ + private void $$$setupUI$$$() { + createUIComponents(); + myRootComponent = new JPanel(); + myRootComponent.setLayout(new com.intellij.uiDesigner.core.GridLayoutManager(1, 1, new java.awt.Insets(0, 0, 0, 0), -1, -1)); + myRootComponent.add(myLabel, new com.intellij.uiDesigner.core.GridConstraints(0, 0, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_NONE, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false)); + } + + /** + * @noinspection ALL + */ + public JComponent $$$getRootComponent$$$() { + return myRootComponent; + } + // CHECKSTYLE:ON +} diff --git a/plugins/ui-designer/testData/sourceCodeGenerator/setupCallWithComments/BindingTest.java.after b/plugins/ui-designer/testData/sourceCodeGenerator/setupCallWithComments/BindingTest.java.after new file mode 100644 index 000000000000..b6213bed574c --- /dev/null +++ b/plugins/ui-designer/testData/sourceCodeGenerator/setupCallWithComments/BindingTest.java.after @@ -0,0 +1,64 @@ +/* + * Copyright 2000-2009 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import javax.swing.*; + +public class BindingTest { + private int a; + public JComponent myRootComponent; + private JLabel myLabel; + + public BindingTest() { + a = 0; + $$$setupUI$$$(); + myLabel.setText("My Text"); + } + + public BindingTest(int i) { + a = i; + $$$setupUI$$$(); + myLabel.setText("My Text"); + } + + private void createUIComponents() { + myLabel = new JLabel(); + } + + // CHECKSTYLE:OFF + + /** + * Method generated by IntelliJ IDEA GUI Designer + * >>> IMPORTANT!! <<< + * DO NOT edit this method OR call it in your code! + * + * @noinspection ALL + */ + private void $$$setupUI$$$() { + createUIComponents(); + myRootComponent = new JPanel(); + myRootComponent.setLayout(new com.intellij.uiDesigner.core.GridLayoutManager(1, 1, new java.awt.Insets(0, 0, 0, 0), -1, -1)); + myLabel.setText("Label"); + myRootComponent.add(myLabel, new com.intellij.uiDesigner.core.GridConstraints(0, 0, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_NONE, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false)); + } + + /** + * @noinspection ALL + */ + public JComponent $$$getRootComponent$$$() { + return myRootComponent; + } + + // CHECKSTYLE:ON +} diff --git a/plugins/ui-designer/testData/sourceCodeGenerator/setupCallWithComments/Test.form b/plugins/ui-designer/testData/sourceCodeGenerator/setupCallWithComments/Test.form new file mode 100644 index 000000000000..9066786d570b --- /dev/null +++ b/plugins/ui-designer/testData/sourceCodeGenerator/setupCallWithComments/Test.form @@ -0,0 +1,21 @@ + +
+ + + + + + + + + + + + + + + + + + +
diff --git a/plugins/ui-designer/testSrc/com/intellij/uiDesigner/make/FormSourceCodeGeneratorTest.java b/plugins/ui-designer/testSrc/com/intellij/uiDesigner/make/FormSourceCodeGeneratorTest.java index 5d6b49047c7e..e80e225ef657 100644 --- a/plugins/ui-designer/testSrc/com/intellij/uiDesigner/make/FormSourceCodeGeneratorTest.java +++ b/plugins/ui-designer/testSrc/com/intellij/uiDesigner/make/FormSourceCodeGeneratorTest.java @@ -72,6 +72,14 @@ public class FormSourceCodeGeneratorTest extends PsiTestCase { doTest(); } + public void testSetupCallWithComments() throws IOException { + doTest(); + } + + public void testInitializerWithComments() throws IOException { + doTest(); + } + private void doTest() throws IOException { final VirtualFile form = myTestProjectRoot.findChild("Test.form"); assertNotNull(form);