1 19 package org.lucane.applications.userprofile; 20 21 import org.lucane.client.*; 22 import org.lucane.client.widgets.*; 23 import org.lucane.common.*; 24 import org.lucane.common.net.ObjectConnection; 25 import java.awt.*; 26 import java.awt.event.*; 27 import java.util.Collections ; 28 import java.util.StringTokenizer ; 29 30 import javax.swing.*; 31 32 33 public class UserProfilePlugin extends StandalonePlugin 34 implements ActionListener 35 { 36 private ManagedWindow window; 38 private JTextField realName; 39 private JTextField mailAddress; 40 private JComboBox language; 41 private JButton btnChange; 42 43 private ConnectInfo service; 45 46 49 public UserProfilePlugin() 50 { 51 } 52 53 59 public Plugin newInstance(ConnectInfo[] friends) 60 { 61 return new UserProfilePlugin(); 62 } 63 64 67 public void start() 68 { 69 this.service = Communicator.getInstance().getConnectInfo(getName()); 70 ProfileInfo info = null; 71 try { 72 info = getProfile(); 73 } catch (Exception e) { 74 DialogBox.error(tr("err.unableToFetchUserProfile")); 75 exit(); 76 return; 77 } 78 79 window = new ManagedWindow(this, getTitle()); 80 window.setName("window"); 81 window.setExitPluginOnClose(true); 82 83 window.getContentPane().setLayout(new GridLayout(0, 2)); 84 realName = new JTextField(info.getRealName()); 85 mailAddress = new JTextField(info.getMailAddress()); 86 87 StringTokenizer stk = new StringTokenizer (tr("all.languages"), "| "); 88 Object [] langs = Collections.list(stk).toArray(); 89 language = new JComboBox(langs); 90 language.setRenderer(new LanguageCellRenderer(this)); 91 language.setSelectedItem(info.getLanguage()); 92 93 btnChange = new JButton(tr("btn.change"), Client.getImageIcon("ok.png")); 94 btnChange.addActionListener(this); 95 96 window.getContentPane().add(new JLabel(tr("lbl.realName"))); 97 window.getContentPane().add(realName); 98 window.getContentPane().add(new JLabel(tr("lbl.mailAddress"))); 99 window.getContentPane().add(mailAddress); 100 window.getContentPane().add(new JLabel(tr("lbl.language"))); 101 window.getContentPane().add(language); 102 window.getContentPane().add(new JLabel("")); 103 window.getContentPane().add(btnChange); 104 window.show(); 105 } 106 107 public void actionPerformed(ActionEvent ae) 108 { 109 try { 110 setProfile(); 111 window.dispose(); 112 exit(); 113 } catch(Exception e) { 114 DialogBox.error(tr("err.unableToChangeProfile")); 115 } 116 } 117 118 public ImageIcon getFlag(String lang) 119 { 120 return getImageIcon("flags/" +lang + ".png"); 121 } 122 123 private ProfileInfo getProfile() 124 throws Exception 125 { 126 UserProfileAction action = new UserProfileAction(UserProfileAction.GET_PROFILE); 127 128 ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action); 129 String ack = oc.readString(); 130 if(!ack.equals("OK")) 131 { 132 oc.close(); 133 throw new Exception (ack); 134 } 135 136 ProfileInfo pi = (ProfileInfo)oc.read(); 137 oc.close(); 138 139 return pi; 140 } 141 142 private void setProfile() 143 throws Exception 144 { 145 ProfileInfo pi = new ProfileInfo(realName.getText(), mailAddress.getText(), 146 (String )language.getSelectedItem()); 147 UserProfileAction action = new UserProfileAction(UserProfileAction.SET_PROFILE, pi); 148 149 ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action); 150 String ack = oc.readString(); 151 oc.close(); 152 153 if(!ack.equals("OK")) 154 throw new Exception (ack); 155 } 156 } 157 | Popular Tags |