KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > forumadmin > ForumAdmin


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2002 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.forumadmin;
20
21 import org.lucane.client.*;
22 import org.lucane.client.acl.AclEditor;
23 import org.lucane.client.widgets.*;
24 import org.lucane.common.*;
25 import org.lucane.common.acl.AclInfo;
26 import org.lucane.common.acl.AclProducer;
27 import org.lucane.common.net.ObjectConnection;
28
29 import java.awt.*;
30 import java.awt.event.*;
31 import java.util.ArrayList JavaDoc;
32 import javax.swing.*;
33
34 public class ForumAdmin
35 extends StandalonePlugin
36 implements ActionListener, AclProducer
37 {
38     
39     private ManagedWindow mainWindow;
40     private JButton btnCreate;
41     private JButton btnDelete;
42     private JButton btnACL;
43     private JButton btnClose;
44     private JList lstForums;
45     private ConnectInfo service;
46     
47     
48     public ForumAdmin()
49     {
50     }
51     
52     public Plugin newInstance(ConnectInfo[] friends)
53     {
54         return new ForumAdmin();
55     }
56     
57     public void start()
58     {
59         this.service = Communicator.getInstance().getConnectInfo("org.lucane.applications.forumadmin");
60         
61         mainWindow = new ManagedWindow(this, getTitle());
62         lstForums = new JList();
63         btnCreate = new JButton(tr("btn.create"));
64         btnDelete = new JButton(tr("btn.remove"));
65         btnACL = new JButton(tr("btn.accessControl"));
66         btnClose = new JButton(tr("btn.close"));
67         btnCreate.addActionListener(this);
68         btnDelete.addActionListener(this);
69         btnACL.addActionListener(this);
70         btnClose.addActionListener(this);
71         mainWindow.getContentPane().setLayout(new BorderLayout());
72         mainWindow.getContentPane().add(lstForums, BorderLayout.CENTER);
73         
74         
75         JPanel pnlBtns = new JPanel(new BorderLayout());
76         JPanel pnlTop = new JPanel(new GridLayout(0, 1));
77         pnlTop.add(btnCreate);
78         pnlTop.add(btnDelete);
79         pnlTop.add(btnACL);
80         pnlBtns.add(pnlTop, BorderLayout.NORTH);
81         pnlBtns.add(btnClose, BorderLayout.SOUTH);
82         mainWindow.getContentPane().add(pnlBtns, BorderLayout.EAST);
83         mainWindow.setPreferredSize(new Dimension(400, 300));
84         mainWindow.show();
85         getForumList();
86     }
87     
88     public void actionPerformed(ActionEvent ae)
89     {
90         if(ae.getSource() == btnCreate)
91             createForum();
92         else if(ae.getSource() == btnDelete)
93             deleteForum();
94         else if(ae.getSource() == btnClose)
95         {
96             mainWindow.dispose();
97             exit();
98         }
99         else if(ae.getSource() == btnACL)
100             editAcl();
101     }
102     
103     private void getForumList()
104     {
105         try
106         {
107             ForumAdminAction action = new ForumAdminAction(ForumAdminAction.LIST_FORUMS);
108             ObjectConnection oc = Communicator.getInstance().sendMessageTo(service,
109                     getName(), action);
110             
111             String JavaDoc ack = oc.readString();
112             if(ack.equals("OK"))
113             {
114                 ArrayList JavaDoc forums = (ArrayList JavaDoc)oc.read();
115                 lstForums.setListData(forums.toArray());
116             }
117             else
118                 DialogBox.error(ack);
119             
120             oc.close();
121         }
122         catch(Exception JavaDoc e)
123         {
124             DialogBox.error(tr("err.list"));
125         }
126     }
127     
128     private void createForum()
129     {
130         String JavaDoc forum = JOptionPane.showInputDialog(tr("msg.newForum"));
131         
132         if(forum == null || forum.equals(""))
133             return;
134         
135         try
136         {
137             ForumAdminAction action = new ForumAdminAction(ForumAdminAction.CREATE_FORUM, forum);
138             ObjectConnection oc = Communicator.getInstance().sendMessageTo(service,
139                     getName(), action);
140             
141             String JavaDoc ack = (String JavaDoc)oc.read();
142             oc.close();
143             
144             if(ack.equals("OK"))
145                 getForumList();
146             else
147                 DialogBox.error(ack);
148             
149         }
150         catch(Exception JavaDoc e)
151         {
152             DialogBox.error(tr("err.create"));
153         }
154     }
155     
156     private void deleteForum()
157     {
158         String JavaDoc forum = (String JavaDoc)lstForums.getSelectedValue();
159         
160         if(forum == null)
161             return;
162         
163         try
164         {
165             ForumAdminAction action = new ForumAdminAction(ForumAdminAction.DELETE_FORUM, forum);
166             
167             ObjectConnection oc = Communicator.getInstance().sendMessageTo(service,
168                     getName(), action);
169             String JavaDoc ack = (String JavaDoc)oc.read();
170             oc.close();
171             
172             if(ack.equals("OK"))
173                 getForumList();
174             else
175                 DialogBox.error(ack);
176         }
177         catch(Exception JavaDoc e)
178         {
179             DialogBox.error(tr("err.delete"));
180         }
181     }
182     
183     private void editAcl()
184     {
185         String JavaDoc forum = (String JavaDoc)lstForums.getSelectedValue();
186         
187         if(forum == null)
188             return;
189
190         try {
191             AclEditor editor = new AclEditor(this, this, forum);
192             editor.show();
193         } catch (Exception JavaDoc e) {
194             DialogBox.error(tr("err.unableToEditAcl"));
195             e.printStackTrace();
196         }
197     }
198     
199     public AclInfo[] getAcls(String JavaDoc forum)
200     throws Exception JavaDoc
201     {
202         AclInfo[] acl = null;
203         
204         ForumAdminAction action = new ForumAdminAction(ForumAdminAction.GET_ACLS, forum);
205         ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action);
206         String JavaDoc ack = (String JavaDoc)oc.read();
207         
208         if(ack.equals("OK"))
209         {
210             acl = (AclInfo[])oc.read();
211             oc.close();
212             
213             return acl;
214         }
215         oc.close();
216         throw new Exception JavaDoc(ack);
217     }
218     
219     public ArrayList JavaDoc getAllGroups()
220     throws Exception JavaDoc
221     {
222         ArrayList JavaDoc groups = null;
223         
224         ForumAdminAction action = new ForumAdminAction(ForumAdminAction.GET_GROUPS);
225         ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action);
226         String JavaDoc ack = (String JavaDoc)oc.read();
227         
228         if(ack.equals("OK"))
229         {
230             groups = (ArrayList JavaDoc)oc.read();
231             oc.close();
232             
233             return groups;
234         }
235         oc.close();
236         throw new Exception JavaDoc(ack);
237     }
238     
239     public ArrayList JavaDoc getAllUsers()
240     throws Exception JavaDoc
241     {
242         ArrayList JavaDoc users = null;
243         
244         ForumAdminAction action = new ForumAdminAction(ForumAdminAction.GET_USERS);
245         ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action);
246         String JavaDoc ack = (String JavaDoc)oc.read();
247         
248         if(ack.equals("OK"))
249         {
250             users = (ArrayList JavaDoc)oc.read();
251             oc.close();
252             
253             return users;
254         }
255         oc.close();
256         throw new Exception JavaDoc(ack);
257     }
258     
259     public void addAcl(String JavaDoc forum, AclInfo info)
260     throws Exception JavaDoc
261     {
262         ForumAdminAction action = new ForumAdminAction(ForumAdminAction.ADD_ACL, forum, info);
263         ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action);
264         String JavaDoc ack = (String JavaDoc)oc.read();
265         oc.close();
266         
267         if(!ack.equals("OK"))
268             throw new Exception JavaDoc(ack);
269     }
270     
271     public void removeAcl(String JavaDoc forum, AclInfo info)
272     throws Exception JavaDoc
273     {
274         ForumAdminAction action = new ForumAdminAction(ForumAdminAction.REMOVE_ACL, forum, info);
275         ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action);
276         String JavaDoc ack = (String JavaDoc)oc.read();
277         oc.close();
278         
279         if(!ack.equals("OK"))
280             throw new Exception JavaDoc(ack);
281     }
282
283     //introduced for acl webapp
284
public String JavaDoc[] getAccesses() {
285         return new String JavaDoc[] {"Moderate", "Write", "Read"};
286     }
287
288     public String JavaDoc getItemName(String JavaDoc itemId) {
289         return itemId;
290     }
291 }
292
Popular Tags