Source code: plugins/Messenger/ChatFrameFactory.java
1 /*
2 This file is part of DeXter - Java Internet Communication Solution
3 Copyright (c) 2002 Tobias Riemer
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
13 GNU 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 program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 /*
21 * ChatFrameFactory.java
22 *
23 * Created on October 23, 2002, 3:02 PM
24 */
25
26 package plugins.Messenger;
27
28 import javax.swing.*;
29 import plugins.Messenger.entity.Smiley;
30 import plugins.Messenger.event.*;
31
32 /**
33 *
34 * @author Christoph Walcher
35 * @todo Remove differences between creating a Menu Bar and the ToolBox. sync these two Behaviors to have all two controls enabled or disabled in sync
36 *
37 */
38 public class ChatFrameFactory {
39
40 private static final java.util.Map iconMap = new java.util.HashMap();
41
42 /** Creates a new instance of ChatFrameFactory */
43 protected ChatFrameFactory() {
44 }
45
46
47 public static ChatFrame createChatFrame(PluginChain chain, MessengerEngine engine) {
48
49 ChatFrame chatFrame = new plugins.Messenger.ChatFrame();
50
51 // Register the plugichnChain to the ChatFrame
52 chatFrame.setConversation(chain);
53
54 // Add the chatFrame to the PluginChain as a ConversationListener
55 chain.addConversationListener(chatFrame);
56
57 // Create the Toolbox and Menu for the chatFrame Plugins
58 ChatFrameFactory.buildPluginControls(chatFrame.getJToolBox(), chatFrame.getJMenuBar(), chain);
59
60 // Generate the Action for the Smiley Button!
61 chatFrame.setSmileyButtonAction(ChatFrameFactory.createSmileyAction(chatFrame.getInputDocument()));
62
63 chatFrame.setIconImage(engine.getOnlineIcon().getImage());
64
65 return chatFrame;
66 }
67
68 /**
69 * @todo make ThreadSave and establish a better system for loading
70 */
71 public static synchronized java.util.Map createIconMap() {
72
73 if (iconMap.isEmpty()) {
74 try {
75 java.io.InputStream is = ChatFrameFactory.class.getClassLoader().getResourceAsStream("plugins/Messenger/emicons/emicons2.txt");
76
77 java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.InputStreamReader(is));
78
79 String line = null;
80 while ((line = reader.readLine()) != null) {
81
82 int idx = line.indexOf('=');
83 if (idx >= 0) {
84 Smiley smiley = new Smiley();
85
86 String iconPath = line.substring(idx + 1 , line.length()).trim();
87 ImageIcon icon = new ImageIcon(ChatFrameFactory.class.getClassLoader().getResource("plugins/Messenger/emicons/" + iconPath));
88 smiley.setIcon(icon);
89
90 String tokens = line.substring(0 , idx - 1);
91 java.util.StringTokenizer tokenizer = new java.util.StringTokenizer(tokens);
92
93 while (tokenizer.hasMoreTokens()) {
94 smiley.addMnemonic(tokenizer.nextToken());
95 }
96 iconMap.put(smiley.getMnemonic(), smiley);
97 }
98 }
99 }
100 catch (java.io.IOException ex) {
101 ex.printStackTrace();
102 }
103 }
104 return java.util.Collections.unmodifiableMap(iconMap);
105 }
106
107 public static JToggleButton createPluginButton(MessengerPlugin plugin) {
108
109 String name = plugin.getName();
110 javax.swing.Icon icon = plugin.getIcon();
111
112 Action pluginAction = new plugins.Messenger.ChatFrameFactory.PluginEnableDisableAction(name, icon, plugin);
113
114 JToggleButton jPluginEnDisable = new JToggleButton(pluginAction);
115 return jPluginEnDisable;
116 }
117
118
119 public static void buildPluginControls(javax.swing.JToolBar jToolBar, javax.swing.JMenuBar jMenuBar, PluginChain pluginChain) {
120
121 java.util.Iterator pluginIter = pluginChain.getPlugins().iterator();
122
123 javax.swing.JMenu jPluginMenu = new javax.swing.JMenu("Plugins");
124 jPluginMenu.setMnemonic('P');
125
126 while (pluginIter.hasNext()) {
127 MessengerPlugin plugin = (MessengerPlugin)pluginIter.next();
128
129 // Create Action
130 Action action = createPluginActivationAction(plugin);
131
132 JCheckBoxMenuItem jMenuItem = new JCheckBoxMenuItem(action);
133 JToggleButton jToggleButton = new JToggleButton(action);
134
135 // Create Model
136 plugins.Messenger.ChatFrameFactory.EnableDisableButtonModel model = new plugins.Messenger.ChatFrameFactory.EnableDisableButtonModel();
137
138 jMenuItem.setModel(model);
139 jToggleButton.setModel(model);
140
141 jToolBar.add(jToggleButton);
142 jPluginMenu.add(jMenuItem);
143
144 plugin.addStateListener(model);
145 }
146
147 jMenuBar.add(jPluginMenu);
148 }
149
150 public static Action createPluginActivationAction(MessengerPlugin plugin) {
151 String name = plugin.getName();
152 javax.swing.Icon icon = plugin.getIcon();
153
154 return new plugins.Messenger.ChatFrameFactory.PluginEnableDisableAction(name, icon, plugin);
155 }
156
157 /**
158 * @todo Refactor this code - it does not seem to be very handy and maintainable!!!
159 *
160 */
161 public static Action createSmileyAction(javax.swing.text.Document document) {
162 java.util.Map iconMap = createIconMap();
163
164 final javax.swing.JPanel panel = new javax.swing.JPanel();
165 panel.setBackground(java.awt.Color.white);
166 panel.setBorder(javax.swing.BorderFactory.createEmptyBorder());
167
168 ImageIcon buttonIcon = new ImageIcon(ChatFrameFactory.class.getClassLoader().getResource("plugins/Messenger/images/smileyButton.gif"));
169
170 int rows = iconMap.size() / 6;
171 panel.setLayout(new java.awt.GridLayout(rows, 6));
172
173 // Get mnemonics for Icons
174 java.util.Iterator smileyIter = iconMap.values().iterator();
175
176 while (smileyIter.hasNext()) {
177 Smiley smiley = (Smiley)smileyIter.next();
178
179 plugins.Messenger.ChatFrameFactory.SmileyInsertAction insertSmileyAction = new plugins.Messenger.ChatFrameFactory.SmileyInsertAction(smiley.getMnemonic(), smiley.getIcon());
180 insertSmileyAction.setInputDocument(document);
181
182 javax.swing.JButton smileyButton = new javax.swing.JButton(insertSmileyAction);
183 smileyButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(2, 2, 2, 2));
184 smileyButton.setFont(new java.awt.Font("Dialog", 0, 10));
185 smileyButton.setOpaque(false);
186 smileyButton.setToolTipText("Or type " + smiley.getMnemonic() + " in the input field");
187 panel.add(smileyButton);
188 }
189
190 final JPopupMenu popupWindow = new JPopupMenu();
191 popupWindow.add(panel);
192 popupWindow.pack();
193
194 return new plugins.Messenger.ChatFrameFactory.SmileyAction(buttonIcon, popupWindow);
195 }
196
197 protected static class PluginEnableDisableAction extends javax.swing.AbstractAction {
198
199 private MessengerPlugin plugin;
200
201 public PluginEnableDisableAction(String name, javax.swing.Icon icon, MessengerPlugin plugin) {
202 super(name, icon);
203
204 if (plugin == null) {
205 throw new IllegalArgumentException("MessengerPlugin must not be null");
206 }
207 this.plugin = plugin;
208 }
209
210 /** Invoked when an action occurs.
211 */
212 public void actionPerformed(java.awt.event.ActionEvent e) {
213 if (e.getSource() instanceof JToggleButton) {
214 JToggleButton myButton = (JToggleButton)e.getSource();
215 plugin.setEnabled(myButton.getModel().isSelected(), false);
216 }
217 if (e.getSource() instanceof JCheckBoxMenuItem) {
218 JCheckBoxMenuItem myMenuItem= (JCheckBoxMenuItem)e.getSource();
219 plugin.setEnabled(myMenuItem.getModel().isSelected(), false);
220 }
221 }
222 }
223
224 protected static class EnableDisableButtonModel extends javax.swing.JToggleButton.ToggleButtonModel
225 implements StateListener {
226
227 public void stateChanged(StateEvent e) {
228
229 MessengerPlugin plugin = ((MessengerPlugin)e.getSource());
230 super.setSelected(plugin.isEnabled());
231 }
232
233 }
234
235 protected static class SmileyInsertAction extends javax.swing.AbstractAction {
236 private String mnemonic;
237 private javax.swing.text.Document document;
238
239 public SmileyInsertAction(String mnemonic, ImageIcon icon) {
240 super("", icon);
241 this.mnemonic = mnemonic;
242 }
243
244 public void setInputDocument(javax.swing.text.Document document) {
245 this.document = document;
246 }
247
248 /** Invoked when an action occurs.
249 */
250 public void actionPerformed(java.awt.event.ActionEvent e) {
251 if (document != null) {
252 try {
253 int offset = document.getLength();
254 document.insertString(offset, " " + mnemonic, null);
255
256 if (e.getSource() instanceof JComponent) {
257 JComponent parent = (JComponent)e.getSource();
258 parent.getParent().getParent().setVisible(false);
259 }
260 }
261 catch (Exception ex) {
262 ex.printStackTrace();
263 }
264 }
265 }
266 }
267
268 protected static class SmileyAction extends javax.swing.AbstractAction {
269 private JPopupMenu popupMenu;
270
271 public SmileyAction(ImageIcon icon, JPopupMenu popupMenu) {
272 super("", icon);
273 this.popupMenu = popupMenu;
274
275 }
276
277 /** Invoked when an action occurs.
278 */
279 public void actionPerformed(java.awt.event.ActionEvent e) {
280 System.out.println("SmileyPanel should get on Top now!!!");
281 if (e.getSource() instanceof JComponent) {
282 JComponent invoker = (JComponent)e.getSource();
283 popupMenu.show(invoker, 0, 0);
284 }
285 }
286 }
287 }