1 package com
.intellij
.ide
.fileTemplates
.ui
;
3 import com
.intellij
.ide
.IdeBundle
;
4 import com
.intellij
.ide
.fileTemplates
.FileTemplate
;
5 import com
.intellij
.ide
.fileTemplates
.actions
.AttributesDefaults
;
6 import com
.intellij
.openapi
.diagnostic
.Logger
;
7 import com
.intellij
.openapi
.ui
.impl
.DialogWrapperPeerImpl
;
8 import com
.intellij
.openapi
.util
.Pair
;
9 import com
.intellij
.openapi
.util
.TextRange
;
10 import org
.jetbrains
.annotations
.Nullable
;
14 import java
.util
.ArrayList
;
15 import java
.util
.Arrays
;
16 import java
.util
.Properties
;
22 public class CreateFromTemplatePanel
{
23 private static final Logger LOG
= Logger
.getInstance("#com.intellij.ide.fileTemplates.ui.CreateFromTemplatePanel");
25 private JPanel myMainPanel
;
26 private JPanel myAttrPanel
;
27 private JTextField myFilenameField
;
28 private String
[] myUnsetAttributes
;
29 private ArrayList
<Pair
<String
, JTextField
>> myAttributes
= new ArrayList
<Pair
<String
,JTextField
>>();
31 private int myLastRow
= 0;
33 private int myHorisontalMargin
= -1;
34 private int myVerticalMargin
= -1;
35 private boolean myMustEnterName
;
36 private AttributesDefaults myAttributesDefaults
;
38 public CreateFromTemplatePanel(final String
[] unsetAttributes
, final boolean mustEnterName
,
39 @Nullable final AttributesDefaults attributesDefaults
){
40 myMustEnterName
= mustEnterName
;
41 myUnsetAttributes
= unsetAttributes
;
42 myAttributesDefaults
= attributesDefaults
;
43 Arrays
.sort(myUnsetAttributes
);
46 public boolean hasSomethingToAsk() {
47 return myMustEnterName
|| myUnsetAttributes
.length
!= 0;
50 public JComponent
getComponent() {
51 if (myMainPanel
== null){
52 myMainPanel
= new JPanel(new GridBagLayout()){
53 public Dimension
getPreferredSize(){
54 return getMainPanelPreferredSize(super.getPreferredSize());
57 myAttrPanel
= new JPanel(new GridBagLayout());
58 JPanel myScrollPanel
= new JPanel(new GridBagLayout());
61 myScrollPanel
.setBorder(null);
62 int attrCount
= myUnsetAttributes
.length
;
63 if (myMustEnterName
&& !Arrays
.asList(myUnsetAttributes
).contains(FileTemplate
.ATTRIBUTE_NAME
)) {
66 Insets insets
= (attrCount
> 1) ?
new Insets(2, 2, 2, 2) : new Insets(0, 0, 0, 0);
67 myScrollPanel
.add(myAttrPanel
, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints
.CENTER
, GridBagConstraints
.HORIZONTAL
, insets
, 0, 0));
69 myScrollPanel
.add(new JPanel(), new GridBagConstraints(0, 1, 1, 1, 0.0, 1.0, GridBagConstraints
.CENTER
, GridBagConstraints
.BOTH
, new Insets(2, 2, 2, 2), 0, 0));
70 JScrollPane attrScroll
= new JScrollPane(myScrollPanel
);
71 attrScroll
.setViewportBorder(null);
72 myMainPanel
.add(attrScroll
, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints
.CENTER
, GridBagConstraints
.BOTH
, new Insets(2, 2, 2, 2), 0, 0));
75 myMainPanel
.add(myScrollPanel
, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints
.CENTER
, GridBagConstraints
.BOTH
, new Insets(0, 0, 0, 0), 0, 0));
81 public void ensureFitToScreen(int horisontalMargin
, int verticalMargin
){
82 myHorisontalMargin
= horisontalMargin
;
83 myVerticalMargin
= verticalMargin
;
86 private Dimension
getMainPanelPreferredSize(Dimension superPreferredSize
){
87 if((myHorisontalMargin
> 0) && (myVerticalMargin
> 0)){
88 Dimension screenSize
= Toolkit
.getDefaultToolkit().getScreenSize();
89 Dimension preferredSize
= superPreferredSize
;
90 Dimension maxSize
= new Dimension(screenSize
.width
- myHorisontalMargin
, screenSize
.height
- myVerticalMargin
);
91 int width
= Math
.min(preferredSize
.width
, maxSize
.width
);
92 int height
= Math
.min(preferredSize
.height
, maxSize
.height
);
93 if(height
< preferredSize
.height
){
94 width
= Math
.min(width
+ 50, maxSize
.width
); // to disable horizontal scroller
96 preferredSize
= new Dimension(width
, height
);
100 return superPreferredSize
;
104 private void updateShown() {
105 final Insets insets
= new Insets(2, 2, 2, 2);
106 myAttrPanel
.add(Box
.createHorizontalStrut(200), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints
.CENTER
, GridBagConstraints
.HORIZONTAL
, insets
, 0, 0));
107 if(myMustEnterName
|| Arrays
.asList(myUnsetAttributes
).contains(FileTemplate
.ATTRIBUTE_NAME
)){
108 final JLabel filenameLabel
= new JLabel(IdeBundle
.message("label.file.name"));
109 myAttrPanel
.add(filenameLabel
, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints
.WEST
, GridBagConstraints
.NONE
, insets
, 0, 0));
110 myFilenameField
= new JTextField();
112 // if default settings specified
113 if (myAttributesDefaults
!= null) {
114 final String fileName
= myAttributesDefaults
.getDefaultFileName();
115 // if default file name specified
116 if (fileName
!= null) {
117 // set predefined file name value
118 myFilenameField
.setText(fileName
);
119 final TextRange selectionRange
;
120 // select range from default attrubutes or select file name without extension
121 if (myAttributesDefaults
.getDefaultFileNameSelection() != null) {
122 selectionRange
= myAttributesDefaults
.getDefaultFileNameSelection();
124 final int dot
= fileName
.indexOf('.');
126 selectionRange
= new TextRange(0, dot
);
128 selectionRange
= null;
131 // set selection in editor
132 if (selectionRange
!= null) {
133 setPredefinedSelectionFor(myFilenameField
, selectionRange
);
137 myAttrPanel
.add(myFilenameField
, new GridBagConstraints(0, 2, 1, 1, 1.0, 0.0, GridBagConstraints
.WEST
, GridBagConstraints
.HORIZONTAL
, insets
, 0, 0));
140 for (String attribute
: myUnsetAttributes
) {
141 if (attribute
.equals(FileTemplate
.ATTRIBUTE_NAME
)) { // already asked above
144 final JLabel label
= new JLabel(attribute
.replace('_', ' ') + ":");
145 final JTextField field
= new JTextField();
146 if (myAttributesDefaults
!= null) {
147 final String defaultValue
= myAttributesDefaults
.getDefaultValueFor(attribute
);
148 final TextRange selectionRange
= myAttributesDefaults
.getRangeFor(attribute
);
149 if (defaultValue
!= null) {
150 field
.setText(defaultValue
);
151 // set default selection
152 if (selectionRange
!= null) {
153 setPredefinedSelectionFor(field
, selectionRange
);
157 myAttributes
.add(new Pair
<String
, JTextField
>(attribute
, field
));
158 myAttrPanel
.add(label
, new GridBagConstraints(0, myLastRow
* 2 + 3, 1, 1, 0.0, 0.0, GridBagConstraints
.WEST
, GridBagConstraints
.NONE
,
160 myAttrPanel
.add(field
, new GridBagConstraints(0, myLastRow
* 2 + 4, 1, 1, 1.0, 0.0, GridBagConstraints
.WEST
,
161 GridBagConstraints
.HORIZONTAL
, insets
, 0, 0));
165 myAttrPanel
.repaint();
166 myAttrPanel
.revalidate();
167 myMainPanel
.revalidate();
171 public String
getFileName(){
172 if (myFilenameField
!=null) {
173 String fileName
= myFilenameField
.getText();
174 return fileName
== null ?
"" : fileName
;
180 public Properties
getProperties(Properties predefinedProperties
){
181 Properties result
= (Properties
) predefinedProperties
.clone();
182 for (Pair
<String
, JTextField
> pair
: myAttributes
) {
183 result
.put(pair
.first
, pair
.second
.getText());
188 private void setPredefinedSelectionFor(final JTextField field
, final TextRange selectionRange
) {
189 field
.select(selectionRange
.getStartOffset(), selectionRange
.getEndOffset());
190 field
.putClientProperty(DialogWrapperPeerImpl
.HAVE_INITIAL_SELECTION
, true);