From 373ceef0d18a0f884bc82c5699444794573f82a0 Mon Sep 17 00:00:00 2001 From: irengrig Date: Mon, 23 Nov 2015 22:17:56 +0100 Subject: [PATCH] WEB-19193 JSCS: Import Code Styles: new scheme is not created and not added to the schemes list correct refresh after schema creation (the bug was there) also fix the case when nothing was selected in a table in manage schemas dialog --- .../options/CodeStyleSchemesConfigurable.java | 2 +- .../codeStyle/CodeStyleSchemesModel.java | 4 +++ .../ManageCodeStyleSchemesDialog.java | 31 ++++++++++++++----- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/application/options/CodeStyleSchemesConfigurable.java b/platform/lang-impl/src/com/intellij/application/options/CodeStyleSchemesConfigurable.java index 4a52e0efca5c..154900f428a8 100644 --- a/platform/lang-impl/src/com/intellij/application/options/CodeStyleSchemesConfigurable.java +++ b/platform/lang-impl/src/com/intellij/application/options/CodeStyleSchemesConfigurable.java @@ -276,7 +276,7 @@ public class CodeStyleSchemesConfigurable extends SearchableConfigurable.Parent. @Override public void schemeChanged(final CodeStyleScheme scheme) { - reset(); + if (scheme == myModel.getSelectedScheme()) myRootSchemesPanel.onSelectedSchemeChanged(); } }); } diff --git a/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesModel.java b/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesModel.java index 4946d644c217..4badd2504855 100644 --- a/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesModel.java +++ b/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesModel.java @@ -189,6 +189,10 @@ public class CodeStyleSchemesModel { myDispatcher.getMulticaster().schemeChanged(scheme); } + public void fireSchemeListChanged() { + myDispatcher.getMulticaster().schemeListChanged(); + } + public CodeStyleScheme getSelectedGlobalScheme() { return myGlobalSelected; } diff --git a/platform/lang-impl/src/com/intellij/application/options/codeStyle/ManageCodeStyleSchemesDialog.java b/platform/lang-impl/src/com/intellij/application/options/codeStyle/ManageCodeStyleSchemesDialog.java index 495a80772124..931d1c813f35 100644 --- a/platform/lang-impl/src/com/intellij/application/options/codeStyle/ManageCodeStyleSchemesDialog.java +++ b/platform/lang-impl/src/com/intellij/application/options/codeStyle/ManageCodeStyleSchemesDialog.java @@ -199,9 +199,10 @@ public class ManageCodeStyleSchemesDialog extends DialogWrapper { CodeStyleSchemesUIConfiguration.Util.setRecentImportFile(selectedFile); final SchemeCreator schemeCreator = new SchemeCreator(); final CodeStyleScheme - schemeImported = importer.importScheme(myModel.getProject(), selectedFile, myModel.getSelectedScheme(), schemeCreator); + schemeImported = importer.importScheme(myModel.getProject(), selectedFile, getSelectedScheme(), schemeCreator); if (schemeImported != null) { - myModel.fireSchemeChanged(schemeImported); + if (schemeCreator.isSchemeWasCreated()) myModel.fireSchemeListChanged(); + else myModel.fireSchemeChanged(schemeImported); return schemeImported; } } @@ -230,15 +231,23 @@ public class ManageCodeStyleSchemesDialog extends DialogWrapper { } private void updateActions() { - CodeStyleScheme selectedScheme = getSelectedScheme(); - myDeleteButton.setEnabled(!(selectedScheme.isDefault() || mySchemesTableModel.isProjectScheme(selectedScheme))); - myCopyToProjectButton.setEnabled(!mySchemesTableModel.isProjectScheme(selectedScheme)); + // there is a possibility that nothing will be selected in a table. So we just need to corresponding disable actions + final CodeStyleScheme selectedScheme = getSelectedInTableScheme(); + myDeleteButton.setEnabled(selectedScheme != null && (!(selectedScheme.isDefault() || mySchemesTableModel.isProjectScheme(selectedScheme)))); + myCopyToProjectButton.setEnabled(selectedScheme != null && !mySchemesTableModel.isProjectScheme(selectedScheme)); + } + + @Nullable + private CodeStyleScheme getSelectedInTableScheme() { + int row = mySchemesTable.getSelectedRow(); + if (row < 0) return null; + return mySchemesTableModel.getSchemeAt(row); } @NotNull private CodeStyleScheme getSelectedScheme() { int row = mySchemesTable.getSelectedRow(); - assert row >= 0; + if (row < 0) row = mySchemesTableModel.getDefaultRow(); return mySchemesTableModel.getSchemeAt(row); } @@ -270,13 +279,21 @@ public class ManageCodeStyleSchemesDialog extends DialogWrapper { } private class SchemeCreator implements SchemeFactory { + private boolean mySchemeWasCreated; + @Override public CodeStyleScheme createNewScheme(@Nullable String targetName) { + mySchemeWasCreated = true; if (targetName == null) targetName = ApplicationBundle.message("code.style.scheme.import.unnamed"); final int row = mySchemesTableModel.createNewScheme(getSelectedScheme(), targetName); + mySchemesTable.getSelectionModel().setSelectionInterval(row, row); return mySchemesTableModel.getSchemeAt(row); } + + public boolean isSchemeWasCreated() { + return mySchemeWasCreated; + } } private class MySchemesTable extends JBTable { @@ -368,7 +385,7 @@ public class ManageCodeStyleSchemesDialog extends DialogWrapper { } return row; } - + public int getDefaultRow() { int row = 0; for (CodeStyleScheme scheme : mySchemes) {