diff --git a/platform/lang-impl/src/com/intellij/application/options/DefaultSchemeActions.java b/platform/lang-impl/src/com/intellij/application/options/DefaultSchemeActions.java new file mode 100644 index 000000000000..4a283b93aece --- /dev/null +++ b/platform/lang-impl/src/com/intellij/application/options/DefaultSchemeActions.java @@ -0,0 +1,200 @@ +/* + * Copyright 2000-2016 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. + */ +package com.intellij.application.options; + +import com.intellij.openapi.actionSystem.ActionGroup; +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.Presentation; +import com.intellij.openapi.options.Scheme; +import com.intellij.openapi.options.SchemeExporterEP; +import com.intellij.openapi.options.SchemeImporterEP; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +public abstract class DefaultSchemeActions { + private final Collection mySchemeImportersNames; + private final Collection mySchemeExporterNames; + + protected DefaultSchemeActions() { + mySchemeImportersNames = getSchemeImportersNames(); + mySchemeExporterNames = getSchemeExporterNames(); + } + + + private Collection getSchemeImportersNames() { + List importersNames = new ArrayList<>(); + for (SchemeImporterEP importerEP : SchemeImporterEP.getExtensions(getSchemeType())) { + importersNames.add(importerEP.name); + } + return importersNames; + } + + private Collection getSchemeExporterNames() { + List exporterNames = new ArrayList<>(); + for (SchemeExporterEP exporterEP : SchemeExporterEP.getExtensions(getSchemeType())) { + exporterNames.add(exporterEP.name); + } + return exporterNames; + } + + public final Collection getActions() { + List actions = new ArrayList<>(); + actions.add(new SaveAsAction()); + actions.add(new DeleteAction()); + actions.add(new ResetAction()); + if (!mySchemeExporterNames.isEmpty()) { + actions.add(new ExportGroup()); + } + if (!mySchemeImportersNames.isEmpty()) { + actions.add(new ImportGroup()); + } + addAdditionalActions(actions); + return actions; + } + + protected void addAdditionalActions(@NotNull List defaultActions) {} + + private class ResetAction extends AnAction { + + public ResetAction() { + super("Reset"); + } + + @Override + public void actionPerformed(AnActionEvent e) { + doReset(); + } + } + + + private class SaveAsAction extends AnAction { + public SaveAsAction() { + super("Save As"); + } + + @Override + public void actionPerformed(AnActionEvent e) { + doSaveAs(); + } + } + + private class DeleteAction extends AnAction { + public DeleteAction() { + super("Delete"); + } + + @Override + public void actionPerformed(AnActionEvent e) { + doDelete(); + } + + @Override + public void update(AnActionEvent e) { + Presentation p = e.getPresentation(); + T currentScheme = getCurrentScheme(); + p.setEnabled(currentScheme != null && isDeleteAvailable(currentScheme)); + } + } + + private class ImportGroup extends ActionGroup { + + public ImportGroup() { + super("Import From", true); + } + + @NotNull + @Override + public AnAction[] getChildren(@Nullable AnActionEvent e) { + List importActions = new ArrayList<>(); + for (String importerName : mySchemeImportersNames) { + importActions.add(new ImportAction(importerName)); + } + return importActions.toArray(new AnAction[importActions.size()]); + } + } + + private class ImportAction extends AnAction { + + private String myImporterName; + + public ImportAction(@NotNull String importerName) { + super(importerName); + myImporterName = importerName; + } + + @Override + public void actionPerformed(AnActionEvent e) { + doImport(myImporterName); + } + } + + private class ExportGroup extends ActionGroup { + public ExportGroup() { + super("Export To", true); + } + + @NotNull + @Override + public AnAction[] getChildren(@Nullable AnActionEvent e) { + List exportActions = new ArrayList<>(); + for (String exporterName : mySchemeExporterNames) { + exportActions.add(new ExportAction(exporterName)); + } + return exportActions.toArray(new AnAction[exportActions.size()]); + } + } + + private class ExportAction extends AnAction { + private String myExporterName; + + public ExportAction(@NotNull String exporterName) { + super(exporterName); + myExporterName = exporterName; + } + + @Override + public void actionPerformed(AnActionEvent e) { + T currentScheme = getCurrentScheme(); + if (currentScheme != null) { + doExport(currentScheme, myExporterName); + } + } + } + + protected abstract void doImport(@NotNull String importerName); + + protected abstract void doReset(); + + protected abstract void doSaveAs(); + + protected abstract void doDelete(); + + protected abstract boolean isDeleteAvailable(@NotNull T scheme); + + protected abstract void doExport(@NotNull T scheme, @NotNull String exporterName); + + @Nullable + protected abstract T getCurrentScheme(); + + protected abstract Class getSchemeType(); + +} + diff --git a/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemeExporterUI.java b/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemeExporterUI.java deleted file mode 100644 index cc08b6a64e5c..000000000000 --- a/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemeExporterUI.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Copyright 2000-2015 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. - */ -package com.intellij.application.options.codeStyle; - -import com.intellij.openapi.application.ApplicationBundle; -import com.intellij.openapi.application.WriteAction; -import com.intellij.openapi.fileChooser.FileChooserFactory; -import com.intellij.openapi.fileChooser.FileSaverDescriptor; -import com.intellij.openapi.fileChooser.FileSaverDialog; -import com.intellij.openapi.options.SchemeExporter; -import com.intellij.openapi.options.SchemeExporterEP; -import com.intellij.openapi.ui.MessageType; -import com.intellij.openapi.ui.popup.JBPopupFactory; -import com.intellij.openapi.ui.popup.ListPopup; -import com.intellij.openapi.ui.popup.PopupStep; -import com.intellij.openapi.ui.popup.util.BaseListPopupStep; -import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.openapi.vfs.VirtualFileWrapper; -import com.intellij.psi.codeStyle.CodeStyleScheme; -import com.intellij.util.ArrayUtil; -import org.jetbrains.annotations.NotNull; - -import java.awt.*; -import java.io.OutputStream; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - - -/** - * @author Rustam Vishnyakov - */ -class CodeStyleSchemeExporterUI { - @NotNull private final Component myParentComponent; - @NotNull private final CodeStyleScheme myScheme; - @NotNull private final StatusCallback myStatusCallback; - - CodeStyleSchemeExporterUI(@NotNull Component parentComponent, @NotNull CodeStyleScheme scheme, @NotNull StatusCallback statusCallback) { - myParentComponent = parentComponent; - myScheme = scheme; - myStatusCallback = statusCallback; - } - - void export() { - ListPopup popup = JBPopupFactory.getInstance().createListPopup( - new BaseListPopupStep(ApplicationBundle.message("scheme.exporter.ui.export.as.title"), enumExporters()) { - @Override - public PopupStep onChosen(final String selectedValue, boolean finalChoice) { - return doFinalStep(() -> exportSchemeUsing(selectedValue)); - } - }); - popup.showInCenterOf(myParentComponent); - } - - private static String[] enumExporters() { - List names = new ArrayList<>(); - Collection> extensions = SchemeExporterEP.getExtensions(CodeStyleScheme.class); - for (SchemeExporterEP extension : extensions) { - names.add(extension.name); - } - return ArrayUtil.toStringArray(names); - } - - private void exportSchemeUsing(@NotNull String exporterName) { - SchemeExporter exporter = SchemeExporterEP.getExporter(exporterName, CodeStyleScheme.class); - if (exporter != null) { - String ext = exporter.getExtension(); - FileSaverDialog saver = - FileChooserFactory.getInstance() - .createSaveFileDialog(new FileSaverDescriptor( - ApplicationBundle.message("scheme.exporter.ui.file.chooser.title"), - ApplicationBundle.message("scheme.exporter.ui.file.chooser.message"), - ext), myParentComponent); - VirtualFileWrapper target = saver.save(null, getFileNameSuggestion() + "." + ext); - if (target != null) { - VirtualFile targetFile = target.getVirtualFile(true); - String message; - MessageType messageType; - if (targetFile != null) { - try { - WriteAction.run(() -> { - OutputStream outputStream = targetFile.getOutputStream(this); - try { - exporter.exportScheme(myScheme, outputStream); - } - finally { - outputStream.close(); - } - }); - message = ApplicationBundle - .message("scheme.exporter.ui.code.style.exported.message", myScheme.getName(), targetFile.getPresentableUrl()); - messageType = MessageType.INFO; - } - catch (Exception e) { - message = ApplicationBundle.message("scheme.exporter.ui.export.failed", e.getMessage()); - messageType = MessageType.ERROR; - } - } - else { - message = ApplicationBundle.message("scheme.exporter.ui.cannot.write.message"); - messageType = MessageType.ERROR; - } - myStatusCallback.showMessage(message, messageType); - } - } - } - - private String getFileNameSuggestion() { - return myScheme.getName(); - } - - interface StatusCallback { - void showMessage(@NotNull String message, @NotNull MessageType messageType); - } -} diff --git a/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesActions.java b/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesActions.java new file mode 100644 index 000000000000..e5bc7a3ecd22 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesActions.java @@ -0,0 +1,289 @@ +/* + * Copyright 2000-2016 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. + */ +package com.intellij.application.options.codeStyle; + +import com.intellij.application.options.DefaultSchemeActions; +import com.intellij.application.options.SaveSchemeDialog; +import com.intellij.application.options.SchemesToImportPopup; +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.Presentation; +import com.intellij.openapi.application.ApplicationBundle; +import com.intellij.openapi.application.WriteAction; +import com.intellij.openapi.fileChooser.FileChooserFactory; +import com.intellij.openapi.fileChooser.FileSaverDescriptor; +import com.intellij.openapi.fileChooser.FileSaverDialog; +import com.intellij.openapi.options.*; +import com.intellij.openapi.ui.MessageType; +import com.intellij.openapi.ui.Messages; +import com.intellij.openapi.util.text.StringUtil; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.openapi.vfs.VirtualFileWrapper; +import com.intellij.psi.codeStyle.CodeStyleScheme; +import com.intellij.psi.impl.source.codeStyle.CodeStyleSchemesImpl; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import javax.swing.*; +import java.io.OutputStream; +import java.util.Collection; +import java.util.List; + +abstract class CodeStyleSchemesActions extends DefaultSchemeActions { + private final CodeStyleSchemesModel mySchemesModel; + + private final static String SHARED_IMPORT_SOURCE = ApplicationBundle.message("import.scheme.shared"); + + CodeStyleSchemesActions(@NotNull CodeStyleSchemesModel model) { + mySchemesModel = model; + } + + @Override + protected void addAdditionalActions(@NotNull List defaultActions) { + defaultActions.add(1, new CopyToProjectAction()); + } + + private class CopyToProjectAction extends AnAction { + + public CopyToProjectAction() { + super("Copy To Project"); + } + + @Override + public void actionPerformed(AnActionEvent e) { + CodeStyleScheme currentScheme = getCurrentScheme(); + if (currentScheme != null && !mySchemesModel.isProjectScheme(currentScheme)) { + copyToProject(currentScheme); + } + } + + @Override + public void update(AnActionEvent e) { + Presentation p = e.getPresentation(); + CodeStyleScheme currentScheme = getCurrentScheme(); + p.setEnabled(currentScheme != null && !mySchemesModel.isProjectScheme(currentScheme)); + } + } + + @Override + protected void doReset() { + CodeStyleScheme selectedScheme = getCurrentScheme(); + if (selectedScheme != null) { + if (Messages + .showOkCancelDialog(ApplicationBundle.message("settings.code.style.reset.to.defaults.message"), + ApplicationBundle.message("settings.code.style.reset.to.defaults.title"), Messages.getQuestionIcon()) == + Messages.OK) { + selectedScheme.resetToDefaults(); + mySchemesModel.fireSchemeChanged(selectedScheme); + } + } + } + + @Override + protected void doSaveAs() { + CodeStyleScheme scheme = getCurrentScheme(); + if (mySchemesModel.isProjectScheme(scheme)) { + exportProjectScheme(); + } + else { + CodeStyleScheme currentScheme = getCurrentScheme(); + if (currentScheme != null) { + String selectedName = currentScheme.getName(); + Collection names = CodeStyleSchemesImpl.getSchemeManager().getAllSchemeNames(); + SaveSchemeDialog saveDialog = + new SaveSchemeDialog(getParentComponent(), ApplicationBundle.message("title.save.code.style.scheme.as"), names, selectedName); + if (saveDialog.showAndGet()) { + CodeStyleScheme newScheme = mySchemesModel.createNewScheme(saveDialog.getSchemeName(), getCurrentScheme()); + mySchemesModel.addScheme(newScheme, true); + } + } + } + } + + @Override + protected void doDelete() { + mySchemesModel.removeScheme(getCurrentScheme()); + } + + @Override + protected boolean isDeleteAvailable(@NotNull CodeStyleScheme scheme) { + return !mySchemesModel.isProjectScheme(scheme) && !scheme.isDefault(); + } + + @Override + protected void doImport(@NotNull String importerName) { + CodeStyleScheme currentScheme = getCurrentScheme(); + if (currentScheme != null) { + chooseAndImport(currentScheme, importerName); + } + } + + private void exportProjectScheme() { + String name = Messages.showInputDialog("Enter new scheme name:", "Copy Project Scheme to Global List", Messages.getQuestionIcon()); + if (name != null && !CodeStyleSchemesModel.PROJECT_SCHEME_NAME.equals(name)) { + CodeStyleScheme newScheme = mySchemesModel.exportProjectScheme(name); + int switchToGlobal = Messages + .showYesNoDialog("Project scheme was copied to global scheme list as '" + newScheme.getName() + "'.\n" + + "Switch to this created scheme?", + "Copy Project Scheme to Global List", Messages.getQuestionIcon()); + if (switchToGlobal == Messages.YES) { + mySchemesModel.setUsePerProjectSettings(false); + mySchemesModel.selectScheme(newScheme, null); + } + } + } + + @NotNull + protected abstract JComponent getParentComponent(); + + private void chooseAndImport(@NotNull CodeStyleScheme currentScheme, @NotNull String importerName) { + if (importerName.equals(SHARED_IMPORT_SOURCE)) { + new SchemesToImportPopup(getParentComponent()) { + @Override + protected void onSchemeSelected(CodeStyleScheme scheme) { + if (scheme != null) { + mySchemesModel.addScheme(scheme, true); + } + } + }.show(mySchemesModel.getSchemes()); + } + else { + final SchemeImporter importer = SchemeImporterEP.getImporter(importerName, CodeStyleScheme.class); + if (importer == null) return; + try { + final CodeStyleScheme scheme = importExternalCodeStyle(importer, currentScheme); + if (scheme != null) { + final String additionalImportInfo = StringUtil.notNullize(importer.getAdditionalImportInfo(scheme)); + SchemeImportUtil + .showStatus(getParentComponent(), + ApplicationBundle.message("message.code.style.scheme.import.success", importerName, scheme.getName(), + additionalImportInfo), + MessageType.INFO); + } + } + catch (SchemeImportException e) { + if (e.isWarning()) { + SchemeImportUtil.showStatus(getParentComponent(), e.getMessage(), MessageType.WARNING); + return; + } + final String message = ApplicationBundle.message("message.code.style.scheme.import.failure", importerName, e.getMessage()); + SchemeImportUtil.showStatus(getParentComponent(), message, MessageType.ERROR); + } + } + } + + @Nullable + private CodeStyleScheme importExternalCodeStyle(final SchemeImporter importer, @NotNull CodeStyleScheme currentScheme) + throws SchemeImportException { + final VirtualFile selectedFile = SchemeImportUtil + .selectImportSource(importer.getSourceExtensions(), getParentComponent(), CodeStyleSchemesUIConfiguration.Util.getRecentImportFile()); + if (selectedFile != null) { + CodeStyleSchemesUIConfiguration.Util.setRecentImportFile(selectedFile); + final SchemeCreator schemeCreator = new SchemeCreator(); + final CodeStyleScheme + schemeImported = importer.importScheme(mySchemesModel.getProject(), selectedFile, currentScheme, schemeCreator); + if (schemeImported != null) { + if (schemeCreator.isSchemeWasCreated()) { + mySchemesModel.fireSchemeListChanged(); + } + else { + mySchemesModel.fireSchemeChanged(schemeImported); + } + return schemeImported; + } + } + return null; + } + + 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"); + CodeStyleScheme newScheme = mySchemesModel.createNewScheme(targetName, getCurrentScheme()); + mySchemesModel.addScheme(newScheme, true); + return newScheme; + } + + public boolean isSchemeWasCreated() { + return mySchemeWasCreated; + } + } + + @Override + protected Class getSchemeType() { + return CodeStyleScheme.class; + } + + public void copyToProject(CodeStyleScheme scheme) { + mySchemesModel.copyToProject(scheme); + int switchToProject = Messages + .showYesNoDialog("Scheme '" + scheme.getName() + "' was copied to be used as the project scheme.\n" + + "Switch to this created scheme?", + "Copy Scheme to Project", Messages.getQuestionIcon()); + if (switchToProject == Messages.YES) { + mySchemesModel.setUsePerProjectSettings(true, true); + } + } + + @SuppressWarnings("Duplicates") + @Override + protected void doExport(@NotNull CodeStyleScheme scheme, @NotNull String exporterName) { + SchemeExporter exporter = SchemeExporterEP.getExporter(exporterName, CodeStyleScheme.class); + if (exporter != null) { + String ext = exporter.getExtension(); + FileSaverDialog saver = + FileChooserFactory.getInstance() + .createSaveFileDialog(new FileSaverDescriptor( + ApplicationBundle.message("scheme.exporter.ui.file.chooser.title"), + ApplicationBundle.message("scheme.exporter.ui.file.chooser.message"), + ext), getParentComponent()); + VirtualFileWrapper target = saver.save(null, scheme.getName() + "." + ext); + if (target != null) { + VirtualFile targetFile = target.getVirtualFile(true); + String message; + MessageType messageType; + if (targetFile != null) { + try { + WriteAction.run(() -> { + OutputStream outputStream = targetFile.getOutputStream(this); + try { + exporter.exportScheme(scheme, outputStream); + } + finally { + outputStream.close(); + } + }); + message = ApplicationBundle + .message("scheme.exporter.ui.code.style.exported.message", scheme.getName(), targetFile.getPresentableUrl()); + messageType = MessageType.INFO; + } + catch (Exception e) { + message = ApplicationBundle.message("scheme.exporter.ui.export.failed", e.getMessage()); + messageType = MessageType.ERROR; + } + } + else { + message = ApplicationBundle.message("scheme.exporter.ui.cannot.write.message"); + messageType = MessageType.ERROR; + } + SchemeImportUtil.showStatus(getParentComponent(), message, messageType); + } + } + } +} \ No newline at end of file diff --git a/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesPanel.form b/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesPanel.form index 20d7e48925a7..1f593c745523 100644 --- a/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesPanel.form +++ b/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesPanel.form @@ -1,6 +1,6 @@
- + @@ -10,12 +10,12 @@ - + - + @@ -26,30 +26,32 @@ - + + - - - + - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + diff --git a/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesPanel.java b/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesPanel.java index 78a8f4e4271e..b6a1add548d4 100644 --- a/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesPanel.java +++ b/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesPanel.java @@ -17,11 +17,10 @@ package com.intellij.application.options.codeStyle; -import com.intellij.openapi.application.ApplicationBundle; import com.intellij.openapi.application.ApplicationManager; -import com.intellij.openapi.ui.Messages; import com.intellij.psi.codeStyle.CodeStyleScheme; import com.intellij.ui.ListCellRendererWrapper; +import com.intellij.ui.ManageSchemesComboAction; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -37,8 +36,7 @@ public class CodeStyleSchemesPanel { private final CodeStyleSchemesModel myModel; private JPanel myPanel; - private JButton myManageButton; - private JButton myResetButton; + @SuppressWarnings("unused") private JButton myManageButton; private boolean myIsReset = false; private final Font myDefaultComboFont; @@ -71,19 +69,6 @@ public class CodeStyleSchemesPanel { } }); - myManageButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(@NotNull ActionEvent e) { - showManageSchemesDialog(); - } - }); - - myResetButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - resetCurrentSchemeToDefaults(); - } - }); } private void onCombo() { @@ -129,11 +114,8 @@ public class CodeStyleSchemesPanel { finally { myIsReset = false; } - - } - public void onSelectedSchemeChanged() { myIsReset = true; try { @@ -153,11 +135,6 @@ public class CodeStyleSchemesPanel { return myPanel; } - private void showManageSchemesDialog() { - ManageCodeStyleSchemesDialog manageSchemesDialog = new ManageCodeStyleSchemesDialog(myPanel, myModel); - manageSchemesDialog.show(); - } - public void usePerProjectSettingsOptionChanged() { if (myModel.isProjectScheme(myModel.getSelectedScheme())) { myCombo.setSelectedItem(myModel.getProjectScheme()); @@ -167,15 +144,21 @@ public class CodeStyleSchemesPanel { } } - private void resetCurrentSchemeToDefaults() { - CodeStyleScheme selectedScheme = getSelectedScheme(); - if (selectedScheme != null) { - if (Messages - .showOkCancelDialog(ApplicationBundle.message("settings.code.style.reset.to.defaults.message"), - ApplicationBundle.message("settings.code.style.reset.to.defaults.title"), Messages.getQuestionIcon()) == Messages.OK) { - selectedScheme.resetToDefaults(); - myModel.fireSchemeChanged(selectedScheme); + private void createUIComponents() { + ManageSchemesComboAction manageSchemesComboAction = new ManageSchemesComboAction(new CodeStyleSchemesActions(myModel) { + @NotNull + @Override + protected JComponent getParentComponent() { + return myPanel; } - } + + @Nullable + @Override + protected CodeStyleScheme getCurrentScheme() { + return getSelectedScheme(); + } + }); + myManageButton = manageSchemesComboAction.createCombo(); } + } diff --git a/platform/lang-impl/src/com/intellij/application/options/codeStyle/ManageCodeStyleSchemesDialog.form b/platform/lang-impl/src/com/intellij/application/options/codeStyle/ManageCodeStyleSchemesDialog.form deleted file mode 100644 index 8c11896369d9..000000000000 --- a/platform/lang-impl/src/com/intellij/application/options/codeStyle/ManageCodeStyleSchemesDialog.form +++ /dev/null @@ -1,93 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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 deleted file mode 100644 index 471d70810c2c..000000000000 --- a/platform/lang-impl/src/com/intellij/application/options/codeStyle/ManageCodeStyleSchemesDialog.java +++ /dev/null @@ -1,406 +0,0 @@ -/* - * Copyright 2000-2016 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. - */ -package com.intellij.application.options.codeStyle; - -import com.intellij.application.options.ImportSourceChooserDialog; -import com.intellij.application.options.SaveSchemeDialog; -import com.intellij.application.options.SchemesToImportPopup; -import com.intellij.openapi.application.ApplicationBundle; -import com.intellij.openapi.options.*; -import com.intellij.openapi.ui.DialogWrapper; -import com.intellij.openapi.ui.MessageType; -import com.intellij.openapi.ui.Messages; -import com.intellij.openapi.util.text.StringUtil; -import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.psi.codeStyle.CodeStyleScheme; -import com.intellij.psi.impl.source.codeStyle.CodeStyleSchemesImpl; -import com.intellij.ui.table.JBTable; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import javax.swing.*; -import javax.swing.table.AbstractTableModel; -import javax.swing.table.DefaultTableCellRenderer; -import javax.swing.table.TableCellRenderer; -import java.awt.*; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -/** - * @author: rvishnyakov - */ -public class ManageCodeStyleSchemesDialog extends DialogWrapper { - private JPanel myContentPane; - private JBTable mySchemesTable; - private JButton myDeleteButton; - private JButton mySaveAsButton; - private JButton myCopyToProjectButton; - private JButton myCloseButton; - private JButton myExportButton; - private JButton myImportButton; - private final MySchemesTableModel mySchemesTableModel; - private final CodeStyleSchemesModel myModel; - private final Component myParent; - - protected ManageCodeStyleSchemesDialog(final Component parent, CodeStyleSchemesModel schemesModel) { - super(parent, true); - myParent = parent; - myModel = schemesModel; - setTitle("Code Style Schemes"); - mySchemesTableModel = new MySchemesTableModel(schemesModel); - mySchemesTable.setModel(mySchemesTableModel); - mySchemesTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); - mySchemesTable.getSelectionModel().addListSelectionListener(e -> updateActions()); - setDefaultSelection(); - - - myDeleteButton.addActionListener(e -> deleteSelected()); - mySaveAsButton.addActionListener(e -> onSaveAs()); - myCopyToProjectButton.addActionListener(e -> onCopyToProject()); - myCloseButton.addActionListener(e -> doCancelAction()); - - myExportButton.setVisible(false); - - if (SchemeImporterEP.getExtensions(CodeStyleScheme.class).isEmpty()) { - myImportButton.setVisible(false); - } - else { - myImportButton.setVisible(true); - myImportButton.addActionListener(e -> chooseAndImport()); - } - - if (SchemeExporterEP.getExtensions(CodeStyleScheme.class).isEmpty()) { - myExportButton.setVisible(false); - } - else { - myExportButton.setVisible(true); - myExportButton.addActionListener(e -> exportSelectedScheme()); - } - - init(); - } - - private void chooseAndImport() { - ImportSourceChooserDialog importSourceChooserDialog = - new ImportSourceChooserDialog<>(myContentPane, CodeStyleScheme.class); - if (importSourceChooserDialog.showAndGet()) { - if (importSourceChooserDialog.isImportFromSharedSelected()) { - new SchemesToImportPopup(myContentPane) { - @Override - protected void onSchemeSelected(CodeStyleScheme scheme) { - if (scheme != null) { - myModel.addScheme(scheme, true); - } - } - }.show(myModel.getSchemes()); - } - else { - final String selectedImporterName = importSourceChooserDialog.getSelectedSourceName(); - if (selectedImporterName != null) { - final SchemeImporter importer = SchemeImporterEP.getImporter(selectedImporterName, CodeStyleScheme.class); - if (importer == null) return; - try { - final CodeStyleScheme scheme = importExternalCodeStyle(importer); - if (scheme != null) { - final String additionalImportInfo = StringUtil.notNullize(importer.getAdditionalImportInfo(scheme)); - SchemeImportUtil - .showStatus(myImportButton, - ApplicationBundle.message("message.code.style.scheme.import.success", selectedImporterName, scheme.getName(), additionalImportInfo), - MessageType.INFO); - } - } - catch (SchemeImportException e) { - if (e.isWarning()) { - SchemeImportUtil.showStatus(myImportButton, e.getMessage(), MessageType.WARNING); - return; - } - final String message = ApplicationBundle.message("message.code.style.scheme.import.failure", selectedImporterName, e.getMessage()); - SchemeImportUtil.showStatus(myImportButton, message, MessageType.ERROR); - } - } - } - } - } - - @Nullable - private CodeStyleScheme importExternalCodeStyle(final SchemeImporter importer) throws SchemeImportException { - final VirtualFile selectedFile = SchemeImportUtil - .selectImportSource(importer.getSourceExtensions(), myContentPane, CodeStyleSchemesUIConfiguration.Util.getRecentImportFile()); - if (selectedFile != null) { - CodeStyleSchemesUIConfiguration.Util.setRecentImportFile(selectedFile); - final SchemeCreator schemeCreator = new SchemeCreator(); - final CodeStyleScheme - schemeImported = importer.importScheme(myModel.getProject(), selectedFile, getSelectedScheme(), schemeCreator); - if (schemeImported != null) { - if (schemeCreator.isSchemeWasCreated()) myModel.fireSchemeListChanged(); - else myModel.fireSchemeChanged(schemeImported); - return schemeImported; - } - } - return null; - } - - private void updateActions() { - // 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(); - if (row < 0) row = mySchemesTableModel.getDefaultRow(); - return mySchemesTableModel.getSchemeAt(row); - } - - @NotNull - @Override - protected Action[] createActions() { - return new Action[]{}; - } - - private void deleteSelected() { - int row = mySchemesTable.getSelectedRow(); - if (row >= 0) { - int rowToSelect = row + 1; - if (rowToSelect >= mySchemesTableModel.getRowCount()) { - rowToSelect = mySchemesTableModel.getDefaultRow(); - } - mySchemesTable.getSelectionModel().setSelectionInterval(rowToSelect, rowToSelect); - mySchemesTableModel.deleteAt(row); - } - } - - @Override - protected JComponent createCenterPanel() { - return myContentPane; - } - - private void createUIComponents() { - mySchemesTable = new MySchemesTable(); - } - - 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 { - private final TableCellRenderer myFixedItemsRenderer; - - private MySchemesTable() { - myFixedItemsRenderer = new DefaultTableCellRenderer() { - @NotNull - @Override - public Component getTableCellRendererComponent(@NotNull JTable table, - Object value, - boolean isSelected, - boolean hasFocus, - int row, - int column) { - Component defaultComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); - if (value instanceof CodeStyleScheme) { - CodeStyleScheme scheme = (CodeStyleScheme)value; - if (scheme.isDefault() || myModel.isProjectScheme(scheme)) { - defaultComponent.setFont(defaultComponent.getFont().deriveFont(Font.BOLD)); - } - } - return defaultComponent; - } - }; - } - - @Override - public TableCellRenderer getCellRenderer(int row, int column) { - return myFixedItemsRenderer; - } - } - - private static class MySchemesTableModel extends AbstractTableModel { - private final CodeStyleSchemesModel mySchemesModel; - private final List mySchemes; - - public MySchemesTableModel(CodeStyleSchemesModel schemesModel) { - mySchemesModel = schemesModel; - mySchemes = new ArrayList<>(); - updateSchemes(); - } - - @NotNull - @Override - public String getColumnName(int column) { - assert column == 0; - return "Name"; - } - - @Override - public int getRowCount() { - return mySchemes.size(); - } - - @Override - public int getColumnCount() { - return 1; - } - - @Override - public Object getValueAt(int rowIndex, int columnIndex) { - assert columnIndex == 0; - return mySchemes.get(rowIndex); - } - - public CodeStyleScheme getSchemeAt(int row) { - return mySchemes.get(row); - } - - public void deleteAt(int row) { - CodeStyleScheme scheme = mySchemes.get(row); - mySchemesModel.removeScheme(scheme); - updateSchemes(); - fireTableRowsDeleted(row, row); - } - - public int createNewScheme(CodeStyleScheme selectedScheme, String schemeName) { - CodeStyleScheme newScheme = mySchemesModel.createNewScheme(schemeName, selectedScheme); - mySchemesModel.addScheme(newScheme, true); - updateSchemes(); - int row = 0; - for (CodeStyleScheme scheme : mySchemes) { - if (scheme == newScheme) { - fireTableRowsInserted(row, row); - break; - } - row ++; - } - return row; - } - - public int getDefaultRow() { - int row = 0; - for (CodeStyleScheme scheme : mySchemes) { - if (scheme.isDefault()) return row; - row ++; - } - return 0; - } - - public void copyToProject(CodeStyleScheme scheme) { - mySchemesModel.copyToProject(scheme); - int switchToProject = Messages - .showYesNoDialog("Scheme '" + scheme.getName() + "' was copied to be used as the project scheme.\n" + - "Switch to this created scheme?", - "Copy Scheme to Project", Messages.getQuestionIcon()); - if (switchToProject == Messages.YES) { - mySchemesModel.setUsePerProjectSettings(true, true); - } - } - - public int exportProjectScheme() { - String name = Messages.showInputDialog("Enter new scheme name:", "Copy Project Scheme to Global List", Messages.getQuestionIcon()); - if (name != null && !CodeStyleSchemesModel.PROJECT_SCHEME_NAME.equals(name)) { - CodeStyleScheme newScheme = mySchemesModel.exportProjectScheme(name); - updateSchemes(); - int switchToGlobal = Messages - .showYesNoDialog("Project scheme was copied to global scheme list as '" + newScheme.getName() + "'.\n" + - "Switch to this created scheme?", - "Copy Project Scheme to Global List", Messages.getQuestionIcon()); - if (switchToGlobal == Messages.YES) { - mySchemesModel.setUsePerProjectSettings(false); - mySchemesModel.selectScheme(newScheme, null); - } - int row = 0; - for (CodeStyleScheme scheme : mySchemes) { - if (scheme == newScheme) { - fireTableRowsInserted(row, row); - return switchToGlobal == 0 ? row : -1; - } - row++; - } - } - return -1; - } - - private void updateSchemes() { - mySchemes.clear(); - mySchemes.addAll(mySchemesModel.getAllSortedSchemes()); - } - - - public boolean isProjectScheme(CodeStyleScheme scheme) { - return mySchemesModel.isProjectScheme(scheme); - } - } - - private void onSaveAs() { - if (mySchemesTableModel.isProjectScheme(getSelectedScheme())) { - int rowToSelect = mySchemesTableModel.exportProjectScheme(); - if (rowToSelect > 0) { - mySchemesTable.getSelectionModel().setSelectionInterval(rowToSelect, rowToSelect); - } - } - else { - Collection names = CodeStyleSchemesImpl.getSchemeManager().getAllSchemeNames(); - String selectedName = getSelectedScheme().getName(); - SaveSchemeDialog saveDialog = - new SaveSchemeDialog(myParent, ApplicationBundle.message("title.save.code.style.scheme.as"), names, selectedName); - if (saveDialog.showAndGet()) { - int row = mySchemesTableModel.createNewScheme(getSelectedScheme(), saveDialog.getSchemeName()); - mySchemesTable.getSelectionModel().setSelectionInterval(row, row); - } - } - } - - private void onCopyToProject() { - mySchemesTableModel.copyToProject(getSelectedScheme()); - } - - private void setDefaultSelection() { - CodeStyleScheme selectedScheme = myModel.getSelectedScheme(); - for (int i = 0; i < mySchemesTableModel.getRowCount(); i ++) { - if (mySchemesTableModel.getSchemeAt(i).equals(selectedScheme)) { - mySchemesTable.getSelectionModel().setSelectionInterval(i, i); - return; - } - } - } - - private void exportSelectedScheme() { - new CodeStyleSchemeExporterUI(myExportButton, getSelectedScheme(), - (message, messageType) -> SchemeImportUtil.showStatus(myExportButton, message, messageType)).export(); - } -} diff --git a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleSchemeImpl.java b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleSchemeImpl.java index 412e6bb0603b..ae0c2aa9951f 100644 --- a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleSchemeImpl.java +++ b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleSchemeImpl.java @@ -28,6 +28,8 @@ import org.jetbrains.annotations.Nullable; public class CodeStyleSchemeImpl extends ExternalizableSchemeAdapter implements CodeStyleScheme, SerializableScheme { private static final Logger LOG = Logger.getInstance(CodeStyleSchemeImpl.class); + + static final String DEFAULT_SCHEME_NAME = "Default"; private SchemeDataHolder myDataHolder; private String myParentSchemeName; @@ -37,7 +39,7 @@ public class CodeStyleSchemeImpl extends ExternalizableSchemeAdapter implements CodeStyleSchemeImpl(@NotNull String name, String parentSchemeName, @NotNull SchemeDataHolder dataHolder) { setName(name); myDataHolder = dataHolder; - myIsDefault = false; + myIsDefault = DEFAULT_SCHEME_NAME.equals(name); myParentSchemeName = parentSchemeName; } diff --git a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleSchemesImpl.java b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleSchemesImpl.java index 124b9a3f4bf8..9fac75081dc1 100644 --- a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleSchemesImpl.java +++ b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleSchemesImpl.java @@ -29,7 +29,6 @@ import org.jetbrains.annotations.Nullable; import java.util.function.Function; public abstract class CodeStyleSchemesImpl extends CodeStyleSchemes { - protected static final String DEFAULT_SCHEME_NAME = "Default"; @NonNls static final String CODE_STYLES_DIR_PATH = "codestyles"; @@ -119,9 +118,9 @@ public abstract class CodeStyleSchemesImpl extends CodeStyleSchemes { @Override public CodeStyleScheme getDefaultScheme() { - CodeStyleScheme defaultScheme = mySchemeManager.findSchemeByName(DEFAULT_SCHEME_NAME); + CodeStyleScheme defaultScheme = mySchemeManager.findSchemeByName(CodeStyleSchemeImpl.DEFAULT_SCHEME_NAME); if (defaultScheme == null) { - defaultScheme = new CodeStyleSchemeImpl(DEFAULT_SCHEME_NAME, true, null); + defaultScheme = new CodeStyleSchemeImpl(CodeStyleSchemeImpl.DEFAULT_SCHEME_NAME, true, null); addScheme(defaultScheme); } return defaultScheme; diff --git a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/PersistableCodeStyleSchemes.java b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/PersistableCodeStyleSchemes.java index 6574a24cf651..978049dc9548 100644 --- a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/PersistableCodeStyleSchemes.java +++ b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/PersistableCodeStyleSchemes.java @@ -39,7 +39,7 @@ import org.jetbrains.annotations.Nullable; additionalExportFile = CodeStyleSchemesImpl.CODE_STYLES_DIR_PATH ) class PersistableCodeStyleSchemes extends CodeStyleSchemesImpl implements PersistentStateComponent { - public String CURRENT_SCHEME_NAME = DEFAULT_SCHEME_NAME; + public String CURRENT_SCHEME_NAME = CodeStyleSchemeImpl.DEFAULT_SCHEME_NAME; public PersistableCodeStyleSchemes(@NotNull SchemeManagerFactory schemeManagerFactory) { super(schemeManagerFactory); @@ -54,7 +54,7 @@ class PersistableCodeStyleSchemes extends CodeStyleSchemesImpl implements Persis @Override public boolean accepts(@NotNull Accessor accessor, @NotNull Object bean) { if ("CURRENT_SCHEME_NAME".equals(accessor.getName())) { - return !DEFAULT_SCHEME_NAME.equals(accessor.read(bean)); + return !CodeStyleSchemeImpl.DEFAULT_SCHEME_NAME.equals(accessor.read(bean)); } else { return accessor.getValueClass().equals(String.class); diff --git a/platform/lang-impl/src/com/intellij/ui/ManageSchemesComboAction.java b/platform/lang-impl/src/com/intellij/ui/ManageSchemesComboAction.java new file mode 100644 index 000000000000..2b02d7245274 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/ui/ManageSchemesComboAction.java @@ -0,0 +1,61 @@ +/* + * Copyright 2000-2016 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. + */ +package com.intellij.ui; + +import com.intellij.icons.AllIcons; +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.DefaultActionGroup; +import com.intellij.openapi.actionSystem.Presentation; +import com.intellij.openapi.actionSystem.ex.ComboBoxAction; +import com.intellij.application.options.DefaultSchemeActions; +import org.jetbrains.annotations.NotNull; + +import javax.swing.*; +import java.util.Collection; + +public class ManageSchemesComboAction extends ComboBoxAction { + + private Collection myActions; + + public ManageSchemesComboAction(@NotNull DefaultSchemeActions schemeActions) { + myActions = schemeActions.getActions(); + } + + @NotNull + @Override + protected DefaultActionGroup createPopupActionGroup(JComponent button) { + DefaultActionGroup group = new DefaultActionGroup(); + for(AnAction action : myActions) { + group.add(action); + } + return group; + } + + @Override + public void update(AnActionEvent e) { + for(AnAction action : myActions) { + action.update(e); + } + } + + public JButton createCombo() { + Presentation presentation = getTemplatePresentation(); + presentation.setIcon(AllIcons.General.GearPlain); + return createComboBoxButton(presentation); + } + +}