1 package com
.intellij
.ide
.fileTemplates
.ui
;
3 import com
.intellij
.ide
.IdeBundle
;
4 import com
.intellij
.ide
.fileTemplates
.FileTemplate
;
5 import com
.intellij
.openapi
.diagnostic
.Logger
;
6 import com
.intellij
.openapi
.util
.Pair
;
7 import org
.jetbrains
.annotations
.Nullable
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Arrays
;
13 import java
.util
.Properties
;
19 public class CreateFromTemplatePanel
{
20 private static final Logger LOG
= Logger
.getInstance("#com.intellij.ide.fileTemplates.ui.CreateFromTemplatePanel");
22 private JPanel myMainPanel
;
23 private JPanel myAttrPanel
;
24 private JTextField myFilenameField
;
25 private String
[] myUnsetAttributes
;
26 private ArrayList
<Pair
<String
, JTextField
>> myAttributes
= new ArrayList
<Pair
<String
,JTextField
>>();
28 private int myLastRow
= 0;
30 private int myHorisontalMargin
= -1;
31 private int myVerticalMargin
= -1;
32 private boolean myMustEnterName
;
33 private String myDefaultFileName
;
35 public CreateFromTemplatePanel(final String
[] unsetAttributes
, final boolean mustEnterName
,
36 @Nullable final String defaultFilename
){
37 myMustEnterName
= mustEnterName
;
38 myUnsetAttributes
= unsetAttributes
;
39 myDefaultFileName
= defaultFilename
;
40 Arrays
.sort(myUnsetAttributes
);
43 public boolean hasSomethingToAsk() {
44 return myMustEnterName
|| myUnsetAttributes
.length
!= 0;
47 public JComponent
getComponent() {
48 if (myMainPanel
== null){
49 myMainPanel
= new JPanel(new GridBagLayout()){
50 public Dimension
getPreferredSize(){
51 return getMainPanelPreferredSize(super.getPreferredSize());
54 myAttrPanel
= new JPanel(new GridBagLayout());
55 JPanel myScrollPanel
= new JPanel(new GridBagLayout());
58 myScrollPanel
.setBorder(null);
59 int attrCount
= myUnsetAttributes
.length
;
60 if (myMustEnterName
&& !Arrays
.asList(myUnsetAttributes
).contains(FileTemplate
.ATTRIBUTE_NAME
)) {
63 Insets insets
= (attrCount
> 1) ?
new Insets(2, 2, 2, 2) : new Insets(0, 0, 0, 0);
64 myScrollPanel
.add(myAttrPanel
, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints
.CENTER
, GridBagConstraints
.HORIZONTAL
, insets
, 0, 0));
66 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));
67 JScrollPane attrScroll
= new JScrollPane(myScrollPanel
);
68 attrScroll
.setViewportBorder(null);
69 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));
72 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));
78 public void ensureFitToScreen(int horisontalMargin
, int verticalMargin
){
79 myHorisontalMargin
= horisontalMargin
;
80 myVerticalMargin
= verticalMargin
;
83 private Dimension
getMainPanelPreferredSize(Dimension superPreferredSize
){
84 if((myHorisontalMargin
> 0) && (myVerticalMargin
> 0)){
85 Dimension screenSize
= Toolkit
.getDefaultToolkit().getScreenSize();
86 Dimension preferredSize
= superPreferredSize
;
87 Dimension maxSize
= new Dimension(screenSize
.width
- myHorisontalMargin
, screenSize
.height
- myVerticalMargin
);
88 int width
= Math
.min(preferredSize
.width
, maxSize
.width
);
89 int height
= Math
.min(preferredSize
.height
, maxSize
.height
);
90 if(height
< preferredSize
.height
){
91 width
= Math
.min(width
+ 50, maxSize
.width
); // to disable horizontal scroller
93 preferredSize
= new Dimension(width
, height
);
97 return superPreferredSize
;
101 private void updateShown() {
102 Insets insets
= new Insets(2, 2, 2, 2);
103 myAttrPanel
.add(Box
.createHorizontalStrut(200), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints
.CENTER
, GridBagConstraints
.HORIZONTAL
, insets
, 0, 0));
104 if(myMustEnterName
|| Arrays
.asList(myUnsetAttributes
).contains(FileTemplate
.ATTRIBUTE_NAME
)){
105 final JLabel filenameLabel
= new JLabel(IdeBundle
.message("label.file.name"));
106 myAttrPanel
.add(filenameLabel
, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints
.WEST
, GridBagConstraints
.NONE
, insets
, 0, 0));
107 myFilenameField
= new JTextField();
108 if (myDefaultFileName
!= null) {
109 myFilenameField
.setText(myDefaultFileName
);
111 myAttrPanel
.add(myFilenameField
, new GridBagConstraints(0, 2, 1, 1, 1.0, 0.0, GridBagConstraints
.WEST
, GridBagConstraints
.HORIZONTAL
, insets
, 0, 0));
114 for (String attribute
: myUnsetAttributes
) {
115 if (attribute
.equals(FileTemplate
.ATTRIBUTE_NAME
)) { // already asked above
118 JLabel label
= new JLabel(attribute
.replace('_', ' ') + ":");
119 JTextField field
= new JTextField();
120 myAttributes
.add(new Pair
<String
, JTextField
>(attribute
, field
));
121 myAttrPanel
.add(label
, new GridBagConstraints(0, myLastRow
* 2 + 3, 1, 1, 0.0, 0.0, GridBagConstraints
.WEST
, GridBagConstraints
.NONE
,
123 myAttrPanel
.add(field
, new GridBagConstraints(0, myLastRow
* 2 + 4, 1, 1, 1.0, 0.0, GridBagConstraints
.WEST
,
124 GridBagConstraints
.HORIZONTAL
, insets
, 0, 0));
128 myAttrPanel
.repaint();
129 myAttrPanel
.revalidate();
130 myMainPanel
.revalidate();
134 public String
getFileName(){
135 if (myFilenameField
!=null) {
136 String fileName
= myFilenameField
.getText();
137 return fileName
== null ?
"" : fileName
;
143 public Properties
getProperties(Properties predefinedProperties
){
144 Properties result
= (Properties
) predefinedProperties
.clone();
145 for (Pair
<String
, JTextField
> pair
: myAttributes
) {
146 result
.put(pair
.first
, pair
.second
.getText());