From 43ed935bdfb1b52634a33f1add067394d5d58d7e Mon Sep 17 00:00:00 2001 From: Anton Lobov Date: Mon, 4 Jun 2018 16:41:19 +0200 Subject: [PATCH] WEB-33099 JSON Schema Mappings conflicting mappings for newly added item - don't show conflicts for the newly added item when still editing it - always correctly update the warning - show [Project Directory] for empty folder path --- .../UserDefinedJsonSchemaConfiguration.java | 6 +++++- .../mappings/JsonMappingsTableCellEditor.java | 6 +++--- .../mappings/JsonSchemaConfigurable.java | 16 ++++++++++------ .../mappings/JsonSchemaMappingsConfigurable.java | 15 ++++++++------- .../mappings/JsonSchemaMappingsView.java | 16 ++++++++-------- .../settings/mappings/TreeUpdater.java | 6 ++++++ 6 files changed, 40 insertions(+), 25 deletions(-) create mode 100644 json/src/com/jetbrains/jsonSchema/settings/mappings/TreeUpdater.java diff --git a/json/src/com/jetbrains/jsonSchema/UserDefinedJsonSchemaConfiguration.java b/json/src/com/jetbrains/jsonSchema/UserDefinedJsonSchemaConfiguration.java index f988cd798b5b..5daa70b93ade 100644 --- a/json/src/com/jetbrains/jsonSchema/UserDefinedJsonSchemaConfiguration.java +++ b/json/src/com/jetbrains/jsonSchema/UserDefinedJsonSchemaConfiguration.java @@ -35,6 +35,7 @@ import com.jetbrains.jsonSchema.impl.JsonSchemaVersion; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.io.File; import java.util.Collections; import java.util.Comparator; import java.util.List; @@ -239,7 +240,7 @@ public class UserDefinedJsonSchemaConfiguration { @NotNull private static String normalizePath(String path) { - return path.replace('\\', '/'); + return StringUtil.trimEnd(path.replace('\\', '/').replace('/', File.separatorChar), File.separatorChar); } public String getPath() { @@ -267,6 +268,9 @@ public class UserDefinedJsonSchemaConfiguration { } public String getPresentation() { + if (mappingKind == JsonMappingKind.Directory && StringUtil.isEmpty(path)) { + return mappingKind.getPrefix() + "[Project Directory]"; + } return mappingKind.getPrefix() + path; } diff --git a/json/src/com/jetbrains/jsonSchema/settings/mappings/JsonMappingsTableCellEditor.java b/json/src/com/jetbrains/jsonSchema/settings/mappings/JsonMappingsTableCellEditor.java index 2ba1a2ecd778..1aa640bb0fbc 100644 --- a/json/src/com/jetbrains/jsonSchema/settings/mappings/JsonMappingsTableCellEditor.java +++ b/json/src/com/jetbrains/jsonSchema/settings/mappings/JsonMappingsTableCellEditor.java @@ -33,9 +33,9 @@ class JsonMappingsTableCellEditor extends AbstractTableCellEditor { final JPanel myWrapper; private final UserDefinedJsonSchemaConfiguration.Item myItem; private final Project myProject; - private final Runnable myTreeUpdater; + private final TreeUpdater myTreeUpdater; - public JsonMappingsTableCellEditor(UserDefinedJsonSchemaConfiguration.Item item, Project project, Runnable treeUpdater) { + public JsonMappingsTableCellEditor(UserDefinedJsonSchemaConfiguration.Item item, Project project, TreeUpdater treeUpdater) { myItem = item; myProject = project; myTreeUpdater = treeUpdater; @@ -111,7 +111,7 @@ class JsonMappingsTableCellEditor extends AbstractTableCellEditor { @Override public boolean stopCellEditing() { myItem.setPath(myComponent.getChildComponent().getText()); - myTreeUpdater.run(); + myTreeUpdater.updateTree(true); return super.stopCellEditing(); } diff --git a/json/src/com/jetbrains/jsonSchema/settings/mappings/JsonSchemaConfigurable.java b/json/src/com/jetbrains/jsonSchema/settings/mappings/JsonSchemaConfigurable.java index abc6f35b3cff..66489c1d788e 100644 --- a/json/src/com/jetbrains/jsonSchema/settings/mappings/JsonSchemaConfigurable.java +++ b/json/src/com/jetbrains/jsonSchema/settings/mappings/JsonSchemaConfigurable.java @@ -32,7 +32,7 @@ public class JsonSchemaConfigurable extends NamedConfigurable myNameCreator; private JsonSchemaMappingsView myView; private String myDisplayName; @@ -40,9 +40,13 @@ public class JsonSchemaConfigurable extends NamedConfigurable nameCreator) { - super(true, updateTree); + super(true, () -> { + if (updateTree != null) { + updateTree.updateTree(true); + } + }); myProject = project; mySchemaFilePath = schemaFilePath; mySchema = schema; @@ -88,7 +92,7 @@ public class JsonSchemaConfigurable extends NamedConfigurable { + private final TreeUpdater myTreeUpdater = showWarning -> { TREE_UPDATER.run(); - updateWarningText(); + updateWarningText(showWarning); }; private final Function myNameCreator = s -> createUniqueName(s); @@ -127,7 +127,7 @@ public class JsonSchemaMappingsConfigurable extends MasterDetailsComponent imple private void addCreatedMappings(@NotNull final UserDefinedJsonSchemaConfiguration info) { final JsonSchemaConfigurable configurable = new JsonSchemaConfigurable(myProject, "", info, myTreeUpdater, myNameCreator); - configurable.setError(myError); + configurable.setError(myError, true); final MyNode node = new MyNode(configurable); addNode(node, myRoot); selectNodeInTree(node, true); @@ -144,7 +144,7 @@ public class JsonSchemaMappingsConfigurable extends MasterDetailsComponent imple final JsonSchemaConfigurable configurable = new JsonSchemaConfigurable(myProject, isHttpPath(pathToSchema) || new File(pathToSchema).isAbsolute() ? pathToSchema : new File(myProject.getBasePath(), pathToSchema).getPath(), info, myTreeUpdater, myNameCreator); - configurable.setError(myError); + configurable.setError(myError, true); myRoot.add(new MyNode(configurable)); } ((DefaultTreeModel) myTree.getModel()).reload(myRoot); @@ -209,7 +209,7 @@ public class JsonSchemaMappingsConfigurable extends MasterDetailsComponent imple return !storedList.equals(uiList); } - private void updateWarningText() { + private void updateWarningText(boolean showWarning) { final MultiMap patternsMap = new MultiMap<>(); final StringBuilder sb = new StringBuilder(); final List list; @@ -221,6 +221,7 @@ public class JsonSchemaMappingsConfigurable extends MasterDetailsComponent imple return; } for (UserDefinedJsonSchemaConfiguration info : list) { + info.refreshPatterns(); final JsonSchemaPatternComparator comparator = new JsonSchemaPatternComparator(myProject); final List patterns = info.getPatterns(); for (UserDefinedJsonSchemaConfiguration.Item pattern : patterns) { @@ -247,7 +248,7 @@ public class JsonSchemaMappingsConfigurable extends MasterDetailsComponent imple while (children.hasMoreElements()) { Object o = children.nextElement(); if (o instanceof MyNode && ((MyNode)o).getConfigurable() instanceof JsonSchemaConfigurable) { - ((JsonSchemaConfigurable) ((MyNode)o).getConfigurable()).setError(myError); + ((JsonSchemaConfigurable) ((MyNode)o).getConfigurable()).setError(myError, showWarning); } } } @@ -284,7 +285,7 @@ public class JsonSchemaMappingsConfigurable extends MasterDetailsComponent imple @Override public void reset() { fillTree(); - updateWarningText(); + updateWarningText(true); } @Override diff --git a/json/src/com/jetbrains/jsonSchema/settings/mappings/JsonSchemaMappingsView.java b/json/src/com/jetbrains/jsonSchema/settings/mappings/JsonSchemaMappingsView.java index 75d4cf293f7c..8a0a4f47bc90 100644 --- a/json/src/com/jetbrains/jsonSchema/settings/mappings/JsonSchemaMappingsView.java +++ b/json/src/com/jetbrains/jsonSchema/settings/mappings/JsonSchemaMappingsView.java @@ -58,7 +58,7 @@ public class JsonSchemaMappingsView implements Disposable { private static final String ADD_SCHEMA_MAPPING = "settings.json.schema.add.mapping"; private static final String EDIT_SCHEMA_MAPPING = "settings.json.schema.edit.mapping"; private static final String REMOVE_SCHEMA_MAPPING = "settings.json.schema.remove.mapping"; - private final Runnable myTreeUpdater; + private final TreeUpdater myTreeUpdater; private final Consumer mySchemaPathChangedCallback; private TableView myTableView; private JComponent myComponent; @@ -71,7 +71,7 @@ public class JsonSchemaMappingsView implements Disposable { private boolean myInitialized; public JsonSchemaMappingsView(Project project, - Runnable treeUpdater, + TreeUpdater treeUpdater, Consumer schemaPathChangedCallback) { myTreeUpdater = treeUpdater; mySchemaPathChangedCallback = schemaPathChangedCallback; @@ -134,7 +134,7 @@ public class JsonSchemaMappingsView implements Disposable { JPanel panel = decorator.createPanel(); panel.setBorder(BorderFactory.createCompoundBorder(JBUI.Borders.empty(0, 8), panel.getBorder())); builder.addComponentFillVertically(panel, 5); - JLabel commentComponent = ComponentPanelBuilder.createCommentComponent("Path to file, directory or file name pattern *.config.json", false); + JLabel commentComponent = ComponentPanelBuilder.createCommentComponent("Path to file or directory relative to project root, or file name pattern like *.config.json", false); commentComponent.setBorder(JBUI.Borders.empty(0, 8, 5, 0)); builder.addComponent(commentComponent); @@ -145,10 +145,10 @@ public class JsonSchemaMappingsView implements Disposable { public void dispose() { } - public void setError(final String text) { + public void setError(final String text, boolean showWarning) { myErrorText = text; - myError.setVisible(text != null); - myErrorIcon.setVisible(text != null); + myError.setVisible(showWarning && text != null); + myErrorIcon.setVisible(showWarning && text != null); } private void attachNavigateToSchema() { @@ -281,7 +281,7 @@ public class JsonSchemaMappingsView implements Disposable { myTableView.getListTableModel().addRow(currentItem); myTableView.editCellAt(myTableView.getListTableModel().getRowCount() - 1, 0); - myTreeUpdater.run(); + myTreeUpdater.updateTree(false); } } @@ -313,7 +313,7 @@ public class JsonSchemaMappingsView implements Disposable { ++cnt; } myTableView.getListTableModel().fireTableDataChanged(); - myTreeUpdater.run(); + myTreeUpdater.updateTree(true); } } } diff --git a/json/src/com/jetbrains/jsonSchema/settings/mappings/TreeUpdater.java b/json/src/com/jetbrains/jsonSchema/settings/mappings/TreeUpdater.java new file mode 100644 index 000000000000..364f730edf50 --- /dev/null +++ b/json/src/com/jetbrains/jsonSchema/settings/mappings/TreeUpdater.java @@ -0,0 +1,6 @@ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.jetbrains.jsonSchema.settings.mappings; + +public interface TreeUpdater { + void updateTree(boolean showWarning); +}