KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > multiview > ui > SimpleDialogPanel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.multiview.ui;
21
22 // AWT
23
import java.awt.GridBagLayout JavaDoc;
24 import java.awt.GridBagConstraints JavaDoc;
25 import java.awt.BorderLayout JavaDoc;
26
27 // Swing
28
import javax.swing.JTextField JavaDoc;
29 import javax.swing.JTextArea JavaDoc;
30 import javax.swing.text.JTextComponent JavaDoc;
31 import javax.swing.JPanel JavaDoc;
32 import javax.swing.JLabel JavaDoc;
33 import javax.swing.JButton JavaDoc;
34
35 // Netbeans
36
import org.openide.util.NbBundle;
37
38 /** The simple dialog panel containing labels, text fields and browse buttons.
39  * <br>
40  * Individual components should be described using SimpleDialogPanel.DialogDescriptor class.
41  *
42  * @author mk115033
43  * Created on January 03, 2005
44  *
45  */

46 public class SimpleDialogPanel extends JPanel JavaDoc {
47     private JTextComponent JavaDoc[] jTextComponents;
48     private JLabel JavaDoc[] jLabels;
49     private JButton JavaDoc[] jButtons;
50     private GridBagConstraints JavaDoc gridBagConstraints;
51
52     /** Constructor that creates the simple ADD/EDIT dialog with n labels and n empty text fields,
53     * where the shape of the dialog is described using the DialogDescriptor object.
54     * @param desc describes components inside the panel
55     */

56     public SimpleDialogPanel(DialogDescriptor desc) {
57         super();
58         initComponents(desc.getLabels(), desc.isTextField(), desc.getSize(), desc.getButtons(), desc.getMnemonics(), desc.getA11yDesc());
59         String JavaDoc[] initValues = desc.getInitValues();
60         if (initValues!=null)
61             for (int i=0;i<initValues.length;i++) {
62                 jTextComponents[i].setText(initValues[i]);
63             }
64     }
65
66     private void initComponents(String JavaDoc[] labels, boolean[] isTextField, int size, boolean[] customizers, char[] mnem, String JavaDoc[] a11yDesc) {
67         setLayout(new GridBagLayout JavaDoc());
68         jLabels = new JLabel JavaDoc [labels.length];
69         jTextComponents = new JTextComponent JavaDoc [labels.length];
70         for (int i=0;i<labels.length;i++) {
71             jLabels[i] = new JLabel JavaDoc(labels[i]);
72             gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
73             gridBagConstraints.gridx = 0;
74             gridBagConstraints.gridy = i;
75             gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 12, 0, 0);
76             if (isTextField[i])
77                 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
78             else
79                 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
80             add(jLabels[i], gridBagConstraints);
81         }
82         for (int i=0;i<jTextComponents.length;i++) {
83             if (isTextField[i]) { // text field
84
jTextComponents[i] = new JTextField JavaDoc();
85                 ((JTextField JavaDoc)jTextComponents[i]).setColumns(size);
86             } else { // text area
87
jTextComponents[i] = new JTextArea JavaDoc();
88                 ((JTextArea JavaDoc)jTextComponents[i]).setRows(3);
89                 if (i>0) jTextComponents[i].setBorder(jTextComponents[0].getBorder());
90             }
91             gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
92             gridBagConstraints.gridx = 1;
93             gridBagConstraints.gridy = i;
94             gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 12, 0, 11);
95             gridBagConstraints.weightx = 1.0;
96             jLabels[i].setLabelFor(jTextComponents[i]);
97             if (isTextField[i]) {// text field
98
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
99                 add(jTextComponents[i], gridBagConstraints);
100             } else {
101                 gridBagConstraints.weighty = 1.0;
102                 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
103                 javax.swing.JScrollPane JavaDoc sp = new javax.swing.JScrollPane JavaDoc(jTextComponents[i]);
104                 add(sp, gridBagConstraints);
105             }
106         }
107         if (customizers!=null) {
108             java.util.List JavaDoc buttonList = new java.util.ArrayList JavaDoc();
109             int j=0;
110             for (int i=0;i<customizers.length;i++) {
111                 if (customizers[i]) {
112                     JButton JavaDoc button = new JButton JavaDoc();
113                     button.setText(NbBundle.getMessage(SimpleDialogPanel.class,"LBL_browse"+String.valueOf(j)));
114                     button.setMnemonic(NbBundle.getMessage(SimpleDialogPanel.class,"LBL_browse"+String.valueOf(j++)+"_mnem").charAt(0));
115                     button.setMargin(new java.awt.Insets JavaDoc(0, 14, 0, 14));
116                     buttonList.add(button);
117                     gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
118                     gridBagConstraints.gridx = 2;
119                     gridBagConstraints.gridy = i;
120                     gridBagConstraints.insets = new java.awt.Insets JavaDoc(3, 0, 0, 11);
121                     add(button, gridBagConstraints);
122                 }
123             }
124             jButtons = new JButton JavaDoc[buttonList.size()];
125             buttonList.toArray(jButtons);
126         }
127         if (mnem!=null) {
128             for (int i=0;i<labels.length;i++) {
129                 jLabels[i].setLabelFor(jTextComponents[i]);
130                 jLabels[i].setDisplayedMnemonic(mnem[i]);
131             }
132         }
133         if (a11yDesc!=null) {
134             for (int i=0;i<jTextComponents.length;i++) {
135                 jTextComponents[i].getAccessibleContext().setAccessibleDescription(a11yDesc[i]);
136             }
137         }
138     }
139     /** Returns the values from the text fields.
140     * @return text fields values
141     */

