ui designer: update generated java forms code on save

required for IJI-2340 Remove UI Designer forms compilation


(cherry picked from commit 75f67b35e640757a655668e5bc360488578b9db8)

IJ-CR-148847

GitOrigin-RevId: 8d172caec6f1a3f518c9ad2129a8e1825f2b51ae
This commit is contained in:
Leonid Shalupov
2024-11-07 17:35:00 +01:00
committed by intellij-monorepo-bot
parent 6b9a06d2ae
commit 0f208ed8b1
6 changed files with 83 additions and 3 deletions

View File

@@ -513,6 +513,7 @@ group.GuiDesigner.MainToolbarActions.text=UI Designer Toolbar Actions
group.GuiDesigner.NewActions.text=Swing UI Designer
ui.designer.general.settings.resize.column.and.row.headers=&Resize column and row headers with mouse
ui.designer.general.settings.use.dynamic.bundles=Allow changing locale at runtime (requires 2020.1)
ui.designer.general.settings.generate.source.code.on.form.save=Generate source code on form save
inspection.message.typo.in.word=Typo in word ''{0}''
inspection.message.typo.in.word.title=Typo in a UI form
intention.name.save.to.dictionary=Save ''{0}'' to dictionary

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.intellij.uiDesigner.GuiDesignerConfigurable.MyGeneralUI">
<grid id="4a770" binding="myPanel" layout-manager="GridLayoutManager" row-count="7" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="4a770" binding="myPanel" layout-manager="GridLayoutManager" row-count="8" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="62" y="47" width="493" height="278"/>
@@ -10,7 +10,7 @@
<children>
<vspacer id="d5f77">
<constraints>
<grid row="6" column="0" row-span="1" col-span="2" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
<grid row="7" column="0" row-span="1" col-span="2" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<grid id="b77c5" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="0">
@@ -117,6 +117,15 @@
<text resource-bundle="messages/UIDesignerBundle" key="ui.designer.general.settings.use.dynamic.bundles"/>
</properties>
</component>
<component id="12841" class="javax.swing.JCheckBox" binding="myChkGenerateSourceCodeOnFormSave">
<constraints>
<grid row="6" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<margin top="10" left="0" bottom="2" right="2"/>
<text resource-bundle="messages/UIDesignerBundle" key="ui.designer.general.settings.generate.source.code.on.form.save"/>
</properties>
</component>
</children>
</grid>
<buttonGroups>

View File

@@ -74,6 +74,10 @@ public final class GuiDesignerConfigurable implements SearchableConfigurable, Co
return true;
}
if (myGeneralUI.myChkGenerateSourceCodeOnFormSave.isSelected() != configuration.GENERATE_SOURCES_ON_SAVE) {
return true;
}
if (!Comparing.equal(configuration.DEFAULT_LAYOUT_MANAGER, myGeneralUI.myLayoutManagerCombo.getSelectedItem())) {
return true;
}
@@ -102,6 +106,7 @@ public final class GuiDesignerConfigurable implements SearchableConfigurable, Co
configuration.DEFAULT_FIELD_ACCESSIBILITY = (String)myGeneralUI .myDefaultFieldAccessibilityCombo.getSelectedItem();
configuration.RESIZE_HEADERS = myGeneralUI.myResizeHeaders.isSelected();
configuration.USE_DYNAMIC_BUNDLES = myGeneralUI.myChkUseDynamicBundles.isSelected();
configuration.GENERATE_SOURCES_ON_SAVE = myGeneralUI.myChkGenerateSourceCodeOnFormSave.isSelected();
if (configuration.INSTRUMENT_CLASSES && !myProject.isDefault()) {
final DispatchThreadProgressWindow progressWindow = new DispatchThreadProgressWindow(false, myProject);
@@ -124,6 +129,7 @@ public final class GuiDesignerConfigurable implements SearchableConfigurable, Co
}
myGeneralUI.myChkCopyFormsRuntime.setSelected(configuration.COPY_FORMS_RUNTIME_TO_OUTPUT);
myGeneralUI.myChkUseDynamicBundles.setSelected(configuration.USE_DYNAMIC_BUNDLES);
myGeneralUI.myChkGenerateSourceCodeOnFormSave.setSelected(configuration.GENERATE_SOURCES_ON_SAVE);
myGeneralUI.myLayoutManagerCombo.setModel(new DefaultComboBoxModel<>(LayoutManagerRegistry.getNonDeprecatedLayoutManagerNames()));
myGeneralUI.myLayoutManagerCombo.setRenderer(SimpleListCellRenderer.create("", LayoutManagerRegistry::getLayoutManagerDisplayName));
@@ -145,6 +151,7 @@ public final class GuiDesignerConfigurable implements SearchableConfigurable, Co
public JRadioButton myRbInstrumentSources;
public JCheckBox myChkCopyFormsRuntime;
public JCheckBox myChkUseDynamicBundles;
public JCheckBox myChkGenerateSourceCodeOnFormSave;
private JComboBox<String> myLayoutManagerCombo;
private JComboBox<String> myDefaultFieldAccessibilityCombo;
private JCheckBox myResizeHeaders;

View File

