001: /*
002: * Lucane - a collaborative platform
003: * Copyright (C) 2005 Vincent Fiack <vfiack@mail15.com>
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package org.lucane.client.acl;
020:
021: import java.awt.BorderLayout;
022: import java.awt.Dimension;
023: import java.awt.GridLayout;
024: import java.awt.event.ActionEvent;
025: import java.awt.event.ActionListener;
026: import java.util.*;
027:
028: import javax.swing.*;
029: import javax.swing.table.DefaultTableModel;
030: import javax.swing.event.ListSelectionListener;
031: import javax.swing.event.ListSelectionEvent;
032:
033: import org.lucane.client.widgets.ManagedWindow;
034: import org.lucane.client.widgets.DialogBox;
035: import org.lucane.client.Plugin;
036: import org.lucane.client.Client;
037: import org.lucane.client.util.Translation;
038: import org.lucane.common.acl.AclInfo;
039: import org.lucane.common.acl.AclProducer;
040: import org.lucane.common.acl.ConceptComparator;
041: import org.lucane.common.concepts.Concept;
042: import org.lucane.common.concepts.GroupConcept;
043: import org.lucane.common.concepts.UserConcept;
044:
045: public class AclEditor extends ManagedWindow implements ActionListener,
046: ListSelectionListener {
047: private Plugin plugin;
048: private AclProducer producer;
049: private String itemId;
050: private String[] accesses;
051: private Map aclInfos;
052:
053: private AclRenderer renderer;
054:
055: private JList elements;
056: private JButton addGroup;
057: private JButton addUser;
058: private JButton removeAllow;
059:
060: private JSplitPane split;
061:
062: private JTable permissions;
063: private JButton btnAccept;
064:
065: public AclEditor(Plugin plugin, AclProducer producer, String itemId)
066: throws Exception {
067: super (plugin, Translation.tr("acl.msg.accessControl") + ' '
068: + producer.getItemName(itemId));
069:
070: this .plugin = plugin;
071: this .producer = producer;
072: this .itemId = itemId;
073: this .accesses = producer.getAccesses();
074: this .aclInfos = new HashMap();
075:
076: setPreferredSize(new Dimension(400, 400));
077: setName("ACLEditor");
078:
079: //load acls from producer
080: AclInfo[] infos = producer.getAcls(itemId);
081: for (int i = 0; i < infos.length; i++) {
082: AclInfo info = infos[i];
083:
084: //get concept (map key)
085: Concept concept;
086: if (info.getGroup() != null)
087: concept = new GroupConcept(info.getGroup());
088: else
089: concept = new UserConcept(info.getUser());
090:
091: //set initial list if not yet in the map
092: addAclInfoToMap(concept, info);
093: }
094:
095: renderer = new AclRenderer();
096: JPanel elements = createElementsPanel();
097:
098: permissions = new JTable();
099:
100: split = new JSplitPane(JSplitPane.VERTICAL_SPLIT, elements,
101: new JScrollPane(permissions));
102: split.setDividerLocation(220);
103: manageWidget(split);
104: getContentPane().add(split, BorderLayout.CENTER);
105:
106: btnAccept = new JButton(Translation.tr("acl.btn.accept"),
107: Client.getImageIcon("ok.png"));
108: btnAccept.addActionListener(this );
109: JPanel buttons = new JPanel(new BorderLayout());
110: buttons.add(btnAccept, BorderLayout.EAST);
111: getContentPane().add(buttons, BorderLayout.SOUTH);
112:
113: refreshElementList();
114: }
115:
116: private JPanel createElementsPanel() {
117: this .elements = new JList(new DefaultListModel());
118: this .elements.setCellRenderer(renderer);
119: this .elements.addListSelectionListener(this );
120: this .addGroup = new JButton(Translation.tr("acl.btn.addGroup"));
121: this .addGroup.addActionListener(this );
122: this .addUser = new JButton(Translation.tr("acl.btn.addUser"));
123: this .addUser.addActionListener(this );
124: this .removeAllow = new JButton(Translation.tr("acl.btn.remove"));
125: this .removeAllow.addActionListener(this );
126:
127: JPanel allows = new JPanel(new BorderLayout());
128: allows.add(new JScrollPane(elements), BorderLayout.CENTER);
129:
130: JPanel buttons = new JPanel(new GridLayout(3, 1));
131: buttons.add(addGroup);
132: buttons.add(addUser);
133: buttons.add(removeAllow);
134:
135: JPanel container = new JPanel(new BorderLayout());
136: container.add(buttons, BorderLayout.NORTH);
137:
138: allows.add(container, BorderLayout.EAST);
139:
140: return allows;
141: }
142:
143: public void actionPerformed(ActionEvent ae) {
144: if (ae.getSource() == addGroup)
145: addGroup();
146: else if (ae.getSource() == addUser)
147: selectUser();
148: else if (ae.getSource() == removeAllow)
149: removeSelected();
150: else if (ae.getSource() == btnAccept)
151: dispose();
152: }
153:
154: public void valueChanged(ListSelectionEvent e) {
155: Concept selected = (Concept) elements.getSelectedValue();
156: if (selected == null)
157: permissions.setModel(new DefaultTableModel());
158: else {
159: ArrayList infos = (ArrayList) aclInfos.get(selected);
160: permissions.setModel(new AclTableModel(plugin, this ,
161: accesses, infos));
162: }
163: }
164:
165: private void addGroup() {
166: String group = null;
167: try {
168: ArrayList list = producer.getAllGroups();
169: int index = DialogBox.list(null, Translation
170: .tr("acl.msg.selectUser"), Translation
171: .tr("acl.msg.selectGroup"), new Vector(list));
172: if (index >= 0)
173: group = (String) list.get(index);
174: } catch (Exception e) {
175: DialogBox.error(Translation
176: .tr("acl.err.unableToListGroups")
177: + e);
178: }
179:
180: if (group != null) {
181: Concept concept = new GroupConcept(group);
182: aclInfos.put(concept, new ArrayList());
183: refreshElementList();
184: }
185: }
186:
187: private void selectUser() {
188: String user = null;
189: try {
190: ArrayList list = producer.getAllUsers();
191: int index = DialogBox.list(null, Translation
192: .tr("acl.msg.selectUser"), Translation
193: .tr("acl.msg.selectUser"), new Vector(list));
194: if (index >= 0)
195: user = (String) list.get(index);
196: } catch (Exception e) {
197: DialogBox.error(Translation.tr("acl.err.unableToListUsers")
198: + e);
199: }
200:
201: if (user != null) {
202: Concept concept = new UserConcept(user);
203: aclInfos.put(concept, new ArrayList());
204: refreshElementList();
205: }
206: }
207:
208: private void removeSelected() {
209: Concept selected = (Concept) elements.getSelectedValue();
210: if (selected == null)
211: return;
212:
213: ArrayList infos = (ArrayList) aclInfos.get(selected);
214: Iterator i = infos.iterator();
215: while (i.hasNext()) {
216: AclInfo info = (AclInfo) i.next();
217: try {
218: producer.removeAcl(itemId, info);
219: } catch (Exception e) {
220: DialogBox.error(Translation
221: .tr("acl.err.unableToRemoveAccessRule"));
222: e.printStackTrace();
223: return;
224: }
225: }
226: aclInfos.remove(selected);
227: refreshElementList();
228: }
229:
230: private void refreshElementList() {
231: ArrayList concepts = new ArrayList(aclInfos.keySet());
232: Collections.sort(concepts, new ConceptComparator());
233: DefaultListModel model = (DefaultListModel) elements.getModel();
234: model.clear();
235:
236: Iterator i = concepts.iterator();
237: while (i.hasNext())
238: model.addElement(i.next());
239: }
240:
241: public void allowAccess(String access) {
242: try {
243: addAclInfo(access, true);
244: } catch (Exception e) {
245: DialogBox.error(Translation
246: .tr("acl.err.unableToAllowAccess"));
247: e.printStackTrace();
248: }
249: }
250:
251: public void denyAccess(String access) {
252: try {
253: addAclInfo(access, false);
254: } catch (Exception e) {
255: DialogBox.error(Translation
256: .tr("acl.err.unableToDenyAccess"));
257: e.printStackTrace();
258: }
259: }
260:
261: public void removeAccess(String access) {
262: Concept selected = (Concept) elements.getSelectedValue();
263: if (selected == null)
264: return;
265:
266: String group = null;
267: String user = null;
268:
269: if (selected instanceof GroupConcept)
270: group = selected.getName();
271: else if (selected instanceof UserConcept)
272: user = selected.getName();
273:
274: AclInfo info = new AclInfo(access, true, group, user);
275: try {
276: producer.removeAcl(itemId, info);
277: removeAclInfoFromMap(selected, info);
278: refreshElementList();
279: elements.setSelectedValue(selected, true);
280: valueChanged(null);
281: } catch (Exception e) {
282: DialogBox.error(Translation
283: .tr("acl.err.unableToRemoveAccessRule"));
284: e.printStackTrace();
285: }
286: }
287:
288: private void addAclInfo(String access, boolean allow)
289: throws Exception {
290: Concept selected = (Concept) elements.getSelectedValue();
291: if (selected == null)
292: return;
293:
294: String group = null;
295: String user = null;
296:
297: if (selected instanceof GroupConcept)
298: group = selected.getName();
299: else if (selected instanceof UserConcept)
300: user = selected.getName();
301:
302: AclInfo info = new AclInfo(access, allow, group, user);
303: producer.addAcl(itemId, info);
304:
305: removeAclInfoFromMap(selected, info);
306: addAclInfoToMap(selected, info);
307: refreshElementList();
308: elements.setSelectedValue(selected, true);
309: valueChanged(null);
310: }
311:
312: private void addAclInfoToMap(Concept concept, AclInfo info) {
313: ArrayList list = (ArrayList) aclInfos.get(concept);
314: if (list == null) {
315: list = new ArrayList();
316: aclInfos.put(concept, list);
317: }
318:
319: list.add(info);
320: }
321:
322: private void removeAclInfoFromMap(Concept concept, AclInfo info) {
323: ArrayList list = (ArrayList) aclInfos.get(concept);
324: list.remove(info);
325: info = new AclInfo(info.getAccess(), !info.isAllow(), info
326: .getGroup(), info.getUser());
327: list.remove(info);
328: }
329: }
|