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.event.ActionEvent;
022 import java.awt.event.ActionListener;
023 import java.io.File;
024 import java.io.FileWriter;
025 import java.io.IOException;
026 import java.io.PrintWriter;
027 import java.util.Iterator;
028 import java.util.Locale;
029 import java.util.ResourceBundle;
030
031 import javax.swing.*;
032
033 import com.valhalla.gui.Standard;
034 import com.valhalla.jbother.*;
035 import org.jivesoftware.smack.Roster;
036
037 /**
038 * The menu that pops up when you right click on a ChatPanel
039 *
040 * @author Adam Olsen
041 * @version 1.0
042 */
043 public class ConversationPopupMenu extends JPopupMenu {
044 private ResourceBundle resources = ResourceBundle.getBundle(
045 "JBotherBundle", Locale.getDefault());
046
047 private ConversationPanel window;
048
049 private ConversationArea area;
050
051 private JMenuItem log = new JMenuItem(resources.getString("viewLog"));
052
053 private JMenuItem clear = new JMenuItem(resources.getString("clearWindow"));
054
055 private JMenuItem block = new JMenuItem(resources.getString("blockUser"));
056
057 private JMenuItem addperson = new JMenuItem(resources.getString("addBuddy"));
058
059 private JMenuItem events = new JMenuItem(resources.getString("messageEvents"));
060
061 protected JMenuItem sendFileItem = new JMenuItem(resources.getString("sendFile"));
062 protected JMenu sendFileMenu = new JMenu(resources.getString("sendFile"));
063
064 /**
065 * Sets up the menu
066 *
067 * @param window
068 * the window to attach this menu to
069 * @param area
070 * the conversation area to attach this menu to
071 */
072 public ConversationPopupMenu(final ConversationPanel window,
073 final ConversationArea area) {
074 this.window = window;
075 this.area = area;
076
077 initComponents();
078 }
079
080 public void disableBlock() {
081 block.setEnabled(false);
082 }
083
084 /**
085 * Sets up the visuals and event listeners
086 */
087 private void initComponents() {
088 add(log);
089 add(clear);
090 add(block);
091 add(events);
092 add(sendFileItem);
093
094 final String userId = window.getBuddy().getUser();
095
096 try {
097 Roster are = BuddyList.getInstance().getConnection().getRoster();
098 if ( !( window instanceof ConsolePanel ) && !are.contains( userId ) ) {
099 add(addperson);
100 }
101 }
102 catch( NullPointerException ex ){ }
103
104 log.addActionListener(new ActionListener() {
105 public void actionPerformed(ActionEvent e) {
106 window.openLogWindow();
107 }
108 });
109
110 clear.addActionListener(new ActionListener() {
111 public void actionPerformed(ActionEvent e) {
112 area.setText("");
113 }
114 });
115
116 addperson.addActionListener(new ActionListener() {
117 public void actionPerformed(ActionEvent e) {
118 // Create AddBuddyDialog window.
119 AddBuddyDialog addUser = new AddBuddyDialog();
120 addUser.setVisible(true);
121
122 // Set text of user in buddyIDBox.
123 addUser.setBuddyId( userId );
124 }
125 });
126
127 events.addActionListener(
128 new ActionListener() {
129 public void actionPerformed(ActionEvent e) {
130 JFrame f = window.getFrame();
131 if (f == null) {
132 f = BuddyList.getInstance().getTabFrame();
133 }
134 Events events = new Events();
135 events.setEvents(f, window.getBuddy());
136 }
137 });
138
139 block.addActionListener(new BlockActionListener());
140
141 // Stellaris did this with help from synic
142 sendFileItem.addActionListener(new ActionListener(){
143 public void actionPerformed(ActionEvent e) {
144 new FileSendDialog(window.getBuddy().getAddress());
145 }
146 });
147 }
148
149
150 /**
151 * Adds a user to the blocked users list
152 *
153 * @author Adam Olsen
154 * @version 1.0
155 */
156 class BlockActionListener implements ActionListener {
157 public void actionPerformed(ActionEvent e) {
158 // just adds the user to the blocked list in buddy list
159 File blockedFile = new File(JBother.profileDir + File.separator
160 + "blocked");
161 BuddyList.getInstance().getBlockedUsers().put(
162 window.getBuddy().getUser(), "blocked");
163
164 // and then writes all of them to the blocked users file
165 try {
166 FileWriter fw = new FileWriter(blockedFile);
167 PrintWriter out = new PrintWriter(fw);
168
169 Iterator i = BuddyList.getInstance().getBlockedUsers().keySet()
170 .iterator();
171 while (i.hasNext()) {
172 out.println((String) i.next());
173 }
174
175 fw.close();
176 } catch (IOException ex) {
177 Standard.warningMessage(window, resources
178 .getString("blockUser"), resources
179 .getString("problemWritingBlockedFile"));
180 return;
181 }
182
183 BuddyList.getInstance().getBuddyListTree().removeBuddy(
184 window.getBuddy(), window.getBuddy().getGroup(), true);
185
186 Standard.noticeMessage(window, resources.getString("blockUser"),
187 resources.getString("userHasBeenBlocked"));
188 }
189 }
190 }