avatar

936

Show a pretty icon for success/failure, improve the account info recognition, use Id and Key instead of email and password, and clean up the configuration dialog.

by mrs, 30 May, 2009 11:10 AM
840 936  
2020 public class JConfigMyJBidwatcherTab extends JConfigTab {
2121   private JCheckBox mEnable;
2222   private JTextField mEmail;
23   private JPasswordField mPassword;
24   private JTextField mUserId;
23   private JTextField mPassword;
2524   private JButton mCreateOrUpdate;
2625   private final OptionUI mOui = new OptionUI();
2726 
------
3332     JConfig.setConfiguration("my.jbidwatcher.enabled", Boolean.toString(enabled));
3433 
3534     String email = mEmail.getText();
36     char[] rawPassword = mPassword.getPassword();
35     String password = mPassword.getText();
3736 
38     if(email != null && rawPassword != null) {
39       String password = new String(rawPassword);
40       if(MyJBidwatcher.getInstance().login(email, password)) {
41         JConfig.setConfiguration("my.jbidwatcher.email", email);
42         JConfig.setConfiguration("my.jbidwatcher.password", password);
37     if(email != null && password != null) {
38       if(MyJBidwatcher.getInstance().getAccountInfo()) {
39         JConfig.setConfiguration("my.jbidwatcher.id", email);
40         JConfig.setConfiguration("my.jbidwatcher.key", password);
4341       }
4442     }
4543 
------
4745   }
4846 
4947   public void updateValues() {
50     String email = JConfig.queryConfiguration("my.jbidwatcher.email", "");
51     String pass = JConfig.queryConfiguration("my.jbidwatcher.password", "");
48     String email = JConfig.queryConfiguration("my.jbidwatcher.id", "");
49     String pass = JConfig.queryConfiguration("my.jbidwatcher.key", "");
5250     boolean enabled = JConfig.queryConfiguration("my.jbidwatcher.enabled", "false").equals("true");
53     String id = JConfig.queryConfiguration("my.jbidwatcher.id");
51 //    String id = JConfig.queryConfiguration("my.jbidwatcher.id");
5452 
5553     mEmail.setText(email);
5654     mPassword.setText(pass);
5755     mEnable.setSelected(enabled);
5856 
59     if(id != null && id.length() != 0) {
60       mUserId.setText(id);
61       mCreateOrUpdate.setText("Update");
62     } else {
63       mUserId.setText("");
64       mCreateOrUpdate.setText("Create");
65     }
66 
6757     for(ActionListener al : mEnable.getActionListeners()) {
6858       al.actionPerformed(new ActionEvent(mEnable, ActionEvent.ACTION_PERFORMED, "Redraw"));
6959     }
------
7464     comp.getAccessibleContext().setAccessibleDescription(text);
7565   }
7666 
67   private static final ImageIcon successIcon = new ImageIcon(JConfig.getResource("/icons/status_green_16.png"));
68   private static final ImageIcon failIcon = new ImageIcon(JConfig.getResource("/icons/status_red_16.png"));
69 
7770   private JPanel buildUserSettings() {
7871     JPanel jp = new JPanel(new BorderLayout());
7972     jp.setBorder(BorderFactory.createTitledBorder("My JBidwatcher User Settings"));
------
8982     innerPanel.add(emailLabel);
9083     innerPanel.add(mEmail);
9184 
92     mPassword = new JPasswordField();
85     mPassword = new JTextField();
9386     mPassword.addMouseListener(JPasteListener.getInstance());
94     setComponentTooltip(mPassword, "Password to use on My JBidwatcher (NOT the same as your eBay password!)");
95     final JLabel passwordLabel = new JLabel("My JBid Password:");
87     setComponentTooltip(mPassword, "My JBidwatcher access key");
88     final JLabel passwordLabel = new JLabel("Access Key:");
9689     passwordLabel.setLabelFor(mPassword);
9790     innerPanel.add(passwordLabel);
9891     innerPanel.add(mPassword);
9992 
100     innerPanel.add(new JLabel(""));
101     final JLabel mWarningMessage = new JLabel("This should not be the same as your eBay password!", JLabel.RIGHT);
102     mWarningMessage.setFont(mWarningMessage.getFont().deriveFont(Font.ITALIC | Font.BOLD));
10393     Box button = Box.createHorizontalBox();
104     mCreateOrUpdate = new JButton("");
94     final JLabel statusLabel = new JLabel("");
95     mCreateOrUpdate = new JButton("Test Access");
10596     mCreateOrUpdate.addActionListener(new ActionListener() {
10697       public void actionPerformed(ActionEvent event) {
10798         String action = event.getActionCommand();
10899         if(action == null) return;
109100 
110         boolean create = action.equals("Create");
111         boolean update = action.equals("Update");
101         String oldId = JConfig.queryConfiguration("my.jbidwatcher.id");
102         String oldKey= JConfig.queryConfiguration("my.jbidwatcher.key");
112103 
113         if(create) {
114           createNewUser();
115         } else if(update) {
116           updateUser();
104         JConfig.setConfiguration("my.jbidwatcher.id", mEmail.getText());
105         JConfig.setConfiguration("my.jbidwatcher.key", mPassword.getText());
106 
107         if(MyJBidwatcher.getInstance().getAccountInfo()) {
108           statusLabel.setIcon(successIcon);
109           statusLabel.setText("success!");
117110         } else {
118           System.err.println("Unknown action command: " + action);
111           statusLabel.setIcon(failIcon);
112           statusLabel.setText("failed.");
119113         }
114 
115         JConfig.setConfiguration("my.jbidwatcher.id", oldId);
116         JConfig.setConfiguration("my.jbidwatcher.key", oldKey);
120117       }
121118     });
122119     button.add(mCreateOrUpdate);
123     button.add(Box.createHorizontalGlue());
124     button.add(mWarningMessage);
120     button.add(statusLabel);
121     innerPanel.add(new JLabel(""));
125122     innerPanel.add(button);
126123 
127     mUserId = new JTextField();
128     mUserId.setEditable(false);
129     JLabel userIdLabel = new JLabel("My JBidwatcher Id:");
130     userIdLabel.setEnabled(false);
131     userIdLabel.setLabelFor(mUserId);
132     innerPanel.add(userIdLabel);
133     innerPanel.add(mUserId);
124     SpringUtilities.makeCompactGrid(innerPanel, 3, 2, 6, 6, 6, 1);
134125 
135     SpringUtilities.makeCompactGrid(innerPanel, 4, 2, 6, 6, 6, 1);
136 
137126     mEnable = new JCheckBox();
138127     mEnable.addActionListener(new ActionListener() {
139128       public void actionPerformed(ActionEvent event) {
------
148137         mPassword.setEditable(selected);
149138 
150139         mCreateOrUpdate.setEnabled(selected);
151         mWarningMessage.setEnabled(selected);
152         mUserId.setEnabled(selected);
153140       }
154141     });
155142     JLabel enableLabel = new JLabel("Enable My JBidwatcher");
------
163150 
164151   private void updateUser() {
165152     final String email = mEmail.getText();
166     final String password = new String(mPassword.getPassword());
153     final String password = mPassword.getText();
167154 
168155     final boolean success = MyJBidwatcher.getInstance().updateAccount(email, password);
169156     final String message = success ? "Account updated to:\nEmail: " + email + "\nPassword: " + password : "Account update failed.";
------
178165 
179166   private void createNewUser() {
180167     String email = mEmail.getText();
181     String password = new String(mPassword.getPassword());
168     String password = mPassword.getText();
182169     final boolean success = MyJBidwatcher.getInstance().createAccount(email, password);
183170 
184171     final JPanel panel = this;