@@ -25,6 +25,13 @@ public final class GuiDesignerConfiguration implements PersistentStateComponent<
public boolean COPY_FORMS_RUNTIME_TO_OUTPUT = true;
/**
* If INSTRUMENT_CLASSES is false, the user may select between generating source files
* upon Build (default, GENERATE_SOURCES_ON_SAVE is false) and
* generating right after saving the form (GENERATE_SOURCES_ON_SAVE is true)
*/
public boolean GENERATE_SOURCES_ON_SAVE = false;
public @NlsSafe String DEFAULT_LAYOUT_MANAGER = UIFormXmlConstants.LAYOUT_INTELLIJ;
public @NlsSafe String DEFAULT_FIELD_ACCESSIBILITY = PsiModifier.PRIVATE;

View File

@@ -2,6 +2,9 @@
package com.intellij.uiDesigner.designSurface;
import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer;
import com.intellij.compiler.CompilerMessageImpl;
import com.intellij.compiler.impl.ExitStatus;
import com.intellij.compiler.progress.BuildOutputService;
import com.intellij.designer.DesignerEditorPanelFacade;
import com.intellij.designer.LightFillLayout;
import com.intellij.ide.DeleteProvider;
@@ -15,6 +18,7 @@ import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.command.undo.UndoManager;
import com.intellij.openapi.compiler.CompilerMessageCategory;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
@@ -41,6 +45,7 @@ import com.intellij.ui.JBColor;
import com.intellij.ui.ScrollPaneFactory;
import com.intellij.ui.components.JBLayeredPane;
import com.intellij.uiDesigner.*;
import com.intellij.uiDesigner.compiler.FormErrorInfo;
import com.intellij.uiDesigner.compiler.Utils;
import com.intellij.uiDesigner.componentTree.ComponentPtr;
import com.intellij.uiDesigner.componentTree.ComponentSelectionListener;
@@ -51,6 +56,8 @@ import com.intellij.uiDesigner.editor.UIFormEditor;
import com.intellij.uiDesigner.lw.CompiledClassPropertiesProvider;
import com.intellij.uiDesigner.lw.IProperty;
import com.intellij.uiDesigner.lw.LwRootContainer;
import com.intellij.uiDesigner.make.FormElementNavigatable;
import com.intellij.uiDesigner.make.FormSourceCodeGenerator;
import com.intellij.uiDesigner.palette.ComponentItem;
import com.intellij.uiDesigner.propertyInspector.DesignerToolWindow;
import com.intellij.uiDesigner.propertyInspector.DesignerToolWindowManager;
@@ -627,6 +634,9 @@ public final class GuiEditor extends JPanel implements DesignerEditorPanelFacade
private void saveToFile() {
LOG.debug("GuiEditor.saveToFile(): group ID=" + myNextSaveGroupId);
GuiDesignerConfiguration designerConfiguration = GuiDesignerConfiguration.getInstance(myProject);
CommandProcessor.getInstance().executeCommand(getProject(), () -> ApplicationManager.getApplication().runWriteAction(() -> {
myInsideChange = true;
try {
@@ -648,6 +658,50 @@ public final class GuiEditor extends JPanel implements DesignerEditorPanelFacade
LOG.error(e);
myDocument.replaceString(0, oldText.length(), newText);
}
if (!designerConfiguration.INSTRUMENT_CLASSES && designerConfiguration.GENERATE_SOURCES_ON_SAVE) {
try {
LOG.debug("Updating sources for form '" + myFile.getPath());
FileDocumentManager.getInstance().saveDocument(myDocument);
PsiDocumentManager.getInstance(myProject).commitAllDocuments();
final FormSourceCodeGenerator generator = new FormSourceCodeGenerator(myProject);
generator.generate(myFile);
final ArrayList<FormErrorInfo> errors = generator.getErrors();
if (!errors.isEmpty()) {
StringBuilder builder = new StringBuilder();
builder.append("Unable to update sources for form '").append(myFile.getPath()).append("' due to errors:\n");
errors.forEach(error -> {
builder.append(error.getComponentId()).append(" ").append(error.getErrorMessage()).append("\n");
});
LOG.info(builder.toString());
BuildOutputService buildOutput = new BuildOutputService(myProject, "UI Form Compilation"); //NON-NLS
Object sessionId = new Object();
long stamp = System.currentTimeMillis();
buildOutput.onStart(sessionId, stamp, null, null);
for (FormErrorInfo error : errors) {
FormElementNavigatable navigatable = new FormElementNavigatable(myProject, myFile, error.getComponentId());
buildOutput.addMessage(
sessionId,
new CompilerMessageImpl(
myProject,
CompilerMessageCategory.ERROR,
myFile.getPresentableUrl() + ": " + error.getErrorMessage(), //NON-NLS
myFile,
-1, -1,
navigatable
));
}
buildOutput.onEnd(sessionId, ExitStatus.ERRORS, stamp + 1);
}
} catch (Throwable e) {
LOG.error(e);
}
}
}
finally {
myInsideChange = false;

View File

@@ -65,7 +65,9 @@ public final class Form2SourceCompiler implements SourceInstrumentingCompiler{
@Override
public ProcessingItem @NotNull [] getProcessingItems(final @NotNull CompileContext context) {
final Project project = context.getProject();
if (GuiDesignerConfiguration.getInstance(project).INSTRUMENT_CLASSES) {
GuiDesignerConfiguration designerConfiguration = GuiDesignerConfiguration.getInstance(project);
if (designerConfiguration.INSTRUMENT_CLASSES || designerConfiguration.GENERATE_SOURCES_ON_SAVE) {
return ProcessingItem.EMPTY_ARRAY;
}