IDEA-42567 Settings / File encoding / Default encoding for properties files combobox has GUI defects

This commit is contained in:
Alexey Kudravtsev
2012-12-10 16:23:05 +04:00
parent 22144941a9
commit 8d5426dc31
5 changed files with 83 additions and 80 deletions
@@ -199,7 +199,7 @@ public class LossyEncodingInspection extends LocalInspectionTool {
}
}
};
DefaultActionGroup group = action.createGroup(false);
DefaultActionGroup group = action.createGroup(null);
DataContext dataContext = DataManager.getInstance().getDataContext();
JBPopupFactory.getInstance().createActionGroupPopup(null, group, dataContext, false, false, false, null, 30, null).showInBestPositionFor(dataContext);
}
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.intellij.openapi.vfs.encoding.FileEncodingConfigurable">
<grid id="27dc6" binding="myPanel" layout-manager="GridLayoutManager" row-count="5" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="myPanel" layout-manager="GridLayoutManager" row-count="4" 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>
<xy x="20" y="20" width="1037" height="400"/>
<xy x="20" y="86" width="454" height="334"/>
</constraints>
<properties/>
<clientProperties>
@@ -11,14 +11,6 @@
</clientProperties>
<border type="none"/>
<children>
<component id="e9af6" class="javax.swing.JLabel" binding="myTitleLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="messages/IdeBundle" key="encodings.dialog.caption"/>
</properties>
</component>
<scrollpane id="fe0c3" class="com.intellij.ui.components.JBScrollPane" binding="myTreePanel" custom-create="true">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
@@ -29,21 +21,16 @@
</border>
<children/>
</scrollpane>
<component id="29b05" class="javax.swing.JCheckBox" binding="myAutodetectUTFEncodedFilesCheckBox" default-binding="true">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="messages/IdeBundle" key="checkbox.autodetect.utf"/>
</properties>
</component>
<grid id="7187a" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="7187a" layout-manager="GridLayoutManager" row-count="1" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<clientProperties>
<BorderFactoryClass class="java.lang.String" value="com.intellij.ui.IdeBorderFactory$PlainSmallWithIndent"/>
</clientProperties>
<border type="etched" title="Properties Files (*.properties)"/>
<children>
<grid id="9ed8f" binding="myPropertiesFilesEncodingCombo" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
@@ -63,7 +50,7 @@
</component>
<component id="9d083" class="javax.swing.JCheckBox" binding="myTransparentNativeToAsciiCheckBox" default-binding="true">
<constraints>
<grid row="1" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="4" fill="0" indent="0" use-parent-layout="false"/>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="4" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="messages/IdeBundle" key="checkbox.transparent.native.to.ascii.conversion"/>
@@ -71,20 +58,14 @@
</component>
</children>
</grid>
<grid id="f15cb" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="f15cb" layout-manager="GridLayoutManager" row-count="1" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<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"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="a570a" class="javax.swing.JComboBox" binding="myIdeEncodingsCombo">
<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"/>
</constraints>
<properties/>
</component>
<component id="afe27" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
@@ -93,13 +74,33 @@
<text value="IDE Encoding:"/>
</properties>
</component>
<grid id="1c91d" binding="myIdeEncodingsListCombo" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
<component id="29b05" class="javax.swing.JCheckBox" binding="myAutodetectUTFEncodedFilesCheckBox" default-binding="true">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<horizontalAlignment value="4"/>
<text resource-bundle="messages/IdeBundle" key="checkbox.autodetect.utf"/>
</properties>
</component>
</children>
</grid>
<hspacer id="a4841">
<component id="e9af6" class="javax.swing.JLabel" binding="myTitleLabel">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<properties>
<text resource-bundle="messages/IdeBundle" key="encodings.dialog.caption"/>
</properties>
</component>
</children>
</grid>
</form>
@@ -18,6 +18,7 @@ package com.intellij.openapi.vfs.encoding;
import com.intellij.ide.IdeBundle;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.application.ApplicationNamesInfo;
import com.intellij.openapi.options.Configurable;
@@ -26,8 +27,8 @@ import com.intellij.openapi.options.OptionalConfigurable;
import com.intellij.openapi.options.SearchableConfigurable;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.CharsetToolkit;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.ScrollPaneFactory;
import com.intellij.ui.table.JBTable;
@@ -51,10 +52,12 @@ public class FileEncodingConfigurable implements SearchableConfigurable, Optiona
private JCheckBox myAutodetectUTFEncodedFilesCheckBox;
private JCheckBox myTransparentNativeToAsciiCheckBox;
private JPanel myPropertiesFilesEncodingCombo;
private Charset mySelectedCharsetForPropertiesFiles;
private JComboBox myIdeEncodingsCombo;
private final Ref<Charset> mySelectedCharsetForPropertiesFiles = new Ref<Charset>();
private final Ref<Charset> mySelectedIdeCharset = new Ref<Charset>();
private JLabel myTitleLabel;
private ChooseFileEncodingAction myAction;
private JPanel myIdeEncodingsListCombo;
private ChooseFileEncodingAction myPropertiesEncodingAction;
private ChooseFileEncodingAction myIdeEncodingAction;
public FileEncodingConfigurable(Project project) {
myProject = project;
@@ -85,25 +88,38 @@ public class FileEncodingConfigurable implements SearchableConfigurable, Optiona
return null;
}
@Override
public JComponent createComponent() {
myAction = new ChooseFileEncodingAction(null) {
private static ChooseFileEncodingAction installChooseEncodingCombo(@NotNull JPanel parentPanel, @NotNull final Ref<Charset> selected) {
ChooseFileEncodingAction myAction = new ChooseFileEncodingAction(null) {
@Override
public void update(final AnActionEvent e) {
getTemplatePresentation().setEnabled(true);
getTemplatePresentation().setText(mySelectedCharsetForPropertiesFiles == null ? SYSTEM_DEFAULT :
mySelectedCharsetForPropertiesFiles.displayName());
Charset charset = selected.get();
getTemplatePresentation().setText(charset == null ? SYSTEM_DEFAULT : charset.displayName());
}
@Override
protected void chosen(final VirtualFile virtualFile, final Charset charset) {
mySelectedCharsetForPropertiesFiles = charset == NO_ENCODING ? null : charset;
selected.set(charset == NO_ENCODING ? null : charset);
update((AnActionEvent)null);
}
@NotNull
@Override
protected DefaultActionGroup createPopupActionGroup(JComponent button) {
return createGroup("<System Default>");
}
};
myPropertiesFilesEncodingCombo.removeAll();
parentPanel.removeAll();
Presentation templatePresentation = myAction.getTemplatePresentation();
myPropertiesFilesEncodingCombo.add(myAction.createCustomComponent(templatePresentation), BorderLayout.CENTER);
parentPanel.add(myAction.createCustomComponent(templatePresentation), BorderLayout.CENTER);
myAction.update((AnActionEvent)null);
return myAction;
}
@Override
public JComponent createComponent() {
myPropertiesEncodingAction = installChooseEncodingCombo(myPropertiesFilesEncodingCombo, mySelectedCharsetForPropertiesFiles);
myIdeEncodingAction = installChooseEncodingCombo(myIdeEncodingsListCombo, mySelectedIdeCharset);
myTreeView = new FileTreeTable(myProject);
myTreePanel.setViewportView(myTreeView);
myTreeView.getEmptyText().setText(IdeBundle.message("file.encodings.not.configured"));
@@ -118,7 +134,7 @@ public class FileEncodingConfigurable implements SearchableConfigurable, Optiona
Map<VirtualFile, Charset> editing = myTreeView.getValues();
Map<VirtualFile, Charset> mapping = EncodingProjectManager.getInstance(myProject).getAllMappings();
boolean same = editing.equals(mapping)
&& Comparing.equal(encodingManager.getDefaultCharsetForPropertiesFiles(null), mySelectedCharsetForPropertiesFiles)
&& Comparing.equal(encodingManager.getDefaultCharsetForPropertiesFiles(null), mySelectedCharsetForPropertiesFiles.get())
&& encodingManager.isUseUTFGuessing(null) == myAutodetectUTFEncodedFilesCheckBox.isSelected()
&& encodingManager.isNative2AsciiForPropertiesFiles() == myTransparentNativeToAsciiCheckBox.isSelected()
;
@@ -126,12 +142,12 @@ public class FileEncodingConfigurable implements SearchableConfigurable, Optiona
}
public boolean isEncodingModified() {
final Object item = myIdeEncodingsCombo.getSelectedItem();
if (SYSTEM_DEFAULT.equals(item)) {
Charset charset = mySelectedIdeCharset.get();
if (null == charset) {
return !StringUtil.isEmpty(EncodingManager.getInstance().getDefaultCharsetName());
}
return !Comparing.equal(item, EncodingManager.getInstance().getDefaultCharset());
return !Comparing.equal(charset, EncodingManager.getInstance().getDefaultCharset());
}
@Override
@@ -139,17 +155,12 @@ public class FileEncodingConfigurable implements SearchableConfigurable, Optiona
Map<VirtualFile,Charset> result = myTreeView.getValues();
EncodingProjectManager encodingManager = EncodingProjectManager.getInstance(myProject);
encodingManager.setMapping(result);
encodingManager.setDefaultCharsetForPropertiesFiles(null, mySelectedCharsetForPropertiesFiles);
encodingManager.setDefaultCharsetForPropertiesFiles(null, mySelectedCharsetForPropertiesFiles.get());
encodingManager.setNative2AsciiForPropertiesFiles(null, myTransparentNativeToAsciiCheckBox.isSelected());
encodingManager.setUseUTFGuessing(null, myAutodetectUTFEncodedFilesCheckBox.isSelected());
final Object item = myIdeEncodingsCombo.getSelectedItem();
if (SYSTEM_DEFAULT.equals(item)) {
EncodingManager.getInstance().setDefaultCharsetName("");
}
else if (item != null) {
EncodingManager.getInstance().setDefaultCharsetName(((Charset)item).name());
}
Charset charset = mySelectedIdeCharset.get();
EncodingManager.getInstance().setDefaultCharsetName(charset == null ? "" : charset.name());
}
@Override
@@ -158,25 +169,15 @@ public class FileEncodingConfigurable implements SearchableConfigurable, Optiona
myTreeView.reset(encodingManager.getAllMappings());
myAutodetectUTFEncodedFilesCheckBox.setSelected(encodingManager.isUseUTFGuessing(null));
myTransparentNativeToAsciiCheckBox.setSelected(encodingManager.isNative2AsciiForPropertiesFiles());
mySelectedCharsetForPropertiesFiles = encodingManager.getDefaultCharsetForPropertiesFiles(null);
myAction.update((AnActionEvent)null);
mySelectedCharsetForPropertiesFiles.set(encodingManager.getDefaultCharsetForPropertiesFiles(null));
final DefaultComboBoxModel encodingsModel = new DefaultComboBoxModel(CharsetToolkit.getAvailableCharsets());
encodingsModel.insertElementAt(SYSTEM_DEFAULT, 0);
myIdeEncodingsCombo.setModel(encodingsModel);
final String name = EncodingManager.getInstance().getDefaultCharsetName();
if (StringUtil.isEmpty(name)) {
myIdeEncodingsCombo.setSelectedItem(SYSTEM_DEFAULT);
}
else {
myIdeEncodingsCombo.setSelectedItem(EncodingManager.getInstance().getDefaultCharset());
}
mySelectedIdeCharset.set(EncodingManager.getInstance().getDefaultCharset());
myPropertiesEncodingAction.update((AnActionEvent)null);
myIdeEncodingAction.update((AnActionEvent)null);
}
@Override
public void disposeUIResources() {
myAction = null;
}
public void selectFile(@NotNull VirtualFile virtualFile) {
@@ -112,7 +112,7 @@ public abstract class ChooseFileEncodingAction extends ComboBoxAction {
@Override
@NotNull
protected DefaultActionGroup createPopupActionGroup(final JComponent button) {
return createGroup(true);
return createGroup("<Clear>");
}
private void fillCharsetActions(DefaultActionGroup group, final VirtualFile virtualFile, List<Charset> charsets) {
@@ -174,8 +174,8 @@ public abstract class ChooseFileEncodingAction extends ComboBoxAction {
private class ClearThisFileEncodingAction extends AnAction {
private final VirtualFile myFile;
private ClearThisFileEncodingAction(@Nullable VirtualFile file) {
super("<Clear>", "Clear " +
private ClearThisFileEncodingAction(@Nullable VirtualFile file, @NotNull String clearItemText) {
super(clearItemText, "Clear " +
(file == null ? "default" : "file '"+file.getName()+"'") +
" encoding.", null);
myFile = file;
@@ -205,17 +205,18 @@ public abstract class ChooseFileEncodingAction extends ComboBoxAction {
};
protected abstract void chosen(VirtualFile virtualFile, Charset charset);
public DefaultActionGroup createGroup(boolean showClear) {
@NotNull
public DefaultActionGroup createGroup(@Nullable String clearItemText) {
DefaultActionGroup group = new DefaultActionGroup();
List<Charset> favorites = new ArrayList<Charset>(EncodingManager.getInstance().getFavorites());
Collections.sort(favorites);
Charset current = myVirtualFile == null ? null : myVirtualFile.getCharset();
favorites.remove(current);
if (showClear) {
group.add(new ClearThisFileEncodingAction(myVirtualFile));
if (clearItemText != null) {
group.add(new ClearThisFileEncodingAction(myVirtualFile, clearItemText));
}
if (favorites.isEmpty() && !showClear) {
if (favorites.isEmpty() && clearItemText == null) {
fillCharsetActions(group, myVirtualFile, Arrays.asList(CharsetToolkit.getAvailableCharsets()));
}
else {
@@ -185,7 +185,7 @@ public class EncodingPanel extends EditorBasedWidget implements StatusBarWidget.
EncodingPanel.this.update();
}
}
}.createGroup(false);
}.createGroup(null);
return JBPopupFactory.getInstance().createActionGroupPopup(null, group, dataContext, false, false, false, null, 30, null);
}