4 * Copyright 2015 Eddie Berrisford-Lynch <dev@fun2be.me>.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 package uk.ac.ntu.n0521366.wsyd.client;
26 import java.awt.Color;
27 import java.lang.reflect.Array;
28 import java.text.SimpleDateFormat;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.TreeSet;
32 import javax.swing.DefaultListModel;
33 import javax.swing.JOptionPane;
34 import javax.swing.ListModel;
35 import uk.ac.ntu.n0521366.wsyd.libs.WSYD_Member;
37 * To change this license header, choose License Headers in Project Properties.
38 * To change this template file, choose Tools | Templates
39 * and open the template in the editor.
44 * @author Eddie Berrisford-Lynch
46 public class EditProfile extends javax.swing.JDialog {
49 * Creates new form WSYD_RegiProfile
51 public EditProfile(java.awt.Frame parent, boolean modal) {
57 * Populate a model with the applications pre-defined interests.
61 DefaultListModel getInterestsListModel() {
62 DefaultListModel<String> m = new DefaultListModel<>();
63 for (String i : WSYD_Member.Interests)
65 System.err.println(m.toString());
69 * This method is called from within the constructor to initialize the form.
70 * WARNING: Do NOT modify this code. The content of this method is always
71 * regenerated by the Form Editor.
73 @SuppressWarnings("unchecked")
74 // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
75 private void initComponents() {
76 java.awt.GridBagConstraints gridBagConstraints;
78 gLabelUsername = new javax.swing.JLabel();
79 gFieldUsername = new javax.swing.JTextField();
80 gButtonConfirmation = new javax.swing.JButton();
81 gLabelPassword = new javax.swing.JLabel();
82 gPFieldPassword = new javax.swing.JPasswordField();
83 gLabelPConfirmation = new javax.swing.JLabel();
84 gPFieldPConfirmation = new javax.swing.JPasswordField();
85 gLabelPWarning = new javax.swing.JLabel();
86 gLabelBirthDate = new javax.swing.JLabel();
87 gFieldBirthDate = new javax.swing.JTextField();
88 gLabelCurrLocation = new javax.swing.JLabel();
89 gFieldCurrLocation = new javax.swing.JTextField();
90 gLabelInterests = new javax.swing.JLabel();
91 jScrollPane1 = new javax.swing.JScrollPane();
92 gListInterests = new javax.swing.JList();
93 gLabelBio = new javax.swing.JLabel();
94 jScrollPane2 = new javax.swing.JScrollPane();
95 gTAreaBio = new javax.swing.JTextArea();
97 setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
98 setTitle("Your Profile");
99 setMinimumSize(new java.awt.Dimension(600, 600));
100 java.awt.GridBagLayout layout = new java.awt.GridBagLayout();
101 layout.columnWidths = new int[] {0, 5, 0, 5, 0};
102 layout.rowHeights = new int[] {0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0};
103 getContentPane().setLayout(layout);
105 gLabelUsername.setLabelFor(gFieldUsername);
106 gLabelUsername.setText("Username:");
107 gridBagConstraints = new java.awt.GridBagConstraints();
108 gridBagConstraints.gridx = 0;
109 gridBagConstraints.gridy = 0;
110 gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
111 getContentPane().add(gLabelUsername, gridBagConstraints);
113 gFieldUsername.setMinimumSize(new java.awt.Dimension(70, 25));
114 gFieldUsername.setPreferredSize(new java.awt.Dimension(150, 25));
115 gridBagConstraints = new java.awt.GridBagConstraints();
116 gridBagConstraints.gridx = 2;
117 gridBagConstraints.gridy = 0;
118 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
119 getContentPane().add(gFieldUsername, gridBagConstraints);
121 gButtonConfirmation.setText("Register");
122 gButtonConfirmation.addMouseListener(new java.awt.event.MouseAdapter() {
123 public void mouseClicked(java.awt.event.MouseEvent evt) {
124 gButtonConfirmationMouseClicked(evt);
127 gridBagConstraints = new java.awt.GridBagConstraints();
128 gridBagConstraints.gridx = 4;
129 gridBagConstraints.gridy = 0;
130 getContentPane().add(gButtonConfirmation, gridBagConstraints);
132 gLabelPassword.setLabelFor(gPFieldPassword);
133 gLabelPassword.setText("Password:");
134 gridBagConstraints = new java.awt.GridBagConstraints();
135 gridBagConstraints.gridx = 0;
136 gridBagConstraints.gridy = 2;
137 gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
138 getContentPane().add(gLabelPassword, gridBagConstraints);
140 gPFieldPassword.setMinimumSize(new java.awt.Dimension(70, 25));
141 gPFieldPassword.setPreferredSize(new java.awt.Dimension(120, 25));
142 gridBagConstraints = new java.awt.GridBagConstraints();
143 gridBagConstraints.gridx = 2;
144 gridBagConstraints.gridy = 2;
145 gridBagConstraints.gridwidth = 3;
146 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
147 getContentPane().add(gPFieldPassword, gridBagConstraints);
149 gLabelPConfirmation.setLabelFor(gPFieldPConfirmation);
150 gLabelPConfirmation.setText("Password:");
151 gLabelPConfirmation.setToolTipText("Repeat password");
152 gridBagConstraints = new java.awt.GridBagConstraints();
153 gridBagConstraints.gridx = 0;
154 gridBagConstraints.gridy = 4;
155 gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
156 getContentPane().add(gLabelPConfirmation, gridBagConstraints);
158 gPFieldPConfirmation.setMinimumSize(new java.awt.Dimension(70, 25));
159 gPFieldPConfirmation.setPreferredSize(new java.awt.Dimension(120, 25));
160 gridBagConstraints = new java.awt.GridBagConstraints();
161 gridBagConstraints.gridx = 2;
162 gridBagConstraints.gridy = 4;
163 gridBagConstraints.gridwidth = 3;
164 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
165 getContentPane().add(gPFieldPConfirmation, gridBagConstraints);
166 gridBagConstraints = new java.awt.GridBagConstraints();
167 gridBagConstraints.gridx = 4;
168 gridBagConstraints.gridy = 4;
169 gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
170 getContentPane().add(gLabelPWarning, gridBagConstraints);
172 gLabelBirthDate.setLabelFor(gFieldBirthDate);
173 gLabelBirthDate.setText("Birth Date:");
174 gridBagConstraints = new java.awt.GridBagConstraints();
175 gridBagConstraints.gridx = 0;
176 gridBagConstraints.gridy = 6;
177 gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
178 getContentPane().add(gLabelBirthDate, gridBagConstraints);
180 gFieldBirthDate.setText("YYYY-mm-dd");
181 gFieldBirthDate.setMinimumSize(new java.awt.Dimension(100, 25));
182 gFieldBirthDate.setPreferredSize(new java.awt.Dimension(200, 25));
183 gFieldBirthDate.addActionListener(new java.awt.event.ActionListener() {
184 public void actionPerformed(java.awt.event.ActionEvent evt) {
185 gFieldBirthDateActionPerformed(evt);
188 gridBagConstraints = new java.awt.GridBagConstraints();
189 gridBagConstraints.gridx = 2;
190 gridBagConstraints.gridy = 6;
191 getContentPane().add(gFieldBirthDate, gridBagConstraints);
193 gLabelCurrLocation.setLabelFor(gLabelCurrLocation);
194 gLabelCurrLocation.setText("Current City/Town:");
195 gridBagConstraints = new java.awt.GridBagConstraints();
196 gridBagConstraints.gridx = 0;
197 gridBagConstraints.gridy = 8;
198 gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
199 getContentPane().add(gLabelCurrLocation, gridBagConstraints);
201 gFieldCurrLocation.setMinimumSize(new java.awt.Dimension(100, 25));
202 gFieldCurrLocation.setPreferredSize(new java.awt.Dimension(200, 25));
203 gridBagConstraints = new java.awt.GridBagConstraints();
204 gridBagConstraints.gridx = 2;
205 gridBagConstraints.gridy = 8;
206 getContentPane().add(gFieldCurrLocation, gridBagConstraints);
208 gLabelInterests.setLabelFor(gListInterests);
209 gLabelInterests.setText("Interests:");
210 gridBagConstraints = new java.awt.GridBagConstraints();
211 gridBagConstraints.gridx = 0;
212 gridBagConstraints.gridy = 10;
213 gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
214 getContentPane().add(gLabelInterests, gridBagConstraints);
216 gListInterests.setModel(getInterestsListModel());
217 gListInterests.setToolTipText("Select the interests you have, they will be displayed (Hold CTRL + Click)");
218 jScrollPane1.setViewportView(gListInterests);
220 gridBagConstraints = new java.awt.GridBagConstraints();
221 gridBagConstraints.gridx = 2;
222 gridBagConstraints.gridy = 10;
223 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
224 getContentPane().add(jScrollPane1, gridBagConstraints);
226 gLabelBio.setLabelFor(gTAreaBio);
227 gLabelBio.setText("Biography:");
228 gridBagConstraints = new java.awt.GridBagConstraints();
229 gridBagConstraints.gridx = 0;
230 gridBagConstraints.gridy = 12;
231 gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
232 getContentPane().add(gLabelBio, gridBagConstraints);
234 gTAreaBio.setColumns(35);
235 gTAreaBio.setFont(new java.awt.Font("Dialog", 0, 10)); // NOI18N
236 gTAreaBio.setLineWrap(true);
237 gTAreaBio.setRows(12);
238 gTAreaBio.setTabSize(4);
239 gTAreaBio.setMinimumSize(new java.awt.Dimension(100, 50));
240 jScrollPane2.setViewportView(gTAreaBio);
242 gridBagConstraints = new java.awt.GridBagConstraints();
243 gridBagConstraints.gridx = 2;
244 gridBagConstraints.gridy = 12;
245 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
246 gridBagConstraints.gridheight = 5;
247 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
248 getContentPane().add(jScrollPane2, gridBagConstraints);
251 }// </editor-fold>//GEN-END:initComponents
253 private void gFieldBirthDateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gFieldBirthDateActionPerformed
254 // TODO add your handling code here:
255 }//GEN-LAST:event_gFieldBirthDateActionPerformed
258 * Verify the user-entered data before attempting to submit it to SocialServer.
260 * Provides error feedback to the user in the form of a message dialog with a list of problems
261 * and sets the colour of field data to red if an error was found, or green otherwise.
265 private void gButtonConfirmationMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_gButtonConfirmationMouseClicked
266 // TODO add your handling code here:
267 boolean dataGood = true; // assume user input is OK until errors found
268 ArrayList<String> errorMessages = new ArrayList<>();
270 String birthdateFormat = "YYYY-mm-dd";
271 SimpleDateFormat sd = new SimpleDateFormat(birthdateFormat);
273 WSYD_Member m = new WSYD_Member();
275 m._userName = this.gFieldUsername.getText();
276 if (m._userName.length() == 0) {
277 this.gFieldUsername.setForeground(Color.red);
278 errorMessages.add("Username cannot be empty");
281 this.gFieldUsername.setForeground(Color.green);
283 if (Array.getLength(this.gPFieldPassword.getPassword()) == 0 || Array.getLength(this.gPFieldPConfirmation.getPassword()) == 0) {
285 this.gPFieldPassword.setForeground(Color.red);
286 this.gPFieldPConfirmation.setForeground(Color.red);
287 errorMessages.add("Password cannot be empty");
288 } else if (!Arrays.equals(this.gPFieldPassword.getPassword(), this.gPFieldPConfirmation.getPassword())) {
290 this.gPFieldPassword.setForeground(Color.red);
291 this.gPFieldPConfirmation.setForeground(Color.red);
292 errorMessages.add("Passwords do not match");
294 this.gPFieldPassword.setForeground(Color.green);
295 this.gPFieldPConfirmation.setForeground(Color.green);
296 m._password = new String(this.gPFieldPassword.getPassword());
300 m._birthDate = sd.parse(this.gFieldBirthDate.getText());
301 this.gFieldBirthDate.setForeground(Color.green);
302 } catch(java.text.ParseException e) {
304 this.gFieldBirthDate.setForeground(Color.red);
305 errorMessages.add("Birth Date format is " + birthdateFormat);
308 m._bio = this.gTAreaBio.getText();
309 m._currentLocation = this.gFieldCurrLocation.getText();
311 java.util.List<?> selectedInterests = this.gListInterests.getSelectedValuesList();
312 if (selectedInterests.isEmpty()) {
314 this.gListInterests.setForeground(Color.red);
315 errorMessages.add("Must select at least 1 interest");
317 this.gListInterests.setForeground(Color.green);
319 m._interests = new TreeSet<>();
320 m._interests.addAll(this.gListInterests.getSelectedValuesList());
322 DefaultListModel<String> errorModel = new DefaultListModel<>();
323 for (String s: errorMessages)
324 errorModel.addElement(s);
325 javax.swing.JList errorList = new javax.swing.JList();
326 errorList.setModel(errorModel);
327 JOptionPane.showMessageDialog(this, errorList, "Errors in Registration details", JOptionPane.ERROR_MESSAGE);
330 // trigger sending of the data to SocialServer
331 ((ClientGUI)this.getOwner()).confirmRegistration(m);
333 // FIXME: gButtonConfirmationMouseClicked(): remove superceded code that has been commented out
335 TreeSet<String> interests = null;
337 for (int i = 0; i < gListInterests.getModel().getSize(); i++)
339 interests.add(((String)gListInterests.getModel().getElementAt(i)));
341 if (((ClientGUI)this.getOwner()).confirmRegistration(gFieldUsername.getText(),
342 new String(gPFieldPassword.getPassword()),
343 gFieldBirthDate.getText(),
344 gFieldCurrLocation.getText(),
346 gTAreaBio.getText())) this.dispose();
347 else gLabelPWarning.setText("Server failed to register. Retry.");
349 }//GEN-LAST:event_gButtonConfirmationMouseClicked
352 * @param args the command line arguments
354 public static void main(String args[]) {
355 /* Set the Nimbus look and feel */
356 //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
357 /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
358 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
361 for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
362 if ("Nimbus".equals(info.getName())) {
363 javax.swing.UIManager.setLookAndFeel(info.getClassName());
367 } catch (ClassNotFoundException ex) {
368 java.util.logging.Logger.getLogger(EditProfile.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
369 } catch (InstantiationException ex) {
370 java.util.logging.Logger.getLogger(EditProfile.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
371 } catch (IllegalAccessException ex) {
372 java.util.logging.Logger.getLogger(EditProfile.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
373 } catch (javax.swing.UnsupportedLookAndFeelException ex) {
374 java.util.logging.Logger.getLogger(EditProfile.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
379 /* Create and display the dialog */
380 java.awt.EventQueue.invokeLater(new Runnable() {
382 EditProfile dialog = new EditProfile(new javax.swing.JFrame(), true);
383 dialog.addWindowListener(new java.awt.event.WindowAdapter() {
385 public void windowClosing(java.awt.event.WindowEvent e) {
389 dialog.setVisible(true);
394 // Variables declaration - do not modify//GEN-BEGIN:variables
395 private javax.swing.JButton gButtonConfirmation;
396 private javax.swing.JTextField gFieldBirthDate;
397 private javax.swing.JTextField gFieldCurrLocation;
398 private javax.swing.JTextField gFieldUsername;
399 private javax.swing.JLabel gLabelBio;
400 private javax.swing.JLabel gLabelBirthDate;
401 private javax.swing.JLabel gLabelCurrLocation;
402 private javax.swing.JLabel gLabelInterests;
403 private javax.swing.JLabel gLabelPConfirmation;
404 private javax.swing.JLabel gLabelPWarning;
405 private javax.swing.JLabel gLabelPassword;
406 private javax.swing.JLabel gLabelUsername;
407 private javax.swing.JList gListInterests;
408 private javax.swing.JPasswordField gPFieldPConfirmation;
409 private javax.swing.JPasswordField gPFieldPassword;
410 private javax.swing.JTextArea gTAreaBio;
411 private javax.swing.JScrollPane jScrollPane1;
412 private javax.swing.JScrollPane jScrollPane2;
413 // End of variables declaration//GEN-END:variables