[3952] | 1 | package nu.fw.jeti.ui; |
---|
| 2 | |
---|
| 3 | import java.awt.BorderLayout; |
---|
| 4 | import java.awt.Cursor; |
---|
| 5 | import java.awt.Dimension; |
---|
| 6 | import java.awt.event.ActionEvent; |
---|
| 7 | import java.awt.event.WindowEvent; |
---|
| 8 | import java.text.MessageFormat; |
---|
| 9 | import java.util.Iterator; |
---|
| 10 | import java.util.LinkedHashMap; |
---|
| 11 | import java.util.Map; |
---|
| 12 | |
---|
| 13 | import javax.swing.*; |
---|
| 14 | |
---|
| 15 | import nu.fw.jeti.backend.NewAccount; |
---|
| 16 | import nu.fw.jeti.jabber.Backend; |
---|
| 17 | import nu.fw.jeti.jabber.JID; |
---|
| 18 | import nu.fw.jeti.jabber.XDataCallback; |
---|
| 19 | import nu.fw.jeti.jabber.XDataPanel; |
---|
| 20 | import nu.fw.jeti.jabber.elements.IQRegister; |
---|
| 21 | import nu.fw.jeti.jabber.elements.InfoQuery; |
---|
| 22 | import nu.fw.jeti.jabber.elements.XData; |
---|
| 23 | import nu.fw.jeti.util.I18N; |
---|
| 24 | import nu.fw.jeti.util.Utils; |
---|
| 25 | |
---|
| 26 | //TODO add xdata to register constructor, update languages |
---|
| 27 | /** |
---|
| 28 | * Title: im |
---|
| 29 | * Description: |
---|
| 30 | * Copyright: Copyright (c) 2001 |
---|
| 31 | * Company: |
---|
| 32 | * @author E.S. de Boer |
---|
| 33 | * @version 1.0 |
---|
| 34 | */ |
---|
| 35 | |
---|
| 36 | public class RegisterWindow extends JFrame implements XDataCallback |
---|
| 37 | { |
---|
| 38 | private JPanel jPanel1 = new JPanel(); |
---|
| 39 | private JPanel jPanel2 = new JPanel(); |
---|
| 40 | private JButton btnOK = new JButton(); |
---|
| 41 | private JButton btnCancel = new JButton(); |
---|
| 42 | private JPasswordField txtPassword; |
---|
| 43 | private JPanel pnlPassword = new JPanel(); |
---|
| 44 | private JLabel jLabel3 = new JLabel(); |
---|
| 45 | private JPanel pnlUsername = new JPanel(); |
---|
| 46 | private JLabel jLabel2 = new JLabel(); |
---|
| 47 | private JTextField txtUsername; |
---|
| 48 | private JPanel pnlInstructions = new JPanel(); |
---|
| 49 | private JTextArea txtInstructions = new JTextArea(); |
---|
| 50 | private JLabel jLabel1 = new JLabel(); |
---|
| 51 | private String key; |
---|
| 52 | private Map map; |
---|
| 53 | private Backend backend; |
---|
| 54 | private NewAccount backend2; |
---|
| 55 | private boolean newAccount; |
---|
| 56 | private JID from; |
---|
| 57 | private JCheckBox checkBox = new JCheckBox(I18N.gettext("Remove")); |
---|
| 58 | //private String id; |
---|
| 59 | public static final int STRUT_SIZE = 5; |
---|
| 60 | |
---|
| 61 | |
---|
| 62 | public RegisterWindow(NewAccount backend,IQRegister register,String username,String password) |
---|
| 63 | { |
---|
| 64 | newAccount = true; |
---|
| 65 | Map forms = register.getFields(); |
---|
| 66 | //setIconImage(nu.fw.jeti.images.StatusIcons.getImageIcon("jeti").getImage()); |
---|
| 67 | setTitle(I18N.gettext("main.register.Register")); |
---|
| 68 | //I18N.setTextAndMnemonic("Remove",checkBox); |
---|
| 69 | //checkBox.setText("Remove"); |
---|
| 70 | map = new LinkedHashMap(15); |
---|
| 71 | this.backend2 = backend; |
---|
| 72 | key =(String) forms.remove("key"); |
---|
| 73 | try |
---|
| 74 | { |
---|
| 75 | if(forms.containsKey("instructions")) |
---|
| 76 | { |
---|
| 77 | String instructions = (String)forms.remove("instructions"); |
---|
| 78 | txtInstructions.setText(instructions); |
---|
| 79 | txtInstructions.setEnabled(false); |
---|
| 80 | jLabel1.setText(I18N.gettext("main.register.Instructions")); |
---|
| 81 | jLabel1.setPreferredSize(new Dimension(80, 17)); |
---|
| 82 | pnlInstructions.setLayout(new BorderLayout()); |
---|
| 83 | pnlInstructions.add(jLabel1, BorderLayout.WEST ); |
---|
| 84 | pnlInstructions.add(txtInstructions, BorderLayout.CENTER); |
---|
| 85 | /* |
---|
| 86 | java.awt.FontMetrics font = getFontMetrics((Font)UIManager.get("TextArea.font")); |
---|
| 87 | int naamLengte = (font.stringWidth(message)); |
---|
| 88 | if(naamLengte > 400) |
---|
| 89 | {//message is to long enable autowrap |
---|
| 90 | font.getHeight(); |
---|
| 91 | txtInstructions.setPreferredSize(new Dimension(400 )) |
---|
| 92 | txtInstructions.setLineWrap(true); |
---|
| 93 | return; |
---|
| 94 | } |
---|
| 95 | */ |
---|
| 96 | |
---|
| 97 | jPanel2.add(Box.createVerticalStrut(STRUT_SIZE)); |
---|
| 98 | jPanel2.add(pnlInstructions); |
---|
| 99 | |
---|
| 100 | } |
---|
| 101 | if(forms.containsKey("password")) |
---|
| 102 | { |
---|
| 103 | //txtPassword = new JPasswordField((String)forms.remove("password")); |
---|
| 104 | forms.remove("password"); |
---|
| 105 | txtPassword = new JPasswordField(password); |
---|
| 106 | //txtPassword.setText(); |
---|
| 107 | } |
---|
| 108 | if(forms.containsKey("username")) |
---|
| 109 | { |
---|
| 110 | //txtUsername = new JTextField((String)forms.remove("username")); |
---|
| 111 | forms.remove("username"); |
---|
| 112 | txtUsername = new JTextField(username); |
---|
| 113 | } |
---|
| 114 | jbInit(); |
---|
| 115 | if(forms.containsKey("registered")) |
---|
| 116 | { |
---|
| 117 | forms.remove("registered"); |
---|
| 118 | checkBox.setEnabled(true); |
---|
| 119 | } |
---|
| 120 | for (Iterator i=forms.entrySet().iterator(); i.hasNext(); ) { |
---|
| 121 | Map.Entry e = (Map.Entry) i.next(); |
---|
| 122 | //System.out.println(e.getKey() + ": " + e.getValue()); |
---|
| 123 | |
---|
| 124 | JTextField text; |
---|
| 125 | //if(((String)e.getKey()).equals("password")) text = new JPasswordField(); |
---|
| 126 | text = new JTextField(); |
---|
| 127 | |
---|
| 128 | //text.setPreferredSize(new Dimension(200, 21)); |
---|
| 129 | text.setText((String)e.getValue()); |
---|
| 130 | |
---|
| 131 | JPanel panel = new JPanel(); |
---|
| 132 | panel.setLayout(new BorderLayout()); |
---|
| 133 | map.put(e.getKey(),text); |
---|
| 134 | JLabel label = new JLabel((String)e.getKey()); |
---|
| 135 | label.setPreferredSize(new Dimension(80, 17)); |
---|
| 136 | panel.add(label, BorderLayout.WEST); |
---|
| 137 | panel.add(text,BorderLayout.CENTER); |
---|
| 138 | jPanel2.add(Box.createVerticalStrut(STRUT_SIZE)); |
---|
| 139 | jPanel2.add(panel); |
---|
| 140 | } |
---|
| 141 | jPanel2.add(checkBox); |
---|
| 142 | } |
---|
| 143 | catch(Exception e) |
---|
| 144 | { |
---|
| 145 | e.printStackTrace(); |
---|
| 146 | } |
---|
| 147 | pack(); |
---|
| 148 | setLocationRelativeTo(null); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | //TODO remove id |
---|
| 152 | public RegisterWindow(Backend backend,IQRegister register,JID jid,String id) |
---|
| 153 | { |
---|
| 154 | from = jid; |
---|
| 155 | //this.id = id; |
---|
| 156 | this.backend = backend; |
---|
| 157 | XData xData =register.getXData(); |
---|
| 158 | if(xData !=null) |
---|
| 159 | { |
---|
| 160 | //setIconImage(nu.fw.jeti.images.StatusIcons.getImageIcon("jeti").getImage()); |
---|
| 161 | if(xData.getTitle() !=null) setTitle(xData.getTitle()); |
---|
| 162 | else setTitle(I18N.gettext("main.register.Register")); |
---|
| 163 | setContentPane(new XDataPanel(xData,this)); |
---|
| 164 | this.addWindowListener(new java.awt.event.WindowAdapter() |
---|
| 165 | { |
---|
| 166 | public void windowClosing(WindowEvent e) |
---|
| 167 | { |
---|
| 168 | cancelForm(); |
---|
| 169 | } |
---|
| 170 | }); |
---|
| 171 | pack(); |
---|
| 172 | setLocationRelativeTo(null); |
---|
| 173 | setVisible(true); |
---|
| 174 | } |
---|
| 175 | else if(register.getFields()!=null) |
---|
| 176 | { |
---|
| 177 | oldRegister(register); |
---|
| 178 | } |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | private void oldRegister(IQRegister register) |
---|
| 182 | { |
---|
| 183 | newAccount = false; |
---|
| 184 | Map forms = register.getFields(); |
---|
| 185 | //setIconImage(nu.fw.jeti.images.StatusIcons.getImageIcon("jeti").getImage()); |
---|
| 186 | setTitle(I18N.gettext("main.register.Register")); |
---|
| 187 | map = new LinkedHashMap(15); |
---|
| 188 | |
---|
| 189 | key =(String) forms.remove("key"); |
---|
| 190 | |
---|
| 191 | try |
---|
| 192 | { |
---|
| 193 | if(forms.containsKey("instructions")) |
---|
| 194 | { |
---|
| 195 | String instructions = (String)forms.remove("instructions"); |
---|
| 196 | txtInstructions.setText(instructions); |
---|
| 197 | txtInstructions.setEnabled(false); |
---|
| 198 | jLabel1.setText(I18N.gettext("main.register.Instructions")); |
---|
| 199 | jLabel1.setPreferredSize(new Dimension(80, 17)); |
---|
| 200 | pnlInstructions.setLayout(new BorderLayout()); |
---|
| 201 | pnlInstructions.add(jLabel1, BorderLayout.WEST ); |
---|
| 202 | pnlInstructions.add(txtInstructions, BorderLayout.CENTER); |
---|
| 203 | /* |
---|
| 204 | java.awt.FontMetrics font = getFontMetrics((Font)UIManager.get("TextArea.font")); |
---|
| 205 | int naamLengte = (font.stringWidth(message)); |
---|
| 206 | if(naamLengte > 400) |
---|
| 207 | {//message is to long enable autowrap |
---|
| 208 | font.getHeight(); |
---|
| 209 | txtInstructions.setPreferredSize(new Dimension(400 )) |
---|
| 210 | txtInstructions.setLineWrap(true); |
---|
| 211 | return; |
---|
| 212 | } |
---|
| 213 | */ |
---|
| 214 | |
---|
| 215 | jPanel2.add(Box.createVerticalStrut(STRUT_SIZE)); |
---|
| 216 | jPanel2.add(pnlInstructions); |
---|
| 217 | |
---|
| 218 | } |
---|
| 219 | if(forms.containsKey("password")) |
---|
| 220 | { |
---|
| 221 | txtPassword = new JPasswordField((String)forms.remove("password")); |
---|
| 222 | //txtPassword.setText(); |
---|
| 223 | } |
---|
| 224 | if(forms.containsKey("username")) |
---|
| 225 | { |
---|
| 226 | txtUsername = new JTextField((String)forms.remove("username")); |
---|
| 227 | } |
---|
| 228 | jbInit(); |
---|
| 229 | if(forms.containsKey("registered")) |
---|
| 230 | { |
---|
| 231 | forms.remove("registered"); |
---|
| 232 | checkBox.setEnabled(true); |
---|
| 233 | } |
---|
| 234 | for (Iterator i=forms.entrySet().iterator(); i.hasNext(); ) { |
---|
| 235 | Map.Entry e = (Map.Entry) i.next(); |
---|
| 236 | // System.out.println(e.getKey() + ": " + e.getValue()); |
---|
| 237 | |
---|
| 238 | JTextField text; |
---|
| 239 | //if(((String)e.getKey()).equals("password")) text = new JPasswordField(); |
---|
| 240 | text = new JTextField(); |
---|
| 241 | |
---|
| 242 | //text.setPreferredSize(new Dimension(200, 21)); |
---|
| 243 | text.setText((String)e.getValue()); |
---|
| 244 | |
---|
| 245 | JPanel panel = new JPanel(); |
---|
| 246 | panel.setLayout(new BorderLayout()); |
---|
| 247 | map.put(e.getKey(),text); |
---|
| 248 | JLabel label = new JLabel((String)e.getKey()); |
---|
| 249 | label.setPreferredSize(new Dimension(80, 17)); |
---|
| 250 | panel.add(label, BorderLayout.WEST); |
---|
| 251 | panel.add(text,BorderLayout.CENTER); |
---|
| 252 | jPanel2.add(Box.createVerticalStrut(STRUT_SIZE)); |
---|
| 253 | jPanel2.add(panel); |
---|
| 254 | } |
---|
| 255 | jPanel2.add(checkBox); |
---|
| 256 | } |
---|
| 257 | catch(Exception e) |
---|
| 258 | { |
---|
| 259 | e.printStackTrace(); |
---|
| 260 | } |
---|
| 261 | pack(); |
---|
| 262 | setLocationRelativeTo(null); |
---|
| 263 | setVisible(true); |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | |
---|
| 267 | private void jbInit() throws Exception |
---|
| 268 | { |
---|
| 269 | getRootPane().setDefaultButton(btnOK); |
---|
| 270 | I18N.setTextAndMnemonic("OK",btnOK); |
---|
| 271 | btnOK.addActionListener(new java.awt.event.ActionListener() |
---|
| 272 | { |
---|
| 273 | public void actionPerformed(ActionEvent e) |
---|
| 274 | { |
---|
| 275 | btnOK_actionPerformed(e); |
---|
| 276 | } |
---|
| 277 | }); |
---|
| 278 | Action cancelAction = new AbstractAction(I18N.gettext("Cancel")) |
---|
| 279 | { |
---|
| 280 | public void actionPerformed(ActionEvent e) |
---|
| 281 | { |
---|
| 282 | btnCancel_actionPerformed(e); |
---|
| 283 | } |
---|
| 284 | }; |
---|
| 285 | btnCancel.setAction(cancelAction); |
---|
| 286 | Utils.addCancelButton(this, btnCancel, cancelAction); |
---|
| 287 | |
---|
| 288 | //txtInstructions.setPreferredSize(new Dimension(200, 17)); |
---|
| 289 | checkBox.setEnabled(false); |
---|
| 290 | |
---|
| 291 | //txtUsername.setPreferredSize(new Dimension(200, 21)); |
---|
| 292 | //txtPassword.setPreferredSize(new Dimension(200, 21)); |
---|
| 293 | |
---|
| 294 | if(txtUsername!=null) |
---|
| 295 | { |
---|
| 296 | jLabel2.setPreferredSize(new Dimension(80, 17)); |
---|
| 297 | jLabel2.setText(I18N.gettext("main.register.Username")); |
---|
| 298 | pnlUsername.setLayout(new BorderLayout()); |
---|
| 299 | pnlUsername.add(jLabel2, BorderLayout.WEST); |
---|
| 300 | pnlUsername.add(txtUsername, BorderLayout.CENTER); |
---|
| 301 | jPanel2.add(Box.createVerticalStrut(STRUT_SIZE)); |
---|
| 302 | jPanel2.add(pnlUsername); |
---|
| 303 | } |
---|
| 304 | if(txtPassword!=null) |
---|
| 305 | { |
---|
| 306 | jLabel3.setPreferredSize(new Dimension(80, 17)); |
---|
| 307 | jLabel3.setText(I18N.gettext("main.register.Password")); |
---|
| 308 | pnlPassword.setLayout(new BorderLayout()); |
---|
| 309 | pnlPassword.add(jLabel3, BorderLayout.WEST); |
---|
| 310 | pnlPassword.add(txtPassword, BorderLayout.CENTER); |
---|
| 311 | jPanel2.add(Box.createVerticalStrut(STRUT_SIZE)); |
---|
| 312 | jPanel2.add(pnlPassword); |
---|
| 313 | } |
---|
| 314 | |
---|
| 315 | //jPanel2.setLayout(new VerticalLayout(5,VerticalLayout.BOTH)); |
---|
| 316 | jPanel2.setLayout(new BoxLayout(jPanel2,BoxLayout.Y_AXIS)); |
---|
| 317 | this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
---|
| 318 | this.getContentPane().add(jPanel2, BorderLayout.CENTER); |
---|
| 319 | jPanel1.add(btnOK, null); |
---|
| 320 | jPanel1.add(btnCancel, null); |
---|
| 321 | this.getContentPane().add(jPanel1, BorderLayout.SOUTH); |
---|
| 322 | this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | void btnCancel_actionPerformed(ActionEvent e) |
---|
| 326 | { |
---|
| 327 | this.dispose(); |
---|
| 328 | } |
---|
| 329 | |
---|
| 330 | void btnOK_actionPerformed(ActionEvent e) |
---|
| 331 | { |
---|
| 332 | setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); |
---|
| 333 | btnOK.setEnabled(false); |
---|
| 334 | btnCancel.setEnabled(false); |
---|
| 335 | |
---|
| 336 | LinkedHashMap tmap = new LinkedHashMap(16); |
---|
| 337 | if(txtUsername!=null) |
---|
| 338 | { |
---|
| 339 | tmap.put("username",txtUsername.getText().trim()); |
---|
| 340 | } |
---|
| 341 | if(txtPassword!=null) |
---|
| 342 | { |
---|
| 343 | tmap.put("password",txtPassword.getText()); |
---|
| 344 | } |
---|
| 345 | for (Iterator i=map.entrySet().iterator(); i.hasNext(); ) |
---|
| 346 | { |
---|
| 347 | Map.Entry entry = (Map.Entry) i.next(); |
---|
| 348 | String value = ((javax.swing.JTextField)entry.getValue()).getText().trim(); |
---|
| 349 | if(value .equals("")) continue; |
---|
| 350 | tmap.put(entry.getKey(),value); |
---|
| 351 | } |
---|
| 352 | tmap.put("key",key); |
---|
| 353 | if(newAccount) |
---|
| 354 | { |
---|
| 355 | backend2.sendRegister(new IQRegister(false,tmap),this); |
---|
| 356 | } |
---|
| 357 | else |
---|
| 358 | { |
---|
| 359 | //if(checkBox.isSelected()) backend.sendRemove(from,key); |
---|
| 360 | //else backend.sendRegister(map,from,key); |
---|
| 361 | if(checkBox.isSelected()) backend.send(new InfoQuery(from,"set",new IQRegister(true,null))); |
---|
| 362 | else backend.send(new InfoQuery(from,"set",new IQRegister(false,tmap))); |
---|
| 363 | this.dispose(); |
---|
| 364 | } |
---|
| 365 | } |
---|
| 366 | |
---|
| 367 | public void sendForm(XData xdata) |
---|
| 368 | { |
---|
| 369 | backend.send(new InfoQuery(from,"set",new IQRegister(xdata))); |
---|
| 370 | this.dispose(); |
---|
| 371 | } |
---|
| 372 | |
---|
| 373 | public void cancelForm() |
---|
| 374 | { |
---|
| 375 | backend.send(new InfoQuery(from,"set",new IQRegister(new XData("cancel")))); |
---|
| 376 | this.dispose(); |
---|
| 377 | } |
---|
| 378 | |
---|
| 379 | public void login() |
---|
| 380 | { |
---|
| 381 | //setVisible(false); |
---|
| 382 | nu.fw.jeti.util.Popups.messagePopup(I18N.gettext("main.register.registration_succeded"),I18N.gettext("main.register.Register")); |
---|
| 383 | this.dispose(); |
---|
| 384 | } |
---|
| 385 | |
---|
| 386 | public void error(String errorCode) |
---|
| 387 | { |
---|
| 388 | setVisible(true); |
---|
| 389 | setCursor(Cursor.getDefaultCursor()); |
---|
| 390 | nu.fw.jeti.util.Popups.errorPopup(MessageFormat.format(I18N.gettext("main.error.registration_aborted,_code_{0}"), new Object[] { errorCode }),I18N.gettext("main.register.Register")); |
---|
| 391 | btnOK.setEnabled(true); |
---|
| 392 | btnCancel.setEnabled(true); |
---|
| 393 | } |
---|
| 394 | } |
---|
| 395 | /* |
---|
| 396 | * Overrides for emacs |
---|
| 397 | * Local variables: |
---|
| 398 | * tab-width: 4 |
---|
| 399 | * End: |
---|
| 400 | */ |
---|