KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > userprofile > UserProfilePlugin


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

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 JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29
30 import javax.swing.*;
31
32
33 public class UserProfilePlugin extends StandalonePlugin
34 implements ActionListener
35 {
36     // dialog components
37
private ManagedWindow window;
38     private JTextField realName;
39     private JTextField mailAddress;
40     private JComboBox language;
41     private JButton btnChange;
42     
43     // network stuff
44
private ConnectInfo service;
45     
46     /**
47      * Void contructor. Used by PluginManager
48      */

49     public UserProfilePlugin()
50     {
51     }
52     
53     /**
54      * @param friends the Client the Plugin belongs to
55      * @param starter is true if the Clients started the plugin (start() will be
56      * called instead of follow()
57      * @return a new instance of the Plugin.
58      */

59     public Plugin newInstance(ConnectInfo[] friends)
60     {
61         return new UserProfilePlugin();
62     }
63     
64     /**
65      * Show a dialog asking for the friend name
66      */

67     public void start()
68     {
69         this.service = Communicator.getInstance().getConnectInfo(getName());
70         ProfileInfo info = null;
71         try {
72             info = getProfile();
73         } catch (Exception JavaDoc 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 JavaDoc stk = new StringTokenizer JavaDoc(tr("all.languages"), "| ");
88         Object JavaDoc[] 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 JavaDoc e) {
114             DialogBox.error(tr("err.unableToChangeProfile"));
115         }
116     }
117     
118     public ImageIcon getFlag(String JavaDoc lang)
119     {
120         return getImageIcon("flags/" +lang + ".png");
121     }
122     
123     private ProfileInfo getProfile()
124     throws Exception JavaDoc
125     {
126         UserProfileAction action = new UserProfileAction(UserProfileAction.GET_PROFILE);
127         
128         ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action);
129         String JavaDoc ack = oc.readString();
130         if(!ack.equals("OK"))
131         {
132             oc.close();
133             throw new Exception JavaDoc(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 JavaDoc
144     {
145         ProfileInfo pi = new ProfileInfo(realName.getText(), mailAddress.getText(),
146                 (String JavaDoc)language.getSelectedItem());
147         UserProfileAction action = new UserProfileAction(UserProfileAction.SET_PROFILE, pi);
148         
149         ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action);
150         String JavaDoc ack = oc.readString();
151         oc.close();
152
153         if(!ack.equals("OK"))
154             throw new Exception JavaDoc(ack);
155     }
156 }
157
Popular Tags