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
;
38 import java
.awt
.event
.KeyEvent
;
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
) {
55 myKindCombo
.setRenderer(new DefaultListCellRenderer() {
57 public Component
getListCellRendererComponent(JList list
, Object value
, int index
, boolean isSelected
, boolean cellHasFocus
) {
58 super.getListCellRendererComponent(list
, value
, index
, isSelected
, cellHasFocus
);
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
);
71 //myNameLabel.setText(prompt);
73 new ComboboxSpeedSearch(myKindCombo
) {
75 protected String
getElementText(Object element
) {
76 return ((Trinity
<String
, Icon
, String
>)element
).first
;
80 final AnAction arrow
= new AnAction() {
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
) {
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
);
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
;
122 protected JComponent
createCenterPanel() {
127 protected void doOKAction() {
128 if (myCreator
.tryCreate(getEnteredName()).length
== 0) {
135 public JComponent
getPreferredFocusedComponent() {
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
));
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
) {
153 protected void checkBeforeCreate(String newName
) throws IncorrectOperationException
{
154 creator
.checkBeforeCreate(newName
, dialog
.getTemplateName());
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
;
168 protected String
getActionName(String newName
) {
169 return creator
.getActionName(newName
, dialog
.getTemplateName());
173 if (dialog
.getExitCode() == OK_EXIT_CODE
) {
174 return created
.get();
181 public interface Builder
{
182 Builder
addKind(@NotNull String kind
, @Nullable Icon icon
, @NotNull String templateName
);
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
;
191 T
createFile(@NotNull String name
, @NotNull String templateName
);
194 String
getActionName(@NotNull String name
, @NotNull String templateName
);