Code style schemes panel refactoring, iteration #1 (see IDEA-125965)

This commit is contained in:
Rustam Vishnyakov
2016-12-01 17:33:39 +03:00
parent 6084ded972
commit 4203dadabf
11 changed files with 600 additions and 691 deletions
@@ -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<T extends Scheme> {
private final Collection<String> mySchemeImportersNames;
private final Collection<String> mySchemeExporterNames;
protected DefaultSchemeActions() {
mySchemeImportersNames = getSchemeImportersNames();
mySchemeExporterNames = getSchemeExporterNames();
}
private Collection<String> getSchemeImportersNames() {
List<String> importersNames = new ArrayList<>();
for (SchemeImporterEP importerEP : SchemeImporterEP.getExtensions(getSchemeType())) {
importersNames.add(importerEP.name);
}
return importersNames;
}
private Collection<String> getSchemeExporterNames() {
List<String> exporterNames = new ArrayList<>();
for (SchemeExporterEP exporterEP : SchemeExporterEP.getExtensions(getSchemeType())) {
exporterNames.add(exporterEP.name);
}
return exporterNames;
}
public final Collection<AnAction> getActions() {
List<AnAction> 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<AnAction> 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<ImportAction> 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<ExportAction> 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<T> getSchemeType();
}
@@ -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<String>(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<String> names = new ArrayList<>();
Collection<SchemeExporterEP<CodeStyleScheme>> extensions = SchemeExporterEP.getExtensions(CodeStyleScheme.class);
for (SchemeExporterEP<CodeStyleScheme> extension : extensions) {
names.add(extension.name);
}
return ArrayUtil.toStringArray(names);
}
private void exportSchemeUsing(@NotNull String exporterName) {
SchemeExporter<CodeStyleScheme> 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);
}
}
@@ -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<CodeStyleScheme> {
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<AnAction> 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<String> 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<CodeStyleScheme>(getParentComponent()) {
@Override
protected void onSchemeSelected(CodeStyleScheme scheme) {
if (scheme != null) {
mySchemesModel.addScheme(scheme, true);
}
}
}.show(mySchemesModel.getSchemes());
}
else {
final SchemeImporter<CodeStyleScheme> 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<CodeStyleScheme> 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<CodeStyleScheme> {
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<CodeStyleScheme> 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<CodeStyleScheme> 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);
}
}
}
}
@@ -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.application.options.codeStyle.CodeStyleSchemesPanel">
<grid id="27dc6" binding="myPanel" layout-manager="GridLayoutManager" row-count="2" column-count="6" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="myPanel" layout-manager="GridLayoutManager" row-count="2" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="5" right="0"/>
<constraints>
<xy x="20" y="20" width="566" height="113"/>
@@ -10,12 +10,12 @@
<children>
<vspacer id="d9e80">
<constraints>
<grid row="1" column="0" row-span="1" col-span="6" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
<grid row="1" column="0" row-span="1" col-span="3" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<hspacer id="fd090">
<constraints>
<grid row="0" column="5" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="a1636" class="javax.swing.JLabel">
@@ -26,30 +26,32 @@
<text resource-bundle="messages/ApplicationBundle" key="editbox.scheme.name"/>
</properties>
</component>
<component id="c234b" class="javax.swing.JComboBox" binding="myCombo">
<grid id="b828" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="100" height="-1"/>
</grid>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="dc89" class="javax.swing.JButton" binding="myManageButton">
<constraints>
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="&amp;More..."/>
</properties>
</component>
<component id="3cef4" class="javax.swing.JButton" binding="myResetButton">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Reset"/>
</properties>
</component>
<border type="none"/>
<children>
<component id="c234b" class="javax.swing.JComboBox" binding="myCombo">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="100" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="98c4e" class="javax.swing.JButton" binding="myManageButton" custom-create="true">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Manage"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
<buttonGroups>
@@ -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();
}
}
@@ -1,93 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.intellij.application.options.codeStyle.ManageCodeStyleSchemesDialog">
<grid id="27dc6" binding="myContentPane" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<scrollpane id="fb787" class="com.intellij.ui.components.JBScrollPane">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="200" height="200"/>
</grid>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="578bf" class="com.intellij.ui.table.JBTable" binding="mySchemesTable" custom-create="true">
<constraints/>
<properties>
<striped value="true"/>
</properties>
</component>
</children>
</scrollpane>
<grid id="8885" layout-manager="GridLayoutManager" row-count="7" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="27e35" class="javax.swing.JButton" binding="myDeleteButton">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="&amp;Delete"/>
</properties>
</component>
<vspacer id="e34fc">
<constraints>
<grid row="6" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="2095b" class="javax.swing.JButton" binding="mySaveAsButton">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="&amp;Save As..."/>
</properties>
</component>
<component id="dbfe3" class="javax.swing.JButton" binding="myCopyToProjectButton" default-binding="true">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Copy to &amp;Project"/>
</properties>
</component>
<component id="9a9cc" class="javax.swing.JButton" binding="myCloseButton">
<constraints>
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="&amp;Close"/>
</properties>
</component>
<component id="2e22f" class="javax.swing.JButton" binding="myExportButton" default-binding="true">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="E&amp;xport..."/>
</properties>
</component>
<component id="816ef" class="javax.swing.JButton" binding="myImportButton" default-binding="true">
<constraints>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Import..."/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</form>
@@ -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<CodeStyleScheme> importSourceChooserDialog =
new ImportSourceChooserDialog<>(myContentPane, CodeStyleScheme.class);
if (importSourceChooserDialog.showAndGet()) {
if (importSourceChooserDialog.isImportFromSharedSelected()) {
new SchemesToImportPopup<CodeStyleScheme>(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<CodeStyleScheme> 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<CodeStyleScheme> 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<CodeStyleScheme> {
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<CodeStyleScheme> 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<String> 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();
}
}
@@ -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<? super CodeStyleSchemeImpl> myDataHolder;
private String myParentSchemeName;
@@ -37,7 +39,7 @@ public class CodeStyleSchemeImpl extends ExternalizableSchemeAdapter implements
CodeStyleSchemeImpl(@NotNull String name, String parentSchemeName, @NotNull SchemeDataHolder<? super CodeStyleSchemeImpl> dataHolder) {
setName(name);
myDataHolder = dataHolder;
myIsDefault = false;
myIsDefault = DEFAULT_SCHEME_NAME.equals(name);
myParentSchemeName = parentSchemeName;
}
@@ -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;
@@ -39,7 +39,7 @@ import org.jetbrains.annotations.Nullable;
additionalExportFile = CodeStyleSchemesImpl.CODE_STYLES_DIR_PATH
)
class PersistableCodeStyleSchemes extends CodeStyleSchemesImpl implements PersistentStateComponent<Element> {
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);
@@ -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<AnAction> 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);
}
}