add IDE encoding configuration to General settings page in PyCharm, like it was done...
authorDmitry Jemerov <yole@jetbrains.com>
Thu, 8 Jul 2010 15:09:34 +0000 (19:09 +0400)
committerDmitry Jemerov <yole@jetbrains.com>
Thu, 8 Jul 2010 17:24:47 +0000 (21:24 +0400)
platform/platform-impl/src/com/intellij/openapi/vfs/encoding/PlatformEncodingOptionsPanel.form [new file with mode: 0644]
platform/platform-impl/src/com/intellij/openapi/vfs/encoding/PlatformFileEncodingConfigurable.java [new file with mode: 0644]

diff --git a/platform/platform-impl/src/com/intellij/openapi/vfs/encoding/PlatformEncodingOptionsPanel.form b/platform/platform-impl/src/com/intellij/openapi/vfs/encoding/PlatformEncodingOptionsPanel.form
new file mode 100644 (file)
index 0000000..ce65c70
--- /dev/null
@@ -0,0 +1,33 @@
+<?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.PlatformFileEncodingConfigurable.PlatformEncodingOptionsPanel">
+  <grid id="27dc6" binding="myMainPanel" layout-manager="GridLayoutManager" row-count="2" column-count="4" 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>
+      <component id="34bb0" 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"/>
+        </constraints>
+        <properties>
+          <labelFor value="4fd18"/>
+          <text value="IDE Encoding:"/>
+        </properties>
+      </component>
+      <vspacer id="ce93f">
+        <constraints>
+          <grid row="1" 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="4fd18" class="javax.swing.JComboBox" binding="myIDEEncodingComboBox" default-binding="true">
+        <constraints>
+          <grid row="0" column="1" row-span="1" col-span="3" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
+        </constraints>
+        <properties/>
+      </component>
+    </children>
+  </grid>
+</form>
diff --git a/platform/platform-impl/src/com/intellij/openapi/vfs/encoding/PlatformFileEncodingConfigurable.java b/platform/platform-impl/src/com/intellij/openapi/vfs/encoding/PlatformFileEncodingConfigurable.java
new file mode 100644 (file)
index 0000000..e561c0d
--- /dev/null
@@ -0,0 +1,97 @@
+package com.intellij.openapi.vfs.encoding;
+
+import com.intellij.ide.IdeBundle;
+import com.intellij.openapi.options.ConfigurationException;
+import com.intellij.openapi.options.SearchableConfigurable;
+import com.intellij.openapi.util.Comparing;
+import com.intellij.openapi.util.text.StringUtil;
+import com.intellij.openapi.vfs.CharsetToolkit;
+import org.jetbrains.annotations.Nls;
+
+import javax.swing.*;
+import java.nio.charset.Charset;
+
+/**
+ * Component that adds "IDE Encoding" option to General settings tab. Register it as generalOptionsProvider extension
+ * if you want to use it in your product. 
+ *
+ * @author yole
+ */
+public class PlatformFileEncodingConfigurable implements SearchableConfigurable {
+  private static final String SYSTEM_DEFAULT = IdeBundle.message("encoding.name.system.default");
+  private PlatformEncodingOptionsPanel myPanel;
+
+  public String getId() {
+    return "GeneralEncodingOptions";
+  }
+
+  public Runnable enableSearch(String option) {
+    return null;
+  }
+
+  @Nls
+  public String getDisplayName() {
+    return "";
+  }
+
+  public Icon getIcon() {
+    return null;
+  }
+
+  public String getHelpTopic() {
+    return null;
+  }
+
+  public JComponent createComponent() {
+    if (myPanel == null) {
+      myPanel = new PlatformEncodingOptionsPanel();
+    }
+    return myPanel.getMainPanel();
+  }
+
+  public boolean isModified() {
+    final Object item = myPanel.myIDEEncodingComboBox.getSelectedItem();
+    if (SYSTEM_DEFAULT.equals(item)) {
+      return !StringUtil.isEmpty(EncodingManager.getInstance().getDefaultCharsetName());
+    }
+
+    return !Comparing.equal(item, EncodingManager.getInstance().getDefaultCharset());
+  }
+
+  public void apply() throws ConfigurationException {
+    final Object item = myPanel.myIDEEncodingComboBox.getSelectedItem();
+    if (SYSTEM_DEFAULT.equals(item)) {
+      EncodingManager.getInstance().setDefaultCharsetName("");
+    }
+    else if (item != null) {
+      EncodingManager.getInstance().setDefaultCharsetName(((Charset)item).name());
+    }
+  }
+
+  public void reset() {
+    final DefaultComboBoxModel encodingsModel = new DefaultComboBoxModel(CharsetToolkit.getAvailableCharsets());
+    encodingsModel.insertElementAt(SYSTEM_DEFAULT, 0);
+    myPanel.myIDEEncodingComboBox.setModel(encodingsModel);
+
+    final String name = EncodingManager.getInstance().getDefaultCharsetName();
+    if (StringUtil.isEmpty(name)) {
+      myPanel.myIDEEncodingComboBox.setSelectedItem(SYSTEM_DEFAULT);
+    }
+    else {
+      myPanel.myIDEEncodingComboBox.setSelectedItem(EncodingManager.getInstance().getDefaultCharset());
+    }
+  }
+
+  public void disposeUIResources() {
+    myPanel = null;
+  }
+
+  public static class PlatformEncodingOptionsPanel {
+    JComboBox myIDEEncodingComboBox;
+    private JPanel myMainPanel;
+
+    public JPanel getMainPanel() {
+      return myMainPanel;
+    }
+  }
+}