added opportunity to create task files of all supported file types
[idea/community.git] / python / educational / course-creator / src / com / jetbrains / edu / coursecreator / ui / CreateTaskFileDialog.java
1 package com.jetbrains.edu.coursecreator.ui;
2
3 import com.intellij.openapi.fileTypes.FileType;
4 import com.intellij.openapi.fileTypes.FileTypeManager;
5 import com.intellij.openapi.fileTypes.impl.FileTypeRenderer;
6 import com.intellij.openapi.project.Project;
7 import com.intellij.openapi.ui.DialogWrapper;
8 import com.intellij.ui.DoubleClickListener;
9 import com.intellij.ui.ListScrollingUtil;
10 import com.intellij.ui.components.JBList;
11 import org.jetbrains.annotations.Nullable;
12
13 import javax.swing.*;
14 import javax.swing.event.ListSelectionEvent;
15 import javax.swing.event.ListSelectionListener;
16 import java.awt.event.MouseEvent;
17
18 public class CreateTaskFileDialog extends DialogWrapper {
19   private JPanel myPanel;
20   private JBList myList;
21   private JTextField myTextField;
22
23   @SuppressWarnings("unchecked")
24   public CreateTaskFileDialog(@Nullable Project project, String generatedFileName) {
25     super(project);
26     FileType[] fileTypes = FileTypeManager.getInstance().getRegisteredFileTypes();
27
28     DefaultListModel model = new DefaultListModel();
29     for (FileType type : fileTypes) {
30       if (!type.isReadOnly() && !type.getDefaultExtension().isEmpty()) {
31         model.addElement(type);
32       }
33     }
34     myList.setModel(model);
35     myTextField.setText(generatedFileName);
36     setTitle("Create New Task File");
37     init();
38   }
39
40   @Nullable
41   @Override
42   protected JComponent createCenterPanel() {
43     myList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
44     myList.setCellRenderer(new FileTypeRenderer());
45
46     new DoubleClickListener() {
47       @Override
48       protected boolean onDoubleClick(MouseEvent e) {
49         doOKAction();
50         return true;
51       }
52     }.installOn(myList);
53
54     myList.getSelectionModel().addListSelectionListener(
55       new ListSelectionListener() {
56         @Override
57         public void valueChanged(ListSelectionEvent e) {
58           //TODO: do smth to check validness
59         }
60       }
61     );
62
63     ListScrollingUtil.selectItem(myList, FileTypeManager.getInstance().getFileTypeByExtension("py"));
64     return myPanel;
65   }
66
67   @Nullable
68   @Override
69   public JComponent getPreferredFocusedComponent() {
70     return myTextField;
71   }
72
73   public String getFileName() {
74     return myTextField.getText();
75   }
76
77   public FileType getFileType() {
78     return (FileType)myList.getSelectedValue();
79   }
80 }