142     public String JavaDoc[] getValues() {
143         if (jTextComponents==null) return null;
144         String JavaDoc[] values = new String JavaDoc[jTextComponents.length];
145         for (int i=0;i<values.length;i++) {
146             values[i] = jTextComponents[i].getText();
147         }
148         return values;
149     }
150     /** Returns the JButton components from the dialog described in DialogDescriptor.
151     * @return JButton components that are used for invoking the text field customizers
152     */

153     public JButton JavaDoc[] getCustomizerButtons() {
154         return jButtons;
155     }
156     /** Returns the dialog text fields.
157     * @return array of text fields
158     */

159     public JTextComponent JavaDoc[] getTextComponents() {
160         return jTextComponents;
161     }
162     /** This is the descriptor for the dialog components.
163     * Parameters are :<ul>
164     * <li>labels = text array for text fields
165     * <li>initValues = initialization values for text fields
166     * <li>adding = indicates the type of the dialog (ADD/EDIT). Defaultly set to true (ADD dialog)
167     * <li>customizers = describes the layout of the customizers buttons.<br>
168     * For example setCustomizers(new boolean{false,true,false}) sets the customizer only for the second text field
169     * <li>size = default number of columns for text fields. Defaultly set to 25
170     * </ul>
171     */

172     
173     public static class DialogDescriptor {
174         String JavaDoc[] labels;
175         String JavaDoc[] initValues;
176         boolean adding;
177         boolean[] buttons;
178         boolean textField[];
179         char[] mnem;
180         String JavaDoc[] a11yDesc;
181         int size;
182
183         
184         /** the constructor for DialogDescriptor object
185         * @param labels labels names
186         */

187         public DialogDescriptor(String JavaDoc[] labels) {
188             this.labels=labels;
189             size=25;
190             adding=true;
191             textField = new boolean[labels.length];
192             for (int i=0;i<labels.length;i++) {
193                 textField[i]=true; // setting textFields to text fields
194
}
195         }
196         public String JavaDoc[] getLabels() {
197             return labels;
198         }
199         /** Specifies which text fields should contain the Browse buttons
200          * Limited up to 3 Browse bottons (number of "true" items in buttons array)
201          * Example : setButtons (new boolean[] {true, false, false, true, false, false});
202          */

203         public void setButtons(boolean[] buttons) {
204             this.buttons=buttons;
205         }
206         public boolean[] getButtons() {
207             return buttons;
208         }
209         /** Sets the text fields
210          */

211         public void setTextField(boolean[] textField) {
212             this.textField=textField;
213         }
214         public boolean[] isTextField() {
215             return textField;
216         }
217         /** Sets the init values for text fields
218          */

219         public void setInitValues(String JavaDoc[] initValues) {
220             this.initValues=initValues;
221             adding=false;
222         }
223         public String JavaDoc[] getInitValues() {
224             return initValues;
225         }
226         /** Specifies whether the dialog adds or adits values
227          */

228         public void setAdding(boolean adding) {
229             this.adding=adding;
230         }
231         public boolean isAdding() {
232             return adding;
233         }
234         /** Sets the longest text field size
235          */

236         public void setSize(int size) {
237             this.size=size;
238         }
239         public int getSize() {
240             return size;
241         }
242         /** Sets mnemonics for labels
243          */

244         public void setMnemonics(char[] mnem) {
245             this.mnem=mnem;
246         }
247         public char[] getMnemonics() {
248             return mnem;
249         }
250         /** Sets A11Y desc for text fields
251          */

252         public void setA11yDesc(String JavaDoc[] a11yDesc) {
253             this.a11yDesc=a11yDesc;
254         }
255         public String JavaDoc[] getA11yDesc() {
256             return a11yDesc;
257         }
258     }
259 }
Popular Tags