suggest file name patterns to 'Associate With File Type' dialog storm/108.1343
authorNikolay Matveev <Nikolay.Matveev@jetbrains.com>
Tue, 20 Sep 2011 19:08:59 +0000 (23:08 +0400)
committerNikolay Matveev <Nikolay.Matveev@jetbrains.com>
Wed, 21 Sep 2011 08:11:06 +0000 (12:11 +0400)
platform/platform-impl/src/com/intellij/openapi/fileTypes/ex/FileTypeChooser.java
platform/platform-impl/src/com/intellij/openapi/fileTypes/ex/FileTypeChooserPanel.form
platform/platform-impl/testSrc/com/intellij/openapi/fileTypes/ex/FileTypeChooserTest.java [new file with mode: 0644]

index 06fd4db48cc8f5cfe7d86d255122339aa10e51b3..e566ee2bbb074876d61111f3802ed0c4f6d990da 100644 (file)
@@ -19,11 +19,16 @@ import com.intellij.openapi.application.ApplicationManager;
 import com.intellij.openapi.application.ApplicationNamesInfo;
 import com.intellij.openapi.fileTypes.*;
 import com.intellij.openapi.fileTypes.impl.FileTypeRenderer;
+import com.intellij.openapi.ui.ComboBox;
 import com.intellij.openapi.ui.DialogWrapper;
-import com.intellij.openapi.util.io.FileUtil;
 import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.openapi.vfs.VirtualFile;
+import com.intellij.ui.CollectionComboBoxModel;
 import com.intellij.ui.ListScrollingUtil;
+import com.intellij.util.ArrayUtil;
+import com.intellij.util.Function;
+import com.intellij.util.containers.ContainerUtil;
+import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
 import javax.swing.*;
@@ -33,17 +38,19 @@ import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
 import java.util.Arrays;
 import java.util.Comparator;
+import java.util.Deque;
+import java.util.LinkedList;
 
 public class FileTypeChooser extends DialogWrapper {
   private JList myList;
   private JLabel myTitleLabel;
-  private JTextField myPattern;
+  private ComboBox myPattern;
   private JPanel myPanel;
   private JRadioButton myOpenInIdea;
   private JRadioButton myOpenAsNative;
   private final String myFileName;
 
-  private FileTypeChooser(String pattern, String fileName) {
+  private FileTypeChooser(@NotNull String[] patterns, @NotNull String fileName) {
     super(true);
     myFileName = fileName;
 
@@ -69,7 +76,7 @@ public class FileTypeChooser extends DialogWrapper {
       }
     }
     myList.setModel(model);
-    myPattern.setText(pattern);
+    myPattern.setModel(new CollectionComboBoxModel(ContainerUtil.map(patterns, Function.ID), patterns[0]));
 
     setTitle(FileTypesBundle.message("filetype.chooser.title"));
     init();
@@ -146,8 +153,8 @@ public class FileTypeChooser extends DialogWrapper {
   }
 
   @Nullable
-  public static FileType associateFileType(String fileName) {
-    final FileTypeChooser chooser = new FileTypeChooser(suggestPatternText(fileName), fileName);
+  public static FileType associateFileType(@NotNull final String fileName) {
+    final FileTypeChooser chooser = new FileTypeChooser(suggestPatterns(fileName), fileName);
     chooser.show();
     if (!chooser.isOK()) return null;
     final FileType type = chooser.getSelectedType();
@@ -155,24 +162,25 @@ public class FileTypeChooser extends DialogWrapper {
 
     ApplicationManager.getApplication().runWriteAction(new Runnable() {
       public void run() {
-        FileTypeManagerEx.getInstanceEx().associatePattern(type, chooser.myPattern.getText());
+        FileTypeManagerEx.getInstanceEx().associatePattern(type, (String)chooser.myPattern.getSelectedItem());
       }
     });
 
     return type;
   }
 
-  private static String suggestPatternText(final String fileName) {
-    String pattern = FileUtil.getExtension(fileName);
-
-    final String finalPattern;
-    if (StringUtil.isEmpty(pattern)) {
-      finalPattern = fileName;
-    }
-    else {
-      finalPattern = "*." + pattern;
+  @NotNull
+  static String[] suggestPatterns(@NotNull final String fileName) {
+    final Deque<String> patterns = new LinkedList<String>();
+    int i = -1;
+    patterns.addFirst(fileName);
+    while ((i = fileName.indexOf('.', i + 1)) > 0) {
+      final String extension = fileName.substring(i);
+      if (!StringUtil.isEmpty(extension)) {
+        patterns.addFirst("*" + extension);
+      }
     }
-    return finalPattern;
+    return ArrayUtil.toStringArray(patterns);
   }
 
   @Override
index ffd0443b9e8b42020b21ca279b8e3d70a01470b0..af2a24831d7b8a58f84bfa43e0f4c78932cb12c8 100644 (file)
           <text resource-bundle="messages/FileTypesBundle" key="filetype.chooser.file.pattern"/>
         </properties>
       </component>
-      <component id="ec3da" class="javax.swing.JTextField" binding="myPattern">
+      <component id="ec3da" class="com.intellij.openapi.ui.ComboBox" binding="myPattern">
         <constraints>
           <grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
             <preferred-size width="150" height="-1"/>
           </grid>
         </constraints>
-        <properties/>
+        <properties>
+          <editable value="true"/>
+        </properties>
       </component>
       <scrollpane class="com.intellij.ui.components.JBScrollPane" id="f588b">
         <constraints>
diff --git a/platform/platform-impl/testSrc/com/intellij/openapi/fileTypes/ex/FileTypeChooserTest.java b/platform/platform-impl/testSrc/com/intellij/openapi/fileTypes/ex/FileTypeChooserTest.java
new file mode 100644 (file)
index 0000000..8b57b54
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2000-2011 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.openapi.fileTypes.ex;
+
+import com.intellij.testFramework.UsefulTestCase;
+
+/**
+ * @author Nikolay Matveev
+ */
+public class FileTypeChooserTest extends UsefulTestCase {
+
+  public void testSuggestPatterns() {
+    assertSameElements(FileTypeChooser.suggestPatterns("a"), "a");
+    assertSameElements(FileTypeChooser.suggestPatterns("a.b"), "a.b", "*.b");
+    assertSameElements(FileTypeChooser.suggestPatterns("a.b.c"), "a.b.c", "*.c", "*.b.c");
+  }
+
+}
+