001    /*
002     Copyright (C) 2003 Adam Olsen
003     
004     This program is free software; you can redistribute it and/or modify
005     it under the terms of the GNU General Public License as published by
006     the Free Software Foundation; either version 1, or (at your option)
007     any later version.
008     
009     This program is distributed in the hope that it will be useful,
010     but WITHOUT ANY WARRANTY; without even the implied warranty of
011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     GNU General Public License for more details.
013     
014     You should have received a copy of the GNU General Public License
015     along with this program; if not, write to the Free Software
016     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
017     */
018    
019    package com.valhalla.jbother.menus;
020    
021    import java.awt.Component;
022    import java.awt.event.ActionEvent;
023    import java.awt.event.ActionListener;
024    import java.awt.event.KeyEvent;
025    import java.util.Locale;
026    import java.util.ResourceBundle;
027    
028    import javax.swing.JMenu;
029    import javax.swing.JMenuItem;
030    import javax.swing.JOptionPane;
031    import javax.swing.JPopupMenu;
032    
033    import org.jivesoftware.smack.packet.Presence;
034    
035    import com.valhalla.gui.Standard;
036    import com.valhalla.jbother.AddBuddyDialog;
037    import com.valhalla.jbother.BuddyList;
038    import com.valhalla.jbother.ConnectorThread;
039    import com.valhalla.jbother.InformationViewerDialog;
040    import com.valhalla.jbother.JBother;
041    import com.valhalla.jbother.MessageDelegator;
042    import com.valhalla.jbother.MessagePanel;
043    import com.valhalla.jbother.ProfileEditorDialog;
044    import com.valhalla.jbother.ProfileManager;
045    import com.valhalla.jbother.RegistrationForm;
046    import com.valhalla.jbother.SearchDialog;
047    import com.valhalla.jbother.ServiceDiscoveryDialog;
048    import com.valhalla.jbother.groupchat.GroupChatBookmarks;
049    import com.valhalla.settings.Settings;
050    
051    /**
052     * Buddies menu - contains all the other menus - excluding the help menu
053     *
054     * @author Adam Olsen
055     * @author Andrey Zakirov
056     * @version 1.0
057     */
058    public class BuddyListBuddiesMenu extends JPopupMenu {
059        private BuddyList blist;
060        
061        private ResourceBundle resources = ResourceBundle.getBundle(
062        "JBotherBundle", Locale.getDefault());
063        
064        private JMenuItem addBuddyItem = new JMenuItem(resources
065        .getString("addBuddy"));
066        
067        private JMenuItem signOnItem = new JMenuItem(resources.getString("signOn"));
068        
069        private JMenuItem quitItem = new JMenuItem(resources
070        .getString("quitButton"));
071        
072        private JMenu servicesMenu = new JMenu(resources
073        .getString("jabberServices"));
074        
075        private JMenuItem joinChatItem = new JMenuItem(resources
076        .getString("joinGroupChat"));
077        
078        private JMenuItem discoItem = new JMenuItem(resources
079        .getString("serviceDiscovery"));
080        
081        private JMenuItem blankItem = new JMenuItem(resources
082        .getString("blankMessage"));
083        
084        private JMenuItem registerItem = new JMenuItem(resources
085        .getString("registerForService"));
086        
087        private JMenuItem searchItem = new JMenuItem(resources.getString("userSearch"));
088        
089        private BuddyListOptionsMenu optionsMenu = new BuddyListOptionsMenu();
090        
091        private JMenuItem switchItem = new JMenuItem(resources
092        .getString("switchProfile"));
093        
094        private JMenuItem editItem = new JMenuItem(resources
095        .getString("editAccount"));
096        
097        private JMenuItem infoItem = new JMenuItem(resources
098        .getString("editInformation"));
099        
100        private JMenu profileMenu = new JMenu("Profile");
101        
102        
103        
104        /**
105         * Creates the buddies menu
106         *
107         * @param blist
108         *            the buddy list to attach this menu to
109         */
110        public BuddyListBuddiesMenu(BuddyList blist) {
111            super("JBother");
112            
113            if (System.getProperty("mrj.version") != null) {
114    //            setText(resources.getString("actions"));
115    //            setMnemonic(KeyEvent.VK_A);
116            } else {
117    //            setMnemonic(KeyEvent.VK_J);
118            }
119            
120            addBuddyItem.setMnemonic(KeyEvent.VK_A);
121            quitItem.setMnemonic(KeyEvent.VK_Q);
122            signOnItem.setMnemonic(KeyEvent.VK_L);
123            servicesMenu.setMnemonic(KeyEvent.VK_J);
124            joinChatItem.setMnemonic(KeyEvent.VK_G);
125            blankItem.setMnemonic(KeyEvent.VK_B);
126            switchItem.setMnemonic(KeyEvent.VK_S);
127            editItem.setMnemonic(KeyEvent.VK_E);
128            optionsMenu.setMnemonic(KeyEvent.VK_O);
129            profileMenu.setMnemonic(KeyEvent.VK_E);
130            discoItem.setMnemonic(KeyEvent.VK_S);
131            registerItem.setMnemonic(KeyEvent.VK_R);
132            
133            this.blist = blist;
134            
135            initComponents();
136        }
137        
138        public void logOn() {
139            signOnItem.setText(resources.getString("signOff"));
140        }
141        
142        public void logOff() {
143            signOnItem.setText(resources.getString("signOn"));
144        }
145        
146        /**
147         * Sets up the visual components
148         */
149        private void initComponents() {
150            MenuActionListener listener = new MenuActionListener();
151            
152            if (!JBother.kiosk_mode) {
153                joinChatItem.addActionListener(new MenuActionListener());
154                
155                addBuddyItem.addActionListener(listener);
156                blankItem.addActionListener(listener);
157                signOnItem.addActionListener(listener);
158                registerItem.addActionListener(listener);
159                discoItem.addActionListener(listener);
160                infoItem.addActionListener(listener);
161                switchItem.addActionListener(listener);
162                searchItem.addActionListener(listener);
163                
164                editItem.addActionListener(listener);
165                
166                add(addBuddyItem);
167                
168                addSeparator();
169                
170                add(blankItem);
171                add(joinChatItem);
172                
173                addSeparator();
174                
175                servicesMenu.add(discoItem);
176                servicesMenu.add(registerItem);
177                servicesMenu.add(searchItem);
178                
179                add(servicesMenu);
180                
181                profileMenu.add(switchItem);
182                profileMenu.add(editItem);
183                profileMenu.add(infoItem);
184                add(profileMenu);
185                
186                
187                add(optionsMenu);
188                if (System.getProperty("mrj.version") == null) {
189                    add(new BuddyListHelpMenu());
190                }
191                addSeparator();
192                add(signOnItem);
193            }
194            
195            quitItem.addActionListener(listener);
196            add(quitItem);
197        }
198        
199        /**
200         * @param var
201         *            true if this is OS X
202         */
203        public void setOSX(boolean var) {
204            if (var) {
205                remove(quitItem);
206            } else {
207    //            if (!isMenuComponent(quitItem))
208    //                add(quitItem);
209            }
210            
211            optionsMenu.setOSX(var);
212        }
213        
214        /**
215         * Listens for an item to be clicked
216         *
217         * @author Adam Olsen
218         * @version 1.0
219         */
220        class MenuActionListener implements ActionListener {
221            public void actionPerformed(ActionEvent e) {
222                if (e.getSource() == quitItem)
223                    blist.quitHandler();
224                else if (e.getSource() == switchItem)
225                    switchHandler();
226                else if (e.getSource() == editItem)
227                    editHandler();
228                else if (e.getSource() == signOnItem)
229                    signOnHandler();
230                else {
231                    if (!BuddyList.getInstance().checkConnection()) {
232                        BuddyList.getInstance().connectionError();
233                        return;
234                    }
235                    
236                    if (e.getSource() == addBuddyItem)
237                        new AddBuddyDialog().setVisible(true);
238                    else if (e.getSource() == blankItem) {
239                        MessagePanel panel = new MessagePanel();
240                        MessageDelegator.getInstance().showPanel(panel);
241                        MessageDelegator.getInstance().frontFrame(panel);
242                    } else if (e.getSource() == joinChatItem) {
243                        GroupChatBookmarks gc = new GroupChatBookmarks(BuddyList.getInstance()
244                        .getTabFrame());
245                        gc.load();
246                        gc.setVisible(true);
247                    } else if (e.getSource() == registerItem)
248                        registrationHandler();
249                    else if (e.getSource() == discoItem)
250                        new ServiceDiscoveryDialog(blist.getContainerFrame())
251                        .setVisible(true);
252                    else if (e.getSource() == infoItem)
253                        new InformationViewerDialog(BuddyList.getInstance()
254                        .getConnection().getUser(), true);
255                    else if (e.getSource() == searchItem) {
256                        searchHandler();
257                    }
258                }
259            }
260        }
261        
262        private void signOnHandler() {
263            if (!BuddyList.getInstance().checkConnection()) {
264                ConnectorThread.getInstance().setCancelled(false);
265                BuddyList.getInstance().setStatus(Presence.Mode.AVAILABLE,
266                resources.getString("available"), false);
267            } else
268                signOffHandler();
269        }
270        
271        private void signOffHandler() {
272            ConnectorThread.getInstance().setCancelled(true);
273            BuddyList.getInstance().getStatusMenu().signOffHandler();
274        }
275        
276        private void editHandler() {
277            ProfileEditorDialog dialog = new ProfileEditorDialog(BuddyList.getInstance().getContainerFrame(),null,
278            ProfileManager.getCurrentProfile());
279            dialog.setIsCurrentProfile(true);
280            dialog.setVisible(true);
281        }
282        
283        private void switchHandler() {
284            if (BuddyList.getInstance().checkConnection()) {
285                Standard.warningMessage(null, resources.getString("error"),
286                resources.getString("stillConnected"));
287            } else {
288                BuddyList.getInstance().getContainerFrame().setVisible(false);
289                new ProfileManager().setVisible(true);
290            }
291        }
292        
293        /**
294         * Allows you to search for a user
295         **/
296        private void searchHandler() {
297            
298            String d = Settings.getInstance().getProperty("defaultSearchService", "users.jabber.org");
299            
300            String result = (String) JOptionPane.showInputDialog(null, resources
301            .getString("pleaseEnterSearchServer"), resources
302            .getString("registerForService"), JOptionPane.QUESTION_MESSAGE,
303            null, null, d);  
304            
305            if(result != null && !result.equals(""))
306            {
307                Settings.getInstance().setProperty("defaultSearchService", d);
308                new SearchDialog(result);
309            }
310        }
311        
312        /**
313         * Registers for a server by displaying a RegistrationForm
314         */
315        private void registrationHandler() {
316            String result = (String) JOptionPane.showInputDialog(null, resources
317            .getString("pleaseEnterServer"), resources
318            .getString("registerForService"), JOptionPane.QUESTION_MESSAGE,
319            null, null, "");
320            
321            if (result != null && !result.equals("")) {
322                RegistrationForm form = new RegistrationForm(BuddyList.getInstance().getContainerFrame(),result);
323                form.getRegistrationInfo();
324            }
325        }
326        
327        public void showMenu( Component tree, int x, int y) {
328            show( tree, x, y );
329        }
330        
331        public void signOn() {
332            logOn();
333        }
334        
335        public void signOff() {
336            logOff();
337        }
338        
339        public JMenu getServicesMenu()
340        {
341            return servicesMenu;
342        }
343    }