1 19 20 package org.netbeans.modules.xml.multiview.ui; 21 22 import java.awt.GridBagLayout ; 24 import java.awt.GridBagConstraints ; 25 import java.awt.BorderLayout ; 26 27 import javax.swing.JTextField ; 29 import javax.swing.JTextArea ; 30 import javax.swing.text.JTextComponent ; 31 import javax.swing.JPanel ; 32 import javax.swing.JLabel ; 33 import javax.swing.JButton ; 34 35 import org.openide.util.NbBundle; 37 38 46 public class SimpleDialogPanel extends JPanel { 47 private JTextComponent [] jTextComponents; 48 private JLabel [] jLabels; 49 private JButton [] jButtons; 50 private GridBagConstraints gridBagConstraints; 51 52 56 public SimpleDialogPanel(DialogDescriptor desc) { 57 super(); 58 initComponents(desc.getLabels(), desc.isTextField(), desc.getSize(), desc.getButtons(), desc.getMnemonics(), desc.getA11yDesc()); 59 String [] 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 [] labels, boolean[] isTextField, int size, boolean[] customizers, char[] mnem, String [] a11yDesc) { 67 setLayout(new GridBagLayout ()); 68 jLabels = new JLabel [labels.length]; 69 jTextComponents = new JTextComponent [labels.length]; 70 for (int i=0;i<labels.length;i++) { 71 jLabels[i] = new JLabel (labels[i]); 72 gridBagConstraints = new java.awt.GridBagConstraints (); 73 gridBagConstraints.gridx = 0; 74 gridBagConstraints.gridy = i; 75 gridBagConstraints.insets = new java.awt.Insets (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]) { jTextComponents[i] = new JTextField (); 85 ((JTextField )jTextComponents[i]).setColumns(size); 86 } else { jTextComponents[i] = new JTextArea (); 88 ((JTextArea )jTextComponents[i]).setRows(3); 89 if (i>0) jTextComponents[i].setBorder(jTextComponents[0].getBorder()); 90 } 91 gridBagConstraints = new java.awt.GridBagConstraints (); 92 gridBagConstraints.gridx = 1; 93 gridBagConstraints.gridy = i; 94 gridBagConstraints.insets = new java.awt.Insets (5, 12, 0, 11); 95 gridBagConstraints.weightx = 1.0; 96 jLabels[i].setLabelFor(jTextComponents[i]); 97 if (isTextField[i]) { 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 sp = new javax.swing.JScrollPane (jTextComponents[i]); 104 add(sp, gridBagConstraints); 105 } 106 } 107 if (customizers!=null) { 108 java.util.List buttonList = new java.util.ArrayList (); 109 int j=0; 110 for (int i=0;i<customizers.length;i++) { 111 if (customizers[i]) { 112 JButton button = new JButton (); 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 (0, 14, 0, 14)); 116 buttonList.add(button); 117 gridBagConstraints = new java.awt.GridBagConstraints (); 118 gridBagConstraints.gridx = 2; 119 gridBagConstraints.gridy = i; 120 gridBagConstraints.insets = new java.awt.Insets (3, 0, 0, 11); 121 add(button, gridBagConstraints); 122 } 123 } 124 jButtons = new JButton [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 142 public String [] getValues() { 143 if (jTextComponents==null) return null; 144 String [] values = new String [jTextComponents.length]; 145 for (int i=0;i<values.length;i++) { 146 values[i] = jTextComponents[i].getText(); 147 } 148 return values; 149 } 150 153 public JButton [] getCustomizerButtons() { 154 return jButtons; 155 } 156 159 public JTextComponent [] getTextComponents() { 160 return jTextComponents; 161 } 162 172 173 public static class DialogDescriptor { 174 String [] labels; 175 String [] initValues; 176 boolean adding; 177 boolean[] buttons; 178 boolean textField[]; 179 char[] mnem; 180 String [] a11yDesc; 181 int size; 182 183 184 187 public DialogDescriptor(String [] 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; } 195 } 196 public String [] getLabels() { 197 return labels; 198 } 199 203 public void setButtons(boolean[] buttons) { 204 this.buttons=buttons; 205 } 206 public boolean[] getButtons() { 207 return buttons; 208 } 209 211 public void setTextField(boolean[] textField) { 212 this.textField=textField; 213 } 214 public boolean[] isTextField() { 215 return textField; 216 } 217 219 public void setInitValues(String [] initValues) { 220 this.initValues=initValues; 221 adding=false; 222 } 223 public String [] getInitValues() { 224 return initValues; 225 } 226 228 public void setAdding(boolean adding) { 229 this.adding=adding; 230 } 231 public boolean isAdding() { 232 return adding; 233 } 234 236 public void setSize(int size) { 237 this.size=size; 238 } 239 public int getSize() { 240 return size; 241 } 242 244 public void setMnemonics(char[] mnem) { 245 this.mnem=mnem; 246 } 247 public char[] getMnemonics() { 248 return mnem; 249 } 250 252 public void setA11yDesc(String [] a11yDesc) { 253 this.a11yDesc=a11yDesc; 254 } 255 public String [] getA11yDesc() { 256 return a11yDesc; 257 } 258 } 259 } | Popular Tags |