IDEA-50343 Suggestion for create new class dialog: Up/Down in name field changes...
[fedora-idea.git] / platform / lang-impl / src / com / intellij / ide / actions / CreateFileFromTemplateDialog.java
blob74362b084edcbb480e51aabb589e867c4f045a11
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.intellij.ide.actions;
19 import com.intellij.ide.ui.UISettings;
20 import com.intellij.openapi.actionSystem.AnAction;
21 import com.intellij.openapi.actionSystem.AnActionEvent;
22 import com.intellij.openapi.actionSystem.CustomShortcutSet;
23 import com.intellij.openapi.actionSystem.KeyboardShortcut;
24 import com.intellij.openapi.diagnostic.Logger;
25 import com.intellij.openapi.project.Project;
26 import com.intellij.openapi.ui.DialogWrapper;
27 import com.intellij.openapi.util.Ref;
28 import com.intellij.openapi.util.Trinity;
29 import com.intellij.psi.PsiElement;
30 import com.intellij.ui.ComboboxSpeedSearch;
31 import com.intellij.util.Icons;
32 import com.intellij.util.IncorrectOperationException;
33 import org.jetbrains.annotations.NotNull;
34 import org.jetbrains.annotations.Nullable;
36 import javax.swing.*;
37 import java.awt.*;
38 import java.awt.event.KeyEvent;
40 /**
41 * @author peter
43 public class CreateFileFromTemplateDialog extends DialogWrapper {
44 private static final Logger LOG = Logger.getInstance("#com.intellij.ide.actions.CreateFileFromTemplateDialog");
45 private JTextField myNameField;
46 private JComboBox myKindCombo;
47 private JPanel myPanel;
48 private JLabel myUpDownHint;
50 private ElementCreator myCreator;
52 private CreateFileFromTemplateDialog(@NotNull Project project, @NotNull final String title) {
53 super(project, true);
55 myKindCombo.setRenderer(new DefaultListCellRenderer() {
56 @Override
57 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
58 super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
59 if (value == null) {
60 LOG.error("Model: " + list.getModel().toString());
63 @SuppressWarnings({"unchecked"}) Trinity<String, Icon, String> _value = (Trinity<String, Icon, String>) value;
64 setText(_value.first);
65 setIcon(_value.second);
66 return this;
68 });
70 setTitle(title);
71 //myNameLabel.setText(prompt);
73 new ComboboxSpeedSearch(myKindCombo) {
74 @Override
75 protected String getElementText(Object element) {
76 return ((Trinity<String, Icon, String>)element).first;
80 final AnAction arrow = new AnAction() {
81 @Override
82 public void actionPerformed(AnActionEvent e) {
83 if (e.getInputEvent() instanceof KeyEvent) {
84 final int code = ((KeyEvent)e.getInputEvent()).getKeyCode();
85 final int delta = code == KeyEvent.VK_DOWN ? 1 : code == KeyEvent.VK_UP ? -1 : 0;
87 final int size = myKindCombo.getModel().getSize();
88 int next = myKindCombo.getSelectedIndex() + delta;
89 if (next < 0 || next >= size) {
90 if (!UISettings.getInstance().CYCLE_SCROLLING) {
91 return;
93 next = (next + size) % size;
95 myKindCombo.setSelectedIndex(next);
99 final KeyboardShortcut up = new KeyboardShortcut(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), null);
100 final KeyboardShortcut down = new KeyboardShortcut(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), null);
101 arrow.registerCustomShortcutSet(new CustomShortcutSet(up, down), myNameField);
103 myUpDownHint.setIcon(Icons.UP_DOWN_ARROWS);
104 init();
107 private String getEnteredName() {
108 return myNameField.getText();
111 private String getTemplateName() {
112 //noinspection unchecked
113 final Trinity<String, Icon, String> trinity = (Trinity<String, Icon, String>)myKindCombo.getSelectedItem();
114 if (trinity == null) {
115 LOG.error("Model: " + myKindCombo.getModel());
118 return trinity.third;
121 @Override
122 protected JComponent createCenterPanel() {
123 return myPanel;
126 @Override
127 protected void doOKAction() {
128 if (myCreator.tryCreate(getEnteredName()).length == 0) {
129 return;
131 super.doOKAction();
134 @Override
135 public JComponent getPreferredFocusedComponent() {
136 return myNameField;
139 public static <T extends PsiElement> Builder createDialog(@NotNull final Project project, @NotNull final String title) {
140 final CreateFileFromTemplateDialog dialog = new CreateFileFromTemplateDialog(project, title);
142 return new Builder() {
144 public Builder addKind(@NotNull String name, @Nullable Icon icon, @NotNull String templateName) {
145 dialog.myKindCombo.addItem(new Trinity<String, Icon, String>(name, icon, templateName));
146 return this;
149 public <T extends PsiElement> T show(@NotNull String errorTitle, @NotNull final FileCreator<T> creator) {
150 final Ref<T> created = Ref.create(null);
151 dialog.myCreator = new ElementCreator(project, errorTitle) {
152 @Override
153 protected void checkBeforeCreate(String newName) throws IncorrectOperationException {
154 creator.checkBeforeCreate(newName, dialog.getTemplateName());
157 @Override
158 protected PsiElement[] create(String newName) throws Exception {
159 final T element = creator.createFile(dialog.getEnteredName(), dialog.getTemplateName());
160 created.set(element);
161 if (element != null) {
162 return new PsiElement[]{element};
164 return PsiElement.EMPTY_ARRAY;
167 @Override
168 protected String getActionName(String newName) {
169 return creator.getActionName(newName, dialog.getTemplateName());
172 dialog.show();
173 if (dialog.getExitCode() == OK_EXIT_CODE) {
174 return created.get();
176 return null;
181 public interface Builder {
182 Builder addKind(@NotNull String kind, @Nullable Icon icon, @NotNull String templateName);
183 @Nullable
184 <T extends PsiElement> T show(@NotNull String errorTitle, @NotNull FileCreator<T> creator);
187 public interface FileCreator<T> {
188 void checkBeforeCreate(@NotNull String name, @NotNull String templateName) throws IncorrectOperationException;
190 @Nullable
191 T createFile(@NotNull String name, @NotNull String templateName);
193 @NotNull
194 String getActionName(@NotNull String name, @NotNull String templateName);