KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > administrator > gui > MessagePanel


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2003 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.administrator.gui;
20
21 import javax.swing.*;
22 import java.awt.*;
23 import java.awt.event.*;
24 import java.util.*;
25 import java.util.List JavaDoc;
26
27 import org.lucane.client.Communicator;
28 import org.lucane.client.widgets.*;
29 import org.lucane.applications.administrator.AdministratorPlugin;
30 import org.lucane.common.*;
31 import org.lucane.common.concepts.*;
32 import org.lucane.common.net.ObjectConnection;
33
34 public class MessagePanel extends JPanel
35 implements ActionListener
36 {
37     private static final String JavaDoc QUICKMESSAGE = "org.lucane.applications.quickmessage";
38     
39     private transient AdministratorPlugin plugin;
40
41     private HTMLEditor message;
42
43     private JButton sendToAll;
44     private JButton sendToGroup;
45     private JButton sendToPlugin;
46     private JButton sendToService;
47
48     private JComboBox comboGroup;
49     private JComboBox comboPlugin;
50     private JComboBox comboService;
51
52     public MessagePanel(AdministratorPlugin plugin)
53     {
54         super(new BorderLayout());
55         this.plugin = plugin;
56
57         this.message = new HTMLEditor();
58         this.sendToAll = new JButton(tr("send"));
59         this.sendToAll.addActionListener(this);
60         this.sendToGroup = new JButton(tr("send"));
61         this.sendToGroup.addActionListener(this);
62         this.sendToPlugin = new JButton(tr("send"));
63         this.sendToPlugin.addActionListener(this);
64         this.sendToService = new JButton(tr("send"));
65         this.sendToService.addActionListener(this);
66         
67         this.comboGroup = new JComboBox();
68         this.comboPlugin = new JComboBox();
69         this.comboService = new JComboBox();
70
71         comboGroup.setModel(new DefaultComboBoxModel(plugin.getAllGroups(false).toArray()));
72         comboPlugin.setModel(new DefaultComboBoxModel(plugin.getAllPlugins(false).toArray()));
73         comboService.setModel(new DefaultComboBoxModel(plugin.getAllServices(false).toArray()));
74     
75         initLayout();
76     }
77
78     private void initLayout()
79     {
80         JPanel actions = new JPanel(new GridBagLayout());
81         GridBagConstraints constraints = new GridBagConstraints();
82         constraints.fill = GridBagConstraints.BOTH;
83         constraints.insets = new Insets(1, 1, 1, 1);
84
85         constraints.gridy = 0;
86         constraints.gridx = 0;
87         constraints.weightx = 1;
88         constraints.gridwidth = 2;
89         actions.add(new JLabel(tr("send.toAll")), constraints);
90         constraints.gridwidth = 1;
91         constraints.gridx = 2;
92         constraints.weightx = 0.2;
93         actions.add(sendToAll, constraints);
94         constraints.gridy++;
95         constraints.gridx = 0;
96         constraints.weightx = 1;
97         actions.add(new JLabel(tr("send.toGroup")), constraints);
98         constraints.gridx = 1;
99         actions.add(comboGroup, constraints);
100         constraints.gridx = 2;
101         constraints.weightx = 0.2;
102         actions.add(sendToGroup, constraints);
103         constraints.gridy++;
104         constraints.gridx = 0;
105         constraints.weightx = 1;
106         actions.add(new JLabel(tr("send.toPlugin")), constraints);
107         constraints.gridx = 1;
108         actions.add(comboPlugin, constraints);
109         constraints.gridx = 2;
110         constraints.weightx = 0.2;
111         actions.add(sendToPlugin, constraints);
112         constraints.gridy++;
113         constraints.gridx = 0;
114         constraints.weightx = 1;
115         actions.add(new JLabel(tr("send.toService")), constraints);
116         constraints.gridx = 1;
117         actions.add(comboService, constraints);
118         constraints.gridx = 2;
119         constraints.weightx = 0.2;
120         actions.add(sendToService, constraints);
121
122         add(message, BorderLayout.CENTER);
123         add(actions, BorderLayout.SOUTH);
124     }
125
126     public void actionPerformed(ActionEvent ae)
127     {
128         if(ae.getSource().equals(sendToAll))
129         {
130             List JavaDoc users = plugin.getAllConnectedUsers();
131             sendToUsers(users);
132         }
133         else if(ae.getSource().equals(sendToGroup))
134         {
135             GroupConcept concept = (GroupConcept)comboGroup.getSelectedItem();
136             ArrayList users = plugin.getConnectedUsersForConcept(concept);
137             sendToUsers(users);
138         }
139         else if(ae.getSource().equals(sendToPlugin))
140         {
141             PluginConcept concept = (PluginConcept)comboPlugin.getSelectedItem();
142             ArrayList users = plugin.getConnectedUsersForConcept(concept);
143             sendToUsers(users);
144         }
145         else if(ae.getSource().equals(sendToService))
146         {
147             ServiceConcept concept = (ServiceConcept)comboService.getSelectedItem();
148             ArrayList users = plugin.getConnectedUsersForConcept(concept);
149             sendToUsers(users);
150         }
151     }
152
153     private void sendToUsers(List JavaDoc users)
154     {
155         Logging.getLogger().info("sending message to " + users);
156         Iterator i = users.iterator();
157         Communicator com = Communicator.getInstance();
158         while(i.hasNext())
159         {
160             ConnectInfo ci = com.getConnectInfo((String JavaDoc)i.next());
161             ObjectConnection oc = com.sendMessageTo(ci, QUICKMESSAGE, message.getText());
162             oc.close();
163         }
164         DialogBox.info(tr("msg.sentToUsers", users.size()));
165     }
166
167     private String JavaDoc tr(String JavaDoc s, int i)
168     {
169         return tr(s).replaceAll("%1", ""+ i);
170     }
171     
172     private String JavaDoc tr(String JavaDoc s)
173     {
174         return plugin.tr(s);
175     }
176 }
Popular Tags