[3952] | 1 | // Created on 28-jul-2003 |
---|
| 2 | package nu.fw.jeti.jabber; |
---|
| 3 | |
---|
| 4 | import java.awt.*; |
---|
| 5 | import java.awt.event.ActionEvent; |
---|
| 6 | import java.awt.event.ActionListener; |
---|
| 7 | import java.awt.event.KeyEvent; |
---|
| 8 | import java.util.Iterator; |
---|
| 9 | import java.util.LinkedList; |
---|
| 10 | import java.util.List; |
---|
| 11 | |
---|
| 12 | import javax.swing.*; |
---|
| 13 | import javax.swing.text.JTextComponent; |
---|
| 14 | |
---|
| 15 | import nu.fw.jeti.jabber.elements.XData; |
---|
| 16 | import nu.fw.jeti.jabber.elements.XDataBuilder; |
---|
| 17 | import nu.fw.jeti.jabber.elements.XDataField; |
---|
| 18 | import nu.fw.jeti.util.I18N; |
---|
| 19 | |
---|
| 20 | /** |
---|
| 21 | * @author E.S. de Boer |
---|
| 22 | * |
---|
| 23 | */ |
---|
| 24 | public class XDataPanel extends JPanel |
---|
| 25 | { |
---|
| 26 | private boolean editable; |
---|
| 27 | private List fields = new LinkedList(); |
---|
| 28 | private XData xdata; |
---|
| 29 | private int current; |
---|
| 30 | |
---|
| 31 | public XDataPanel(XData xdata,final XDataCallback callback) |
---|
| 32 | { |
---|
| 33 | setLayout(new BorderLayout()); |
---|
| 34 | this.xdata = xdata; |
---|
| 35 | if(xdata.getType().equals("form")) |
---|
| 36 | { |
---|
| 37 | editable =true; |
---|
| 38 | JPanel panel = new JPanel(); |
---|
| 39 | JButton btnOk = new JButton(); |
---|
| 40 | I18N.setTextAndMnemonic("Send",btnOk); |
---|
| 41 | btnOk.addActionListener(new ActionListener() |
---|
| 42 | { |
---|
| 43 | public void actionPerformed(ActionEvent event) |
---|
| 44 | { |
---|
| 45 | sendForm(callback); |
---|
| 46 | } |
---|
| 47 | }); |
---|
| 48 | panel.add(btnOk); |
---|
| 49 | JButton btnCancel = new JButton(I18N.gettext("Cancel")); |
---|
| 50 | btnCancel.setMnemonic(KeyEvent.VK_ESCAPE); |
---|
| 51 | btnCancel.addActionListener(new ActionListener() |
---|
| 52 | { |
---|
| 53 | public void actionPerformed(ActionEvent event) |
---|
| 54 | { |
---|
| 55 | callback.cancelForm(); |
---|
| 56 | } |
---|
| 57 | }); |
---|
| 58 | panel.add(btnCancel); |
---|
| 59 | add(panel,BorderLayout.SOUTH); |
---|
| 60 | } |
---|
| 61 | if(xdata.getInstructions() !=null) |
---|
| 62 | { |
---|
| 63 | JTextArea area = new JTextArea(xdata.getInstructions()); |
---|
| 64 | area.setEditable(false); |
---|
| 65 | area.setBackground(SystemColor.control); |
---|
| 66 | add(area,BorderLayout.NORTH); |
---|
| 67 | } |
---|
| 68 | if(xdata.hasItems()) |
---|
| 69 | { |
---|
| 70 | createMulti(xdata.getItems()); |
---|
| 71 | } |
---|
| 72 | else if(xdata.hasFields()) |
---|
| 73 | { |
---|
| 74 | add(new JScrollPane(createFieldsPanel(xdata.getFields())),BorderLayout.CENTER); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | public Iterator getCurrentField() |
---|
| 80 | { |
---|
| 81 | if(xdata.hasItems())return ((List)xdata.getItems().get(current)).iterator(); |
---|
| 82 | return xdata.getFields(); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | //TODO use reported to make fields |
---|
| 86 | private JPanel createFieldsPanel(Iterator fields) |
---|
| 87 | { |
---|
| 88 | JPanel pnlFields = new JPanel(new GridBagLayout()); |
---|
| 89 | GridBagConstraints c = new GridBagConstraints(); |
---|
| 90 | c.gridwidth = GridBagConstraints.REMAINDER; |
---|
| 91 | c.anchor = GridBagConstraints.LINE_START; |
---|
| 92 | c.fill = GridBagConstraints.HORIZONTAL; |
---|
| 93 | c.insets = new Insets(3, 5, 0, 3); |
---|
| 94 | for(Iterator i = fields;i.hasNext();) |
---|
| 95 | { |
---|
| 96 | XDataField data = (XDataField)i.next(); |
---|
| 97 | parseField(data, pnlFields, c); |
---|
| 98 | } |
---|
| 99 | c.gridwidth = 1; |
---|
| 100 | c.weighty = 1.0; |
---|
| 101 | c.weightx = 1.0; |
---|
| 102 | pnlFields.add(Box.createVerticalGlue(), c); |
---|
| 103 | return pnlFields; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | private void createMulti(final List list) |
---|
| 107 | { |
---|
| 108 | if(list.size()>1) |
---|
| 109 | { |
---|
| 110 | JPanel panel = new JPanel(); |
---|
| 111 | final JButton btnNext = new JButton(); |
---|
| 112 | final JButton btnPrevious = new JButton(); |
---|
| 113 | I18N.setTextAndMnemonic("search.Previous", btnPrevious); |
---|
| 114 | panel.add(btnPrevious,BorderLayout.EAST); |
---|
| 115 | final JLabel title = new JLabel("1/" + list.size()); |
---|
| 116 | panel.add(title,BorderLayout.CENTER); |
---|
| 117 | btnPrevious.addActionListener(new ActionListener() |
---|
| 118 | { |
---|
| 119 | public void actionPerformed(ActionEvent e) |
---|
| 120 | { |
---|
| 121 | if(current!=0)current--; |
---|
| 122 | if(current==0)btnPrevious.setEnabled(false); |
---|
| 123 | if(current==list.size()-2)btnNext.setEnabled(true); |
---|
| 124 | title.setText(current + 1 + "/" + list.size()); |
---|
| 125 | gotoPostion(list); |
---|
| 126 | } |
---|
| 127 | }); |
---|
| 128 | I18N.setTextAndMnemonic("search.Next", btnNext); |
---|
| 129 | btnNext.addActionListener(new ActionListener() |
---|
| 130 | { |
---|
| 131 | public void actionPerformed(ActionEvent e) |
---|
| 132 | { |
---|
| 133 | if(current<list.size()-1)current++; |
---|
| 134 | if(current==list.size()-1)btnNext.setEnabled(false); |
---|
| 135 | if(current==1)btnPrevious.setEnabled(true); |
---|
| 136 | title.setText(current + 1 + "/" + list.size()); |
---|
| 137 | gotoPostion(list); |
---|
| 138 | } |
---|
| 139 | }); |
---|
| 140 | panel.add(btnNext,BorderLayout.EAST); |
---|
| 141 | add(panel, BorderLayout.NORTH); |
---|
| 142 | } |
---|
| 143 | add(new JScrollPane( |
---|
| 144 | createFieldsPanel(((List)list.get(0)).iterator())) |
---|
| 145 | ,BorderLayout.CENTER); |
---|
| 146 | // JPanel panel = new JPanel(); |
---|
| 147 | // panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS)); |
---|
| 148 | // add(panel, BorderLayout.CENTER); |
---|
| 149 | // makeForm((Map)list.get(0), panel); |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | private void gotoPostion(List list) |
---|
| 153 | { |
---|
| 154 | //Map form =(Map) list.get(current); |
---|
| 155 | //final JPanel panel = new JPanel(); |
---|
| 156 | //panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS)); |
---|
| 157 | final JPanel panel = createFieldsPanel( |
---|
| 158 | ((List)list.get(current)).iterator()); |
---|
| 159 | SwingUtilities.invokeLater(new Runnable() |
---|
| 160 | { |
---|
| 161 | public void run() |
---|
| 162 | { |
---|
| 163 | //remove(jPanel2); |
---|
| 164 | //jPanel2 = panel; |
---|
| 165 | add(panel, BorderLayout.CENTER); |
---|
| 166 | validate(); |
---|
| 167 | } |
---|
| 168 | }); |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | private void parseField(XDataField field,JPanel pnlFields, GridBagConstraints c) |
---|
| 172 | {//change to implemnt check for required fields? |
---|
| 173 | String type = field.getType(); |
---|
| 174 | String value = field.getValue(); |
---|
| 175 | String label = field.getLabel(); |
---|
| 176 | String description = field.getDescription(); |
---|
| 177 | boolean required = field.getRequired(); |
---|
| 178 | final String var = field.getVar(); |
---|
| 179 | if(type.equals("boolean")) |
---|
| 180 | { |
---|
| 181 | String label2; |
---|
| 182 | if(label != null) label2 =field.getLabel(); |
---|
| 183 | else label2 = var; |
---|
| 184 | if(required) label2 = "*" + label2; |
---|
| 185 | JCheckBox chkBox = new JCheckBox(label2); |
---|
| 186 | if("0".equals(value))chkBox.setSelected(false); |
---|
| 187 | if("1".equals(value))chkBox.setSelected(true); |
---|
| 188 | chkBox.setEnabled(editable); |
---|
| 189 | if(description !=null)chkBox.setToolTipText(description); |
---|
| 190 | pnlFields.add(chkBox, c); |
---|
| 191 | chkBox.setName(var); |
---|
| 192 | fields.add(chkBox); |
---|
| 193 | } |
---|
| 194 | else if (type.equals("fixed")) |
---|
| 195 | { |
---|
| 196 | JLabel lbl = new JLabel(value); |
---|
| 197 | lbl.setAlignmentX(0.0f); |
---|
| 198 | if(description !=null) lbl.setToolTipText(description); |
---|
| 199 | lbl.setAlignmentX(0.0f); |
---|
| 200 | pnlFields.add(lbl, c); |
---|
| 201 | } |
---|
| 202 | else if (type.equals("list-multi")) |
---|
| 203 | {//assmodel |
---|
| 204 | addLabel(label,var,description, required, pnlFields, c); |
---|
| 205 | JList list = new JList(); |
---|
| 206 | list.setEnabled(editable); |
---|
| 207 | pnlFields.add(list, c); |
---|
| 208 | list.setAlignmentX(0.0f); |
---|
| 209 | list.setName(var); |
---|
| 210 | fields.add(list); |
---|
| 211 | } |
---|
| 212 | else if (type.equals("list-single")) |
---|
| 213 | {//addcombo |
---|
| 214 | addLabel(label,var,description, required,pnlFields, c); |
---|
| 215 | if(field.hasOptions()) |
---|
| 216 | { |
---|
| 217 | Object[] values = new Object[field.getOptionsSize()]; |
---|
| 218 | int tel=0; |
---|
| 219 | int index = 0; |
---|
| 220 | for (Iterator i = field.getOptionsIterator(); i.hasNext();tel++) |
---|
| 221 | { |
---|
| 222 | Object[] obj = (Object[]) i.next(); |
---|
| 223 | values[tel] = new LabelValue(obj); |
---|
| 224 | if(((String)obj[1]).equals(value)) index = tel; |
---|
| 225 | |
---|
| 226 | } |
---|
| 227 | JComboBox cmbBox = new JComboBox(values); |
---|
| 228 | cmbBox.setSelectedIndex(index); |
---|
| 229 | cmbBox.setEditable(editable); |
---|
| 230 | cmbBox.setAlignmentX(0.0f); |
---|
| 231 | pnlFields.add(cmbBox, c); |
---|
| 232 | cmbBox.setName(var); |
---|
| 233 | fields.add(cmbBox); |
---|
| 234 | } |
---|
| 235 | } |
---|
| 236 | else if (type.equals("text-multi")) |
---|
| 237 | { |
---|
| 238 | addLabel(label,var,description, required,pnlFields, c); |
---|
| 239 | final JTextArea area = new JTextArea(value, 2, 20); |
---|
| 240 | JScrollPane scrollPane = new JScrollPane(area); |
---|
| 241 | area.setEditable(editable); |
---|
| 242 | area.setAlignmentX(0.0f); |
---|
| 243 | pnlFields.add(scrollPane, c); |
---|
| 244 | area.setName(var); |
---|
| 245 | fields.add(area); |
---|
| 246 | } |
---|
| 247 | else if (type.equals("text-private")) |
---|
| 248 | { |
---|
| 249 | addLabel(label,var,description, required,pnlFields, c); |
---|
| 250 | final JPasswordField pass = new JPasswordField(value); |
---|
| 251 | pass.setEditable(editable); |
---|
| 252 | pass.setAlignmentX(0.0f); |
---|
| 253 | pnlFields.add(pass, c); |
---|
| 254 | pass.setName(var); |
---|
| 255 | fields.add(pass); |
---|
| 256 | } |
---|
| 257 | else if (type.equals("text-single")) |
---|
| 258 | { |
---|
| 259 | addLabel(label,var,description, required, pnlFields, c); |
---|
| 260 | final JTextField text = new JTextField(value); |
---|
| 261 | text.setEditable(editable); |
---|
| 262 | text.setAlignmentX(0.0f); |
---|
| 263 | pnlFields.add(text, c); |
---|
| 264 | text.setName(var); |
---|
| 265 | fields.add(text); |
---|
| 266 | } |
---|
| 267 | else if (type.equals("jid-multi")) |
---|
| 268 | { |
---|
| 269 | addLabel(label,var,description, required, pnlFields, c); |
---|
| 270 | } |
---|
| 271 | else if (type.equals("jid-single")) |
---|
| 272 | { |
---|
| 273 | addLabel(label,var,description, required,pnlFields, c); |
---|
| 274 | } |
---|
| 275 | else if (type.equals("hidden")) |
---|
| 276 | { |
---|
| 277 | fields.add(new Object[]{var,value}); |
---|
| 278 | } |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | private void addLabel(String label,String var,String description, |
---|
| 282 | boolean required,JPanel panel, GridBagConstraints c) |
---|
| 283 | { |
---|
| 284 | String label2; |
---|
| 285 | if(label != null) label2 =label; |
---|
| 286 | else label2 = var; |
---|
| 287 | if(required) label2 = "*" + label2; |
---|
| 288 | JLabel lbl = new JLabel(label2); |
---|
| 289 | if(description !=null) lbl.setToolTipText(description); |
---|
| 290 | lbl.setAlignmentX(0.0f); |
---|
| 291 | panel.add(lbl, c); |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | private void sendForm(XDataCallback callback) |
---|
| 295 | { |
---|
| 296 | XDataBuilder builder = new XDataBuilder(); |
---|
| 297 | builder.type ="submit"; |
---|
| 298 | for (Iterator iter = fields.iterator(); iter.hasNext();) |
---|
| 299 | { |
---|
| 300 | Object element = iter.next(); |
---|
| 301 | String var = null; |
---|
| 302 | String value = null; |
---|
| 303 | if(element instanceof Object[]) |
---|
| 304 | { |
---|
| 305 | var =(String)((Object[])element)[0]; |
---|
| 306 | value =(String)((Object[])element)[1]; |
---|
| 307 | if(value != null) |
---|
| 308 | { |
---|
| 309 | builder.addField(new XDataField(var,value)); |
---|
| 310 | } |
---|
| 311 | } |
---|
| 312 | else |
---|
| 313 | { |
---|
| 314 | JComponent component = (JComponent)element; |
---|
| 315 | var = component.getName(); |
---|
| 316 | if(component instanceof JCheckBox) |
---|
| 317 | { |
---|
| 318 | value = String.valueOf(((JCheckBox)component).isSelected()?1:0); |
---|
| 319 | } |
---|
| 320 | else if(component instanceof JTextComponent) |
---|
| 321 | { |
---|
| 322 | value = ((JTextComponent)component).getText(); |
---|
| 323 | } |
---|
| 324 | else if(component instanceof JComboBox) |
---|
| 325 | { |
---|
| 326 | value =((LabelValue)((JComboBox)component).getSelectedItem()).value; |
---|
| 327 | } |
---|
| 328 | if(value != null) |
---|
| 329 | { |
---|
| 330 | builder.addField(new XDataField(var,value)); |
---|
| 331 | } |
---|
| 332 | if(component instanceof JList) |
---|
| 333 | { |
---|
| 334 | JList list = ((JList)component); |
---|
| 335 | if (!list.isSelectionEmpty()) |
---|
| 336 | { |
---|
| 337 | Object[] values = list.getSelectedValues(); |
---|
| 338 | for (int i = 0; i < values.length; i++) |
---|
| 339 | { |
---|
| 340 | builder.addField(new XDataField(var,((LabelValue)values[i]).value)); |
---|
| 341 | } |
---|
| 342 | } |
---|
| 343 | } |
---|
| 344 | } |
---|
| 345 | } |
---|
| 346 | try |
---|
| 347 | { |
---|
| 348 | callback.sendForm(builder.build()); |
---|
| 349 | } |
---|
| 350 | catch (InstantiationException e) |
---|
| 351 | { |
---|
| 352 | e.printStackTrace(); |
---|
| 353 | } |
---|
| 354 | } |
---|
| 355 | } |
---|
| 356 | |
---|
| 357 | class LabelValue |
---|
| 358 | { |
---|
| 359 | String label; |
---|
| 360 | String value; |
---|
| 361 | |
---|
| 362 | LabelValue(Object[] obj) |
---|
| 363 | { |
---|
| 364 | value = (String)obj[1]; |
---|
| 365 | if(obj[0]==null)label= value; |
---|
| 366 | else label =(String) obj[0]; |
---|
| 367 | } |
---|
| 368 | |
---|
| 369 | public String toString() |
---|
| 370 | { |
---|
| 371 | return label; |
---|
| 372 | } |
---|
| 373 | } |
---|
| 374 | |
---|
| 375 | |
---|
| 376 | |
---|
| 377 | |
---|
| 378 | /* |
---|
| 379 | * Overrides for emacs |
---|
| 380 | * Local variables: |
---|
| 381 | * tab-width: 4 |
---|
| 382 | * End: |
---|
| 383 | */ |
---|