1    package com.instantbank.lettertemplate.editor.applet;
2    
3    import javax.swing.JPanel;
4    import javax.swing.JTextField;
5    import javax.swing.JButton;
6    import java.awt.BorderLayout;
7    import javax.swing.JDialog;
8    import javax.swing.JOptionPane;
9    import javax.swing.JComboBox;
10   import java.awt.Dimension;
11   import java.awt.event.ActionListener;
12   import java.awt.event.ActionEvent;
13   import com.instantbank.lettertemplate.editor.util.LetterOp;
14   import com.instantbank.common.utilcomponents.CodeDescription;
15   import com.instantbank.common.uiutils.GetLineFromListDialog;
16   import com.instantbank.component.lettertemplate.util.LetterComponent;
17   
18   /**
19    *  Visual control that shows and allows to choose/type a LetterComponent name.
20    *
21    * @author InstantBank (Rodrigo Lopez)
22    * @created September 2002
23    */
24   
25   public class ComponentNameDisplay extends JPanel {
26     /**
27      *  Button that must be pressed in order to choose a component.
28      */
29     JButton btn = new JButton("...");
30   
31     /**
32      *  Name of the component.
33      */
34     JTextField txtName = new JTextField();
35   
36     /**
37      *  Array of < code, name> of the choosable components.
38      */
39     CodeDescription[] compNames;
40   
41     /**
42      *  Mode of selection. See this class' {@link
43      *  #ComponentNameDisplay(CodeDescription [],String,int) main constructor.}
44      */
45     int mode;
46   
47     /**
48      *  Name of the component type: "header", "body", "closing".
49      */
50     String compType;
51   
52   
53     /**
54      *  Constructor for ComponentNameDisplay.
55      *
56      * @param cmpNames Array of < code,name> of the components that can be
57      *      chosen.
58      * @param type Name of the component type; i.e: "header", "body" ,
59      *      "closing".
60      * @param mode Mode of use of the control.
61      *      <ul>
62      *        <li> {@link LetterOp#LOAD} => The component's name cannot be typed.
63      *        It can only be chosen from a dropdown list.
64      *        <li> {@link LetterOp#SAVEAS} => The component's name can be either
65      *        typed or chosen from a dropdown list.
66      *      </ul>
67      */
68     public ComponentNameDisplay(CodeDescription[] cmpNames, String type, int mode) {
69       this.compNames = cmpNames;
70       this.compType = type;
71       this.mode = mode;
72       init();
73     }
74   
75   
76     /**
77      *  Inits the look and feel of the control.
78      */
79     public void init() {
80       setLayout(new BorderLayout());
81       add(txtName, BorderLayout.CENTER);
82       if(mode == LetterOp.LOAD) {
83         txtName.setEditable(false);
84       }
85   
86       if(mode == LetterOp.SAVEAS) {
87         add(btn, BorderLayout.EAST);
88       }
89   
90       btn.addActionListener(
91         new ActionListener() {
92           public void actionPerformed(ActionEvent e) {
93   
94             //The compNames array is never null but can have 0 entries.
95             if(compNames.length == 0) {
96               JOptionPane.showMessageDialog(
97                 null, "There are no " + compType + " to choose");
98             }
99             else {
100              /*GetNameDialog gnd = new GetNameDialog(txtName, compNames, compType);
101              gnd.setLocationRelativeTo(ComponentNameDisplay.this);
102              gnd.show();*/
103              Object[] answer = new Object[2];
104              JDialog gld =
105                new GetLineFromListDialog(
106                "Choosing a " + compType,
107                "Existing " + LetterComponent.typeToPlural(compType),
108                CodeDescription.toDescriptions(compNames),
109                answer);
110              gld.setLocationRelativeTo(ComponentNameDisplay.this);
111              gld.show();
112  
113              int ansVal = ((Integer)answer[0]).intValue();
114  
115              if(ansVal == GetLineFromListDialog.CANCEL) {
116                return;
117              }
118  
119              String selected = (String)answer[1];
120              if(selected == null) {  //Nothing was selected
121                return;
122              }
123  
124              txtName.setText(selected);
125  
126            }
127          }
128        });
129    }
130  
131  
132    /**
133     *  Returns the text that has been chosen or typed in the {@link #txtName}
134     *  control. The text is converted to lower case.
135     *
136     * @return The name value
137     */
138    public String getName() {
139      return txtName.getText().trim().toLowerCase();
140    }
141  
142  
143    /**
144     *  Sets the name of the component in the {@link #txtName} area.
145     *
146     * @param s The new name value
147     */
148    public void setName(String s) {
149      txtName.setText(s);
150    }
151  
152  
153    /**
154     * The focus goes to the text field in this component.
155     */
156    public void grabFocus() {
157      txtName.grabFocus();
158    }
159  
160  
161    /**
162     * All the text in {@link #txtName} is selected. Must be invoked
163     * after {@link #grabFocus()}.
164     */
165    public void selectAll() {
166      txtName.selectAll();
167    }
168  
169  }
170  
171